UNPKG

@worldbrain/storex-sync

Version:

Offline-first syncing between multiple databases using Storex

417 lines 21.4 kB
"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 _this = this; Object.defineProperty(exports, "__esModule", { value: true }); var expect = require("expect"); var storex_1 = require("@worldbrain/storex"); var storex_backend_dexie_1 = require("@worldbrain/storex-backend-dexie"); var in_memory_1 = require("@worldbrain/storex-backend-dexie/lib/in-memory"); var storex_pattern_modules_1 = require("@worldbrain/storex-pattern-modules"); var client_sync_log_1 = require("../client-sync-log"); var _1 = require("."); function setupTest(_a) { var now = _a.now, userFields = _a.userFields; return __awaiter(this, void 0, void 0, function () { var backend, storageManager, clientSyncLog, loggingMiddleware; return __generator(this, function (_b) { switch (_b.label) { case 0: backend = new storex_backend_dexie_1.DexieStorageBackend({ idbImplementation: in_memory_1.default(), dbName: 'unittest' }); storageManager = new storex_1.default({ backend: backend }); storageManager.registry.registerCollections({ user: { version: new Date('2019-02-19'), fields: userFields || { displayName: { type: 'string' } } } }); clientSyncLog = new client_sync_log_1.ClientSyncLogStorage({ storageManager: storageManager }); storex_pattern_modules_1.registerModuleCollections(storageManager.registry, clientSyncLog); return [4, storageManager.finishInitialization()]; case 1: _b.sent(); loggingMiddleware = new _1.SyncLoggingMiddleware({ clientSyncLog: clientSyncLog, storageManager: storageManager, includeCollections: ['user'] }); loggingMiddleware._getNow = now; storageManager.setMiddleware([loggingMiddleware]); return [2, { storageManager: storageManager, clientSyncLog: clientSyncLog }]; } }); }); } describe('Sync logging middleware', function () { it('should write createObject operations to the ClientSyncLog in a batch write', function () { return __awaiter(_this, void 0, void 0, function () { var _a, storageManager, clientSyncLog, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: return [4, setupTest({ now: function () { return 3; } })]; case 1: _a = _c.sent(), storageManager = _a.storageManager, clientSyncLog = _a.clientSyncLog; return [4, storageManager.collection('user').createObject({ id: 53, displayName: 'John Doe' })]; case 2: _c.sent(); _b = expect; return [4, clientSyncLog.getEntriesCreatedAfter(1)]; case 3: _b.apply(void 0, [_c.sent()]).toEqual([ { id: expect.anything(), createdOn: 3, sharedOn: null, needsIntegration: false, collection: 'user', pk: 53, operation: 'create', value: { displayName: 'John Doe' } }, ]); return [2]; } }); }); }); it('should write updateObject operations done by pk on a single field to the ClientSyncLog in a batch write', function () { return __awaiter(_this, void 0, void 0, function () { var now, _a, storageManager, clientSyncLog, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: now = 2; return [4, setupTest({ now: function () { return ++now; } })]; case 1: _a = _c.sent(), storageManager = _a.storageManager, clientSyncLog = _a.clientSyncLog; return [4, storageManager.collection('user').createObject({ id: 53, displayName: 'John Doe' })]; case 2: _c.sent(); return [4, storageManager.collection('user').updateOneObject({ id: 53 }, { displayName: 'Jack Doe' })]; case 3: _c.sent(); _b = expect; return [4, clientSyncLog.getEntriesCreatedAfter(1)]; case 4: _b.apply(void 0, [_c.sent()]).toEqual([ { id: expect.anything(), createdOn: 3, sharedOn: null, needsIntegration: false, collection: 'user', pk: 53, operation: 'create', value: { displayName: 'John Doe' } }, { id: expect.anything(), createdOn: 4, sharedOn: null, needsIntegration: false, collection: 'user', pk: 53, operation: 'modify', field: 'displayName', value: 'Jack Doe', }, ]); return [2]; } }); }); }); it('should write updateObject operations done by pk on multiple fields to the ClientSyncLog in a batch write', function () { return __awaiter(_this, void 0, void 0, function () { var now, _a, storageManager, clientSyncLog, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: now = 2; return [4, setupTest({ now: function () { return ++now; }, userFields: { firstName: { type: 'string' }, lastName: { type: 'string' }, } })]; case 1: _a = _c.sent(), storageManager = _a.storageManager, clientSyncLog = _a.clientSyncLog; return [4, storageManager.collection('user').createObject({ id: 53, firstName: 'John', lastName: 'Doe' })]; case 2: _c.sent(); return [4, storageManager.collection('user').updateOneObject({ id: 53 }, { firstName: 'Jack', lastName: 'Trump' })]; case 3: _c.sent(); _b = expect; return [4, clientSyncLog.getEntriesCreatedAfter(1)]; case 4: _b.apply(void 0, [_c.sent()]).toEqual([ { id: expect.anything(), createdOn: 3, sharedOn: null, needsIntegration: false, collection: 'user', pk: 53, operation: 'create', value: { firstName: 'John', lastName: 'Doe' } }, { id: expect.anything(), createdOn: 4, sharedOn: null, needsIntegration: false, collection: 'user', pk: 53, operation: 'modify', field: 'firstName', value: 'Jack', }, { id: expect.anything(), createdOn: 5, sharedOn: null, needsIntegration: false, collection: 'user', pk: 53, operation: 'modify', field: 'lastName', value: 'Trump', }, ]); return [2]; } }); }); }); it('should write updateObjects operations done on a single field to the ClientSyncLog in a batch write', function () { return __awaiter(_this, void 0, void 0, function () { var now, _a, storageManager, clientSyncLog, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: now = 2; return [4, setupTest({ now: function () { return ++now; }, userFields: { firstName: { type: 'string' }, lastName: { type: 'string' }, } })]; case 1: _a = _c.sent(), storageManager = _a.storageManager, clientSyncLog = _a.clientSyncLog; return [4, storageManager.collection('user').createObject({ id: 53, firstName: 'John', lastName: 'Doe' })]; case 2: _c.sent(); return [4, storageManager.collection('user').createObject({ id: 54, firstName: 'Jane', lastName: 'Doe' })]; case 3: _c.sent(); return [4, storageManager.collection('user').createObject({ id: 55, firstName: 'Jack', lastName: 'Daniels' })]; case 4: _c.sent(); return [4, storageManager.collection('user').updateObjects({ lastName: 'Doe' }, { lastName: 'Trump' })]; case 5: _c.sent(); _b = expect; return [4, clientSyncLog.getEntriesCreatedAfter(1)]; case 6: _b.apply(void 0, [_c.sent()]).toEqual([ { id: expect.anything(), createdOn: 3, sharedOn: null, needsIntegration: false, collection: 'user', pk: 53, operation: 'create', value: { firstName: 'John', lastName: 'Doe' } }, { id: expect.anything(), createdOn: 4, sharedOn: null, needsIntegration: false, collection: 'user', pk: 54, operation: 'create', value: { firstName: 'Jane', lastName: 'Doe' } }, { id: expect.anything(), createdOn: 5, sharedOn: null, needsIntegration: false, collection: 'user', pk: 55, operation: 'create', value: { firstName: 'Jack', lastName: 'Daniels' } }, { id: expect.anything(), createdOn: 6, sharedOn: null, needsIntegration: false, collection: 'user', pk: 53, operation: 'modify', field: 'lastName', value: 'Trump', }, { id: expect.anything(), createdOn: 7, sharedOn: null, needsIntegration: false, collection: 'user', pk: 54, operation: 'modify', field: 'lastName', value: 'Trump', }, ]); return [2]; } }); }); }); it('should write updateObjects operations done on multiple fields to the ClientSyncLog in a batch write', function () { return __awaiter(_this, void 0, void 0, function () { var now, _a, storageManager, clientSyncLog, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: now = 2; return [4, setupTest({ now: function () { return ++now; }, userFields: { firstName: { type: 'string' }, lastName: { type: 'string' }, } })]; case 1: _a = _c.sent(), storageManager = _a.storageManager, clientSyncLog = _a.clientSyncLog; return [4, storageManager.collection('user').createObject({ id: 53, firstName: 'John', lastName: 'Doe' })]; case 2: _c.sent(); return [4, storageManager.collection('user').createObject({ id: 54, firstName: 'Jane', lastName: 'Doe' })]; case 3: _c.sent(); return [4, storageManager.collection('user').createObject({ id: 55, firstName: 'Jack', lastName: 'Daniels' })]; case 4: _c.sent(); return [4, storageManager.collection('user').updateObjects({ lastName: 'Doe' }, { firstName: 'Pinata', lastName: 'Trump' })]; case 5: _c.sent(); _b = expect; return [4, clientSyncLog.getEntriesCreatedAfter(1)]; case 6: _b.apply(void 0, [_c.sent()]).toEqual([ { id: expect.anything(), createdOn: 3, sharedOn: null, needsIntegration: false, collection: 'user', pk: 53, operation: 'create', value: { firstName: 'John', lastName: 'Doe' } }, { id: expect.anything(), createdOn: 4, sharedOn: null, needsIntegration: false, collection: 'user', pk: 54, operation: 'create', value: { firstName: 'Jane', lastName: 'Doe' } }, { id: expect.anything(), createdOn: 5, sharedOn: null, needsIntegration: false, collection: 'user', pk: 55, operation: 'create', value: { firstName: 'Jack', lastName: 'Daniels' } }, { id: expect.anything(), createdOn: 6, sharedOn: null, needsIntegration: false, collection: 'user', pk: 53, operation: 'modify', field: 'firstName', value: 'Pinata', }, { id: expect.anything(), createdOn: 7, sharedOn: null, needsIntegration: false, collection: 'user', pk: 53, operation: 'modify', field: 'lastName', value: 'Trump', }, { id: expect.anything(), createdOn: 8, sharedOn: null, needsIntegration: false, collection: 'user', pk: 54, operation: 'modify', field: 'firstName', value: 'Pinata', }, { id: expect.anything(), createdOn: 9, sharedOn: null, needsIntegration: false, collection: 'user', pk: 54, operation: 'modify', field: 'lastName', value: 'Trump', }, ]); return [2]; } }); }); }); it('should correctly process batch operations with createObject operations', function () { return __awaiter(_this, void 0, void 0, function () { var now, _a, storageManager, clientSyncLog, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: now = 2; return [4, setupTest({ now: function () { return ++now; } })]; case 1: _a = _c.sent(), storageManager = _a.storageManager, clientSyncLog = _a.clientSyncLog; return [4, storageManager.operation('executeBatch', [ { placeholder: 'john', operation: 'createObject', collection: 'user', args: { id: 53, displayName: 'John Doe' } }, { placeholder: 'jane', operation: 'createObject', collection: 'user', args: { id: 54, displayName: 'Jane Does' } }, ])]; case 2: _c.sent(); _b = expect; return [4, clientSyncLog.getEntriesCreatedAfter(1)]; case 3: _b.apply(void 0, [_c.sent()]).toEqual([ { id: expect.anything(), createdOn: 3, sharedOn: null, needsIntegration: false, collection: 'user', pk: 53, operation: 'create', value: { displayName: 'John Doe' } }, { id: expect.anything(), createdOn: 4, sharedOn: null, needsIntegration: false, collection: 'user', pk: 54, operation: 'create', value: { displayName: 'Jane Does' } }, ]); return [2]; } }); }); }); }); //# sourceMappingURL=index.test.js.map