@worldbrain/storex-sync
Version:
Offline-first syncing between multiple databases using Storex
435 lines • 23.4 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 __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
};
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 _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
var expect = require("expect");
var index_tests_1 = require("@worldbrain/storex-pattern-modules/lib/index.tests");
var storex_1 = require("./shared-sync-log/storex");
var client_sync_log_1 = require("./client-sync-log");
var custom_auto_pk_1 = require("./custom-auto-pk");
var logging_middleware_1 = require("./logging-middleware");
var _1 = require(".");
var reconciliation_1 = require("./reconciliation");
describe('Storex sync integration tests', function () {
function setupBackend(options) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2, index_tests_1.setupStorexTest({
dbName: 'backend',
collections: {},
modules: {
sharedSyncLog: function (_a) {
var storageManager = _a.storageManager;
return new storex_1.SharedSyncLogStorage({ storageManager: storageManager });
}
}
})];
});
});
}
function createGetNow(options) {
var now = options.start;
return function () { return now++; };
}
function setupClient(options) {
return __awaiter(this, void 0, void 0, function () {
var _a, storageManager, modules, includeCollections, pkMiddleware, syncLoggingMiddleware;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4, index_tests_1.setupStorexTest({
dbName: "client-" + options.clientName,
collections: {
user: {
version: new Date('2019-01-01'),
fields: {
displayName: { type: 'string' }
}
},
email: {
version: new Date('2019-01-01'),
fields: {
address: { type: 'string' },
},
relationships: [
{ childOf: 'user' }
]
}
},
modules: {
clientSyncLog: function (_a) {
var storageManager = _a.storageManager;
return new client_sync_log_1.ClientSyncLogStorage({ storageManager: storageManager });
}
}
})];
case 1:
_a = _b.sent(), storageManager = _a.storageManager, modules = _a.modules;
includeCollections = ['user', 'email'];
pkMiddleware = new custom_auto_pk_1.CustomAutoPkMiddleware({ pkGenerator: options.pkGenerator });
pkMiddleware.setup({ storageRegistry: storageManager.registry, collections: includeCollections });
syncLoggingMiddleware = new logging_middleware_1.SyncLoggingMiddleware({ storageManager: storageManager, clientSyncLog: modules.clientSyncLog, includeCollections: includeCollections });
syncLoggingMiddleware._getNow = options.getNow;
storageManager.setMiddleware([
pkMiddleware,
syncLoggingMiddleware
]);
return [2, { storageManager: storageManager, modules: modules, deviceId: null, objects: {} }];
}
});
});
}
function setupTest(options) {
return __awaiter(this, void 0, void 0, function () {
var e_1, _a, idsGenerated, pkGenerator, userId, backend, clients, _b, _c, name_1, _d, _e, _f, e_1_1;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
idsGenerated = 0;
pkGenerator = function () { return "id-" + ++idsGenerated; };
userId = 1;
return [4, setupBackend({})];
case 1:
backend = _g.sent();
clients = {};
_g.label = 2;
case 2:
_g.trys.push([2, 8, 9, 10]);
_b = __values(options.clients || []), _c = _b.next();
_g.label = 3;
case 3:
if (!!_c.done) return [3, 7];
name_1 = _c.value.name;
_d = clients;
_e = name_1;
return [4, setupClient({ backend: backend, clientName: name_1, getNow: options.getNow, pkGenerator: pkGenerator })];
case 4:
_d[_e] = _g.sent();
_f = clients[name_1];
return [4, backend.modules.sharedSyncLog.createDeviceId({ userId: userId, sharedUntil: 10 })];
case 5:
_f.deviceId = _g.sent();
_g.label = 6;
case 6:
_c = _b.next();
return [3, 3];
case 7: return [3, 10];
case 8:
e_1_1 = _g.sent();
e_1 = { error: e_1_1 };
return [3, 10];
case 9:
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
return [7];
case 10: return [2, { backend: backend, clients: clients, userId: userId }];
}
});
});
}
describe('shareLogEntries()', function () {
function setupShareTest() {
return __awaiter(this, void 0, void 0, function () {
var _a, backend, clients, userId, share;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4, setupTest({ clients: [{ name: 'one' }, { name: 'two' }], getNow: createGetNow({ start: 2 }) })];
case 1:
_a = _b.sent(), backend = _a.backend, clients = _a.clients, userId = _a.userId;
return [4, clients.one.storageManager.collection('user').createObject({ displayName: 'Joe', emails: [{ address: 'joe@doe.com' }] })];
case 2:
_b.sent();
share = function (options) { return _1.shareLogEntries({
sharedSyncLog: backend.modules.sharedSyncLog, clientSyncLog: clients.one.modules.clientSyncLog,
userId: userId, deviceId: clients.one.deviceId, now: options.now
}); };
return [2, { backend: backend, clients: clients, userId: userId, share: share }];
}
});
});
}
it('should correctly share log entries', function () { return __awaiter(_this, void 0, void 0, function () {
var _a, backend, clients, userId, share, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4, setupShareTest()];
case 1:
_a = _c.sent(), backend = _a.backend, clients = _a.clients, userId = _a.userId, share = _a.share;
return [4, share({ now: 55 })];
case 2:
_c.sent();
_b = expect;
return [4, backend.modules.sharedSyncLog.getUnsyncedEntries({ deviceId: clients.two.deviceId })];
case 3:
_b.apply(void 0, [_c.sent()]).toEqual([
{
id: expect.anything(),
userId: userId,
deviceId: clients.one.deviceId,
createdOn: 2,
sharedOn: 55,
data: '{"operation":"create","collection":"user","pk":"id-1","field":null,"value":{"displayName":"Joe"}}',
},
{
id: expect.anything(),
userId: userId,
deviceId: clients.one.deviceId,
createdOn: 3,
sharedOn: 55,
data: '{"operation":"create","collection":"email","pk":"id-2","field":null,"value":{"address":"joe@doe.com"}}',
},
]);
return [2];
}
});
}); });
it('should not reshare entries that are already shared', function () { return __awaiter(_this, void 0, void 0, function () {
var _a, backend, clients, share, entries, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4, setupShareTest()];
case 1:
_a = _c.sent(), backend = _a.backend, clients = _a.clients, share = _a.share;
return [4, share({ now: 55 })];
case 2:
_c.sent();
return [4, backend.modules.sharedSyncLog.getUnsyncedEntries({ deviceId: clients.two.deviceId })];
case 3:
entries = _c.sent();
return [4, share({ now: 60 })];
case 4:
_c.sent();
_b = expect;
return [4, backend.modules.sharedSyncLog.getUnsyncedEntries({ deviceId: clients.two.deviceId })];
case 5:
_b.apply(void 0, [_c.sent()]).toEqual(entries);
return [2];
}
});
}); });
});
describe('receiveLogEntries()', function () {
function setupReceiveTest() {
return __awaiter(this, void 0, void 0, function () {
var _a, backend, clients, userId, receive;
var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4, setupTest({ clients: [{ name: 'one' }, { name: 'two' }], getNow: createGetNow({ start: 2 }) })];
case 1:
_a = _b.sent(), backend = _a.backend, clients = _a.clients, userId = _a.userId;
receive = function (options) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, _1.receiveLogEntries({
clientSyncLog: clients.one.modules.clientSyncLog,
sharedSyncLog: backend.modules.sharedSyncLog,
deviceId: clients.one.deviceId,
now: options.now,
})];
case 1:
_a.sent();
return [2];
}
});
}); };
return [2, { backend: backend, clients: clients, userId: userId, receive: receive }];
}
});
});
}
it('should correctly receive unsynced entries and write them to the local log marked as needing integration', function () { return __awaiter(_this, void 0, void 0, function () {
var _a, backend, clients, userId, receive, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4, setupReceiveTest()];
case 1:
_a = _c.sent(), backend = _a.backend, clients = _a.clients, userId = _a.userId, receive = _a.receive;
return [4, clients.one.storageManager.collection('user').createObject({ displayName: 'Bob' })];
case 2:
_c.sent();
return [4, backend.modules.sharedSyncLog.writeEntries([
{
userId: userId,
deviceId: clients.one.deviceId,
createdOn: 5,
sharedOn: 55,
data: '{"operation":"create","collection":"user","pk":"id-2","field":null,"value":{"displayName":"Joe"}}',
},
{
userId: userId,
deviceId: clients.one.deviceId,
createdOn: 7,
sharedOn: 55,
data: '{"operation":"create","collection":"email","pk":"id-3","field":null,"value":{"address":"joe@doe.com"}}',
},
], { userId: userId, deviceId: clients.one.deviceId })];
case 3:
_c.sent();
return [4, receive({ now: 60 })];
case 4:
_c.sent();
_b = expect;
return [4, clients.one.modules.clientSyncLog.getEntriesCreatedAfter(1)];
case 5:
_b.apply(void 0, [_c.sent()]).toEqual([
{
id: expect.anything(),
createdOn: 2,
sharedOn: null,
needsIntegration: false,
collection: 'user',
pk: 'id-1',
operation: 'create',
value: { displayName: 'Bob' },
},
{
id: expect.anything(),
createdOn: 5,
sharedOn: 60,
needsIntegration: true,
collection: 'user',
pk: 'id-2',
field: null,
operation: 'create',
value: { displayName: 'Joe' },
},
{
id: expect.anything(),
createdOn: 7,
sharedOn: 60,
needsIntegration: true,
collection: 'email',
pk: 'id-3',
field: null,
operation: 'create',
value: { address: 'joe@doe.com' },
}
]);
return [2];
}
});
}); });
});
describe('doSync()', function () {
function setupSyncTest() {
return __awaiter(this, void 0, void 0, function () {
var _a, backend, clients, userId, sync;
var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4, setupTest({ clients: [{ name: 'one' }, { name: 'two' }], getNow: createGetNow({ start: 50 }) })];
case 1:
_a = _b.sent(), backend = _a.backend, clients = _a.clients, userId = _a.userId;
sync = function (options) { return __awaiter(_this, void 0, void 0, function () {
var client;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
client = clients[options.clientName];
return [4, _1.doSync({
clientSyncLog: client.modules.clientSyncLog,
sharedSyncLog: backend.modules.sharedSyncLog,
storageManager: client.storageManager,
reconciler: reconciliation_1.reconcileSyncLog,
now: options.now,
userId: userId,
deviceId: client.deviceId
})];
case 1:
_a.sent();
return [2];
}
});
}); };
return [2, { clients: clients, sync: sync }];
}
});
});
}
it('should work when pulling changes after being offline', function () { return __awaiter(_this, void 0, void 0, function () {
var _a, clients, sync, orig, emails, user, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0: return [4, setupSyncTest()];
case 1:
_a = _d.sent(), clients = _a.clients, sync = _a.sync;
return [4, clients.one.storageManager.collection('user').createObject({ displayName: 'Joe', emails: [{ address: 'joe@doe.com' }] })];
case 2:
orig = (_d.sent()).object;
emails = orig.emails, user = __rest(orig, ["emails"]);
return [4, sync({ clientName: 'one', now: 55 })];
case 3:
_d.sent();
return [4, sync({ clientName: 'two', now: 60 })];
case 4:
_d.sent();
_b = expect;
return [4, clients.two.storageManager.collection('user').findObject({ id: user.id })];
case 5:
_b.apply(void 0, [_d.sent()]).toEqual(user);
_c = expect;
return [4, clients.two.storageManager.collection('email').findObject({ id: emails[0].id })];
case 6:
_c.apply(void 0, [_d.sent()]).toEqual(emails[0]);
return [2];
}
});
}); });
});
});
//# sourceMappingURL=index.test.js.map