@powersync/service-core 0.0.0-dev-20261005120023 → 0.0.0-dev-20261015071128
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/CHANGELOG.md +23 -12
- package/dist/api/RouteAPI.d.ts +8 -1
- package/dist/api/api-index.d.ts +1 -0
- package/dist/api/api-index.js +1 -0
- package/dist/api/api-index.js.map +1 -1
- package/dist/api/diagnostics.js +28 -14
- package/dist/api/diagnostics.js.map +1 -1
- package/dist/api/source-capabilities.d.ts +7 -0
- package/dist/api/source-capabilities.js +23 -0
- package/dist/api/source-capabilities.js.map +1 -0
- package/dist/replication/AbstractReplicationJob.d.ts +4 -6
- package/dist/replication/AbstractReplicationJob.js.map +1 -1
- package/dist/replication/AbstractReplicator.d.ts +11 -0
- package/dist/replication/AbstractReplicator.js +24 -2
- package/dist/replication/AbstractReplicator.js.map +1 -1
- package/dist/replication/ReplicationModule.js +13 -2
- package/dist/replication/ReplicationModule.js.map +1 -1
- package/dist/routes/configure-fastify.d.ts +24 -24
- package/dist/routes/endpoints/admin.d.ts +48 -48
- package/dist/routes/endpoints/admin.js +8 -8
- package/dist/routes/endpoints/admin.js.map +1 -1
- package/dist/routes/endpoints/sync-rules.js +41 -14
- package/dist/routes/endpoints/sync-rules.js.map +1 -1
- package/dist/storage/BucketStorageFactory.d.ts +11 -13
- package/dist/storage/BucketStorageFactory.js +21 -11
- package/dist/storage/BucketStorageFactory.js.map +1 -1
- package/dist/storage/PersistedReplicationStream.d.ts +1 -2
- package/dist/storage/PersistedReplicationStream.js.map +1 -1
- package/dist/storage/PersistedSyncConfigContent.d.ts +6 -5
- package/dist/storage/PersistedSyncConfigContent.js +5 -12
- package/dist/storage/PersistedSyncConfigContent.js.map +1 -1
- package/dist/storage/StorageEngine.d.ts +0 -4
- package/dist/storage/StorageEngine.js +1 -7
- package/dist/storage/StorageEngine.js.map +1 -1
- package/dist/storage/StorageProvider.d.ts +1 -6
- package/dist/storage/SyncRulesBucketStorage.d.ts +1 -1
- package/dist/storage/storage-index.d.ts +0 -1
- package/dist/storage/storage-index.js +0 -1
- package/dist/storage/storage-index.js.map +1 -1
- package/dist/system/ServiceContext.d.ts +0 -20
- package/dist/system/ServiceContext.js +1 -29
- package/dist/system/ServiceContext.js.map +1 -1
- package/package.json +11 -11
- package/src/api/RouteAPI.ts +9 -1
- package/src/api/api-index.ts +1 -0
- package/src/api/diagnostics.ts +24 -12
- package/src/api/source-capabilities.ts +24 -0
- package/src/replication/AbstractReplicationJob.ts +2 -4
- package/src/replication/AbstractReplicator.ts +32 -2
- package/src/replication/ReplicationModule.ts +12 -2
- package/src/routes/endpoints/admin.ts +14 -6
- package/src/routes/endpoints/sync-rules.ts +42 -16
- package/src/storage/BucketStorageFactory.ts +21 -19
- package/src/storage/PersistedReplicationStream.ts +1 -2
- package/src/storage/PersistedSyncConfigContent.ts +11 -16
- package/src/storage/StorageEngine.ts +1 -10
- package/src/storage/StorageProvider.ts +1 -6
- package/src/storage/SyncRulesBucketStorage.ts +1 -1
- package/src/storage/storage-index.ts +0 -1
- package/src/system/ServiceContext.ts +1 -47
- package/test/src/AbstractReplicator.test.ts +109 -23
- package/test/src/PersistedSyncConfigContent.test.ts +1 -3
- package/test/src/diagnostics.test.ts +104 -0
- package/test/src/routes/admin.test.ts +221 -12
- package/test/src/routes/mocks.ts +0 -2
- package/test/src/routes/sync-rules.test.ts +124 -0
- package/test/src/source_table_config.test.ts +104 -0
- package/test/src/storage/BucketStorageFactory.test.ts +46 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/storage/SyncConfigParser.d.ts +0 -44
- package/dist/storage/SyncConfigParser.js +0 -73
- package/dist/storage/SyncConfigParser.js.map +0 -1
- package/src/storage/SyncConfigParser.ts +0 -124
- package/test/src/SyncConfigParser.test.ts +0 -309
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { AdditionalSyncConfigParser, JsonObject, SyncConfig, SyncConfigWithErrors, SyncRulesOptions, YamlError } from '@powersync/service-sync-rules';
|
|
2
|
-
export type ParseSyncConfigOptions = Omit<SyncRulesOptions, 'parsers' | 'schemaValidator'>;
|
|
3
|
-
/**
|
|
4
|
-
* Shared by service routes, deployment, replication and persisted storage; only initialization can register hooks.
|
|
5
|
-
*/
|
|
6
|
-
export interface SyncConfigParser {
|
|
7
|
-
/**
|
|
8
|
-
* A JSON schema which can be used to validate the format of a supplied sync config.
|
|
9
|
-
*/
|
|
10
|
-
readonly jsonSchema: JsonObject;
|
|
11
|
-
/**
|
|
12
|
-
* Parses string content.
|
|
13
|
-
*
|
|
14
|
-
* @returns The parsed {@link SyncConfigWithErrors}
|
|
15
|
-
*/
|
|
16
|
-
parseContent(content: string, options: ParseSyncConfigOptions): SyncConfigWithErrors;
|
|
17
|
-
/**
|
|
18
|
-
* Validates a persisted/parsed sync config.
|
|
19
|
-
*
|
|
20
|
-
* @returns Any errors detected.
|
|
21
|
-
*/
|
|
22
|
-
validatePersisted(options: {
|
|
23
|
-
config: SyncConfig;
|
|
24
|
-
context: {
|
|
25
|
-
defaultSchema: string;
|
|
26
|
-
};
|
|
27
|
-
}): YamlError[];
|
|
28
|
-
}
|
|
29
|
-
export declare class SqlSyncConfigParser implements SyncConfigParser {
|
|
30
|
-
#private;
|
|
31
|
-
constructor(parsers?: readonly AdditionalSyncConfigParser[]);
|
|
32
|
-
get jsonSchema(): JsonObject;
|
|
33
|
-
/**
|
|
34
|
-
* Validate the candidate composition before mutating the registry.
|
|
35
|
-
*/
|
|
36
|
-
registerParser(parser: AdditionalSyncConfigParser): void;
|
|
37
|
-
parseContent(content: string, options: ParseSyncConfigOptions): SyncConfigWithErrors;
|
|
38
|
-
validatePersisted({ config, context }: {
|
|
39
|
-
config: SyncConfig;
|
|
40
|
-
context: {
|
|
41
|
-
defaultSchema: string;
|
|
42
|
-
};
|
|
43
|
-
}): YamlError[];
|
|
44
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { compileSyncRulesSchemaValidator, createSyncRulesSchema, PrecompiledSyncConfig, SqlSyncRules, SyncRulesErrors, validateAdditionalSyncConfigParsers, YamlError } from '@powersync/service-sync-rules';
|
|
2
|
-
export class SqlSyncConfigParser {
|
|
3
|
-
#parsers;
|
|
4
|
-
#jsonSchema;
|
|
5
|
-
#schemaValidator;
|
|
6
|
-
constructor(parsers = []) {
|
|
7
|
-
validateAdditionalSyncConfigParsers(parsers);
|
|
8
|
-
this.#parsers = [...parsers];
|
|
9
|
-
this.#jsonSchema = createSyncRulesSchema(parsers);
|
|
10
|
-
this.#schemaValidator = compileSyncRulesSchemaValidator(this.#jsonSchema);
|
|
11
|
-
}
|
|
12
|
-
get jsonSchema() {
|
|
13
|
-
// Tooling can customize its copy without changing validation for the running service.
|
|
14
|
-
return structuredClone(this.#jsonSchema);
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Validate the candidate composition before mutating the registry.
|
|
18
|
-
*/
|
|
19
|
-
registerParser(parser) {
|
|
20
|
-
const parsers = [...this.#parsers, parser];
|
|
21
|
-
validateAdditionalSyncConfigParsers(parsers);
|
|
22
|
-
const schema = createSyncRulesSchema(parsers);
|
|
23
|
-
const validator = compileSyncRulesSchemaValidator(schema);
|
|
24
|
-
this.#parsers.push(parser);
|
|
25
|
-
this.#jsonSchema = schema;
|
|
26
|
-
this.#schemaValidator = validator;
|
|
27
|
-
}
|
|
28
|
-
parseContent(content, options) {
|
|
29
|
-
return SqlSyncRules.fromYaml(content, {
|
|
30
|
-
...options,
|
|
31
|
-
parsers: this.#parsers,
|
|
32
|
-
schemaValidator: this.#schemaValidator
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
validatePersisted({ config, context }) {
|
|
36
|
-
const errors = [];
|
|
37
|
-
const reportDiagnostic = (diagnostic) => {
|
|
38
|
-
const location = diagnostic.location;
|
|
39
|
-
const error = new YamlError(new Error(diagnostic.message), location && {
|
|
40
|
-
start: location.start_offset,
|
|
41
|
-
end: location.end_offset
|
|
42
|
-
});
|
|
43
|
-
error.type = diagnostic.level;
|
|
44
|
-
errors.push(error);
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Ensure that all modules which created a persisted SyncConfig
|
|
48
|
-
* are currently loaded.
|
|
49
|
-
* This prevents a case for a SyncConfig with missing external modules to be loaded.
|
|
50
|
-
*/
|
|
51
|
-
const requiredIds = new Set(config instanceof PrecompiledSyncConfig ? Object.keys(config.plan.moduleData ?? {}) : []);
|
|
52
|
-
const registeredIds = new Set(this.#parsers.map((parser) => parser.id));
|
|
53
|
-
const missingIds = [...requiredIds].filter((id) => !registeredIds.has(id));
|
|
54
|
-
if (missingIds.length != 0) {
|
|
55
|
-
reportDiagnostic({
|
|
56
|
-
level: 'fatal',
|
|
57
|
-
message: `Missing required sync config parsers: ${missingIds.join(', ')}`
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
for (const parser of this.#parsers) {
|
|
61
|
-
if (!requiredIds.has(parser.id))
|
|
62
|
-
continue;
|
|
63
|
-
/**
|
|
64
|
-
* This allows additional validation of module specific semantics
|
|
65
|
-
*/
|
|
66
|
-
parser.validatePersisted?.({ config, context: { ...context, reportDiagnostic } });
|
|
67
|
-
}
|
|
68
|
-
if (errors.some((error) => error.type == 'fatal'))
|
|
69
|
-
throw new SyncRulesErrors(errors);
|
|
70
|
-
return errors;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
//# sourceMappingURL=SyncConfigParser.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SyncConfigParser.js","sourceRoot":"","sources":["../../src/storage/SyncConfigParser.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,+BAA+B,EAC/B,qBAAqB,EAErB,qBAAqB,EACrB,YAAY,EAIZ,eAAe,EAGf,mCAAmC,EACnC,SAAS,EACV,MAAM,+BAA+B,CAAC;AA4BvC,MAAM,OAAO,mBAAmB;IACrB,QAAQ,CAA+B;IAChD,WAAW,CAAa;IACxB,gBAAgB,CAA2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,GAAG,OAAO,EAAE,gBAAgB,EAAE,EAAE,CAAC,CAAC;QACpF,CAAC;QACD,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,IAAI,OAAO,CAAC;YAAE,MAAM,IAAI,eAAe,CAAC,MAAM,CAAC,CAAC;QACrF,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AdditionalSyncConfigParser,
|
|
3
|
-
compileSyncRulesSchemaValidator,
|
|
4
|
-
createSyncRulesSchema,
|
|
5
|
-
JsonObject,
|
|
6
|
-
PrecompiledSyncConfig,
|
|
7
|
-
SqlSyncRules,
|
|
8
|
-
SyncConfig,
|
|
9
|
-
SyncConfigDiagnostic,
|
|
10
|
-
SyncConfigWithErrors,
|
|
11
|
-
SyncRulesErrors,
|
|
12
|
-
SyncRulesOptions,
|
|
13
|
-
SyncRulesSchemaValidator,
|
|
14
|
-
validateAdditionalSyncConfigParsers,
|
|
15
|
-
YamlError
|
|
16
|
-
} from '@powersync/service-sync-rules';
|
|
17
|
-
|
|
18
|
-
export type ParseSyncConfigOptions = Omit<SyncRulesOptions, 'parsers' | 'schemaValidator'>;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Shared by service routes, deployment, replication and persisted storage; only initialization can register hooks.
|
|
22
|
-
*/
|
|
23
|
-
export interface SyncConfigParser {
|
|
24
|
-
/**
|
|
25
|
-
* A JSON schema which can be used to validate the format of a supplied sync config.
|
|
26
|
-
*/
|
|
27
|
-
readonly jsonSchema: JsonObject;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Parses string content.
|
|
31
|
-
*
|
|
32
|
-
* @returns The parsed {@link SyncConfigWithErrors}
|
|
33
|
-
*/
|
|
34
|
-
parseContent(content: string, options: ParseSyncConfigOptions): SyncConfigWithErrors;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Validates a persisted/parsed sync config.
|
|
38
|
-
*
|
|
39
|
-
* @returns Any errors detected.
|
|
40
|
-
*/
|
|
41
|
-
validatePersisted(options: { config: SyncConfig; context: { defaultSchema: string } }): YamlError[];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export class SqlSyncConfigParser implements SyncConfigParser {
|
|
45
|
-
readonly #parsers: AdditionalSyncConfigParser[];
|
|
46
|
-
#jsonSchema: JsonObject;
|
|
47
|
-
#schemaValidator: SyncRulesSchemaValidator;
|
|
48
|
-
|
|
49
|
-
constructor(parsers: readonly AdditionalSyncConfigParser[] = []) {
|
|
50
|
-
validateAdditionalSyncConfigParsers(parsers);
|
|
51
|
-
this.#parsers = [...parsers];
|
|
52
|
-
this.#jsonSchema = createSyncRulesSchema(parsers);
|
|
53
|
-
this.#schemaValidator = compileSyncRulesSchemaValidator(this.#jsonSchema);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
get jsonSchema(): JsonObject {
|
|
57
|
-
// Tooling can customize its copy without changing validation for the running service.
|
|
58
|
-
return structuredClone(this.#jsonSchema);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Validate the candidate composition before mutating the registry.
|
|
63
|
-
*/
|
|
64
|
-
registerParser(parser: AdditionalSyncConfigParser): void {
|
|
65
|
-
const parsers = [...this.#parsers, parser];
|
|
66
|
-
validateAdditionalSyncConfigParsers(parsers);
|
|
67
|
-
const schema = createSyncRulesSchema(parsers);
|
|
68
|
-
const validator = compileSyncRulesSchemaValidator(schema);
|
|
69
|
-
this.#parsers.push(parser);
|
|
70
|
-
this.#jsonSchema = schema;
|
|
71
|
-
this.#schemaValidator = validator;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
parseContent(content: string, options: ParseSyncConfigOptions): SyncConfigWithErrors {
|
|
75
|
-
return SqlSyncRules.fromYaml(content, {
|
|
76
|
-
...options,
|
|
77
|
-
parsers: this.#parsers,
|
|
78
|
-
schemaValidator: this.#schemaValidator
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
validatePersisted({ config, context }: { config: SyncConfig; context: { defaultSchema: string } }): YamlError[] {
|
|
83
|
-
const errors: YamlError[] = [];
|
|
84
|
-
const reportDiagnostic = (diagnostic: SyncConfigDiagnostic) => {
|
|
85
|
-
const location = diagnostic.location;
|
|
86
|
-
const error = new YamlError(
|
|
87
|
-
new Error(diagnostic.message),
|
|
88
|
-
location && {
|
|
89
|
-
start: location.start_offset,
|
|
90
|
-
end: location.end_offset
|
|
91
|
-
}
|
|
92
|
-
);
|
|
93
|
-
error.type = diagnostic.level;
|
|
94
|
-
errors.push(error);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Ensure that all modules which created a persisted SyncConfig
|
|
99
|
-
* are currently loaded.
|
|
100
|
-
* This prevents a case for a SyncConfig with missing external modules to be loaded.
|
|
101
|
-
*/
|
|
102
|
-
const requiredIds = new Set(
|
|
103
|
-
config instanceof PrecompiledSyncConfig ? Object.keys(config.plan.moduleData ?? {}) : []
|
|
104
|
-
);
|
|
105
|
-
const registeredIds = new Set(this.#parsers.map((parser) => parser.id));
|
|
106
|
-
const missingIds = [...requiredIds].filter((id) => !registeredIds.has(id));
|
|
107
|
-
if (missingIds.length != 0) {
|
|
108
|
-
reportDiagnostic({
|
|
109
|
-
level: 'fatal',
|
|
110
|
-
message: `Missing required sync config parsers: ${missingIds.join(', ')}`
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
for (const parser of this.#parsers) {
|
|
115
|
-
if (!requiredIds.has(parser.id)) continue;
|
|
116
|
-
/**
|
|
117
|
-
* This allows additional validation of module specific semantics
|
|
118
|
-
*/
|
|
119
|
-
parser.validatePersisted?.({ config, context: { ...context, reportDiagnostic } });
|
|
120
|
-
}
|
|
121
|
-
if (errors.some((error) => error.type == 'fatal')) throw new SyncRulesErrors(errors);
|
|
122
|
-
return errors;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
@@ -1,309 +0,0 @@
|
|
|
1
|
-
import { SqlSyncConfigParser } from '@/storage/SyncConfigParser.js';
|
|
2
|
-
import { isCompatible, parsePersistedSyncConfigContent, updateSyncRulesFromConfig } from '@/storage/storage-index.js';
|
|
3
|
-
import { ServiceContextContainer, ServiceContextMode } from '@/system/ServiceContext.js';
|
|
4
|
-
import { logger } from '@powersync/lib-services-framework';
|
|
5
|
-
import * as syncRules from '@powersync/service-sync-rules';
|
|
6
|
-
import {
|
|
7
|
-
AdditionalSyncConfigParser,
|
|
8
|
-
DEFAULT_HYDRATION_STATE,
|
|
9
|
-
HydratedSyncConfig,
|
|
10
|
-
nodeSqlite,
|
|
11
|
-
normalizeSourceTableConfig,
|
|
12
|
-
PrecompiledSyncConfig,
|
|
13
|
-
SyncConfig
|
|
14
|
-
} from '@powersync/service-sync-rules';
|
|
15
|
-
import * as sqlite from 'node:sqlite';
|
|
16
|
-
import { describe, expect, test, vi } from 'vitest';
|
|
17
|
-
|
|
18
|
-
const STREAMS = /* yaml */ `
|
|
19
|
-
# Sync config fixture.
|
|
20
|
-
config:
|
|
21
|
-
edition: 3
|
|
22
|
-
streams:
|
|
23
|
-
orders:
|
|
24
|
-
query: SELECT * FROM orders
|
|
25
|
-
`;
|
|
26
|
-
const CONFIGURED = /* yaml */ `
|
|
27
|
-
# Sync config fixture.
|
|
28
|
-
config:
|
|
29
|
-
edition: 3
|
|
30
|
-
source_table_options:
|
|
31
|
-
orders:
|
|
32
|
-
sample: 10
|
|
33
|
-
streams:
|
|
34
|
-
orders:
|
|
35
|
-
query: SELECT * FROM orders
|
|
36
|
-
`;
|
|
37
|
-
|
|
38
|
-
// This fixture represents an external module. Core only knows the source-table map; sample is module-owned.
|
|
39
|
-
function additionalParser(): AdditionalSyncConfigParser {
|
|
40
|
-
const check = (config: SyncConfig) => {
|
|
41
|
-
for (const options of Object.values(config.sourceTableConfig)) {
|
|
42
|
-
if (typeof (options as { sample?: number })?.sample != 'number') throw new Error('Invalid persisted sample.');
|
|
43
|
-
if ((options as { sample?: number })?.sample === 13) throw new Error('Sample 13 is unsupported.');
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
return {
|
|
47
|
-
id: 'example.tables',
|
|
48
|
-
extendJsonSchema({ schema }) {
|
|
49
|
-
const options = (schema.properties as any).config.properties.source_table_options.additionalProperties;
|
|
50
|
-
options.properties.sample = { type: 'number' };
|
|
51
|
-
},
|
|
52
|
-
parse({ config, context }) {
|
|
53
|
-
const sourceTables = normalizeSourceTableConfig(
|
|
54
|
-
(config as { config?: { source_table_options?: unknown } }).config?.source_table_options
|
|
55
|
-
);
|
|
56
|
-
for (const [table, options] of Object.entries(sourceTables)) {
|
|
57
|
-
if (!Object.hasOwn(options!, 'sample')) continue;
|
|
58
|
-
context.parsedConfig.sourceTableConfig = {
|
|
59
|
-
...context.parsedConfig.sourceTableConfig,
|
|
60
|
-
[table]: options
|
|
61
|
-
};
|
|
62
|
-
const { plan } = context.parsedConfig as PrecompiledSyncConfig;
|
|
63
|
-
plan.moduleData = { ...plan.moduleData, ['example.tables']: null };
|
|
64
|
-
}
|
|
65
|
-
check(context.parsedConfig);
|
|
66
|
-
},
|
|
67
|
-
validatePersisted({ config }) {
|
|
68
|
-
check(config);
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function deployOptions(yaml = CONFIGURED, parser = new SqlSyncConfigParser([additionalParser()])) {
|
|
74
|
-
return updateSyncRulesFromConfig(parser.parseContent(yaml, { defaultSchema: 'app' }));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
describe('service sync config parser', () => {
|
|
78
|
-
test('reuses compiled validation until a parser is registered', () => {
|
|
79
|
-
const compile = vi.spyOn(syncRules, 'compileSyncRulesSchemaValidator');
|
|
80
|
-
try {
|
|
81
|
-
const parser = new SqlSyncConfigParser();
|
|
82
|
-
expect(compile).toHaveBeenCalledTimes(1);
|
|
83
|
-
for (let i = 0; i < 2; i++) {
|
|
84
|
-
expect(parser.parseContent(STREAMS, { defaultSchema: 'app' }).errors).toEqual([]);
|
|
85
|
-
expect(() => parser.parseContent(CONFIGURED, { defaultSchema: 'app' })).toThrow();
|
|
86
|
-
}
|
|
87
|
-
expect(compile).toHaveBeenCalledTimes(1);
|
|
88
|
-
|
|
89
|
-
parser.registerParser(additionalParser());
|
|
90
|
-
expect(compile).toHaveBeenCalledTimes(2);
|
|
91
|
-
for (let i = 0; i < 2; i++) {
|
|
92
|
-
expect(parser.parseContent(CONFIGURED, { defaultSchema: 'app' }).errors).toEqual([]);
|
|
93
|
-
expect(() =>
|
|
94
|
-
parser.parseContent(CONFIGURED.replace('sample: 10', 'sample: wrong'), { defaultSchema: 'app' })
|
|
95
|
-
).toThrow();
|
|
96
|
-
}
|
|
97
|
-
expect(compile).toHaveBeenCalledTimes(2);
|
|
98
|
-
} finally {
|
|
99
|
-
compile.mockRestore();
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
test('composes generic registrations atomically and keeps returned schema copies isolated', () => {
|
|
104
|
-
const parser = new SqlSyncConfigParser();
|
|
105
|
-
const baseSchema = parser.jsonSchema;
|
|
106
|
-
expect(() => parser.parseContent(CONFIGURED, { defaultSchema: 'app' })).toThrow();
|
|
107
|
-
parser.registerParser(additionalParser());
|
|
108
|
-
expect(parser.parseContent(CONFIGURED, { defaultSchema: 'app' }).errors).toEqual([]);
|
|
109
|
-
expect(baseSchema).toEqual(new SqlSyncConfigParser().jsonSchema);
|
|
110
|
-
const composed = parser.jsonSchema;
|
|
111
|
-
delete (composed.properties as any).config.properties.source_table_options;
|
|
112
|
-
expect(parser.parseContent(CONFIGURED, { defaultSchema: 'app' }).errors).toEqual([]);
|
|
113
|
-
|
|
114
|
-
expect(() => parser.registerParser(additionalParser())).toThrow('already registered');
|
|
115
|
-
expect(() => parser.registerParser({ id: '', parse() {} })).toThrow('must not be empty');
|
|
116
|
-
expect(() =>
|
|
117
|
-
parser.registerParser({
|
|
118
|
-
id: 'bad-schema',
|
|
119
|
-
parse() {},
|
|
120
|
-
extendJsonSchema({ schema }) {
|
|
121
|
-
schema.misspelledKeyword = true;
|
|
122
|
-
}
|
|
123
|
-
})
|
|
124
|
-
).toThrow();
|
|
125
|
-
// The failed schema is not retained and its ID can be used by a corrected registration.
|
|
126
|
-
parser.registerParser({ id: 'bad-schema', parse() {} });
|
|
127
|
-
expect(parser.parseContent(CONFIGURED, { defaultSchema: 'app' }).errors).toEqual([]);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
test('supplies one parser to storage and closes registration before asynchronous startup completes', async () => {
|
|
131
|
-
const context = new ServiceContextContainer({
|
|
132
|
-
serviceMode: ServiceContextMode.TEST_CONNECTION,
|
|
133
|
-
configuration: {
|
|
134
|
-
storage: { type: 'example' },
|
|
135
|
-
api_parameters: {
|
|
136
|
-
max_data_fetch_concurrency: 1,
|
|
137
|
-
max_buckets_per_connection: 1,
|
|
138
|
-
max_parameter_query_results: 1
|
|
139
|
-
}
|
|
140
|
-
} as never
|
|
141
|
-
});
|
|
142
|
-
const parser = context.syncConfigParser;
|
|
143
|
-
parser.registerParser(additionalParser());
|
|
144
|
-
let finish!: () => void;
|
|
145
|
-
const ready = new Promise<void>((resolve) => {
|
|
146
|
-
finish = resolve;
|
|
147
|
-
});
|
|
148
|
-
const getStorage = vi.fn(async () => {
|
|
149
|
-
await ready;
|
|
150
|
-
return { storage: {} as never, reportStorage: {} as never, shutDown: async () => {}, tearDown: async () => true };
|
|
151
|
-
});
|
|
152
|
-
context.storageEngine.registerProvider({ type: 'example', getStorage });
|
|
153
|
-
const startup = context.storageEngine.start();
|
|
154
|
-
try {
|
|
155
|
-
expect(() => parser.registerParser({ id: 'too-late', parse() {} })).toThrow(
|
|
156
|
-
'after the storage engine has started'
|
|
157
|
-
);
|
|
158
|
-
finish();
|
|
159
|
-
await startup;
|
|
160
|
-
expect(getStorage).toHaveBeenCalledWith({ resolvedConfig: context.configuration, syncConfigParser: parser });
|
|
161
|
-
expect(context.syncConfigParser).toBe(parser);
|
|
162
|
-
} finally {
|
|
163
|
-
finish();
|
|
164
|
-
await startup;
|
|
165
|
-
await context.storageEngine.shutDown();
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
test('requires recorded modules and semantic validation when restoring a saved plan', () => {
|
|
170
|
-
const parser = new SqlSyncConfigParser([additionalParser()]);
|
|
171
|
-
const compiled = deployOptions();
|
|
172
|
-
const restore = (syncConfigParser = parser, compiledPlan = compiled.config.plan) =>
|
|
173
|
-
parsePersistedSyncConfigContent({
|
|
174
|
-
content: CONFIGURED,
|
|
175
|
-
compiledPlan,
|
|
176
|
-
storageVersion: 2,
|
|
177
|
-
parseOptions: { defaultSchema: 'app' },
|
|
178
|
-
syncConfigParser
|
|
179
|
-
});
|
|
180
|
-
const restored = restore();
|
|
181
|
-
expect(restored.config.sourceTableConfig).toEqual(compiled.config.parsed.config.sourceTableConfig);
|
|
182
|
-
expect(
|
|
183
|
-
restored.config.hydrate({ hydrationState: DEFAULT_HYDRATION_STATE, sqlite: nodeSqlite(sqlite) }).sourceTableConfig
|
|
184
|
-
).toEqual(restored.config.sourceTableConfig);
|
|
185
|
-
expect(() => restore(new SqlSyncConfigParser())).toThrow('Missing required sync config parsers: example.tables');
|
|
186
|
-
const malformed = structuredClone(compiled.config.plan!);
|
|
187
|
-
(malformed.plan.sourceTableConfig!.orders as any).sample = 'wrong';
|
|
188
|
-
expect(() => restore(parser, malformed)).toThrow();
|
|
189
|
-
(malformed.plan.sourceTableConfig!.orders as any).sample = 13;
|
|
190
|
-
expect(() => restore(parser, malformed)).toThrow('Sample 13');
|
|
191
|
-
// A compiled plan is authoritative; its failure must not be hidden by reparsing valid source YAML.
|
|
192
|
-
expect(() => restore(new SqlSyncConfigParser(), null)).toThrow();
|
|
193
|
-
expect(restore(parser, null).config.sourceTableConfig).toEqual(restored.config.sourceTableConfig);
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
test('reports missing dependencies, runs required installed validators and ignores unused registered parsers', () => {
|
|
197
|
-
const validatePersisted = vi.fn();
|
|
198
|
-
const parser = new SqlSyncConfigParser([{ id: 'installed', parse() {}, validatePersisted }]);
|
|
199
|
-
const { config } = parser.parseContent(STREAMS, { defaultSchema: 'app' });
|
|
200
|
-
expect(parser.validatePersisted({ config, context: { defaultSchema: 'app' } })).toEqual([]);
|
|
201
|
-
expect(validatePersisted).not.toHaveBeenCalled();
|
|
202
|
-
(config as PrecompiledSyncConfig).plan.moduleData = { installed: null, missing: null, 'also-missing': null };
|
|
203
|
-
expect(() => parser.validatePersisted({ config, context: { defaultSchema: 'app' } })).toThrow(
|
|
204
|
-
'Missing required sync config parsers: missing, also-missing'
|
|
205
|
-
);
|
|
206
|
-
expect(validatePersisted).toHaveBeenCalledTimes(1);
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
test('restores transformed module state without applying the authoring schema', () => {
|
|
210
|
-
const parser = new SqlSyncConfigParser([
|
|
211
|
-
{
|
|
212
|
-
id: 'transformed',
|
|
213
|
-
parse({ context }) {
|
|
214
|
-
const { plan } = context.parsedConfig as PrecompiledSyncConfig;
|
|
215
|
-
plan.moduleData = { ...plan.moduleData, ['transformed']: null };
|
|
216
|
-
context.parsedConfig.sourceTableConfig = { orders: { compiled: true } };
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
]);
|
|
220
|
-
const compiled = deployOptions(STREAMS, parser);
|
|
221
|
-
const restore = (syncConfigParser: SqlSyncConfigParser) =>
|
|
222
|
-
parsePersistedSyncConfigContent({
|
|
223
|
-
content: STREAMS,
|
|
224
|
-
compiledPlan: compiled.config.plan,
|
|
225
|
-
storageVersion: 2,
|
|
226
|
-
parseOptions: { defaultSchema: 'app' },
|
|
227
|
-
syncConfigParser
|
|
228
|
-
});
|
|
229
|
-
expect((restore(parser).config as PrecompiledSyncConfig).plan.moduleData).toEqual({ transformed: null });
|
|
230
|
-
expect(restore(parser).config.sourceTableConfig).toEqual(compiled.config.parsed.config.sourceTableConfig);
|
|
231
|
-
expect(() => restore(new SqlSyncConfigParser())).toThrow('Missing required sync config parsers: transformed');
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
test('persists module dependencies without source-table options and accepts legacy plans without module data', () => {
|
|
235
|
-
const parser = new SqlSyncConfigParser([
|
|
236
|
-
{
|
|
237
|
-
id: 'required',
|
|
238
|
-
parse({ context }) {
|
|
239
|
-
const { plan } = context.parsedConfig as PrecompiledSyncConfig;
|
|
240
|
-
plan.moduleData = { ...plan.moduleData, ['required']: null };
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
]);
|
|
244
|
-
const compiled = deployOptions(STREAMS, parser);
|
|
245
|
-
expect(compiled.config.plan!.plan.version).toBe(3);
|
|
246
|
-
expect(compiled.config.plan!.plan.moduleData).toEqual({ required: null });
|
|
247
|
-
const restore = (compiledPlan = compiled.config.plan) =>
|
|
248
|
-
parsePersistedSyncConfigContent({
|
|
249
|
-
content: STREAMS,
|
|
250
|
-
compiledPlan,
|
|
251
|
-
storageVersion: 2,
|
|
252
|
-
parseOptions: { defaultSchema: 'app' },
|
|
253
|
-
syncConfigParser: new SqlSyncConfigParser()
|
|
254
|
-
});
|
|
255
|
-
expect(() => restore()).toThrow('Missing required sync config parsers: required');
|
|
256
|
-
const malformed = structuredClone(compiled.config.plan!);
|
|
257
|
-
(malformed.plan as any).moduleData = 'required';
|
|
258
|
-
expect(() => restore(malformed)).toThrow('Invalid sync config module data');
|
|
259
|
-
const legacy = deployOptions(STREAMS, new SqlSyncConfigParser());
|
|
260
|
-
expect((restore(legacy.config.plan).config as PrecompiledSyncConfig).plan.moduleData).toBeUndefined();
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
test('runs generic persisted validators with context and retains their warnings', () => {
|
|
264
|
-
const hook: AdditionalSyncConfigParser = {
|
|
265
|
-
id: 'core-check',
|
|
266
|
-
parse({ context }) {
|
|
267
|
-
const { plan } = context.parsedConfig as PrecompiledSyncConfig;
|
|
268
|
-
plan.moduleData = { ...plan.moduleData, ['core-check']: null };
|
|
269
|
-
},
|
|
270
|
-
validatePersisted({ config, context }) {
|
|
271
|
-
expect(config.getSourceTables()[0].name).toBe('orders');
|
|
272
|
-
expect(context.defaultSchema).toBe('app');
|
|
273
|
-
context.reportDiagnostic({ level: 'warning', message: 'Review this configuration.' });
|
|
274
|
-
}
|
|
275
|
-
};
|
|
276
|
-
const parser = new SqlSyncConfigParser([hook]);
|
|
277
|
-
const parsed = parser.parseContent(STREAMS, { defaultSchema: 'app' });
|
|
278
|
-
expect(parser.validatePersisted({ config: parsed.config, context: { defaultSchema: 'app' } })).toEqual([
|
|
279
|
-
expect.objectContaining({ type: 'warning', message: 'Review this configuration.' })
|
|
280
|
-
]);
|
|
281
|
-
hook.validatePersisted = ({ context }) => context.reportDiagnostic({ level: 'fatal', message: 'Rejected.' });
|
|
282
|
-
expect(() => parser.validatePersisted({ config: parsed.config, context: { defaultSchema: 'app' } })).toThrow(
|
|
283
|
-
'Rejected.'
|
|
284
|
-
);
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
test('reuses equal source-table options but requires replacement for additions, changes and removals', () => {
|
|
288
|
-
const first = deployOptions();
|
|
289
|
-
const same = deployOptions(CONFIGURED.replace('SELECT * FROM orders', 'SELECT id FROM orders'));
|
|
290
|
-
const changed = deployOptions(CONFIGURED.replace('sample: 10', 'sample: 20'));
|
|
291
|
-
const absent = deployOptions(STREAMS);
|
|
292
|
-
expect(isCompatible([first.config.plan], same.config, logger)).toBe(true);
|
|
293
|
-
expect(isCompatible([first.config.plan], changed.config, logger)).toBe(false);
|
|
294
|
-
expect(isCompatible([first.config.plan], absent.config, logger)).toBe(false);
|
|
295
|
-
expect(isCompatible([absent.config.plan], first.config, logger)).toBe(false);
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
test('merges equal source-table config once', () => {
|
|
299
|
-
const first = deployOptions().config.parsed.config;
|
|
300
|
-
const same = deployOptions(CONFIGURED.replace('SELECT * FROM orders', 'SELECT id FROM orders')).config.parsed
|
|
301
|
-
.config;
|
|
302
|
-
const hydrate = (definitions: SyncConfig[]) =>
|
|
303
|
-
new HydratedSyncConfig({
|
|
304
|
-
definitions,
|
|
305
|
-
createParams: { hydrationState: DEFAULT_HYDRATION_STATE, sqlite: nodeSqlite(sqlite) }
|
|
306
|
-
});
|
|
307
|
-
expect(hydrate([first, same]).sourceTableConfig).toEqual(first.sourceTableConfig);
|
|
308
|
-
});
|
|
309
|
-
});
|