@syncular/server 0.0.1-72 → 0.0.1-73
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/dist/dialect/base.d.ts +99 -0
- package/dist/dialect/base.d.ts.map +1 -0
- package/dist/dialect/base.js +112 -0
- package/dist/dialect/base.js.map +1 -0
- package/dist/dialect/helpers.d.ts +10 -0
- package/dist/dialect/helpers.d.ts.map +1 -0
- package/dist/dialect/helpers.js +59 -0
- package/dist/dialect/helpers.js.map +1 -0
- package/dist/dialect/index.d.ts +2 -0
- package/dist/dialect/index.d.ts.map +1 -1
- package/dist/dialect/index.js +2 -0
- package/dist/dialect/index.js.map +1 -1
- package/package.json +1 -1
- package/src/dialect/base.ts +262 -0
- package/src/dialect/helpers.ts +61 -0
- package/src/dialect/index.ts +2 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @syncular/server - Base Server Sync Dialect
|
|
3
|
+
*
|
|
4
|
+
* Abstract base class that implements shared query methods for all
|
|
5
|
+
* database-specific sync dialect implementations.
|
|
6
|
+
*/
|
|
7
|
+
import type { ScopeValues, StoredScopes, SyncOp } from '@syncular/core';
|
|
8
|
+
import type { Kysely, RawBuilder, Transaction } from 'kysely';
|
|
9
|
+
import type { SyncChangeRow, SyncCommitRow, SyncCoreDb } from '../schema';
|
|
10
|
+
import type { DbExecutor, ServerSyncDialect } from './types';
|
|
11
|
+
/**
|
|
12
|
+
* Abstract base class for server sync dialects.
|
|
13
|
+
*
|
|
14
|
+
* Implements methods that are identical across dialects (pure SQL with no
|
|
15
|
+
* dialect-specific syntax) and methods that differ only in trivial SQL
|
|
16
|
+
* fragments (IN vs ANY, jsonb casts). Dialect-specific fragments are
|
|
17
|
+
* provided via small abstract hook methods.
|
|
18
|
+
*
|
|
19
|
+
* Genuinely different methods (DDL, transaction control, scope filtering,
|
|
20
|
+
* compaction, streaming) remain abstract for each dialect to implement.
|
|
21
|
+
*/
|
|
22
|
+
export declare abstract class BaseServerSyncDialect implements ServerSyncDialect {
|
|
23
|
+
abstract readonly name: string;
|
|
24
|
+
abstract readonly supportsForUpdate: boolean;
|
|
25
|
+
abstract readonly supportsSavepoints: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Build a SQL fragment for "column IN/= list of numbers".
|
|
28
|
+
* SQLite: `IN (1, 2, 3)` via sql.join
|
|
29
|
+
* Postgres: `= ANY(ARRAY[1,2,3]::bigint[])`
|
|
30
|
+
*/
|
|
31
|
+
protected abstract buildNumberListFilter(values: number[]): RawBuilder<unknown>;
|
|
32
|
+
/**
|
|
33
|
+
* Build a SQL fragment for "column IN/= list of strings".
|
|
34
|
+
* SQLite: `IN ('a', 'b')` via sql.join
|
|
35
|
+
* Postgres: `= ANY(ARRAY['a','b']::text[])`
|
|
36
|
+
*/
|
|
37
|
+
protected abstract buildStringListFilter(values: string[]): RawBuilder<unknown>;
|
|
38
|
+
abstract ensureSyncSchema<DB extends SyncCoreDb>(db: Kysely<DB>): Promise<void>;
|
|
39
|
+
abstract ensureConsoleSchema?<DB extends SyncCoreDb>(db: Kysely<DB>): Promise<void>;
|
|
40
|
+
abstract executeInTransaction<DB extends SyncCoreDb, T>(db: Kysely<DB>, fn: (executor: DbExecutor<DB>) => Promise<T>): Promise<T>;
|
|
41
|
+
abstract setRepeatableRead<DB extends SyncCoreDb>(trx: DbExecutor<DB>): Promise<void>;
|
|
42
|
+
abstract readChangesForCommits<DB extends SyncCoreDb>(db: DbExecutor<DB>, args: {
|
|
43
|
+
commitSeqs: number[];
|
|
44
|
+
table: string;
|
|
45
|
+
scopes: ScopeValues;
|
|
46
|
+
partitionId?: string;
|
|
47
|
+
}): Promise<SyncChangeRow[]>;
|
|
48
|
+
abstract readIncrementalPullRows<DB extends SyncCoreDb>(db: DbExecutor<DB>, args: {
|
|
49
|
+
table: string;
|
|
50
|
+
scopes: ScopeValues;
|
|
51
|
+
cursor: number;
|
|
52
|
+
limitCommits: number;
|
|
53
|
+
partitionId?: string;
|
|
54
|
+
}): Promise<Array<{
|
|
55
|
+
commit_seq: number;
|
|
56
|
+
actor_id: string;
|
|
57
|
+
created_at: string;
|
|
58
|
+
change_id: number;
|
|
59
|
+
table: string;
|
|
60
|
+
row_id: string;
|
|
61
|
+
op: SyncOp;
|
|
62
|
+
row_json: unknown | null;
|
|
63
|
+
row_version: number | null;
|
|
64
|
+
scopes: StoredScopes;
|
|
65
|
+
}>>;
|
|
66
|
+
abstract compactChanges<DB extends SyncCoreDb>(db: DbExecutor<DB>, args: {
|
|
67
|
+
fullHistoryHours: number;
|
|
68
|
+
}): Promise<number>;
|
|
69
|
+
abstract scopesToDb(scopes: StoredScopes): unknown;
|
|
70
|
+
abstract dbToArray(value: unknown): string[];
|
|
71
|
+
abstract arrayToDb(values: string[]): unknown;
|
|
72
|
+
readMaxCommitSeq<DB extends SyncCoreDb>(db: Kysely<DB> | Transaction<DB>, options?: {
|
|
73
|
+
partitionId?: string;
|
|
74
|
+
}): Promise<number>;
|
|
75
|
+
readMinCommitSeq<DB extends SyncCoreDb>(db: Kysely<DB> | Transaction<DB>, options?: {
|
|
76
|
+
partitionId?: string;
|
|
77
|
+
}): Promise<number>;
|
|
78
|
+
readAffectedTablesFromChanges<DB extends SyncCoreDb>(db: Kysely<DB> | Transaction<DB>, commitSeq: number, options?: {
|
|
79
|
+
partitionId?: string;
|
|
80
|
+
}): Promise<string[]>;
|
|
81
|
+
dbToScopes(value: unknown): StoredScopes;
|
|
82
|
+
readCommitSeqsForPull<DB extends SyncCoreDb>(db: Kysely<DB> | Transaction<DB>, args: {
|
|
83
|
+
cursor: number;
|
|
84
|
+
limitCommits: number;
|
|
85
|
+
tables: string[];
|
|
86
|
+
partitionId?: string;
|
|
87
|
+
}): Promise<number[]>;
|
|
88
|
+
readCommits<DB extends SyncCoreDb>(db: Kysely<DB> | Transaction<DB>, commitSeqs: number[], options?: {
|
|
89
|
+
partitionId?: string;
|
|
90
|
+
}): Promise<SyncCommitRow[]>;
|
|
91
|
+
recordClientCursor<DB extends SyncCoreDb>(db: Kysely<DB> | Transaction<DB>, args: {
|
|
92
|
+
partitionId?: string;
|
|
93
|
+
clientId: string;
|
|
94
|
+
actorId: string;
|
|
95
|
+
cursor: number;
|
|
96
|
+
effectiveScopes: ScopeValues;
|
|
97
|
+
}): Promise<void>;
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/dialect/base.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxE,OAAO,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAE9D,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAE1E,OAAO,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAE7D;;;;;;;;;;GAUG;AACH,8BAAsB,qBAAsB,YAAW,iBAAiB;IACtE,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,QAAQ,CAAC,iBAAiB,EAAE,OAAO,CAAC;IAC7C,QAAQ,CAAC,QAAQ,CAAC,kBAAkB,EAAE,OAAO,CAAC;IAM9C;;;;OAIG;IACH,SAAS,CAAC,QAAQ,CAAC,qBAAqB,CACtC,MAAM,EAAE,MAAM,EAAE,GACf,UAAU,CAAC,OAAO,CAAC,CAAC;IAEvB;;;;OAIG;IACH,SAAS,CAAC,QAAQ,CAAC,qBAAqB,CACtC,MAAM,EAAE,MAAM,EAAE,GACf,UAAU,CAAC,OAAO,CAAC,CAAC;IAMvB,QAAQ,CAAC,gBAAgB,CAAC,EAAE,SAAS,UAAU,EAC7C,EAAE,EAAE,MA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EACjB,OAAO,CAAC,EAAE;QAAE,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,GACjC,OAAO,CAAC,MAAM,EAAE,CAAC,CAYnB;IAED,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,YAAY,CAEvC;IAMK,qBAAqB,CAAC,EAAE,SAAS,UAAU,EAC/C,EAAE,EAAE,MAAM,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC,EAAE,CAAC,EAChC,IAAI,EAAE;QACJ,MAAM,EAAE,MAAM,CAAC;QACf,YAAY,EAAE,MAAM,CAAC;QACrB,MAAM,EAAE,MAAM,EAAE,CAAC;QACjB,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB,GACA,OAAO,CAAC,MAAM,EAAE,CAAC,CAsBnB;IAEK,WAAW,CAAC,EAAE,SAAS,UAAU,EACrC,EAAE,EAAE,MAAM,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC,EAAE,CAAC,EAChC,UAAU,EAAE,MAAM,EAAE,EACpB,OAAO,CAAC,EAAE;QAAE,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,GACjC,OAAO,CAAC,aAAa,EAAE,CAAC,CAyB1B;IAEK,kBAAkB,CAAC,EAAE,SAAS,UAAU,EAC5C,EAAE,EAAE,MAAM,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC,EAAE,CAAC,EAChC,IAAI,EAAE;QACJ,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,QAAQ,EAAE,MAAM,CAAC;QACjB,OAAO,EAAE,MAAM,CAAC;QAChB,MAAM,EAAE,MAAM,CAAC;QACf,eAAe,EAAE,WAAW,CAAC;KAC9B,GACA,OAAO,CAAC,IAAI,CAAC,CAcf;CACF"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @syncular/server - Base Server Sync Dialect
|
|
3
|
+
*
|
|
4
|
+
* Abstract base class that implements shared query methods for all
|
|
5
|
+
* database-specific sync dialect implementations.
|
|
6
|
+
*/
|
|
7
|
+
import { sql } from 'kysely';
|
|
8
|
+
import { coerceIsoString, coerceNumber, parseScopes } from './helpers';
|
|
9
|
+
/**
|
|
10
|
+
* Abstract base class for server sync dialects.
|
|
11
|
+
*
|
|
12
|
+
* Implements methods that are identical across dialects (pure SQL with no
|
|
13
|
+
* dialect-specific syntax) and methods that differ only in trivial SQL
|
|
14
|
+
* fragments (IN vs ANY, jsonb casts). Dialect-specific fragments are
|
|
15
|
+
* provided via small abstract hook methods.
|
|
16
|
+
*
|
|
17
|
+
* Genuinely different methods (DDL, transaction control, scope filtering,
|
|
18
|
+
* compaction, streaming) remain abstract for each dialect to implement.
|
|
19
|
+
*/
|
|
20
|
+
export class BaseServerSyncDialect {
|
|
21
|
+
// ===========================================================================
|
|
22
|
+
// Concrete methods (identical SQL across dialects)
|
|
23
|
+
// ===========================================================================
|
|
24
|
+
async readMaxCommitSeq(db, options) {
|
|
25
|
+
const partitionId = options?.partitionId ?? 'default';
|
|
26
|
+
const res = await sql `
|
|
27
|
+
SELECT max(commit_seq) as max_seq
|
|
28
|
+
FROM sync_commits
|
|
29
|
+
WHERE partition_id = ${partitionId}
|
|
30
|
+
`.execute(db);
|
|
31
|
+
return coerceNumber(res.rows[0]?.max_seq) ?? 0;
|
|
32
|
+
}
|
|
33
|
+
async readMinCommitSeq(db, options) {
|
|
34
|
+
const partitionId = options?.partitionId ?? 'default';
|
|
35
|
+
const res = await sql `
|
|
36
|
+
SELECT min(commit_seq) as min_seq
|
|
37
|
+
FROM sync_commits
|
|
38
|
+
WHERE partition_id = ${partitionId}
|
|
39
|
+
`.execute(db);
|
|
40
|
+
return coerceNumber(res.rows[0]?.min_seq) ?? 0;
|
|
41
|
+
}
|
|
42
|
+
async readAffectedTablesFromChanges(db, commitSeq, options) {
|
|
43
|
+
const partitionId = options?.partitionId ?? 'default';
|
|
44
|
+
const res = await sql `
|
|
45
|
+
SELECT DISTINCT "table"
|
|
46
|
+
FROM sync_changes
|
|
47
|
+
WHERE commit_seq = ${commitSeq}
|
|
48
|
+
AND partition_id = ${partitionId}
|
|
49
|
+
`.execute(db);
|
|
50
|
+
return res.rows
|
|
51
|
+
.map((r) => r.table)
|
|
52
|
+
.filter((t) => typeof t === 'string' && t.length > 0);
|
|
53
|
+
}
|
|
54
|
+
dbToScopes(value) {
|
|
55
|
+
return parseScopes(value);
|
|
56
|
+
}
|
|
57
|
+
// ===========================================================================
|
|
58
|
+
// Concrete methods using hooks (trivial dialect diffs)
|
|
59
|
+
// ===========================================================================
|
|
60
|
+
async readCommitSeqsForPull(db, args) {
|
|
61
|
+
const partitionId = args.partitionId ?? 'default';
|
|
62
|
+
if (args.tables.length === 0)
|
|
63
|
+
return [];
|
|
64
|
+
const tablesFilter = this.buildStringListFilter(args.tables);
|
|
65
|
+
const res = await sql `
|
|
66
|
+
SELECT DISTINCT commit_seq
|
|
67
|
+
FROM sync_table_commits
|
|
68
|
+
WHERE partition_id = ${partitionId}
|
|
69
|
+
AND "table" ${tablesFilter}
|
|
70
|
+
AND commit_seq > ${args.cursor}
|
|
71
|
+
ORDER BY commit_seq ASC
|
|
72
|
+
LIMIT ${args.limitCommits}
|
|
73
|
+
`.execute(db);
|
|
74
|
+
return res.rows
|
|
75
|
+
.map((r) => coerceNumber(r.commit_seq))
|
|
76
|
+
.filter((n) => typeof n === 'number' && Number.isFinite(n) && n > args.cursor);
|
|
77
|
+
}
|
|
78
|
+
async readCommits(db, commitSeqs, options) {
|
|
79
|
+
const partitionId = options?.partitionId ?? 'default';
|
|
80
|
+
if (commitSeqs.length === 0)
|
|
81
|
+
return [];
|
|
82
|
+
const seqsFilter = this.buildNumberListFilter(commitSeqs);
|
|
83
|
+
const res = await sql `
|
|
84
|
+
SELECT commit_seq, actor_id, created_at, result_json
|
|
85
|
+
FROM sync_commits
|
|
86
|
+
WHERE commit_seq ${seqsFilter}
|
|
87
|
+
AND partition_id = ${partitionId}
|
|
88
|
+
ORDER BY commit_seq ASC
|
|
89
|
+
`.execute(db);
|
|
90
|
+
return res.rows.map((row) => ({
|
|
91
|
+
commit_seq: coerceNumber(row.commit_seq) ?? 0,
|
|
92
|
+
actor_id: row.actor_id,
|
|
93
|
+
created_at: coerceIsoString(row.created_at),
|
|
94
|
+
result_json: row.result_json ?? null,
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
async recordClientCursor(db, args) {
|
|
98
|
+
const partitionId = args.partitionId ?? 'default';
|
|
99
|
+
const now = new Date().toISOString();
|
|
100
|
+
const scopesJson = JSON.stringify(args.effectiveScopes);
|
|
101
|
+
await sql `
|
|
102
|
+
INSERT INTO sync_client_cursors (partition_id, client_id, actor_id, cursor, effective_scopes, updated_at)
|
|
103
|
+
VALUES (${partitionId}, ${args.clientId}, ${args.actorId}, ${args.cursor}, ${scopesJson}, ${now})
|
|
104
|
+
ON CONFLICT(partition_id, client_id) DO UPDATE SET
|
|
105
|
+
actor_id = ${args.actorId},
|
|
106
|
+
cursor = ${args.cursor},
|
|
107
|
+
effective_scopes = ${scopesJson},
|
|
108
|
+
updated_at = ${now}
|
|
109
|
+
`.execute(db);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=base.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/dialect/base.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAE7B,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAGvE;;;;;;;;;;GAUG;AACH,MAAM,OAAgB,qBAAqB;IA2FzC,8EAA8E;IAC9E,mDAAmD;IACnD,8EAA8E;IAE9E,KAAK,CAAC,gBAAgB,CACpB,EAAgC,EAChC,OAAkC,EACjB;QACjB,MAAM,WAAW,GAAG,OAAO,EAAE,WAAW,IAAI,SAAS,CAAC;QACtD,MAAM,GAAG,GAAG,MAAM,GAAG,CAAsB;;;6BAG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YAC3C,WAAW,EAAE,GAAG,CAAC,WAAW,IAAI,IAAI;SACrC,CAAC,CAAC,CAAC;IAAA,CACL;IAED,KAAK,CAAC,kBAAkB,CACtB,EAAgC,EAChC,IAMC,EACc;QACf,MAAM,WAAW,GAAG,IAAI,CAAC,WAAW,IAAI,SAAS,CAAC;QAClD,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;QACrC,MAAM,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;QAExD,MAAM,GAAG,CAAA;;gBAEG,WAAW,KAAK,IAAI,CAAC,QAAQ,KAAK,IAAI,CAAC,OAAO,KAAK,IAAI,CAAC,MAAM,KAAK,UAAU,KAAK,GAAG;;qBAEhF,IAAI,CAAC,OAAO;mBACd,IAAI,CAAC,MAAM;6BACD,UAAU;uBAChB,GAAG;KACrB,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAAA,CACf;CACF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @syncular/server - Shared dialect helpers
|
|
3
|
+
*
|
|
4
|
+
* Pure helper functions used by all server sync dialect implementations.
|
|
5
|
+
*/
|
|
6
|
+
import type { StoredScopes } from '@syncular/core';
|
|
7
|
+
export declare function coerceNumber(value: unknown): number | null;
|
|
8
|
+
export declare function coerceIsoString(value: unknown): string;
|
|
9
|
+
export declare function parseScopes(value: unknown): StoredScopes;
|
|
10
|
+
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/dialect/helpers.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAEnD,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAU1D;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAItD;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,YAAY,CAkCxD"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @syncular/server - Shared dialect helpers
|
|
3
|
+
*
|
|
4
|
+
* Pure helper functions used by all server sync dialect implementations.
|
|
5
|
+
*/
|
|
6
|
+
export function coerceNumber(value) {
|
|
7
|
+
if (value === null || value === undefined)
|
|
8
|
+
return null;
|
|
9
|
+
if (typeof value === 'number')
|
|
10
|
+
return Number.isFinite(value) ? value : null;
|
|
11
|
+
if (typeof value === 'bigint')
|
|
12
|
+
return Number.isFinite(Number(value)) ? Number(value) : null;
|
|
13
|
+
if (typeof value === 'string') {
|
|
14
|
+
const n = Number(value);
|
|
15
|
+
return Number.isFinite(n) ? n : null;
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
export function coerceIsoString(value) {
|
|
20
|
+
if (typeof value === 'string')
|
|
21
|
+
return value;
|
|
22
|
+
if (value instanceof Date)
|
|
23
|
+
return value.toISOString();
|
|
24
|
+
return String(value);
|
|
25
|
+
}
|
|
26
|
+
export function parseScopes(value) {
|
|
27
|
+
if (value === null || value === undefined)
|
|
28
|
+
return {};
|
|
29
|
+
if (typeof value === 'object' && !Array.isArray(value)) {
|
|
30
|
+
const result = {};
|
|
31
|
+
for (const [k, v] of Object.entries(value)) {
|
|
32
|
+
if (typeof v === 'string') {
|
|
33
|
+
result[k] = v;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
if (typeof value === 'string') {
|
|
39
|
+
try {
|
|
40
|
+
const parsed = JSON.parse(value);
|
|
41
|
+
if (typeof parsed === 'object' &&
|
|
42
|
+
parsed !== null &&
|
|
43
|
+
!Array.isArray(parsed)) {
|
|
44
|
+
const result = {};
|
|
45
|
+
for (const [k, v] of Object.entries(parsed)) {
|
|
46
|
+
if (typeof v === 'string') {
|
|
47
|
+
result[k] = v;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// ignore
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return {};
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../src/dialect/helpers.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,MAAM,UAAU,YAAY,CAAC,KAAc,EAAiB;IAC1D,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IACvD,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;IAC5E,IAAI,OAAO,KAAK,KAAK,QAAQ;QAC3B,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IAC/D,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,MAAM,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;QACxB,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACvC,CAAC;IACD,OAAO,IAAI,CAAC;AAAA,CACb;AAED,MAAM,UAAU,eAAe,CAAC,KAAc,EAAU;IACtD,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC5C,IAAI,KAAK,YAAY,IAAI;QAAE,OAAO,KAAK,CAAC,WAAW,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ACX"}
|
package/dist/dialect/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/dialect/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,SAAS,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/dialect/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,QAAQ,CAAC;AACvB,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC"}
|
package/dist/dialect/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/dialect/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,SAAS,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/dialect/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,QAAQ,CAAC;AACvB,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @syncular/server - Base Server Sync Dialect
|
|
3
|
+
*
|
|
4
|
+
* Abstract base class that implements shared query methods for all
|
|
5
|
+
* database-specific sync dialect implementations.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ScopeValues, StoredScopes, SyncOp } from '@syncular/core';
|
|
9
|
+
import type { Kysely, RawBuilder, Transaction } from 'kysely';
|
|
10
|
+
import { sql } from 'kysely';
|
|
11
|
+
import type { SyncChangeRow, SyncCommitRow, SyncCoreDb } from '../schema';
|
|
12
|
+
import { coerceIsoString, coerceNumber, parseScopes } from './helpers';
|
|
13
|
+
import type { DbExecutor, ServerSyncDialect } from './types';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Abstract base class for server sync dialects.
|
|
17
|
+
*
|
|
18
|
+
* Implements methods that are identical across dialects (pure SQL with no
|
|
19
|
+
* dialect-specific syntax) and methods that differ only in trivial SQL
|
|
20
|
+
* fragments (IN vs ANY, jsonb casts). Dialect-specific fragments are
|
|
21
|
+
* provided via small abstract hook methods.
|
|
22
|
+
*
|
|
23
|
+
* Genuinely different methods (DDL, transaction control, scope filtering,
|
|
24
|
+
* compaction, streaming) remain abstract for each dialect to implement.
|
|
25
|
+
*/
|
|
26
|
+
export abstract class BaseServerSyncDialect implements ServerSyncDialect {
|
|
27
|
+
abstract readonly name: string;
|
|
28
|
+
abstract readonly supportsForUpdate: boolean;
|
|
29
|
+
abstract readonly supportsSavepoints: boolean;
|
|
30
|
+
|
|
31
|
+
// ===========================================================================
|
|
32
|
+
// Abstract SQL fragment hooks
|
|
33
|
+
// ===========================================================================
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Build a SQL fragment for "column IN/= list of numbers".
|
|
37
|
+
* SQLite: `IN (1, 2, 3)` via sql.join
|
|
38
|
+
* Postgres: `= ANY(ARRAY[1,2,3]::bigint[])`
|
|
39
|
+
*/
|
|
40
|
+
protected abstract buildNumberListFilter(
|
|
41
|
+
values: number[]
|
|
42
|
+
): RawBuilder<unknown>;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Build a SQL fragment for "column IN/= list of strings".
|
|
46
|
+
* SQLite: `IN ('a', 'b')` via sql.join
|
|
47
|
+
* Postgres: `= ANY(ARRAY['a','b']::text[])`
|
|
48
|
+
*/
|
|
49
|
+
protected abstract buildStringListFilter(
|
|
50
|
+
values: string[]
|
|
51
|
+
): RawBuilder<unknown>;
|
|
52
|
+
|
|
53
|
+
// ===========================================================================
|
|
54
|
+
// Abstract methods (genuinely different implementations)
|
|
55
|
+
// ===========================================================================
|
|
56
|
+
|
|
57
|
+
abstract ensureSyncSchema<DB extends SyncCoreDb>(
|
|
58
|
+
db: Kysely<DB>
|
|
59
|
+
): Promise<void>;
|
|
60
|
+
|
|
61
|
+
abstract ensureConsoleSchema?<DB extends SyncCoreDb>(
|
|
62
|
+
db: Kysely<DB>
|
|
63
|
+
): Promise<void>;
|
|
64
|
+
|
|
65
|
+
abstract executeInTransaction<DB extends SyncCoreDb, T>(
|
|
66
|
+
db: Kysely<DB>,
|
|
67
|
+
fn: (executor: DbExecutor<DB>) => Promise<T>
|
|
68
|
+
): Promise<T>;
|
|
69
|
+
|
|
70
|
+
abstract setRepeatableRead<DB extends SyncCoreDb>(
|
|
71
|
+
trx: DbExecutor<DB>
|
|
72
|
+
): Promise<void>;
|
|
73
|
+
|
|
74
|
+
abstract readChangesForCommits<DB extends SyncCoreDb>(
|
|
75
|
+
db: DbExecutor<DB>,
|
|
76
|
+
args: {
|
|
77
|
+
commitSeqs: number[];
|
|
78
|
+
table: string;
|
|
79
|
+
scopes: ScopeValues;
|
|
80
|
+
partitionId?: string;
|
|
81
|
+
}
|
|
82
|
+
): Promise<SyncChangeRow[]>;
|
|
83
|
+
|
|
84
|
+
abstract readIncrementalPullRows<DB extends SyncCoreDb>(
|
|
85
|
+
db: DbExecutor<DB>,
|
|
86
|
+
args: {
|
|
87
|
+
table: string;
|
|
88
|
+
scopes: ScopeValues;
|
|
89
|
+
cursor: number;
|
|
90
|
+
limitCommits: number;
|
|
91
|
+
partitionId?: string;
|
|
92
|
+
}
|
|
93
|
+
): Promise<
|
|
94
|
+
Array<{
|
|
95
|
+
commit_seq: number;
|
|
96
|
+
actor_id: string;
|
|
97
|
+
created_at: string;
|
|
98
|
+
change_id: number;
|
|
99
|
+
table: string;
|
|
100
|
+
row_id: string;
|
|
101
|
+
op: SyncOp;
|
|
102
|
+
row_json: unknown | null;
|
|
103
|
+
row_version: number | null;
|
|
104
|
+
scopes: StoredScopes;
|
|
105
|
+
}>
|
|
106
|
+
>;
|
|
107
|
+
|
|
108
|
+
abstract compactChanges<DB extends SyncCoreDb>(
|
|
109
|
+
db: DbExecutor<DB>,
|
|
110
|
+
args: { fullHistoryHours: number }
|
|
111
|
+
): Promise<number>;
|
|
112
|
+
|
|
113
|
+
abstract scopesToDb(scopes: StoredScopes): unknown;
|
|
114
|
+
abstract dbToArray(value: unknown): string[];
|
|
115
|
+
abstract arrayToDb(values: string[]): unknown;
|
|
116
|
+
|
|
117
|
+
// ===========================================================================
|
|
118
|
+
// Concrete methods (identical SQL across dialects)
|
|
119
|
+
// ===========================================================================
|
|
120
|
+
|
|
121
|
+
async readMaxCommitSeq<DB extends SyncCoreDb>(
|
|
122
|
+
db: Kysely<DB> | Transaction<DB>,
|
|
123
|
+
options?: { partitionId?: string }
|
|
124
|
+
): Promise<number> {
|
|
125
|
+
const partitionId = options?.partitionId ?? 'default';
|
|
126
|
+
const res = await sql<{ max_seq: unknown }>`
|
|
127
|
+
SELECT max(commit_seq) as max_seq
|
|
128
|
+
FROM sync_commits
|
|
129
|
+
WHERE partition_id = ${partitionId}
|
|
130
|
+
`.execute(db);
|
|
131
|
+
|
|
132
|
+
return coerceNumber(res.rows[0]?.max_seq) ?? 0;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async readMinCommitSeq<DB extends SyncCoreDb>(
|
|
136
|
+
db: Kysely<DB> | Transaction<DB>,
|
|
137
|
+
options?: { partitionId?: string }
|
|
138
|
+
): Promise<number> {
|
|
139
|
+
const partitionId = options?.partitionId ?? 'default';
|
|
140
|
+
const res = await sql<{ min_seq: unknown }>`
|
|
141
|
+
SELECT min(commit_seq) as min_seq
|
|
142
|
+
FROM sync_commits
|
|
143
|
+
WHERE partition_id = ${partitionId}
|
|
144
|
+
`.execute(db);
|
|
145
|
+
|
|
146
|
+
return coerceNumber(res.rows[0]?.min_seq) ?? 0;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async readAffectedTablesFromChanges<DB extends SyncCoreDb>(
|
|
150
|
+
db: Kysely<DB> | Transaction<DB>,
|
|
151
|
+
commitSeq: number,
|
|
152
|
+
options?: { partitionId?: string }
|
|
153
|
+
): Promise<string[]> {
|
|
154
|
+
const partitionId = options?.partitionId ?? 'default';
|
|
155
|
+
const res = await sql<{ table: string }>`
|
|
156
|
+
SELECT DISTINCT "table"
|
|
157
|
+
FROM sync_changes
|
|
158
|
+
WHERE commit_seq = ${commitSeq}
|
|
159
|
+
AND partition_id = ${partitionId}
|
|
160
|
+
`.execute(db);
|
|
161
|
+
|
|
162
|
+
return res.rows
|
|
163
|
+
.map((r) => r.table)
|
|
164
|
+
.filter((t): t is string => typeof t === 'string' && t.length > 0);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
dbToScopes(value: unknown): StoredScopes {
|
|
168
|
+
return parseScopes(value);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// ===========================================================================
|
|
172
|
+
// Concrete methods using hooks (trivial dialect diffs)
|
|
173
|
+
// ===========================================================================
|
|
174
|
+
|
|
175
|
+
async readCommitSeqsForPull<DB extends SyncCoreDb>(
|
|
176
|
+
db: Kysely<DB> | Transaction<DB>,
|
|
177
|
+
args: {
|
|
178
|
+
cursor: number;
|
|
179
|
+
limitCommits: number;
|
|
180
|
+
tables: string[];
|
|
181
|
+
partitionId?: string;
|
|
182
|
+
}
|
|
183
|
+
): Promise<number[]> {
|
|
184
|
+
const partitionId = args.partitionId ?? 'default';
|
|
185
|
+
if (args.tables.length === 0) return [];
|
|
186
|
+
|
|
187
|
+
const tablesFilter = this.buildStringListFilter(args.tables);
|
|
188
|
+
|
|
189
|
+
const res = await sql<{ commit_seq: unknown }>`
|
|
190
|
+
SELECT DISTINCT commit_seq
|
|
191
|
+
FROM sync_table_commits
|
|
192
|
+
WHERE partition_id = ${partitionId}
|
|
193
|
+
AND "table" ${tablesFilter}
|
|
194
|
+
AND commit_seq > ${args.cursor}
|
|
195
|
+
ORDER BY commit_seq ASC
|
|
196
|
+
LIMIT ${args.limitCommits}
|
|
197
|
+
`.execute(db);
|
|
198
|
+
|
|
199
|
+
return res.rows
|
|
200
|
+
.map((r) => coerceNumber(r.commit_seq))
|
|
201
|
+
.filter(
|
|
202
|
+
(n): n is number =>
|
|
203
|
+
typeof n === 'number' && Number.isFinite(n) && n > args.cursor
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async readCommits<DB extends SyncCoreDb>(
|
|
208
|
+
db: Kysely<DB> | Transaction<DB>,
|
|
209
|
+
commitSeqs: number[],
|
|
210
|
+
options?: { partitionId?: string }
|
|
211
|
+
): Promise<SyncCommitRow[]> {
|
|
212
|
+
const partitionId = options?.partitionId ?? 'default';
|
|
213
|
+
if (commitSeqs.length === 0) return [];
|
|
214
|
+
|
|
215
|
+
const seqsFilter = this.buildNumberListFilter(commitSeqs);
|
|
216
|
+
|
|
217
|
+
const res = await sql<{
|
|
218
|
+
commit_seq: unknown;
|
|
219
|
+
actor_id: string;
|
|
220
|
+
created_at: unknown;
|
|
221
|
+
result_json: unknown | null;
|
|
222
|
+
}>`
|
|
223
|
+
SELECT commit_seq, actor_id, created_at, result_json
|
|
224
|
+
FROM sync_commits
|
|
225
|
+
WHERE commit_seq ${seqsFilter}
|
|
226
|
+
AND partition_id = ${partitionId}
|
|
227
|
+
ORDER BY commit_seq ASC
|
|
228
|
+
`.execute(db);
|
|
229
|
+
|
|
230
|
+
return res.rows.map((row) => ({
|
|
231
|
+
commit_seq: coerceNumber(row.commit_seq) ?? 0,
|
|
232
|
+
actor_id: row.actor_id,
|
|
233
|
+
created_at: coerceIsoString(row.created_at),
|
|
234
|
+
result_json: row.result_json ?? null,
|
|
235
|
+
}));
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async recordClientCursor<DB extends SyncCoreDb>(
|
|
239
|
+
db: Kysely<DB> | Transaction<DB>,
|
|
240
|
+
args: {
|
|
241
|
+
partitionId?: string;
|
|
242
|
+
clientId: string;
|
|
243
|
+
actorId: string;
|
|
244
|
+
cursor: number;
|
|
245
|
+
effectiveScopes: ScopeValues;
|
|
246
|
+
}
|
|
247
|
+
): Promise<void> {
|
|
248
|
+
const partitionId = args.partitionId ?? 'default';
|
|
249
|
+
const now = new Date().toISOString();
|
|
250
|
+
const scopesJson = JSON.stringify(args.effectiveScopes);
|
|
251
|
+
|
|
252
|
+
await sql`
|
|
253
|
+
INSERT INTO sync_client_cursors (partition_id, client_id, actor_id, cursor, effective_scopes, updated_at)
|
|
254
|
+
VALUES (${partitionId}, ${args.clientId}, ${args.actorId}, ${args.cursor}, ${scopesJson}, ${now})
|
|
255
|
+
ON CONFLICT(partition_id, client_id) DO UPDATE SET
|
|
256
|
+
actor_id = ${args.actorId},
|
|
257
|
+
cursor = ${args.cursor},
|
|
258
|
+
effective_scopes = ${scopesJson},
|
|
259
|
+
updated_at = ${now}
|
|
260
|
+
`.execute(db);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @syncular/server - Shared dialect helpers
|
|
3
|
+
*
|
|
4
|
+
* Pure helper functions used by all server sync dialect implementations.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { StoredScopes } from '@syncular/core';
|
|
8
|
+
|
|
9
|
+
export function coerceNumber(value: unknown): number | null {
|
|
10
|
+
if (value === null || value === undefined) return null;
|
|
11
|
+
if (typeof value === 'number') return Number.isFinite(value) ? value : null;
|
|
12
|
+
if (typeof value === 'bigint')
|
|
13
|
+
return Number.isFinite(Number(value)) ? Number(value) : null;
|
|
14
|
+
if (typeof value === 'string') {
|
|
15
|
+
const n = Number(value);
|
|
16
|
+
return Number.isFinite(n) ? n : null;
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function coerceIsoString(value: unknown): string {
|
|
22
|
+
if (typeof value === 'string') return value;
|
|
23
|
+
if (value instanceof Date) return value.toISOString();
|
|
24
|
+
return String(value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function parseScopes(value: unknown): StoredScopes {
|
|
28
|
+
if (value === null || value === undefined) return {};
|
|
29
|
+
if (typeof value === 'object' && !Array.isArray(value)) {
|
|
30
|
+
const result: StoredScopes = {};
|
|
31
|
+
for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
|
|
32
|
+
if (typeof v === 'string') {
|
|
33
|
+
result[k] = v;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
if (typeof value === 'string') {
|
|
39
|
+
try {
|
|
40
|
+
const parsed = JSON.parse(value);
|
|
41
|
+
if (
|
|
42
|
+
typeof parsed === 'object' &&
|
|
43
|
+
parsed !== null &&
|
|
44
|
+
!Array.isArray(parsed)
|
|
45
|
+
) {
|
|
46
|
+
const result: StoredScopes = {};
|
|
47
|
+
for (const [k, v] of Object.entries(
|
|
48
|
+
parsed as Record<string, unknown>
|
|
49
|
+
)) {
|
|
50
|
+
if (typeof v === 'string') {
|
|
51
|
+
result[k] = v;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
} catch {
|
|
57
|
+
// ignore
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return {};
|
|
61
|
+
}
|