@moltendb-web/query 0.5.0 → 1.0.0-rc.3
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/LICENSE.md +21 -0
- package/README.md +5 -1
- package/dist/index.d.ts +0 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +0 -41
- package/dist/index.js +1 -42
- package/dist/index.js.map +1 -1
- package/package.json +5 -3
- package/src/index.ts +0 -44
- package/dist/esm/index.js +0 -443
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Maximilian Both
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -227,8 +227,12 @@ npm run typecheck # Type-check without emitting
|
|
|
227
227
|
npm run test # Run the Jest test suite
|
|
228
228
|
```
|
|
229
229
|
|
|
230
|
+
## Contributing & Feedback
|
|
231
|
+
|
|
232
|
+
Found a bug or have a feature request? Please open an issue on the [GitHub issue tracker](https://github.com/maximilian27/moltendb-web/issues).
|
|
233
|
+
|
|
230
234
|
---
|
|
231
235
|
|
|
232
236
|
## License
|
|
233
237
|
|
|
234
|
-
MIT
|
|
238
|
+
MIT
|
package/dist/index.d.ts
CHANGED
|
@@ -64,21 +64,6 @@ export type ExtendsMap = {
|
|
|
64
64
|
export interface MoltenTransport {
|
|
65
65
|
sendMessage(action: 'get' | 'set' | 'update' | 'delete', payload: Document): Promise<JsonValue>;
|
|
66
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
|
-
onEvent?: (event: any) => void;
|
|
79
|
-
constructor(worker: Worker, startId?: number);
|
|
80
|
-
sendMessage(action: 'get' | 'set' | 'update' | 'delete', payload: Document): Promise<JsonValue>;
|
|
81
|
-
}
|
|
82
67
|
/**
|
|
83
68
|
* Builder for GET (read/query) operations.
|
|
84
69
|
*
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +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,M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AAC;CAG3B;AAID;;;;GAIG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,SAAS,CAAkB;IACnC,OAAO,CAAC,cAAc,CAAS;IAE/B,gBAAgB;gBACJ,SAAS,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM;IAK9D;;;;;OAKG;IACH,GAAG,IAAI,QAAQ;IAIf;;;;;;;;OAQG;IACH,GAAG,CAAC,IAAI,EAAE,OAAO,GAAG,QAAQ,EAAE,GAAG,QAAQ;IAIzC;;;;;;;OAOG;IACH,MAAM,CAAC,IAAI,EAAE,OAAO,GAAG,WAAW;IAIlC;;;;;;OAMG;IACH,MAAM,IAAI,WAAW;CAGtB;AAID;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,SAAS,CAAkB;gBAEvB,SAAS,EAAE,eAAe;IAItC;;;;;OAKG;IACH,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB;CAG3C"}
|
package/dist/index.esm.js
CHANGED
|
@@ -40,47 +40,6 @@
|
|
|
40
40
|
// await db.collection('laptops').delete().drop().exec();
|
|
41
41
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
42
42
|
|
|
43
|
-
// ─── WorkerTransport ──────────────────────────────────────────────────────────
|
|
44
|
-
/**
|
|
45
|
-
* Default transport that communicates with a MoltenDB Web Worker.
|
|
46
|
-
*
|
|
47
|
-
* The worker must follow the moltendb-worker.js message protocol:
|
|
48
|
-
* postMessage({ id, action, ...payload })
|
|
49
|
-
* onmessage → { id, result } | { id, error }
|
|
50
|
-
*/
|
|
51
|
-
class WorkerTransport {
|
|
52
|
-
constructor(worker, startId = 0) {
|
|
53
|
-
this.pending = new Map();
|
|
54
|
-
this.messageId = startId;
|
|
55
|
-
this.worker = worker;
|
|
56
|
-
this.worker.addEventListener('message', (event) => {
|
|
57
|
-
// 1. Intercept unsolicited broadcast events from the Rust core
|
|
58
|
-
if (event.data && event.data.type === 'event') {
|
|
59
|
-
if (this.onEvent)
|
|
60
|
-
this.onEvent(event.data);
|
|
61
|
-
return; // Don't try to process this as a promise resolution
|
|
62
|
-
}
|
|
63
|
-
// 2. Standard request/response routing
|
|
64
|
-
const { id, result, error } = event.data;
|
|
65
|
-
const p = this.pending.get(id);
|
|
66
|
-
if (!p)
|
|
67
|
-
return;
|
|
68
|
-
this.pending.delete(id);
|
|
69
|
-
if (error)
|
|
70
|
-
p.reject(new Error(error));
|
|
71
|
-
else
|
|
72
|
-
p.resolve(result ?? null);
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
sendMessage(action, payload) {
|
|
76
|
-
return new Promise((resolve, reject) => {
|
|
77
|
-
const id = this.messageId++;
|
|
78
|
-
this.pending.set(id, { resolve, reject });
|
|
79
|
-
this.worker.postMessage({ id, action, ...payload });
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
export { WorkerTransport as WorkerTransport };
|
|
84
43
|
// ─── GetQuery ─────────────────────────────────────────────────────────────────
|
|
85
44
|
/**
|
|
86
45
|
* Builder for GET (read/query) operations.
|
package/dist/index.js
CHANGED
|
@@ -41,48 +41,7 @@
|
|
|
41
41
|
// await db.collection('laptops').delete().drop().exec();
|
|
42
42
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
43
43
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
-
exports.MoltenDBClient = exports.CollectionHandle = exports.DeleteQuery = exports.UpdateQuery = exports.SetQuery = exports.GetQuery =
|
|
45
|
-
// ─── WorkerTransport ──────────────────────────────────────────────────────────
|
|
46
|
-
/**
|
|
47
|
-
* Default transport that communicates with a MoltenDB Web Worker.
|
|
48
|
-
*
|
|
49
|
-
* The worker must follow the moltendb-worker.js message protocol:
|
|
50
|
-
* postMessage({ id, action, ...payload })
|
|
51
|
-
* onmessage → { id, result } | { id, error }
|
|
52
|
-
*/
|
|
53
|
-
class WorkerTransport {
|
|
54
|
-
constructor(worker, startId = 0) {
|
|
55
|
-
this.pending = new Map();
|
|
56
|
-
this.messageId = startId;
|
|
57
|
-
this.worker = worker;
|
|
58
|
-
this.worker.addEventListener('message', (event) => {
|
|
59
|
-
// 1. Intercept unsolicited broadcast events from the Rust core
|
|
60
|
-
if (event.data && event.data.type === 'event') {
|
|
61
|
-
if (this.onEvent)
|
|
62
|
-
this.onEvent(event.data);
|
|
63
|
-
return; // Don't try to process this as a promise resolution
|
|
64
|
-
}
|
|
65
|
-
// 2. Standard request/response routing
|
|
66
|
-
const { id, result, error } = event.data;
|
|
67
|
-
const p = this.pending.get(id);
|
|
68
|
-
if (!p)
|
|
69
|
-
return;
|
|
70
|
-
this.pending.delete(id);
|
|
71
|
-
if (error)
|
|
72
|
-
p.reject(new Error(error));
|
|
73
|
-
else
|
|
74
|
-
p.resolve(result ?? null);
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
sendMessage(action, payload) {
|
|
78
|
-
return new Promise((resolve, reject) => {
|
|
79
|
-
const id = this.messageId++;
|
|
80
|
-
this.pending.set(id, { resolve, reject });
|
|
81
|
-
this.worker.postMessage({ id, action, ...payload });
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
exports.WorkerTransport = WorkerTransport;
|
|
44
|
+
exports.MoltenDBClient = exports.CollectionHandle = exports.DeleteQuery = exports.UpdateQuery = exports.SetQuery = exports.GetQuery = void 0;
|
|
86
45
|
// ─── GetQuery ─────────────────────────────────────────────────────────────────
|
|
87
46
|
/**
|
|
88
47
|
* Builder for GET (read/query) operations.
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,iFAAiF;AACjF,mDAAmD;AACnD,EAAE;AACF,yEAAyE;AACzE,6EAA6E;AAC7E,iBAAiB;AACjB,EAAE;AACF,qEAAqE;AACrE,+CAA+C;AAC/C,8CAA8C;AAC9C,qCAAqC;AACrC,2CAA2C;AAC3C,EAAE;AACF,oCAAoC;AACpC,EAAE;AACF,2CAA2C;AAC3C,EAAE;AACF,6BAA6B;AAC7B,mDAAmD;AACnD,aAAa;AACb,iCAAiC;AACjC,2CAA2C;AAC3C,gDAAgD;AAChD,iBAAiB;AACjB,eAAe;AACf,EAAE;AACF,2BAA2B;AAC3B,mCAAmC;AACnC,sDAAsD;AACtD,eAAe;AACf,EAAE;AACF,8BAA8B;AAC9B,mCAAmC;AACnC,wDAAwD;AACxD,eAAe;AACf,EAAE;AACF,2CAA2C;AAC3C,gEAAgE;AAChE,yEAAyE;AACzE,2DAA2D;AAC3D,gFAAgF;;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,iFAAiF;AACjF,mDAAmD;AACnD,EAAE;AACF,yEAAyE;AACzE,6EAA6E;AAC7E,iBAAiB;AACjB,EAAE;AACF,qEAAqE;AACrE,+CAA+C;AAC/C,8CAA8C;AAC9C,qCAAqC;AACrC,2CAA2C;AAC3C,EAAE;AACF,oCAAoC;AACpC,EAAE;AACF,2CAA2C;AAC3C,EAAE;AACF,6BAA6B;AAC7B,mDAAmD;AACnD,aAAa;AACb,iCAAiC;AACjC,2CAA2C;AAC3C,gDAAgD;AAChD,iBAAiB;AACjB,eAAe;AACf,EAAE;AACF,2BAA2B;AAC3B,mCAAmC;AACnC,sDAAsD;AACtD,eAAe;AACf,EAAE;AACF,8BAA8B;AAC9B,mCAAmC;AACnC,wDAAwD;AACxD,eAAe;AACf,EAAE;AACF,2CAA2C;AAC3C,gEAAgE;AAChE,yEAAyE;AACzE,2DAA2D;AAC3D,gFAAgF;;;AAwFhF,iFAAiF;AAEjF;;;;;;;;;;;;;;GAcG;AACH,MAAa,QAAQ;IAInB,gBAAgB;IAChB,YAAY,SAA0B,EAAE,UAAkB;QACxD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,OAAO,GAAG,EAAE,UAAU,EAAE,CAAC;IACh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,iFAAiF;AAEjF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAa,cAAc;IAGzB,YAAY,SAA0B;QACpC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAED;;;;;OAKG;IACH,UAAU,CAAC,IAAY;QACrB,OAAO,IAAI,gBAAgB,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACpD,CAAC;CACF;AAhBD,wCAgBC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moltendb-web/query",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-rc.3",
|
|
4
4
|
"description": "Type-safe query builder for MoltenDB — chainable API for get, set, update and delete operations.",
|
|
5
|
+
"author": "Maximilian Both <maximilian.both27@outlook.com>",
|
|
5
6
|
"main": "./dist/index.js",
|
|
6
7
|
"module": "./dist/index.esm.js",
|
|
7
8
|
"types": "./dist/index.d.ts",
|
|
@@ -23,10 +24,11 @@
|
|
|
23
24
|
"scripts": {
|
|
24
25
|
"build": "tsc --project tsconfig.build.json && node scripts/build-esm.js",
|
|
25
26
|
"typecheck": "tsc --noEmit",
|
|
26
|
-
"test": "node --experimental-vm-modules node_modules/.bin/jest"
|
|
27
|
+
"test": "node --experimental-vm-modules node_modules/.bin/jest",
|
|
28
|
+
"prepublishOnly": "npm run build"
|
|
27
29
|
},
|
|
28
30
|
"keywords": ["moltendb", "database", "query-builder", "nosql", "wasm"],
|
|
29
|
-
"license": "MIT
|
|
31
|
+
"license": "MIT",
|
|
30
32
|
"devDependencies": {
|
|
31
33
|
"typescript": "^6.0.2"
|
|
32
34
|
}
|
package/src/index.ts
CHANGED
|
@@ -125,50 +125,6 @@ export interface MoltenTransport {
|
|
|
125
125
|
sendMessage(action: 'get' | 'set' | 'update' | 'delete', payload: Document): Promise<JsonValue>;
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
// ─── WorkerTransport ──────────────────────────────────────────────────────────
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Default transport that communicates with a MoltenDB Web Worker.
|
|
132
|
-
*
|
|
133
|
-
* The worker must follow the moltendb-worker.js message protocol:
|
|
134
|
-
* postMessage({ id, action, ...payload })
|
|
135
|
-
* onmessage → { id, result } | { id, error }
|
|
136
|
-
*/
|
|
137
|
-
export class WorkerTransport implements MoltenTransport {
|
|
138
|
-
private worker: Worker;
|
|
139
|
-
private messageId: number;
|
|
140
|
-
private pending = new Map<number, { resolve: (v: JsonValue) => void; reject: (e: Error) => void }>();
|
|
141
|
-
public onEvent?: (event: any) => void;
|
|
142
|
-
|
|
143
|
-
constructor(worker: Worker, startId = 0) {
|
|
144
|
-
this.messageId = startId;
|
|
145
|
-
this.worker = worker;
|
|
146
|
-
this.worker.addEventListener('message', (event: MessageEvent) => {
|
|
147
|
-
// 1. Intercept unsolicited broadcast events from the Rust core
|
|
148
|
-
if (event.data && event.data.type === 'event') {
|
|
149
|
-
if (this.onEvent) this.onEvent(event.data);
|
|
150
|
-
return; // Don't try to process this as a promise resolution
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// 2. Standard request/response routing
|
|
154
|
-
const { id, result, error } = event.data as { id: number; result?: JsonValue; error?: string };
|
|
155
|
-
const p = this.pending.get(id);
|
|
156
|
-
if (!p) return;
|
|
157
|
-
|
|
158
|
-
this.pending.delete(id);
|
|
159
|
-
if (error) p.reject(new Error(error));
|
|
160
|
-
else p.resolve(result ?? null);
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
sendMessage(action: 'get' | 'set' | 'update' | 'delete', payload: Document): Promise<JsonValue> {
|
|
165
|
-
return new Promise((resolve, reject) => {
|
|
166
|
-
const id = this.messageId++;
|
|
167
|
-
this.pending.set(id, { resolve, reject });
|
|
168
|
-
this.worker.postMessage({ id, action, ...payload });
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
128
|
|
|
173
129
|
// ─── GetQuery ─────────────────────────────────────────────────────────────────
|
|
174
130
|
|
package/dist/esm/index.js
DELETED
|
@@ -1,443 +0,0 @@
|
|
|
1
|
-
// ─── MoltenDB Query Builder ───────────────────────────────────────────────────
|
|
2
|
-
// Chainable, type-safe query builder for MoltenDB.
|
|
3
|
-
//
|
|
4
|
-
// Each operation has its own builder class that only exposes the methods
|
|
5
|
-
// that are valid for that operation — matching the server's allowed-property
|
|
6
|
-
// lists exactly:
|
|
7
|
-
//
|
|
8
|
-
// GET_ALLOWED: collection, keys, where, fields, excludedFields,
|
|
9
|
-
// joins, sort, count, offset
|
|
10
|
-
// SET_ALLOWED: collection, data, extends
|
|
11
|
-
// UPDATE_ALLOWED: collection, data
|
|
12
|
-
// DELETE_ALLOWED: collection, keys, drop
|
|
13
|
-
//
|
|
14
|
-
// Usage (vanilla JS or TypeScript):
|
|
15
|
-
//
|
|
16
|
-
// const db = new MoltenDBClient(worker);
|
|
17
|
-
//
|
|
18
|
-
// // GET — chainable query
|
|
19
|
-
// const results = await db.collection('laptops')
|
|
20
|
-
// .get()
|
|
21
|
-
// .where({ brand: 'Apple' })
|
|
22
|
-
// .fields(['brand', 'model', 'price'])
|
|
23
|
-
// .sort([{ field: 'price', order: 'asc' }])
|
|
24
|
-
// .count(10)
|
|
25
|
-
// .exec();
|
|
26
|
-
//
|
|
27
|
-
// // SET — insert/upsert
|
|
28
|
-
// await db.collection('laptops')
|
|
29
|
-
// .set({ lp1: { brand: 'Lenovo', price: 1499 } })
|
|
30
|
-
// .exec();
|
|
31
|
-
//
|
|
32
|
-
// // UPDATE — partial patch
|
|
33
|
-
// await db.collection('laptops')
|
|
34
|
-
// .update({ lp4: { price: 1749, in_stock: true } })
|
|
35
|
-
// .exec();
|
|
36
|
-
//
|
|
37
|
-
// // DELETE — single key, batch, or drop
|
|
38
|
-
// await db.collection('laptops').delete().keys('lp6').exec();
|
|
39
|
-
// await db.collection('laptops').delete().keys(['lp4', 'lp5']).exec();
|
|
40
|
-
// await db.collection('laptops').delete().drop().exec();
|
|
41
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
42
|
-
// ─── WorkerTransport ──────────────────────────────────────────────────────────
|
|
43
|
-
/**
|
|
44
|
-
* Default transport that communicates with a MoltenDB Web Worker.
|
|
45
|
-
*
|
|
46
|
-
* The worker must follow the moltendb-worker.js message protocol:
|
|
47
|
-
* postMessage({ id, action, ...payload })
|
|
48
|
-
* onmessage → { id, result } | { id, error }
|
|
49
|
-
*/
|
|
50
|
-
export class WorkerTransport {
|
|
51
|
-
constructor(worker, startId = 0) {
|
|
52
|
-
this.pending = new Map();
|
|
53
|
-
this.messageId = startId;
|
|
54
|
-
this.worker = worker;
|
|
55
|
-
this.worker.addEventListener('message', (event) => {
|
|
56
|
-
const { id, result, error } = event.data;
|
|
57
|
-
const p = this.pending.get(id);
|
|
58
|
-
if (!p)
|
|
59
|
-
return;
|
|
60
|
-
this.pending.delete(id);
|
|
61
|
-
if (error)
|
|
62
|
-
p.reject(new Error(error));
|
|
63
|
-
else
|
|
64
|
-
p.resolve(result ?? null);
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
send(action, payload) {
|
|
68
|
-
return new Promise((resolve, reject) => {
|
|
69
|
-
const id = this.messageId++;
|
|
70
|
-
this.pending.set(id, { resolve, reject });
|
|
71
|
-
this.worker.postMessage({ id, action, ...payload });
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
// ─── GetQuery ─────────────────────────────────────────────────────────────────
|
|
76
|
-
/**
|
|
77
|
-
* Builder for GET (read/query) operations.
|
|
78
|
-
*
|
|
79
|
-
* Allowed fields: collection, keys, where, fields, excludedFields,
|
|
80
|
-
* joins, sort, count, offset
|
|
81
|
-
*
|
|
82
|
-
* @example
|
|
83
|
-
* const rows = await db.collection('laptops')
|
|
84
|
-
* .get()
|
|
85
|
-
* .where({ brand: 'Apple' })
|
|
86
|
-
* .fields(['brand', 'model', 'price'])
|
|
87
|
-
* .sort([{ field: 'price', order: 'asc' }])
|
|
88
|
-
* .count(5)
|
|
89
|
-
* .exec();
|
|
90
|
-
*/
|
|
91
|
-
export class GetQuery {
|
|
92
|
-
/** @internal */
|
|
93
|
-
constructor(transport, collection) {
|
|
94
|
-
this.transport = transport;
|
|
95
|
-
this.payload = { collection };
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Fetch a single document by key, or a batch of documents by key array.
|
|
99
|
-
*
|
|
100
|
-
* @param keys - A single key string or an array of key strings.
|
|
101
|
-
*
|
|
102
|
-
* @example
|
|
103
|
-
* // Single key
|
|
104
|
-
* .keys('lp2')
|
|
105
|
-
* // Batch
|
|
106
|
-
* .keys(['lp1', 'lp3', 'lp5'])
|
|
107
|
-
*/
|
|
108
|
-
keys(keys) {
|
|
109
|
-
this.payload['keys'] = keys;
|
|
110
|
-
return this;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Filter documents using a WHERE clause.
|
|
114
|
-
* Multiple conditions are combined with implicit AND.
|
|
115
|
-
*
|
|
116
|
-
* Supported operators: $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $contains
|
|
117
|
-
* Dot-notation is supported for nested fields.
|
|
118
|
-
*
|
|
119
|
-
* @example
|
|
120
|
-
* .where({ brand: 'Apple' })
|
|
121
|
-
* .where({ price: { $gt: 1000, $lt: 3000 } })
|
|
122
|
-
* .where({ 'specs.cpu.brand': 'Intel', in_stock: true })
|
|
123
|
-
*/
|
|
124
|
-
where(clause) {
|
|
125
|
-
this.payload['where'] = clause;
|
|
126
|
-
return this;
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Project the response to only the specified fields (dot-notation supported).
|
|
130
|
-
* Cannot be combined with {@link excludedFields}.
|
|
131
|
-
*
|
|
132
|
-
* @example
|
|
133
|
-
* .fields(['brand', 'model', 'specs.cpu.ghz'])
|
|
134
|
-
*/
|
|
135
|
-
fields(fields) {
|
|
136
|
-
this.payload['fields'] = fields;
|
|
137
|
-
return this;
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Return all fields except the specified ones.
|
|
141
|
-
* Cannot be combined with {@link fields}.
|
|
142
|
-
*
|
|
143
|
-
* @example
|
|
144
|
-
* .excludedFields(['price', 'memory_id', 'display_id'])
|
|
145
|
-
*/
|
|
146
|
-
excludedFields(fields) {
|
|
147
|
-
this.payload['excludedFields'] = fields;
|
|
148
|
-
return this;
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Join related documents from other collections.
|
|
152
|
-
* Each join embeds the related document under the given alias.
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* .joins([
|
|
156
|
-
* { alias: 'ram', from: 'memory', on: 'memory_id', fields: ['capacity_gb', 'type'] },
|
|
157
|
-
* { alias: 'screen', from: 'display', on: 'display_id', fields: ['refresh_hz', 'panel'] },
|
|
158
|
-
* ])
|
|
159
|
-
*/
|
|
160
|
-
joins(specs) {
|
|
161
|
-
this.payload['joins'] = specs.map(({ alias, from, on, fields }) => ({
|
|
162
|
-
[alias]: fields ? { from, on, fields } : { from, on },
|
|
163
|
-
}));
|
|
164
|
-
return this;
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Sort the results.
|
|
168
|
-
* Multiple specs are applied in priority order (first = primary sort key).
|
|
169
|
-
*
|
|
170
|
-
* @example
|
|
171
|
-
* .sort([{ field: 'price', order: 'asc' }])
|
|
172
|
-
* .sort([{ field: 'brand', order: 'asc' }, { field: 'price', order: 'desc' }])
|
|
173
|
-
*/
|
|
174
|
-
sort(specs) {
|
|
175
|
-
this.payload['sort'] = specs;
|
|
176
|
-
return this;
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Limit the number of results returned (applied after filtering and sorting).
|
|
180
|
-
*
|
|
181
|
-
* @example
|
|
182
|
-
* .count(10)
|
|
183
|
-
*/
|
|
184
|
-
count(n) {
|
|
185
|
-
this.payload['count'] = n;
|
|
186
|
-
return this;
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Skip the first N results (for pagination, applied after sorting).
|
|
190
|
-
*
|
|
191
|
-
* @example
|
|
192
|
-
* .offset(20).count(10) // page 3 of 10
|
|
193
|
-
*/
|
|
194
|
-
offset(n) {
|
|
195
|
-
this.payload['offset'] = n;
|
|
196
|
-
return this;
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Build and return the raw JSON payload without sending it.
|
|
200
|
-
* Useful for debugging or passing to a custom transport.
|
|
201
|
-
*/
|
|
202
|
-
build() {
|
|
203
|
-
return { ...this.payload };
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Execute the query and return the result.
|
|
207
|
-
* Returns a single document for single-key lookups, or an array for all others.
|
|
208
|
-
*/
|
|
209
|
-
exec() {
|
|
210
|
-
return this.transport.send('get', this.payload);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
// ─── SetQuery ─────────────────────────────────────────────────────────────────
|
|
214
|
-
/**
|
|
215
|
-
* Builder for SET (insert / upsert) operations.
|
|
216
|
-
*
|
|
217
|
-
* Allowed fields: collection, data, extends
|
|
218
|
-
*
|
|
219
|
-
* @example
|
|
220
|
-
* await db.collection('laptops')
|
|
221
|
-
* .set({
|
|
222
|
-
* lp1: { brand: 'Lenovo', model: 'ThinkPad X1', price: 1499 },
|
|
223
|
-
* lp2: { brand: 'Apple', model: 'MacBook Pro', price: 3499 },
|
|
224
|
-
* })
|
|
225
|
-
* .exec();
|
|
226
|
-
*/
|
|
227
|
-
export class SetQuery {
|
|
228
|
-
/** @internal */
|
|
229
|
-
constructor(transport, collection, data) {
|
|
230
|
-
this.transport = transport;
|
|
231
|
-
this.payload = { collection, data: data };
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* Embed data from other collections into each document at insert time.
|
|
235
|
-
* The referenced document is fetched once and stored as a snapshot.
|
|
236
|
-
*
|
|
237
|
-
* Format: `{ alias: "collection.key" }`
|
|
238
|
-
*
|
|
239
|
-
* @example
|
|
240
|
-
* .extends({ ram: 'memory.mem4', screen: 'display.dsp3' })
|
|
241
|
-
*/
|
|
242
|
-
extends(map) {
|
|
243
|
-
// The extends map is applied to every document in data.
|
|
244
|
-
// We store it at the top level; the server resolves it per-document.
|
|
245
|
-
const data = this.payload['data'];
|
|
246
|
-
if (Array.isArray(data)) {
|
|
247
|
-
// Array format — inject extends into each item
|
|
248
|
-
this.payload['data'] = data.map((doc) => ({
|
|
249
|
-
...doc,
|
|
250
|
-
extends: map,
|
|
251
|
-
}));
|
|
252
|
-
}
|
|
253
|
-
else if (data && typeof data === 'object') {
|
|
254
|
-
// Object format — inject extends into each document value
|
|
255
|
-
const updated = {};
|
|
256
|
-
for (const [key, doc] of Object.entries(data)) {
|
|
257
|
-
updated[key] = { ...doc, extends: map };
|
|
258
|
-
}
|
|
259
|
-
this.payload['data'] = updated;
|
|
260
|
-
}
|
|
261
|
-
return this;
|
|
262
|
-
}
|
|
263
|
-
/** Build and return the raw JSON payload without sending it. */
|
|
264
|
-
build() {
|
|
265
|
-
return { ...this.payload };
|
|
266
|
-
}
|
|
267
|
-
/** Execute the insert/upsert and return `{ count, status }`. */
|
|
268
|
-
exec() {
|
|
269
|
-
return this.transport.send('set', this.payload);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
// ─── UpdateQuery ──────────────────────────────────────────────────────────────
|
|
273
|
-
/**
|
|
274
|
-
* Builder for UPDATE (partial patch / merge) operations.
|
|
275
|
-
*
|
|
276
|
-
* Allowed fields: collection, data
|
|
277
|
-
*
|
|
278
|
-
* Only the fields present in each patch object are updated —
|
|
279
|
-
* all other fields on the existing document are left unchanged.
|
|
280
|
-
*
|
|
281
|
-
* @example
|
|
282
|
-
* await db.collection('laptops')
|
|
283
|
-
* .update({ lp4: { price: 1749, in_stock: true } })
|
|
284
|
-
* .exec();
|
|
285
|
-
*/
|
|
286
|
-
export class UpdateQuery {
|
|
287
|
-
/** @internal */
|
|
288
|
-
constructor(transport, collection, data) {
|
|
289
|
-
this.transport = transport;
|
|
290
|
-
this.payload = { collection, data: data };
|
|
291
|
-
}
|
|
292
|
-
/** Build and return the raw JSON payload without sending it. */
|
|
293
|
-
build() {
|
|
294
|
-
return { ...this.payload };
|
|
295
|
-
}
|
|
296
|
-
/** Execute the patch and return `{ count, status }`. */
|
|
297
|
-
exec() {
|
|
298
|
-
return this.transport.send('update', this.payload);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
// ─── DeleteQuery ──────────────────────────────────────────────────────────────
|
|
302
|
-
/**
|
|
303
|
-
* Builder for DELETE operations.
|
|
304
|
-
*
|
|
305
|
-
* Allowed fields: collection, keys, drop
|
|
306
|
-
*
|
|
307
|
-
* @example
|
|
308
|
-
* // Delete a single document
|
|
309
|
-
* await db.collection('laptops').delete().keys('lp6').exec();
|
|
310
|
-
*
|
|
311
|
-
* // Delete multiple documents
|
|
312
|
-
* await db.collection('laptops').delete().keys(['lp4', 'lp5']).exec();
|
|
313
|
-
*
|
|
314
|
-
* // Drop the entire collection
|
|
315
|
-
* await db.collection('laptops').delete().drop().exec();
|
|
316
|
-
*/
|
|
317
|
-
export class DeleteQuery {
|
|
318
|
-
/** @internal */
|
|
319
|
-
constructor(transport, collection) {
|
|
320
|
-
this.transport = transport;
|
|
321
|
-
this.payload = { collection };
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* Delete a single document by key, or multiple documents by key array.
|
|
325
|
-
*
|
|
326
|
-
* @example
|
|
327
|
-
* .keys('lp6')
|
|
328
|
-
* .keys(['lp4', 'lp5'])
|
|
329
|
-
*/
|
|
330
|
-
keys(keys) {
|
|
331
|
-
this.payload['keys'] = keys;
|
|
332
|
-
return this;
|
|
333
|
-
}
|
|
334
|
-
/**
|
|
335
|
-
* Drop the entire collection (deletes all documents).
|
|
336
|
-
* Cannot be combined with {@link keys}.
|
|
337
|
-
*
|
|
338
|
-
* @example
|
|
339
|
-
* .drop()
|
|
340
|
-
*/
|
|
341
|
-
drop() {
|
|
342
|
-
this.payload['drop'] = true;
|
|
343
|
-
return this;
|
|
344
|
-
}
|
|
345
|
-
/** Build and return the raw JSON payload without sending it. */
|
|
346
|
-
build() {
|
|
347
|
-
return { ...this.payload };
|
|
348
|
-
}
|
|
349
|
-
/** Execute the delete and return `{ count, status }`. */
|
|
350
|
-
exec() {
|
|
351
|
-
return this.transport.send('delete', this.payload);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
// ─── CollectionHandle ─────────────────────────────────────────────────────────
|
|
355
|
-
/**
|
|
356
|
-
* A handle to a specific collection.
|
|
357
|
-
* Returned by `MoltenDBClient.collection(name)`.
|
|
358
|
-
* Use it to start any of the four operation builders.
|
|
359
|
-
*/
|
|
360
|
-
export class CollectionHandle {
|
|
361
|
-
/** @internal */
|
|
362
|
-
constructor(transport, collectionName) {
|
|
363
|
-
this.transport = transport;
|
|
364
|
-
this.collectionName = collectionName;
|
|
365
|
-
}
|
|
366
|
-
/**
|
|
367
|
-
* Start a GET (read/query) builder for this collection.
|
|
368
|
-
*
|
|
369
|
-
* @example
|
|
370
|
-
* db.collection('laptops').get().where({ brand: 'Apple' }).exec()
|
|
371
|
-
*/
|
|
372
|
-
get() {
|
|
373
|
-
return new GetQuery(this.transport, this.collectionName);
|
|
374
|
-
}
|
|
375
|
-
/**
|
|
376
|
-
* Start a SET (insert/upsert) builder for this collection.
|
|
377
|
-
*
|
|
378
|
-
* @param data - A map of `{ key: document }` pairs, or an array of documents
|
|
379
|
-
* (keys are auto-generated as UUIDv7 when using array format).
|
|
380
|
-
*
|
|
381
|
-
* @example
|
|
382
|
-
* db.collection('laptops').set({ lp1: { brand: 'Lenovo', price: 1499 } }).exec()
|
|
383
|
-
*/
|
|
384
|
-
set(data) {
|
|
385
|
-
return new SetQuery(this.transport, this.collectionName, data);
|
|
386
|
-
}
|
|
387
|
-
/**
|
|
388
|
-
* Start an UPDATE (partial patch) builder for this collection.
|
|
389
|
-
*
|
|
390
|
-
* @param data - A map of `{ key: patch }` pairs. Only the provided fields are updated.
|
|
391
|
-
*
|
|
392
|
-
* @example
|
|
393
|
-
* db.collection('laptops').update({ lp4: { price: 1749 } }).exec()
|
|
394
|
-
*/
|
|
395
|
-
update(data) {
|
|
396
|
-
return new UpdateQuery(this.transport, this.collectionName, data);
|
|
397
|
-
}
|
|
398
|
-
/**
|
|
399
|
-
* Start a DELETE builder for this collection.
|
|
400
|
-
* Chain `.keys(...)` or `.drop()` to specify what to delete.
|
|
401
|
-
*
|
|
402
|
-
* @example
|
|
403
|
-
* db.collection('laptops').delete().keys('lp6').exec()
|
|
404
|
-
*/
|
|
405
|
-
delete() {
|
|
406
|
-
return new DeleteQuery(this.transport, this.collectionName);
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
// ─── MoltenDBClient ───────────────────────────────────────────────────────────
|
|
410
|
-
/**
|
|
411
|
-
* The main entry point for the MoltenDB query builder.
|
|
412
|
-
*
|
|
413
|
-
* Accepts any {@link MoltenTransport} implementation — use {@link WorkerTransport}
|
|
414
|
-
* to connect to a MoltenDB WASM Web Worker, or provide your own transport
|
|
415
|
-
* for HTTP, WebSocket, or testing.
|
|
416
|
-
*
|
|
417
|
-
* @example
|
|
418
|
-
* // Browser + WASM Web Worker
|
|
419
|
-
* const worker = new Worker('./moltendb-worker.js', { type: 'module' });
|
|
420
|
-
* const transport = new WorkerTransport(worker);
|
|
421
|
-
* const db = new MoltenDBClient(transport);
|
|
422
|
-
*
|
|
423
|
-
* const results = await db.collection('laptops')
|
|
424
|
-
* .get()
|
|
425
|
-
* .where({ in_stock: true })
|
|
426
|
-
* .sort([{ field: 'price', order: 'asc' }])
|
|
427
|
-
* .count(5)
|
|
428
|
-
* .exec();
|
|
429
|
-
*/
|
|
430
|
-
export class MoltenDBClient {
|
|
431
|
-
constructor(transport) {
|
|
432
|
-
this.transport = transport;
|
|
433
|
-
}
|
|
434
|
-
/**
|
|
435
|
-
* Select a collection to operate on.
|
|
436
|
-
* Returns a {@link CollectionHandle} from which you can start any query builder.
|
|
437
|
-
*
|
|
438
|
-
* @param name - The collection name (e.g. `'laptops'`).
|
|
439
|
-
*/
|
|
440
|
-
collection(name) {
|
|
441
|
-
return new CollectionHandle(this.transport, name);
|
|
442
|
-
}
|
|
443
|
-
}
|