@omnifyjp/ts 1.3.0 → 1.3.1
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/php/model-generator.js +69 -8
- package/package.json +1 -1
|
@@ -23,9 +23,12 @@ export function generateModels(reader, config) {
|
|
|
23
23
|
return files;
|
|
24
24
|
}
|
|
25
25
|
function generateForSchema(name, schema, reader, config) {
|
|
26
|
+
const properties = (schema.properties ?? {});
|
|
27
|
+
const options = schema.options ?? {};
|
|
28
|
+
const hasNestedSet = detectNestedSet(options, properties);
|
|
26
29
|
return [
|
|
27
30
|
generateBaseModel(name, schema, reader, config),
|
|
28
|
-
generateUserModel(name, config),
|
|
31
|
+
generateUserModel(name, config, hasNestedSet),
|
|
29
32
|
];
|
|
30
33
|
}
|
|
31
34
|
function generateBaseModel(name, schema, reader, config) {
|
|
@@ -40,7 +43,9 @@ function generateBaseModel(name, schema, reader, config) {
|
|
|
40
43
|
const isAuthenticatable = options['authenticatable'] ?? false;
|
|
41
44
|
const hasSoftDelete = options.softDelete ?? false;
|
|
42
45
|
const hasTimestamps = options.timestamps ?? false;
|
|
43
|
-
|
|
46
|
+
// NodeTrait is added to the user-editable Model, not BaseModel
|
|
47
|
+
const hasNestedSet = false;
|
|
48
|
+
const nestedSetParentColumn = null;
|
|
44
49
|
const translatableFields = reader.getTranslatableFields(name);
|
|
45
50
|
const hasTranslatable = translatableFields.length > 0;
|
|
46
51
|
const primaryKey = options['primaryKey'] ?? 'id';
|
|
@@ -60,6 +65,7 @@ function generateBaseModel(name, schema, reader, config) {
|
|
|
60
65
|
const casts = buildCasts(properties, expandedProperties, propertyOrder);
|
|
61
66
|
const relations = buildRelations(name, properties, propertyOrder, modelNamespace, reader);
|
|
62
67
|
const accessors = buildAccessors(expandedProperties);
|
|
68
|
+
const nestedSetMethod = buildNestedSetParentIdMethod(nestedSetParentColumn);
|
|
63
69
|
let keyTypeSection = '';
|
|
64
70
|
if (idType === 'Uuid' || idType === 'Ulid' || idType === 'String') {
|
|
65
71
|
keyTypeSection = `
|
|
@@ -156,16 +162,29 @@ ${appends} ];
|
|
|
156
162
|
return [
|
|
157
163
|
${casts} ];
|
|
158
164
|
}
|
|
159
|
-
${relations}${accessors}
|
|
165
|
+
${relations}${accessors}${nestedSetMethod}
|
|
160
166
|
}
|
|
161
167
|
`;
|
|
162
168
|
return baseFile(`${config.models.basePath}/${modelName}BaseModel.php`, content);
|
|
163
169
|
}
|
|
164
|
-
function generateUserModel(name, config) {
|
|
170
|
+
function generateUserModel(name, config, hasNestedSet = false) {
|
|
165
171
|
const modelName = toPascalCase(name);
|
|
166
172
|
const modelNamespace = config.models.namespace;
|
|
167
173
|
const baseNamespace = config.models.baseNamespace;
|
|
168
174
|
const factoryNamespace = config.factories.namespace;
|
|
175
|
+
const nestedSetNamespace = config.nestedset.namespace;
|
|
176
|
+
const imports = [
|
|
177
|
+
`use ${baseNamespace}\\${modelName}BaseModel;`,
|
|
178
|
+
`use ${factoryNamespace}\\${modelName}Factory;`,
|
|
179
|
+
`use Illuminate\\Database\\Eloquent\\Factories\\HasFactory;`,
|
|
180
|
+
];
|
|
181
|
+
if (hasNestedSet) {
|
|
182
|
+
imports.push(`use ${nestedSetNamespace}\\NodeTrait;`);
|
|
183
|
+
}
|
|
184
|
+
const traits = [' use HasFactory;'];
|
|
185
|
+
if (hasNestedSet) {
|
|
186
|
+
traits.push(' use NodeTrait;');
|
|
187
|
+
}
|
|
169
188
|
const content = `<?php
|
|
170
189
|
|
|
171
190
|
/**
|
|
@@ -176,16 +195,14 @@ function generateUserModel(name, config) {
|
|
|
176
195
|
|
|
177
196
|
namespace ${modelNamespace};
|
|
178
197
|
|
|
179
|
-
|
|
180
|
-
use ${factoryNamespace}\\${modelName}Factory;
|
|
181
|
-
use Illuminate\\Database\\Eloquent\\Factories\\HasFactory;
|
|
198
|
+
${imports.join('\n')}
|
|
182
199
|
|
|
183
200
|
/**
|
|
184
201
|
* ${modelName} — add project-specific model logic here.
|
|
185
202
|
*/
|
|
186
203
|
class ${modelName} extends ${modelName}BaseModel
|
|
187
204
|
{
|
|
188
|
-
|
|
205
|
+
${traits.join('\n')}
|
|
189
206
|
|
|
190
207
|
/**
|
|
191
208
|
* Create a new factory instance for the model.
|
|
@@ -461,6 +478,50 @@ class ${modelName} extends Package${modelName}
|
|
|
461
478
|
`;
|
|
462
479
|
return userFile(`${config.models.path}/${modelName}.php`, content);
|
|
463
480
|
}
|
|
481
|
+
/**
|
|
482
|
+
* Detect whether a schema uses nested set (tree structure).
|
|
483
|
+
* Checks both the explicit `nestedSet: true` option and the presence of
|
|
484
|
+
* nested set columns (_lft, _rgt, parent_id / _parent_id) in properties.
|
|
485
|
+
*/
|
|
486
|
+
function detectNestedSet(options, properties) {
|
|
487
|
+
if (options['nestedSet'] === true)
|
|
488
|
+
return true;
|
|
489
|
+
const propNames = Object.keys(properties);
|
|
490
|
+
const hasLft = propNames.some(p => p === '_lft' || p === 'lft');
|
|
491
|
+
const hasRgt = propNames.some(p => p === '_rgt' || p === 'rgt');
|
|
492
|
+
const hasParent = propNames.some(p => p === 'parent_id' || p === '_parent_id' || p === 'parentId' || p === 'parent');
|
|
493
|
+
return hasLft && hasRgt && hasParent;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Detect the parent ID column name for nested set models.
|
|
497
|
+
* Returns the column name if it differs from the default 'parent_id', or null.
|
|
498
|
+
*/
|
|
499
|
+
function detectNestedSetParentColumn(properties) {
|
|
500
|
+
const propNames = Object.keys(properties);
|
|
501
|
+
// Check for non-standard parent_id column names
|
|
502
|
+
if (propNames.includes('_parent_id'))
|
|
503
|
+
return '_parent_id';
|
|
504
|
+
if (propNames.includes('parent_id') || propNames.includes('parentId') || propNames.includes('parent'))
|
|
505
|
+
return null;
|
|
506
|
+
return null;
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* Build the getParentIdName() method override for nested set models
|
|
510
|
+
* that use a non-standard parent_id column name.
|
|
511
|
+
*/
|
|
512
|
+
function buildNestedSetParentIdMethod(parentColumn) {
|
|
513
|
+
if (!parentColumn)
|
|
514
|
+
return '';
|
|
515
|
+
return `
|
|
516
|
+
/**
|
|
517
|
+
* Get the column name for the parent ID.
|
|
518
|
+
*/
|
|
519
|
+
public function getParentIdName(): string
|
|
520
|
+
{
|
|
521
|
+
return '${parentColumn}';
|
|
522
|
+
}
|
|
523
|
+
`;
|
|
524
|
+
}
|
|
464
525
|
function fullNameAccessor(prefix, suffix, fields, separator) {
|
|
465
526
|
const methodName = 'get' + toPascalCase(prefix) + suffix + 'Attribute';
|
|
466
527
|
const fieldAccess = fields.map(f => `$this->${f}`).join(', ');
|