@lenne.tech/nest-server 12.1.0 → 12.2.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.
- package/.claude/rules/architecture.md +1 -0
- package/.claude/rules/better-auth.md +18 -6
- package/.claude/rules/configurable-features.md +5 -4
- package/.claude/rules/package-management.md +16 -1
- package/.claude/rules/persistence.md +26 -0
- package/.claude/rules/testing.md +102 -28
- package/CLAUDE.md +3 -2
- package/FRAMEWORK-API.md +17 -2
- package/README.md +24 -3
- package/dist/core/common/helpers/file.helper.js +3 -4
- package/dist/core/common/helpers/file.helper.js.map +1 -1
- package/dist/core/common/helpers/graphql-ws-connection.helper.js.map +1 -1
- package/dist/core/common/interfaces/server-options.interface.d.ts +41 -0
- package/dist/core/common/interfaces/service-options.interface.d.ts +3 -0
- package/dist/core/common/persistence/core-persistence.module.js +12 -2
- package/dist/core/common/persistence/core-persistence.module.js.map +1 -1
- package/dist/core/common/persistence/data-source.runtime.js +14 -19
- package/dist/core/common/persistence/data-source.runtime.js.map +1 -1
- package/dist/core/common/persistence/data-store.interface.d.ts +24 -9
- package/dist/core/common/persistence/id-format.d.ts +2 -0
- package/dist/core/common/persistence/id-format.js +14 -0
- package/dist/core/common/persistence/id-format.js.map +1 -1
- package/dist/core/common/persistence/model-descriptor.interface.d.ts +6 -0
- package/dist/core/common/persistence/model-metadata.registry.d.ts +2 -1
- package/dist/core/common/persistence/model-metadata.registry.js +20 -2
- package/dist/core/common/persistence/model-metadata.registry.js.map +1 -1
- package/dist/core/common/persistence/mongo/sync-schema.plugin.d.ts +18 -0
- package/dist/core/common/persistence/mongo/sync-schema.plugin.js +368 -0
- package/dist/core/common/persistence/mongo/sync-schema.plugin.js.map +1 -0
- package/dist/core/common/persistence/mongoose-data-store.d.ts +19 -9
- package/dist/core/common/persistence/mongoose-data-store.js +107 -31
- package/dist/core/common/persistence/mongoose-data-store.js.map +1 -1
- package/dist/core/common/persistence/persisted-model.decorator.d.ts +3 -0
- package/dist/core/common/persistence/persisted-model.decorator.js +9 -0
- package/dist/core/common/persistence/persisted-model.decorator.js.map +1 -1
- package/dist/core/common/persistence/persistence-runtime.registry.d.ts +5 -1
- package/dist/core/common/persistence/persistence-runtime.registry.js.map +1 -1
- package/dist/core/common/persistence/policies/sync.policy.d.ts +17 -0
- package/dist/core/common/persistence/policies/sync.policy.js +77 -0
- package/dist/core/common/persistence/policies/sync.policy.js.map +1 -0
- package/dist/core/common/persistence/sql/mysql/mysql-data-store.d.ts +12 -8
- package/dist/core/common/persistence/sql/mysql/mysql-data-store.js +82 -29
- package/dist/core/common/persistence/sql/mysql/mysql-data-store.js.map +1 -1
- package/dist/core/common/persistence/sql/mysql/mysql-schema.compiler.d.ts +10 -0
- package/dist/core/common/persistence/sql/mysql/mysql-schema.compiler.js +61 -4
- package/dist/core/common/persistence/sql/mysql/mysql-schema.compiler.js.map +1 -1
- package/dist/core/common/persistence/sql/mysql/mysql-schema.migrator.d.ts +2 -0
- package/dist/core/common/persistence/sql/mysql/mysql-schema.migrator.js +145 -4
- package/dist/core/common/persistence/sql/mysql/mysql-schema.migrator.js.map +1 -1
- package/dist/core/common/persistence/sql/postgres/postgres-schema.compiler.d.ts +10 -0
- package/dist/core/common/persistence/sql/postgres/postgres-schema.compiler.js +41 -2
- package/dist/core/common/persistence/sql/postgres/postgres-schema.compiler.js.map +1 -1
- package/dist/core/common/persistence/sql/postgres/postgres-schema.migrator.js +98 -10
- package/dist/core/common/persistence/sql/postgres/postgres-schema.migrator.js.map +1 -1
- package/dist/core/common/persistence/sql/sql-data-store.d.ts +38 -14
- package/dist/core/common/persistence/sql/sql-data-store.js +212 -38
- package/dist/core/common/persistence/sql/sql-data-store.js.map +1 -1
- package/dist/core/common/persistence/sql/sql-dialect.d.ts +2 -0
- package/dist/core/common/persistence/sql/sql-dialect.js +14 -2
- package/dist/core/common/persistence/sql/sql-dialect.js.map +1 -1
- package/dist/core/common/persistence/sql/sql-errors.d.ts +2 -1
- package/dist/core/common/persistence/sql/sql-errors.js +3 -1
- package/dist/core/common/persistence/sql/sql-errors.js.map +1 -1
- package/dist/core/common/persistence/sync-access.registry.d.ts +17 -0
- package/dist/core/common/persistence/sync-access.registry.js +8 -0
- package/dist/core/common/persistence/sync-access.registry.js.map +1 -0
- package/dist/core/common/persistence/sync-model.d.ts +29 -0
- package/dist/core/common/persistence/sync-model.js +138 -0
- package/dist/core/common/persistence/sync-model.js.map +1 -0
- package/dist/core/common/services/core-cron-jobs.service.d.ts +27 -0
- package/dist/core/common/services/core-cron-jobs.service.js +269 -13
- package/dist/core/common/services/core-cron-jobs.service.js.map +1 -1
- package/dist/core/common/services/core-redis-pubsub.js +5 -5
- package/dist/core/common/services/core-redis-pubsub.js.map +1 -1
- package/dist/core/common/services/core-redis.service.d.ts +6 -0
- package/dist/core/common/services/core-redis.service.js +57 -0
- package/dist/core/common/services/core-redis.service.js.map +1 -1
- package/dist/core/common/services/crud.service.d.ts +13 -1
- package/dist/core/common/services/crud.service.js +174 -16
- package/dist/core/common/services/crud.service.js.map +1 -1
- package/dist/core/modules/better-auth/better-auth-sql.helper.d.ts +1 -0
- package/dist/core/modules/better-auth/better-auth-sql.helper.js +41 -14
- package/dist/core/modules/better-auth/better-auth-sql.helper.js.map +1 -1
- package/dist/core/modules/better-auth/better-auth.config.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth-sql.store.d.ts +4 -7
- package/dist/core/modules/better-auth/core-better-auth-sql.store.js +23 -16
- package/dist/core/modules/better-auth/core-better-auth-sql.store.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth-user.mapper.d.ts +12 -2
- package/dist/core/modules/better-auth/core-better-auth-user.mapper.js +82 -5
- package/dist/core/modules/better-auth/core-better-auth-user.mapper.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.module.js +1 -1
- package/dist/core/modules/better-auth/core-better-auth.module.js.map +1 -1
- package/dist/core/modules/error-code/error-codes.d.ts +54 -0
- package/dist/core/modules/error-code/error-codes.js +48 -0
- package/dist/core/modules/error-code/error-codes.js.map +1 -1
- package/dist/core/modules/hub/hub-buffer.js +3 -4
- package/dist/core/modules/hub/hub-buffer.js.map +1 -1
- package/dist/core/modules/migrate/index.js.map +1 -1
- package/dist/core/modules/migrate/migration-runner.d.ts +6 -0
- package/dist/core/modules/migrate/migration-runner.js +28 -2
- package/dist/core/modules/migrate/migration-runner.js.map +1 -1
- package/dist/core/modules/migrate/mongo-state-store.d.ts +0 -1
- package/dist/core/modules/migrate/mongo-state-store.js +0 -5
- package/dist/core/modules/migrate/mongo-state-store.js.map +1 -1
- package/dist/core/modules/migrate/sql-migration.helper.d.ts +15 -1
- package/dist/core/modules/migrate/sql-migration.helper.js +90 -5
- package/dist/core/modules/migrate/sql-migration.helper.js.map +1 -1
- package/dist/core/modules/sync/core-sync-base.controller.d.ts +7 -0
- package/dist/core/modules/sync/core-sync-base.controller.js +44 -0
- package/dist/core/modules/sync/core-sync-base.controller.js.map +1 -0
- package/dist/core/modules/sync/core-sync-cursor.d.ts +19 -0
- package/dist/core/modules/sync/core-sync-cursor.js +77 -0
- package/dist/core/modules/sync/core-sync-cursor.js.map +1 -0
- package/dist/core/modules/sync/core-sync-hints.controller.d.ts +4 -0
- package/dist/core/modules/sync/core-sync-hints.controller.js +55 -0
- package/dist/core/modules/sync/core-sync-hints.controller.js.map +1 -0
- package/dist/core/modules/sync/core-sync-hints.service.d.ts +28 -0
- package/dist/core/modules/sync/core-sync-hints.service.js +155 -0
- package/dist/core/modules/sync/core-sync-hints.service.js.map +1 -0
- package/dist/core/modules/sync/core-sync-infrastructure.models.d.ts +2 -0
- package/dist/core/modules/sync/core-sync-infrastructure.models.js +17 -0
- package/dist/core/modules/sync/core-sync-infrastructure.models.js.map +1 -0
- package/dist/core/modules/sync/core-sync-mutation.store.d.ts +43 -0
- package/dist/core/modules/sync/core-sync-mutation.store.js +88 -0
- package/dist/core/modules/sync/core-sync-mutation.store.js.map +1 -0
- package/dist/core/modules/sync/core-sync-state.store.d.ts +18 -0
- package/dist/core/modules/sync/core-sync-state.store.js +64 -0
- package/dist/core/modules/sync/core-sync-state.store.js.map +1 -0
- package/dist/core/modules/sync/core-sync.constants.d.ts +28 -0
- package/dist/core/modules/sync/core-sync.constants.js +23 -0
- package/dist/core/modules/sync/core-sync.constants.js.map +1 -0
- package/dist/core/modules/sync/core-sync.controller.d.ts +8 -0
- package/dist/core/modules/sync/core-sync.controller.js +79 -0
- package/dist/core/modules/sync/core-sync.controller.js.map +1 -0
- package/dist/core/modules/sync/core-sync.decorators.d.ts +12 -0
- package/dist/core/modules/sync/core-sync.decorators.js +28 -0
- package/dist/core/modules/sync/core-sync.decorators.js.map +1 -0
- package/dist/core/modules/sync/core-sync.module.d.ts +16 -0
- package/dist/core/modules/sync/core-sync.module.js +50 -0
- package/dist/core/modules/sync/core-sync.module.js.map +1 -0
- package/dist/core/modules/sync/core-sync.resolver.d.ts +9 -0
- package/dist/core/modules/sync/core-sync.resolver.js +46 -0
- package/dist/core/modules/sync/core-sync.resolver.js.map +1 -0
- package/dist/core/modules/sync/core-sync.service.d.ts +142 -0
- package/dist/core/modules/sync/core-sync.service.js +1208 -0
- package/dist/core/modules/sync/core-sync.service.js.map +1 -0
- package/dist/core/modules/sync/interfaces/core-sync.interfaces.d.ts +56 -0
- package/dist/core/modules/sync/interfaces/core-sync.interfaces.js +2 -0
- package/dist/core/modules/sync/interfaces/core-sync.interfaces.js.map +1 -0
- package/dist/core/modules/sync/models/core-sync-hint.model.d.ts +4 -0
- package/dist/core/modules/sync/models/core-sync-hint.model.js +30 -0
- package/dist/core/modules/sync/models/core-sync-hint.model.js.map +1 -0
- package/dist/core/modules/tenant/core-tenant.guard.js +8 -3
- package/dist/core/modules/tenant/core-tenant.guard.js.map +1 -1
- package/dist/core.module.js +14 -0
- package/dist/core.module.js.map +1 -1
- package/dist/index.d.ts +16 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -1
- package/dist/server/modules/error-code/error-codes.d.ts +6 -0
- package/dist/server/modules/sync-note/inputs/sync-note-create.input.d.ts +4 -0
- package/dist/server/modules/sync-note/inputs/sync-note-create.input.js +27 -0
- package/dist/server/modules/sync-note/inputs/sync-note-create.input.js.map +1 -0
- package/dist/server/modules/sync-note/inputs/sync-note.input.d.ts +6 -0
- package/dist/server/modules/sync-note/inputs/sync-note.input.js +37 -0
- package/dist/server/modules/sync-note/inputs/sync-note.input.js.map +1 -0
- package/dist/server/modules/sync-note/sync-note.model.d.ts +10 -0
- package/dist/server/modules/sync-note/sync-note.model.js +73 -0
- package/dist/server/modules/sync-note/sync-note.model.js.map +1 -0
- package/dist/server/modules/sync-note/sync-note.module.d.ts +2 -0
- package/dist/server/modules/sync-note/sync-note.module.js +21 -0
- package/dist/server/modules/sync-note/sync-note.module.js.map +1 -0
- package/dist/server/modules/sync-note/sync-note.service.d.ts +10 -0
- package/dist/server/modules/sync-note/sync-note.service.js +35 -0
- package/dist/server/modules/sync-note/sync-note.service.js.map +1 -0
- package/docs/REQUEST-LIFECYCLE.md +3 -2
- package/docs/choosing-a-database.md +7 -4
- package/docs/offline-sync-protocol.md +410 -0
- package/docs/security-overrides.md +14 -0
- package/migration-guides/12.0.x-to-12.1.x.md +9 -4
- package/migration-guides/12.1.0-to-12.1.1.md +125 -0
- package/migration-guides/12.1.x-to-12.2.x.md +154 -0
- package/package.json +10 -8
- package/src/core/common/helpers/file.helper.ts +7 -5
- package/src/core/common/helpers/graphql-ws-connection.helper.ts +3 -2
- package/src/core/common/interfaces/cron-job-config.interface.ts +7 -2
- package/src/core/common/interfaces/server-options.interface.ts +149 -0
- package/src/core/common/interfaces/service-options.interface.ts +15 -0
- package/src/core/common/persistence/core-persistence.module.ts +21 -2
- package/src/core/common/persistence/data-source.runtime.ts +22 -23
- package/src/core/common/persistence/data-store.interface.ts +65 -12
- package/src/core/common/persistence/id-format.ts +26 -0
- package/src/core/common/persistence/model-descriptor.interface.ts +12 -0
- package/src/core/common/persistence/model-metadata.registry.ts +31 -2
- package/src/core/common/persistence/mongo/sync-schema.plugin.ts +508 -0
- package/src/core/common/persistence/mongoose-data-store.ts +171 -35
- package/src/core/common/persistence/persisted-model.decorator.ts +26 -0
- package/src/core/common/persistence/persistence-runtime.registry.ts +16 -5
- package/src/core/common/persistence/policies/sync.policy.ts +121 -0
- package/src/core/common/persistence/sql/mysql/mysql-data-store.ts +108 -30
- package/src/core/common/persistence/sql/mysql/mysql-schema.compiler.ts +107 -4
- package/src/core/common/persistence/sql/mysql/mysql-schema.migrator.ts +194 -3
- package/src/core/common/persistence/sql/postgres/postgres-schema.compiler.ts +74 -2
- package/src/core/common/persistence/sql/postgres/postgres-schema.migrator.ts +139 -7
- package/src/core/common/persistence/sql/sql-data-store.ts +325 -44
- package/src/core/common/persistence/sql/sql-dialect.ts +38 -1
- package/src/core/common/persistence/sql/sql-errors.ts +5 -1
- package/src/core/common/persistence/sql/sql-executor.interface.ts +4 -1
- package/src/core/common/persistence/sync-access.registry.ts +50 -0
- package/src/core/common/persistence/sync-model.ts +270 -0
- package/src/core/common/services/core-cron-jobs.service.ts +441 -27
- package/src/core/common/services/core-redis-pubsub.ts +8 -6
- package/src/core/common/services/core-redis.service.ts +99 -0
- package/src/core/common/services/crud.service.ts +257 -19
- package/src/core/modules/better-auth/README.md +25 -1
- package/src/core/modules/better-auth/better-auth-sql.helper.ts +56 -24
- package/src/core/modules/better-auth/better-auth.config.ts +4 -3
- package/src/core/modules/better-auth/core-better-auth-sql.store.ts +28 -21
- package/src/core/modules/better-auth/core-better-auth-user.mapper.ts +122 -6
- package/src/core/modules/better-auth/core-better-auth.module.ts +2 -2
- package/src/core/modules/error-code/error-codes.ts +59 -0
- package/src/core/modules/hub/README.md +5 -2
- package/src/core/modules/hub/hub-buffer.ts +13 -7
- package/src/core/modules/migrate/README.md +20 -11
- package/src/core/modules/migrate/index.ts +1 -0
- package/src/core/modules/migrate/migration-runner.ts +68 -3
- package/src/core/modules/migrate/mongo-state-store.ts +7 -30
- package/src/core/modules/migrate/sql-migration.helper.ts +141 -7
- package/src/core/modules/sync/INTEGRATION-CHECKLIST.md +95 -0
- package/src/core/modules/sync/README.md +188 -0
- package/src/core/modules/sync/SECURITY.md +65 -0
- package/src/core/modules/sync/core-sync-base.controller.ts +43 -0
- package/src/core/modules/sync/core-sync-cursor.ts +117 -0
- package/src/core/modules/sync/core-sync-hints.controller.ts +52 -0
- package/src/core/modules/sync/core-sync-hints.service.ts +178 -0
- package/src/core/modules/sync/core-sync-infrastructure.models.ts +40 -0
- package/src/core/modules/sync/core-sync-mutation.store.ts +146 -0
- package/src/core/modules/sync/core-sync-state.store.ts +89 -0
- package/src/core/modules/sync/core-sync.constants.ts +51 -0
- package/src/core/modules/sync/core-sync.controller.ts +68 -0
- package/src/core/modules/sync/core-sync.decorators.ts +84 -0
- package/src/core/modules/sync/core-sync.module.ts +75 -0
- package/src/core/modules/sync/core-sync.resolver.ts +35 -0
- package/src/core/modules/sync/core-sync.service.ts +1728 -0
- package/src/core/modules/sync/interfaces/core-sync.interfaces.ts +85 -0
- package/src/core/modules/sync/models/core-sync-hint.model.ts +18 -0
- package/src/core/modules/tenant/README.md +1 -0
- package/src/core/modules/tenant/core-tenant.guard.ts +16 -3
- package/src/core.module.ts +25 -0
- package/src/index.ts +21 -0
- package/src/server/modules/sync-note/inputs/sync-note-create.input.ts +14 -0
- package/src/server/modules/sync-note/inputs/sync-note.input.ts +23 -0
- package/src/server/modules/sync-note/sync-note.model.ts +77 -0
- package/src/server/modules/sync-note/sync-note.module.ts +16 -0
- package/src/server/modules/sync-note/sync-note.service.ts +25 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
+
};
|
|
13
|
+
var CoreSyncHintService_1;
|
|
14
|
+
import { Injectable, Logger, Optional } from '@nestjs/common';
|
|
15
|
+
import { randomUUID } from 'node:crypto';
|
|
16
|
+
import { ConfigService } from '../../common/services/config.service.js';
|
|
17
|
+
import { CoreRedisService } from '../../common/services/core-redis.service.js';
|
|
18
|
+
import { SYNC_DEFAULTS } from './core-sync.constants.js';
|
|
19
|
+
let CoreSyncHintService = CoreSyncHintService_1 = class CoreSyncHintService {
|
|
20
|
+
redisService;
|
|
21
|
+
logger = new Logger(CoreSyncHintService_1.name);
|
|
22
|
+
origin = randomUUID();
|
|
23
|
+
subscribers = new Set();
|
|
24
|
+
pending = new Map();
|
|
25
|
+
channel;
|
|
26
|
+
redisListener;
|
|
27
|
+
constructor(redisService) {
|
|
28
|
+
this.redisService = redisService;
|
|
29
|
+
}
|
|
30
|
+
get config() {
|
|
31
|
+
const sync = ConfigService.configFastButReadOnly?.sync;
|
|
32
|
+
const hints = sync && typeof sync === 'object' ? sync.hints : undefined;
|
|
33
|
+
if (hints === false) {
|
|
34
|
+
return { enabled: false };
|
|
35
|
+
}
|
|
36
|
+
return hints && typeof hints === 'object' ? hints : {};
|
|
37
|
+
}
|
|
38
|
+
get enabled() {
|
|
39
|
+
return this.config.enabled !== false;
|
|
40
|
+
}
|
|
41
|
+
get debounceMs() {
|
|
42
|
+
const value = this.config.debounceMs;
|
|
43
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : SYNC_DEFAULTS.hintDebounceMs;
|
|
44
|
+
}
|
|
45
|
+
async onApplicationBootstrap() {
|
|
46
|
+
if (!this.enabled || !this.redisService?.enabled) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const channel = this.redisService.key('sync', 'hints');
|
|
50
|
+
try {
|
|
51
|
+
this.channel = channel;
|
|
52
|
+
this.redisListener = (incoming, message) => {
|
|
53
|
+
if (incoming === channel) {
|
|
54
|
+
this.receive(message);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
this.redisService.getSubscriber().on('message', this.redisListener);
|
|
58
|
+
await this.redisService.subscribeChannel(channel);
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
this.logger.warn(`Sync hints: Redis subscribe failed (${error?.message ?? error}) — retried when the connection is ` +
|
|
62
|
+
"up; until then this replica's subscribers receive only its own hints");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
onModuleDestroy() {
|
|
66
|
+
for (const timer of this.pending.values()) {
|
|
67
|
+
clearTimeout(timer);
|
|
68
|
+
}
|
|
69
|
+
this.pending.clear();
|
|
70
|
+
this.subscribers.clear();
|
|
71
|
+
if (this.redisListener) {
|
|
72
|
+
try {
|
|
73
|
+
this.redisService?.getSubscriber().off('message', this.redisListener);
|
|
74
|
+
if (this.channel) {
|
|
75
|
+
void this.redisService?.releaseChannel(this.channel).catch(() => undefined);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
}
|
|
80
|
+
this.redisListener = undefined;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
notify(model, tenantIds = [undefined]) {
|
|
84
|
+
if (!this.enabled) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
for (const tenantId of tenantIds.length ? new Set(tenantIds) : [undefined]) {
|
|
88
|
+
const key = `${model}\u0000${tenantId ?? ''}`;
|
|
89
|
+
if (this.pending.has(key)) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const timer = setTimeout(() => {
|
|
93
|
+
this.pending.delete(key);
|
|
94
|
+
this.send({ at: Date.now(), model, ...(tenantId ? { tenantId } : {}) });
|
|
95
|
+
}, this.debounceMs);
|
|
96
|
+
timer.unref?.();
|
|
97
|
+
this.pending.set(key, timer);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
subscribe(handler) {
|
|
101
|
+
this.subscribers.add(handler);
|
|
102
|
+
return () => {
|
|
103
|
+
this.subscribers.delete(handler);
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
send(hint) {
|
|
107
|
+
this.deliver(hint);
|
|
108
|
+
if (!this.channel || !this.redisService?.enabled) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
void this.redisService
|
|
113
|
+
.getClient()
|
|
114
|
+
.publish(this.channel, JSON.stringify({ ...hint, origin: this.origin }))
|
|
115
|
+
.catch((error) => this.logger.debug(`Sync hint publish failed: ${error.message}`));
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
this.logger.debug(`Sync hint publish failed: ${error?.message ?? error}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
receive(message) {
|
|
122
|
+
let parsed;
|
|
123
|
+
try {
|
|
124
|
+
parsed = JSON.parse(message);
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (!parsed || parsed.origin === this.origin || typeof parsed.model !== 'string' || !Number.isFinite(parsed.at)) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
this.deliver({
|
|
133
|
+
at: Number(parsed.at),
|
|
134
|
+
model: parsed.model,
|
|
135
|
+
...(typeof parsed.tenantId === 'string' && parsed.tenantId ? { tenantId: parsed.tenantId } : {}),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
deliver(hint) {
|
|
139
|
+
for (const handler of this.subscribers) {
|
|
140
|
+
try {
|
|
141
|
+
handler(hint);
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
this.logger.warn(`Sync hint subscriber failed: ${error?.message ?? error}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
CoreSyncHintService = CoreSyncHintService_1 = __decorate([
|
|
150
|
+
Injectable(),
|
|
151
|
+
__param(0, Optional()),
|
|
152
|
+
__metadata("design:paramtypes", [CoreRedisService])
|
|
153
|
+
], CoreSyncHintService);
|
|
154
|
+
export { CoreSyncHintService };
|
|
155
|
+
//# sourceMappingURL=core-sync-hints.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core-sync-hints.service.js","sourceRoot":"","sources":["../../../../src/core/modules/sync/core-sync-hints.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,EAA2C,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AACvG,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AACxE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6CAA6C,CAAC;AAC/E,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAqBlD,IAAM,mBAAmB,2BAAzB,MAAM,mBAAmB;IAUa;IATxB,MAAM,GAAG,IAAI,MAAM,CAAC,qBAAmB,CAAC,IAAI,CAAC,CAAC;IAE9C,MAAM,GAAG,UAAU,EAAE,CAAC;IACtB,WAAW,GAAG,IAAI,GAAG,EAAiC,CAAC;IAEvD,OAAO,GAAG,IAAI,GAAG,EAAyC,CAAC;IACpE,OAAO,CAAU;IACjB,aAAa,CAA8C;IAErE,YAA2C,YAA+B;QAA/B,iBAAY,GAAZ,YAAY,CAAmB;IAAG,CAAC;IAG9E,IAAc,MAAM;QAClB,MAAM,IAAI,GAAG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;YAChB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,gCAAiC,KAAe,EAAE,OAAO,IAAI,KAAK,EAAE,CAAC,CAAC;YACzF,CAAC;QACH,CAAC;IACH,CAAC;CACF,CAAA;AAtJY,mBAAmB;IAD/B,UAAU,EAAE;IAWE,WAAA,QAAQ,EAAE,CAAA;qCAAmC,gBAAgB;GAV/D,mBAAmB,CAsJ/B"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createSchema } from '../../common/persistence/mongo/mongoose-compat.js';
|
|
2
|
+
export const SyncMutationSchema = createSchema({
|
|
3
|
+
attempt: { type: Number },
|
|
4
|
+
claimedAt: { type: Date },
|
|
5
|
+
code: { type: String },
|
|
6
|
+
expiresAt: { index: { expireAfterSeconds: 0 }, type: Date },
|
|
7
|
+
key: { index: { unique: true }, type: String },
|
|
8
|
+
state: { type: String },
|
|
9
|
+
status: { type: String },
|
|
10
|
+
version: { type: Number },
|
|
11
|
+
}, { collection: 'syncMutations', versionKey: false });
|
|
12
|
+
export const SyncStateSchema = createSchema({
|
|
13
|
+
epoch: { type: Number },
|
|
14
|
+
horizon: { type: Date },
|
|
15
|
+
key: { index: { unique: true }, type: String },
|
|
16
|
+
}, { collection: 'syncStates', versionKey: false });
|
|
17
|
+
//# sourceMappingURL=core-sync-infrastructure.models.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core-sync-infrastructure.models.js","sourceRoot":"","sources":["../../../../src/core/modules/sync/core-sync-infrastructure.models.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mDAAmD,CAAC;AAcjF,MAAM,CAAC,MAAM,kBAAkB,GAAG,YAAY,CAC5C;IACE,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;IACzB,SAAS,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE;IACzB,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;IACtB,SAAS,EAAE,EAAE,KAAK,EAAE,EAAE,kBAAkB,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE;IAC3D,GAAG,EAAE,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;IAC9C,KAAK,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;IACvB,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;IACxB,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;CAC1B,EACD,EAAE,UAAU,EAAE,eAAe,EAAE,UAAU,EAAE,KAAK,EAAE,CACnD,CAAC;AAMF,MAAM,CAAC,MAAM,eAAe,GAAG,YAAY,CACzC;IACE,KAAK,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;IACvB,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE;IACvB,GAAG,EAAE,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;CAC/C,EACD,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,KAAK,EAAE,CAChD,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { DataStore, StoredRecord } from '../../common/persistence/data-store.interface.js';
|
|
2
|
+
export interface SyncMutationOutcome {
|
|
3
|
+
code?: string;
|
|
4
|
+
status: string;
|
|
5
|
+
version?: number;
|
|
6
|
+
}
|
|
7
|
+
export type SyncMutationClaim = {
|
|
8
|
+
kind: 'busy';
|
|
9
|
+
} | {
|
|
10
|
+
kind: 'claimed';
|
|
11
|
+
} | {
|
|
12
|
+
kind: 'done';
|
|
13
|
+
outcome: SyncMutationOutcome;
|
|
14
|
+
};
|
|
15
|
+
interface MutationRecord {
|
|
16
|
+
attempt?: number;
|
|
17
|
+
claimedAt?: Date;
|
|
18
|
+
code?: null | string;
|
|
19
|
+
expiresAt?: Date;
|
|
20
|
+
key: string;
|
|
21
|
+
state?: string;
|
|
22
|
+
status?: null | string;
|
|
23
|
+
version?: null | number;
|
|
24
|
+
}
|
|
25
|
+
export declare class CoreSyncMutationStore {
|
|
26
|
+
protected readonly store: DataStore<MutationRecord>;
|
|
27
|
+
protected readonly options: {
|
|
28
|
+
claimTimeoutMs: number;
|
|
29
|
+
retentionMs: number;
|
|
30
|
+
};
|
|
31
|
+
constructor(store: DataStore<MutationRecord>, options: {
|
|
32
|
+
claimTimeoutMs: number;
|
|
33
|
+
retentionMs: number;
|
|
34
|
+
});
|
|
35
|
+
static key(model: string, userId: string, recordId: string, mutationId: string): string;
|
|
36
|
+
claim(key: string, now?: Date): Promise<SyncMutationClaim>;
|
|
37
|
+
complete(key: string, outcome: SyncMutationOutcome, now?: Date): Promise<void>;
|
|
38
|
+
release(key: string): Promise<void>;
|
|
39
|
+
protected pending(now: Date, attempt: number): Omit<MutationRecord, 'key'>;
|
|
40
|
+
protected takeOver(existing: StoredRecord<MutationRecord>, now: Date): Promise<boolean>;
|
|
41
|
+
}
|
|
42
|
+
export declare function isDuplicateKey(error: unknown): boolean;
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
export class CoreSyncMutationStore {
|
|
3
|
+
store;
|
|
4
|
+
options;
|
|
5
|
+
constructor(store, options) {
|
|
6
|
+
this.store = store;
|
|
7
|
+
this.options = options;
|
|
8
|
+
}
|
|
9
|
+
static key(model, userId, recordId, mutationId) {
|
|
10
|
+
return createHash('sha256').update([model, userId, recordId, mutationId].join('\u0000')).digest('hex');
|
|
11
|
+
}
|
|
12
|
+
async claim(key, now = new Date()) {
|
|
13
|
+
for (let round = 0; round < 3; round++) {
|
|
14
|
+
const existing = await this.store.findOne({ key });
|
|
15
|
+
if (!existing) {
|
|
16
|
+
try {
|
|
17
|
+
await this.store.insert({ key, ...this.pending(now, 1) });
|
|
18
|
+
return { kind: 'claimed' };
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
if (!isDuplicateKey(error)) {
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (isExpired(existing, now)) {
|
|
28
|
+
if (await this.takeOver(existing, now)) {
|
|
29
|
+
return { kind: 'claimed' };
|
|
30
|
+
}
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (existing.state === 'done' && existing.status) {
|
|
34
|
+
return { kind: 'done', outcome: outcomeOf(existing) };
|
|
35
|
+
}
|
|
36
|
+
const claimedAt = existing.claimedAt ? new Date(existing.claimedAt).getTime() : 0;
|
|
37
|
+
if (now.getTime() - claimedAt < this.options.claimTimeoutMs) {
|
|
38
|
+
return { kind: 'busy' };
|
|
39
|
+
}
|
|
40
|
+
return (await this.takeOver(existing, now)) ? { kind: 'claimed' } : { kind: 'busy' };
|
|
41
|
+
}
|
|
42
|
+
return { kind: 'busy' };
|
|
43
|
+
}
|
|
44
|
+
async complete(key, outcome, now = new Date()) {
|
|
45
|
+
await this.store.updateOne({ key }, {
|
|
46
|
+
$set: {
|
|
47
|
+
code: outcome.code ?? null,
|
|
48
|
+
expiresAt: new Date(now.getTime() + this.options.retentionMs),
|
|
49
|
+
state: 'done',
|
|
50
|
+
status: outcome.status,
|
|
51
|
+
version: outcome.version ?? null,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
async release(key) {
|
|
56
|
+
await this.store.deleteOne({ key, state: 'pending' });
|
|
57
|
+
}
|
|
58
|
+
pending(now, attempt) {
|
|
59
|
+
return {
|
|
60
|
+
attempt,
|
|
61
|
+
claimedAt: now,
|
|
62
|
+
code: null,
|
|
63
|
+
expiresAt: new Date(now.getTime() + this.options.retentionMs),
|
|
64
|
+
state: 'pending',
|
|
65
|
+
status: null,
|
|
66
|
+
version: null,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
async takeOver(existing, now) {
|
|
70
|
+
const attempt = existing.attempt ?? 0;
|
|
71
|
+
const taken = await this.store.updateOne({ attempt: existing.attempt ?? null, id: existing.id }, { $set: this.pending(now, attempt + 1) });
|
|
72
|
+
return !!taken;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function isExpired(record, now) {
|
|
76
|
+
return !!record.expiresAt && new Date(record.expiresAt).getTime() <= now.getTime();
|
|
77
|
+
}
|
|
78
|
+
function outcomeOf(record) {
|
|
79
|
+
return {
|
|
80
|
+
...(record.code ? { code: record.code } : {}),
|
|
81
|
+
status: record.status,
|
|
82
|
+
...(typeof record.version === 'number' ? { version: record.version } : {}),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
export function isDuplicateKey(error) {
|
|
86
|
+
return error?.code === 11000;
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=core-sync-mutation.store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core-sync-mutation.store.js","sourceRoot":"","sources":["../../../../src/core/modules/sync/core-sync-mutation.store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAuCzC,MAAM,OAAO,qBAAqB;IAEX;IACA;IAFrB,YACqB,KAAgC,EAChC,OAAwD;QADxD,UAAK,GAAL,KAAK,CAA2B;QAChC,YAAO,GAAP,OAAO,CAAiD;IAC1E,CAAC;IAMJ,MAAM,CAAC,GAAG,CAAC,KAAa,EAAE,MAAc,EAAE,QAAgB,EAAE,UAAkB;QAC5E,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACzG,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,GAAW,EAAE,GAAG,GAAG,IAAI,IAAI,EAAE;QAEvC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Record {
|
|
3
|
+
epoch?: null | number;
|
|
4
|
+
horizon?: Date | null;
|
|
5
|
+
key: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class CoreSyncStateStore {
|
|
8
|
+
protected readonly store: DataStore<StateRecord>;
|
|
9
|
+
constructor(store: DataStore<StateRecord>);
|
|
10
|
+
static tenantEpochKey(model: string, tenantId: string): string;
|
|
11
|
+
static userEpochKey(model: string, tenantId: string, userId: string): string;
|
|
12
|
+
static horizonKey(model: string): string;
|
|
13
|
+
epochs(keys: readonly string[]): Promise<Map<string, number>>;
|
|
14
|
+
bump(key: string): Promise<void>;
|
|
15
|
+
horizon(model: string): Promise<Date | undefined>;
|
|
16
|
+
advanceHorizon(model: string, at: Date): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { isDuplicateKey } from './core-sync-mutation.store.js';
|
|
2
|
+
export class CoreSyncStateStore {
|
|
3
|
+
store;
|
|
4
|
+
constructor(store) {
|
|
5
|
+
this.store = store;
|
|
6
|
+
}
|
|
7
|
+
static tenantEpochKey(model, tenantId) {
|
|
8
|
+
return `epoch:t:${model}:${tenantId}`;
|
|
9
|
+
}
|
|
10
|
+
static userEpochKey(model, tenantId, userId) {
|
|
11
|
+
return `epoch:u:${model}:${tenantId}:${userId}`;
|
|
12
|
+
}
|
|
13
|
+
static horizonKey(model) {
|
|
14
|
+
return `horizon:${model}`;
|
|
15
|
+
}
|
|
16
|
+
async epochs(keys) {
|
|
17
|
+
const result = new Map(keys.map((key) => [key, 0]));
|
|
18
|
+
if (!keys.length) {
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
for (const row of await this.store.find({ key: { $in: [...keys] } }, { select: ['epoch', 'key'] })) {
|
|
22
|
+
result.set(row.key, typeof row.epoch === 'number' ? row.epoch : 0);
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
async bump(key) {
|
|
27
|
+
if (await this.store.updateOne({ key }, { $inc: { epoch: 1 } })) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
await this.store.insert({ epoch: 1, key });
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
if (!isDuplicateKey(error)) {
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
await this.store.updateOne({ key }, { $inc: { epoch: 1 } });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async horizon(model) {
|
|
41
|
+
const row = await this.store.findOne({ key: CoreSyncStateStore.horizonKey(model) });
|
|
42
|
+
return row?.horizon ? new Date(row.horizon) : undefined;
|
|
43
|
+
}
|
|
44
|
+
async advanceHorizon(model, at) {
|
|
45
|
+
const key = CoreSyncStateStore.horizonKey(model);
|
|
46
|
+
const behind = { $or: [{ horizon: null }, { horizon: { $lt: at } }], key };
|
|
47
|
+
if (await this.store.updateOne(behind, { $set: { horizon: at } })) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (await this.store.findOne({ key })) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
await this.store.insert({ horizon: at, key });
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
if (!isDuplicateKey(error)) {
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
await this.store.updateOne(behind, { $set: { horizon: at } });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=core-sync-state.store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core-sync-state.store.js","sourceRoot":"","sources":["../../../../src/core/modules/sync/core-sync-state.store.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAoB/D,MAAM,OAAO,kBAAkB;IACE;IAA/B,YAA+B,KAA6B;QAA7B,UAAK,GAAL,KAAK,CAAwB;IAA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O,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC;YACtC,OAAO;QACT,CAAC;QACD,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;QAChD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,EAAE,CAAC;gBAC3B,MAAM,KAAK,CAAC;YACd,CAAC;YACD,MAAM,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;QAChE,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export declare const SYNC_MUTATION_MODEL = "SyncMutation";
|
|
2
|
+
export declare const SYNC_STATE_MODEL = "SyncState";
|
|
3
|
+
export declare const SYNC_SERVICE_METADATA = "lt:sync:service";
|
|
4
|
+
export declare const SYNC_FIELD_METADATA = "lt:sync:fields";
|
|
5
|
+
export declare const SYNC_CLAIM_TIMEOUT_MS = 30000;
|
|
6
|
+
export declare const SYNC_REAPPLY_ATTEMPTS = 3;
|
|
7
|
+
export declare const SYNC_DEFAULTS: Readonly<{
|
|
8
|
+
hintDebounceMs: 250;
|
|
9
|
+
maxBytes: 1048576;
|
|
10
|
+
maxItems: 100;
|
|
11
|
+
maxLimit: 1000;
|
|
12
|
+
mutationsDays: 30;
|
|
13
|
+
path: "sync";
|
|
14
|
+
pullLimit: 200;
|
|
15
|
+
rateLimitPull: {
|
|
16
|
+
max: number;
|
|
17
|
+
windowSeconds: number;
|
|
18
|
+
};
|
|
19
|
+
rateLimitPush: {
|
|
20
|
+
max: number;
|
|
21
|
+
windowSeconds: number;
|
|
22
|
+
};
|
|
23
|
+
tombstonesDays: 90;
|
|
24
|
+
}>;
|
|
25
|
+
export declare const SYNC_PURGE_BATCH = 1000;
|
|
26
|
+
export declare const SYNC_PURGE_INTERVAL_MS: number;
|
|
27
|
+
export declare const SYNC_HINT_KEEPALIVE_MS = 25000;
|
|
28
|
+
export declare const SYNC_PULL_LIMIT_CEILING = 1000;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const SYNC_MUTATION_MODEL = 'SyncMutation';
|
|
2
|
+
export const SYNC_STATE_MODEL = 'SyncState';
|
|
3
|
+
export const SYNC_SERVICE_METADATA = 'lt:sync:service';
|
|
4
|
+
export const SYNC_FIELD_METADATA = 'lt:sync:fields';
|
|
5
|
+
export const SYNC_CLAIM_TIMEOUT_MS = 30_000;
|
|
6
|
+
export const SYNC_REAPPLY_ATTEMPTS = 3;
|
|
7
|
+
export const SYNC_DEFAULTS = Object.freeze({
|
|
8
|
+
hintDebounceMs: 250,
|
|
9
|
+
maxBytes: 1_048_576,
|
|
10
|
+
maxItems: 100,
|
|
11
|
+
maxLimit: 1000,
|
|
12
|
+
mutationsDays: 30,
|
|
13
|
+
path: 'sync',
|
|
14
|
+
pullLimit: 200,
|
|
15
|
+
rateLimitPull: { max: 120, windowSeconds: 60 },
|
|
16
|
+
rateLimitPush: { max: 60, windowSeconds: 60 },
|
|
17
|
+
tombstonesDays: 90,
|
|
18
|
+
});
|
|
19
|
+
export const SYNC_PURGE_BATCH = 1000;
|
|
20
|
+
export const SYNC_PURGE_INTERVAL_MS = 60 * 60_000;
|
|
21
|
+
export const SYNC_HINT_KEEPALIVE_MS = 25_000;
|
|
22
|
+
export const SYNC_PULL_LIMIT_CEILING = 1000;
|
|
23
|
+
//# sourceMappingURL=core-sync.constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core-sync.constants.js","sourceRoot":"","sources":["../../../../src/core/modules/sync/core-sync.constants.ts"],"names":[],"mappings":"AAMA,MAAM,CAAC,MAAM,mBAAmB,GAAG,cAAc,CAAC;AAGlD,MAAM,CAAC,MAAM,gBAAgB,GAAG,WAAW,CAAC;AAG5C,MAAM,CAAC,MAAM,qBAAqB,GAAG,iBAAiB,CAAC;AAGvD,MAAM,CAAC,MAAM,mBAAmB,GAAG,gBAAgB,CAAC;AAMpD,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CAAC;AAG5C,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,CAAC;AAGvC,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC;IACzC,cAAc,EAAE,GAAG;IACnB,QAAQ,EAAE,SAAS;IACnB,QAAQ,EAAE,GAAG;IACb,QAAQ,EAAE,IAAI;IACd,aAAa,EAAE,EAAE;IACjB,IAAI,EAAE,MAAM;IACZ,SAAS,EAAE,GAAG;IACd,aAAa,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,aAAa,EAAE,EAAE,EAAE;IAC9C,aAAa,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,aAAa,EAAE,EAAE,EAAE;IAC7C,cAAc,EAAE,EAAE;CACnB,CAAC,CAAC;AAIH,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAErC,MAAM,CAAC,MAAM,sBAAsB,GAAG,EAAE,GAAG,MAAM,CAAC;AAGlD,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAAC;AAG7C,MAAM,CAAC,MAAM,uBAAuB,GAAG,IAAI,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CoreSyncBaseController } from './core-sync-base.controller.js';
|
|
2
|
+
import type { ISyncPullResult, ISyncPushResult } from './interfaces/core-sync.interfaces.js';
|
|
3
|
+
export declare class CoreSyncController extends CoreSyncBaseController {
|
|
4
|
+
pull(model: string, cursor: string, limit: string, currentUser: any, req: any, res: any): Promise<ISyncPullResult | object>;
|
|
5
|
+
push(model: string, body: any, currentUser: any, req: any, res: any): Promise<{
|
|
6
|
+
results: ISyncPushResult[];
|
|
7
|
+
}>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
+
};
|
|
13
|
+
import { Body, Controller, Get, HttpCode, HttpStatus, Param, Post, Query, Req, Res } from '@nestjs/common';
|
|
14
|
+
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
|
15
|
+
import { CurrentUser } from '../../common/decorators/current-user.decorator.js';
|
|
16
|
+
import { Roles } from '../../common/decorators/roles.decorator.js';
|
|
17
|
+
import { RoleEnum } from '../../common/enums/role.enum.js';
|
|
18
|
+
import { CoreSyncBaseController } from './core-sync-base.controller.js';
|
|
19
|
+
import { SYNC_DEFAULTS } from './core-sync.constants.js';
|
|
20
|
+
import { SyncResyncRequiredException } from './core-sync.service.js';
|
|
21
|
+
let CoreSyncController = class CoreSyncController extends CoreSyncBaseController {
|
|
22
|
+
async pull(model, cursor, limit, currentUser, req, res) {
|
|
23
|
+
await this.limit('pull', currentUser, res);
|
|
24
|
+
await this.scopeTenants(req, currentUser);
|
|
25
|
+
try {
|
|
26
|
+
return await this.syncService.pull(model, { cursor, limit }, currentUser);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (error instanceof SyncResyncRequiredException) {
|
|
30
|
+
res?.status?.(error.getStatus());
|
|
31
|
+
return error.getResponse();
|
|
32
|
+
}
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async push(model, body, currentUser, req, res) {
|
|
37
|
+
this.syncService.assertPushRequest(req);
|
|
38
|
+
await this.limit('push', currentUser, res);
|
|
39
|
+
await this.scopeTenants(req, currentUser);
|
|
40
|
+
return this.syncService.push(model, body, currentUser);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
__decorate([
|
|
44
|
+
ApiBearerAuth(),
|
|
45
|
+
ApiOperation({ description: 'One page of changes after the cursor (protocol §5)', summary: 'Pull changes' }),
|
|
46
|
+
Get(':model'),
|
|
47
|
+
Roles(RoleEnum.S_USER),
|
|
48
|
+
__param(0, Param('model')),
|
|
49
|
+
__param(1, Query('cursor')),
|
|
50
|
+
__param(2, Query('limit')),
|
|
51
|
+
__param(3, CurrentUser()),
|
|
52
|
+
__param(4, Req()),
|
|
53
|
+
__param(5, Res({ passthrough: true })),
|
|
54
|
+
__metadata("design:type", Function),
|
|
55
|
+
__metadata("design:paramtypes", [String, String, String, Object, Object, Object]),
|
|
56
|
+
__metadata("design:returntype", Promise)
|
|
57
|
+
], CoreSyncController.prototype, "pull", null);
|
|
58
|
+
__decorate([
|
|
59
|
+
ApiBearerAuth(),
|
|
60
|
+
ApiOperation({ description: 'Apply pushed changes item by item (protocol §6)', summary: 'Push changes' }),
|
|
61
|
+
HttpCode(HttpStatus.OK),
|
|
62
|
+
Post(':model'),
|
|
63
|
+
Roles(RoleEnum.S_USER),
|
|
64
|
+
__param(0, Param('model')),
|
|
65
|
+
__param(1, Body()),
|
|
66
|
+
__param(2, CurrentUser()),
|
|
67
|
+
__param(3, Req()),
|
|
68
|
+
__param(4, Res({ passthrough: true })),
|
|
69
|
+
__metadata("design:type", Function),
|
|
70
|
+
__metadata("design:paramtypes", [String, Object, Object, Object, Object]),
|
|
71
|
+
__metadata("design:returntype", Promise)
|
|
72
|
+
], CoreSyncController.prototype, "push", null);
|
|
73
|
+
CoreSyncController = __decorate([
|
|
74
|
+
ApiTags('sync'),
|
|
75
|
+
Controller(SYNC_DEFAULTS.path),
|
|
76
|
+
Roles(RoleEnum.S_USER)
|
|
77
|
+
], CoreSyncController);
|
|
78
|
+
export { CoreSyncController };
|
|
79
|
+
//# sourceMappingURL=core-sync.controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core-sync.controller.js","sourceRoot":"","sources":["../../../../src/core/modules/sync/core-sync.controller.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,GAAG,EAAE,QAAQ,EAAE,UAAU,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAC3G,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAEvE,OAAO,EAAE,WAAW,EAAE,MAAM,mDAAmD,CAAC;AAChF,OAAO,EAAE,KAAK,EAAE,MAAM,4CAA4C,CAAC;AACnE,OAAO,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAC;AAC3D,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AACxE,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,2BAA2B,EAAE,MAAM,wBAAwB,CAAC;AAe9D,IAAM,kBAAkB,GAAxB,MAAM,kBAAmB,SAAQ,sBAAsB;IAKtD,AAAN,KAAK,CAAC,IAAI,CACQ,KAAa,EACZ,MAAc,EACf,KAAa,EACd,WAAgB,EACxB,GAAQ,EACa,GAAQ;QAEpC,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,WAAW,EAAE,GAAG,CAAC,CAAC;QAC3C,MAAM,IA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L,WAAA,GAAG,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CAAA;;;;8CAc5B;AAOK;IALL,aAAa,EAAE;IACf,YAAY,CAAC,EAAE,WAAW,EAAE,iDAAiD,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC;IACzG,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;IACvB,IAAI,CAAC,QAAQ,CAAC;IACd,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC;IAEpB,WAAA,KAAK,CAAC,OAAO,CAAC,CAAA;IACd,WAAA,IAAI,EAAE,CAAA;IACN,WAAA,WAAW,EAAE,CAAA;IACb,WAAA,GAAG,EAAE,CAAA;IACL,WAAA,GAAG,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CAAA;;;;8CAM5B;AA3CU,kBAAkB;IAH9B,OAAO,CAAC,MAAM,CAAC;IACf,UAAU,CAAC,aAAa,CAAC,IAAI,CAAC;IAC9B,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC;GACV,kBAAkB,CA4C9B"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Type } from '@nestjs/common';
|
|
2
|
+
export interface SyncServiceOptions {
|
|
3
|
+
createInput: Type<any>;
|
|
4
|
+
model: Type<any>;
|
|
5
|
+
modelName?: string;
|
|
6
|
+
updateInput: Type<any>;
|
|
7
|
+
}
|
|
8
|
+
export declare function SyncService(options: SyncServiceOptions): ClassDecorator;
|
|
9
|
+
export declare function getSyncServiceOptions(target: Function): SyncServiceOptions | undefined;
|
|
10
|
+
export type SyncFieldStrategy = 'lww' | 'reject';
|
|
11
|
+
export declare function SyncField(strategy: SyncFieldStrategy): PropertyDecorator;
|
|
12
|
+
export declare function getSyncFieldStrategies(modelClass: Function): Record<string, SyncFieldStrategy>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { SYNC_FIELD_METADATA, SYNC_SERVICE_METADATA } from './core-sync.constants.js';
|
|
2
|
+
export function SyncService(options) {
|
|
3
|
+
return (target) => {
|
|
4
|
+
Reflect.defineMetadata(SYNC_SERVICE_METADATA, { ...options }, target);
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
export function getSyncServiceOptions(target) {
|
|
8
|
+
return Reflect.getMetadata(SYNC_SERVICE_METADATA, target);
|
|
9
|
+
}
|
|
10
|
+
export function SyncField(strategy) {
|
|
11
|
+
if (strategy !== 'lww' && strategy !== 'reject') {
|
|
12
|
+
throw new Error(`@SyncField: strategy must be 'lww' or 'reject', got ${JSON.stringify(strategy)}`);
|
|
13
|
+
}
|
|
14
|
+
return (target, property) => {
|
|
15
|
+
const own = Reflect.hasOwnMetadata(SYNC_FIELD_METADATA, target.constructor)
|
|
16
|
+
? Reflect.getOwnMetadata(SYNC_FIELD_METADATA, target.constructor)
|
|
17
|
+
: {};
|
|
18
|
+
Reflect.defineMetadata(SYNC_FIELD_METADATA, { ...own, [String(property)]: strategy }, target.constructor);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export function getSyncFieldStrategies(modelClass) {
|
|
22
|
+
const chain = [];
|
|
23
|
+
for (let current = modelClass; current && current !== Function.prototype; current = Object.getPrototypeOf(current)) {
|
|
24
|
+
chain.unshift(current);
|
|
25
|
+
}
|
|
26
|
+
return Object.assign({}, ...chain.map((current) => Reflect.hasOwnMetadata(SYNC_FIELD_METADATA, current) ? Reflect.getOwnMetadata(SYNC_FIELD_METADATA, current) : {}));
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=core-sync.decorators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core-sync.decorators.js","sourceRoot":"","sources":["../../../../src/core/modules/sync/core-sync.decorators.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AA8BtF,MAAM,UAAU,WAAW,CAAC,OAA2B;IACrD,OAAO,CAAC,MAAM,EAAE,EAAE;QAChB,OAAO,CAAC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C,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CACvB,OAAO,CAAC,cAAc,CAAC,mBAAmB,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,cAAc,CAAC,mBAAmB,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CACjH,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { DynamicModule, Type } from '@nestjs/common';
|
|
2
|
+
import { CoreSyncController } from './core-sync.controller.js';
|
|
3
|
+
import { CoreSyncHintsController } from './core-sync-hints.controller.js';
|
|
4
|
+
import { CoreSyncResolver } from './core-sync.resolver.js';
|
|
5
|
+
import { CoreSyncService } from './core-sync.service.js';
|
|
6
|
+
export interface CoreSyncModuleOptions {
|
|
7
|
+
controller?: Type<CoreSyncController>;
|
|
8
|
+
graphQlHints?: boolean;
|
|
9
|
+
hintsController?: Type<CoreSyncHintsController>;
|
|
10
|
+
path?: string;
|
|
11
|
+
resolver?: Type<CoreSyncResolver>;
|
|
12
|
+
service?: Type<CoreSyncService>;
|
|
13
|
+
}
|
|
14
|
+
export declare class CoreSyncModule {
|
|
15
|
+
static forRoot(options?: CoreSyncModuleOptions): DynamicModule;
|
|
16
|
+
}
|