@omnifyjp/omnify 3.2.2 → 3.2.4
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@omnifyjp/omnify",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.4",
|
|
4
4
|
"description": "Schema-driven code generation for Laravel, TypeScript, and SQL",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -36,10 +36,10 @@
|
|
|
36
36
|
"zod": "^3.24.0"
|
|
37
37
|
},
|
|
38
38
|
"optionalDependencies": {
|
|
39
|
-
"@omnifyjp/omnify-darwin-arm64": "3.2.
|
|
40
|
-
"@omnifyjp/omnify-darwin-x64": "3.2.
|
|
41
|
-
"@omnifyjp/omnify-linux-x64": "3.2.
|
|
42
|
-
"@omnifyjp/omnify-linux-arm64": "3.2.
|
|
43
|
-
"@omnifyjp/omnify-win32-x64": "3.2.
|
|
39
|
+
"@omnifyjp/omnify-darwin-arm64": "3.2.4",
|
|
40
|
+
"@omnifyjp/omnify-darwin-x64": "3.2.4",
|
|
41
|
+
"@omnifyjp/omnify-linux-x64": "3.2.4",
|
|
42
|
+
"@omnifyjp/omnify-linux-arm64": "3.2.4",
|
|
43
|
+
"@omnifyjp/omnify-win32-x64": "3.2.4"
|
|
44
44
|
}
|
|
45
45
|
}
|
|
@@ -74,6 +74,7 @@ function generateBaseModel(name, schema, reader, config) {
|
|
|
74
74
|
const relations = buildRelations(name, properties, propertyOrder, modelNamespace, reader);
|
|
75
75
|
const accessors = buildAccessors(expandedProperties);
|
|
76
76
|
const fileAccessors = hasFiles ? buildFileAccessors(properties, propertyOrder, modelNamespace) : '';
|
|
77
|
+
const auditSection = buildAuditSection(options, reader, modelNamespace);
|
|
77
78
|
const nestedSetMethod = buildNestedSetParentIdMethod(nestedSetParentColumn);
|
|
78
79
|
let keyTypeSection = '';
|
|
79
80
|
if (idType === 'Uuid' || idType === 'Ulid' || idType === 'String') {
|
|
@@ -171,7 +172,7 @@ ${appends} ];
|
|
|
171
172
|
return [
|
|
172
173
|
${casts} ];
|
|
173
174
|
}
|
|
174
|
-
${relations}${accessors}${fileAccessors}${nestedSetMethod}
|
|
175
|
+
${relations}${accessors}${fileAccessors}${auditSection}${nestedSetMethod}
|
|
175
176
|
}
|
|
176
177
|
`;
|
|
177
178
|
return baseFile(resolveModularBasePath(config, name, 'Models', `${modelName}BaseModel.php`, config.models.basePath), content);
|
|
@@ -568,6 +569,89 @@ function buildNestedSetParentIdMethod(parentColumn) {
|
|
|
568
569
|
}
|
|
569
570
|
`;
|
|
570
571
|
}
|
|
572
|
+
function buildAuditSection(options, reader, modelNamespace) {
|
|
573
|
+
const opts = options;
|
|
574
|
+
const audit = opts?.['audit'];
|
|
575
|
+
if (!audit)
|
|
576
|
+
return '';
|
|
577
|
+
const hasCreatedBy = audit['createdBy'] ?? false;
|
|
578
|
+
const hasUpdatedBy = audit['updatedBy'] ?? false;
|
|
579
|
+
const hasDeletedBy = audit['deletedBy'] ?? false;
|
|
580
|
+
if (!hasCreatedBy && !hasUpdatedBy && !hasDeletedBy)
|
|
581
|
+
return '';
|
|
582
|
+
// Get audit model name from schemas.json auditConfig
|
|
583
|
+
const auditModel = reader.data?.auditConfig?.model ?? 'User';
|
|
584
|
+
const fqcn = `\\${modelNamespace}\\${auditModel}`;
|
|
585
|
+
const parts = [];
|
|
586
|
+
// Model events (booted)
|
|
587
|
+
const events = [];
|
|
588
|
+
if (hasCreatedBy) {
|
|
589
|
+
events.push(` static::creating(function ($model) {
|
|
590
|
+
if (auth()->check() && !$model->created_by_id) {
|
|
591
|
+
$model->created_by_id = auth()->id();
|
|
592
|
+
}
|
|
593
|
+
});`);
|
|
594
|
+
}
|
|
595
|
+
if (hasUpdatedBy) {
|
|
596
|
+
events.push(` static::updating(function ($model) {
|
|
597
|
+
if (auth()->check()) {
|
|
598
|
+
$model->updated_by_id = auth()->id();
|
|
599
|
+
}
|
|
600
|
+
});`);
|
|
601
|
+
}
|
|
602
|
+
if (hasDeletedBy) {
|
|
603
|
+
events.push(` static::deleting(function ($model) {
|
|
604
|
+
if (auth()->check() && method_exists($model, 'isForceDeleting') && !$model->isForceDeleting()) {
|
|
605
|
+
$model->deleted_by_id = auth()->id();
|
|
606
|
+
$model->saveQuietly();
|
|
607
|
+
}
|
|
608
|
+
});`);
|
|
609
|
+
}
|
|
610
|
+
if (events.length > 0) {
|
|
611
|
+
parts.push(`
|
|
612
|
+
/**
|
|
613
|
+
* Boot audit events.
|
|
614
|
+
*/
|
|
615
|
+
protected static function booted(): void
|
|
616
|
+
{
|
|
617
|
+
parent::booted();
|
|
618
|
+
|
|
619
|
+
${events.join('\n\n')}
|
|
620
|
+
}`);
|
|
621
|
+
}
|
|
622
|
+
// Relations
|
|
623
|
+
if (hasCreatedBy) {
|
|
624
|
+
parts.push(`
|
|
625
|
+
/**
|
|
626
|
+
* Get the user who created this record.
|
|
627
|
+
*/
|
|
628
|
+
public function createdBy(): BelongsTo
|
|
629
|
+
{
|
|
630
|
+
return $this->belongsTo(${fqcn}::class, 'created_by_id');
|
|
631
|
+
}`);
|
|
632
|
+
}
|
|
633
|
+
if (hasUpdatedBy) {
|
|
634
|
+
parts.push(`
|
|
635
|
+
/**
|
|
636
|
+
* Get the user who last updated this record.
|
|
637
|
+
*/
|
|
638
|
+
public function updatedBy(): BelongsTo
|
|
639
|
+
{
|
|
640
|
+
return $this->belongsTo(${fqcn}::class, 'updated_by_id');
|
|
641
|
+
}`);
|
|
642
|
+
}
|
|
643
|
+
if (hasDeletedBy) {
|
|
644
|
+
parts.push(`
|
|
645
|
+
/**
|
|
646
|
+
* Get the user who deleted this record.
|
|
647
|
+
*/
|
|
648
|
+
public function deletedBy(): BelongsTo
|
|
649
|
+
{
|
|
650
|
+
return $this->belongsTo(${fqcn}::class, 'deleted_by_id');
|
|
651
|
+
}`);
|
|
652
|
+
}
|
|
653
|
+
return parts.join('\n');
|
|
654
|
+
}
|
|
571
655
|
function buildFileAccessors(properties, propertyOrder, modelNamespace) {
|
|
572
656
|
const methods = [];
|
|
573
657
|
for (const propName of propertyOrder) {
|
|
@@ -40,14 +40,15 @@ function generateBaseService(name, schema, reader, config) {
|
|
|
40
40
|
if (!prop)
|
|
41
41
|
continue;
|
|
42
42
|
const colName = toSnakeCase(propName);
|
|
43
|
+
const translatable = prop.translatable ?? false;
|
|
43
44
|
if (prop.searchable) {
|
|
44
|
-
searchableFields.push({ propName, colName });
|
|
45
|
+
searchableFields.push({ propName, colName, translatable });
|
|
45
46
|
}
|
|
46
47
|
if (prop.filterable) {
|
|
47
|
-
filterableFields.push({ propName, colName, type: prop.type, prop });
|
|
48
|
+
filterableFields.push({ propName, colName, type: prop.type, prop, translatable });
|
|
48
49
|
}
|
|
49
50
|
if (prop.sortable) {
|
|
50
|
-
sortableFields.push(colName);
|
|
51
|
+
sortableFields.push({ colName, translatable });
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
54
|
// Build search block
|
|
@@ -101,7 +102,12 @@ ${searchBlock}${filterBlock}${sortBlock}
|
|
|
101
102
|
}
|
|
102
103
|
|
|
103
104
|
if ($search = $filters['search'] ?? null) {
|
|
104
|
-
$query->where(function ($q) use ($search) {${searchableFields.length > 0 ? `\n${searchableFields.map((f, i) =>
|
|
105
|
+
$query->where(function ($q) use ($search) {${searchableFields.length > 0 ? `\n${searchableFields.map((f, i) => {
|
|
106
|
+
if (f.translatable) {
|
|
107
|
+
return ` $q->${i === 0 ? 'whereTranslationLike' : 'orWhereTranslationLike'}('${f.colName}', "%{$search}%");`;
|
|
108
|
+
}
|
|
109
|
+
return ` $q->${i === 0 ? 'where' : 'orWhere'}('${f.colName}', 'like', "%{$search}%");`;
|
|
110
|
+
}).join('\n')}` : `\n // No searchable fields defined`}
|
|
105
111
|
});
|
|
106
112
|
}
|
|
107
113
|
|
|
@@ -196,6 +202,10 @@ function buildSearchBlock(fields) {
|
|
|
196
202
|
return '';
|
|
197
203
|
const clauses = fields.map((f, i) => {
|
|
198
204
|
const method = i === 0 ? 'where' : 'orWhere';
|
|
205
|
+
if (f.translatable) {
|
|
206
|
+
// Astrotomic scope for translatable fields
|
|
207
|
+
return ` $q->${i === 0 ? 'whereTranslationLike' : 'orWhereTranslationLike'}('${f.colName}', "%{$search}%");`;
|
|
208
|
+
}
|
|
199
209
|
return ` $q->${method}('${f.colName}', 'like', "%{$search}%");`;
|
|
200
210
|
});
|
|
201
211
|
return `
|
|
@@ -214,7 +224,14 @@ function buildFilterBlock(fields) {
|
|
|
214
224
|
lines.push('');
|
|
215
225
|
lines.push(' // Filters');
|
|
216
226
|
for (const f of fields) {
|
|
217
|
-
const { colName, type } = f;
|
|
227
|
+
const { colName, type, translatable } = f;
|
|
228
|
+
// Translatable fields use Astrotomic scopes
|
|
229
|
+
if (translatable) {
|
|
230
|
+
lines.push(` if (isset($filters['${colName}'])) {`);
|
|
231
|
+
lines.push(` $query->whereTranslation('${colName}', $filters['${colName}']);`);
|
|
232
|
+
lines.push(` }`);
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
218
235
|
switch (type) {
|
|
219
236
|
case 'Decimal':
|
|
220
237
|
case 'Int':
|
|
@@ -264,11 +281,22 @@ function buildSortBlock(sortableFields) {
|
|
|
264
281
|
$query->orderBy('created_at', 'desc');
|
|
265
282
|
`;
|
|
266
283
|
}
|
|
267
|
-
const allowedList = sortableFields.map(f => `'${f}'`).join(', ');
|
|
284
|
+
const allowedList = sortableFields.map(f => `'${f.colName}'`).join(', ');
|
|
285
|
+
const translatableSorts = sortableFields.filter(f => f.translatable).map(f => `'${f.colName}'`);
|
|
286
|
+
const translatableCheck = translatableSorts.length > 0
|
|
287
|
+
? `\n $translatableSorts = [${translatableSorts.join(', ')}];`
|
|
288
|
+
: '';
|
|
289
|
+
const orderByLine = translatableSorts.length > 0
|
|
290
|
+
? ` if (in_array($sortField, $translatableSorts ?? [], true)) {
|
|
291
|
+
$query->orderByTranslation($sortField, $direction);
|
|
292
|
+
} else {
|
|
293
|
+
$query->orderBy($sortField, $direction);
|
|
294
|
+
}`
|
|
295
|
+
: ` $query->orderBy($sortField, $direction);`;
|
|
268
296
|
return `
|
|
269
297
|
// Sort
|
|
270
298
|
$sortParam = $filters['sort'] ?? '-created_at';
|
|
271
|
-
$allowedSorts = [${allowedList}]
|
|
299
|
+
$allowedSorts = [${allowedList}];${translatableCheck}
|
|
272
300
|
|
|
273
301
|
foreach (explode(',', $sortParam) as $sortField) {
|
|
274
302
|
$sortField = trim($sortField);
|
|
@@ -280,7 +308,7 @@ function buildSortBlock(sortableFields) {
|
|
|
280
308
|
}
|
|
281
309
|
|
|
282
310
|
if (in_array($sortField, $allowedSorts, true) || $sortField === 'created_at' || $sortField === 'updated_at') {
|
|
283
|
-
|
|
311
|
+
${orderByLine}
|
|
284
312
|
}
|
|
285
313
|
}
|
|
286
314
|
`;
|