UNPKG

@worldbrain/storex-sync

Version:

Offline-first syncing between multiple databases using Storex

239 lines 10.6 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 }; } }; }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("../utils"); function _throwModificationBeforeCreation(logEntry) { throw new Error("Detected modification to collection '" + logEntry.collection + "', " + ("pk '" + JSON.stringify(logEntry.pk) + "' before it was created (likely pk collision)")); } exports.reconcileSyncLog = function (logEntries, options) { var e_1, _a, e_2, _b, e_3, _c; var modificationsByObject = {}; try { for (var logEntries_1 = __values(logEntries), logEntries_1_1 = logEntries_1.next(); !logEntries_1_1.done; logEntries_1_1 = logEntries_1.next()) { var logEntry = logEntries_1_1.value; var collectionModifications = modificationsByObject[logEntry.collection] = modificationsByObject[logEntry.collection] || {}; var pkAsJson = JSON.stringify(logEntry.pk); var objectModifications = collectionModifications[pkAsJson]; if (logEntry.operation === 'modify') { _processModificationEntry({ objectModifications: objectModifications, logEntry: logEntry, collectionModifications: collectionModifications, pkAsJson: pkAsJson }); } else if (logEntry.operation === 'delete') { _processDeletionEntry({ objectModifications: objectModifications, logEntry: logEntry, collectionModifications: collectionModifications, pkAsJson: pkAsJson }); } else if (logEntry.operation === 'create') { _processCreationEntry({ objectModifications: objectModifications, logEntry: logEntry, collectionModifications: collectionModifications, pkAsJson: pkAsJson }); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (logEntries_1_1 && !logEntries_1_1.done && (_a = logEntries_1.return)) _a.call(logEntries_1); } finally { if (e_1) throw e_1.error; } } var operations = []; try { for (var _d = __values(Object.entries(modificationsByObject)), _e = _d.next(); !_e.done; _e = _d.next()) { var _f = __read(_e.value, 2), collection = _f[0], collectionModifications = _f[1]; try { for (var _g = __values(Object.entries(collectionModifications)), _h = _g.next(); !_h.done; _h = _g.next()) { var _j = __read(_h.value, 2), pkAsJson = _j[0], objectModifications = _j[1]; var pk = JSON.parse(pkAsJson); operations.push.apply(operations, __spread((_processModifications({ objectModifications: objectModifications, collection: collection, pk: pk, storageRegistry: options.storageRegistry }) || []))); } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (_h && !_h.done && (_c = _g.return)) _c.call(_g); } finally { if (e_3) throw e_3.error; } } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_e && !_e.done && (_b = _d.return)) _b.call(_d); } finally { if (e_2) throw e_2.error; } } return operations; }; function _processCreationEntry(_a) { var objectModifications = _a.objectModifications, logEntry = _a.logEntry, collectionModifications = _a.collectionModifications, pkAsJson = _a.pkAsJson; var e_4, _b, e_5, _c; if (!objectModifications) { var fields = {}; try { for (var _d = __values(Object.entries(logEntry.value)), _e = _d.next(); !_e.done; _e = _d.next()) { var _f = __read(_e.value, 2), key = _f[0], value = _f[1]; fields[key] = { value: value, createdOn: logEntry.createdOn, syncedOn: logEntry.sharedOn }; } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (_e && !_e.done && (_b = _d.return)) _b.call(_d); } finally { if (e_4) throw e_4.error; } } collectionModifications[pkAsJson] = { shouldBeCreated: true, createdOn: logEntry.createdOn, isDeleted: false, shouldBeDeleted: false, fields: fields }; } else { if (objectModifications.shouldBeCreated) { throw new Error("Detected double create in collection '" + logEntry.collection + "', pk '" + JSON.stringify(logEntry.pk) + "'"); } var fields = objectModifications.fields; try { for (var _g = __values(Object.entries(logEntry.value)), _h = _g.next(); !_h.done; _h = _g.next()) { var _j = __read(_h.value, 2), key = _j[0], value = _j[1]; if (!fields[key]) { fields[key] = { value: value, createdOn: logEntry.createdOn, syncedOn: logEntry.sharedOn }; } else if (logEntry.createdOn > fields[key].createdOn) { _throwModificationBeforeCreation(logEntry); } } } catch (e_5_1) { e_5 = { error: e_5_1 }; } finally { try { if (_h && !_h.done && (_c = _g.return)) _c.call(_g); } finally { if (e_5) throw e_5.error; } } objectModifications.shouldBeCreated = true; objectModifications.createdOn = logEntry.createdOn; } } exports._processCreationEntry = _processCreationEntry; function _processDeletionEntry(_a) { var objectModifications = _a.objectModifications, logEntry = _a.logEntry, collectionModifications = _a.collectionModifications, pkAsJson = _a.pkAsJson; var updates = { isDeleted: !!logEntry.sharedOn, shouldBeDeleted: true, fields: {} }; if (!objectModifications) { collectionModifications[pkAsJson] = __assign({ shouldBeCreated: false }, updates); } else (Object.assign(objectModifications, updates)); } exports._processDeletionEntry = _processDeletionEntry; function _processModificationEntry(_a) { var objectModifications = _a.objectModifications, logEntry = _a.logEntry, collectionModifications = _a.collectionModifications, pkAsJson = _a.pkAsJson; var _b; var updates = { createdOn: logEntry.createdOn, syncedOn: logEntry.sharedOn, value: logEntry.value }; if (!objectModifications) { collectionModifications[pkAsJson] = { shouldBeCreated: false, isDeleted: !!logEntry.sharedOn, shouldBeDeleted: false, fields: (_b = {}, _b[logEntry.field] = updates, _b) }; return; } if (objectModifications.shouldBeCreated && objectModifications.createdOn > logEntry.createdOn) { _throwModificationBeforeCreation(logEntry); } var fieldModifications = objectModifications.fields[logEntry.field]; if (!fieldModifications) { objectModifications[logEntry.field] = updates; } else if (logEntry.createdOn > fieldModifications.createdOn) { Object.assign(fieldModifications, updates); } } exports._processModificationEntry = _processModificationEntry; function _processModifications(_a) { var objectModifications = _a.objectModifications, collection = _a.collection, pk = _a.pk, storageRegistry = _a.storageRegistry; var e_6, _b, e_7, _c, _d; var pkFields = utils_1.setObjectPk({}, pk, collection, storageRegistry); if (objectModifications.shouldBeDeleted) { if (!objectModifications.isDeleted && !objectModifications.shouldBeCreated) { return [{ operation: 'deleteOneObject', collection: collection, args: [pkFields] }]; } } else if (objectModifications.shouldBeCreated) { var object = {}; try { for (var _e = __values(Object.entries(objectModifications.fields)), _f = _e.next(); !_f.done; _f = _e.next()) { var _g = __read(_f.value, 2), key = _g[0], fieldModification = _g[1]; object[key] = fieldModification.value; } } catch (e_6_1) { e_6 = { error: e_6_1 }; } finally { try { if (_f && !_f.done && (_b = _e.return)) _b.call(_e); } finally { if (e_6) throw e_6.error; } } return [{ operation: 'createObject', collection: collection, args: __assign({}, pkFields, object) }]; } else { var operations = []; try { for (var _h = __values(Object.entries(objectModifications.fields)), _j = _h.next(); !_j.done; _j = _h.next()) { var _k = __read(_j.value, 2), fieldName = _k[0], fieldModification = _k[1]; if (!fieldModification.syncedOn) { operations.push({ operation: 'updateOneObject', collection: collection, args: [pkFields, (_d = {}, _d[fieldName] = fieldModification.value, _d)] }); } } } catch (e_7_1) { e_7 = { error: e_7_1 }; } finally { try { if (_j && !_j.done && (_c = _h.return)) _c.call(_h); } finally { if (e_7) throw e_7.error; } } return operations; } } exports._processModifications = _processModifications; //# sourceMappingURL=default.js.map