dynamo-bao 0.1.4 → 0.1.5
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/generators/model.js +18 -28
- package/package.json +1 -1
package/bin/generators/model.js
CHANGED
|
@@ -147,8 +147,6 @@ function generateModelClass(
|
|
|
147
147
|
})
|
|
148
148
|
.join("\n");
|
|
149
149
|
|
|
150
|
-
const standardMethods = generateStandardModelMethods(modelConfig);
|
|
151
|
-
|
|
152
150
|
// Generate the final code with separated imports
|
|
153
151
|
return `// 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨 🧨
|
|
154
152
|
// DO NOT EDIT: Generated by model-codegen
|
|
@@ -197,7 +195,6 @@ ${fields}
|
|
|
197
195
|
${indexes ? `\n static indexes = {\n${indexes}\n };` : ""}
|
|
198
196
|
${uniqueConstraints ? `\n static uniqueConstraints = {\n${uniqueConstraints}\n };` : ""}
|
|
199
197
|
${queryMethods}
|
|
200
|
-
${standardMethods}
|
|
201
198
|
${uniqueConstraintMethods}
|
|
202
199
|
${relatedFieldMethods}
|
|
203
200
|
}
|
|
@@ -297,15 +294,27 @@ function generateQueryMethods(modelName, modelConfig, allModels) {
|
|
|
297
294
|
relatedModels.add(otherModelName);
|
|
298
295
|
|
|
299
296
|
// Use the primary key index name if it exists in indexes
|
|
300
|
-
const
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
297
|
+
const primaryKeyIndexEntry = Object.entries(otherModel.indexes).find(
|
|
298
|
+
([_, index]) => index === "primaryKey",
|
|
299
|
+
);
|
|
300
|
+
|
|
301
|
+
let methodName;
|
|
302
|
+
if (primaryKeyIndexEntry) {
|
|
303
|
+
const [indexName] = primaryKeyIndexEntry;
|
|
304
|
+
if (indexName.includes("For")) {
|
|
305
|
+
const [prefix] = indexName.split("For");
|
|
306
|
+
methodName = `${codeGenPrefix}query${prefix.charAt(0).toUpperCase()}${prefix.slice(1)}`;
|
|
307
|
+
} else {
|
|
308
|
+
methodName = `${codeGenPrefix}query${indexName.charAt(0).toUpperCase()}${indexName.slice(1)}`;
|
|
309
|
+
}
|
|
310
|
+
} else {
|
|
311
|
+
methodName = `${codeGenPrefix}query${otherModelName}s`;
|
|
312
|
+
}
|
|
304
313
|
|
|
305
314
|
methods += `
|
|
306
|
-
async ${
|
|
315
|
+
async ${methodName}(skCondition = null, options = {}) {
|
|
307
316
|
const results = await ${otherModelName}.queryByIndex(
|
|
308
|
-
'${
|
|
317
|
+
'${primaryKeyIndexEntry?.[0] || "primaryKey"}',
|
|
309
318
|
this._getPkValue(),
|
|
310
319
|
skCondition,
|
|
311
320
|
options
|
|
@@ -432,23 +441,4 @@ function generateRelatedFieldMethods(fields) {
|
|
|
432
441
|
return methods.join("\n");
|
|
433
442
|
}
|
|
434
443
|
|
|
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
|
-
|
|
454
444
|
module.exports = { generateModelFiles };
|