@famgia/omnify-core 2.0.3 → 2.0.5

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
@@ -510,6 +510,7 @@ var VALID_SCHEMA_FIELDS = /* @__PURE__ */ new Set([
510
510
  var VALID_SCHEMA_OPTIONS_FIELDS = /* @__PURE__ */ new Set([
511
511
  "id",
512
512
  "idType",
513
+ "primaryKey",
513
514
  "timestamps",
514
515
  "softDelete",
515
516
  "unique",
@@ -610,6 +611,13 @@ function buildSchemaOptions(data) {
610
611
  if (data.idType !== void 0) {
611
612
  options.idType = data.idType;
612
613
  }
614
+ if (data.primaryKey !== void 0) {
615
+ if (typeof data.primaryKey === "string") {
616
+ options.primaryKey = data.primaryKey;
617
+ } else if (Array.isArray(data.primaryKey)) {
618
+ options.primaryKey = data.primaryKey;
619
+ }
620
+ }
613
621
  if (data.authenticatable !== void 0 && typeof data.authenticatable === "boolean") {
614
622
  options.authenticatable = data.authenticatable;
615
623
  }
@@ -2683,7 +2691,7 @@ function validateUnknownOptionsFields(schema, filePath) {
2683
2691
  validationError(
2684
2692
  `Unknown option field '${field}'`,
2685
2693
  buildLocation7(filePath),
2686
- `Valid option fields: id, idType, timestamps, softDelete, unique, indexes, translations, tableName, authenticatable, authenticatableLoginIdField, authenticatablePasswordField, authenticatableGuardName`
2694
+ `Valid option fields: id, idType, primaryKey, timestamps, softDelete, unique, indexes, translations, tableName, authenticatable, authenticatableLoginIdField, authenticatablePasswordField, authenticatableGuardName`
2687
2695
  )
2688
2696
  );
2689
2697
  }
@@ -3269,6 +3277,43 @@ function validateOptions(schema, filePath) {
3269
3277
  );
3270
3278
  }
3271
3279
  }
3280
+ if (options.primaryKey !== void 0) {
3281
+ const primaryKeyColumns = typeof options.primaryKey === "string" ? [options.primaryKey] : options.primaryKey;
3282
+ const validColumns = /* @__PURE__ */ new Set();
3283
+ if (schema.properties) {
3284
+ for (const [propName, propDef] of Object.entries(schema.properties)) {
3285
+ validColumns.add(toSnakeCase(propName));
3286
+ validColumns.add(propName);
3287
+ if (propDef.type === "Association") {
3288
+ const assoc = propDef;
3289
+ if (assoc.relation === "ManyToOne" || assoc.relation === "OneToOne" && !assoc.mappedBy) {
3290
+ const fkColumn = `${toSnakeCase(propName)}_id`;
3291
+ validColumns.add(fkColumn);
3292
+ }
3293
+ }
3294
+ }
3295
+ }
3296
+ for (const column of primaryKeyColumns) {
3297
+ if (!validColumns.has(column) && !validColumns.has(toSnakeCase(column))) {
3298
+ errors.push(
3299
+ validationError(
3300
+ `primaryKey references non-existent column '${column}'`,
3301
+ buildLocation7(filePath),
3302
+ `Available columns: ${[...validColumns].join(", ")}`
3303
+ )
3304
+ );
3305
+ }
3306
+ }
3307
+ if (options.id === true) {
3308
+ errors.push(
3309
+ validationError(
3310
+ `Conflicting options: 'id: true' and 'primaryKey' cannot be used together`,
3311
+ buildLocation7(filePath),
3312
+ `When using 'primaryKey', the auto-generated ID is disabled. Remove 'id: true' or 'primaryKey'.`
3313
+ )
3314
+ );
3315
+ }
3316
+ }
3272
3317
  if (options.unique && schema.properties) {
3273
3318
  const propertyNames = Object.keys(schema.properties);
3274
3319
  const uniqueConstraints = Array.isArray(options.unique[0]) ? options.unique : [options.unique];