Public
Script
Readme

The Eventsy class represents a system for logging and managing events. It utilizes Dobby for database operations and provides methods for logging events, retrieving events by type, performing full-text searches, and more. This class is designed to be flexible and adaptable to various event logging needs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* The Eventsy class represents a system for logging and managing events. It utilizes Dobby for database operations and provides methods for logging events, retrieving events by type, performing full-text searches, and more. This class is designed to be fl
*/
import { Dobby } from "https://esm.town/v/yawnxyz/dobby";
/**
* Represents an events logger system.
*/
export class Eventsy {
private dobby: Dobby;
private namespace: string;
private databaseName: string;
private indexes: { name: string; columns: string[] }[];
private ftsIndexes: { name: string; columns: string[] }[];
/**
* Initializes a new instance of the Eventsy class.
*
* @param {Object} options - The options for initializing the Eventsy instance.
* @param {string} options.namespace - The namespace for the events.
* @param {string} options.databaseName - The name of the database.
* @param {{ name: string; columns: string[] }[]} options.indexes - The indexes for the database.
* @param {{ name: string; columns: string[] }[]} options.ftsIndexes - The full-text search indexes for the database.
*/
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;
// Initialize Dobby with the necessary tables, indexes, and FTS indexes
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
);
// Initialize the database and create indexes
this.initializeDatabase();
}
/**
* Initializes the database and creates indexes.
*/
private async initializeDatabase() {
await this.dobby.createDatabase();
// Additional setup if needed
}
/**
* Logs an event.
*
* @param {{ type: string; aggregateId?: string; description?: string; eventData: object; version?: number }} options - The options for logging the event.
* @param {string} options.type - The type of the event.
* @param {string} [options.aggregateId] - The aggregate ID of the event.
* @param {string} [options.description] - The description of the event.
* @param {object} options.eventData - The data of the event.
* @param {number} [options.version=1] - The version of the event.
* @returns {Promise<void>} A promise that resolves when the event is logged.
*/
async logEvent({
type,
aggregateId,
description,
eventData,
version = 1
}: {
type: string,
aggregateId?: string,
description?: string,
eventData: object,
version?: number
}): Promise<void> {
const event = {
type,
aggregateId,
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
September 9, 2024