@rebasepro/common 0.1.0 → 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/dist/index.umd.js CHANGED
@@ -1,6 +1,6 @@
1
1
  (function(global, factory) {
2
- typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("@rebasepro/types"), require("@rebasepro/utils"), require("json-logic-js"), require("fast-equals"), require("lodash/cloneDeep.js")) : typeof define === "function" && define.amd ? define(["exports", "@rebasepro/types", "@rebasepro/utils", "json-logic-js", "fast-equals", "lodash/cloneDeep.js"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["Rebase Util"] = {}, global.types, global.utils, global.jsonLogic, global.fastEquals, global.cloneDeep));
3
- })(this, function(exports2, types, utils, jsonLogic, fastEquals, cloneDeep) {
2
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("@rebasepro/types"), require("@rebasepro/utils"), require("json-logic-js"), require("fast-equals")) : typeof define === "function" && define.amd ? define(["exports", "@rebasepro/types", "@rebasepro/utils", "json-logic-js", "fast-equals"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["Rebase Util"] = {}, global.types, global.utils, global.jsonLogic, global.fastEquals));
3
+ })(this, function(exports2, types, utils, jsonLogic, fastEquals) {
4
4
  "use strict";
5
5
  const DEFAULT_ONE_OF_TYPE = "type";
6
6
  const DEFAULT_ONE_OF_VALUE = "value";
@@ -59,6 +59,10 @@
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 @@
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 = utils.toSnakeCase(targetCollection.slug);
1183
1229
  }
@@ -1230,6 +1276,17 @@
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,11 +1654,17 @@
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
  }));
1603
1666
  normalizedCollections.forEach((c, index) => {
1604
- const raw = cloneDeep(collections[index]);
1667
+ const raw = utils.deepClone(collections[index]);
1605
1668
  this.rootCollections.push(c);
1606
1669
  this.rawRootCollections.push(raw);
1607
1670
  const normalized = this.normalizeCollection(c);
@@ -1621,7 +1684,7 @@
1621
1684
  if (!subCollection) return;
1622
1685
  this._registerRecursively(this.normalizeCollection({
1623
1686
  ...subCollection
1624
- }), cloneDeep(subCollection));
1687
+ }), utils.deepClone(subCollection));
1625
1688
  });
1626
1689
  }
1627
1690
  });
@@ -1629,7 +1692,7 @@
1629
1692
  return true;
1630
1693
  }
1631
1694
  register(collection, rawCollection) {
1632
- const raw = rawCollection ? cloneDeep(rawCollection) : cloneDeep(collection);
1695
+ const raw = rawCollection ? utils.deepClone(rawCollection) : utils.deepClone(collection);
1633
1696
  this.rootCollections.push(collection);
1634
1697
  this.rawRootCollections.push(raw);
1635
1698
  this._registerRecursively(collection, raw);
@@ -1653,7 +1716,7 @@
1653
1716
  if (!subCollection) return;
1654
1717
  this._registerRecursively(this.normalizeCollection({
1655
1718
  ...subCollection
1656
- }), cloneDeep(subCollection));
1719
+ }), utils.deepClone(subCollection));
1657
1720
  });
1658
1721
  }
1659
1722
  }
@@ -1675,7 +1738,7 @@
1675
1738
  if (types.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
  }