@anu8151/adonisjs-blueprint 0.4.6 → 0.4.8
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.
|
@@ -8,33 +8,33 @@ var MigrationGenerator = class extends BaseGenerator {
|
|
|
8
8
|
const processedAttributes = /* @__PURE__ */ new Set();
|
|
9
9
|
if (definition.attributes) for (const [attrName, attrType] of Object.entries(definition.attributes)) {
|
|
10
10
|
let migrationLine = "";
|
|
11
|
-
if (typeof attrType === "string") if (attrType === "foreign") migrationLine = `table.integer('${attrName}').unsigned().references('id').inTable('${string.plural(attrName.replace("_id", ""))}')`;
|
|
12
|
-
else if (attrType.startsWith("enum:")) migrationLine = `table.enum('${attrName}', [${attrType.split(":")[1]
|
|
11
|
+
if (typeof attrType === "string") if (attrType === "foreign") migrationLine = `table.integer('${attrName}').unsigned().references('id').inTable('${string.plural(attrName.replace("_id", "") || "")}')`;
|
|
12
|
+
else if (attrType.startsWith("enum:")) migrationLine = `table.enum('${attrName}', [${attrType.split(":")[1]?.split(",").map((v) => `'${v.trim()}'`).join(", ") || ""}])`;
|
|
13
13
|
else migrationLine = `table.${attrType}('${attrName}')`;
|
|
14
14
|
attributes.push({ migrationLine });
|
|
15
15
|
processedAttributes.add(attrName);
|
|
16
16
|
}
|
|
17
17
|
if (definition.relationships) {
|
|
18
|
-
for (const [relName,
|
|
18
|
+
for (const [relName, relValue] of Object.entries(definition.relationships)) if ((typeof relValue === "string" ? relValue : relValue.type) === "belongsTo" && !processedAttributes.has(`${relName}_id`)) attributes.push({ migrationLine: `table.integer('${relName}_id').unsigned().references('id').inTable('${string.plural(relName || "")}').onDelete('CASCADE')` });
|
|
19
19
|
}
|
|
20
20
|
if (definition.softDeletes) attributes.push({ migrationLine: `table.timestamp('deleted_at')` });
|
|
21
21
|
await this.generateStub("make/migration/main.stub", {
|
|
22
22
|
entity: {
|
|
23
23
|
...entity,
|
|
24
|
-
tableName: string.plural(string.snakeCase(entity.name))
|
|
24
|
+
tableName: string.plural(string.snakeCase(entity.name || ""))
|
|
25
25
|
},
|
|
26
26
|
attributes,
|
|
27
27
|
attributesLines: attributes.map((a) => a.migrationLine).join("\n ")
|
|
28
28
|
});
|
|
29
29
|
if (definition.relationships) {
|
|
30
|
-
for (const [relName,
|
|
30
|
+
for (const [relName, relValue] of Object.entries(definition.relationships)) if ((typeof relValue === "string" ? relValue : relValue.type) === "belongsToMany") await this.generatePivotMigration(entity.name, relName);
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
async generatePivotMigration(modelA, modelB) {
|
|
34
|
-
const models = [string.snakeCase(modelA), string.snakeCase(modelB)].sort();
|
|
34
|
+
const models = [string.snakeCase(modelA || ""), string.snakeCase(modelB || "")].sort();
|
|
35
35
|
const tableName = `${models[0]}_${models[1]}`;
|
|
36
36
|
const entity = this.app.generators.createEntity(tableName);
|
|
37
|
-
const pivotAttributes = [`table.integer('${models[0]}_id').unsigned().references('id').inTable('${string.plural(models[0])}').onDelete('CASCADE')`, `table.integer('${models[1]}_id').unsigned().references('id').inTable('${string.plural(models[1])}').onDelete('CASCADE')`];
|
|
37
|
+
const pivotAttributes = [`table.integer('${models[0]}_id').unsigned().references('id').inTable('${string.plural(models[0] || "")}').onDelete('CASCADE')`, `table.integer('${models[1]}_id').unsigned().references('id').inTable('${string.plural(models[1] || "")}').onDelete('CASCADE')`];
|
|
38
38
|
await this.generateStub("make/migration/main.stub", {
|
|
39
39
|
entity: {
|
|
40
40
|
...entity,
|
|
@@ -26,10 +26,13 @@ var ModelGenerator = class extends BaseGenerator {
|
|
|
26
26
|
attributes.push({ line: `${columnDecorator}\ndeclare ${attrName}: ${tsType}` });
|
|
27
27
|
processedAttributes.add(attrName);
|
|
28
28
|
}
|
|
29
|
-
if (definition.relationships) for (const [relName,
|
|
29
|
+
if (definition.relationships) for (const [relName, relValue] of Object.entries(definition.relationships)) {
|
|
30
|
+
const relType = typeof relValue === "string" ? relValue : relValue.type;
|
|
31
|
+
if (!relType) continue;
|
|
30
32
|
const relatedModelName = string.pascalCase(string.singular(relName));
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
const relTypeClass = relType.charAt(0).toUpperCase() + relType.slice(1);
|
|
34
|
+
let relationshipLine = `@${relType}(() => ${relatedModelName})\ndeclare ${relName}: ${relTypeClass}<typeof ${relatedModelName}>`;
|
|
35
|
+
if (relType === "belongsToMany") relationshipLine = `@${relType}(() => ${relatedModelName}, { pivotTable: '${[entity.className, relatedModelName].sort().map((m) => string.snakeCase(m || "")).join("_")}' })\ndeclare ${relName}: ${relTypeClass}<typeof ${relatedModelName}>`;
|
|
33
36
|
if (relType === "belongsTo") {
|
|
34
37
|
const foreignKey = `${string.camelCase(relName)}Id`;
|
|
35
38
|
if (!processedAttributes.has(foreignKey)) {
|
|
@@ -38,7 +41,7 @@ var ModelGenerator = class extends BaseGenerator {
|
|
|
38
41
|
}
|
|
39
42
|
}
|
|
40
43
|
relationships.push({
|
|
41
|
-
importLine: `import { ${relType} } from '@adonisjs/lucid/orm'\nimport type { ${
|
|
44
|
+
importLine: `import { ${relType} } from '@adonisjs/lucid/orm'\nimport type { ${relTypeClass} } from '@adonisjs/lucid/types'\nimport ${relatedModelName} from '#models/${string.snakeCase(relatedModelName || "")}'`,
|
|
42
45
|
line: relationshipLine
|
|
43
46
|
});
|
|
44
47
|
}
|
|
@@ -13,7 +13,7 @@ import { {{ entity.className }}Factory } from '#database/factories/{{ entity.nam
|
|
|
13
13
|
export default class extends BaseSeeder {
|
|
14
14
|
async run() {
|
|
15
15
|
@if(data)
|
|
16
|
-
await {{ entity.className }}.createMany({{
|
|
16
|
+
await {{ entity.className }}.createMany({{{ dataString }}})
|
|
17
17
|
@else
|
|
18
18
|
await {{ entity.className }}Factory.createMany({{ count }})
|
|
19
19
|
@end
|
package/package.json
CHANGED
|
@@ -13,7 +13,7 @@ import { {{ entity.className }}Factory } from '#database/factories/{{ entity.nam
|
|
|
13
13
|
export default class extends BaseSeeder {
|
|
14
14
|
async run() {
|
|
15
15
|
@if(data)
|
|
16
|
-
await {{ entity.className }}.createMany({{
|
|
16
|
+
await {{ entity.className }}.createMany({{{ dataString }}})
|
|
17
17
|
@else
|
|
18
18
|
await {{ entity.className }}Factory.createMany({{ count }})
|
|
19
19
|
@end
|