# Esquemas de Base de Datos

Respetando la mantenibilidad como requerimiento no funcional, el microservicio debe ser responsable de guardar en base de datos los eventos originales, en un esquema desacoplado al esquema del proveedor, con el fin de evitar refactorizaciones en caso en que se cambie la fuente de suscripción del webhook.

La base de datos encargada de persistir los eventos originales contará con solo una tabla encargada de generalizar en una misma interface, el almacenamiento de eventos de origen de diferentes proveedores:

```typescript
interface MetaSportBookEvent{
    createAt:Date,
    providerName: string,
    providerEventHeader: Record<string, unknown> | null,
}

interface SportBookEvent {
    metaData:MetaSportBookEvent
    body: Record<string, unknown>
}
```

### Ejemplo de aplicación:

En este caso la interface SportBookEvent busca separar los datos propios del evento, de información que permita determinar el proveedor del evento almacenado y las cabeceras adicionales que este anexe a los eventos emitidos.

Pueden haber distintas implementaciones para diferenciar datos de metadatos de acuerdo al proveedor, mediante una tabla de proveedores o como en el ejemplo con un patrón strategy.

```typescript
const event: SportBookEvent = new EventManager(ProviderX).transformEvent(providerEvent)
const eventStored = new SportBookRepository().save(event)

console.log(eventStored)

// {
//     header: {
//       createAt: new Date("2024-08-21T14:30:00Z"),
//       providerName: "ProviderX",
//       providerEventHeader: {
//         eventId: 12345,
//         sport: "Soccer",
//         league: "Premier League",
//         match: "Team A vs Team B",
//         location: "Stadium XYZ",
//         startTime: "2024-08-21T16:00:00Z"
//       }
//     },
//     body: {
//       odds: {
//         teamA: 1.8,
//         teamB: 2.0,
//         draw: 3.5
//       },
//       status: "Scheduled",
//       markets: [
//         {
//           marketName: "Match Result",
//           selections: [
//             { selectionName: "Team A", odds: 1.8 },
//             { selectionName: "Team B", odds: 2.0 },
//             { selectionName: "Draw", odds: 3.5 }
//           ]
//         },
//         {
//           marketName: "Total Goals",
//           selections: [
//             { selectionName: "Over 2.5", odds: 2.1 },
//             { selectionName: "Under 2.5", odds: 1.7 }
//           ]
//         }
//       ]
//     }
//   };
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-affiliates.inlaze.com/gaming-docs/architecture/sistema-de-sportsbook/estructura/webhook/esquemas-de-base-de-datos.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
