@baclet/cli 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/package.json +1 -1
- package/src/config.js +19 -3
package/package.json
CHANGED
package/src/config.js
CHANGED
|
@@ -31,6 +31,21 @@ function isModel(value) {
|
|
|
31
31
|
return !!value && typeof value === "object" && typeof value.name === "string" && !!value.fields && typeof value.fields === "object";
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
function columnName(field) {
|
|
35
|
+
if (!field || field.includes("_") || field.toLowerCase() === field) return field;
|
|
36
|
+
return field.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function normalizeModel(model) {
|
|
40
|
+
const fields = {};
|
|
41
|
+
for (const [name, field] of Object.entries(model.fields)) {
|
|
42
|
+
fields[columnName(name)] = field;
|
|
43
|
+
}
|
|
44
|
+
if (!fields.created_at) fields.created_at = { type: "timestamp", default: { expr: "now()" } };
|
|
45
|
+
if (!fields.updated_at) fields.updated_at = { type: "timestamp", default: { expr: "now()" } };
|
|
46
|
+
return { ...model, fields };
|
|
47
|
+
}
|
|
48
|
+
|
|
34
49
|
export async function loadProject(cwd = process.cwd()) {
|
|
35
50
|
const configPath = await findConfig(cwd);
|
|
36
51
|
const root = path.dirname(configPath);
|
|
@@ -82,9 +97,10 @@ export async function loadProject(cwd = process.cwd()) {
|
|
|
82
97
|
for (const module of Object.values(namespace)) {
|
|
83
98
|
for (const value of Object.values(module)) {
|
|
84
99
|
if (!isModel(value)) continue;
|
|
85
|
-
|
|
86
|
-
seen.
|
|
87
|
-
|
|
100
|
+
const model = normalizeModel(value);
|
|
101
|
+
if (seen.has(model.name)) throw new Error(`Duplicate model name "${model.name}".`);
|
|
102
|
+
seen.add(model.name);
|
|
103
|
+
models.push(model);
|
|
88
104
|
}
|
|
89
105
|
}
|
|
90
106
|
if (!models.length) throw new Error("No models found. Export defineModel(...) objects from your model files.");
|