defuss-db 1.1.4 → 2.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.
package/dist/server.d.ts CHANGED
@@ -1,113 +1,70 @@
1
- import { DefussProvider, DefussRecord, PrimaryKeyValue, RecordValue } from './index.js';
2
- export { DefussTable } from './index.js';
1
+ import { DefussRecord, DefussIndexDefinition, RecordValue, DefussProvider, DefussTableDefinition, PrimaryKeyValue, DefussSelector } from './index.js';
2
+ export { AggregationBuilder, AggregationCreateOptions, AggregationDistinctOptions, AggregationDistinctSelector, AggregationFieldSortSpec, AggregationGroupContext, AggregationGroupKeys, AggregationGroupReducer, AggregationJoinResult, AggregationJoinSpec, AggregationJoinType, AggregationMergeContext, AggregationMergeRowsFn, AggregationMergeRowsResult, AggregationNamespacedRow, AggregationProjectionMap, AggregationRemoveFieldsInput, AggregationResolverSortSpec, AggregationRowContext, AggregationRowMapper, AggregationRowsLoader, AggregationRowsSource, AggregationSortComparator, AggregationSortContext, AggregationSortDirection, AggregationSortSpec, AggregationSource, AggregationTableLike, AggregationTableSource, AggregationValueResolver, DefussTable, DefussValue, aggregationReducers, avgBy, countRows, createAggregation, defineTable, firstBy, lastBy, maxBy, minBy, sumBy } from './index.js';
3
3
  import { Config, Client } from '@libsql/client';
4
- import { MongoClient, Db, Collection, Document, ObjectId } from 'mongodb';
4
+ import { MongoClient, Db, Collection, Document, ObjectId, MongoClientOptions } from 'mongodb';
5
+ import { N as NormalizedDefussTableDefinition$1 } from './runtime-CJK5Yy0J.js';
6
+
7
+ interface NormalizedDefussIndexDefinition<T extends DefussRecord = DefussRecord> extends DefussIndexDefinition<T> {
8
+ selector: string;
9
+ storageKey: string;
10
+ source: string | ((value: T) => RecordValue | undefined);
11
+ unique: boolean;
12
+ implicit?: boolean;
13
+ }
14
+ interface NormalizedDefussTableDefinition<T extends DefussRecord = DefussRecord> {
15
+ name: string;
16
+ indexes: Array<NormalizedDefussIndexDefinition<T>>;
17
+ selectorToIndex: Map<string, NormalizedDefussIndexDefinition<T>>;
18
+ }
19
+ interface DefussStoredRow {
20
+ id: string;
21
+ payload: string;
22
+ indexValues: Record<string, string | undefined>;
23
+ }
24
+
25
+ interface JsonlProviderOptions {
26
+ baseDir: string;
27
+ }
28
+ declare class JsonlProvider implements DefussProvider<JsonlProviderOptions> {
29
+ baseDir: string;
30
+ connected: boolean;
31
+ rowsByTable: Map<string, Map<string, DefussStoredRow>>;
32
+ definitions: Map<string, NormalizedDefussTableDefinition<any>>;
33
+ connect(options: JsonlProviderOptions): Promise<void>;
34
+ disconnect(): Promise<void>;
35
+ isConnected(): boolean;
36
+ private tableFile;
37
+ private loadExistingTables;
38
+ private rewriteTable;
39
+ private getTableRows;
40
+ private ensureUniqueConstraints;
41
+ private rowsToRecords;
42
+ ensureTable<T extends DefussRecord>(definition: DefussTableDefinition<T>): Promise<void>;
43
+ insert<T extends DefussRecord>(definition: DefussTableDefinition<T>, value: T): Promise<PrimaryKeyValue>;
44
+ find<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector?: DefussSelector): Promise<T[]>;
45
+ findOne<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector?: DefussSelector): Promise<T | null>;
46
+ update<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector: DefussSelector, update: Partial<T>): Promise<void>;
47
+ delete<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector: DefussSelector): Promise<void>;
48
+ upsert<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector: DefussSelector, value: T): Promise<PrimaryKeyValue>;
49
+ }
5
50
 
6
51
  type LibsqlProviderOptions = Config;
7
- /**
8
- * LibsqlProvider implements DefussProvider using libsql for SQLite databases.
9
- */
10
52
  declare class LibsqlProvider implements DefussProvider<Config> {
11
53
  db: Client;
12
- /**
13
- * Connects to the SQLite database using libsql.
14
- * @param config - The libsql connection configuration.
15
- */
54
+ definitions: Map<string, NormalizedDefussTableDefinition<any>>;
16
55
  connect(config: Config): Promise<void>;
17
- /**
18
- * Disconnects from the SQLite database.
19
- */
20
56
  disconnect(): Promise<void>;
21
- /**
22
- * Checks if the provider is currently connected to the database.
23
- * @returns True if connected, false otherwise.
24
- */
25
57
  isConnected(): boolean;
26
- /**
27
- * Creates a table if it doesn't exist.
28
- * Sets up the primary key and any initial structure needed.
29
- * @param table - The name of the table.
30
- */
31
- createTable(table: string): Promise<void>;
32
- /**
33
- * Ensures all necessary columns exist in the table
34
- * @param table - The name of the table
35
- * @param record - DefussRecord containing fields to ensure
36
- */
37
- private ensureColumnsExist;
38
- /**
39
- * Gets the SQLite data type for a JavaScript value
40
- * @param value The JavaScript value to map to SQLite type
41
- * @returns The SQLite type string
42
- */
43
- private getSqliteType;
44
- /**
45
- * Retrieves existing columns in the specified table.
46
- * @param table - The name of the table.
47
- * @returns An array of column names.
48
- */
49
58
  private getExistingColumns;
50
- /**
51
- * Converts a DefussRecord to SQLite values
52
- * @param record The DefussRecord to convert
53
- * @returns Object with column names and values for SQLite
54
- */
55
- private toSqliteValues;
56
- /**
57
- * Convert JavaScript value to SQLite-compatible value
58
- */
59
- private convertValueForSqlite;
60
- /**
61
- * Convert SQLite row to DefussRecord
62
- */
63
- private fromSqliteRow;
64
- /**
65
- * Inserts a record into the table.
66
- * @param table - The name of the table.
67
- * @param record - The record to insert.
68
- * @returns The primary key of the inserted record.
69
- */
70
- insert<T extends DefussRecord>(table: string, record: T): Promise<PrimaryKeyValue>;
71
- /**
72
- * Finds records in the table based on query.
73
- * @param table - The name of the table.
74
- * @param query - Query criteria.
75
- * @returns An array of matching records.
76
- */
77
- find<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>): Promise<T[]>;
78
- /**
79
- * Finds a single record in the table based on query.
80
- * @param table - The name of the table.
81
- * @param query - Query criteria.
82
- * @returns The first matching record or null.
83
- */
84
- findOne<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>): Promise<T | null>;
85
- /**
86
- * Updates records in the table based on query.
87
- * @param table - The name of the table.
88
- * @param query - Query criteria.
89
- * @param update - Fields to update.
90
- */
91
- update<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>, update: Partial<T>): Promise<void>;
92
- /**
93
- * Deletes records from the table based on query.
94
- * @param table - The name of the table.
95
- * @param query - Query criteria.
96
- */
97
- delete(table: string, query: Partial<DefussRecord>): Promise<void>;
98
- /**
99
- * Upserts a record into the table based on query.
100
- * @param table - The name of the table.
101
- * @param record - The record to upsert.
102
- * @param query - Query criteria.
103
- * @returns The primary key of the upserted record.
104
- */
105
- upsert<T extends DefussRecord>(table: string, record: T, query: Partial<Record<string, RecordValue>>): Promise<PrimaryKeyValue>;
106
- /**
107
- * Ensures that the specified table exists.
108
- * @param table - The name of the table.
109
- */
110
- private ensureTableExists;
59
+ private upsertRow;
60
+ private queryRows;
61
+ ensureTable<T extends DefussRecord>(definition: DefussTableDefinition<T>): Promise<void>;
62
+ insert<T extends DefussRecord>(definition: DefussTableDefinition<T>, value: T): Promise<PrimaryKeyValue>;
63
+ find<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector?: DefussSelector): Promise<T[]>;
64
+ findOne<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector?: DefussSelector): Promise<T | null>;
65
+ update<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector: DefussSelector, update: Partial<T>): Promise<void>;
66
+ delete<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector: DefussSelector): Promise<void>;
67
+ upsert<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector: DefussSelector, value: T): Promise<PrimaryKeyValue>;
111
68
  }
112
69
 
113
70
  interface MongoProviderOptions {
@@ -120,135 +77,45 @@ type DbClientMap = {
120
77
  type DbMap = {
121
78
  [dbName: string]: Db | null;
122
79
  };
123
- /**
124
- * Parses a MongoDB connection string into its components
125
- * @param connectionString - MongoDB connection string
126
- * @returns Object containing databaseName and clientKey
127
- */
80
+ type MongoStoredId = PrimaryKeyValue | ObjectId;
81
+ type MongoStoredDocument = Document & {
82
+ _id?: MongoStoredId;
83
+ };
128
84
  declare const parseMongoConnectionString: (connectionString: string) => {
129
85
  databaseName: string;
130
86
  clientKey: string;
131
87
  };
132
- /**
133
- * Core connection function to be used by all parts of the system
134
- * @param connectionString MongoDB connection string
135
- * @returns Object containing the MongoClient and Db instances
136
- */
137
- declare function connectToMongo(connectionString: string): Promise<{
88
+ declare function connectToMongo(connectionString: string, options?: MongoClientOptions): Promise<{
138
89
  client: MongoClient;
139
90
  db: Db;
140
91
  }>;
141
- /** Returns true if both dbClient and database are initialized */
142
92
  declare const isConnected: (connectionString: string) => boolean;
143
- /**
144
- * Pings the MongoDB server to check connectivity
145
- * @param connectionString MongoDB connection string
146
- */
147
- declare const ping: (connectionString: string) => Promise<unknown>;
148
- /**
149
- * Gets a collection by name, connecting if necessary
150
- */
93
+ declare const ping: (connectionString: string) => Promise<Document>;
151
94
  declare const getCollectionByName: <T extends Document>(collectionName: string, connectionString: string) => Promise<Collection<T>>;
152
95
  declare function isValidObjectId(id: string): boolean;
153
96
  declare const toObjectId: (id: string | ObjectId) => ObjectId;
154
- interface MongoDefussDocument extends Document {
155
- type?: "json" | "blob";
156
- json?: string;
157
- blob?: ArrayBuffer;
158
- [key: string]: any;
159
- }
160
- /**
161
- * MongoProvider implements DefussProvider for MongoDB.
162
- */
163
97
  declare class MongoProvider implements DefussProvider<MongoProviderOptions> {
164
98
  client: MongoClient | null;
165
99
  db: Db | null;
166
- collections: Map<string, Collection<Document>>;
100
+ collections: Map<string, Collection<MongoStoredDocument>>;
101
+ definitions: Map<string, NormalizedDefussTableDefinition$1<any>>;
167
102
  connectionString: string;
168
103
  constructor(options: MongoProviderOptions);
169
- /**
170
- * Connects to MongoDB using the shared connection function.
171
- */
172
104
  connect(options?: MongoProviderOptions): Promise<void>;
173
- /**
174
- * Disconnects from MongoDB.
175
- */
176
105
  disconnect(): Promise<void>;
177
- /**
178
- * Checks if the provider is currently connected to the database.
179
- * @returns True if connected, false otherwise.
180
- */
181
106
  isConnected(): boolean;
182
- /**
183
- * Creates a collection if it doesn't exist.
184
- */
185
- createTable(table: string): Promise<void>;
186
- /**
187
- * Converts DefussRecord to MongoDB document
188
- * @param record DefussRecord to convert
189
- * @returns MongoDB document
190
- */
191
- private toMongoDoc;
192
- /**
193
- * Converts MongoDB document to DefussRecord
194
- * @param doc MongoDB document to convert
195
- * @returns DefussRecord
196
- */
197
- private fromMongoDoc;
198
- /**
199
- * Inserts a new document into the collection.
200
- * @param table - The name of the collection.
201
- * @param record - The record to insert.
202
- * @returns The primary key of the inserted document.
203
- */
204
- insert<T extends DefussRecord>(table: string, record: T): Promise<PrimaryKeyValue>;
205
- /**
206
- * Finds documents in the collection based on query.
207
- * @param table - The name of the collection.
208
- * @param query - Query criteria.
209
- * @returns An array of matching documents.
210
- */
211
- find<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>): Promise<T[]>;
212
- /**
213
- * Finds a single document in the collection based on query.
214
- * @param table - The name of the collection.
215
- * @param query - Query criteria.
216
- * @returns The first matching document or null.
217
- */
218
- findOne<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>): Promise<T | null>;
219
- /**
220
- * Updates documents in the collection based on query.
221
- * @param table - The name of the collection.
222
- * @param query - Query criteria.
223
- * @param update - Fields to update.
224
- */
225
- update<T extends DefussRecord>(table: string, query: Partial<T>, update: Partial<T>): Promise<void>;
226
- /**
227
- * Deletes documents from the collection based on query.
228
- * @param table - The name of the collection.
229
- * @param query - Query criteria.
230
- */
231
- delete(table: string, query: Partial<DefussRecord>): Promise<void>;
232
- /**
233
- * Upserts a document into the collection based on query.
234
- * @param table - The name of the collection.
235
- * @param record - The record to upsert.
236
- * @param query - Query criteria.
237
- * @returns The primary key of the upserted document.
238
- */
239
- upsert<T extends DefussRecord>(table: string, record: T, query: Partial<Record<string, RecordValue>>): Promise<PrimaryKeyValue>;
240
- /**
241
- * Deserializes a MongoDB document into a JavaScript object.
242
- * @param row - The document to deserialize.
243
- * @returns The deserialized JavaScript object.
244
- */
245
- deserializeRow(row: MongoDefussDocument): any;
107
+ private ensureCollection;
108
+ private buildMongoQuery;
109
+ ensureTable<T extends DefussRecord>(definition: DefussTableDefinition<T>): Promise<void>;
110
+ insert<T extends DefussRecord>(definition: DefussTableDefinition<T>, value: T): Promise<PrimaryKeyValue>;
111
+ find<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector?: DefussSelector): Promise<T[]>;
112
+ findOne<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector?: DefussSelector): Promise<T | null>;
113
+ update<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector: DefussSelector, update: Partial<T>): Promise<void>;
114
+ delete<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector: DefussSelector): Promise<void>;
115
+ upsert<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector: DefussSelector, value: T): Promise<PrimaryKeyValue>;
246
116
  }
247
- /**
248
- * Closes all MongoDB connections - useful for test cleanup
249
- */
250
117
  declare function closeAllConnections(): Promise<void>;
251
118
  declare function clearMongoConnections(): void;
252
119
 
253
- export { DefussProvider, DefussRecord, LibsqlProvider, MongoProvider, PrimaryKeyValue, RecordValue, clearMongoConnections, closeAllConnections, connectToMongo, getCollectionByName, isConnected, isValidObjectId, parseMongoConnectionString, ping, toObjectId };
254
- export type { DbClientMap, DbMap, LibsqlProviderOptions, MongoProviderOptions };
120
+ export { DefussIndexDefinition, DefussProvider, DefussRecord, DefussSelector, DefussTableDefinition, JsonlProvider, LibsqlProvider, MongoProvider, PrimaryKeyValue, RecordValue, clearMongoConnections, closeAllConnections, connectToMongo, getCollectionByName, isConnected, isValidObjectId, parseMongoConnectionString, ping, toObjectId };
121
+ export type { DbClientMap, DbMap, JsonlProviderOptions, LibsqlProviderOptions, MongoProviderOptions };