metal-orm 1.1.5 → 1.1.7

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
@@ -114,6 +114,8 @@ __export(index_exports, {
114
114
  acos: () => acos,
115
115
  add: () => add,
116
116
  addDomainEvent: () => addDomainEvent,
117
+ addEntityRelation: () => addEntityRelation,
118
+ addRelation: () => addRelation,
117
119
  age: () => age,
118
120
  aliasRef: () => aliasRef,
119
121
  and: () => and,
@@ -461,6 +463,9 @@ var defineTable = (name, columns, relations = {}, hooks, options = {}) => {
461
463
  function setRelations(table, relations) {
462
464
  table.relations = relations;
463
465
  }
466
+ function addRelation(table, name, relation) {
467
+ table.relations[name] = relation;
468
+ }
464
469
  var TABLE_REF_CACHE = /* @__PURE__ */ new WeakMap();
465
470
  var withColumnProps = (table) => {
466
471
  const cached = TABLE_REF_CACHE.get(table);
@@ -9194,6 +9199,69 @@ var bootstrapEntities = () => {
9194
9199
  }
9195
9200
  return metas.map((meta) => meta.table);
9196
9201
  };
9202
+ var resolveSingleRelation = (relationName, relation, rootMeta) => {
9203
+ const tableMap = /* @__PURE__ */ new Map();
9204
+ for (const m of getAllEntityMetadata()) {
9205
+ if (m.table) tableMap.set(m.target, m.table);
9206
+ }
9207
+ switch (relation.kind) {
9208
+ case RelationKinds.HasOne: {
9209
+ const foreignKey = relation.foreignKey ?? `${getPivotKeyBaseFromRoot(rootMeta)}_id`;
9210
+ return hasOne(
9211
+ resolveTableTarget(relation.target, tableMap),
9212
+ foreignKey,
9213
+ relation.localKey,
9214
+ relation.cascade
9215
+ );
9216
+ }
9217
+ case RelationKinds.HasMany: {
9218
+ const foreignKey = relation.foreignKey ?? `${getPivotKeyBaseFromRoot(rootMeta)}_id`;
9219
+ return hasMany(
9220
+ resolveTableTarget(relation.target, tableMap),
9221
+ foreignKey,
9222
+ relation.localKey,
9223
+ relation.cascade
9224
+ );
9225
+ }
9226
+ case RelationKinds.BelongsTo: {
9227
+ return belongsTo(
9228
+ resolveTableTarget(relation.target, tableMap),
9229
+ relation.foreignKey,
9230
+ relation.localKey,
9231
+ relation.cascade
9232
+ );
9233
+ }
9234
+ case RelationKinds.BelongsToMany: {
9235
+ const pivotForeignKeyToRoot = relation.pivotForeignKeyToRoot ?? `${getPivotKeyBaseFromRoot(rootMeta)}_id`;
9236
+ const pivotForeignKeyToTarget = relation.pivotForeignKeyToTarget ?? `${getPivotKeyBaseFromTarget(relation.target)}_id`;
9237
+ return belongsToMany(
9238
+ resolveTableTarget(relation.target, tableMap),
9239
+ resolveTableTarget(relation.pivotTable, tableMap),
9240
+ {
9241
+ pivotForeignKeyToRoot,
9242
+ pivotForeignKeyToTarget,
9243
+ localKey: relation.localKey,
9244
+ targetKey: relation.targetKey,
9245
+ pivotPrimaryKey: relation.pivotPrimaryKey,
9246
+ defaultPivotColumns: relation.defaultPivotColumns,
9247
+ cascade: relation.cascade
9248
+ }
9249
+ );
9250
+ }
9251
+ default:
9252
+ throw new Error(`Unknown relation kind for relation '${relationName}'`);
9253
+ }
9254
+ };
9255
+ var addEntityRelation = (ctor, name, relation) => {
9256
+ const meta = getEntityMetadata(ctor);
9257
+ if (!meta) {
9258
+ throw new Error(`Entity '${ctor.name}' is not registered. Did you decorate it with @Entity?`);
9259
+ }
9260
+ addRelationMetadata(ctor, name, relation);
9261
+ if (meta.table) {
9262
+ meta.table.relations[name] = resolveSingleRelation(name, relation, meta);
9263
+ }
9264
+ };
9197
9265
  var getTableDefFromEntity = (ctor) => {
9198
9266
  const meta = getEntityMetadata(ctor);
9199
9267
  if (!meta) return void 0;
@@ -19815,6 +19883,8 @@ var TagIndex = class {
19815
19883
  acos,
19816
19884
  add,
19817
19885
  addDomainEvent,
19886
+ addEntityRelation,
19887
+ addRelation,
19818
19888
  age,
19819
19889
  aliasRef,
19820
19890
  and,