UNPKG

@worldbrain/storex-sync

Version:

Offline-first syncing between multiple databases using Storex

114 lines 4.01 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __values = (this && this.__values) || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; }; Object.defineProperty(exports, "__esModule", { value: true }); function getObjectPk(object, collection, registry) { var e_1, _a; var pkIndex = registry.collections[collection].pkIndex; if (typeof pkIndex === 'string') { return object[pkIndex]; } var pk = []; try { for (var pkIndex_1 = __values(pkIndex), pkIndex_1_1 = pkIndex_1.next(); !pkIndex_1_1.done; pkIndex_1_1 = pkIndex_1.next()) { var indexField = pkIndex_1_1.value; if (typeof indexField === 'string') { pk.push(object[indexField]); } else { throw new Error("getObject() called with relationship as pk, which is not supported yet."); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (pkIndex_1_1 && !pkIndex_1_1.done && (_a = pkIndex_1.return)) _a.call(pkIndex_1); } finally { if (e_1) throw e_1.error; } } return pk; } exports.getObjectPk = getObjectPk; function getObjectWithoutPk(object, collection, registry) { var e_2, _a; object = __assign({}, object); var pkIndex = registry.collections[collection].pkIndex; if (typeof pkIndex === 'string') { delete object[pkIndex]; return object; } try { for (var pkIndex_2 = __values(pkIndex), pkIndex_2_1 = pkIndex_2.next(); !pkIndex_2_1.done; pkIndex_2_1 = pkIndex_2.next()) { var indexField = pkIndex_2_1.value; if (typeof indexField === 'string') { delete object[indexField]; } else { throw new Error("getObject() called with relationship as pk, which is not supported yet."); } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (pkIndex_2_1 && !pkIndex_2_1.done && (_a = pkIndex_2.return)) _a.call(pkIndex_2); } finally { if (e_2) throw e_2.error; } } return object; } exports.getObjectWithoutPk = getObjectWithoutPk; function setObjectPk(object, pk, collection, registry) { var e_3, _a; var collectionDefinition = registry.collections[collection]; if (!collectionDefinition) { throw new Error("Could not find collection definition for '" + collection + "'"); } var pkIndex = collectionDefinition.pkIndex; if (typeof pkIndex === 'string') { object[pkIndex] = pk; return object; } var indexFieldIdx = 0; try { for (var pkIndex_3 = __values(pkIndex), pkIndex_3_1 = pkIndex_3.next(); !pkIndex_3_1.done; pkIndex_3_1 = pkIndex_3.next()) { var indexField = pkIndex_3_1.value; if (typeof indexField === 'string') { object[indexField] = pk[indexFieldIdx++]; } else { throw new Error("setObjectPk() called with relationship as pk, which is not supported yet."); } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (pkIndex_3_1 && !pkIndex_3_1.done && (_a = pkIndex_3.return)) _a.call(pkIndex_3); } finally { if (e_3) throw e_3.error; } } return object; } exports.setObjectPk = setObjectPk; //# sourceMappingURL=utils.js.map