@omnifyjp/ts 6.3.8 → 6.3.9

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 user models for package schemas (extending the package model). */
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 user models for package schemas (extending the package model). */
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: user model only (extends package model), plus a trait
19
- // carrying anything a project-level `kind: extend` added to them (#164).
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 !== config.models.namespace) {
23
- const extended = extendedPropertyNames(schema);
24
- if (extended.length > 0) {
25
- files.push(generateExtendAttributesTrait(name, schema, extended, reader, config));
26
- }
27
- files.push(generatePackageUserModel(name, pkgNs, config, extended.length > 0, schema.kind === 'pivot'));
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 that extends the package's model class. */
1060
- function generatePackageUserModel(name, packageNamespace, config, hasExtendAttributes = false, isPivot = false) {
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 = hasExtendAttributes
1078
- ? `use ${modelNamespace}\\Concerns\\${traitName};\n`
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, hasExtendAttributes
1101
- ? {
1102
- needle: `use ${traitName};`,
1103
- hint: `this project's \`kind: extend\` adds columns to the package-owned `
1104
- + `${modelName} schema, but this model predates the trait that carries `
1105
- + `them, so those columns are still not fillable or cast. Add `
1106
- + `\`use ${config.models.namespace}\\Concerns\\${traitName};\` and `
1107
- + `\`use ${traitName};\` inside the class (#164).`,
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).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnifyjp/ts",
3
- "version": "6.3.8",
3
+ "version": "6.3.9",
4
4
  "description": "TypeScript model type generator from Omnify schemas.json",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",