@moltendb-web/query 0.1.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,360 @@
1
+ /** A plain JSON-serialisable value. */
2
+ export type JsonValue = string | number | boolean | null | JsonValue[] | {
3
+ [key: string]: JsonValue;
4
+ };
5
+ /** A document stored in MoltenDB — any object with string keys. */
6
+ export type Document = {
7
+ [key: string]: JsonValue;
8
+ };
9
+ /** A map of document key → document body used in set/update payloads. */
10
+ export type DataMap = {
11
+ [key: string]: Document;
12
+ };
13
+ /** Comparison operators supported in a WHERE clause. */
14
+ export interface WhereOperators {
15
+ $eq?: JsonValue;
16
+ $ne?: JsonValue;
17
+ $gt?: number;
18
+ $gte?: number;
19
+ $lt?: number;
20
+ $lte?: number;
21
+ $in?: JsonValue[];
22
+ $nin?: JsonValue[];
23
+ $contains?: JsonValue;
24
+ }
25
+ /**
26
+ * A WHERE clause: each key is a field path (dot-notation supported),
27
+ * and the value is either a plain value (implicit equality) or an operator object.
28
+ */
29
+ export type WhereClause = {
30
+ [field: string]: JsonValue | WhereOperators;
31
+ };
32
+ /** A single sort specification. */
33
+ export interface SortSpec {
34
+ field: string;
35
+ order?: 'asc' | 'desc';
36
+ }
37
+ /** A single join specification. */
38
+ export interface JoinSpec {
39
+ /** The alias under which the joined document is embedded. */
40
+ alias: string;
41
+ /** The collection to join from. */
42
+ from: string;
43
+ /** The foreign-key field path on the main document. */
44
+ on: string;
45
+ /** Optional field projection on the joined document. */
46
+ fields?: string[];
47
+ }
48
+ /**
49
+ * Inline reference embedding at insert time.
50
+ * Format: { alias: "collection.key" }
51
+ * Example: { ram: "memory.mem4", screen: "display.dsp3" }
52
+ */
53
+ export type ExtendsMap = {
54
+ [alias: string]: string;
55
+ };
56
+ /**
57
+ * The transport layer used by MoltenDBClient to send messages.
58
+ * Implement this interface to connect the query builder to any backend:
59
+ * - A Web Worker (WASM in-browser)
60
+ * - A fetch-based HTTP client
61
+ * - A WebSocket connection
62
+ * - A mock for testing
63
+ */
64
+ export interface MoltenTransport {
65
+ send(action: 'get' | 'set' | 'update' | 'delete', payload: Document): Promise<JsonValue>;
66
+ }
67
+ /**
68
+ * Default transport that communicates with a MoltenDB Web Worker.
69
+ *
70
+ * The worker must follow the moltendb-worker.js message protocol:
71
+ * postMessage({ id, action, ...payload })
72
+ * onmessage → { id, result } | { id, error }
73
+ */
74
+ export declare class WorkerTransport implements MoltenTransport {
75
+ private worker;
76
+ private messageId;
77
+ private pending;
78
+ constructor(worker: Worker, startId?: number);
79
+ send(action: 'get' | 'set' | 'update' | 'delete', payload: Document): Promise<JsonValue>;
80
+ }
81
+ /**
82
+ * Builder for GET (read/query) operations.
83
+ *
84
+ * Allowed fields: collection, keys, where, fields, excludedFields,
85
+ * joins, sort, count, offset
86
+ *
87
+ * @example
88
+ * const rows = await db.collection('laptops')
89
+ * .get()
90
+ * .where({ brand: 'Apple' })
91
+ * .fields(['brand', 'model', 'price'])
92
+ * .sort([{ field: 'price', order: 'asc' }])
93
+ * .count(5)
94
+ * .exec();
95
+ */
96
+ export declare class GetQuery {
97
+ private payload;
98
+ private transport;
99
+ /** @internal */
100
+ constructor(transport: MoltenTransport, collection: string);
101
+ /**
102
+ * Fetch a single document by key, or a batch of documents by key array.
103
+ *
104
+ * @param keys - A single key string or an array of key strings.
105
+ *
106
+ * @example
107
+ * // Single key
108
+ * .keys('lp2')
109
+ * // Batch
110
+ * .keys(['lp1', 'lp3', 'lp5'])
111
+ */
112
+ keys(keys: string | string[]): this;
113
+ /**
114
+ * Filter documents using a WHERE clause.
115
+ * Multiple conditions are combined with implicit AND.
116
+ *
117
+ * Supported operators: $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $contains
118
+ * Dot-notation is supported for nested fields.
119
+ *
120
+ * @example
121
+ * .where({ brand: 'Apple' })
122
+ * .where({ price: { $gt: 1000, $lt: 3000 } })
123
+ * .where({ 'specs.cpu.brand': 'Intel', in_stock: true })
124
+ */
125
+ where(clause: WhereClause): this;
126
+ /**
127
+ * Project the response to only the specified fields (dot-notation supported).
128
+ * Cannot be combined with {@link excludedFields}.
129
+ *
130
+ * @example
131
+ * .fields(['brand', 'model', 'specs.cpu.ghz'])
132
+ */
133
+ fields(fields: string[]): this;
134
+ /**
135
+ * Return all fields except the specified ones.
136
+ * Cannot be combined with {@link fields}.
137
+ *
138
+ * @example
139
+ * .excludedFields(['price', 'memory_id', 'display_id'])
140
+ */
141
+ excludedFields(fields: string[]): this;
142
+ /**
143
+ * Join related documents from other collections.
144
+ * Each join embeds the related document under the given alias.
145
+ *
146
+ * @example
147
+ * .joins([
148
+ * { alias: 'ram', from: 'memory', on: 'memory_id', fields: ['capacity_gb', 'type'] },
149
+ * { alias: 'screen', from: 'display', on: 'display_id', fields: ['refresh_hz', 'panel'] },
150
+ * ])
151
+ */
152
+ joins(specs: JoinSpec[]): this;
153
+ /**
154
+ * Sort the results.
155
+ * Multiple specs are applied in priority order (first = primary sort key).
156
+ *
157
+ * @example
158
+ * .sort([{ field: 'price', order: 'asc' }])
159
+ * .sort([{ field: 'brand', order: 'asc' }, { field: 'price', order: 'desc' }])
160
+ */
161
+ sort(specs: SortSpec[]): this;
162
+ /**
163
+ * Limit the number of results returned (applied after filtering and sorting).
164
+ *
165
+ * @example
166
+ * .count(10)
167
+ */
168
+ count(n: number): this;
169
+ /**
170
+ * Skip the first N results (for pagination, applied after sorting).
171
+ *
172
+ * @example
173
+ * .offset(20).count(10) // page 3 of 10
174
+ */
175
+ offset(n: number): this;
176
+ /**
177
+ * Build and return the raw JSON payload without sending it.
178
+ * Useful for debugging or passing to a custom transport.
179
+ */
180
+ build(): Document;
181
+ /**
182
+ * Execute the query and return the result.
183
+ * Returns a single document for single-key lookups, or an array for all others.
184
+ */
185
+ exec(): Promise<JsonValue>;
186
+ }
187
+ /**
188
+ * Builder for SET (insert / upsert) operations.
189
+ *
190
+ * Allowed fields: collection, data, extends
191
+ *
192
+ * @example
193
+ * await db.collection('laptops')
194
+ * .set({
195
+ * lp1: { brand: 'Lenovo', model: 'ThinkPad X1', price: 1499 },
196
+ * lp2: { brand: 'Apple', model: 'MacBook Pro', price: 3499 },
197
+ * })
198
+ * .exec();
199
+ */
200
+ export declare class SetQuery {
201
+ private payload;
202
+ private transport;
203
+ /** @internal */
204
+ constructor(transport: MoltenTransport, collection: string, data: DataMap | Document[]);
205
+ /**
206
+ * Embed data from other collections into each document at insert time.
207
+ * The referenced document is fetched once and stored as a snapshot.
208
+ *
209
+ * Format: `{ alias: "collection.key" }`
210
+ *
211
+ * @example
212
+ * .extends({ ram: 'memory.mem4', screen: 'display.dsp3' })
213
+ */
214
+ extends(map: ExtendsMap): this;
215
+ /** Build and return the raw JSON payload without sending it. */
216
+ build(): Document;
217
+ /** Execute the insert/upsert and return `{ count, status }`. */
218
+ exec(): Promise<JsonValue>;
219
+ }
220
+ /**
221
+ * Builder for UPDATE (partial patch / merge) operations.
222
+ *
223
+ * Allowed fields: collection, data
224
+ *
225
+ * Only the fields present in each patch object are updated —
226
+ * all other fields on the existing document are left unchanged.
227
+ *
228
+ * @example
229
+ * await db.collection('laptops')
230
+ * .update({ lp4: { price: 1749, in_stock: true } })
231
+ * .exec();
232
+ */
233
+ export declare class UpdateQuery {
234
+ private payload;
235
+ private transport;
236
+ /** @internal */
237
+ constructor(transport: MoltenTransport, collection: string, data: DataMap);
238
+ /** Build and return the raw JSON payload without sending it. */
239
+ build(): Document;
240
+ /** Execute the patch and return `{ count, status }`. */
241
+ exec(): Promise<JsonValue>;
242
+ }
243
+ /**
244
+ * Builder for DELETE operations.
245
+ *
246
+ * Allowed fields: collection, keys, drop
247
+ *
248
+ * @example
249
+ * // Delete a single document
250
+ * await db.collection('laptops').delete().keys('lp6').exec();
251
+ *
252
+ * // Delete multiple documents
253
+ * await db.collection('laptops').delete().keys(['lp4', 'lp5']).exec();
254
+ *
255
+ * // Drop the entire collection
256
+ * await db.collection('laptops').delete().drop().exec();
257
+ */
258
+ export declare class DeleteQuery {
259
+ private payload;
260
+ private transport;
261
+ /** @internal */
262
+ constructor(transport: MoltenTransport, collection: string);
263
+ /**
264
+ * Delete a single document by key, or multiple documents by key array.
265
+ *
266
+ * @example
267
+ * .keys('lp6')
268
+ * .keys(['lp4', 'lp5'])
269
+ */
270
+ keys(keys: string | string[]): this;
271
+ /**
272
+ * Drop the entire collection (deletes all documents).
273
+ * Cannot be combined with {@link keys}.
274
+ *
275
+ * @example
276
+ * .drop()
277
+ */
278
+ drop(): this;
279
+ /** Build and return the raw JSON payload without sending it. */
280
+ build(): Document;
281
+ /** Execute the delete and return `{ count, status }`. */
282
+ exec(): Promise<JsonValue>;
283
+ }
284
+ /**
285
+ * A handle to a specific collection.
286
+ * Returned by `MoltenDBClient.collection(name)`.
287
+ * Use it to start any of the four operation builders.
288
+ */
289
+ export declare class CollectionHandle {
290
+ private transport;
291
+ private collectionName;
292
+ /** @internal */
293
+ constructor(transport: MoltenTransport, collectionName: string);
294
+ /**
295
+ * Start a GET (read/query) builder for this collection.
296
+ *
297
+ * @example
298
+ * db.collection('laptops').get().where({ brand: 'Apple' }).exec()
299
+ */
300
+ get(): GetQuery;
301
+ /**
302
+ * Start a SET (insert/upsert) builder for this collection.
303
+ *
304
+ * @param data - A map of `{ key: document }` pairs, or an array of documents
305
+ * (keys are auto-generated as UUIDv7 when using array format).
306
+ *
307
+ * @example
308
+ * db.collection('laptops').set({ lp1: { brand: 'Lenovo', price: 1499 } }).exec()
309
+ */
310
+ set(data: DataMap | Document[]): SetQuery;
311
+ /**
312
+ * Start an UPDATE (partial patch) builder for this collection.
313
+ *
314
+ * @param data - A map of `{ key: patch }` pairs. Only the provided fields are updated.
315
+ *
316
+ * @example
317
+ * db.collection('laptops').update({ lp4: { price: 1749 } }).exec()
318
+ */
319
+ update(data: DataMap): UpdateQuery;
320
+ /**
321
+ * Start a DELETE builder for this collection.
322
+ * Chain `.keys(...)` or `.drop()` to specify what to delete.
323
+ *
324
+ * @example
325
+ * db.collection('laptops').delete().keys('lp6').exec()
326
+ */
327
+ delete(): DeleteQuery;
328
+ }
329
+ /**
330
+ * The main entry point for the MoltenDB query builder.
331
+ *
332
+ * Accepts any {@link MoltenTransport} implementation — use {@link WorkerTransport}
333
+ * to connect to a MoltenDB WASM Web Worker, or provide your own transport
334
+ * for HTTP, WebSocket, or testing.
335
+ *
336
+ * @example
337
+ * // Browser + WASM Web Worker
338
+ * const worker = new Worker('./moltendb-worker.js', { type: 'module' });
339
+ * const transport = new WorkerTransport(worker);
340
+ * const db = new MoltenDBClient(transport);
341
+ *
342
+ * const results = await db.collection('laptops')
343
+ * .get()
344
+ * .where({ in_stock: true })
345
+ * .sort([{ field: 'price', order: 'asc' }])
346
+ * .count(5)
347
+ * .exec();
348
+ */
349
+ export declare class MoltenDBQueryBuilder {
350
+ private transport;
351
+ constructor(transport: MoltenTransport);
352
+ /**
353
+ * Select a collection to operate on.
354
+ * Returns a {@link CollectionHandle} from which you can start any query builder.
355
+ *
356
+ * @param name - The collection name (e.g. `'laptops'`).
357
+ */
358
+ collection(name: string): CollectionHandle;
359
+ }
360
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA4CA,uCAAuC;AACvC,MAAM,MAAM,SAAS,GACjB,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,SAAS,EAAE,GACX;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEjC,mEAAmE;AACnE,MAAM,MAAM,QAAQ,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEpD,yEAAyE;AACzE,MAAM,MAAM,OAAO,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ,CAAA;CAAE,CAAC;AAIlD,wDAAwD;AACxD,MAAM,WAAW,cAAc;IAC7B,GAAG,CAAC,EAAE,SAAS,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