@rebasepro/common 0.1.2 → 0.2.1

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021 Rebase
3
+ Copyright (c) 2026 Rebase
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.es.js CHANGED
@@ -59,6 +59,10 @@ function getDefaultValueFortype(type) {
59
59
  return [];
60
60
  } else if (type === "map") {
61
61
  return {};
62
+ } else if (type === "vector") {
63
+ return null;
64
+ } else if (type === "binary") {
65
+ return null;
62
66
  } else {
63
67
  return null;
64
68
  }
@@ -1170,14 +1174,56 @@ const buildPropertyCallbacks = (properties) => {
1170
1174
  }
1171
1175
  return Object.keys(propertyCallbacks).length > 0 ? propertyCallbacks : void 0;
1172
1176
  };
1173
- function sanitizeRelation(relation, sourceCollection) {
1177
+ function sanitizeRelation(relation, sourceCollection, resolveCollection) {
1174
1178
  if (!relation.target) {
1175
1179
  throw new Error("Relation is missing a `target` collection.");
1176
1180
  }
1177
- const targetCollection = relation.target();
1181
+ const rawTarget = relation.target;
1182
+ let targetCollection;
1183
+ if (typeof rawTarget === "string") {
1184
+ if (resolveCollection) {
1185
+ targetCollection = resolveCollection(rawTarget);
1186
+ }
1187
+ if (!targetCollection) {
1188
+ targetCollection = {
1189
+ slug: rawTarget,
1190
+ name: rawTarget
1191
+ };
1192
+ }
1193
+ } else if (typeof rawTarget === "function") {
1194
+ const evaluated = rawTarget();
1195
+ if (typeof evaluated === "string") {
1196
+ if (resolveCollection) {
1197
+ targetCollection = resolveCollection(evaluated);
1198
+ }
1199
+ if (!targetCollection) {
1200
+ targetCollection = {
1201
+ slug: evaluated,
1202
+ name: evaluated
1203
+ };
1204
+ }
1205
+ } else {
1206
+ targetCollection = evaluated;
1207
+ }
1208
+ }
1209
+ if (!targetCollection) {
1210
+ throw new Error("Relation is missing a valid `target` collection.");
1211
+ }
1178
1212
  const newRelation = {
1179
1213
  ...relation
1180
1214
  };
1215
+ newRelation.target = () => {
1216
+ if (typeof rawTarget === "string") {
1217
+ return resolveCollection && resolveCollection(rawTarget) || targetCollection;
1218
+ } else if (typeof rawTarget === "function") {
1219
+ const evaluated = rawTarget();
1220
+ if (typeof evaluated === "string") {
1221
+ return resolveCollection && resolveCollection(evaluated) || targetCollection;
1222
+ }
1223
+ return evaluated;
1224
+ }
1225
+ return targetCollection;
1226
+ };
1181
1227
  if (!newRelation.relationName) {
1182
1228
  newRelation.relationName = toSnakeCase(targetCollection.slug);
1183
1229
  }
@@ -1230,6 +1276,17 @@ function sanitizeRelation(relation, sourceCollection) {
1230
1276
  break;
1231
1277
  }
1232
1278
  }
1279
+ if (!isManyToManyInverse && targetCollection.properties) {
1280
+ for (const [propKey, prop] of Object.entries(targetCollection.properties)) {
1281
+ if (prop.type !== "relation") continue;
1282
+ const relProp = prop;
1283
+ const relName = relProp.relationName || propKey;
1284
+ if (relName === newRelation.inverseRelationName && relProp.cardinality === "many" && (relProp.direction === "owning" || !relProp.direction)) {
1285
+ isManyToManyInverse = true;
1286
+ break;
1287
+ }
1288
+ }
1289
+ }
1233
1290
  } catch (e) {
1234
1291
  }
1235
1292
  }
@@ -1597,6 +1654,12 @@ class CollectionRegistry {
1597
1654
  return false;
1598
1655
  }
1599
1656
  this.reset();
1657
+ collections.forEach((c) => {
1658
+ if (c.slug) {
1659
+ this.collectionsBySlug.set(c.slug, c);
1660
+ }
1661
+ this.collectionsByTableName.set(getTableName(c), c);
1662
+ });
1600
1663
  const normalizedCollections = collections.map((c) => this.normalizeCollection({
1601
1664
  ...c
1602
1665
  }));
@@ -1675,7 +1738,7 @@ class CollectionRegistry {
1675
1738
  if (getDataSourceCapabilities(result.driver).supportsRelations) {
1676
1739
  mergedRelations = mergedRelationsRaw.map((r) => {
1677
1740
  try {
1678
- return sanitizeRelation(r, result);
1741
+ return sanitizeRelation(r, result, (slug) => this.get(slug));
1679
1742
  } catch {
1680
1743
  return r;
1681
1744
  }