@omnifyjp/ts 1.1.1 → 1.1.3

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/cli.js CHANGED
@@ -71,7 +71,7 @@ program
71
71
  .option('-c, --config <path>', 'Path to omnify.yaml (default: ./omnify.yaml)')
72
72
  .option('-i, --input <path>', 'Path to schemas.json (overrides config)')
73
73
  .option('-o, --output <path>', 'Output directory for TypeScript (overrides config)')
74
- .option('--force', 'Overwrite user-editable model files', false)
74
+ .option('--force', 'Force regeneration of auto-generated files', false)
75
75
  .action((opts) => {
76
76
  let inputPath;
77
77
  let tsEnabled = true;
@@ -126,7 +126,7 @@ program
126
126
  filePath = join(tsOutput, file.filePath);
127
127
  }
128
128
  mkdirSync(dirname(filePath), { recursive: true });
129
- if (!file.overwrite && existsSync(filePath) && !opts.force) {
129
+ if (!file.overwrite && existsSync(filePath)) {
130
130
  tsSkipped++;
131
131
  continue;
132
132
  }
@@ -157,7 +157,7 @@ program
157
157
  for (const file of phpFiles) {
158
158
  const filePath = resolve(configDir, file.path);
159
159
  mkdirSync(dirname(filePath), { recursive: true });
160
- if (!file.overwrite && existsSync(filePath) && !opts.force) {
160
+ if (!file.overwrite && existsSync(filePath)) {
161
161
  phpSkipped++;
162
162
  continue;
163
163
  }
@@ -42,20 +42,23 @@ function generateBaseModel(name, schema, reader, config) {
42
42
  const hasTimestamps = options.timestamps ?? false;
43
43
  const translatableFields = reader.getTranslatableFields(name);
44
44
  const hasTranslatable = translatableFields.length > 0;
45
- const imports = buildImports(baseNamespace, modelName, hasSoftDelete, isAuthenticatable, hasTranslatable, properties);
45
+ const primaryKey = options['primaryKey'] ?? 'id';
46
+ const rawId = options.id;
47
+ const idType = typeof rawId === 'string' ? rawId : 'BigInt';
48
+ const isCompositeKey = primaryKey.includes(',');
49
+ const needsUuidTrait = !isCompositeKey && idType === 'Uuid';
50
+ const needsUlidTrait = !isCompositeKey && idType === 'Ulid';
51
+ const imports = buildImports(baseNamespace, modelName, hasSoftDelete, isAuthenticatable, hasTranslatable, properties, needsUuidTrait, needsUlidTrait);
46
52
  const docProperties = buildDocProperties(properties, expandedProperties, propertyOrder);
47
53
  const baseClass = isAuthenticatable ? 'Authenticatable' : 'BaseModel';
48
54
  const implementsClause = hasTranslatable ? ' implements TranslatableContract' : '';
49
- const traits = buildTraits(hasSoftDelete, isAuthenticatable, hasTranslatable);
55
+ const traits = buildTraits(hasSoftDelete, isAuthenticatable, hasTranslatable, needsUuidTrait, needsUlidTrait);
50
56
  const fillable = buildFillable(properties, expandedProperties, propertyOrder);
51
57
  const hidden = buildHidden(properties, expandedProperties, propertyOrder);
52
58
  const appends = buildAppends(expandedProperties);
53
59
  const casts = buildCasts(properties, expandedProperties, propertyOrder);
54
60
  const relations = buildRelations(name, properties, propertyOrder, modelNamespace, reader);
55
61
  const accessors = buildAccessors(expandedProperties);
56
- const primaryKey = options['primaryKey'] ?? 'id';
57
- const rawId = options.id;
58
- const idType = typeof rawId === 'string' ? rawId : 'BigInt';
59
62
  let keyTypeSection = '';
60
63
  if (idType === 'Uuid' || idType === 'Ulid' || idType === 'String') {
61
64
  keyTypeSection = `
@@ -161,11 +164,13 @@ function generateUserModel(name, config) {
161
164
  const modelName = toPascalCase(name);
162
165
  const modelNamespace = config.models.namespace;
163
166
  const baseNamespace = config.models.baseNamespace;
167
+ const factoryNamespace = config.factories.namespace;
164
168
  const content = `<?php
165
169
 
166
170
  namespace ${modelNamespace};
167
171
 
168
172
  use ${baseNamespace}\\${modelName}BaseModel;
173
+ use ${factoryNamespace}\\${modelName}Factory;
169
174
  use Illuminate\\Database\\Eloquent\\Factories\\HasFactory;
170
175
 
171
176
  /**
@@ -186,17 +191,31 @@ class ${modelName} extends ${modelName}BaseModel
186
191
  parent::__construct($attributes);
187
192
  }
188
193
 
194
+ /**
195
+ * Create a new factory instance for the model.
196
+ */
197
+ protected static function newFactory(): ${modelName}Factory
198
+ {
199
+ return ${modelName}Factory::new();
200
+ }
201
+
189
202
  // Add your custom methods here
190
203
  }
191
204
  `;
192
205
  return userFile(`${config.models.path}/${modelName}.php`, content);
193
206
  }
194
- function buildImports(baseNamespace, modelName, hasSoftDelete, isAuthenticatable, hasTranslatable, properties) {
207
+ function buildImports(baseNamespace, modelName, hasSoftDelete, isAuthenticatable, hasTranslatable, properties, needsUuidTrait = false, needsUlidTrait = false) {
195
208
  const lines = [];
196
209
  if (isAuthenticatable) {
197
210
  lines.push('use Illuminate\\Foundation\\Auth\\User as Authenticatable;');
198
211
  lines.push('use Illuminate\\Notifications\\Notifiable;');
199
212
  }
213
+ if (needsUuidTrait) {
214
+ lines.push('use Illuminate\\Database\\Eloquent\\Concerns\\HasUuids;');
215
+ }
216
+ if (needsUlidTrait) {
217
+ lines.push('use Illuminate\\Database\\Eloquent\\Concerns\\HasUlids;');
218
+ }
200
219
  // Always include all relation types for simplicity (matching PHP)
201
220
  const allRelationTypes = ['BelongsTo', 'HasMany', 'HasOne', 'BelongsToMany', 'MorphTo', 'MorphOne', 'MorphMany', 'MorphToMany'];
202
221
  for (const type of allRelationTypes) {
@@ -248,9 +267,13 @@ function buildDocProperties(properties, expandedProperties, propertyOrder) {
248
267
  }
249
268
  return lines.length === 0 ? '' : lines.join('\n') + '\n';
250
269
  }
251
- function buildTraits(hasSoftDelete, isAuthenticatable, hasTranslatable) {
270
+ function buildTraits(hasSoftDelete, isAuthenticatable, hasTranslatable, needsUuidTrait = false, needsUlidTrait = false) {
252
271
  const lines = [];
253
272
  lines.push(' use HasLocalizedDisplayName;');
273
+ if (needsUuidTrait)
274
+ lines.push(' use HasUuids;');
275
+ if (needsUlidTrait)
276
+ lines.push(' use HasUlids;');
254
277
  if (isAuthenticatable)
255
278
  lines.push(' use Notifiable;');
256
279
  if (hasSoftDelete)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnifyjp/ts",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "TypeScript model type generator from Omnify schemas.json",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",