dynamo-bao 0.8.1 → 0.9.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/CHANGELOG.md +103 -1
- package/README.md +3 -3
- package/bin/bao-rebuild-search-text.js +40 -14
- package/bin/dynamo-bao-update-table.js +20 -3
- package/bin/generators/manifest.js +1 -1
- package/bin/generators/model.js +7 -8
- package/bin/lib/confirm.js +24 -0
- package/bin/lib/process-models.js +73 -2
- package/package.json +2 -1
- package/src/config.js +21 -11
- package/src/constants.js +4 -0
- package/src/dynamodb-client.js +18 -1
- package/src/fields.js +55 -1
- package/src/filter-expression.js +46 -17
- package/src/index.js +4 -3
- package/src/key-condition.js +2 -2
- package/src/mixins/batch-loading-mixin.js +8 -0
- package/src/mixins/mutation-mixin.js +95 -17
- package/src/mixins/query-mixin.js +4 -1
- package/src/mixins/unique-constraint-mixin.js +1 -1
- package/src/model-manager.js +33 -4
- package/src/model.js +57 -11
- package/src/tenant-context.js +52 -26
- package/src/utils/search-text.js +1 -1
- package/bin/chokidar-test.js +0 -60
package/src/model.js
CHANGED
|
@@ -28,8 +28,6 @@ const {
|
|
|
28
28
|
UniqueConstraintConfig: UniqueConstraintConfigClass,
|
|
29
29
|
} = require("./model-config");
|
|
30
30
|
|
|
31
|
-
const GID_SEPARATOR = "##__SK__##";
|
|
32
|
-
|
|
33
31
|
// Passed to the constructor by _createFromDyItem: the instance is about to
|
|
34
32
|
// take its data from a stored row, so skip computing initial field values
|
|
35
33
|
// (which for auto-assigned ULID and version fields means generating ids
|
|
@@ -51,6 +49,7 @@ const {
|
|
|
51
49
|
ITERATION_PK_FIELD,
|
|
52
50
|
ITERATION_SK_FIELD,
|
|
53
51
|
SEARCH_TEXT_FIELD,
|
|
52
|
+
GID_SEPARATOR,
|
|
54
53
|
} = require("./constants");
|
|
55
54
|
const {
|
|
56
55
|
ConfigurationError,
|
|
@@ -115,7 +114,17 @@ class BaoModel {
|
|
|
115
114
|
static get manager() {
|
|
116
115
|
const { TenantContext } = require("./tenant-context");
|
|
117
116
|
const tenantId = TenantContext.getCurrentTenant();
|
|
118
|
-
|
|
117
|
+
if (tenantId) {
|
|
118
|
+
return ModelManager.forTenant(tenantId);
|
|
119
|
+
}
|
|
120
|
+
// Fail closed: never fall back to the tenant that was current at init.
|
|
121
|
+
if (this._config?.tenancy?.enabled) {
|
|
122
|
+
throw new ConfigurationError(
|
|
123
|
+
`No tenant in context for ${this.name}. Wrap the operation in ` +
|
|
124
|
+
"TenantContext.runWithTenant(tenantId, fn) or register a resolver.",
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
return ModelManager.getInstance(this._tenantId);
|
|
119
128
|
}
|
|
120
129
|
|
|
121
130
|
static _getField(fieldName) {
|
|
@@ -352,6 +361,8 @@ class BaoModel {
|
|
|
352
361
|
* and so the upper bound on each yielded batch.
|
|
353
362
|
* @param {Object} [options.filter] - Filter on attributes projected onto
|
|
354
363
|
* the iteration index.
|
|
364
|
+
* @param {string[]} [options.allowedFilterFields] - Fields `filter` may
|
|
365
|
+
* reference. Set this when the filter comes from user input.
|
|
355
366
|
* @param {number} [options.concurrency=1] - Buckets to read concurrently.
|
|
356
367
|
* @yields {BaoModel[]} One batch of loaded instances at a time.
|
|
357
368
|
*/
|
|
@@ -360,20 +371,33 @@ class BaoModel {
|
|
|
360
371
|
throw new Error(`Model ${this.name} is not configured as iterable`);
|
|
361
372
|
}
|
|
362
373
|
|
|
363
|
-
const {
|
|
374
|
+
const {
|
|
375
|
+
batchSize = 100,
|
|
376
|
+
filter = null,
|
|
377
|
+
allowedFilterFields,
|
|
378
|
+
concurrency = 1,
|
|
379
|
+
} = options;
|
|
364
380
|
if (!Number.isInteger(concurrency) || concurrency < 1) {
|
|
365
381
|
throw new Error("concurrency must be a positive integer");
|
|
366
382
|
}
|
|
367
383
|
|
|
368
384
|
if (this.iterationBuckets === 1) {
|
|
369
|
-
yield* this._iterateSingleBucket(null, {
|
|
385
|
+
yield* this._iterateSingleBucket(null, {
|
|
386
|
+
batchSize,
|
|
387
|
+
filter,
|
|
388
|
+
allowedFilterFields,
|
|
389
|
+
});
|
|
370
390
|
return;
|
|
371
391
|
}
|
|
372
392
|
|
|
373
393
|
const buckets = Array.from({ length: this.iterationBuckets }, (_, b) => b);
|
|
374
394
|
if (concurrency === 1) {
|
|
375
395
|
for (const bucket of buckets) {
|
|
376
|
-
yield* this._iterateSingleBucket(bucket, {
|
|
396
|
+
yield* this._iterateSingleBucket(bucket, {
|
|
397
|
+
batchSize,
|
|
398
|
+
filter,
|
|
399
|
+
allowedFilterFields,
|
|
400
|
+
});
|
|
377
401
|
}
|
|
378
402
|
return;
|
|
379
403
|
}
|
|
@@ -381,7 +405,11 @@ class BaoModel {
|
|
|
381
405
|
yield* this._interleaveGenerators(
|
|
382
406
|
buckets.map(
|
|
383
407
|
(bucket) => () =>
|
|
384
|
-
this._iterateSingleBucket(bucket, {
|
|
408
|
+
this._iterateSingleBucket(bucket, {
|
|
409
|
+
batchSize,
|
|
410
|
+
filter,
|
|
411
|
+
allowedFilterFields,
|
|
412
|
+
}),
|
|
385
413
|
),
|
|
386
414
|
concurrency,
|
|
387
415
|
);
|
|
@@ -515,6 +543,8 @@ class BaoModel {
|
|
|
515
543
|
* @param {Object} [options.filter] - Additional filter combined via AND
|
|
516
544
|
* with the search predicate. Must reference only attributes projected
|
|
517
545
|
* on `iter_search_index` (i.e., `_searchText` and the index/base keys).
|
|
546
|
+
* @param {string[]} [options.allowedFilterFields] - Fields `filter` may
|
|
547
|
+
* reference. Set this when the filter comes from user input.
|
|
518
548
|
* @returns {Promise<{items: BaoModel[], cursor: string|null}>} `items` is
|
|
519
549
|
* never longer than `limit`. `cursor` is `null` when the search is
|
|
520
550
|
* fully exhausted; otherwise pass it back to get the next page.
|
|
@@ -597,6 +627,7 @@ class BaoModel {
|
|
|
597
627
|
operator = "$and",
|
|
598
628
|
batchSize = 100,
|
|
599
629
|
filter = null,
|
|
630
|
+
allowedFilterFields,
|
|
600
631
|
limit = 100,
|
|
601
632
|
maxQueriesPerBucket = 50,
|
|
602
633
|
parallel = true,
|
|
@@ -693,6 +724,7 @@ class BaoModel {
|
|
|
693
724
|
const page = await this._searchSingleBucketPage(bucketArg(bucketIdx), {
|
|
694
725
|
batchSize,
|
|
695
726
|
filter,
|
|
727
|
+
allowedFilterFields,
|
|
696
728
|
searchPredicate,
|
|
697
729
|
exclusiveStartKey: lek || null,
|
|
698
730
|
});
|
|
@@ -908,6 +940,7 @@ class BaoModel {
|
|
|
908
940
|
const {
|
|
909
941
|
batchSize = 100,
|
|
910
942
|
filter = null,
|
|
943
|
+
allowedFilterFields,
|
|
911
944
|
searchPredicate = null,
|
|
912
945
|
exclusiveStartKey = null,
|
|
913
946
|
} = options;
|
|
@@ -929,7 +962,9 @@ class BaoModel {
|
|
|
929
962
|
if (filter) {
|
|
930
963
|
const { FilterExpressionBuilder } = require("./filter-expression");
|
|
931
964
|
const filterBuilder = new FilterExpressionBuilder();
|
|
932
|
-
const filterExpression = filterBuilder.build(filter, this
|
|
965
|
+
const filterExpression = filterBuilder.build(filter, this, {
|
|
966
|
+
allowedFields: allowedFilterFields,
|
|
967
|
+
});
|
|
933
968
|
if (filterExpression) {
|
|
934
969
|
this._assertFilterFitsIterIndex(
|
|
935
970
|
indexName,
|
|
@@ -973,7 +1008,12 @@ class BaoModel {
|
|
|
973
1008
|
}
|
|
974
1009
|
|
|
975
1010
|
static async *_iterateSingleBucket(bucketNum, options = {}) {
|
|
976
|
-
const {
|
|
1011
|
+
const {
|
|
1012
|
+
batchSize = 100,
|
|
1013
|
+
filter = null,
|
|
1014
|
+
allowedFilterFields,
|
|
1015
|
+
searchPredicate = null,
|
|
1016
|
+
} = options;
|
|
977
1017
|
const iterPk = this._getIterationPk(bucketNum);
|
|
978
1018
|
const indexName = this.manager.getIterationIndexName();
|
|
979
1019
|
|
|
@@ -993,7 +1033,9 @@ class BaoModel {
|
|
|
993
1033
|
if (filter) {
|
|
994
1034
|
const { FilterExpressionBuilder } = require("./filter-expression");
|
|
995
1035
|
const filterBuilder = new FilterExpressionBuilder();
|
|
996
|
-
const filterExpression = filterBuilder.build(filter, this
|
|
1036
|
+
const filterExpression = filterBuilder.build(filter, this, {
|
|
1037
|
+
allowedFields: allowedFilterFields,
|
|
1038
|
+
});
|
|
997
1039
|
if (filterExpression) {
|
|
998
1040
|
// Preflight: filter attributes must be in the GSI's projection.
|
|
999
1041
|
// Otherwise DynamoDB returns an opaque "does not project one or
|
|
@@ -1274,7 +1316,11 @@ class BaoModel {
|
|
|
1274
1316
|
}
|
|
1275
1317
|
|
|
1276
1318
|
if (primaryId.indexOf(GID_SEPARATOR) !== -1) {
|
|
1277
|
-
const
|
|
1319
|
+
const parts = primaryId.split(GID_SEPARATOR);
|
|
1320
|
+
if (parts.length !== 2) {
|
|
1321
|
+
throw new ValidationError(`Invalid primary id: ${primaryId}`);
|
|
1322
|
+
}
|
|
1323
|
+
const [pk, sk] = parts;
|
|
1278
1324
|
return { pk, sk };
|
|
1279
1325
|
} else {
|
|
1280
1326
|
if (this.primaryKey.pk === "modelPrefix") {
|
package/src/tenant-context.js
CHANGED
|
@@ -11,6 +11,10 @@ function getModelManager() {
|
|
|
11
11
|
return _ModelManager;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
function getBatchContextStore() {
|
|
15
|
+
return require('./mixins/batch-loading-mixin').getBatchContextStore();
|
|
16
|
+
}
|
|
17
|
+
|
|
14
18
|
/**
|
|
15
19
|
* @class TenantContext
|
|
16
20
|
* @description Manages tenant context for multi-tenant applications using DynamoBao.
|
|
@@ -18,46 +22,63 @@ function getModelManager() {
|
|
|
18
22
|
*/
|
|
19
23
|
class TenantContext {
|
|
20
24
|
static _asyncStorage = new AsyncLocalStorage();
|
|
21
|
-
static _instances = new Map(); // tenantId -> ModelManager
|
|
22
25
|
static _resolvers = [];
|
|
23
|
-
|
|
26
|
+
// Process-wide tenant, set by setCurrentTenant() outside any request
|
|
27
|
+
// context (scripts and tests). Never consulted inside runWithTenant().
|
|
28
|
+
static _fallbackTenant = null;
|
|
24
29
|
|
|
25
30
|
/**
|
|
26
31
|
* Sets the current tenant ID and returns the associated ModelManager instance.
|
|
27
|
-
*
|
|
32
|
+
*
|
|
33
|
+
* Inside runWithTenant() or runWithBatchContext() the tenant is set for that
|
|
34
|
+
* context only. Outside both, it sets a process-wide tenant, which is only
|
|
35
|
+
* appropriate for scripts and tests: concurrent requests that have no tenant
|
|
36
|
+
* of their own would see it.
|
|
28
37
|
* @param {string} tenantId - The tenant identifier
|
|
29
38
|
* @returns {ModelManager} The ModelManager instance for the tenant
|
|
30
39
|
*/
|
|
31
40
|
static setCurrentTenant(tenantId) {
|
|
32
41
|
const store = this._asyncStorage.getStore();
|
|
42
|
+
const batchStore = getBatchContextStore();
|
|
33
43
|
if (store) {
|
|
34
|
-
// We're inside an async context, set the tenant there
|
|
35
44
|
store.tenantId = tenantId;
|
|
45
|
+
} else if (batchStore) {
|
|
46
|
+
batchStore.tenantId = tenantId;
|
|
36
47
|
} else {
|
|
37
|
-
// We're not in an async context, use fallback
|
|
38
48
|
this._fallbackTenant = tenantId;
|
|
39
49
|
}
|
|
40
50
|
return this.getInstance(tenantId);
|
|
41
51
|
}
|
|
42
52
|
|
|
43
53
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
54
|
+
* Sets the process-wide tenant regardless of the current context. Meant for
|
|
55
|
+
* scripts, jobs and tests that handle one tenant at a time; never call it
|
|
56
|
+
* from request handling code.
|
|
57
|
+
* @param {string|null} tenantId - The tenant identifier, or null to clear it
|
|
58
|
+
* @returns {ModelManager} The ModelManager instance for the tenant
|
|
59
|
+
*/
|
|
60
|
+
static setDefaultTenant(tenantId) {
|
|
61
|
+
this._fallbackTenant = tenantId || null;
|
|
62
|
+
return this.getInstance(tenantId);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Gets the current tenant ID. Checked in order: the runWithTenant() context,
|
|
67
|
+
* a tenant set inside the current batch context, resolvers, and finally the
|
|
68
|
+
* process-wide tenant (only outside runWithTenant()).
|
|
46
69
|
* @returns {string|null} The current tenant ID or null if not found
|
|
47
70
|
*/
|
|
48
71
|
static getCurrentTenant() {
|
|
49
|
-
// 1. Check async context first (for concurrent request safety)
|
|
50
72
|
const store = this._asyncStorage.getStore();
|
|
51
73
|
if (store?.tenantId) {
|
|
52
74
|
return store.tenantId;
|
|
53
75
|
}
|
|
54
76
|
|
|
55
|
-
|
|
56
|
-
if (
|
|
57
|
-
return
|
|
77
|
+
const batchTenant = getBatchContextStore()?.tenantId;
|
|
78
|
+
if (batchTenant) {
|
|
79
|
+
return batchTenant;
|
|
58
80
|
}
|
|
59
81
|
|
|
60
|
-
// 3. Try resolvers in order (for request-scoped resolution)
|
|
61
82
|
for (const resolver of this._resolvers) {
|
|
62
83
|
const tenantId = resolver();
|
|
63
84
|
if (tenantId) {
|
|
@@ -65,7 +86,13 @@ class TenantContext {
|
|
|
65
86
|
}
|
|
66
87
|
}
|
|
67
88
|
|
|
68
|
-
|
|
89
|
+
// runWithTenant() is an explicit request scope; a process-wide tenant
|
|
90
|
+
// must not leak into it.
|
|
91
|
+
if (store) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return this._fallbackTenant || null;
|
|
69
96
|
}
|
|
70
97
|
|
|
71
98
|
/**
|
|
@@ -109,27 +136,26 @@ class TenantContext {
|
|
|
109
136
|
*/
|
|
110
137
|
static getInstance(tenantId = null) {
|
|
111
138
|
const ModelManager = getModelManager();
|
|
112
|
-
|
|
113
|
-
const key = effectiveTenantId || "default";
|
|
114
|
-
|
|
115
|
-
if (!this._instances.has(key)) {
|
|
116
|
-
const manager = ModelManager.getInstance(effectiveTenantId);
|
|
117
|
-
this._instances.set(key, manager);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return this._instances.get(key);
|
|
139
|
+
return ModelManager.forTenant(tenantId || this.getCurrentTenant());
|
|
121
140
|
}
|
|
122
141
|
|
|
123
142
|
/**
|
|
124
|
-
* Clears the current tenant
|
|
125
|
-
*
|
|
143
|
+
* Clears the current tenant. Inside runWithTenant() or runWithBatchContext()
|
|
144
|
+
* this clears only that context's tenant; outside both it clears the
|
|
145
|
+
* process-wide tenant.
|
|
126
146
|
*/
|
|
127
147
|
static clearTenant() {
|
|
128
148
|
const store = this._asyncStorage.getStore();
|
|
149
|
+
const batchStore = getBatchContextStore();
|
|
129
150
|
if (store) {
|
|
130
151
|
delete store.tenantId;
|
|
131
152
|
}
|
|
132
|
-
|
|
153
|
+
if (batchStore) {
|
|
154
|
+
batchStore.tenantId = null;
|
|
155
|
+
}
|
|
156
|
+
if (!store && !batchStore) {
|
|
157
|
+
this._fallbackTenant = null;
|
|
158
|
+
}
|
|
133
159
|
}
|
|
134
160
|
|
|
135
161
|
/**
|
|
@@ -157,7 +183,6 @@ class TenantContext {
|
|
|
157
183
|
* Resets all tenant context state (for testing).
|
|
158
184
|
*/
|
|
159
185
|
static reset() {
|
|
160
|
-
this._instances.clear();
|
|
161
186
|
this._resolvers = [];
|
|
162
187
|
this._fallbackTenant = null;
|
|
163
188
|
|
|
@@ -169,6 +194,7 @@ class TenantContext {
|
|
|
169
194
|
if (ModelManager._instances) {
|
|
170
195
|
ModelManager._instances.clear();
|
|
171
196
|
}
|
|
197
|
+
ModelManager._primary = null;
|
|
172
198
|
}
|
|
173
199
|
}
|
|
174
200
|
|
package/src/utils/search-text.js
CHANGED
|
@@ -182,7 +182,7 @@ function buildSearchPredicate(terms, searchConfig, options = {}) {
|
|
|
182
182
|
}
|
|
183
183
|
|
|
184
184
|
// `:stN` placeholders are deliberately a separate namespace from the
|
|
185
|
-
// FilterExpressionBuilder convention (`:
|
|
185
|
+
// FilterExpressionBuilder convention (`:__fN`, see src/filter-expression.js)
|
|
186
186
|
// so the two can be merged into a single FilterExpression without colliding.
|
|
187
187
|
const join = operator === "$or" ? " OR " : " AND ";
|
|
188
188
|
const parts = [];
|
package/bin/chokidar-test.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { promises as fs } from "fs";
|
|
3
|
-
import os from "os";
|
|
4
|
-
import chokidar from "chokidar";
|
|
5
|
-
|
|
6
|
-
async function testChokidar() {
|
|
7
|
-
let tempDir = path.join(os.tmpdir(), "chokidar-test-" + Date.now());
|
|
8
|
-
await fs.mkdir(tempDir, { recursive: true });
|
|
9
|
-
|
|
10
|
-
const testFile = path.join(tempDir, "test-file.js");
|
|
11
|
-
await fs.writeFile(testFile, "// Initial content");
|
|
12
|
-
|
|
13
|
-
console.log("Watching directory:", tempDir);
|
|
14
|
-
console.log("Test file path:", testFile);
|
|
15
|
-
|
|
16
|
-
// DON'T DO THIS tempDir = path.join(tempDir, '**/*.js');
|
|
17
|
-
|
|
18
|
-
const watcher = chokidar.watch(tempDir, {
|
|
19
|
-
// Changed to watch entire directory
|
|
20
|
-
persistent: true,
|
|
21
|
-
ignoreInitial: false, // Changed to false to see initial add events
|
|
22
|
-
usePolling: true,
|
|
23
|
-
interval: 100,
|
|
24
|
-
awaitWriteFinish: {
|
|
25
|
-
stabilityThreshold: 100,
|
|
26
|
-
pollInterval: 100,
|
|
27
|
-
},
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
watcher
|
|
31
|
-
.on("add", (path) => console.log(`File ${path} has been added`))
|
|
32
|
-
.on("change", (filepath) => {
|
|
33
|
-
console.log("Change detected:", filepath);
|
|
34
|
-
})
|
|
35
|
-
.on("error", (error) => console.log(`Watcher error: ${error}`))
|
|
36
|
-
.on("ready", () => {
|
|
37
|
-
console.log("Watcher is ready");
|
|
38
|
-
|
|
39
|
-
setTimeout(async () => {
|
|
40
|
-
console.log("Modifying file");
|
|
41
|
-
try {
|
|
42
|
-
await fs.writeFile(testFile, "// Modified content");
|
|
43
|
-
console.log("File modification completed");
|
|
44
|
-
} catch (err) {
|
|
45
|
-
console.error("Error modifying file:", err);
|
|
46
|
-
}
|
|
47
|
-
}, 1000);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
// Keep process alive longer
|
|
51
|
-
setTimeout(() => {
|
|
52
|
-
console.log("Closing watcher");
|
|
53
|
-
watcher.close();
|
|
54
|
-
}, 10000); // Increased timeout to 10 seconds
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
testChokidar().catch((err) => {
|
|
58
|
-
console.error("Test failed:", err);
|
|
59
|
-
process.exit(1);
|
|
60
|
-
});
|