@famgia/omnify-laravel 0.0.17 → 0.0.18

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/index.cjs CHANGED
@@ -55,6 +55,7 @@ var TYPE_METHOD_MAP = {
55
55
  LongText: "longText",
56
56
  Date: "date",
57
57
  Time: "time",
58
+ DateTime: "dateTime",
58
59
  Timestamp: "timestamp",
59
60
  Json: "json",
60
61
  Email: "string",
@@ -127,6 +128,10 @@ function propertyToColumnMethod(propertyName, property) {
127
128
  if (baseProp.unsigned && (method === "integer" || method === "bigInteger")) {
128
129
  modifiers.push({ method: "unsigned" });
129
130
  }
131
+ const displayName = property.displayName;
132
+ if (displayName) {
133
+ modifiers.push({ method: "comment", args: [displayName] });
134
+ }
130
135
  return {
131
136
  name: columnName,
132
137
  method,
@@ -261,6 +266,9 @@ function generateForeignKey(propertyName, property, allSchemas) {
261
266
  if (assocProp.default !== void 0 && assocProp.default !== null) {
262
267
  modifiers.push({ method: "default", args: [assocProp.default] });
263
268
  }
269
+ if (assocProp.displayName) {
270
+ modifiers.push({ method: "comment", args: [assocProp.displayName] });
271
+ }
264
272
  const column = {
265
273
  name: columnName,
266
274
  method,
@@ -280,7 +288,65 @@ function generateForeignKey(propertyName, property, allSchemas) {
280
288
  };
281
289
  return { column, foreignKey, index };
282
290
  }
283
- function schemaToBlueprint(schema, allSchemas) {
291
+ function expandCompoundType(propName, property, customTypes) {
292
+ const typeDef = customTypes.get(property.type);
293
+ if (!typeDef || !typeDef.compound || !typeDef.expand) {
294
+ return null;
295
+ }
296
+ const expanded = [];
297
+ const baseProp = property;
298
+ for (const field of typeDef.expand) {
299
+ const suffixSnake = toColumnName(field.suffix);
300
+ const columnName = `${propName}_${suffixSnake}`;
301
+ const expandedProp = {
302
+ type: "String"
303
+ // Default type, will be overridden by sql definition
304
+ };
305
+ if (field.sql) {
306
+ const sqlType = field.sql.sqlType.toUpperCase();
307
+ if (sqlType === "VARCHAR" || sqlType === "CHAR" || sqlType === "STRING") {
308
+ expandedProp.type = "String";
309
+ if (field.sql.length) {
310
+ expandedProp.length = field.sql.length;
311
+ }
312
+ } else if (sqlType === "INT" || sqlType === "INTEGER") {
313
+ expandedProp.type = "Int";
314
+ } else if (sqlType === "BIGINT") {
315
+ expandedProp.type = "BigInt";
316
+ } else if (sqlType === "TEXT") {
317
+ expandedProp.type = "Text";
318
+ } else if (sqlType === "BOOLEAN" || sqlType === "BOOL") {
319
+ expandedProp.type = "Boolean";
320
+ } else if (sqlType === "DECIMAL") {
321
+ expandedProp.type = "Decimal";
322
+ if (field.sql.precision) expandedProp.precision = field.sql.precision;
323
+ if (field.sql.scale) expandedProp.scale = field.sql.scale;
324
+ } else if (sqlType === "DATE") {
325
+ expandedProp.type = "Date";
326
+ } else if (sqlType === "TIMESTAMP" || sqlType === "DATETIME") {
327
+ expandedProp.type = "Timestamp";
328
+ }
329
+ if (field.sql.nullable !== void 0) {
330
+ expandedProp.nullable = field.sql.nullable;
331
+ } else if (baseProp.nullable !== void 0) {
332
+ expandedProp.nullable = baseProp.nullable;
333
+ }
334
+ if (field.sql.default !== void 0) {
335
+ expandedProp.default = field.sql.default;
336
+ }
337
+ }
338
+ if (baseProp.displayName) {
339
+ expandedProp.displayName = `${baseProp.displayName} (${field.suffix})`;
340
+ }
341
+ expanded.push({
342
+ name: columnName,
343
+ property: expandedProp
344
+ });
345
+ }
346
+ return expanded;
347
+ }
348
+ function schemaToBlueprint(schema, allSchemas, options = {}) {
349
+ const { customTypes = /* @__PURE__ */ new Map() } = options;
284
350
  const tableName = toTableName(schema.name);
285
351
  const columns = [];
286
352
  const foreignKeys = [];
@@ -291,6 +357,16 @@ function schemaToBlueprint(schema, allSchemas) {
291
357
  }
292
358
  if (schema.properties) {
293
359
  for (const [propName, property] of Object.entries(schema.properties)) {
360
+ const expandedProps = expandCompoundType(propName, property, customTypes);
361
+ if (expandedProps) {
362
+ for (const { name: expandedName, property: expandedProp } of expandedProps) {
363
+ const columnMethod2 = propertyToColumnMethod(expandedName, expandedProp);
364
+ if (columnMethod2) {
365
+ columns.push(columnMethod2);
366
+ }
367
+ }
368
+ continue;
369
+ }
294
370
  const columnMethod = propertyToColumnMethod(propName, property);
295
371
  if (columnMethod) {
296
372
  columns.push(columnMethod);
@@ -737,7 +813,9 @@ function generateMigrations(schemas, options = {}) {
737
813
  const timestamp = options.timestamp ?? generateTimestamp();
738
814
  const offsetTimestamp = incrementTimestamp(timestamp, timestampOffset);
739
815
  timestampOffset++;
740
- const blueprint = schemaToBlueprint(schema, schemas);
816
+ const blueprint = schemaToBlueprint(schema, schemas, {
817
+ customTypes: options.customTypes
818
+ });
741
819
  const migration = generateCreateMigration(blueprint, {
742
820
  ...options,
743
821
  timestamp: offsetTimestamp
@@ -815,6 +893,7 @@ var TYPE_METHOD_MAP2 = {
815
893
  LongText: "longText",
816
894
  Date: "date",
817
895
  Time: "time",
896
+ DateTime: "dateTime",
818
897
  Timestamp: "timestamp",
819
898
  Json: "json",
820
899
  Email: "string",
@@ -2099,6 +2178,24 @@ function getFactoryPath(factory) {
2099
2178
  }
2100
2179
 
2101
2180
  // src/plugin.ts
2181
+ function getExistingMigrationTables(migrationsDir) {
2182
+ const existingTables = /* @__PURE__ */ new Set();
2183
+ if (!(0, import_node_fs.existsSync)(migrationsDir)) {
2184
+ return existingTables;
2185
+ }
2186
+ try {
2187
+ const files = (0, import_node_fs.readdirSync)(migrationsDir);
2188
+ const createMigrationPattern = /^\d{4}_\d{2}_\d{2}_\d{6}_create_(.+)_table\.php$/;
2189
+ for (const file of files) {
2190
+ const match = file.match(createMigrationPattern);
2191
+ if (match) {
2192
+ existingTables.add(match[1]);
2193
+ }
2194
+ }
2195
+ } catch {
2196
+ }
2197
+ return existingTables;
2198
+ }
2102
2199
  var LARAVEL_CONFIG_SCHEMA = {
2103
2200
  fields: [
2104
2201
  {
@@ -2182,18 +2279,81 @@ function laravelPlugin(options) {
2182
2279
  generate: async (ctx) => {
2183
2280
  const migrationOptions = {
2184
2281
  connection: resolved.connection,
2185
- timestamp: resolved.timestamp
2282
+ timestamp: resolved.timestamp,
2283
+ customTypes: ctx.customTypes
2186
2284
  };
2187
- const migrations = generateMigrations(ctx.schemas, migrationOptions);
2188
- return migrations.map((migration) => ({
2189
- path: getMigrationPath(migration, resolved.migrationsPath),
2190
- content: migration.content,
2191
- type: "migration",
2192
- metadata: {
2193
- tableName: migration.tables[0],
2194
- migrationType: migration.type
2285
+ const outputs = [];
2286
+ const migrationsDir = (0, import_node_path.join)(ctx.cwd, resolved.migrationsPath);
2287
+ const existingTables = getExistingMigrationTables(migrationsDir);
2288
+ if (ctx.changes !== void 0) {
2289
+ if (ctx.changes.length === 0) {
2290
+ return outputs;
2195
2291
  }
2196
- }));
2292
+ const addedSchemaNames = new Set(
2293
+ ctx.changes.filter((c) => c.changeType === "added").map((c) => c.schemaName)
2294
+ );
2295
+ if (addedSchemaNames.size > 0) {
2296
+ const addedSchemas = Object.fromEntries(
2297
+ Object.entries(ctx.schemas).filter(([name]) => addedSchemaNames.has(name))
2298
+ );
2299
+ const createMigrations = generateMigrations(addedSchemas, migrationOptions);
2300
+ for (const migration of createMigrations) {
2301
+ const tableName = migration.tables[0];
2302
+ if (existingTables.has(tableName)) {
2303
+ ctx.logger.debug(`Skipping CREATE for ${tableName} (already exists)`);
2304
+ continue;
2305
+ }
2306
+ outputs.push({
2307
+ path: getMigrationPath(migration, resolved.migrationsPath),
2308
+ content: migration.content,
2309
+ type: "migration",
2310
+ metadata: {
2311
+ tableName,
2312
+ migrationType: "create"
2313
+ }
2314
+ });
2315
+ }
2316
+ }
2317
+ const alterChanges = ctx.changes.filter(
2318
+ (c) => c.changeType === "modified" || c.changeType === "removed"
2319
+ );
2320
+ if (alterChanges.length > 0) {
2321
+ const alterMigrations = generateMigrationsFromChanges(
2322
+ alterChanges,
2323
+ migrationOptions
2324
+ );
2325
+ for (const migration of alterMigrations) {
2326
+ outputs.push({
2327
+ path: getMigrationPath(migration, resolved.migrationsPath),
2328
+ content: migration.content,
2329
+ type: "migration",
2330
+ metadata: {
2331
+ tableName: migration.tables[0],
2332
+ migrationType: migration.type
2333
+ }
2334
+ });
2335
+ }
2336
+ }
2337
+ } else {
2338
+ const migrations = generateMigrations(ctx.schemas, migrationOptions);
2339
+ for (const migration of migrations) {
2340
+ const tableName = migration.tables[0];
2341
+ if (migration.type === "create" && existingTables.has(tableName)) {
2342
+ ctx.logger.debug(`Skipping migration for ${tableName} (already exists)`);
2343
+ continue;
2344
+ }
2345
+ outputs.push({
2346
+ path: getMigrationPath(migration, resolved.migrationsPath),
2347
+ content: migration.content,
2348
+ type: "migration",
2349
+ metadata: {
2350
+ tableName,
2351
+ migrationType: migration.type
2352
+ }
2353
+ });
2354
+ }
2355
+ }
2356
+ return outputs;
2197
2357
  }
2198
2358
  };
2199
2359
  const modelGenerator = {