powersync 0.1.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/README.md +339 -0
- package/bin/dev.cmd +3 -0
- package/bin/dev.js +9 -0
- package/bin/run.cmd +3 -0
- package/bin/run.js +8 -0
- package/dist/commands/init.d.ts +7 -0
- package/dist/commands/init.js +49 -0
- package/dist/commands/instance/config.d.ts +6 -0
- package/dist/commands/instance/config.js +23 -0
- package/dist/commands/instance/create.d.ts +16 -0
- package/dist/commands/instance/create.js +163 -0
- package/dist/commands/instance/deploy.d.ts +15 -0
- package/dist/commands/instance/deploy.js +142 -0
- package/dist/commands/instance/destroy.d.ts +9 -0
- package/dist/commands/instance/destroy.js +42 -0
- package/dist/commands/instance/schema.d.ts +6 -0
- package/dist/commands/instance/schema.js +20 -0
- package/dist/commands/instance/set.d.ts +9 -0
- package/dist/commands/instance/set.js +31 -0
- package/dist/commands/instance/status.d.ts +6 -0
- package/dist/commands/instance/status.js +21 -0
- package/dist/commands/instance/stop.d.ts +9 -0
- package/dist/commands/instance/stop.js +42 -0
- package/dist/commands/instance/sync-rules/deploy.d.ts +10 -0
- package/dist/commands/instance/sync-rules/deploy.js +49 -0
- package/dist/commands/instance/sync-rules/generate-schema.d.ts +10 -0
- package/dist/commands/instance/sync-rules/generate-schema.js +55 -0
- package/dist/commands/instance/sync-rules/validate.d.ts +9 -0
- package/dist/commands/instance/sync-rules/validate.js +29 -0
- package/dist/commands/instances.d.ts +6 -0
- package/dist/commands/instances.js +20 -0
- package/dist/constants.d.ts +11 -0
- package/dist/constants.js +8 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/powersync-sdk/accounts/api.d.ts +717 -0
- package/dist/powersync-sdk/accounts/api.js +182 -0
- package/dist/powersync-sdk/accounts/client.d.ts +1096 -0
- package/dist/powersync-sdk/accounts/client.js +124 -0
- package/dist/powersync-sdk/accounts/codecs.d.ts +32 -0
- package/dist/powersync-sdk/accounts/codecs.js +93 -0
- package/dist/powersync-sdk/accounts/index.d.ts +4 -0
- package/dist/powersync-sdk/accounts/index.js +4 -0
- package/dist/powersync-sdk/accounts/models.d.ts +260 -0
- package/dist/powersync-sdk/accounts/models.js +96 -0
- package/dist/powersync-sdk/accounts/routes.d.ts +44 -0
- package/dist/powersync-sdk/accounts/routes.js +45 -0
- package/dist/powersync-sdk/accounts/source.d.ts +76 -0
- package/dist/powersync-sdk/accounts/source.js +32 -0
- package/dist/powersync-sdk/errors/errors.d.ts +62 -0
- package/dist/powersync-sdk/errors/errors.js +115 -0
- package/dist/powersync-sdk/errors/index.d.ts +2 -0
- package/dist/powersync-sdk/errors/index.js +2 -0
- package/dist/powersync-sdk/errors/utils.d.ts +4 -0
- package/dist/powersync-sdk/errors/utils.js +18 -0
- package/dist/powersync-sdk/fetch-clients/codecs.d.ts +4 -0
- package/dist/powersync-sdk/fetch-clients/codecs.js +34 -0
- package/dist/powersync-sdk/fetch-clients/decoders.d.ts +3 -0
- package/dist/powersync-sdk/fetch-clients/decoders.js +35 -0
- package/dist/powersync-sdk/fetch-clients/definitions.d.ts +97 -0
- package/dist/powersync-sdk/fetch-clients/definitions.js +22 -0
- package/dist/powersync-sdk/fetch-clients/errors.d.ts +7 -0
- package/dist/powersync-sdk/fetch-clients/errors.js +19 -0
- package/dist/powersync-sdk/fetch-clients/index.d.ts +5 -0
- package/dist/powersync-sdk/fetch-clients/index.js +5 -0
- package/dist/powersync-sdk/fetch-clients/node-client.d.ts +9 -0
- package/dist/powersync-sdk/fetch-clients/node-client.js +107 -0
- package/dist/powersync-sdk/fetch-clients/request.d.ts +11 -0
- package/dist/powersync-sdk/fetch-clients/request.js +138 -0
- package/dist/powersync-sdk/fetch-clients/streaming.d.ts +65 -0
- package/dist/powersync-sdk/fetch-clients/streaming.js +99 -0
- package/dist/powersync-sdk/fetch-clients/utils.d.ts +13 -0
- package/dist/powersync-sdk/fetch-clients/utils.js +43 -0
- package/dist/powersync-sdk/fetch-clients/web-client.d.ts +14 -0
- package/dist/powersync-sdk/fetch-clients/web-client.js +56 -0
- package/dist/powersync-sdk/json/index.d.ts +4 -0
- package/dist/powersync-sdk/json/index.js +3 -0
- package/dist/powersync-sdk/json/json.d.ts +5 -0
- package/dist/powersync-sdk/json/json.js +35 -0
- package/dist/powersync-sdk/json/json_container.d.ts +18 -0
- package/dist/powersync-sdk/json/json_container.js +28 -0
- package/dist/powersync-sdk/json/json_stringify.d.ts +36 -0
- package/dist/powersync-sdk/json/json_stringify.js +162 -0
- package/dist/powersync-sdk/powersync/client.d.ts +449 -0
- package/dist/powersync-sdk/powersync/client.js +53 -0
- package/dist/powersync-sdk/powersync/config/HostedConfig.d.ts +327 -0
- package/dist/powersync-sdk/powersync/config/HostedConfig.js +92 -0
- package/dist/powersync-sdk/powersync/config/PowerSyncConfig.d.ts +211 -0
- package/dist/powersync-sdk/powersync/config/PowerSyncConfig.js +97 -0
- package/dist/powersync-sdk/powersync/config/normalize.d.ts +32 -0
- package/dist/powersync-sdk/powersync/config/normalize.js +85 -0
- package/dist/powersync-sdk/powersync/definitions.d.ts +149 -0
- package/dist/powersync-sdk/powersync/definitions.js +97 -0
- package/dist/powersync-sdk/powersync/index.d.ts +8 -0
- package/dist/powersync-sdk/powersync/index.js +8 -0
- package/dist/powersync-sdk/powersync/internal_routes.d.ts +247 -0
- package/dist/powersync-sdk/powersync/internal_routes.js +69 -0
- package/dist/powersync-sdk/powersync/routes.d.ts +779 -0
- package/dist/powersync-sdk/powersync/routes.js +191 -0
- package/dist/powersync-sdk/sdk-clients/endpoints.d.ts +31 -0
- package/dist/powersync-sdk/sdk-clients/endpoints.js +48 -0
- package/dist/powersync-sdk/sdk-clients/index.d.ts +3 -0
- package/dist/powersync-sdk/sdk-clients/index.js +3 -0
- package/dist/powersync-sdk/sdk-clients/pagination.d.ts +14 -0
- package/dist/powersync-sdk/sdk-clients/pagination.js +21 -0
- package/dist/powersync-sdk/sdk-clients/sdk-client.d.ts +17 -0
- package/dist/powersync-sdk/sdk-clients/sdk-client.js +30 -0
- package/dist/powersync-sdk/streaming/bson/buffer-array.d.ts +31 -0
- package/dist/powersync-sdk/streaming/bson/buffer-array.js +79 -0
- package/dist/powersync-sdk/streaming/bson/constants.d.ts +2 -0
- package/dist/powersync-sdk/streaming/bson/constants.js +1 -0
- package/dist/powersync-sdk/streaming/bson/decoder.d.ts +10 -0
- package/dist/powersync-sdk/streaming/bson/decoder.js +62 -0
- package/dist/powersync-sdk/streaming/bson/encoder.d.ts +10 -0
- package/dist/powersync-sdk/streaming/bson/encoder.js +23 -0
- package/dist/powersync-sdk/streaming/bson/header.d.ts +11 -0
- package/dist/powersync-sdk/streaming/bson/header.js +52 -0
- package/dist/powersync-sdk/streaming/bson/index.d.ts +5 -0
- package/dist/powersync-sdk/streaming/bson/index.js +5 -0
- package/dist/powersync-sdk/streaming/core/backpressure.d.ts +15 -0
- package/dist/powersync-sdk/streaming/core/backpressure.js +34 -0
- package/dist/powersync-sdk/streaming/core/cross-stream.d.ts +6 -0
- package/dist/powersync-sdk/streaming/core/cross-stream.js +17 -0
- package/dist/powersync-sdk/streaming/core/index.d.ts +4 -0
- package/dist/powersync-sdk/streaming/core/index.js +4 -0
- package/dist/powersync-sdk/streaming/core/node-utils.d.ts +7 -0
- package/dist/powersync-sdk/streaming/core/node-utils.js +11 -0
- package/dist/powersync-sdk/streaming/core/transformers.d.ts +41 -0
- package/dist/powersync-sdk/streaming/core/transformers.js +70 -0
- package/dist/powersync-sdk/streaming/core/utils.d.ts +36 -0
- package/dist/powersync-sdk/streaming/core/utils.js +66 -0
- package/dist/powersync-sdk/streaming/index.d.ts +3 -0
- package/dist/powersync-sdk/streaming/index.js +3 -0
- package/dist/powersync-sdk/streaming/web.d.ts +4 -0
- package/dist/powersync-sdk/streaming/web.js +4 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binaryreader.d.ts +2 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binaryreader.js +36 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binarywriter.d.ts +2 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binarywriter.js +58 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometry.d.ts +18 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometry.js +348 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometrycollection.d.ts +11 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometrycollection.js +118 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/linestring.d.ts +11 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/linestring.js +131 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multilinestring.d.ts +11 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multilinestring.js +134 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipoint.d.ts +11 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipoint.js +122 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipolygon.d.ts +11 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipolygon.js +158 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/point.d.ts +14 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/point.js +165 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/polygon.d.ts +11 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/polygon.js +207 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/types.d.ts +30 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/types.js +29 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wktparser.d.ts +2 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wktparser.js +111 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wkx.d.ts +9 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wkx.js +9 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/zigzag.d.ts +5 -0
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/zigzag.js +9 -0
- package/dist/powersync-sdk/sync-rules/DartSchemaGenerator.d.ts +12 -0
- package/dist/powersync-sdk/sync-rules/DartSchemaGenerator.js +35 -0
- package/dist/powersync-sdk/sync-rules/ExpressionType.d.ts +33 -0
- package/dist/powersync-sdk/sync-rules/ExpressionType.js +62 -0
- package/dist/powersync-sdk/sync-rules/IdSequence.d.ts +4 -0
- package/dist/powersync-sdk/sync-rules/IdSequence.js +7 -0
- package/dist/powersync-sdk/sync-rules/JsSchemaGenerator.d.ts +12 -0
- package/dist/powersync-sdk/sync-rules/JsSchemaGenerator.js +38 -0
- package/dist/powersync-sdk/sync-rules/SchemaGenerator.d.ts +14 -0
- package/dist/powersync-sdk/sync-rules/SchemaGenerator.js +24 -0
- package/dist/powersync-sdk/sync-rules/SourceTableInterface.d.ts +5 -0
- package/dist/powersync-sdk/sync-rules/SourceTableInterface.js +1 -0
- package/dist/powersync-sdk/sync-rules/SqlBucketDescriptor.d.ts +37 -0
- package/dist/powersync-sdk/sync-rules/SqlBucketDescriptor.js +116 -0
- package/dist/powersync-sdk/sync-rules/SqlDataQuery.d.ts +39 -0
- package/dist/powersync-sdk/sync-rules/SqlDataQuery.js +245 -0
- package/dist/powersync-sdk/sync-rules/SqlParameterQuery.d.ts +43 -0
- package/dist/powersync-sdk/sync-rules/SqlParameterQuery.js +254 -0
- package/dist/powersync-sdk/sync-rules/SqlSyncRules.d.ts +52 -0
- package/dist/powersync-sdk/sync-rules/SqlSyncRules.js +252 -0
- package/dist/powersync-sdk/sync-rules/StaticSchema.d.ts +26 -0
- package/dist/powersync-sdk/sync-rules/StaticSchema.js +71 -0
- package/dist/powersync-sdk/sync-rules/StaticSqlParameterQuery.d.ts +24 -0
- package/dist/powersync-sdk/sync-rules/StaticSqlParameterQuery.js +71 -0
- package/dist/powersync-sdk/sync-rules/TablePattern.d.ts +17 -0
- package/dist/powersync-sdk/sync-rules/TablePattern.js +56 -0
- package/dist/powersync-sdk/sync-rules/TableQuerySchema.d.ts +9 -0
- package/dist/powersync-sdk/sync-rules/TableQuerySchema.js +41 -0
- package/dist/powersync-sdk/sync-rules/errors.d.ts +22 -0
- package/dist/powersync-sdk/sync-rules/errors.js +60 -0
- package/dist/powersync-sdk/sync-rules/generators.d.ts +6 -0
- package/dist/powersync-sdk/sync-rules/generators.js +6 -0
- package/dist/powersync-sdk/sync-rules/index.d.ts +17 -0
- package/dist/powersync-sdk/sync-rules/index.js +17 -0
- package/dist/powersync-sdk/sync-rules/json_schema.d.ts +3 -0
- package/dist/powersync-sdk/sync-rules/json_schema.js +50 -0
- package/dist/powersync-sdk/sync-rules/sql_filters.d.ts +91 -0
- package/dist/powersync-sdk/sync-rules/sql_filters.js +491 -0
- package/dist/powersync-sdk/sync-rules/sql_functions.d.ts +53 -0
- package/dist/powersync-sdk/sync-rules/sql_functions.js +755 -0
- package/dist/powersync-sdk/sync-rules/sql_support.d.ts +22 -0
- package/dist/powersync-sdk/sync-rules/sql_support.js +208 -0
- package/dist/powersync-sdk/sync-rules/types.d.ts +174 -0
- package/dist/powersync-sdk/sync-rules/types.js +9 -0
- package/dist/powersync-sdk/sync-rules/utils.d.ts +45 -0
- package/dist/powersync-sdk/sync-rules/utils.js +163 -0
- package/dist/service/accounts.client.d.ts +26 -0
- package/dist/service/accounts.client.js +26 -0
- package/dist/service/powersync.client.d.ts +269 -0
- package/dist/service/powersync.client.js +135 -0
- package/dist/service/types.d.ts +69 -0
- package/dist/service/types.js +1 -0
- package/dist/service/utils.d.ts +5 -0
- package/dist/service/utils.js +7 -0
- package/dist/utils.d.ts +6 -0
- package/dist/utils.js +52 -0
- package/oclif.manifest.json +514 -0
- package/package.json +81 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { IdSequence } from './IdSequence.js';
|
|
2
|
+
import { SourceTableInterface } from './SourceTableInterface.js';
|
|
3
|
+
import { SqlDataQuery } from './SqlDataQuery.js';
|
|
4
|
+
import { SqlParameterQuery } from './SqlParameterQuery.js';
|
|
5
|
+
import { StaticSqlParameterQuery } from './StaticSqlParameterQuery.js';
|
|
6
|
+
import { TablePattern } from './TablePattern.js';
|
|
7
|
+
import { SqlRuleError } from './errors.js';
|
|
8
|
+
import { EvaluateRowOptions, EvaluatedParametersResult, EvaluationResult, QueryBucketIdOptions, SourceSchema, SqliteRow, SyncParameters } from './types.js';
|
|
9
|
+
export interface QueryParseResult {
|
|
10
|
+
errors: SqlRuleError[];
|
|
11
|
+
/**
|
|
12
|
+
* True if parsed in some form, even if there are errors.
|
|
13
|
+
*/
|
|
14
|
+
parsed: boolean;
|
|
15
|
+
}
|
|
16
|
+
export declare class SqlBucketDescriptor {
|
|
17
|
+
idSequence: IdSequence;
|
|
18
|
+
bucket_parameters?: string[];
|
|
19
|
+
/**
|
|
20
|
+
* source table -> queries
|
|
21
|
+
*/
|
|
22
|
+
data_queries: SqlDataQuery[];
|
|
23
|
+
global_parameter_queries: StaticSqlParameterQuery[];
|
|
24
|
+
name: string;
|
|
25
|
+
parameter_queries: SqlParameterQuery[];
|
|
26
|
+
parameterIdSequence: IdSequence;
|
|
27
|
+
constructor(name: string, idSequence: IdSequence);
|
|
28
|
+
addDataQuery(sql: string, schema?: SourceSchema): QueryParseResult;
|
|
29
|
+
addParameterQuery(sql: string, schema?: SourceSchema): QueryParseResult;
|
|
30
|
+
evaluateParameterRow(sourceTable: SourceTableInterface, row: SqliteRow): EvaluatedParametersResult[];
|
|
31
|
+
evaluateRow(options: EvaluateRowOptions): EvaluationResult[];
|
|
32
|
+
getSourceTables(): Set<TablePattern>;
|
|
33
|
+
getStaticBucketIds(parameters: SyncParameters): string[];
|
|
34
|
+
queryBucketIds(options: QueryBucketIdOptions): Promise<string[]>;
|
|
35
|
+
tableSyncsData(table: SourceTableInterface): boolean;
|
|
36
|
+
tableSyncsParameters(table: SourceTableInterface): boolean;
|
|
37
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/* eslint-disable unicorn/filename-case */
|
|
2
|
+
/* eslint-disable no-await-in-loop */
|
|
3
|
+
/* eslint-disable eqeqeq */
|
|
4
|
+
/* eslint-disable no-eq-null */
|
|
5
|
+
/* eslint-disable camelcase */
|
|
6
|
+
import { IdSequence } from './IdSequence.js';
|
|
7
|
+
import { SqlDataQuery } from './SqlDataQuery.js';
|
|
8
|
+
import { SqlParameterQuery } from './SqlParameterQuery.js';
|
|
9
|
+
export class SqlBucketDescriptor {
|
|
10
|
+
idSequence;
|
|
11
|
+
bucket_parameters;
|
|
12
|
+
/**
|
|
13
|
+
* source table -> queries
|
|
14
|
+
*/
|
|
15
|
+
data_queries = [];
|
|
16
|
+
global_parameter_queries = [];
|
|
17
|
+
name;
|
|
18
|
+
parameter_queries = [];
|
|
19
|
+
parameterIdSequence = new IdSequence();
|
|
20
|
+
constructor(name, idSequence) {
|
|
21
|
+
this.idSequence = idSequence;
|
|
22
|
+
this.name = name;
|
|
23
|
+
}
|
|
24
|
+
addDataQuery(sql, schema) {
|
|
25
|
+
if (this.bucket_parameters == null) {
|
|
26
|
+
throw new Error('Bucket parameters must be defined');
|
|
27
|
+
}
|
|
28
|
+
const dataRows = SqlDataQuery.fromSql(this.name, this.bucket_parameters, sql, schema);
|
|
29
|
+
dataRows.ruleId = this.idSequence.nextId();
|
|
30
|
+
this.data_queries.push(dataRows);
|
|
31
|
+
return {
|
|
32
|
+
errors: dataRows.errors,
|
|
33
|
+
parsed: true
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
addParameterQuery(sql, schema) {
|
|
37
|
+
const parameterQuery = SqlParameterQuery.fromSql(this.name, sql, schema);
|
|
38
|
+
if (this.bucket_parameters == null) {
|
|
39
|
+
this.bucket_parameters = parameterQuery.bucket_parameters;
|
|
40
|
+
}
|
|
41
|
+
else if (new Set([...parameterQuery.bucket_parameters, ...this.bucket_parameters]).size != this.bucket_parameters.length) {
|
|
42
|
+
throw new Error('Bucket parameters must match for each parameter query within a bucket');
|
|
43
|
+
}
|
|
44
|
+
parameterQuery.id = this.parameterIdSequence.nextId();
|
|
45
|
+
if (parameterQuery instanceof SqlParameterQuery) {
|
|
46
|
+
this.parameter_queries.push(parameterQuery);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
this.global_parameter_queries.push(parameterQuery);
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
errors: parameterQuery.errors,
|
|
53
|
+
parsed: true
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
evaluateParameterRow(sourceTable, row) {
|
|
57
|
+
const results = [];
|
|
58
|
+
for (const query of this.parameter_queries) {
|
|
59
|
+
if (query.applies(sourceTable)) {
|
|
60
|
+
results.push(...query.evaluateParameterRow(row));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return results;
|
|
64
|
+
}
|
|
65
|
+
evaluateRow(options) {
|
|
66
|
+
const results = [];
|
|
67
|
+
for (const query of this.data_queries) {
|
|
68
|
+
if (!query.applies(options.sourceTable)) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
results.push(...query.evaluateRow(options.sourceTable, options.record));
|
|
72
|
+
}
|
|
73
|
+
return results;
|
|
74
|
+
}
|
|
75
|
+
getSourceTables() {
|
|
76
|
+
const result = new Set();
|
|
77
|
+
for (const query of this.parameter_queries) {
|
|
78
|
+
result.add(query.sourceTable);
|
|
79
|
+
}
|
|
80
|
+
for (const query of this.data_queries) {
|
|
81
|
+
result.add(query.sourceTable);
|
|
82
|
+
}
|
|
83
|
+
// Note: No physical tables for global_parameter_queries
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
getStaticBucketIds(parameters) {
|
|
87
|
+
const results = [];
|
|
88
|
+
for (const query of this.global_parameter_queries) {
|
|
89
|
+
results.push(...query.getStaticBucketIds(parameters));
|
|
90
|
+
}
|
|
91
|
+
return results;
|
|
92
|
+
}
|
|
93
|
+
async queryBucketIds(options) {
|
|
94
|
+
const result = this.getStaticBucketIds(options.parameters);
|
|
95
|
+
for (const query of this.parameter_queries) {
|
|
96
|
+
result.push(...(await query.queryBucketIds(options)));
|
|
97
|
+
}
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
tableSyncsData(table) {
|
|
101
|
+
for (const query of this.data_queries) {
|
|
102
|
+
if (query.applies(table)) {
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
tableSyncsParameters(table) {
|
|
109
|
+
for (const query of this.parameter_queries) {
|
|
110
|
+
if (query.applies(table)) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { SelectedColumn } from 'pgsql-ast-parser';
|
|
2
|
+
import { ColumnDefinition } from './ExpressionType.js';
|
|
3
|
+
import { SourceTableInterface } from './SourceTableInterface.js';
|
|
4
|
+
import { TablePattern } from './TablePattern.js';
|
|
5
|
+
import { SqlRuleError } from './errors.js';
|
|
6
|
+
import { SqlTools } from './sql_filters.js';
|
|
7
|
+
import { EvaluationResult, ParameterMatchClause, QueryParameters, QuerySchema, SourceSchema, SqliteRow } from './types.js';
|
|
8
|
+
interface RowValueExtractor {
|
|
9
|
+
extract(tables: QueryParameters, into: SqliteRow): void;
|
|
10
|
+
getTypes(schema: QuerySchema, into: Record<string, ColumnDefinition>): void;
|
|
11
|
+
}
|
|
12
|
+
export declare class SqlDataQuery {
|
|
13
|
+
bucket_parameters?: string[];
|
|
14
|
+
columns?: SelectedColumn[];
|
|
15
|
+
descriptor_name?: string;
|
|
16
|
+
errors: SqlRuleError[];
|
|
17
|
+
extractors: RowValueExtractor[];
|
|
18
|
+
filter?: ParameterMatchClause;
|
|
19
|
+
ruleId?: string;
|
|
20
|
+
sourceTable?: TablePattern;
|
|
21
|
+
sql?: string;
|
|
22
|
+
table?: string;
|
|
23
|
+
tools?: SqlTools;
|
|
24
|
+
constructor();
|
|
25
|
+
static fromSql(descriptor_name: string, bucket_parameters: string[], sql: string, schema?: SourceSchema): SqlDataQuery;
|
|
26
|
+
addSpecialParameters(table: SourceTableInterface, row: SqliteRow): SqliteRow;
|
|
27
|
+
applies(table: SourceTableInterface): boolean | undefined;
|
|
28
|
+
columnOutputNames(): string[];
|
|
29
|
+
evaluateRow(table: SourceTableInterface, row: SqliteRow): EvaluationResult[];
|
|
30
|
+
getColumnOutputs(schema: SourceSchema): {
|
|
31
|
+
columns: ColumnDefinition[];
|
|
32
|
+
name: string;
|
|
33
|
+
}[];
|
|
34
|
+
getOutputName(sourceTable: string): string;
|
|
35
|
+
isUnaliasedWildcard(): boolean;
|
|
36
|
+
private getColumnOutputsFor;
|
|
37
|
+
private transformRow;
|
|
38
|
+
}
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/* eslint-disable unicorn/filename-case */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
/* eslint-disable unicorn/no-array-callback-reference */
|
|
4
|
+
/* eslint-disable unicorn/consistent-destructuring */
|
|
5
|
+
/* eslint-disable eqeqeq */
|
|
6
|
+
/* eslint-disable no-eq-null */
|
|
7
|
+
/* eslint-disable complexity */
|
|
8
|
+
/* eslint-disable prettier/prettier */
|
|
9
|
+
/* eslint-disable no-useless-constructor */
|
|
10
|
+
/* eslint-disable camelcase */
|
|
11
|
+
import { parse } from 'pgsql-ast-parser';
|
|
12
|
+
import { JSONBig } from '../json/index.js';
|
|
13
|
+
import { ExpressionType } from './ExpressionType.js';
|
|
14
|
+
import { TablePattern } from './TablePattern.js';
|
|
15
|
+
import { TableQuerySchema } from './TableQuerySchema.js';
|
|
16
|
+
import { SqlRuleError } from './errors.js';
|
|
17
|
+
import { SqlTools } from './sql_filters.js';
|
|
18
|
+
import { castAsText } from './sql_functions.js';
|
|
19
|
+
import { checkUnsupportedFeatures, isClauseError } from './sql_support.js';
|
|
20
|
+
import { filterJsonRow, getBucketId, isSelectStatement } from './utils.js';
|
|
21
|
+
export class SqlDataQuery {
|
|
22
|
+
bucket_parameters;
|
|
23
|
+
columns;
|
|
24
|
+
descriptor_name;
|
|
25
|
+
errors = [];
|
|
26
|
+
extractors = [];
|
|
27
|
+
filter;
|
|
28
|
+
ruleId;
|
|
29
|
+
sourceTable;
|
|
30
|
+
sql;
|
|
31
|
+
table;
|
|
32
|
+
tools;
|
|
33
|
+
constructor() { }
|
|
34
|
+
static fromSql(descriptor_name, bucket_parameters, sql, schema) {
|
|
35
|
+
const parsed = parse(sql, { locationTracking: true });
|
|
36
|
+
const rows = new SqlDataQuery();
|
|
37
|
+
if (parsed.length > 1) {
|
|
38
|
+
throw new SqlRuleError('Only a single SELECT statement is supported', sql, parsed[1]?._location);
|
|
39
|
+
}
|
|
40
|
+
const q = parsed[0];
|
|
41
|
+
if (!isSelectStatement(q)) {
|
|
42
|
+
throw new SqlRuleError('Only SELECT statements are supported', sql, q._location);
|
|
43
|
+
}
|
|
44
|
+
rows.errors.push(...checkUnsupportedFeatures(sql, q));
|
|
45
|
+
if (q.from == null || q.from.length != 1 || q.from[0].type != 'table') {
|
|
46
|
+
throw new SqlRuleError('Must SELECT from a single table', sql, q.from?.[0]._location);
|
|
47
|
+
}
|
|
48
|
+
const tableRef = q.from?.[0].name;
|
|
49
|
+
if (tableRef?.name == null) {
|
|
50
|
+
throw new SqlRuleError('Must SELECT from a single table', sql, q.from?.[0]._location);
|
|
51
|
+
}
|
|
52
|
+
const alias = tableRef.alias ?? tableRef.name;
|
|
53
|
+
const sourceTable = new TablePattern(tableRef.schema, tableRef.name);
|
|
54
|
+
let querySchema;
|
|
55
|
+
if (schema) {
|
|
56
|
+
const tables = schema.getTables(sourceTable);
|
|
57
|
+
if (tables.length === 0) {
|
|
58
|
+
const e = new SqlRuleError(`Table ${sourceTable.schema}.${sourceTable.tablePattern} not found`, sql, q.from?.[0]?._location);
|
|
59
|
+
e.type = 'warning';
|
|
60
|
+
rows.errors.push(e);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
querySchema = new TableQuerySchema(tables, alias);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const { where } = q;
|
|
67
|
+
const tools = new SqlTools({
|
|
68
|
+
parameter_tables: ['bucket'],
|
|
69
|
+
schema: querySchema,
|
|
70
|
+
sql,
|
|
71
|
+
table: alias,
|
|
72
|
+
value_tables: [alias]
|
|
73
|
+
});
|
|
74
|
+
const filter = tools.compileWhereClause(where);
|
|
75
|
+
const allParams = new Set([...filter.bucketParameters, ...bucket_parameters.map((p) => `bucket.${p}`)]);
|
|
76
|
+
if ((!filter.error && allParams.size != filter.bucketParameters.length) ||
|
|
77
|
+
allParams.size != bucket_parameters.length) {
|
|
78
|
+
rows.errors.push(new SqlRuleError(`Query must cover all bucket parameters: ${JSONBig.stringify(bucket_parameters)} != ${JSONBig.stringify(filter.bucketParameters)}`, sql, q._location));
|
|
79
|
+
}
|
|
80
|
+
rows.sourceTable = sourceTable;
|
|
81
|
+
rows.table = alias;
|
|
82
|
+
rows.sql = sql;
|
|
83
|
+
rows.filter = filter;
|
|
84
|
+
rows.descriptor_name = descriptor_name;
|
|
85
|
+
rows.bucket_parameters = bucket_parameters;
|
|
86
|
+
rows.columns = q.columns ?? [];
|
|
87
|
+
rows.tools = tools;
|
|
88
|
+
let hasId = false;
|
|
89
|
+
for (const column of q.columns ?? []) {
|
|
90
|
+
const name = tools.getOutputName(column);
|
|
91
|
+
if (name == '*') {
|
|
92
|
+
rows.extractors.push({
|
|
93
|
+
extract(tables, output) {
|
|
94
|
+
const row = tables[alias];
|
|
95
|
+
for (const key in row) {
|
|
96
|
+
if (key.startsWith('_')) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
output[key] ??= row[key];
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
getTypes(schema, into) {
|
|
103
|
+
for (const column of schema.getColumns(alias)) {
|
|
104
|
+
into[column.name] ??= column;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
const clause = tools.compileStaticExtractor(column.expr);
|
|
111
|
+
if (isClauseError(clause)) {
|
|
112
|
+
// Error logged already
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
rows.extractors.push({
|
|
116
|
+
extract(tables, output) {
|
|
117
|
+
output[name] = clause.evaluate(tables);
|
|
118
|
+
},
|
|
119
|
+
getTypes(schema, into) {
|
|
120
|
+
into[name] = { name, type: clause.getType(schema) };
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
if (name == 'id' || name == '*') {
|
|
125
|
+
hasId = true;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (!hasId) {
|
|
129
|
+
rows.errors.push(new SqlRuleError(`Query must return an "id" column`, sql, q.columns?.[0]._location));
|
|
130
|
+
}
|
|
131
|
+
rows.errors.push(...tools.errors);
|
|
132
|
+
return rows;
|
|
133
|
+
}
|
|
134
|
+
addSpecialParameters(table, row) {
|
|
135
|
+
if (this.sourceTable.isWildcard) {
|
|
136
|
+
return {
|
|
137
|
+
...row,
|
|
138
|
+
_table_suffix: this.sourceTable.suffix(table.table)
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
return row;
|
|
142
|
+
}
|
|
143
|
+
applies(table) {
|
|
144
|
+
return this.sourceTable?.matches(table);
|
|
145
|
+
}
|
|
146
|
+
columnOutputNames() {
|
|
147
|
+
return this.columns.map((c) => {
|
|
148
|
+
return this.tools.getOutputName(c);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
evaluateRow(table, row) {
|
|
152
|
+
try {
|
|
153
|
+
const tables = { [this.table]: this.addSpecialParameters(table, row) };
|
|
154
|
+
const bucketParameters = this.filter.filter(tables);
|
|
155
|
+
const bucketIds = bucketParameters.map((params) => getBucketId(this.descriptor_name, this.bucket_parameters, params));
|
|
156
|
+
const data = this.transformRow(tables);
|
|
157
|
+
let { id } = data;
|
|
158
|
+
if (typeof id !== 'string') {
|
|
159
|
+
// While an explicit cast would be better, this covers against very common
|
|
160
|
+
// issues when initially testing out sync, for example when the id column is an
|
|
161
|
+
// auto-incrementing integer.
|
|
162
|
+
// If there is no id column, we use a blank id. This will result in the user syncing
|
|
163
|
+
// a single arbitrary row for this table - better than just not being able to sync
|
|
164
|
+
// anything.
|
|
165
|
+
id = castAsText(id) ?? '';
|
|
166
|
+
}
|
|
167
|
+
const outputTable = this.getOutputName(table.table);
|
|
168
|
+
return bucketIds.map((bucketId) => {
|
|
169
|
+
return {
|
|
170
|
+
bucket: bucketId,
|
|
171
|
+
data,
|
|
172
|
+
id,
|
|
173
|
+
ruleId: this.ruleId,
|
|
174
|
+
table: outputTable
|
|
175
|
+
};
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
return [{ error: error.message ?? `Evaluating data query failed` }];
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
getColumnOutputs(schema) {
|
|
183
|
+
const result = [];
|
|
184
|
+
if (this.isUnaliasedWildcard()) {
|
|
185
|
+
// Separate results
|
|
186
|
+
for (const schemaTable of schema.getTables(this.sourceTable)) {
|
|
187
|
+
const output = {};
|
|
188
|
+
this.getColumnOutputsFor(schemaTable, output);
|
|
189
|
+
result.push({
|
|
190
|
+
columns: Object.values(output),
|
|
191
|
+
name: this.getOutputName(schemaTable.table)
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
// Merged results
|
|
197
|
+
const output = {};
|
|
198
|
+
for (const schemaTable of schema.getTables(this.sourceTable)) {
|
|
199
|
+
this.getColumnOutputsFor(schemaTable, output);
|
|
200
|
+
}
|
|
201
|
+
result.push({
|
|
202
|
+
columns: Object.values(output),
|
|
203
|
+
name: this.table
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
return result;
|
|
207
|
+
}
|
|
208
|
+
getOutputName(sourceTable) {
|
|
209
|
+
if (this.isUnaliasedWildcard()) {
|
|
210
|
+
// Wildcard without alias - use source
|
|
211
|
+
return sourceTable;
|
|
212
|
+
}
|
|
213
|
+
return this.table;
|
|
214
|
+
}
|
|
215
|
+
isUnaliasedWildcard() {
|
|
216
|
+
return this.sourceTable.isWildcard && this.table == this.sourceTable.tablePattern;
|
|
217
|
+
}
|
|
218
|
+
getColumnOutputsFor(schemaTable, output) {
|
|
219
|
+
const querySchema = {
|
|
220
|
+
getColumns: (table) => {
|
|
221
|
+
if (table == this.table) {
|
|
222
|
+
return schemaTable.getColumns();
|
|
223
|
+
}
|
|
224
|
+
return [];
|
|
225
|
+
},
|
|
226
|
+
getType: (table, column) => {
|
|
227
|
+
if (table == this.table) {
|
|
228
|
+
return schemaTable.getType(column) ?? ExpressionType.NONE;
|
|
229
|
+
}
|
|
230
|
+
// TODO: bucket parameters?
|
|
231
|
+
return ExpressionType.NONE;
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
for (const extractor of this.extractors) {
|
|
235
|
+
extractor.getTypes(querySchema, output);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
transformRow(tables) {
|
|
239
|
+
const result = {};
|
|
240
|
+
for (const extractor of this.extractors) {
|
|
241
|
+
extractor.extract(tables, result);
|
|
242
|
+
}
|
|
243
|
+
return filterJsonRow(result);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { SelectedColumn } from 'pgsql-ast-parser';
|
|
2
|
+
import { SourceTableInterface } from './SourceTableInterface.js';
|
|
3
|
+
import { StaticSqlParameterQuery } from './StaticSqlParameterQuery.js';
|
|
4
|
+
import { TablePattern } from './TablePattern.js';
|
|
5
|
+
import { SqlRuleError } from './errors.js';
|
|
6
|
+
import { SqlTools } from './sql_filters.js';
|
|
7
|
+
import { EvaluatedParametersResult, ParameterMatchClause, QueryBucketIdOptions, SourceSchema, SqliteJsonRow, SqliteJsonValue, SqliteRow, StaticRowValueClause, SyncParameters } from './types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Represents a parameter query, such as:
|
|
10
|
+
*
|
|
11
|
+
* SELECT id as user_id FROM users WHERE users.user_id = token_parameters.user_id
|
|
12
|
+
* SELECT id as user_id, token_parameters.is_admin as is_admin FROM users WHERE users.user_id = token_parameters.user_id
|
|
13
|
+
*/
|
|
14
|
+
export declare class SqlParameterQuery {
|
|
15
|
+
/** _Output_ bucket parameters */
|
|
16
|
+
bucket_parameters?: string[];
|
|
17
|
+
columns?: SelectedColumn[];
|
|
18
|
+
descriptor_name?: string;
|
|
19
|
+
errors: SqlRuleError[];
|
|
20
|
+
expanded_input_parameter?: string;
|
|
21
|
+
filter?: ParameterMatchClause;
|
|
22
|
+
id?: string;
|
|
23
|
+
/** _Input_ token / user parameters */
|
|
24
|
+
input_parameters?: string[];
|
|
25
|
+
lookup_columns?: SelectedColumn[];
|
|
26
|
+
lookup_extractors: Record<string, StaticRowValueClause>;
|
|
27
|
+
sourceTable?: TablePattern;
|
|
28
|
+
sql?: string;
|
|
29
|
+
static_columns?: SelectedColumn[];
|
|
30
|
+
static_extractors: Record<string, StaticRowValueClause>;
|
|
31
|
+
static_tools?: SqlTools;
|
|
32
|
+
table?: string;
|
|
33
|
+
tools?: SqlTools;
|
|
34
|
+
constructor();
|
|
35
|
+
static fromSql(descriptor_name: string, sql: string, schema?: SourceSchema): SqlParameterQuery | StaticSqlParameterQuery;
|
|
36
|
+
applies(table: SourceTableInterface): boolean;
|
|
37
|
+
evaluateParameterRow(row: SqliteRow): EvaluatedParametersResult[];
|
|
38
|
+
getLookups(parameters: SyncParameters): SqliteJsonValue[][];
|
|
39
|
+
lookupParam(param: string, parameters: SyncParameters): string | number | bigint | null;
|
|
40
|
+
queryBucketIds(options: QueryBucketIdOptions): Promise<string[]>;
|
|
41
|
+
resolveBucketIds(bucketParameters: SqliteJsonRow[], parameters: SyncParameters): string[];
|
|
42
|
+
transformRows(row: SqliteRow): SqliteRow[];
|
|
43
|
+
}
|