@omnifyjp/ts 6.3.8 → 6.3.10
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.
|
@@ -4,5 +4,6 @@
|
|
|
4
4
|
import { SchemaReader } from './schema-reader.js';
|
|
5
5
|
import type { GeneratedFile, PhpConfig } from './types.js';
|
|
6
6
|
/** Generate base model and user model for all project-owned object schemas,
|
|
7
|
-
* plus
|
|
7
|
+
* plus, for each package schema the project extends, a user model that
|
|
8
|
+
* extends the package model and carries the extend's columns. */
|
|
8
9
|
export declare function generateModels(reader: SchemaReader, config: PhpConfig): GeneratedFile[];
|
|
@@ -8,24 +8,34 @@ import { buildRelation } from './relation-builder.js';
|
|
|
8
8
|
import { baseFile, userFile, shadowingPropertyConflicts, resolveModularBasePath, resolveModularBaseNamespace, resolveSharedBaseNamespace, resolveGlobalTraitNamespace, resolveGlobalEnumNamespace, resolveBaseClass, resolveEditableClass, nestByGroup, nestEditableByGroup, } from './types.js';
|
|
9
9
|
import { enumClassName } from './enum-generator.js';
|
|
10
10
|
/** Generate base model and user model for all project-owned object schemas,
|
|
11
|
-
* plus
|
|
11
|
+
* plus, for each package schema the project extends, a user model that
|
|
12
|
+
* extends the package model and carries the extend's columns. */
|
|
12
13
|
export function generateModels(reader, config) {
|
|
13
14
|
const files = [];
|
|
14
15
|
// Project-owned schemas: base model + user model
|
|
15
16
|
for (const [name, schema] of Object.entries(reader.getProjectObjectSchemas())) {
|
|
16
17
|
files.push(...generateForSchema(name, schema, reader, config));
|
|
17
18
|
}
|
|
18
|
-
// Package schemas
|
|
19
|
-
//
|
|
19
|
+
// Package schemas the project extends with `kind: extend`: a trait carrying
|
|
20
|
+
// the added columns (#164) plus a user model in the project namespace that
|
|
21
|
+
// `use`s it — the trait needs a class to live in, and the package's own
|
|
22
|
+
// model cannot be that class once it is installed under `vendor/`.
|
|
23
|
+
//
|
|
24
|
+
// A package schema the project does NOT extend gets nothing: the package
|
|
25
|
+
// model is the model, and relations already point at it. Every such
|
|
26
|
+
// schema used to get an empty `extends Package<Name>` stub anyway, and a
|
|
27
|
+
// workspace of three packages saw sixteen of them on every fresh checkout
|
|
28
|
+
// after deleting them (#182). A project that wants a local subclass for one
|
|
29
|
+
// of these writes the five lines itself, or extends the schema.
|
|
20
30
|
for (const [name, schema] of Object.entries(reader.getPackageObjectSchemas())) {
|
|
21
31
|
const pkgNs = reader.resolveModelNamespace(name, config.models.namespace);
|
|
22
|
-
if (pkgNs
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
32
|
+
if (pkgNs === config.models.namespace)
|
|
33
|
+
continue;
|
|
34
|
+
const extended = extendedPropertyNames(schema);
|
|
35
|
+
if (extended.length === 0)
|
|
36
|
+
continue;
|
|
37
|
+
files.push(generateExtendAttributesTrait(name, schema, extended, reader, config));
|
|
38
|
+
files.push(generatePackageUserModel(name, pkgNs, config, schema.kind === 'pivot'));
|
|
29
39
|
}
|
|
30
40
|
return files;
|
|
31
41
|
}
|
|
@@ -1056,8 +1066,9 @@ function buildAccessors(expandedProperties) {
|
|
|
1056
1066
|
}
|
|
1057
1067
|
return methods.join('\n');
|
|
1058
1068
|
}
|
|
1059
|
-
/** Generate a user model for a package schema
|
|
1060
|
-
|
|
1069
|
+
/** Generate a user model for a package schema the project extends: it
|
|
1070
|
+
* extends the package's model class and `use`s the extend-attributes trait. */
|
|
1071
|
+
function generatePackageUserModel(name, packageNamespace, config, isPivot = false) {
|
|
1061
1072
|
const modelName = toPascalCase(name);
|
|
1062
1073
|
const modelNamespace = config.models.namespace;
|
|
1063
1074
|
// #178: a `kind: pivot` schema's editable model is emitted under the
|
|
@@ -1074,12 +1085,8 @@ function generatePackageUserModel(name, packageNamespace, config, hasExtendAttri
|
|
|
1074
1085
|
// predates its extend keeps the file it has, and `generateModels` warns with
|
|
1075
1086
|
// the one line to add — overwriting somebody's model to add an import is the
|
|
1076
1087
|
// worse of the two.
|
|
1077
|
-
const traitImport =
|
|
1078
|
-
|
|
1079
|
-
: '';
|
|
1080
|
-
const traitUse = hasExtendAttributes
|
|
1081
|
-
? ` use ${traitName};\n\n`
|
|
1082
|
-
: '';
|
|
1088
|
+
const traitImport = `use ${modelNamespace}\\Concerns\\${traitName};\n`;
|
|
1089
|
+
const traitUse = ` use ${traitName};\n\n`;
|
|
1083
1090
|
const content = `<?php
|
|
1084
1091
|
|
|
1085
1092
|
namespace ${modelNamespace};
|
|
@@ -1097,16 +1104,14 @@ class ${modelName} extends Package${modelName}
|
|
|
1097
1104
|
${traitUse} // Add your custom methods here
|
|
1098
1105
|
}
|
|
1099
1106
|
`;
|
|
1100
|
-
return userFile(`${config.models.path}/${modelName}.php`, content,
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
}
|
|
1109
|
-
: undefined);
|
|
1107
|
+
return userFile(`${config.models.path}/${modelName}.php`, content, {
|
|
1108
|
+
needle: `use ${traitName};`,
|
|
1109
|
+
hint: `this project's \`kind: extend\` adds columns to the package-owned `
|
|
1110
|
+
+ `${modelName} schema, but this model predates the trait that carries `
|
|
1111
|
+
+ `them, so those columns are still not fillable or cast. Add `
|
|
1112
|
+
+ `\`use ${config.models.namespace}\\Concerns\\${traitName};\` and `
|
|
1113
|
+
+ `\`use ${traitName};\` inside the class (#164).`,
|
|
1114
|
+
});
|
|
1110
1115
|
}
|
|
1111
1116
|
/**
|
|
1112
1117
|
* Detect whether a schema uses nested set (tree structure).
|
|
@@ -102,7 +102,7 @@ function generateBaseResource(name, schema, reader, config) {
|
|
|
102
102
|
if (isHiddenByDefault(type) || hidden)
|
|
103
103
|
continue;
|
|
104
104
|
if (type === 'Association') {
|
|
105
|
-
addAssociationFields(propName, prop, fields, resourceNamespace, modelNamespace, reader, config, importer);
|
|
105
|
+
addAssociationFields(name, propName, prop, fields, resourceNamespace, modelNamespace, reader, config, importer);
|
|
106
106
|
continue;
|
|
107
107
|
}
|
|
108
108
|
if (expandedProperties[propName]) {
|
|
@@ -258,7 +258,7 @@ class ${editable.className} extends ${baseAlias}
|
|
|
258
258
|
`;
|
|
259
259
|
return userFile(`${editable.path}/${editable.fileName}`, content);
|
|
260
260
|
}
|
|
261
|
-
function addAssociationFields(propName, prop, fields, resourceNamespace, modelNamespace, reader, config, importer) {
|
|
261
|
+
function addAssociationFields(schemaName, propName, prop, fields, resourceNamespace, modelNamespace, reader, config, importer) {
|
|
262
262
|
const relation = prop['relation'] ?? '';
|
|
263
263
|
const target = prop['target'] ?? '';
|
|
264
264
|
const methodName = toCamelCase(propName);
|
|
@@ -309,6 +309,23 @@ function addAssociationFields(propName, prop, fields, resourceNamespace, modelNa
|
|
|
309
309
|
// If target is a package schema without a resource namespace, or a model no
|
|
310
310
|
// Resource is written for, use simple whenLoaded
|
|
311
311
|
if ((!targetResNs || !targetHasResource) && relation !== 'MorphTo') {
|
|
312
|
+
// Say it. An unwrapped relation serializes the raw model instead of the
|
|
313
|
+
// shaped resource, so the endpoint's response changes shape — and the PHP
|
|
314
|
+
// still compiles, so the only way anyone noticed was an unexpected line in
|
|
315
|
+
// a diff (#187). A target that is not a schema at all is the ordinary case
|
|
316
|
+
// (an external model reached through `targetNamespace`) and stays quiet.
|
|
317
|
+
if (reader.getSchema(target)) {
|
|
318
|
+
console.warn(`[omnify-ts] ${schemaName}.${propName}: no Resource class for target `
|
|
319
|
+
+ `'${target}', so it serializes as the raw model instead of `
|
|
320
|
+
+ `${toPascalCase(target)}Resource. `
|
|
321
|
+
+ (targetResNs
|
|
322
|
+
? `Cause: this codegen target writes no Resource for ${target} `
|
|
323
|
+
+ `(kind: extend, options.hidden, or an includeSchemas / `
|
|
324
|
+
+ `excludeSchemas filter that leaves it out).`
|
|
325
|
+
: `Cause: package '${reader.getSchema(target)?.package}' declares no `
|
|
326
|
+
+ `codegen.laravel.resource.namespace, so its Resource class cannot `
|
|
327
|
+
+ `be named from here.`));
|
|
328
|
+
}
|
|
312
329
|
if (relation === 'ManyToOne') {
|
|
313
330
|
const snakeName = toFkColumnName(propName);
|
|
314
331
|
fields.push(` '${snakeName}' => $this->${snakeName},`);
|
|
@@ -18,6 +18,12 @@ export declare class SchemaReader {
|
|
|
18
18
|
name: string;
|
|
19
19
|
schema: SchemaDefinition;
|
|
20
20
|
} | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Object schemas this run generates for. `referenceOnly` entries are left
|
|
23
|
+
* out: they are in the export solely so relation targets resolve (#187),
|
|
24
|
+
* and every generation loop reaches the set through this method.
|
|
25
|
+
* `getSchema()` deliberately still returns them.
|
|
26
|
+
*/
|
|
21
27
|
getObjectSchemas(): Record<string, SchemaDefinition>;
|
|
22
28
|
getVisibleObjectSchemas(): Record<string, SchemaDefinition>;
|
|
23
29
|
getEnumSchemas(): Record<string, SchemaDefinition>;
|
|
@@ -37,11 +37,17 @@ export class SchemaReader {
|
|
|
37
37
|
}
|
|
38
38
|
return undefined;
|
|
39
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Object schemas this run generates for. `referenceOnly` entries are left
|
|
42
|
+
* out: they are in the export solely so relation targets resolve (#187),
|
|
43
|
+
* and every generation loop reaches the set through this method.
|
|
44
|
+
* `getSchema()` deliberately still returns them.
|
|
45
|
+
*/
|
|
40
46
|
getObjectSchemas() {
|
|
41
47
|
const result = {};
|
|
42
48
|
for (const [name, schema] of Object.entries(this.getSchemas())) {
|
|
43
49
|
const kind = schema.kind ?? 'object';
|
|
44
|
-
if (kind !== 'enum' && kind !== 'partial') {
|
|
50
|
+
if (kind !== 'enum' && kind !== 'partial' && !schema.referenceOnly) {
|
|
45
51
|
result[name] = schema;
|
|
46
52
|
}
|
|
47
53
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -248,6 +248,14 @@ export interface SchemaDefinition {
|
|
|
248
248
|
* run never saw them (#164).
|
|
249
249
|
*/
|
|
250
250
|
readonly extendedProperties?: readonly string[];
|
|
251
|
+
/**
|
|
252
|
+
* Set by the Go side for a PACKAGE schema that this codegen target's
|
|
253
|
+
* include/exclude filter keeps out of the output but that relations still
|
|
254
|
+
* have to resolve against (#187). Generation loops skip it; `getSchema()`
|
|
255
|
+
* still returns it so a cross-package reference names the package's class
|
|
256
|
+
* instead of silently degrading.
|
|
257
|
+
*/
|
|
258
|
+
readonly referenceOnly?: boolean;
|
|
251
259
|
readonly values?: readonly EnumValueDefinition[];
|
|
252
260
|
readonly pivotFor?: readonly string[];
|
|
253
261
|
readonly policies?: readonly PolicyDefinition[];
|