@worldbrain/storex-sync
Version:
Offline-first syncing between multiple databases using Storex
252 lines • 13 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
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 __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 });
var utils_1 = require("../utils");
exports.DEFAULT_OPERATION_PROCESSORS = {
createObject: _processCreateObject,
updateObject: _processUpdateObject,
updateObjects: _processUpdateObjects,
executeBatch: _processExecuteBatch,
};
function _processCreateObject(_a) {
var next = _a.next, operation = _a.operation, executeAndLog = _a.executeAndLog, getNow = _a.getNow, includeCollections = _a.includeCollections, storageRegistry = _a.storageRegistry;
return __awaiter(this, void 0, void 0, function () {
var _b, collection, value, result, object;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_b = __read(operation.slice(1), 2), collection = _b[0], value = _b[1];
if (!includeCollections.has(collection)) {
return [2, next.process({ operation: operation })];
}
return [4, executeAndLog({ placeholder: 'object', operation: 'createObject', collection: collection, args: value }, [_logEntryForCreateObject({ collection: collection, value: value, getNow: getNow, storageRegistry: storageRegistry })])];
case 1:
result = _c.sent();
object = result.info.object.object;
return [2, { object: object }];
}
});
});
}
function _logEntryForCreateObject(_a) {
var collection = _a.collection, value = _a.value, getNow = _a.getNow, storageRegistry = _a.storageRegistry;
return {
collection: collection,
createdOn: getNow(),
needsIntegration: false,
sharedOn: null,
operation: 'create',
pk: utils_1.getObjectPk(value, collection, storageRegistry),
value: utils_1.getObjectWithoutPk(value, collection, storageRegistry)
};
}
function _processUpdateObject(_a) {
var next = _a.next, operation = _a.operation, executeAndLog = _a.executeAndLog, getNow = _a.getNow, includeCollections = _a.includeCollections, storageRegistry = _a.storageRegistry;
return __awaiter(this, void 0, void 0, function () {
var e_1, _b, _c, collection, where, updates, pk, logEntries, _d, _e, _f, fieldName, newValue;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
_c = __read(operation.slice(1), 3), collection = _c[0], where = _c[1], updates = _c[2];
if (!includeCollections.has(collection)) {
return [2, next.process({ operation: operation })];
}
pk = utils_1.getObjectPk(where, collection, storageRegistry);
logEntries = [];
try {
for (_d = __values(Object.entries(updates)), _e = _d.next(); !_e.done; _e = _d.next()) {
_f = __read(_e.value, 2), fieldName = _f[0], newValue = _f[1];
logEntries.push({
createdOn: getNow(),
sharedOn: null,
needsIntegration: false,
field: fieldName,
collection: collection,
operation: 'modify',
pk: pk,
value: newValue
});
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_e && !_e.done && (_b = _d.return)) _b.call(_d);
}
finally { if (e_1) throw e_1.error; }
}
return [4, executeAndLog({ placeholder: 'object', operation: 'updateObjects', collection: collection, where: where, updates: updates }, logEntries)];
case 1:
_g.sent();
return [2];
}
});
});
}
function _processUpdateObjects(_a) {
var next = _a.next, operation = _a.operation, executeAndLog = _a.executeAndLog, getNow = _a.getNow, includeCollections = _a.includeCollections, storageRegistry = _a.storageRegistry;
return __awaiter(this, void 0, void 0, function () {
var _b, collection, where, updates, logEntries;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_b = __read(operation.slice(1), 3), collection = _b[0], where = _b[1], updates = _b[2];
if (!includeCollections.has(collection)) {
return [2, next.process({ operation: operation })];
}
return [4, _logEntriesForUpdateObjects({
next: next, collection: collection, where: where, updates: updates, getNow: getNow, storageRegistry: storageRegistry
})];
case 1:
logEntries = _c.sent();
return [4, executeAndLog({ placeholder: 'update', operation: 'updateObjects', collection: collection, where: where, updates: updates }, logEntries)];
case 2:
_c.sent();
return [2];
}
});
});
}
function _logEntriesForUpdateObjects(_a) {
var next = _a.next, collection = _a.collection, where = _a.where, updates = _a.updates, getNow = _a.getNow, storageRegistry = _a.storageRegistry;
return __awaiter(this, void 0, void 0, function () {
var e_2, _b, e_3, _c, affected, logEntries, affected_1, affected_1_1, object, pk, _d, _e, _f, fieldName, newValue;
return __generator(this, function (_g) {
switch (_g.label) {
case 0: return [4, next.process({ operation: ['findObjects', collection, where] })];
case 1:
affected = _g.sent();
logEntries = [];
try {
for (affected_1 = __values(affected), affected_1_1 = affected_1.next(); !affected_1_1.done; affected_1_1 = affected_1.next()) {
object = affected_1_1.value;
pk = utils_1.getObjectPk(object, collection, storageRegistry);
try {
for (_d = __values(Object.entries(updates)), _e = _d.next(); !_e.done; _e = _d.next()) {
_f = __read(_e.value, 2), fieldName = _f[0], newValue = _f[1];
logEntries.push({
createdOn: getNow(),
sharedOn: null,
needsIntegration: false,
field: fieldName,
collection: collection,
operation: 'modify',
pk: pk,
value: newValue
});
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (_e && !_e.done && (_c = _d.return)) _c.call(_d);
}
finally { if (e_3) throw e_3.error; }
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (affected_1_1 && !affected_1_1.done && (_b = affected_1.return)) _b.call(affected_1);
}
finally { if (e_2) throw e_2.error; }
}
return [2, logEntries];
}
});
});
}
function _processExecuteBatch(_a) {
var next = _a.next, operation = _a.operation, executeAndLog = _a.executeAndLog, getNow = _a.getNow, includeCollections = _a.includeCollections, storageRegistry = _a.storageRegistry;
return __awaiter(this, void 0, void 0, function () {
var e_4, _b, batch, logEntries, batch_1, batch_1_1, step;
return __generator(this, function (_c) {
batch = operation[1];
logEntries = [];
try {
for (batch_1 = __values(batch), batch_1_1 = batch_1.next(); !batch_1_1.done; batch_1_1 = batch_1.next()) {
step = batch_1_1.value;
if (!includeCollections.has(step.collection)) {
continue;
}
if (step.operation === 'createObject') {
logEntries.push(_logEntryForCreateObject({ collection: step.collection, value: step.args, getNow: getNow, storageRegistry: storageRegistry }));
}
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (batch_1_1 && !batch_1_1.done && (_b = batch_1.return)) _b.call(batch_1);
}
finally { if (e_4) throw e_4.error; }
}
if (!logEntries) {
return [2, next.process({ operation: operation })];
}
return [2, executeAndLog(batch, logEntries)];
});
});
}
//# sourceMappingURL=operation-processors.js.map