@storecraft/database-mongodb 1.0.1
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/README.md +50 -0
- package/db-strategy.md +284 -0
- package/index.js +193 -0
- package/jsconfig.json +14 -0
- package/migrate.js +39 -0
- package/migrations/00000_init_tables.js +60 -0
- package/migrations/00001_seed_email_templates.js +271 -0
- package/package.json +38 -0
- package/src/con.auth_users.js +147 -0
- package/src/con.collections.js +232 -0
- package/src/con.customers.js +172 -0
- package/src/con.discounts.js +261 -0
- package/src/con.discounts.utils.js +137 -0
- package/src/con.images.js +173 -0
- package/src/con.notifications.js +101 -0
- package/src/con.orders.js +61 -0
- package/src/con.posts.js +149 -0
- package/src/con.products.js +537 -0
- package/src/con.search.js +162 -0
- package/src/con.shared.js +333 -0
- package/src/con.shipping.js +153 -0
- package/src/con.storefronts.js +223 -0
- package/src/con.tags.js +62 -0
- package/src/con.templates.js +62 -0
- package/src/utils.funcs.js +152 -0
- package/src/utils.query.js +186 -0
- package/src/utils.relations.js +410 -0
- package/tests/mongo-ping.test.js +34 -0
- package/tests/query.cursor.test.js +389 -0
- package/tests/query.vql.test.js +71 -0
- package/tests/runner.test.js +35 -0
- package/tests/sandbox.test.js +56 -0
- package/types.public.d.ts +22 -0
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import { test } from 'uvu';
|
|
2
|
+
import * as assert from 'uvu/assert';
|
|
3
|
+
import { query_cursor_to_mongo } from '../src/utils.query.js'
|
|
4
|
+
|
|
5
|
+
test('(a1, a2, a3)', async () => {
|
|
6
|
+
const t_1 = {
|
|
7
|
+
"$or": [
|
|
8
|
+
{
|
|
9
|
+
"$and": [
|
|
10
|
+
{
|
|
11
|
+
"a1": {
|
|
12
|
+
"$gt": "b1"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
]
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"$and": [
|
|
19
|
+
{
|
|
20
|
+
"a1": "b1"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"a2": {
|
|
24
|
+
"$gt": "b2"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"$and": [
|
|
31
|
+
{
|
|
32
|
+
"a1": "b1"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"a2": "b2"
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"a3": {
|
|
39
|
+
"$gte": "b3"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
]
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const t_2 = {
|
|
48
|
+
"$or": [
|
|
49
|
+
{
|
|
50
|
+
"$and": [
|
|
51
|
+
{
|
|
52
|
+
"a1": {
|
|
53
|
+
"$gt": "b1"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
]
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
"$and": [
|
|
60
|
+
{
|
|
61
|
+
"a1": "b1"
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"a2": {
|
|
65
|
+
"$gt": "b2"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
]
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"$and": [
|
|
72
|
+
{
|
|
73
|
+
"a1": "b1"
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
"a2": "b2"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"a3": {
|
|
80
|
+
"$gt": "b3"
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
}
|
|
85
|
+
]
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const t_3 = {
|
|
89
|
+
"$or": [
|
|
90
|
+
{
|
|
91
|
+
"$and": [
|
|
92
|
+
{
|
|
93
|
+
"a1": {
|
|
94
|
+
"$lt": "b1"
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
"$and": [
|
|
101
|
+
{
|
|
102
|
+
"a1": "b1"
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
"a2": {
|
|
106
|
+
"$lt": "b2"
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
]
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
"$and": [
|
|
113
|
+
{
|
|
114
|
+
"a1": "b1"
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
"a2": "b2"
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
"a3": {
|
|
121
|
+
"$lte": "b3"
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
]
|
|
125
|
+
}
|
|
126
|
+
]
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const t_4 = {
|
|
130
|
+
"$or": [
|
|
131
|
+
{
|
|
132
|
+
"$and": [
|
|
133
|
+
{
|
|
134
|
+
"a1": {
|
|
135
|
+
"$lt": "b1"
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
]
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
"$and": [
|
|
142
|
+
{
|
|
143
|
+
"a1": "b1"
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
"a2": {
|
|
147
|
+
"$lt": "b2"
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
]
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
"$and": [
|
|
154
|
+
{
|
|
155
|
+
"a1": "b1"
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
"a2": "b2"
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
"a3": {
|
|
162
|
+
"$lt": "b3"
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
]
|
|
166
|
+
}
|
|
167
|
+
]
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/** @type {import('@storecraft/core').Cursor[]} */
|
|
171
|
+
const cursor = [
|
|
172
|
+
['a1', 'b1'],
|
|
173
|
+
['a2', 'b2'],
|
|
174
|
+
['a3', 'b3'],
|
|
175
|
+
];
|
|
176
|
+
|
|
177
|
+
const rel_1 = query_cursor_to_mongo(cursor, '>=');
|
|
178
|
+
const rel_2 = query_cursor_to_mongo(cursor, '>');
|
|
179
|
+
const rel_3 = query_cursor_to_mongo(cursor, '<=');
|
|
180
|
+
const rel_4 = query_cursor_to_mongo(cursor, '<');
|
|
181
|
+
|
|
182
|
+
assert.equal(rel_1, t_1);
|
|
183
|
+
assert.equal(rel_2, t_2);
|
|
184
|
+
assert.equal(rel_3, t_3);
|
|
185
|
+
assert.equal(rel_4, t_4);
|
|
186
|
+
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
test('(a1, a2)', async () => {
|
|
191
|
+
const t_1 = {
|
|
192
|
+
"$or": [
|
|
193
|
+
{
|
|
194
|
+
"$and": [
|
|
195
|
+
{
|
|
196
|
+
"a1": {
|
|
197
|
+
"$gt": "b1"
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
]
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
"$and": [
|
|
204
|
+
{
|
|
205
|
+
"a1": "b1"
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
"a2": {
|
|
209
|
+
"$gte": "b2"
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
]
|
|
213
|
+
},
|
|
214
|
+
]
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
const t_2 = {
|
|
218
|
+
"$or": [
|
|
219
|
+
{
|
|
220
|
+
"$and": [
|
|
221
|
+
{
|
|
222
|
+
"a1": {
|
|
223
|
+
"$gt": "b1"
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
]
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
"$and": [
|
|
230
|
+
{
|
|
231
|
+
"a1": "b1"
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
"a2": {
|
|
235
|
+
"$gt": "b2"
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
]
|
|
239
|
+
},
|
|
240
|
+
]
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const t_3 = {
|
|
244
|
+
"$or": [
|
|
245
|
+
{
|
|
246
|
+
"$and": [
|
|
247
|
+
{
|
|
248
|
+
"a1": {
|
|
249
|
+
"$lt": "b1"
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
]
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
"$and": [
|
|
256
|
+
{
|
|
257
|
+
"a1": "b1"
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
"a2": {
|
|
261
|
+
"$lte": "b2"
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
]
|
|
265
|
+
},
|
|
266
|
+
]
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const t_4 = {
|
|
270
|
+
"$or": [
|
|
271
|
+
{
|
|
272
|
+
"$and": [
|
|
273
|
+
{
|
|
274
|
+
"a1": {
|
|
275
|
+
"$lt": "b1"
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
]
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
"$and": [
|
|
282
|
+
{
|
|
283
|
+
"a1": "b1"
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
"a2": {
|
|
287
|
+
"$lt": "b2"
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
]
|
|
291
|
+
},
|
|
292
|
+
]
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
/** @type {import('@storecraft/core').Cursor[]} */
|
|
296
|
+
const cursor = [
|
|
297
|
+
['a1', 'b1'],
|
|
298
|
+
['a2', 'b2'],
|
|
299
|
+
];
|
|
300
|
+
|
|
301
|
+
const rel_1 = query_cursor_to_mongo(cursor, '>=');
|
|
302
|
+
const rel_2 = query_cursor_to_mongo(cursor, '>');
|
|
303
|
+
const rel_3 = query_cursor_to_mongo(cursor, '<=');
|
|
304
|
+
const rel_4 = query_cursor_to_mongo(cursor, '<');
|
|
305
|
+
|
|
306
|
+
assert.equal(rel_1, t_1);
|
|
307
|
+
assert.equal(rel_2, t_2);
|
|
308
|
+
assert.equal(rel_3, t_3);
|
|
309
|
+
assert.equal(rel_4, t_4);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
test('(a1)', async () => {
|
|
315
|
+
const t_1 = {
|
|
316
|
+
"$or": [
|
|
317
|
+
{
|
|
318
|
+
"$and": [
|
|
319
|
+
{
|
|
320
|
+
"a1": {
|
|
321
|
+
"$gte": "b1"
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
]
|
|
325
|
+
},
|
|
326
|
+
]
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
const t_2 = {
|
|
330
|
+
"$or": [
|
|
331
|
+
{
|
|
332
|
+
"$and": [
|
|
333
|
+
{
|
|
334
|
+
"a1": {
|
|
335
|
+
"$gt": "b1"
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
]
|
|
339
|
+
},
|
|
340
|
+
]
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const t_3 = {
|
|
344
|
+
"$or": [
|
|
345
|
+
{
|
|
346
|
+
"$and": [
|
|
347
|
+
{
|
|
348
|
+
"a1": {
|
|
349
|
+
"$lte": "b1"
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
]
|
|
353
|
+
},
|
|
354
|
+
]
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
const t_4 = {
|
|
358
|
+
"$or": [
|
|
359
|
+
{
|
|
360
|
+
"$and": [
|
|
361
|
+
{
|
|
362
|
+
"a1": {
|
|
363
|
+
"$lt": "b1"
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
]
|
|
367
|
+
},
|
|
368
|
+
]
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
/** @type {import('@storecraft/core').Cursor[]} */
|
|
372
|
+
const cursor = [
|
|
373
|
+
['a1', 'b1'],
|
|
374
|
+
];
|
|
375
|
+
|
|
376
|
+
const rel_1 = query_cursor_to_mongo(cursor, '>=');
|
|
377
|
+
const rel_2 = query_cursor_to_mongo(cursor, '>');
|
|
378
|
+
const rel_3 = query_cursor_to_mongo(cursor, '<=');
|
|
379
|
+
const rel_4 = query_cursor_to_mongo(cursor, '<');
|
|
380
|
+
|
|
381
|
+
assert.equal(rel_1, t_1);
|
|
382
|
+
assert.equal(rel_2, t_2);
|
|
383
|
+
assert.equal(rel_3, t_3);
|
|
384
|
+
assert.equal(rel_4, t_4);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
test.run();
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { test } from 'uvu';
|
|
2
|
+
import * as assert from 'uvu/assert';
|
|
3
|
+
import { query_vql_to_mongo } from '../src/utils.query.js'
|
|
4
|
+
|
|
5
|
+
test('VQL', async () => {
|
|
6
|
+
const vql_ast = {
|
|
7
|
+
op: '&',
|
|
8
|
+
args: [
|
|
9
|
+
{
|
|
10
|
+
op: 'LEAF',
|
|
11
|
+
value: 'name:tomer'
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
op: '&',
|
|
15
|
+
args: [
|
|
16
|
+
{
|
|
17
|
+
op: 'LEAF',
|
|
18
|
+
value: 'tag:genre_a'
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
op: '!',
|
|
22
|
+
args: [
|
|
23
|
+
{
|
|
24
|
+
op: 'LEAF',
|
|
25
|
+
value: 'tag:genre_b'
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
],
|
|
30
|
+
group: true
|
|
31
|
+
}
|
|
32
|
+
],
|
|
33
|
+
group: true
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const mongo = {
|
|
37
|
+
"$and": [
|
|
38
|
+
{
|
|
39
|
+
"search": {
|
|
40
|
+
"$regex": "^name:tomer$"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"$and": [
|
|
45
|
+
{
|
|
46
|
+
"search": {
|
|
47
|
+
"$regex": "^tag:genre_a$"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"$nor": [
|
|
52
|
+
{
|
|
53
|
+
"search": {
|
|
54
|
+
"$regex": "^tag:genre_b$"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
]
|
|
60
|
+
}
|
|
61
|
+
]
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const m1 = query_vql_to_mongo(vql_ast);
|
|
65
|
+
|
|
66
|
+
// console.log(JSON.stringify(m1, null, 2))
|
|
67
|
+
|
|
68
|
+
assert.equal(m1, mongo);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test.run();
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { App } from '@storecraft/core';
|
|
2
|
+
import { MongoDB, migrateToLatest } from '@storecraft/database-mongodb';
|
|
3
|
+
import { NodePlatform } from '@storecraft/platforms/node';
|
|
4
|
+
import { api_index } from '@storecraft/test-runner'
|
|
5
|
+
|
|
6
|
+
export const create_app = async () => {
|
|
7
|
+
const app = new App(
|
|
8
|
+
{
|
|
9
|
+
auth_admins_emails: ['admin@sc.com'],
|
|
10
|
+
auth_secret_access_token: 'auth_secret_access_token',
|
|
11
|
+
auth_secret_refresh_token: 'auth_secret_refresh_token'
|
|
12
|
+
}
|
|
13
|
+
)
|
|
14
|
+
.withPlatform(new NodePlatform())
|
|
15
|
+
.withDatabase(new MongoDB({ db_name: 'test'}))
|
|
16
|
+
|
|
17
|
+
return app.init();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function test() {
|
|
21
|
+
const app = await create_app();
|
|
22
|
+
|
|
23
|
+
await migrateToLatest(app.db, false);
|
|
24
|
+
|
|
25
|
+
Object.entries(api_index).slice(0, -1).forEach(
|
|
26
|
+
([name, runner]) => {
|
|
27
|
+
runner.create(app).run();
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
const last_test = Object.values(api_index).at(-1).create(app);
|
|
31
|
+
last_test.after(async ()=>{app.db.disconnect()});
|
|
32
|
+
last_test.run();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
test();
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { App } from '@storecraft/core';
|
|
2
|
+
import { MongoDB } from '@storecraft/database-mongodb';
|
|
3
|
+
import { migrateToLatest } from '@storecraft/database-mongodb/migrate.js';
|
|
4
|
+
import { NodePlatform } from '@storecraft/platforms/node';
|
|
5
|
+
|
|
6
|
+
export const create_app = async () => {
|
|
7
|
+
const app = new App(
|
|
8
|
+
{
|
|
9
|
+
auth_admins_emails: ['admin@sc.com'],
|
|
10
|
+
auth_secret_access_token: 'auth_secret_access_token',
|
|
11
|
+
auth_secret_refresh_token: 'auth_secret_refresh_token'
|
|
12
|
+
}
|
|
13
|
+
)
|
|
14
|
+
.withPlatform(new NodePlatform())
|
|
15
|
+
.withDatabase(new MongoDB({ db_name: 'test'}))
|
|
16
|
+
|
|
17
|
+
return app.init();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
*
|
|
22
|
+
* @template R
|
|
23
|
+
*
|
|
24
|
+
* @param {(()=>Promise<R>) | (() => R)} fn
|
|
25
|
+
*/
|
|
26
|
+
const withTime = async (fn) => {
|
|
27
|
+
const n1 = Date.now() ;
|
|
28
|
+
const r = await fn();
|
|
29
|
+
const delta = Date.now() - n1;
|
|
30
|
+
console.log(delta);
|
|
31
|
+
return r;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function test() {
|
|
35
|
+
const app = await withTime(create_app);
|
|
36
|
+
|
|
37
|
+
await migrateToLatest(app.db, false);
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
const doit = async () => {
|
|
41
|
+
let items = await app.db.resources.search.quicksearch(
|
|
42
|
+
{
|
|
43
|
+
vql: '',
|
|
44
|
+
sortBy: ['updated_at']
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
return items;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const items = await withTime(doit);
|
|
51
|
+
|
|
52
|
+
// console.log('items ', items)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
test();
|
|
56
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { MongoClientOptions } from 'mongodb';
|
|
2
|
+
|
|
3
|
+
export { MongoDB, migrateToLatest } from './index.js';
|
|
4
|
+
|
|
5
|
+
export type Config = {
|
|
6
|
+
/**
|
|
7
|
+
* @description mongo connection url, if absent, will be infered at init
|
|
8
|
+
* with env `app.platform.env.MONGODB_URL`
|
|
9
|
+
*/
|
|
10
|
+
url?: string;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @description the name of the database, if absent, will be infered at init
|
|
14
|
+
* with env `app.platform.env.MONGODB_NAME`
|
|
15
|
+
*/
|
|
16
|
+
db_name?: string;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @description mongo client options
|
|
20
|
+
*/
|
|
21
|
+
options?: MongoClientOptions;
|
|
22
|
+
}
|