@vsaas/loopback 10.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/LICENSE +25 -0
  2. package/README.md +91 -0
  3. package/common/models/README.md +109 -0
  4. package/common/models/access-token.json +37 -0
  5. package/common/models/acl.json +17 -0
  6. package/common/models/application.json +130 -0
  7. package/common/models/change.json +25 -0
  8. package/common/models/checkpoint.json +14 -0
  9. package/common/models/email.json +11 -0
  10. package/common/models/key-value-model.json +4 -0
  11. package/common/models/role-mapping.json +26 -0
  12. package/common/models/role.json +30 -0
  13. package/common/models/scope.json +14 -0
  14. package/common/models/user.json +118 -0
  15. package/dist/_virtual/_rolldown/runtime.cjs +32 -0
  16. package/dist/common/models/access-token.cjs +144 -0
  17. package/dist/common/models/access-token2.cjs +43 -0
  18. package/dist/common/models/acl.cjs +428 -0
  19. package/dist/common/models/acl2.cjs +27 -0
  20. package/dist/common/models/application.cjs +100 -0
  21. package/dist/common/models/application2.cjs +118 -0
  22. package/dist/common/models/change.cjs +404 -0
  23. package/dist/common/models/change2.cjs +25 -0
  24. package/dist/common/models/checkpoint.cjs +43 -0
  25. package/dist/common/models/checkpoint2.cjs +18 -0
  26. package/dist/common/models/email.cjs +18 -0
  27. package/dist/common/models/email2.cjs +30 -0
  28. package/dist/common/models/key-value-model.cjs +140 -0
  29. package/dist/common/models/key-value-model2.cjs +14 -0
  30. package/dist/common/models/role-mapping.cjs +57 -0
  31. package/dist/common/models/role-mapping2.cjs +34 -0
  32. package/dist/common/models/role.cjs +396 -0
  33. package/dist/common/models/role2.cjs +38 -0
  34. package/dist/common/models/scope.cjs +30 -0
  35. package/dist/common/models/scope2.cjs +21 -0
  36. package/dist/common/models/user.cjs +810 -0
  37. package/dist/common/models/user2.cjs +118 -0
  38. package/dist/index.cjs +16 -0
  39. package/dist/lib/access-context.cjs +228 -0
  40. package/dist/lib/application.cjs +450 -0
  41. package/dist/lib/builtin-models.cjs +60 -0
  42. package/dist/lib/configure-shared-methods.cjs +41 -0
  43. package/dist/lib/connectors/base-connector.cjs +23 -0
  44. package/dist/lib/connectors/mail-direct-transport.cjs +375 -0
  45. package/dist/lib/connectors/mail-stub-transport.cjs +86 -0
  46. package/dist/lib/connectors/mail.cjs +128 -0
  47. package/dist/lib/connectors/memory.cjs +19 -0
  48. package/dist/lib/current-context.cjs +22 -0
  49. package/dist/lib/globalize.cjs +29 -0
  50. package/dist/lib/loopback.cjs +313 -0
  51. package/dist/lib/model.cjs +1009 -0
  52. package/dist/lib/persisted-model.cjs +1835 -0
  53. package/dist/lib/registry.cjs +291 -0
  54. package/dist/lib/runtime.cjs +25 -0
  55. package/dist/lib/server-app.cjs +231 -0
  56. package/dist/lib/utils.cjs +154 -0
  57. package/dist/package.cjs +124 -0
  58. package/dist/server/middleware/context.cjs +7 -0
  59. package/dist/server/middleware/error-handler.cjs +6 -0
  60. package/dist/server/middleware/favicon.cjs +13 -0
  61. package/dist/server/middleware/rest.cjs +44 -0
  62. package/dist/server/middleware/static.cjs +14 -0
  63. package/dist/server/middleware/status.cjs +28 -0
  64. package/dist/server/middleware/token.cjs +66 -0
  65. package/dist/server/middleware/url-not-found.cjs +20 -0
  66. package/favicon.ico +0 -0
  67. package/package.json +121 -0
  68. package/templates/reset-form.ejs +3 -0
  69. package/templates/verify.ejs +9 -0
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ const require_runtime = require("../../_virtual/_rolldown/runtime.cjs");
3
+ const require_lib_utils = require("../../lib/utils.cjs");
4
+ //#region src/common/models/application.ts
5
+ var require_application = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
6
+ const assert = require("assert");
7
+ const utils = require_lib_utils;
8
+ /*!
9
+ * Application management functions
10
+ */
11
+ const crypto = require("crypto");
12
+ function generateKey(hmacKey, algorithm, encoding) {
13
+ hmacKey = hmacKey || "loopback";
14
+ algorithm = algorithm || "sha1";
15
+ encoding = encoding || "hex";
16
+ const hmac = crypto.createHmac(algorithm, hmacKey);
17
+ const buf = crypto.randomBytes(32);
18
+ hmac.update(buf);
19
+ return hmac.digest(encoding);
20
+ }
21
+ function configureApplication(Application) {
22
+ Application.observe("before save", function(ctx, next) {
23
+ if (!ctx.instance) return next();
24
+ const app = ctx.instance;
25
+ app.created = app.modified = /* @__PURE__ */ new Date();
26
+ if (!app.id) app.id = generateKey("id", "md5");
27
+ app.clientKey = generateKey("client");
28
+ app.javaScriptKey = generateKey("javaScript");
29
+ app.restApiKey = generateKey("restApi");
30
+ app.windowsKey = generateKey("windows");
31
+ app.masterKey = generateKey("master");
32
+ next();
33
+ });
34
+ Application.register = function(owner, name, options, cb) {
35
+ assert(owner, "owner is required");
36
+ assert(name, "name is required");
37
+ if (typeof options === "function" && !cb) {
38
+ cb = options;
39
+ options = {};
40
+ }
41
+ cb = cb || utils.createPromiseCallback();
42
+ const props = {
43
+ owner,
44
+ name
45
+ };
46
+ for (const p in options) if (!(p in props)) props[p] = options[p];
47
+ this.create(props, cb);
48
+ return cb.promise;
49
+ };
50
+ Application.prototype.resetKeys = function(cb) {
51
+ this.clientKey = generateKey("client");
52
+ this.javaScriptKey = generateKey("javaScript");
53
+ this.restApiKey = generateKey("restApi");
54
+ this.windowsKey = generateKey("windows");
55
+ this.masterKey = generateKey("master");
56
+ this.modified = /* @__PURE__ */ new Date();
57
+ this.save(cb);
58
+ };
59
+ Application.resetKeys = function(appId, cb) {
60
+ cb = cb || utils.createPromiseCallback();
61
+ this.findById(appId, function(err, app) {
62
+ if (err) {
63
+ if (cb) cb(err, app);
64
+ return;
65
+ }
66
+ app.resetKeys(cb);
67
+ });
68
+ return cb.promise;
69
+ };
70
+ Application.authenticate = function(appId, key, cb) {
71
+ cb = cb || utils.createPromiseCallback();
72
+ this.findById(appId, function(err, app) {
73
+ if (err || !app) {
74
+ cb(err, null);
75
+ return cb.promise;
76
+ }
77
+ let result = null;
78
+ const keyNames = [
79
+ "clientKey",
80
+ "javaScriptKey",
81
+ "restApiKey",
82
+ "windowsKey",
83
+ "masterKey"
84
+ ];
85
+ for (let i = 0; i < keyNames.length; i++) if (app[keyNames[i]] === key) {
86
+ result = {
87
+ application: app,
88
+ keyType: keyNames[i]
89
+ };
90
+ break;
91
+ }
92
+ cb(null, result);
93
+ });
94
+ return cb.promise;
95
+ };
96
+ }
97
+ module.exports = configureApplication;
98
+ }));
99
+ //#endregion
100
+ module.exports = require_application();
@@ -0,0 +1,118 @@
1
+ //#region common/models/application.json
2
+ var require_application = /* @__PURE__ */ require("../../_virtual/_rolldown/runtime.cjs").__commonJSMin(((exports, module) => {
3
+ module.exports = {
4
+ "name": "Application",
5
+ "properties": {
6
+ "id": {
7
+ "type": "string",
8
+ "id": true
9
+ },
10
+ "realm": { "type": "string" },
11
+ "name": {
12
+ "type": "string",
13
+ "required": true
14
+ },
15
+ "description": "string",
16
+ "icon": {
17
+ "type": "string",
18
+ "description": "The icon image url"
19
+ },
20
+ "owner": {
21
+ "type": "string",
22
+ "description": "The user id of the developer who registers the application"
23
+ },
24
+ "collaborators": {
25
+ "type": ["string"],
26
+ "description": "A list of users ids who have permissions to work on this app"
27
+ },
28
+ "email": "string",
29
+ "emailVerified": "boolean",
30
+ "url": {
31
+ "type": "string",
32
+ "description": "The application URL for OAuth 2.0"
33
+ },
34
+ "callbackUrls": {
35
+ "type": ["string"],
36
+ "description": "OAuth 2.0 code/token callback URLs"
37
+ },
38
+ "permissions": {
39
+ "type": ["string"],
40
+ "description": "A list of permissions required by the application"
41
+ },
42
+ "clientKey": "string",
43
+ "javaScriptKey": "string",
44
+ "restApiKey": "string",
45
+ "windowsKey": "string",
46
+ "masterKey": "string",
47
+ "pushSettings": {
48
+ "apns": {
49
+ "production": {
50
+ "type": "boolean",
51
+ "description": [
52
+ "Production or development mode. It denotes what default APNS",
53
+ "servers to be used to send notifications.",
54
+ "See API documentation for more details."
55
+ ]
56
+ },
57
+ "certData": {
58
+ "type": "string",
59
+ "description": "The certificate data loaded from the cert.pem file"
60
+ },
61
+ "keyData": {
62
+ "type": "string",
63
+ "description": "The key data loaded from the key.pem file"
64
+ },
65
+ "pushOptions": { "type": {
66
+ "gateway": "string",
67
+ "port": "number"
68
+ } },
69
+ "feedbackOptions": { "type": {
70
+ "gateway": "string",
71
+ "port": "number",
72
+ "batchFeedback": "boolean",
73
+ "interval": "number"
74
+ } }
75
+ },
76
+ "gcm": { "serverApiKey": "string" }
77
+ },
78
+ "authenticationEnabled": {
79
+ "type": "boolean",
80
+ "default": true
81
+ },
82
+ "anonymousAllowed": {
83
+ "type": "boolean",
84
+ "default": true
85
+ },
86
+ "authenticationSchemes": [{
87
+ "scheme": {
88
+ "type": "string",
89
+ "description": "See the API docs for the list of supported values."
90
+ },
91
+ "credential": {
92
+ "type": "object",
93
+ "description": "Scheme-specific credentials"
94
+ }
95
+ }],
96
+ "status": {
97
+ "type": "string",
98
+ "default": "sandbox",
99
+ "description": "Status of the application, production/sandbox/disabled"
100
+ },
101
+ "created": {
102
+ "type": "date",
103
+ "defaultFn": "now"
104
+ },
105
+ "modified": {
106
+ "type": "date",
107
+ "defaultFn": "now"
108
+ }
109
+ }
110
+ };
111
+ }));
112
+ //#endregion
113
+ Object.defineProperty(exports, "default", {
114
+ enumerable: true,
115
+ get: function() {
116
+ return require_application();
117
+ }
118
+ });
@@ -0,0 +1,404 @@
1
+ "use strict";
2
+ const require_runtime = require("../../_virtual/_rolldown/runtime.cjs");
3
+ const require_lib_globalize = require("../../lib/globalize.cjs");
4
+ const require_lib_utils = require("../../lib/utils.cjs");
5
+ //#region src/common/models/change.ts
6
+ var require_change = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
7
+ const g = require_lib_globalize;
8
+ const utils = require_lib_utils;
9
+ const crypto = require("crypto");
10
+ const assert = require("assert");
11
+ const debug = require("debug")("loopback:change");
12
+ function stableStringify(value) {
13
+ return JSON.stringify(sortForStableStringify(value));
14
+ }
15
+ function sortForStableStringify(value) {
16
+ if (value && typeof value.toJSON === "function") value = value.toJSON();
17
+ if (Array.isArray(value)) return value.map(sortForStableStringify);
18
+ if (value && typeof value === "object") {
19
+ const sorted = {};
20
+ Object.keys(value).sort().forEach(function(key) {
21
+ const sortedValue = sortForStableStringify(value[key]);
22
+ if (sortedValue !== void 0) sorted[key] = sortedValue;
23
+ });
24
+ return sorted;
25
+ }
26
+ return value;
27
+ }
28
+ module.exports = function(Change) {
29
+ Change.UPDATE = "update";
30
+ Change.CREATE = "create";
31
+ Change.DELETE = "delete";
32
+ Change.UNKNOWN = "unknown";
33
+ Change.Conflict = Conflict;
34
+ Change.setup = function() {
35
+ this.registry.getModel("PersistedModel").setup.call(this);
36
+ const Change = this;
37
+ Change.getter.id = function() {
38
+ if (!(this.modelName && this.modelId)) return null;
39
+ return Change.idForModel(this.modelName, this.modelId);
40
+ };
41
+ };
42
+ Change.setup();
43
+ Change.rectifyModelChanges = function(modelName, modelIds, callback) {
44
+ const Change = this;
45
+ const errors = [];
46
+ callback = callback || utils.createPromiseCallback();
47
+ const tasks = modelIds.map(function(id) {
48
+ return function(cb) {
49
+ Change.findOrCreateChange(modelName, id, function(err, change) {
50
+ if (err) return next(err);
51
+ change.rectify(next);
52
+ });
53
+ function next(err) {
54
+ if (err) {
55
+ err.modelName = modelName;
56
+ err.modelId = id;
57
+ errors.push(err);
58
+ }
59
+ cb();
60
+ }
61
+ };
62
+ });
63
+ Promise.all(tasks.map(function(task) {
64
+ return utils.invokeWithCallback(task, null, []);
65
+ })).then(function() {
66
+ if (errors.length) {
67
+ const desc = errors.map(function(e) {
68
+ return "#" + e.modelId + " - " + e.toString();
69
+ }).join("\n");
70
+ const err = new Error(g.f("Cannot rectify %s changes:\n%s", modelName, desc));
71
+ err.details = { errors };
72
+ return callback(err);
73
+ }
74
+ callback();
75
+ }, callback);
76
+ return callback.promise;
77
+ };
78
+ Change.idForModel = function(modelName, modelId) {
79
+ return this.hash([modelName, modelId].join("-"));
80
+ };
81
+ Change.findOrCreateChange = function(modelName, modelId, callback) {
82
+ assert(this.registry.findModel(modelName), modelName + " does not exist");
83
+ callback = callback || utils.createPromiseCallback();
84
+ const id = this.idForModel(modelName, modelId);
85
+ const Change = this;
86
+ this.findById(id, function(err, change) {
87
+ if (err) return callback(err);
88
+ if (change) callback(null, change);
89
+ else {
90
+ const ch = new Change({
91
+ id,
92
+ modelName,
93
+ modelId
94
+ });
95
+ ch.debug("creating change");
96
+ Change.updateOrCreate(ch, callback);
97
+ }
98
+ });
99
+ return callback.promise;
100
+ };
101
+ Change.prototype.rectify = function(cb) {
102
+ const change = this;
103
+ const currentRev = this.rev;
104
+ change.debug("rectify change");
105
+ cb = cb || utils.createPromiseCallback();
106
+ const model = this.getModelCtor();
107
+ const id = this.getModelId();
108
+ model.findById(id, function(err, inst) {
109
+ if (err) return cb(err);
110
+ if (inst) inst.fillCustomChangeProperties(change, function() {
111
+ prepareAndDoRectify(Change.revisionForInst(inst));
112
+ });
113
+ else prepareAndDoRectify(null);
114
+ });
115
+ return cb.promise;
116
+ function prepareAndDoRectify(rev) {
117
+ if (currentRev === rev) {
118
+ change.debug("rev and prev are equal (not updating anything)");
119
+ return cb(null, change);
120
+ }
121
+ change.constructor.getCheckpointModel().current(function(err, checkpoint) {
122
+ if (err) return cb(err);
123
+ doRectify(checkpoint, rev);
124
+ });
125
+ }
126
+ function doRectify(checkpoint, rev) {
127
+ if (rev) if (currentRev === rev) {
128
+ change.debug("ASSERTION FAILED: Change currentRev==rev should have been already handled");
129
+ return cb(null, change);
130
+ } else {
131
+ change.rev = rev;
132
+ change.debug("updated revision (was " + currentRev + ")");
133
+ if (change.checkpoint !== checkpoint) {
134
+ change.prev = currentRev;
135
+ change.debug("updated prev");
136
+ }
137
+ }
138
+ else {
139
+ change.rev = null;
140
+ change.debug("updated revision (was " + currentRev + ")");
141
+ if (change.checkpoint !== checkpoint) {
142
+ if (currentRev) change.prev = currentRev;
143
+ else if (!change.prev) {
144
+ change.debug("ERROR - could not determine prev");
145
+ change.prev = Change.UNKNOWN;
146
+ }
147
+ change.debug("updated prev");
148
+ }
149
+ }
150
+ if (change.checkpoint != checkpoint) {
151
+ debug("update checkpoint to", checkpoint);
152
+ change.checkpoint = checkpoint;
153
+ }
154
+ if (change.prev === Change.UNKNOWN) change.remove(cb);
155
+ else change.save(cb);
156
+ }
157
+ };
158
+ Change.prototype.currentRevision = function(cb) {
159
+ cb = cb || utils.createPromiseCallback();
160
+ const model = this.getModelCtor();
161
+ const id = this.getModelId();
162
+ model.findById(id, function(err, inst) {
163
+ if (err) return cb(err);
164
+ if (inst) cb(null, Change.revisionForInst(inst));
165
+ else cb(null, null);
166
+ });
167
+ return cb.promise;
168
+ };
169
+ Change.hash = function(str) {
170
+ return crypto.createHash(Change.settings.hashAlgorithm || "sha1").update(str).digest("hex");
171
+ };
172
+ Change.revisionForInst = function(inst) {
173
+ assert(inst, "Change.revisionForInst() requires an instance object.");
174
+ return this.hash(stableStringify(inst));
175
+ };
176
+ Change.prototype.type = function() {
177
+ if (this.rev && this.prev) return Change.UPDATE;
178
+ if (this.rev && !this.prev) return Change.CREATE;
179
+ if (!this.rev && this.prev) return Change.DELETE;
180
+ return Change.UNKNOWN;
181
+ };
182
+ Change.prototype.equals = function(change) {
183
+ if (!change) return false;
184
+ return (this.rev || null) === (change.rev || null);
185
+ };
186
+ Change.prototype.conflictsWith = function(change) {
187
+ if (!change) return false;
188
+ if (this.equals(change)) return false;
189
+ if (Change.bothDeleted(this, change)) return false;
190
+ if (this.isBasedOn(change)) return false;
191
+ return true;
192
+ };
193
+ Change.bothDeleted = function(a, b) {
194
+ return a.type() === Change.DELETE && b.type() === Change.DELETE;
195
+ };
196
+ Change.prototype.isBasedOn = function(change) {
197
+ return this.prev === change.rev;
198
+ };
199
+ Change.diff = function(modelName, since, remoteChanges, callback) {
200
+ callback = callback || utils.createPromiseCallback();
201
+ if (!Array.isArray(remoteChanges) || remoteChanges.length === 0) {
202
+ callback(null, {
203
+ deltas: [],
204
+ conflicts: []
205
+ });
206
+ return callback.promise;
207
+ }
208
+ const remoteChangeIndex = {};
209
+ const modelIds = [];
210
+ remoteChanges.forEach(function(ch) {
211
+ modelIds.push(ch.modelId);
212
+ remoteChangeIndex[ch.modelId] = new Change(ch);
213
+ });
214
+ since = Number(since) || 0;
215
+ this.find({ where: {
216
+ modelName,
217
+ modelId: { inq: modelIds }
218
+ } }, function(err, allLocalChanges) {
219
+ if (err) return callback(err);
220
+ const deltas = [];
221
+ const conflicts = [];
222
+ const localModelIds = [];
223
+ allLocalChanges.filter(function(c) {
224
+ return c.checkpoint >= since;
225
+ }).forEach(function(localChange) {
226
+ localChange = new Change(localChange);
227
+ localModelIds.push(localChange.modelId);
228
+ const remoteChange = remoteChangeIndex[localChange.modelId];
229
+ if (remoteChange && !localChange.equals(remoteChange)) if (remoteChange.conflictsWith(localChange)) {
230
+ remoteChange.debug("remote conflict");
231
+ localChange.debug("local conflict");
232
+ conflicts.push(localChange);
233
+ } else {
234
+ remoteChange.debug("remote delta");
235
+ deltas.push(remoteChange);
236
+ }
237
+ });
238
+ modelIds.forEach(function(id) {
239
+ if (localModelIds.indexOf(id) !== -1) return;
240
+ const d = remoteChangeIndex[id];
241
+ const oldChange = allLocalChanges.filter(function(c) {
242
+ return c.modelId === id;
243
+ })[0];
244
+ if (oldChange) d.prev = oldChange.rev;
245
+ else d.prev = null;
246
+ deltas.push(d);
247
+ });
248
+ callback(null, {
249
+ deltas,
250
+ conflicts
251
+ });
252
+ });
253
+ return callback.promise;
254
+ };
255
+ Change.rectifyAll = function(cb) {
256
+ debug("rectify all");
257
+ const Change = this;
258
+ cb = cb || utils.createPromiseCallback();
259
+ utils.invokeWithCallback(Change.find, Change, [{}]).then(async function(changes) {
260
+ for (const change of changes || []) await change.rectify();
261
+ }).then(function() {
262
+ cb();
263
+ }, cb);
264
+ return cb.promise;
265
+ };
266
+ Change.getCheckpointModel = function() {
267
+ let checkpointModel = this.Checkpoint;
268
+ if (checkpointModel) return checkpointModel;
269
+ this.Checkpoint = checkpointModel = this.registry.getModel("Checkpoint").extend("checkpoint");
270
+ assert(this.dataSource, "Cannot getCheckpointModel(): " + this.modelName + " is not attached to a dataSource");
271
+ checkpointModel.attachTo(this.dataSource);
272
+ return checkpointModel;
273
+ };
274
+ Change.prototype.debug = function() {
275
+ if (debug.enabled) {
276
+ const args = Array.prototype.slice.call(arguments);
277
+ args[0] = args[0] + " %s";
278
+ args.push(this.modelName);
279
+ debug.apply(this, args);
280
+ debug(" id", this.id);
281
+ debug(" rev", this.rev);
282
+ debug(" prev", this.prev);
283
+ debug(" checkpoint", this.checkpoint);
284
+ debug(" modelName", this.modelName);
285
+ debug(" modelId", this.modelId);
286
+ debug(" type", this.type());
287
+ }
288
+ };
289
+ Change.prototype.getModelCtor = function() {
290
+ return this.constructor.settings.trackModel;
291
+ };
292
+ Change.prototype.getModelId = function() {
293
+ const Model = this.getModelCtor();
294
+ const id = this.modelId;
295
+ const m = new Model();
296
+ m.setId(id);
297
+ return m.getId();
298
+ };
299
+ Change.prototype.getModel = function(callback) {
300
+ const Model = this.constructor.settings.trackModel;
301
+ const id = this.getModelId();
302
+ Model.findById(id, callback);
303
+ };
304
+ function Conflict(modelId, SourceModel, TargetModel) {
305
+ this.SourceModel = SourceModel;
306
+ this.TargetModel = TargetModel;
307
+ this.SourceChange = SourceModel.getChangeModel();
308
+ this.TargetChange = TargetModel.getChangeModel();
309
+ this.modelId = modelId;
310
+ }
311
+ Conflict.prototype.models = function(cb) {
312
+ const conflict = this;
313
+ const SourceModel = this.SourceModel;
314
+ const TargetModel = this.TargetModel;
315
+ const promise = Promise.all([utils.invokeWithCallback(SourceModel.findById, SourceModel, [conflict.modelId]), utils.invokeWithCallback(TargetModel.findById, TargetModel, [conflict.modelId])]);
316
+ if (typeof cb === "function") {
317
+ promise.then(function(results) {
318
+ cb(null, results[0], results[1]);
319
+ }, cb);
320
+ return;
321
+ }
322
+ return promise.then(function(results) {
323
+ return {
324
+ source: results[0],
325
+ target: results[1]
326
+ };
327
+ });
328
+ };
329
+ Conflict.prototype.changes = function(cb) {
330
+ const conflict = this;
331
+ const promise = Promise.all([utils.invokeWithCallback(conflict.SourceModel.findLastChange, conflict.SourceModel, [conflict.modelId]), utils.invokeWithCallback(conflict.TargetModel.findLastChange, conflict.TargetModel, [conflict.modelId])]);
332
+ if (typeof cb === "function") {
333
+ promise.then(function(results) {
334
+ cb(null, results[0], results[1]);
335
+ }, cb);
336
+ return;
337
+ }
338
+ return promise.then(function(results) {
339
+ return {
340
+ sourceChange: results[0],
341
+ targetChange: results[1]
342
+ };
343
+ });
344
+ };
345
+ Conflict.prototype.resolve = function(cb) {
346
+ const conflict = this;
347
+ cb = cb || utils.createPromiseCallback();
348
+ utils.invokeWithCallback(conflict.TargetModel.findLastChange, conflict.TargetModel, [this.modelId]).then(function(targetChange) {
349
+ return utils.invokeWithCallback(conflict.SourceModel.updateLastChange, conflict.SourceModel, [conflict.modelId, { prev: targetChange.rev }]);
350
+ }).then(function(result) {
351
+ cb(null, result);
352
+ }, cb);
353
+ return cb.promise;
354
+ };
355
+ Conflict.prototype.resolveUsingSource = function(cb) {
356
+ this.resolve(function(err) {
357
+ cb(err);
358
+ });
359
+ };
360
+ Conflict.prototype.resolveUsingTarget = function(cb) {
361
+ const conflict = this;
362
+ conflict.models(function(err, source, target) {
363
+ if (err) return done(err);
364
+ if (target === null) return conflict.SourceModel.deleteById(conflict.modelId, done);
365
+ new conflict.SourceModel(target.toObject(), { persisted: true }).save(done);
366
+ });
367
+ function done(err) {
368
+ cb(err);
369
+ }
370
+ };
371
+ Conflict.prototype.swapParties = function() {
372
+ const Ctor = this.constructor;
373
+ return new Ctor(this.modelId, this.TargetModel, this.SourceModel);
374
+ };
375
+ Conflict.prototype.resolveManually = function(data, cb) {
376
+ const conflict = this;
377
+ if (!data) return conflict.SourceModel.deleteById(conflict.modelId, done);
378
+ conflict.models(function(err, source, target) {
379
+ if (err) return done(err);
380
+ const inst = source || new conflict.SourceModel(target);
381
+ inst.setAttributes(data);
382
+ inst.save(function(err) {
383
+ if (err) return done(err);
384
+ conflict.resolve(done);
385
+ });
386
+ });
387
+ function done(err) {
388
+ cb(err);
389
+ }
390
+ };
391
+ Conflict.prototype.type = function(cb) {
392
+ this.changes(function(err, sourceChange, targetChange) {
393
+ if (err) return cb(err);
394
+ const sourceChangeType = sourceChange.type();
395
+ const targetChangeType = targetChange.type();
396
+ if (sourceChangeType === Change.UPDATE && targetChangeType === Change.UPDATE) return cb(null, Change.UPDATE);
397
+ if (sourceChangeType === Change.DELETE || targetChangeType === Change.DELETE) return cb(null, Change.DELETE);
398
+ return cb(null, Change.UNKNOWN);
399
+ });
400
+ };
401
+ };
402
+ }));
403
+ //#endregion
404
+ module.exports = require_change();
@@ -0,0 +1,25 @@
1
+ //#region common/models/change.json
2
+ var require_change = /* @__PURE__ */ require("../../_virtual/_rolldown/runtime.cjs").__commonJSMin(((exports, module) => {
3
+ module.exports = {
4
+ "name": "Change",
5
+ "trackChanges": false,
6
+ "properties": {
7
+ "id": {
8
+ "type": "string",
9
+ "id": true
10
+ },
11
+ "rev": { "type": "string" },
12
+ "prev": { "type": "string" },
13
+ "checkpoint": { "type": "number" },
14
+ "modelName": { "type": "string" },
15
+ "modelId": { "type": "string" }
16
+ }
17
+ };
18
+ }));
19
+ //#endregion
20
+ Object.defineProperty(exports, "default", {
21
+ enumerable: true,
22
+ get: function() {
23
+ return require_change();
24
+ }
25
+ });
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ const require_runtime = require("../../_virtual/_rolldown/runtime.cjs");
3
+ const require_lib_utils = require("../../lib/utils.cjs");
4
+ //#region src/common/models/checkpoint.ts
5
+ var require_checkpoint = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
6
+ const utils = require_lib_utils;
7
+ function configureCheckpoint(Checkpoint) {
8
+ Checkpoint.definition.rawProperties.time.default = Checkpoint.definition.properties.time.default = function() {
9
+ return /* @__PURE__ */ new Date();
10
+ };
11
+ Checkpoint.current = function(cb) {
12
+ cb = cb || utils.createPromiseCallback();
13
+ utils.invokeWithCallback(this._getSingleton, this, []).then(function(cp) {
14
+ cb(null, cp.seq);
15
+ }, cb);
16
+ return cb.promise;
17
+ };
18
+ Checkpoint._getSingleton = function(cb) {
19
+ cb = cb || utils.createPromiseCallback();
20
+ this.findOrCreate({ limit: 1 }, { seq: 1 }, cb);
21
+ return cb.promise;
22
+ };
23
+ Checkpoint.bumpLastSeq = function(cb) {
24
+ cb = cb || utils.createPromiseCallback();
25
+ (async function() {
26
+ const cp = await utils.invokeWithCallback(this._getSingleton, this, []);
27
+ const originalSeq = cp.seq;
28
+ cp.seq++;
29
+ await utils.invokeWithCallback(this.updateAll, this, [{
30
+ id: cp.id,
31
+ seq: originalSeq
32
+ }, { seq: cp.seq }]);
33
+ return cp;
34
+ }).call(this).then(function(cp) {
35
+ cb(null, cp);
36
+ }, cb);
37
+ return cb.promise;
38
+ };
39
+ }
40
+ module.exports = configureCheckpoint;
41
+ }));
42
+ //#endregion
43
+ module.exports = require_checkpoint();