import { Dobby } from "https://esm.town/v/yawnxyz/dobby";
export class Eventsy {
private dobby: Dobby;
private namespace: string;
private databaseName: string;
private indexes: { name: string; columns: string[] }[];
private ftsIndexes: { name: string; columns: string[] }[];
constructor({
namespace,
databaseName,
indexes = [],
ftsIndexes = []
}: {
namespace: string;
databaseName: string;
indexes?: { name: string; columns: string[] }[];
ftsIndexes?: { name: string; columns: string[] }[];
}) {
this.namespace = namespace;
this.databaseName = databaseName;
this.indexes = indexes;
this.ftsIndexes = ftsIndexes;
this.dobby = new Dobby(
databaseName,
[
{ name: 'id', type: 'INTEGER', primaryKey: true },
{ name: 'type', type: 'TEXT', notNull: true },
{ name: 'aggregateId', type: 'TEXT' },
{ name: 'description', type: 'TEXT' },
{ name: 'eventData', type: 'JSON', notNull: true },
{ name: 'timestamp', type: 'INTEGER', notNull: true },
{ name: 'version', type: 'INTEGER', notNull: true },
{ name: 'namespace', type: 'TEXT' },
{ name: 'owner', type: 'TEXT' },
{ name: 'schema', type: 'JSON' },
{ name: 'collections', type: 'JSONARRAY' },
],
indexes,
ftsIndexes
);
this.initializeDatabase();
}
private async initializeDatabase() {
await this.dobby.createDatabase();
}
async logEvent({
type,
aggregateId,
description,
eventData,
version = 1
}: {
type: string,
aggregateId?: string,
description?: string,
eventData: object,
version?: number
}): Promise<void> {
const event = {
type,
aggregateId,