defuss-db 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,243 @@
1
+ import { DefussProvider, DefussRecord, PrimaryKeyValue, RecordValue } from './index.js';
2
+ export { DefussTable } from './index.js';
3
+ import { Config, Client } from '@libsql/client';
4
+ import { MongoClient, Db, Document, Collection, ObjectId } from 'mongodb';
5
+
6
+ type LibsqlProviderOptions = Config;
7
+ /**
8
+ * LibsqlProvider implements DefussProvider using libsql for SQLite databases.
9
+ */
10
+ declare class LibsqlProvider implements DefussProvider<Config> {
11
+ db: Client;
12
+ /**
13
+ * Connects to the SQLite database using libsql.
14
+ * @param config - The libsql connection configuration.
15
+ */
16
+ connect(config: Config): Promise<void>;
17
+ /**
18
+ * Disconnects from the SQLite database.
19
+ */
20
+ disconnect(): Promise<void>;
21
+ /**
22
+ * Creates a table if it doesn't exist.
23
+ * Sets up the primary key and any initial structure needed.
24
+ * @param table - The name of the table.
25
+ */
26
+ createTable(table: string): Promise<void>;
27
+ /**
28
+ * Ensures all necessary columns exist in the table
29
+ * @param table - The name of the table
30
+ * @param record - DefussRecord containing fields to ensure
31
+ */
32
+ private ensureColumnsExist;
33
+ /**
34
+ * Gets the SQLite data type for a JavaScript value
35
+ * @param value The JavaScript value to map to SQLite type
36
+ * @returns The SQLite type string
37
+ */
38
+ private getSqliteType;
39
+ /**
40
+ * Retrieves existing columns in the specified table.
41
+ * @param table - The name of the table.
42
+ * @returns An array of column names.
43
+ */
44
+ private getExistingColumns;
45
+ /**
46
+ * Converts a DefussRecord to SQLite values
47
+ * @param record The DefussRecord to convert
48
+ * @returns Object with column names and values for SQLite
49
+ */
50
+ private toSqliteValues;
51
+ /**
52
+ * Convert JavaScript value to SQLite-compatible value
53
+ */
54
+ private convertValueForSqlite;
55
+ /**
56
+ * Convert SQLite row to DefussRecord
57
+ */
58
+ private fromSqliteRow;
59
+ /**
60
+ * Inserts a record into the table.
61
+ * @param table - The name of the table.
62
+ * @param record - The record to insert.
63
+ * @returns The primary key of the inserted record.
64
+ */
65
+ insert<T extends DefussRecord>(table: string, record: T): Promise<PrimaryKeyValue>;
66
+ /**
67
+ * Finds records in the table based on query.
68
+ * @param table - The name of the table.
69
+ * @param query - Query criteria.
70
+ * @returns An array of matching records.
71
+ */
72
+ find<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>): Promise<T[]>;
73
+ /**
74
+ * Finds a single record in the table based on query.
75
+ * @param table - The name of the table.
76
+ * @param query - Query criteria.
77
+ * @returns The first matching record or null.
78
+ */
79
+ findOne<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>): Promise<T | null>;
80
+ /**
81
+ * Updates records in the table based on query.
82
+ * @param table - The name of the table.
83
+ * @param query - Query criteria.
84
+ * @param update - Fields to update.
85
+ */
86
+ update<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>, update: Partial<T>): Promise<void>;
87
+ /**
88
+ * Deletes records from the table based on query.
89
+ * @param table - The name of the table.
90
+ * @param query - Query criteria.
91
+ */
92
+ delete(table: string, query: Partial<DefussRecord>): Promise<void>;
93
+ /**
94
+ * Upserts a record into the table based on query.
95
+ * @param table - The name of the table.
96
+ * @param record - The record to upsert.
97
+ * @param query - Query criteria.
98
+ * @returns The primary key of the upserted record.
99
+ */
100
+ upsert<T extends DefussRecord>(table: string, record: T, query: Partial<Record<string, RecordValue>>): Promise<PrimaryKeyValue>;
101
+ /**
102
+ * Ensures that the specified table exists.
103
+ * @param table - The name of the table.
104
+ */
105
+ private ensureTableExists;
106
+ }
107
+
108
+ interface MongoProviderOptions {
109
+ connectionString: string;
110
+ databaseName: string;
111
+ }
112
+ type DbClientMap = {
113
+ [clientKey: string]: MongoClient | null;
114
+ };
115
+ type DbMap = {
116
+ [dbName: string]: Db | null;
117
+ };
118
+ /**
119
+ * Parses a MongoDB connection string into its components
120
+ * @param connectionString - MongoDB connection string
121
+ * @returns Object containing databaseName and clientKey
122
+ */
123
+ declare const parseMongoConnectionString: (connectionString: string) => {
124
+ databaseName: string;
125
+ clientKey: string;
126
+ };
127
+ /**
128
+ * Core connection function to be used by all parts of the system
129
+ * @param connectionString MongoDB connection string
130
+ * @returns Object containing the MongoClient and Db instances
131
+ */
132
+ declare function connectToMongo(connectionString: string): Promise<{
133
+ client: MongoClient;
134
+ db: Db;
135
+ }>;
136
+ /** Returns true if both dbClient and database are initialized */
137
+ declare const isConnected: (connectionString: string) => boolean;
138
+ /**
139
+ * Pings the MongoDB server to check connectivity
140
+ * @param connectionString MongoDB connection string
141
+ */
142
+ declare const ping: (connectionString: string) => Promise<unknown>;
143
+ /**
144
+ * Gets a collection by name, connecting if necessary
145
+ */
146
+ declare const getCollectionByName: <T extends Document>(collectionName: string, connectionString: string) => Promise<Collection<T>>;
147
+ declare function isValidObjectId(id: string): boolean;
148
+ declare const toObjectId: (id: string | ObjectId) => ObjectId;
149
+ interface MongoDefussDocument extends Document {
150
+ type?: "json" | "blob";
151
+ json?: string;
152
+ blob?: ArrayBuffer;
153
+ [key: string]: any;
154
+ }
155
+ /**
156
+ * MongoProvider implements DefussProvider for MongoDB.
157
+ */
158
+ declare class MongoProvider implements DefussProvider<MongoProviderOptions> {
159
+ client: MongoClient | null;
160
+ db: Db | null;
161
+ collections: Map<string, Collection<Document>>;
162
+ connectionString: string;
163
+ constructor(options: MongoProviderOptions);
164
+ /**
165
+ * Connects to MongoDB using the shared connection function.
166
+ */
167
+ connect(options?: MongoProviderOptions): Promise<void>;
168
+ /**
169
+ * Disconnects from MongoDB.
170
+ */
171
+ disconnect(): Promise<void>;
172
+ /**
173
+ * Creates a collection if it doesn't exist.
174
+ */
175
+ createTable(table: string): Promise<void>;
176
+ /**
177
+ * Converts DefussRecord to MongoDB document
178
+ * @param record DefussRecord to convert
179
+ * @returns MongoDB document
180
+ */
181
+ private toMongoDoc;
182
+ /**
183
+ * Converts MongoDB document to DefussRecord
184
+ * @param doc MongoDB document to convert
185
+ * @returns DefussRecord
186
+ */
187
+ private fromMongoDoc;
188
+ /**
189
+ * Inserts a new document into the collection.
190
+ * @param table - The name of the collection.
191
+ * @param record - The record to insert.
192
+ * @returns The primary key of the inserted document.
193
+ */
194
+ insert<T extends DefussRecord>(table: string, record: T): Promise<PrimaryKeyValue>;
195
+ /**
196
+ * Finds documents in the collection based on query.
197
+ * @param table - The name of the collection.
198
+ * @param query - Query criteria.
199
+ * @returns An array of matching documents.
200
+ */
201
+ find<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>): Promise<T[]>;
202
+ /**
203
+ * Finds a single document in the collection based on query.
204
+ * @param table - The name of the collection.
205
+ * @param query - Query criteria.
206
+ * @returns The first matching document or null.
207
+ */
208
+ findOne<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>): Promise<T | null>;
209
+ /**
210
+ * Updates documents in the collection based on query.
211
+ * @param table - The name of the collection.
212
+ * @param query - Query criteria.
213
+ * @param update - Fields to update.
214
+ */
215
+ update<T extends DefussRecord>(table: string, query: Partial<T>, update: Partial<T>): Promise<void>;
216
+ /**
217
+ * Deletes documents from the collection based on query.
218
+ * @param table - The name of the collection.
219
+ * @param query - Query criteria.
220
+ */
221
+ delete(table: string, query: Partial<DefussRecord>): Promise<void>;
222
+ /**
223
+ * Upserts a document into the collection based on query.
224
+ * @param table - The name of the collection.
225
+ * @param record - The record to upsert.
226
+ * @param query - Query criteria.
227
+ * @returns The primary key of the upserted document.
228
+ */
229
+ upsert<T extends DefussRecord>(table: string, record: T, query: Partial<Record<string, RecordValue>>): Promise<PrimaryKeyValue>;
230
+ /**
231
+ * Deserializes a MongoDB document into a JavaScript object.
232
+ * @param row - The document to deserialize.
233
+ * @returns The deserialized JavaScript object.
234
+ */
235
+ deserializeRow(row: MongoDefussDocument): any;
236
+ }
237
+ /**
238
+ * Closes all MongoDB connections - useful for test cleanup
239
+ */
240
+ declare function closeAllConnections(): Promise<void>;
241
+ declare function clearMongoConnections(): void;
242
+
243
+ export { type DbClientMap, type DbMap, DefussProvider, DefussRecord, LibsqlProvider, type LibsqlProviderOptions, MongoProvider, type MongoProviderOptions, PrimaryKeyValue, RecordValue, clearMongoConnections, closeAllConnections, connectToMongo, getCollectionByName, isConnected, isValidObjectId, parseMongoConnectionString, ping, toObjectId };