@apso/cli 0.47.5 → 0.48.0
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/dist/commands/generate.js +6 -0
- package/dist/lib/apsorc-parser.d.ts +12 -1
- package/dist/lib/apsorc-parser.js +25 -2
- package/dist/lib/ddl/change.js +2 -0
- package/dist/lib/ddl/index.d.ts +11 -1
- package/dist/lib/ddl/index.js +30 -16
- package/dist/lib/ddl/sync.d.ts +25 -0
- package/dist/lib/ddl/sync.js +78 -0
- package/dist/lib/generators/go.d.ts +12 -6
- package/dist/lib/generators/go.js +223 -97
- package/dist/lib/generators/python.d.ts +6 -0
- package/dist/lib/generators/python.js +234 -23
- package/dist/lib/generators/typescript.js +12 -2
- package/dist/lib/templates/entities/entity-col-array.eta +1 -1
- package/dist/lib/templates/go/dto/dto-col-array.eta +2 -2
- package/dist/lib/templates/go/dto/dto-col-date.eta +1 -1
- package/dist/lib/templates/go/dto/dto-col-decimal.eta +1 -1
- package/dist/lib/templates/go/dto/dto-col-enum.eta +1 -1
- package/dist/lib/templates/go/dto/dto-col-float.eta +1 -1
- package/dist/lib/templates/go/dto/dto-col-integer.eta +3 -3
- package/dist/lib/templates/go/dto/dto-col-numeric.eta +1 -1
- package/dist/lib/templates/go/dto/dto-col-string.eta +1 -1
- package/dist/lib/templates/go/dto/dto-col-text.eta +1 -1
- package/dist/lib/templates/go/dto/dto-col-timestamp.eta +1 -1
- package/dist/lib/templates/go/dto/dto-col-timestamptz.eta +1 -1
- package/dist/lib/templates/go/dto/dto-col-uuid.eta +1 -1
- package/dist/lib/templates/go/dto/dto-col-varchar.eta +1 -1
- package/dist/lib/templates/go/dto/dto.eta +17 -18
- package/dist/lib/templates/go/handlers/handler.eta +2 -0
- package/dist/lib/templates/go/index-module.eta +3 -1
- package/dist/lib/templates/go/middleware/auth.eta +1 -1
- package/dist/lib/templates/go/models/model.eta +22 -19
- package/dist/lib/templates/go/models/schema.eta +72 -0
- package/dist/lib/templates/go/services/service.eta +49 -21
- package/dist/lib/templates/go/utils/query.eta +32 -6
- package/dist/lib/templates/python/models/init.eta +24 -0
- package/dist/lib/templates/python/models/model.eta +24 -56
- package/dist/lib/templates/python/schemas/schema-col-array.eta +1 -1
- package/dist/lib/templates/python/schemas/schema.eta +9 -9
- package/dist/lib/utils/field.d.ts +12 -0
- package/dist/lib/utils/field.js +18 -2
- package/npm-shrinkwrap.json +6 -6
- package/oclif.manifest.json +1 -1
- package/package.json +2 -2
- package/dist/lib/templates/go/models/model-col-array.eta +0 -1
- package/dist/lib/templates/go/models/model-col-boolean.eta +0 -1
- package/dist/lib/templates/go/models/model-col-date.eta +0 -1
- package/dist/lib/templates/go/models/model-col-decimal.eta +0 -1
- package/dist/lib/templates/go/models/model-col-enum.eta +0 -2
- package/dist/lib/templates/go/models/model-col-float.eta +0 -1
- package/dist/lib/templates/go/models/model-col-integer.eta +0 -1
- package/dist/lib/templates/go/models/model-col-json.eta +0 -1
- package/dist/lib/templates/go/models/model-col-jsonb.eta +0 -1
- package/dist/lib/templates/go/models/model-col-numeric.eta +0 -1
- package/dist/lib/templates/go/models/model-col-string.eta +0 -1
- package/dist/lib/templates/go/models/model-col-text.eta +0 -1
- package/dist/lib/templates/go/models/model-col-timestamp.eta +0 -1
- package/dist/lib/templates/go/models/model-col-timestamptz.eta +0 -1
- package/dist/lib/templates/go/models/model-col-uuid.eta +0 -1
- package/dist/lib/templates/go/models/model-col-varchar.eta +0 -1
- package/dist/lib/templates/python/models/model-col-array.eta +0 -4
- package/dist/lib/templates/python/models/model-col-boolean.eta +0 -6
- package/dist/lib/templates/python/models/model-col-date.eta +0 -4
- package/dist/lib/templates/python/models/model-col-datetime.eta +0 -5
- package/dist/lib/templates/python/models/model-col-decimal.eta +0 -4
- package/dist/lib/templates/python/models/model-col-enum.eta +0 -16
- package/dist/lib/templates/python/models/model-col-float.eta +0 -6
- package/dist/lib/templates/python/models/model-col-integer.eta +0 -6
- package/dist/lib/templates/python/models/model-col-json.eta +0 -4
- package/dist/lib/templates/python/models/model-col-jsonb.eta +0 -4
- package/dist/lib/templates/python/models/model-col-numeric.eta +0 -4
- package/dist/lib/templates/python/models/model-col-string.eta +0 -6
- package/dist/lib/templates/python/models/model-col-text.eta +0 -6
- package/dist/lib/templates/python/models/model-col-timestamp.eta +0 -11
- package/dist/lib/templates/python/models/model-col-timestamptz.eta +0 -11
- package/dist/lib/templates/python/models/model-col-uuid.eta +0 -5
- package/dist/lib/templates/python/models/model-col-varchar.eta +0 -6
|
@@ -12,7 +12,60 @@ const relationships_1 = require("../utils/relationships");
|
|
|
12
12
|
const casing_1 = require("../utils/casing");
|
|
13
13
|
const events_1 = require("../events");
|
|
14
14
|
const migration_file_1 = require("../ddl/migration-file");
|
|
15
|
+
const ddl_1 = require("../ddl");
|
|
16
|
+
const sync_1 = require("../ddl/sync");
|
|
15
17
|
const pluralize_1 = tslib_1.__importDefault(require("pluralize"));
|
|
18
|
+
// Postgres array element type -> lib/pq array type and its Go element type.
|
|
19
|
+
// Anything else is read and written as text (pq.StringArray).
|
|
20
|
+
const pqArrayTypes = {
|
|
21
|
+
integer: ["pq.Int32Array", "int32"],
|
|
22
|
+
int: ["pq.Int32Array", "int32"],
|
|
23
|
+
int4: ["pq.Int32Array", "int32"],
|
|
24
|
+
smallint: ["pq.Int32Array", "int32"],
|
|
25
|
+
bigint: ["pq.Int64Array", "int64"],
|
|
26
|
+
numeric: ["pq.Float64Array", "float64"],
|
|
27
|
+
decimal: ["pq.Float64Array", "float64"],
|
|
28
|
+
float: ["pq.Float64Array", "float64"],
|
|
29
|
+
float8: ["pq.Float64Array", "float64"],
|
|
30
|
+
"double precision": ["pq.Float64Array", "float64"],
|
|
31
|
+
real: ["pq.Float32Array", "float32"],
|
|
32
|
+
boolean: ["pq.BoolArray", "bool"],
|
|
33
|
+
bool: ["pq.BoolArray", "bool"],
|
|
34
|
+
};
|
|
35
|
+
// Field type -> [Go type, DTO template]. Arrays and enums are typed per field.
|
|
36
|
+
const goFieldTypes = {
|
|
37
|
+
text: ["string", "text"],
|
|
38
|
+
string: ["string", "string"],
|
|
39
|
+
varchar: ["string", "varchar"],
|
|
40
|
+
char: ["string", "varchar"],
|
|
41
|
+
uuid: ["string", "uuid"],
|
|
42
|
+
enum: ["string", "enum"],
|
|
43
|
+
integer: ["int", "integer"],
|
|
44
|
+
bigint: ["int64", "integer"],
|
|
45
|
+
smallint: ["int16", "integer"],
|
|
46
|
+
float: ["float64", "float"],
|
|
47
|
+
double: ["float64", "float"],
|
|
48
|
+
decimal: ["float64", "decimal"],
|
|
49
|
+
numeric: ["float64", "numeric"],
|
|
50
|
+
boolean: ["bool", "boolean"],
|
|
51
|
+
date: ["time.Time", "date"],
|
|
52
|
+
timestamp: ["time.Time", "timestamp"],
|
|
53
|
+
timestamptz: ["time.Time", "timestamptz"],
|
|
54
|
+
json: ["map[string]interface{}", "json"],
|
|
55
|
+
jsonb: ["map[string]interface{}", "jsonb"],
|
|
56
|
+
"json-plain": ["map[string]interface{}", "json"],
|
|
57
|
+
array: ["", "array"],
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* A gorm struct tag value from its settings. gorm splits on ";" (a "\;"
|
|
61
|
+
* stays literal), and the tag is a Go string literal, so quotes and
|
|
62
|
+
* backslashes are escaped for it.
|
|
63
|
+
*/
|
|
64
|
+
const gormTag = (settings) => settings
|
|
65
|
+
.map((s) => s.replace(/;/g, "\\;"))
|
|
66
|
+
.join(";")
|
|
67
|
+
.replace(/\\/g, "\\\\")
|
|
68
|
+
.replace(/"/g, '\\"');
|
|
16
69
|
/**
|
|
17
70
|
* Go/Gin generator that produces GORM models, DTOs, Gin handlers,
|
|
18
71
|
* and service layer code.
|
|
@@ -59,7 +112,12 @@ class GoGenerator extends base_1.BaseGenerator {
|
|
|
59
112
|
"text",
|
|
60
113
|
"varchar",
|
|
61
114
|
"string",
|
|
115
|
+
"char",
|
|
62
116
|
"integer",
|
|
117
|
+
"bigint",
|
|
118
|
+
"smallint",
|
|
119
|
+
"double",
|
|
120
|
+
"json-plain",
|
|
63
121
|
"boolean",
|
|
64
122
|
"float",
|
|
65
123
|
"decimal",
|
|
@@ -84,30 +142,6 @@ class GoGenerator extends base_1.BaseGenerator {
|
|
|
84
142
|
}
|
|
85
143
|
return this.createValidationResult(errors, warnings);
|
|
86
144
|
}
|
|
87
|
-
/**
|
|
88
|
-
* Maps .apsorc field types to Go types
|
|
89
|
-
*/
|
|
90
|
-
mapFieldToGoType(field) {
|
|
91
|
-
const typeMap = {
|
|
92
|
-
text: "string",
|
|
93
|
-
varchar: "string",
|
|
94
|
-
string: "string",
|
|
95
|
-
integer: "int",
|
|
96
|
-
boolean: "bool",
|
|
97
|
-
float: "float64",
|
|
98
|
-
decimal: "float64",
|
|
99
|
-
numeric: "float64",
|
|
100
|
-
date: "time.Time",
|
|
101
|
-
timestamp: "time.Time",
|
|
102
|
-
timestamptz: "time.Time",
|
|
103
|
-
json: "map[string]interface{}",
|
|
104
|
-
jsonb: "map[string]interface{}",
|
|
105
|
-
uuid: "string",
|
|
106
|
-
enum: "string",
|
|
107
|
-
array: "[]string",
|
|
108
|
-
};
|
|
109
|
-
return typeMap[field.type || "string"] || "string";
|
|
110
|
-
}
|
|
111
145
|
/**
|
|
112
146
|
* Gets the Go type for primary keys
|
|
113
147
|
*/
|
|
@@ -120,58 +154,130 @@ class GoGenerator extends base_1.BaseGenerator {
|
|
|
120
154
|
getPrimaryKeySwaggerType(primaryKeyType) {
|
|
121
155
|
return primaryKeyType === "uuid" ? "string" : "integer";
|
|
122
156
|
}
|
|
157
|
+
arrayTypes(field, entityName) {
|
|
158
|
+
const elementType = (0, field_1.arrayElementType)(field);
|
|
159
|
+
// It goes into a struct tag, so only a plain type name.
|
|
160
|
+
if (!/^[a-z][\w ]*$/i.test(elementType))
|
|
161
|
+
throw new Error(`Field ${entityName}.${field.name} has an invalid elementType "${elementType}".`);
|
|
162
|
+
const [arrayGoType, arrayItemGoType] = pqArrayTypes[elementType.toLowerCase()] || ["pq.StringArray", "string"];
|
|
163
|
+
return { elementType, arrayGoType, arrayItemGoType };
|
|
164
|
+
}
|
|
123
165
|
/**
|
|
124
|
-
*
|
|
166
|
+
* Fields for the Go templates. The gorm tag carries the column exactly as
|
|
167
|
+
* the DDL writer creates it (name, type, NOT NULL, default), so the model
|
|
168
|
+
* reads and writes the database `apso migrate` builds.
|
|
125
169
|
*/
|
|
126
|
-
getGoFieldsForTemplate(fields,
|
|
127
|
-
const
|
|
128
|
-
return
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
170
|
+
getGoFieldsForTemplate(fields, entity) {
|
|
171
|
+
const table = (0, ddl_1.tableName)(entity);
|
|
172
|
+
return (0, field_1.getFieldForTemplate)(fields, entity.name).map((field) => {
|
|
173
|
+
const spec = (0, ddl_1.columnSpec)(table, field);
|
|
174
|
+
const [goScalar, dtoTemplate] = goFieldTypes[field.type] || [];
|
|
175
|
+
const array = field.type === "array" && this.arrayTypes(field, entity.name);
|
|
176
|
+
const enumType = field.type === "enum" ? field.dataType : undefined;
|
|
177
|
+
const scalar = enumType ? `enums.${enumType}` : goScalar;
|
|
178
|
+
const tag = [
|
|
179
|
+
`column:${field.name}`,
|
|
180
|
+
`type:${field.type === "enum"
|
|
181
|
+
? `"${(0, ddl_1.enumTypeName)(table, field.name)}"`
|
|
182
|
+
: spec.type.toLowerCase().replace("timestamp with time zone", "timestamptz")}`,
|
|
183
|
+
...(field.primary ? ["primaryKey"] : []),
|
|
184
|
+
...(spec.notNull ? ["not null"] : []),
|
|
185
|
+
// gorm parses a number or boolean default itself, so it goes unquoted.
|
|
186
|
+
...(spec.default === undefined
|
|
187
|
+
? []
|
|
188
|
+
: [`default:${/^(u?int|float|bool)/.test(goScalar) ? String(field.default) : spec.default}`]),
|
|
189
|
+
...(goScalar === "map[string]interface{}" ? ["serializer:json"] : []),
|
|
190
|
+
];
|
|
191
|
+
return {
|
|
192
|
+
...field,
|
|
193
|
+
...array,
|
|
194
|
+
goType: scalar,
|
|
195
|
+
modelGoType: array
|
|
196
|
+
? array.arrayGoType
|
|
197
|
+
: goScalar === "map[string]interface{}"
|
|
198
|
+
? goScalar
|
|
199
|
+
: `*${scalar}`,
|
|
200
|
+
gormTag: gormTag(tag),
|
|
201
|
+
dtoTemplate,
|
|
202
|
+
pascalName: (0, casing_1.pascalCase)(field.name),
|
|
203
|
+
snakeName: (0, casing_1.snakeCase)(field.name),
|
|
204
|
+
// NOT NULL with no default: the create DTO requires it.
|
|
205
|
+
mustSend: spec.notNull && spec.default === undefined,
|
|
206
|
+
enumType,
|
|
207
|
+
};
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/** Relationships with what the Go templates need: field names, columns, join tables. */
|
|
211
|
+
goAssociations(entity, relationships, allEntities) {
|
|
212
|
+
var _a;
|
|
213
|
+
const byName = new Map(allEntities.map((e) => [e.name, e]));
|
|
214
|
+
const relationshipMap = (_a = this.config.relationshipMap) !== null && _a !== void 0 ? _a : {};
|
|
215
|
+
return (0, relationships_1.getRelationshipForTemplate)(entity.name, relationships, allEntities).map((rel) => {
|
|
216
|
+
const target = byName.get(rel.name);
|
|
217
|
+
const many = rel.type === "OneToMany" || rel.type === "ManyToMany";
|
|
218
|
+
const fieldName = (0, casing_1.pascalCase)(many ? rel.pluralizedRelationshipName : rel.relationshipName);
|
|
219
|
+
const owns = rel.type === "ManyToOne" || (rel.type === "OneToOne" && rel.joinTable);
|
|
220
|
+
const fkNullable = rel.type === "OneToOne" || Boolean(rel.nullable);
|
|
221
|
+
const pkGoType = this.getPrimaryKeyGoType((target === null || target === void 0 ? void 0 : target.primaryKeyType) || "serial");
|
|
222
|
+
let join;
|
|
223
|
+
if (rel.type === "ManyToMany" && target) {
|
|
224
|
+
if (rel.joinTable)
|
|
225
|
+
join = (0, ddl_1.joinTableOf)(entity, rel, target);
|
|
226
|
+
else {
|
|
227
|
+
// The other side owns the join table; its key columns swap.
|
|
228
|
+
const owner = (0, relationships_1.getRelationshipForTemplate)(target.name, relationshipMap[target.name] || [], allEntities).find((r) => r.type === "ManyToMany" && r.joinTable && r.name === entity.name);
|
|
229
|
+
if (owner) {
|
|
230
|
+
const j = (0, ddl_1.joinTableOf)(target, owner, entity);
|
|
231
|
+
join = { name: j.name, ownCol: j.otherCol, otherCol: j.ownCol };
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
...rel,
|
|
237
|
+
fieldName,
|
|
238
|
+
jsonName: (0, casing_1.camelCase)(fieldName),
|
|
239
|
+
owns,
|
|
240
|
+
fkField: `${(0, casing_1.pascalCase)(rel.relationshipName)}ID`,
|
|
241
|
+
fkColumn: owns ? (0, ddl_1.relationColumn)(rel) : undefined,
|
|
242
|
+
fkNullable,
|
|
243
|
+
primaryKeyGoType: pkGoType,
|
|
244
|
+
fkGoType: `${fkNullable ? "*" : ""}${pkGoType}`,
|
|
245
|
+
fkTag: owns
|
|
246
|
+
? gormTag([
|
|
247
|
+
`column:${(0, ddl_1.relationColumn)(rel)}`,
|
|
248
|
+
`type:${(0, ddl_1.fkType)(target)}`,
|
|
249
|
+
...(fkNullable ? [] : ["not null"]),
|
|
250
|
+
])
|
|
251
|
+
: undefined,
|
|
252
|
+
// The child's foreign key field, for OneToMany and the inverse OneToOne.
|
|
253
|
+
foreignKey: `${(0, casing_1.pascalCase)(rel.inverseReferenceName || entity.name)}ID`,
|
|
254
|
+
onDeleteCascade: rel.type === "OneToOne" || Boolean(rel.cascadeDelete),
|
|
255
|
+
join,
|
|
256
|
+
camelReferenceName: (0, casing_1.camelCase)(rel.referenceName || rel.name),
|
|
257
|
+
snakeReferenceName: (0, casing_1.snakeCase)(rel.referenceName || rel.name),
|
|
258
|
+
};
|
|
259
|
+
});
|
|
136
260
|
}
|
|
137
261
|
async generateEntity(options) {
|
|
138
262
|
const { entity, relationships, allEntities, apiType } = options;
|
|
139
|
-
const { name, fields = []
|
|
140
|
-
const columns = this.getGoFieldsForTemplate(fields,
|
|
263
|
+
const { name, fields = [] } = entity;
|
|
264
|
+
const columns = this.getGoFieldsForTemplate(fields, entity);
|
|
141
265
|
const createPrimaryKey = columns.filter((column) => column.primary === true).length === 0;
|
|
142
|
-
const createdAt = entity.created_at;
|
|
143
|
-
const updatedAt = entity.updated_at;
|
|
144
266
|
const primaryKeyType = entity.primaryKeyType || "serial";
|
|
145
|
-
const
|
|
146
|
-
// Add Go-specific properties to relationships
|
|
147
|
-
const goRelationships = relationshipsForTemplate.map((rel) => ({
|
|
148
|
-
...rel,
|
|
149
|
-
primaryKeyGoType: this.getPrimaryKeyGoType(primaryKeyType),
|
|
150
|
-
camelReferenceName: (0, casing_1.camelCase)(rel.referenceName || rel.name),
|
|
151
|
-
snakeReferenceName: (0, casing_1.snakeCase)(rel.referenceName || rel.name),
|
|
152
|
-
pluralName: (0, casing_1.pascalCase)((0, pluralize_1.default)(rel.name)),
|
|
153
|
-
camelPluralName: (0, casing_1.camelCase)((0, pluralize_1.default)(rel.name)),
|
|
154
|
-
inverseName: (0, casing_1.pascalCase)(rel.inversePropertyName || name),
|
|
155
|
-
}));
|
|
156
|
-
const entitiesToImport = (0, relationships_1.getRelationshipsForImport)(name, relationshipsForTemplate);
|
|
267
|
+
const associations = this.goAssociations(entity, relationships, allEntities);
|
|
157
268
|
const data = {
|
|
158
269
|
name,
|
|
159
|
-
indexes: indexes || [],
|
|
160
|
-
uniques: uniques || [],
|
|
161
270
|
createPrimaryKey,
|
|
162
271
|
primaryKeyType,
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
tableName: entity.table || (0, casing_1.snakeCase)(name),
|
|
167
|
-
createdAt,
|
|
168
|
-
updatedAt,
|
|
169
|
-
pluralizedName: (0, casing_1.camelCase)((0, pluralize_1.default)(name)),
|
|
170
|
-
pluralName: (0, casing_1.pascalCase)((0, pluralize_1.default)(name)),
|
|
272
|
+
tableName: (0, ddl_1.tableName)(entity),
|
|
273
|
+
createdAt: entity.created_at,
|
|
274
|
+
updatedAt: entity.updated_at,
|
|
171
275
|
columns,
|
|
172
|
-
associations
|
|
173
|
-
|
|
276
|
+
associations,
|
|
277
|
+
importTime: Boolean(entity.created_at || entity.updated_at) ||
|
|
278
|
+
columns.some((c) => c.goType === "time.Time"),
|
|
174
279
|
importEnums: (0, field_1.typeExistsInEntity)(entity, "enum") !== -1,
|
|
280
|
+
importPq: (0, field_1.typeExistsInEntity)(entity, "array") !== -1,
|
|
175
281
|
apiType,
|
|
176
282
|
};
|
|
177
283
|
const content = await this.renderTemplate("./models/model", data);
|
|
@@ -209,18 +315,8 @@ class GoGenerator extends base_1.BaseGenerator {
|
|
|
209
315
|
async generateService(options) {
|
|
210
316
|
const { entity, relationships, allEntities } = options;
|
|
211
317
|
const { name: entityName, fields = [] } = entity;
|
|
212
|
-
const columns = this.getGoFieldsForTemplate(fields,
|
|
213
|
-
const
|
|
214
|
-
// Add Go-specific properties to relationships
|
|
215
|
-
const goRelationships = relationshipsForTemplate.map((rel) => {
|
|
216
|
-
const referencedEntity = allEntities === null || allEntities === void 0 ? void 0 : allEntities.find((e) => e.name === rel.name);
|
|
217
|
-
const referencedPrimaryKeyType = (referencedEntity === null || referencedEntity === void 0 ? void 0 : referencedEntity.primaryKeyType) || "serial";
|
|
218
|
-
return {
|
|
219
|
-
...rel,
|
|
220
|
-
primaryKeyGoType: this.getPrimaryKeyGoType(referencedPrimaryKeyType),
|
|
221
|
-
snakeReferenceName: (0, casing_1.snakeCase)(rel.referenceName || rel.name),
|
|
222
|
-
};
|
|
223
|
-
});
|
|
318
|
+
const columns = this.getGoFieldsForTemplate(fields, entity);
|
|
319
|
+
const goRelationships = this.goAssociations(entity, relationships, allEntities);
|
|
224
320
|
const pluralEntityName = (0, pluralize_1.default)(entityName);
|
|
225
321
|
const primaryKeyType = entity.primaryKeyType || "serial";
|
|
226
322
|
const createdAt = entity.created_at;
|
|
@@ -248,22 +344,12 @@ class GoGenerator extends base_1.BaseGenerator {
|
|
|
248
344
|
const { entity, relationships, allEntities, apiType } = options;
|
|
249
345
|
const { name: entityName, fields = [] } = entity;
|
|
250
346
|
const scopeFields = new Set((_a = (0, guards_1.entityScope)(entity, Boolean(options.auth))) === null || _a === void 0 ? void 0 : _a.fields.map((f) => f.field));
|
|
251
|
-
const columns = this.getGoFieldsForTemplate(fields,
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
return {
|
|
258
|
-
...rel,
|
|
259
|
-
primaryKeyGoType: this.getPrimaryKeyGoType(referencedPrimaryKeyType),
|
|
260
|
-
camelReferenceName: (0, casing_1.camelCase)(rel.referenceName || rel.name),
|
|
261
|
-
pluralName: (0, casing_1.pascalCase)((0, pluralize_1.default)(rel.name)),
|
|
262
|
-
camelPluralName: (0, casing_1.camelCase)((0, pluralize_1.default)(rel.name)),
|
|
263
|
-
// The service sets a scopeBy foreign key from the caller on create.
|
|
264
|
-
scoped: scopeFields.has(`${(0, casing_1.camelCase)(rel.referenceName || rel.name)}Id`),
|
|
265
|
-
};
|
|
266
|
-
});
|
|
347
|
+
const columns = this.getGoFieldsForTemplate(fields, entity);
|
|
348
|
+
const goRelationships = this.goAssociations(entity, relationships, allEntities).map((rel) => ({
|
|
349
|
+
...rel,
|
|
350
|
+
// The service sets a scopeBy foreign key from the caller on create.
|
|
351
|
+
scoped: scopeFields.has(`${rel.camelReferenceName}Id`),
|
|
352
|
+
}));
|
|
267
353
|
const primaryKeyColumns = columns.filter((column) => column.primary === true);
|
|
268
354
|
const addDefaultPKProperty = primaryKeyColumns.length === 0;
|
|
269
355
|
const createdAt = entity.created_at;
|
|
@@ -278,6 +364,8 @@ class GoGenerator extends base_1.BaseGenerator {
|
|
|
278
364
|
columns,
|
|
279
365
|
associations: goRelationships,
|
|
280
366
|
apiType: apiType !== null && apiType !== void 0 ? apiType : "rest",
|
|
367
|
+
importTime: Boolean(createdAt || updatedAt) ||
|
|
368
|
+
columns.some((c) => c.goType === "time.Time"),
|
|
281
369
|
importEnums: (0, field_1.typeExistsInEntity)(entity, "enum") !== -1,
|
|
282
370
|
};
|
|
283
371
|
const content = await this.renderTemplate("./dto/dto", data);
|
|
@@ -369,13 +457,12 @@ ${downStatements}
|
|
|
369
457
|
entities: entitiesData,
|
|
370
458
|
hasAuth: Boolean(opts === null || opts === void 0 ? void 0 : opts.auth),
|
|
371
459
|
});
|
|
372
|
-
// Generate model registry for AutoMigrate support
|
|
373
460
|
const modelRefs = entities
|
|
374
461
|
.map((e) => `\t\t&${(0, casing_1.pascalCase)(e.name)}{},`)
|
|
375
462
|
.join("\n");
|
|
376
463
|
const registryContent = `package models
|
|
377
464
|
|
|
378
|
-
// All returns all registered GORM models
|
|
465
|
+
// All returns all registered GORM models.
|
|
379
466
|
// This file is auto-generated by the Apso CLI.
|
|
380
467
|
func All() []interface{} {
|
|
381
468
|
\treturn []interface{}{
|
|
@@ -392,8 +479,47 @@ ${modelRefs}
|
|
|
392
479
|
path: "models/registry.go",
|
|
393
480
|
content: registryContent,
|
|
394
481
|
},
|
|
482
|
+
{
|
|
483
|
+
path: "models/schema.go",
|
|
484
|
+
content: await this.renderTemplate("./models/schema", this.schemaData(entities)),
|
|
485
|
+
},
|
|
395
486
|
];
|
|
396
487
|
}
|
|
488
|
+
/**
|
|
489
|
+
* DATABASE_SYNC's statements (the DDL writer's, creating only what is
|
|
490
|
+
* missing) and the join table columns gorm must be told about.
|
|
491
|
+
*/
|
|
492
|
+
schemaData(entities) {
|
|
493
|
+
var _a, _b;
|
|
494
|
+
const relationshipMap = (_a = this.config.relationshipMap) !== null && _a !== void 0 ? _a : {};
|
|
495
|
+
const legacy = [];
|
|
496
|
+
const joinColumns = new Map();
|
|
497
|
+
for (const entity of entities) {
|
|
498
|
+
const table = (0, ddl_1.tableName)(entity);
|
|
499
|
+
// Older Go models let gorm name columns: snake_case of the Go field.
|
|
500
|
+
const renames = ((_b = entity.fields) !== null && _b !== void 0 ? _b : []).map((f) => [
|
|
501
|
+
(0, casing_1.snakeCase)(f.name),
|
|
502
|
+
f.name,
|
|
503
|
+
]);
|
|
504
|
+
for (const a of this.goAssociations(entity, relationshipMap[entity.name] || [], entities)) {
|
|
505
|
+
if (a.owns)
|
|
506
|
+
renames.push([`${(0, casing_1.snakeCase)(a.relationshipName)}_id`, a.fkColumn]);
|
|
507
|
+
if (a.join)
|
|
508
|
+
for (const c of [a.join.ownCol, a.join.otherCol])
|
|
509
|
+
joinColumns.set(c.charAt(0).toUpperCase() + c.slice(1), c);
|
|
510
|
+
}
|
|
511
|
+
for (const [from, to] of renames)
|
|
512
|
+
if (from !== to)
|
|
513
|
+
legacy.push((0, sync_1.legacyColumn)(table, from, to));
|
|
514
|
+
}
|
|
515
|
+
return {
|
|
516
|
+
statements: (0, sync_1.syncStatements)({ entities, relationshipMap }, legacy).map((sql) => JSON.stringify(sql)),
|
|
517
|
+
joinColumns: [...joinColumns].map(([k, v]) => [
|
|
518
|
+
JSON.stringify(k),
|
|
519
|
+
JSON.stringify(v),
|
|
520
|
+
]),
|
|
521
|
+
};
|
|
522
|
+
}
|
|
397
523
|
/**
|
|
398
524
|
* Emits a schema-derived manifest of the entities that opted in to
|
|
399
525
|
* domain-event emission (`.apsorc` `emitEvents`), mirroring the TypeScript
|
|
@@ -428,7 +554,7 @@ ${modelRefs}
|
|
|
428
554
|
}
|
|
429
555
|
/**
|
|
430
556
|
* Scope data for a Go service: each direct scopeBy field's struct field and
|
|
431
|
-
*
|
|
557
|
+
* column (the .apsorc name, as the DDL writer names it). A field declared on the entity is
|
|
432
558
|
* pascalCase; a relation's foreign key (organizationId) is OrganizationID.
|
|
433
559
|
*/
|
|
434
560
|
goScope(entity, hasAuth) {
|
|
@@ -444,7 +570,7 @@ ${modelRefs}
|
|
|
444
570
|
goField: declared.has(f.field) || !f.field.endsWith("Id")
|
|
445
571
|
? (0, casing_1.pascalCase)(f.field)
|
|
446
572
|
: `${(0, casing_1.pascalCase)(f.field.slice(0, -2))}ID`,
|
|
447
|
-
column:
|
|
573
|
+
column: f.field,
|
|
448
574
|
})),
|
|
449
575
|
};
|
|
450
576
|
}
|
|
@@ -461,8 +587,8 @@ ${modelRefs}
|
|
|
461
587
|
else if ((0, types_1.isApiKeyAuth)(auth)) {
|
|
462
588
|
const apiKey = (0, guards_1.apiKeyTemplateData)((0, types_1.resolveApiKeyAuth)(auth), entities);
|
|
463
589
|
const entity = entities.find((e) => e.name === apiKey.apiKeyEntity);
|
|
464
|
-
//
|
|
465
|
-
const column = (name) =>
|
|
590
|
+
// Columns keep their .apsorc names (the models tag each one).
|
|
591
|
+
const column = (name) => name;
|
|
466
592
|
data = {
|
|
467
593
|
apiKey: {
|
|
468
594
|
...apiKey,
|
|
@@ -19,6 +19,12 @@ export declare class PythonGenerator extends BaseGenerator {
|
|
|
19
19
|
generateIndexModule(entities: Entity[], _apiType: string, opts?: {
|
|
20
20
|
auth?: AuthConfig;
|
|
21
21
|
}): Promise<GeneratedFile[]>;
|
|
22
|
+
/**
|
|
23
|
+
* What create_all must run first: the extensions the tables use, and a
|
|
24
|
+
* guard that stops when the database still has a join table or enum type
|
|
25
|
+
* under the name older generated models gave it.
|
|
26
|
+
*/
|
|
27
|
+
private modelsInitData;
|
|
22
28
|
/**
|
|
23
29
|
* Emits a schema-derived manifest of the entities that opted in to
|
|
24
30
|
* domain-event emission (`.apsorc` `emitEvents`), mirroring the TypeScript
|