@famgia/omnify-laravel 0.0.15 → 0.0.17

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.js CHANGED
@@ -19,7 +19,7 @@ import {
19
19
  schemaToBlueprint,
20
20
  toColumnName,
21
21
  toTableName
22
- } from "./chunk-REDFZUQY.js";
22
+ } from "./chunk-52JAFQBQ.js";
23
23
  export {
24
24
  formatColumnMethod,
25
25
  formatForeignKey,
package/dist/plugin.cjs CHANGED
@@ -238,11 +238,18 @@ function generateForeignKey(propertyName, property, allSchemas) {
238
238
  } else if (targetPkType === "String") {
239
239
  method = "string";
240
240
  }
241
+ const modifiers = [];
242
+ if (assocProp.nullable === true) {
243
+ modifiers.push({ method: "nullable" });
244
+ }
245
+ if (assocProp.default !== void 0 && assocProp.default !== null) {
246
+ modifiers.push({ method: "default", args: [assocProp.default] });
247
+ }
241
248
  const column = {
242
249
  name: columnName,
243
250
  method,
244
251
  args: [columnName],
245
- modifiers: assocProp.relation === "ManyToOne" ? [{ method: "nullable" }] : []
252
+ modifiers
246
253
  };
247
254
  const foreignKey = {
248
255
  columns: [columnName],
@@ -293,36 +300,67 @@ function schemaToBlueprint(schema, allSchemas) {
293
300
  columns.push(generateSoftDeleteColumn());
294
301
  }
295
302
  if (schema.options?.indexes) {
303
+ const propToColName = (propName) => {
304
+ const colName = toColumnName(propName);
305
+ const prop = schema.properties?.[propName];
306
+ if (prop?.type === "Association") {
307
+ const assoc = prop;
308
+ if ((assoc.relation === "ManyToOne" || assoc.relation === "OneToOne") && !assoc.mappedBy) {
309
+ return colName + "_id";
310
+ }
311
+ }
312
+ return colName;
313
+ };
296
314
  for (const index of schema.options.indexes) {
297
315
  if (typeof index === "string") {
298
316
  indexes.push({
299
- columns: [toColumnName(index)],
317
+ columns: [propToColName(index)],
300
318
  unique: false
301
319
  });
302
320
  } else {
303
321
  indexes.push({
304
322
  name: index.name,
305
- columns: index.columns.map(toColumnName),
323
+ columns: index.columns.map(propToColName),
306
324
  unique: index.unique ?? false
307
325
  });
308
326
  }
309
327
  }
310
328
  }
311
329
  if (schema.options?.unique) {
330
+ const propToColName = (propName) => {
331
+ const colName = toColumnName(propName);
332
+ const prop = schema.properties?.[propName];
333
+ if (prop?.type === "Association") {
334
+ const assoc = prop;
335
+ if ((assoc.relation === "ManyToOne" || assoc.relation === "OneToOne") && !assoc.mappedBy) {
336
+ return colName + "_id";
337
+ }
338
+ }
339
+ return colName;
340
+ };
312
341
  const uniqueConstraints = Array.isArray(schema.options.unique[0]) ? schema.options.unique : [schema.options.unique];
313
342
  for (const constraint of uniqueConstraints) {
314
343
  indexes.push({
315
- columns: constraint.map(toColumnName),
344
+ columns: constraint.map(propToColName),
316
345
  unique: true
317
346
  });
318
347
  }
319
348
  }
349
+ const seenIndexes = /* @__PURE__ */ new Set();
350
+ const uniqueIndexes = indexes.filter((idx) => {
351
+ const key = idx.columns.join(",") + (idx.unique ? ":unique" : "");
352
+ if (seenIndexes.has(key)) {
353
+ return false;
354
+ }
355
+ seenIndexes.add(key);
356
+ return true;
357
+ });
320
358
  return {
321
359
  tableName,
322
360
  columns,
323
361
  primaryKey: ["id"],
324
362
  foreignKeys,
325
- indexes
363
+ indexes: uniqueIndexes
326
364
  };
327
365
  }
328
366
  function formatColumnMethod(column) {