dynamo-bao 0.1.3 → 0.1.4
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/bin/codegen.js +26 -24
- package/bin/dynamo-bao-create-table.js +1 -1
- package/bin/generators/model.js +34 -8
- package/bin/watch.js +12 -24
- package/package.json +1 -1
- package/src/config.js +33 -13
package/bin/codegen.js
CHANGED
|
@@ -7,24 +7,7 @@ const { generateModelFiles } = require("./generators/model");
|
|
|
7
7
|
const { createLogger } = require("./utils/scriptLogger");
|
|
8
8
|
const FieldResolver = require("../src/fieldResolver");
|
|
9
9
|
const logger = createLogger("CodeGen");
|
|
10
|
-
|
|
11
|
-
function loadConfig(definitionsPath) {
|
|
12
|
-
try {
|
|
13
|
-
// First try environment variable
|
|
14
|
-
if (process.env.DYNAMO_BAO_CONFIG) {
|
|
15
|
-
return require(
|
|
16
|
-
path.resolve(process.cwd(), process.env.DYNAMO_BAO_CONFIG),
|
|
17
|
-
);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// Then try config.js in the same directory as definitions
|
|
21
|
-
const configPath = path.resolve(path.dirname(definitionsPath), "config.js");
|
|
22
|
-
return require(configPath);
|
|
23
|
-
} catch (err) {
|
|
24
|
-
logger.debug("No config file found, using defaults");
|
|
25
|
-
return {};
|
|
26
|
-
}
|
|
27
|
-
}
|
|
10
|
+
const config = require("../src/config");
|
|
28
11
|
|
|
29
12
|
function loadModelDefinitions(definitionsPath, fieldResolver) {
|
|
30
13
|
const models = {};
|
|
@@ -62,6 +45,23 @@ function loadModelDefinitions(definitionsPath, fieldResolver) {
|
|
|
62
45
|
}
|
|
63
46
|
}
|
|
64
47
|
|
|
48
|
+
// Process models to ensure mapping tables get both regular and mapping functions
|
|
49
|
+
for (const [modelName, modelDef] of Object.entries(models)) {
|
|
50
|
+
if (modelDef.mapping) {
|
|
51
|
+
// For mapping tables, merge mapping properties with regular model properties
|
|
52
|
+
models[modelName] = {
|
|
53
|
+
...modelDef,
|
|
54
|
+
// Keep mapping specific properties
|
|
55
|
+
mapping: modelDef.mapping,
|
|
56
|
+
// Ensure other standard model properties are present
|
|
57
|
+
fields: modelDef.fields || {},
|
|
58
|
+
indexes: modelDef.indexes || {},
|
|
59
|
+
methods: modelDef.methods || {},
|
|
60
|
+
// Any other standard model properties you want to include
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
65
|
logger.debug("Final merged models:", models);
|
|
66
66
|
return { models, fieldResolver };
|
|
67
67
|
}
|
|
@@ -69,9 +69,14 @@ function loadModelDefinitions(definitionsPath, fieldResolver) {
|
|
|
69
69
|
function main() {
|
|
70
70
|
const args = process.argv.slice(2);
|
|
71
71
|
|
|
72
|
-
// Set default paths
|
|
73
|
-
let definitionsPath =
|
|
74
|
-
|
|
72
|
+
// Set default paths, but prefer config paths if available
|
|
73
|
+
let definitionsPath = config?.paths?.modelsDefinitionPath
|
|
74
|
+
? path.resolve(process.cwd(), config.paths.modelsDefinitionPath)
|
|
75
|
+
: path.resolve(process.cwd(), "./models.yaml");
|
|
76
|
+
|
|
77
|
+
let outputDir = config?.paths?.modelsDir
|
|
78
|
+
? path.resolve(process.cwd(), config.paths.modelsDir)
|
|
79
|
+
: path.resolve(process.cwd(), "./models");
|
|
75
80
|
|
|
76
81
|
// Override with command line arguments if provided
|
|
77
82
|
if (args.length >= 1) {
|
|
@@ -83,9 +88,6 @@ function main() {
|
|
|
83
88
|
|
|
84
89
|
console.log("codegen definitionsPath", definitionsPath);
|
|
85
90
|
try {
|
|
86
|
-
const config = loadConfig(definitionsPath);
|
|
87
|
-
console.log("codegen config", config);
|
|
88
|
-
|
|
89
91
|
// Built-in fields are in fields.js
|
|
90
92
|
const builtInFieldsPath = path.resolve(__dirname, "../src/fields.js");
|
|
91
93
|
|
|
@@ -78,7 +78,7 @@ const config = {
|
|
|
78
78
|
module.exports = config;
|
|
79
79
|
`;
|
|
80
80
|
|
|
81
|
-
const configPath = path.resolve(process.cwd(), "config.js");
|
|
81
|
+
const configPath = path.resolve(process.cwd(), "dynamo-bao.config.js");
|
|
82
82
|
fs.writeFileSync(configPath, configContent);
|
|
83
83
|
console.log(`Config file written to: ${configPath}`);
|
|
84
84
|
}
|
package/bin/generators/model.js
CHANGED
|
@@ -27,8 +27,17 @@ function generateModelClass(
|
|
|
27
27
|
console.log("model config", modelConfig);
|
|
28
28
|
|
|
29
29
|
// Generate fields and track used field types
|
|
30
|
-
const fields = Object.entries(modelConfig.fields)
|
|
30
|
+
const fields = Object.entries(modelConfig.fields || {})
|
|
31
31
|
.map(([fieldName, fieldConfig]) => {
|
|
32
|
+
// Special handling for mapping table fields
|
|
33
|
+
if (modelConfig.mapping && !fieldConfig.type) {
|
|
34
|
+
// Default to RelatedField if not specified in mapping table
|
|
35
|
+
fieldConfig = {
|
|
36
|
+
type: "RelatedField",
|
|
37
|
+
...fieldConfig,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
32
41
|
// Verify the field exists before using it
|
|
33
42
|
const fieldClass = fieldResolver.getFieldDefinition(fieldConfig.type);
|
|
34
43
|
if (!fieldClass) {
|
|
@@ -138,6 +147,8 @@ function generateModelClass(
|
|
|
138
147
|
})
|
|
139
148
|
.join("\n");
|
|
140
149
|
|
|
150
|
+
const standardMethods = generateStandardModelMethods(modelConfig);
|
|
151
|
+
|
|
141
152
|
// Generate the final code with separated imports
|
|
142
153
|
return `// 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨
|
|
143
154
|
// DO NOT EDIT: Generated by model-codegen
|
|
@@ -186,6 +197,7 @@ ${fields}
|
|
|
186
197
|
${indexes ? `\n static indexes = {\n${indexes}\n };` : ""}
|
|
187
198
|
${uniqueConstraints ? `\n static uniqueConstraints = {\n${uniqueConstraints}\n };` : ""}
|
|
188
199
|
${queryMethods}
|
|
200
|
+
${standardMethods}
|
|
189
201
|
${uniqueConstraintMethods}
|
|
190
202
|
${relatedFieldMethods}
|
|
191
203
|
}
|
|
@@ -278,8 +290,7 @@ function generateQueryMethods(modelName, modelConfig, allModels) {
|
|
|
278
290
|
otherModel.fields[otherModel.primaryKey.partitionKey];
|
|
279
291
|
if (
|
|
280
292
|
pkPartitionField?.type === "RelatedField" &&
|
|
281
|
-
pkPartitionField.model === modelName
|
|
282
|
-
otherModel.tableType !== "mapping"
|
|
293
|
+
pkPartitionField.model === modelName
|
|
283
294
|
) {
|
|
284
295
|
// Simple check here
|
|
285
296
|
|
|
@@ -310,11 +321,7 @@ function generateQueryMethods(modelName, modelConfig, allModels) {
|
|
|
310
321
|
|
|
311
322
|
const pkField = otherModel.fields[index.partitionKey];
|
|
312
323
|
|
|
313
|
-
if (
|
|
314
|
-
pkField?.type === "RelatedField" &&
|
|
315
|
-
pkField.model === modelName &&
|
|
316
|
-
otherModel.tableType !== "mapping"
|
|
317
|
-
) {
|
|
324
|
+
if (pkField?.type === "RelatedField" && pkField.model === modelName) {
|
|
318
325
|
// Simple check here
|
|
319
326
|
|
|
320
327
|
relatedModels.add(otherModelName);
|
|
@@ -425,4 +432,23 @@ function generateRelatedFieldMethods(fields) {
|
|
|
425
432
|
return methods.join("\n");
|
|
426
433
|
}
|
|
427
434
|
|
|
435
|
+
function generateStandardModelMethods(modelConfig) {
|
|
436
|
+
let methods = "";
|
|
437
|
+
|
|
438
|
+
// Only add these methods if the model has standard fields
|
|
439
|
+
if (Object.keys(modelConfig.fields || {}).length > 0) {
|
|
440
|
+
methods += `
|
|
441
|
+
static async findAll(options = {}) {
|
|
442
|
+
return await this.scan(options);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
static async findById(id) {
|
|
446
|
+
return await this.get(id);
|
|
447
|
+
}
|
|
448
|
+
`;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
return methods;
|
|
452
|
+
}
|
|
453
|
+
|
|
428
454
|
module.exports = { generateModelFiles };
|
package/bin/watch.js
CHANGED
|
@@ -7,24 +7,23 @@ const { loadModelDefinitions } = require("./codegen");
|
|
|
7
7
|
const { generateModelFiles } = require("./generators/model");
|
|
8
8
|
const FieldResolver = require("../src/fieldResolver");
|
|
9
9
|
const { createLogger } = require("./utils/scriptLogger");
|
|
10
|
+
const config = require("../src/config");
|
|
10
11
|
|
|
11
12
|
const logger = createLogger("Watch");
|
|
12
13
|
|
|
13
|
-
function
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
console.error(
|
|
19
|
-
"Error loading config.js from current directory:",
|
|
20
|
-
error.message,
|
|
14
|
+
function getDefinitionsPath(config) {
|
|
15
|
+
if (config.paths.modelsDefinitionPath) {
|
|
16
|
+
const definitionPath = path.resolve(
|
|
17
|
+
process.cwd(),
|
|
18
|
+
config.paths.modelsDefinitionPath,
|
|
21
19
|
);
|
|
22
|
-
|
|
20
|
+
return definitionPath;
|
|
23
21
|
}
|
|
22
|
+
// Fall back to models.yaml in current directory
|
|
23
|
+
return path.resolve(process.cwd(), "./models.yaml");
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
function generateModels(definitionsPath) {
|
|
27
|
-
const config = loadConfig();
|
|
28
27
|
console.log(`Generating models from: ${definitionsPath}`);
|
|
29
28
|
console.log(`Output directory: ${config.paths.modelsDir}`);
|
|
30
29
|
console.log(`Fields directory: ${config.paths.fieldsDir}`);
|
|
@@ -50,21 +49,10 @@ function generateModels(definitionsPath) {
|
|
|
50
49
|
}
|
|
51
50
|
}
|
|
52
51
|
|
|
53
|
-
function getDefinitionsPath(config) {
|
|
54
|
-
if (config.paths.modelsDefinitionPath) {
|
|
55
|
-
const definitionPath = path.resolve(
|
|
56
|
-
process.cwd(),
|
|
57
|
-
config.paths.modelsDefinitionPath,
|
|
58
|
-
);
|
|
59
|
-
return definitionPath;
|
|
60
|
-
}
|
|
61
|
-
// Fall back to models.yaml in current directory
|
|
62
|
-
return path.resolve(process.cwd(), "./models.yaml");
|
|
63
|
-
}
|
|
64
|
-
|
|
65
52
|
function main() {
|
|
66
|
-
const
|
|
67
|
-
|
|
53
|
+
const definitionsPath = config.paths.modelsDefinitionPath
|
|
54
|
+
? path.resolve(process.cwd(), config.paths.modelsDefinitionPath)
|
|
55
|
+
: path.resolve(process.cwd(), "./models.yaml");
|
|
68
56
|
|
|
69
57
|
// Initial generation
|
|
70
58
|
generateModels(definitionsPath);
|
package/package.json
CHANGED
package/src/config.js
CHANGED
|
@@ -3,15 +3,26 @@ const path = require("path");
|
|
|
3
3
|
const dotenv = require("dotenv");
|
|
4
4
|
|
|
5
5
|
function findConfig() {
|
|
6
|
+
// First check environment variable
|
|
7
|
+
if (process.env.DYNAMO_BAO_CONFIG) {
|
|
8
|
+
const configPath = path.resolve(
|
|
9
|
+
process.cwd(),
|
|
10
|
+
process.env.DYNAMO_BAO_CONFIG,
|
|
11
|
+
);
|
|
12
|
+
if (fs.existsSync(configPath)) {
|
|
13
|
+
const rawConfig = require(configPath);
|
|
14
|
+
const configDir = path.dirname(configPath);
|
|
15
|
+
return normalizeConfig(rawConfig, configDir);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
6
19
|
const possibleNames = [
|
|
7
|
-
"config.js",
|
|
8
20
|
"dynamo-bao.config.js",
|
|
9
21
|
".dynamo-bao/config.js",
|
|
22
|
+
"config.js",
|
|
10
23
|
];
|
|
11
24
|
const searchPaths = [
|
|
12
25
|
process.cwd(), // Project root
|
|
13
|
-
// path.join(__dirname, '..'), // Library root
|
|
14
|
-
// path.join(__dirname, '../test'), // Library test directory
|
|
15
26
|
];
|
|
16
27
|
|
|
17
28
|
// Search each directory for config files
|
|
@@ -21,16 +32,7 @@ function findConfig() {
|
|
|
21
32
|
if (fs.existsSync(configPath)) {
|
|
22
33
|
const rawConfig = require(configPath);
|
|
23
34
|
const configDir = path.dirname(configPath);
|
|
24
|
-
|
|
25
|
-
return {
|
|
26
|
-
...rawConfig,
|
|
27
|
-
paths: {
|
|
28
|
-
...rawConfig.paths,
|
|
29
|
-
modelsDir: rawConfig.paths.modelsDir
|
|
30
|
-
? path.resolve(configDir, rawConfig.paths.modelsDir)
|
|
31
|
-
: null,
|
|
32
|
-
},
|
|
33
|
-
};
|
|
35
|
+
return normalizeConfig(rawConfig, configDir);
|
|
34
36
|
}
|
|
35
37
|
}
|
|
36
38
|
}
|
|
@@ -55,6 +57,24 @@ function findConfig() {
|
|
|
55
57
|
};
|
|
56
58
|
}
|
|
57
59
|
|
|
60
|
+
function normalizeConfig(rawConfig, configDir) {
|
|
61
|
+
return {
|
|
62
|
+
...rawConfig,
|
|
63
|
+
paths: {
|
|
64
|
+
...rawConfig.paths,
|
|
65
|
+
modelsDir: rawConfig.paths.modelsDir
|
|
66
|
+
? path.resolve(configDir, rawConfig.paths.modelsDir)
|
|
67
|
+
: null,
|
|
68
|
+
modelsDefinitionPath: rawConfig.paths.modelsDefinitionPath
|
|
69
|
+
? path.resolve(configDir, rawConfig.paths.modelsDefinitionPath)
|
|
70
|
+
: null,
|
|
71
|
+
fieldsDir: rawConfig.paths.fieldsDir
|
|
72
|
+
? path.resolve(configDir, rawConfig.paths.fieldsDir)
|
|
73
|
+
: null,
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
58
78
|
// Load config once at module level
|
|
59
79
|
const config = findConfig();
|
|
60
80
|
module.exports = config;
|