dynamo-bao 0.2.8 → 0.2.9
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 +47 -0
- package/bin/chokidar-test.js +4 -4
- package/bin/codegen.js +18 -7
- package/bin/dynamo-bao-create-table.js +5 -5
- package/bin/generators/manifest.js +3 -1
- package/bin/generators/model.js +67 -32
- package/package.json +5 -1
- package/src/config.js +85 -6
- package/src/fieldResolver.js +3 -3
- package/src/index.js +2 -4
- package/src/model-config.js +1 -5
- package/src/model-manager.js +1 -1
- package/src/model.js +11 -7
- package/src/object-not-found.js +1 -3
- package/src/tenant-context.js +13 -2
- package/src/utils/logger.js +2 -5
package/README.md
CHANGED
|
@@ -23,10 +23,12 @@ DynamoBao is the tool I wish I had when I started.
|
|
|
23
23
|
- Built-in multi-tenancy support with complete data isolation and concurrency safety
|
|
24
24
|
- Return total read/write consumed capacity (even when multiple operations were performed)
|
|
25
25
|
- Easily iterate over all items in a model for batch processing or migrations
|
|
26
|
+
- ESM (ECMAScript Modules) support for modern JavaScript projects
|
|
26
27
|
|
|
27
28
|
## Requirements
|
|
28
29
|
|
|
29
30
|
- **Node.js 12.17.0+** (for AsyncLocalStorage support in multi-tenant features)
|
|
31
|
+
- Node.js 14+ recommended for native ESM support
|
|
30
32
|
- AWS credentials configured for DynamoDB access
|
|
31
33
|
|
|
32
34
|
## Example 1: Simple model
|
|
@@ -252,6 +254,51 @@ npx bao-codegen
|
|
|
252
254
|
|
|
253
255
|
You should now have generated models in the `models` directory.
|
|
254
256
|
|
|
257
|
+
### ESM Support
|
|
258
|
+
|
|
259
|
+
DynamoBao supports generating models as ESM (ECMAScript Modules) for modern JavaScript projects. To enable ESM code generation, add the `codegen` configuration to your `config.js`:
|
|
260
|
+
|
|
261
|
+
```javascript
|
|
262
|
+
// config.js
|
|
263
|
+
module.exports = {
|
|
264
|
+
aws: {
|
|
265
|
+
region: "us-west-2",
|
|
266
|
+
},
|
|
267
|
+
db: {
|
|
268
|
+
tableName: "your-table-name",
|
|
269
|
+
},
|
|
270
|
+
codegen: {
|
|
271
|
+
moduleSystem: "esm", // Options: 'commonjs' (default) or 'esm'
|
|
272
|
+
},
|
|
273
|
+
// ... other config
|
|
274
|
+
};
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
When ESM is enabled, generated models will use:
|
|
278
|
+
|
|
279
|
+
- `import` / `export` syntax instead of `require` / `module.exports`
|
|
280
|
+
- `.js` extensions in import paths for ESM compatibility
|
|
281
|
+
|
|
282
|
+
For ESM config files, you can use `.mjs` extension:
|
|
283
|
+
|
|
284
|
+
```javascript
|
|
285
|
+
// dynamo-bao.config.mjs
|
|
286
|
+
export default {
|
|
287
|
+
codegen: {
|
|
288
|
+
moduleSystem: "esm",
|
|
289
|
+
},
|
|
290
|
+
// ... other config
|
|
291
|
+
};
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
Then use the generated ESM models:
|
|
295
|
+
|
|
296
|
+
```javascript
|
|
297
|
+
// With ESM enabled
|
|
298
|
+
import { User } from "./models/user.js";
|
|
299
|
+
import { Post } from "./models/post.js";
|
|
300
|
+
```
|
|
301
|
+
|
|
255
302
|
Let's try using the models. Add the following code to a file called `example.js`.
|
|
256
303
|
|
|
257
304
|
```
|
package/bin/chokidar-test.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { promises as fs } from "fs";
|
|
3
|
+
import os from "os";
|
|
4
|
+
import chokidar from "chokidar";
|
|
5
5
|
|
|
6
6
|
async function testChokidar() {
|
|
7
7
|
let tempDir = path.join(os.tmpdir(), "chokidar-test-" + Date.now());
|
package/bin/codegen.js
CHANGED
|
@@ -7,10 +7,13 @@ const { generateModelFiles } = require("./generators/model");
|
|
|
7
7
|
const { generateManifestFile } = require("./generators/manifest");
|
|
8
8
|
const { createLogger } = require("./utils/scriptLogger");
|
|
9
9
|
const FieldResolver = require("../src/fieldResolver");
|
|
10
|
+
const { initConfig } = require("../src/config");
|
|
11
|
+
|
|
12
|
+
// const __dirname = __dirname;
|
|
13
|
+
|
|
10
14
|
const logger = createLogger("CodeGen");
|
|
11
|
-
const config = require("../src/config");
|
|
12
15
|
|
|
13
|
-
function loadModelDefinitions(definitionsPath, fieldResolver) {
|
|
16
|
+
async function loadModelDefinitions(definitionsPath, fieldResolver) {
|
|
14
17
|
const models = {};
|
|
15
18
|
|
|
16
19
|
if (fs.statSync(definitionsPath).isDirectory()) {
|
|
@@ -97,9 +100,12 @@ function loadModelDefinitions(definitionsPath, fieldResolver) {
|
|
|
97
100
|
return { models, fieldResolver };
|
|
98
101
|
}
|
|
99
102
|
|
|
100
|
-
function main() {
|
|
103
|
+
async function main() {
|
|
101
104
|
const args = process.argv.slice(2);
|
|
102
105
|
|
|
106
|
+
// Initialize config first
|
|
107
|
+
const config = await initConfig();
|
|
108
|
+
|
|
103
109
|
// Paths should be pre-resolved by the config loader.
|
|
104
110
|
let definitionsPath =
|
|
105
111
|
config?.paths?.modelsDefinitionPath ||
|
|
@@ -121,16 +127,20 @@ function main() {
|
|
|
121
127
|
// Built-in fields are in fields.js
|
|
122
128
|
const builtInFieldsPath = path.resolve(__dirname, "../src/fields.js");
|
|
123
129
|
|
|
130
|
+
// Load built-in fields directly for CommonJS compatibility
|
|
131
|
+
const builtInFields = require(builtInFieldsPath);
|
|
132
|
+
|
|
124
133
|
const fieldResolver = new FieldResolver(
|
|
125
|
-
|
|
134
|
+
builtInFields,
|
|
126
135
|
config?.paths?.fieldsDir,
|
|
127
136
|
);
|
|
128
137
|
|
|
129
|
-
const definitions = loadModelDefinitions(definitionsPath, fieldResolver);
|
|
138
|
+
const definitions = await loadModelDefinitions(definitionsPath, fieldResolver);
|
|
130
139
|
generateModelFiles(
|
|
131
140
|
definitions.models,
|
|
132
141
|
outputDir,
|
|
133
142
|
definitions.fieldResolver,
|
|
143
|
+
config?.codegen?.moduleSystem || 'commonjs',
|
|
134
144
|
);
|
|
135
145
|
|
|
136
146
|
// After generating models, generate the manifest file
|
|
@@ -149,10 +159,11 @@ function main() {
|
|
|
149
159
|
}
|
|
150
160
|
}
|
|
151
161
|
|
|
152
|
-
function run() {
|
|
153
|
-
main();
|
|
162
|
+
async function run() {
|
|
163
|
+
await main();
|
|
154
164
|
}
|
|
155
165
|
|
|
166
|
+
// Check if this is the main module in CommonJS
|
|
156
167
|
if (require.main === module) {
|
|
157
168
|
run();
|
|
158
169
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import {
|
|
4
4
|
DynamoDBClient,
|
|
5
5
|
CreateTableCommand,
|
|
6
6
|
ListTablesCommand,
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
} from "../src/dynamodb-client.js";
|
|
8
|
+
import readline from "readline";
|
|
9
|
+
import fs from "fs";
|
|
10
|
+
import path from "path";
|
|
11
11
|
|
|
12
12
|
// Create DynamoDB client at the top level
|
|
13
13
|
const client = new DynamoDBClient();
|
|
@@ -44,7 +44,9 @@ function generateManifestFile(modelsDir, manifestPath) {
|
|
|
44
44
|
const imports = modelFiles
|
|
45
45
|
.map((file, index) => {
|
|
46
46
|
const relativePath = path.relative(manifestDir, file).replace(/\\/g, "/");
|
|
47
|
-
|
|
47
|
+
// Ensure .js extension for ESM imports
|
|
48
|
+
const importPath = relativePath.endsWith('.js') ? relativePath : `${relativePath}.js`;
|
|
49
|
+
return `import * as model${index} from '${importPath}';`;
|
|
48
50
|
})
|
|
49
51
|
.join("\n");
|
|
50
52
|
|
package/bin/generators/model.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
const path = require("path");
|
|
2
2
|
const fs = require("fs");
|
|
3
3
|
const { createLogger } = require("../utils/scriptLogger");
|
|
4
|
+
|
|
5
|
+
// const __dirname = __dirname;
|
|
4
6
|
const logger = createLogger("ModelGen");
|
|
5
7
|
|
|
6
8
|
const codeGenPrefix = ""; // "cg";
|
|
@@ -11,7 +13,13 @@ function generateModelClass(
|
|
|
11
13
|
allModels,
|
|
12
14
|
fieldResolver,
|
|
13
15
|
outputDir,
|
|
16
|
+
moduleSystem = 'commonjs',
|
|
14
17
|
) {
|
|
18
|
+
// Calculate relative path to src directory from output directory
|
|
19
|
+
const projectRoot = path.resolve(__dirname, '../..');
|
|
20
|
+
const srcPath = path.join(projectRoot, 'src');
|
|
21
|
+
const relativeSrcPath = path.relative(outputDir, srcPath).replace(/\\/g, '/');
|
|
22
|
+
const srcImportPrefix = relativeSrcPath.startsWith('.') ? relativeSrcPath : './' + relativeSrcPath;
|
|
15
23
|
if (!modelConfig || !modelConfig.fields) {
|
|
16
24
|
throw new Error(
|
|
17
25
|
`Invalid model configuration for ${modelName}: missing fields`,
|
|
@@ -150,47 +158,73 @@ function generateModelClass(
|
|
|
150
158
|
outputDir,
|
|
151
159
|
fieldResolver.customFieldsPath,
|
|
152
160
|
);
|
|
153
|
-
|
|
161
|
+
|
|
162
|
+
if (moduleSystem === 'esm') {
|
|
163
|
+
return `import { ${fieldType} } from '${relativePath}/${kebabName}-field.js';`;
|
|
164
|
+
} else {
|
|
165
|
+
return `const { ${fieldType} } = require('${relativePath}/${kebabName}-field.js');`;
|
|
166
|
+
}
|
|
154
167
|
})
|
|
155
168
|
.join("\n")
|
|
156
169
|
: "";
|
|
157
170
|
|
|
158
|
-
// Generate
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
171
|
+
// Generate imports/requires based on module system
|
|
172
|
+
const baseImportSection = moduleSystem === 'esm'
|
|
173
|
+
? `import {
|
|
174
|
+
${baseImportStr}
|
|
175
|
+
} from '${srcImportPrefix}/model.js';`
|
|
176
|
+
: `const {
|
|
163
177
|
${baseImportStr}
|
|
164
|
-
} = require('
|
|
178
|
+
} = require('${srcImportPrefix}/model.js');`;
|
|
165
179
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
180
|
+
const constantImportSection = constantImports.size > 0
|
|
181
|
+
? moduleSystem === 'esm'
|
|
182
|
+
? `import {
|
|
169
183
|
${constantImportStr}
|
|
170
|
-
}
|
|
184
|
+
} from '${srcImportPrefix}/constants.js';`
|
|
185
|
+
: `const {
|
|
186
|
+
${constantImportStr}
|
|
187
|
+
} = require('${srcImportPrefix}/constants.js');`
|
|
188
|
+
: "";
|
|
171
189
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
${
|
|
176
|
-
usedFields.size > 0
|
|
177
|
-
? `const {
|
|
190
|
+
const fieldImportSection = usedFields.size > 0
|
|
191
|
+
? moduleSystem === 'esm'
|
|
192
|
+
? `import {
|
|
178
193
|
${fieldImports}
|
|
179
|
-
}
|
|
194
|
+
} from '${srcImportPrefix}/fields.js';`
|
|
195
|
+
: `const {
|
|
196
|
+
${fieldImports}
|
|
197
|
+
} = require('${srcImportPrefix}/fields.js');`
|
|
198
|
+
: "";
|
|
180
199
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
$
|
|
200
|
+
const relatedModelImports = Array.from(relatedModels)
|
|
201
|
+
.map((model) => {
|
|
202
|
+
const kebabName = model
|
|
203
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
204
|
+
.toLowerCase();
|
|
205
|
+
|
|
206
|
+
if (moduleSystem === 'esm') {
|
|
207
|
+
return `import { ${model} } from './${kebabName}.js';`;
|
|
208
|
+
} else {
|
|
209
|
+
return `const { ${model} } = require('./${kebabName}.js');`;
|
|
210
|
+
}
|
|
211
|
+
})
|
|
212
|
+
.join("\n");
|
|
185
213
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
214
|
+
const exportSection = moduleSystem === 'esm'
|
|
215
|
+
? `export { ${modelName} };`
|
|
216
|
+
: `module.exports = { ${modelName} };`;
|
|
217
|
+
|
|
218
|
+
// Generate the final code with separated imports
|
|
219
|
+
return `// 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨
|
|
220
|
+
// DO NOT EDIT: Generated by model-codegen
|
|
221
|
+
// 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨
|
|
222
|
+
${baseImportSection}
|
|
223
|
+
|
|
224
|
+
${constantImportSection ? constantImportSection + '\n' : ''}
|
|
225
|
+
${fieldImportSection ? fieldImportSection + '\n' : ''}
|
|
226
|
+
${customFieldImports ? customFieldImports + '\n' : ''}
|
|
227
|
+
${relatedModelImports ? relatedModelImports + '\n' : ''}
|
|
194
228
|
|
|
195
229
|
class ${modelName} extends BaoModel {
|
|
196
230
|
static modelPrefix = '${modelConfig.modelPrefix}';
|
|
@@ -209,7 +243,7 @@ ${uniqueConstraintMethods}
|
|
|
209
243
|
${relatedFieldMethods}
|
|
210
244
|
}
|
|
211
245
|
|
|
212
|
-
|
|
246
|
+
${exportSection}
|
|
213
247
|
`;
|
|
214
248
|
}
|
|
215
249
|
|
|
@@ -227,7 +261,7 @@ function generateUniqueConstraintMethods(modelConfig) {
|
|
|
227
261
|
.join("\n");
|
|
228
262
|
}
|
|
229
263
|
|
|
230
|
-
function generateModelFiles(models, outputDir, fieldResolver) {
|
|
264
|
+
function generateModelFiles(models, outputDir, fieldResolver, moduleSystem = 'commonjs') {
|
|
231
265
|
if (!fs.existsSync(outputDir)) {
|
|
232
266
|
fs.mkdirSync(outputDir, { recursive: true });
|
|
233
267
|
}
|
|
@@ -251,6 +285,7 @@ function generateModelFiles(models, outputDir, fieldResolver) {
|
|
|
251
285
|
models,
|
|
252
286
|
fieldResolver,
|
|
253
287
|
outputDir,
|
|
288
|
+
moduleSystem,
|
|
254
289
|
);
|
|
255
290
|
|
|
256
291
|
fs.writeFileSync(filePath, code);
|
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dynamo-bao",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"description": "Simple powerful data modeling primitives for DynamoDB in JavaScript.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/index.js",
|
|
8
|
+
"./package.json": "./package.json"
|
|
9
|
+
},
|
|
6
10
|
"scripts": {
|
|
7
11
|
"test": "LOG_LEVEL=OFF jest",
|
|
8
12
|
"test:debug": "LOG_LEVEL=DEBUG jest",
|
package/src/config.js
CHANGED
|
@@ -2,7 +2,7 @@ const fs = require("fs");
|
|
|
2
2
|
const path = require("path");
|
|
3
3
|
const dotenv = require("dotenv");
|
|
4
4
|
|
|
5
|
-
function findConfig() {
|
|
5
|
+
async function findConfig() {
|
|
6
6
|
// First check environment variable
|
|
7
7
|
if (process.env.DYNAMO_BAO_CONFIG) {
|
|
8
8
|
const configPath = path.resolve(
|
|
@@ -10,7 +10,7 @@ function findConfig() {
|
|
|
10
10
|
process.env.DYNAMO_BAO_CONFIG,
|
|
11
11
|
);
|
|
12
12
|
if (fs.existsSync(configPath)) {
|
|
13
|
-
const rawConfig =
|
|
13
|
+
const rawConfig = await loadConfigFile(configPath);
|
|
14
14
|
const configDir = path.dirname(configPath);
|
|
15
15
|
return normalizeConfig(rawConfig, configDir);
|
|
16
16
|
}
|
|
@@ -18,9 +18,12 @@ function findConfig() {
|
|
|
18
18
|
|
|
19
19
|
const possibleNames = [
|
|
20
20
|
"dynamo-bao.config.js",
|
|
21
|
+
"dynamo-bao.config.mjs",
|
|
21
22
|
"dynamo-bao.config.cjs",
|
|
22
23
|
".dynamo-bao/config.js",
|
|
24
|
+
".dynamo-bao/config.mjs",
|
|
23
25
|
"config.js",
|
|
26
|
+
"config.mjs",
|
|
24
27
|
"config.cjs",
|
|
25
28
|
];
|
|
26
29
|
const searchPaths = [
|
|
@@ -32,7 +35,7 @@ function findConfig() {
|
|
|
32
35
|
for (const name of possibleNames) {
|
|
33
36
|
const configPath = path.join(searchPath, name);
|
|
34
37
|
if (fs.existsSync(configPath)) {
|
|
35
|
-
const rawConfig =
|
|
38
|
+
const rawConfig = await loadConfigFile(configPath);
|
|
36
39
|
const configDir = path.dirname(configPath);
|
|
37
40
|
return normalizeConfig(rawConfig, configDir);
|
|
38
41
|
}
|
|
@@ -48,6 +51,9 @@ function findConfig() {
|
|
|
48
51
|
db: {
|
|
49
52
|
tableName: process.env.TABLE_NAME || "dynamo-bao-dev",
|
|
50
53
|
},
|
|
54
|
+
codegen: {
|
|
55
|
+
moduleSystem: 'commonjs',
|
|
56
|
+
},
|
|
51
57
|
logging: {
|
|
52
58
|
level: process.env.LOG_LEVEL || "ERROR",
|
|
53
59
|
},
|
|
@@ -62,10 +68,30 @@ function findConfig() {
|
|
|
62
68
|
};
|
|
63
69
|
}
|
|
64
70
|
|
|
71
|
+
async function loadConfigFile(configPath) {
|
|
72
|
+
if (configPath.endsWith('.cjs')) {
|
|
73
|
+
// Use require for CommonJS files
|
|
74
|
+
return require(configPath);
|
|
75
|
+
} else if (configPath.endsWith('.mjs') || configPath.endsWith('.js')) {
|
|
76
|
+
// Use dynamic import for ESM files
|
|
77
|
+
const fileUrl = 'file://' + configPath;
|
|
78
|
+
const module = await import(fileUrl);
|
|
79
|
+
return module.default || module;
|
|
80
|
+
} else {
|
|
81
|
+
// Default to require for legacy files
|
|
82
|
+
return require(configPath);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
65
87
|
function normalizeConfig(rawConfig, configDir) {
|
|
66
88
|
return {
|
|
67
89
|
...rawConfig,
|
|
68
90
|
models: rawConfig.models || null,
|
|
91
|
+
codegen: {
|
|
92
|
+
moduleSystem: 'commonjs',
|
|
93
|
+
...(rawConfig.codegen || {}),
|
|
94
|
+
},
|
|
69
95
|
paths: {
|
|
70
96
|
...(rawConfig.paths || {}),
|
|
71
97
|
modelsDir: rawConfig.paths?.modelsDir
|
|
@@ -93,6 +119,59 @@ function normalizeConfig(rawConfig, configDir) {
|
|
|
93
119
|
};
|
|
94
120
|
}
|
|
95
121
|
|
|
96
|
-
//
|
|
97
|
-
|
|
98
|
-
|
|
122
|
+
// Synchronous initialization
|
|
123
|
+
let config = null;
|
|
124
|
+
|
|
125
|
+
function getConfig() {
|
|
126
|
+
if (!config) {
|
|
127
|
+
throw new Error('Config not initialized. Call initConfig() first.');
|
|
128
|
+
}
|
|
129
|
+
return config;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function initConfig() {
|
|
133
|
+
config = await findConfig();
|
|
134
|
+
return config;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// For backward compatibility - try to load config synchronously
|
|
138
|
+
try {
|
|
139
|
+
// For now, create a basic synchronous config
|
|
140
|
+
const dotenvResult = dotenv.config();
|
|
141
|
+
config = {
|
|
142
|
+
aws: {
|
|
143
|
+
region: process.env.AWS_REGION || "us-west-2",
|
|
144
|
+
},
|
|
145
|
+
db: {
|
|
146
|
+
tableName: process.env.TABLE_NAME || "dynamo-bao-dev",
|
|
147
|
+
},
|
|
148
|
+
codegen: {
|
|
149
|
+
moduleSystem: 'commonjs',
|
|
150
|
+
},
|
|
151
|
+
logging: {
|
|
152
|
+
level: process.env.LOG_LEVEL || "ERROR",
|
|
153
|
+
},
|
|
154
|
+
paths: {
|
|
155
|
+
modelsDir: process.env.MODELS_DIR
|
|
156
|
+
? path.resolve(process.cwd(), process.env.MODELS_DIR)
|
|
157
|
+
: null,
|
|
158
|
+
},
|
|
159
|
+
tenancy: {
|
|
160
|
+
enabled: process.env.DYNAMO_BAO_TENANCY_ENABLED === "true" || false,
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
} catch (err) {
|
|
164
|
+
console.error('Error loading config:', err);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
module.exports = config || {
|
|
168
|
+
aws: { region: "us-west-2" },
|
|
169
|
+
db: { tableName: "dynamo-bao-dev" },
|
|
170
|
+
codegen: { moduleSystem: 'commonjs' },
|
|
171
|
+
logging: { level: "ERROR" },
|
|
172
|
+
paths: { modelsDir: null },
|
|
173
|
+
tenancy: { enabled: false }
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
module.exports.initConfig = initConfig;
|
|
177
|
+
module.exports.getConfig = getConfig;
|
package/src/fieldResolver.js
CHANGED
|
@@ -2,13 +2,13 @@ const path = require("path");
|
|
|
2
2
|
const fs = require("fs");
|
|
3
3
|
|
|
4
4
|
class FieldResolver {
|
|
5
|
-
constructor(
|
|
6
|
-
this.builtInFields =
|
|
5
|
+
constructor(builtInFields, customFieldsPath) {
|
|
6
|
+
this.builtInFields = builtInFields;
|
|
7
7
|
this.customFieldsPath = customFieldsPath;
|
|
8
8
|
this.fieldDefinitionCache = new Map();
|
|
9
9
|
|
|
10
10
|
console.log("FieldResolver constructor:", {
|
|
11
|
-
|
|
11
|
+
builtInFields: typeof builtInFields,
|
|
12
12
|
customFieldsPath,
|
|
13
13
|
});
|
|
14
14
|
|
package/src/index.js
CHANGED
|
@@ -16,6 +16,8 @@ const {
|
|
|
16
16
|
UniqueConstraintConfig,
|
|
17
17
|
} = require("./model");
|
|
18
18
|
|
|
19
|
+
const { TenantContext } = require("./tenant-context");
|
|
20
|
+
|
|
19
21
|
function findModelFiles(dir) {
|
|
20
22
|
let results = [];
|
|
21
23
|
const items = fs.readdirSync(dir);
|
|
@@ -171,14 +173,12 @@ function initModels(userConfig = {}) {
|
|
|
171
173
|
|
|
172
174
|
// Validate tenant context if tenancy enabled
|
|
173
175
|
if (finalConfig.tenancy?.enabled) {
|
|
174
|
-
const { TenantContext } = require("./tenant-context");
|
|
175
176
|
TenantContext.validateTenantRequired(finalConfig);
|
|
176
177
|
}
|
|
177
178
|
|
|
178
179
|
const modelsDir = finalConfig.paths.modelsDir;
|
|
179
180
|
|
|
180
181
|
// Get/create manager instance with tenantId
|
|
181
|
-
const { TenantContext } = require("./tenant-context");
|
|
182
182
|
const tenantId = TenantContext.getCurrentTenant() || finalConfig.testId;
|
|
183
183
|
const manager = ModelManager.getInstance(tenantId);
|
|
184
184
|
|
|
@@ -200,8 +200,6 @@ function initModels(userConfig = {}) {
|
|
|
200
200
|
return manager;
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
-
const { TenantContext } = require("./tenant-context");
|
|
204
|
-
|
|
205
203
|
const firstExport = {
|
|
206
204
|
// Initialize function
|
|
207
205
|
initModels,
|
package/src/model-config.js
CHANGED
package/src/model-manager.js
CHANGED
|
@@ -25,7 +25,7 @@ class ModelManager {
|
|
|
25
25
|
return ModelManager._instances.get(key);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
init(config = {}) {
|
|
28
|
+
async init(config = {}) {
|
|
29
29
|
// Validate tenant requirement
|
|
30
30
|
const { TenantContext } = require('./tenant-context');
|
|
31
31
|
TenantContext.validateTenantRequired(config);
|
package/src/model.js
CHANGED
|
@@ -18,9 +18,9 @@ const {
|
|
|
18
18
|
} = require("./mixins/batch-loading-mixin");
|
|
19
19
|
|
|
20
20
|
const {
|
|
21
|
-
PrimaryKeyConfig,
|
|
22
|
-
IndexConfig,
|
|
23
|
-
UniqueConstraintConfig,
|
|
21
|
+
PrimaryKeyConfig: PrimaryKeyConfigClass,
|
|
22
|
+
IndexConfig: IndexConfigClass,
|
|
23
|
+
UniqueConstraintConfig: UniqueConstraintConfigClass,
|
|
24
24
|
} = require("./model-config");
|
|
25
25
|
|
|
26
26
|
const GID_SEPARATOR = "##__SK__##";
|
|
@@ -866,12 +866,16 @@ class BaoModel {
|
|
|
866
866
|
}
|
|
867
867
|
}
|
|
868
868
|
|
|
869
|
+
// Factory functions to maintain compatibility
|
|
870
|
+
const PrimaryKeyConfig = (pk, sk) => new PrimaryKeyConfigClass(pk, sk);
|
|
871
|
+
const IndexConfig = (pk, sk, indexId) => new IndexConfigClass(pk, sk, indexId);
|
|
872
|
+
const UniqueConstraintConfig = (field, constraintId) => new UniqueConstraintConfigClass(field, constraintId);
|
|
873
|
+
|
|
869
874
|
module.exports = {
|
|
870
875
|
BaoModel,
|
|
871
|
-
PrimaryKeyConfig
|
|
872
|
-
IndexConfig
|
|
873
|
-
UniqueConstraintConfig
|
|
874
|
-
new UniqueConstraintConfig(field, constraintId),
|
|
876
|
+
PrimaryKeyConfig,
|
|
877
|
+
IndexConfig,
|
|
878
|
+
UniqueConstraintConfig,
|
|
875
879
|
BATCH_REQUEST_TIMEOUT,
|
|
876
880
|
BATCH_REQUESTS,
|
|
877
881
|
};
|
package/src/object-not-found.js
CHANGED
package/src/tenant-context.js
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
const { AsyncLocalStorage } = require('async_hooks');
|
|
2
2
|
|
|
3
|
+
// Lazy getter to avoid circular dependency
|
|
4
|
+
let _ModelManager = null;
|
|
5
|
+
function getModelManager() {
|
|
6
|
+
if (!_ModelManager) {
|
|
7
|
+
// Use require since this is called at runtime, not module load time
|
|
8
|
+
const { ModelManager } = require('./model-manager');
|
|
9
|
+
_ModelManager = ModelManager;
|
|
10
|
+
}
|
|
11
|
+
return _ModelManager;
|
|
12
|
+
}
|
|
13
|
+
|
|
3
14
|
/**
|
|
4
15
|
* @class TenantContext
|
|
5
16
|
* @description Manages tenant context for multi-tenant applications using DynamoBao.
|
|
@@ -97,7 +108,7 @@ class TenantContext {
|
|
|
97
108
|
* @returns {ModelManager} The ModelManager instance
|
|
98
109
|
*/
|
|
99
110
|
static getInstance(tenantId = null) {
|
|
100
|
-
const
|
|
111
|
+
const ModelManager = getModelManager();
|
|
101
112
|
const effectiveTenantId = tenantId || this.getCurrentTenant();
|
|
102
113
|
const key = effectiveTenantId || "default";
|
|
103
114
|
|
|
@@ -154,7 +165,7 @@ class TenantContext {
|
|
|
154
165
|
this.clearTenant();
|
|
155
166
|
|
|
156
167
|
// Also clear ModelManager instances to ensure clean state
|
|
157
|
-
const
|
|
168
|
+
const ModelManager = getModelManager();
|
|
158
169
|
if (ModelManager._instances) {
|
|
159
170
|
ModelManager._instances.clear();
|
|
160
171
|
}
|
package/src/utils/logger.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//
|
|
1
|
+
// import("dotenv").then(dotenv => dotenv.config());
|
|
2
2
|
|
|
3
3
|
const userConfig = require("../config");
|
|
4
4
|
|
|
@@ -48,7 +48,4 @@ class Logger {
|
|
|
48
48
|
// Export a default logger with a generic context
|
|
49
49
|
const defaultLogger = new Logger("Default");
|
|
50
50
|
|
|
51
|
-
module.exports = {
|
|
52
|
-
Logger,
|
|
53
|
-
defaultLogger,
|
|
54
|
-
};
|
|
51
|
+
module.exports = { Logger, defaultLogger };
|