powersync 0.1.0 → 0.1.1
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 +14 -14
- package/dist/commands/init.d.ts +0 -1
- package/dist/commands/init.js +9 -8
- package/dist/commands/instance/sync-rules/generate-schema.js +6 -6
- package/dist/constants.d.ts +2 -5
- package/dist/constants.js +4 -2
- package/dist/service/accounts.client.js +11129 -25
- package/dist/service/powersync.client.js +37453 -113
- package/dist/service/types.js +1 -1
- package/dist/service/utils.js +6 -6
- package/dist/utils.js +3 -3
- package/oclif.manifest.json +1 -1
- package/package.json +28 -20
- package/dist/powersync-sdk/accounts/api.d.ts +0 -717
- package/dist/powersync-sdk/accounts/api.js +0 -182
- package/dist/powersync-sdk/accounts/client.d.ts +0 -1096
- package/dist/powersync-sdk/accounts/client.js +0 -124
- package/dist/powersync-sdk/accounts/codecs.d.ts +0 -32
- package/dist/powersync-sdk/accounts/codecs.js +0 -93
- package/dist/powersync-sdk/accounts/index.d.ts +0 -4
- package/dist/powersync-sdk/accounts/index.js +0 -4
- package/dist/powersync-sdk/accounts/models.d.ts +0 -260
- package/dist/powersync-sdk/accounts/models.js +0 -96
- package/dist/powersync-sdk/accounts/routes.d.ts +0 -44
- package/dist/powersync-sdk/accounts/routes.js +0 -45
- package/dist/powersync-sdk/accounts/source.d.ts +0 -76
- package/dist/powersync-sdk/accounts/source.js +0 -32
- package/dist/powersync-sdk/errors/errors.d.ts +0 -62
- package/dist/powersync-sdk/errors/errors.js +0 -115
- package/dist/powersync-sdk/errors/index.d.ts +0 -2
- package/dist/powersync-sdk/errors/index.js +0 -2
- package/dist/powersync-sdk/errors/utils.d.ts +0 -4
- package/dist/powersync-sdk/errors/utils.js +0 -18
- package/dist/powersync-sdk/fetch-clients/codecs.d.ts +0 -4
- package/dist/powersync-sdk/fetch-clients/codecs.js +0 -34
- package/dist/powersync-sdk/fetch-clients/decoders.d.ts +0 -3
- package/dist/powersync-sdk/fetch-clients/decoders.js +0 -35
- package/dist/powersync-sdk/fetch-clients/definitions.d.ts +0 -97
- package/dist/powersync-sdk/fetch-clients/definitions.js +0 -22
- package/dist/powersync-sdk/fetch-clients/errors.d.ts +0 -7
- package/dist/powersync-sdk/fetch-clients/errors.js +0 -19
- package/dist/powersync-sdk/fetch-clients/index.d.ts +0 -5
- package/dist/powersync-sdk/fetch-clients/index.js +0 -5
- package/dist/powersync-sdk/fetch-clients/node-client.d.ts +0 -9
- package/dist/powersync-sdk/fetch-clients/node-client.js +0 -107
- package/dist/powersync-sdk/fetch-clients/request.d.ts +0 -11
- package/dist/powersync-sdk/fetch-clients/request.js +0 -138
- package/dist/powersync-sdk/fetch-clients/streaming.d.ts +0 -65
- package/dist/powersync-sdk/fetch-clients/streaming.js +0 -99
- package/dist/powersync-sdk/fetch-clients/utils.d.ts +0 -13
- package/dist/powersync-sdk/fetch-clients/utils.js +0 -43
- package/dist/powersync-sdk/fetch-clients/web-client.d.ts +0 -14
- package/dist/powersync-sdk/fetch-clients/web-client.js +0 -56
- package/dist/powersync-sdk/json/index.d.ts +0 -4
- package/dist/powersync-sdk/json/index.js +0 -3
- package/dist/powersync-sdk/json/json.d.ts +0 -5
- package/dist/powersync-sdk/json/json.js +0 -35
- package/dist/powersync-sdk/json/json_container.d.ts +0 -18
- package/dist/powersync-sdk/json/json_container.js +0 -28
- package/dist/powersync-sdk/json/json_stringify.d.ts +0 -36
- package/dist/powersync-sdk/json/json_stringify.js +0 -162
- package/dist/powersync-sdk/powersync/client.d.ts +0 -449
- package/dist/powersync-sdk/powersync/client.js +0 -53
- package/dist/powersync-sdk/powersync/config/HostedConfig.d.ts +0 -327
- package/dist/powersync-sdk/powersync/config/HostedConfig.js +0 -92
- package/dist/powersync-sdk/powersync/config/PowerSyncConfig.d.ts +0 -211
- package/dist/powersync-sdk/powersync/config/PowerSyncConfig.js +0 -97
- package/dist/powersync-sdk/powersync/config/normalize.d.ts +0 -32
- package/dist/powersync-sdk/powersync/config/normalize.js +0 -85
- package/dist/powersync-sdk/powersync/definitions.d.ts +0 -149
- package/dist/powersync-sdk/powersync/definitions.js +0 -97
- package/dist/powersync-sdk/powersync/index.d.ts +0 -8
- package/dist/powersync-sdk/powersync/index.js +0 -8
- package/dist/powersync-sdk/powersync/internal_routes.d.ts +0 -247
- package/dist/powersync-sdk/powersync/internal_routes.js +0 -69
- package/dist/powersync-sdk/powersync/routes.d.ts +0 -779
- package/dist/powersync-sdk/powersync/routes.js +0 -191
- package/dist/powersync-sdk/sdk-clients/endpoints.d.ts +0 -31
- package/dist/powersync-sdk/sdk-clients/endpoints.js +0 -48
- package/dist/powersync-sdk/sdk-clients/index.d.ts +0 -3
- package/dist/powersync-sdk/sdk-clients/index.js +0 -3
- package/dist/powersync-sdk/sdk-clients/pagination.d.ts +0 -14
- package/dist/powersync-sdk/sdk-clients/pagination.js +0 -21
- package/dist/powersync-sdk/sdk-clients/sdk-client.d.ts +0 -17
- package/dist/powersync-sdk/sdk-clients/sdk-client.js +0 -30
- package/dist/powersync-sdk/streaming/bson/buffer-array.d.ts +0 -31
- package/dist/powersync-sdk/streaming/bson/buffer-array.js +0 -79
- package/dist/powersync-sdk/streaming/bson/constants.d.ts +0 -2
- package/dist/powersync-sdk/streaming/bson/constants.js +0 -1
- package/dist/powersync-sdk/streaming/bson/decoder.d.ts +0 -10
- package/dist/powersync-sdk/streaming/bson/decoder.js +0 -62
- package/dist/powersync-sdk/streaming/bson/encoder.d.ts +0 -10
- package/dist/powersync-sdk/streaming/bson/encoder.js +0 -23
- package/dist/powersync-sdk/streaming/bson/header.d.ts +0 -11
- package/dist/powersync-sdk/streaming/bson/header.js +0 -52
- package/dist/powersync-sdk/streaming/bson/index.d.ts +0 -5
- package/dist/powersync-sdk/streaming/bson/index.js +0 -5
- package/dist/powersync-sdk/streaming/core/backpressure.d.ts +0 -15
- package/dist/powersync-sdk/streaming/core/backpressure.js +0 -34
- package/dist/powersync-sdk/streaming/core/cross-stream.d.ts +0 -6
- package/dist/powersync-sdk/streaming/core/cross-stream.js +0 -17
- package/dist/powersync-sdk/streaming/core/index.d.ts +0 -4
- package/dist/powersync-sdk/streaming/core/index.js +0 -4
- package/dist/powersync-sdk/streaming/core/node-utils.d.ts +0 -7
- package/dist/powersync-sdk/streaming/core/node-utils.js +0 -11
- package/dist/powersync-sdk/streaming/core/transformers.d.ts +0 -41
- package/dist/powersync-sdk/streaming/core/transformers.js +0 -70
- package/dist/powersync-sdk/streaming/core/utils.d.ts +0 -36
- package/dist/powersync-sdk/streaming/core/utils.js +0 -66
- package/dist/powersync-sdk/streaming/index.d.ts +0 -3
- package/dist/powersync-sdk/streaming/index.js +0 -3
- package/dist/powersync-sdk/streaming/web.d.ts +0 -4
- package/dist/powersync-sdk/streaming/web.js +0 -4
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binaryreader.d.ts +0 -2
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binaryreader.js +0 -36
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binarywriter.d.ts +0 -2
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binarywriter.js +0 -58
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometry.d.ts +0 -18
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometry.js +0 -348
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometrycollection.d.ts +0 -11
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometrycollection.js +0 -118
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/linestring.d.ts +0 -11
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/linestring.js +0 -131
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multilinestring.d.ts +0 -11
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multilinestring.js +0 -134
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipoint.d.ts +0 -11
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipoint.js +0 -122
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipolygon.d.ts +0 -11
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipolygon.js +0 -158
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/point.d.ts +0 -14
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/point.js +0 -165
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/polygon.d.ts +0 -11
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/polygon.js +0 -207
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/types.d.ts +0 -30
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/types.js +0 -29
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wktparser.d.ts +0 -2
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wktparser.js +0 -111
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wkx.d.ts +0 -9
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wkx.js +0 -9
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/zigzag.d.ts +0 -5
- package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/zigzag.js +0 -9
- package/dist/powersync-sdk/sync-rules/DartSchemaGenerator.d.ts +0 -12
- package/dist/powersync-sdk/sync-rules/DartSchemaGenerator.js +0 -35
- package/dist/powersync-sdk/sync-rules/ExpressionType.d.ts +0 -33
- package/dist/powersync-sdk/sync-rules/ExpressionType.js +0 -62
- package/dist/powersync-sdk/sync-rules/IdSequence.d.ts +0 -4
- package/dist/powersync-sdk/sync-rules/IdSequence.js +0 -7
- package/dist/powersync-sdk/sync-rules/JsSchemaGenerator.d.ts +0 -12
- package/dist/powersync-sdk/sync-rules/JsSchemaGenerator.js +0 -38
- package/dist/powersync-sdk/sync-rules/SchemaGenerator.d.ts +0 -14
- package/dist/powersync-sdk/sync-rules/SchemaGenerator.js +0 -24
- package/dist/powersync-sdk/sync-rules/SourceTableInterface.d.ts +0 -5
- package/dist/powersync-sdk/sync-rules/SourceTableInterface.js +0 -1
- package/dist/powersync-sdk/sync-rules/SqlBucketDescriptor.d.ts +0 -37
- package/dist/powersync-sdk/sync-rules/SqlBucketDescriptor.js +0 -116
- package/dist/powersync-sdk/sync-rules/SqlDataQuery.d.ts +0 -39
- package/dist/powersync-sdk/sync-rules/SqlDataQuery.js +0 -245
- package/dist/powersync-sdk/sync-rules/SqlParameterQuery.d.ts +0 -43
- package/dist/powersync-sdk/sync-rules/SqlParameterQuery.js +0 -254
- package/dist/powersync-sdk/sync-rules/SqlSyncRules.d.ts +0 -52
- package/dist/powersync-sdk/sync-rules/SqlSyncRules.js +0 -252
- package/dist/powersync-sdk/sync-rules/StaticSchema.d.ts +0 -26
- package/dist/powersync-sdk/sync-rules/StaticSchema.js +0 -71
- package/dist/powersync-sdk/sync-rules/StaticSqlParameterQuery.d.ts +0 -24
- package/dist/powersync-sdk/sync-rules/StaticSqlParameterQuery.js +0 -71
- package/dist/powersync-sdk/sync-rules/TablePattern.d.ts +0 -17
- package/dist/powersync-sdk/sync-rules/TablePattern.js +0 -56
- package/dist/powersync-sdk/sync-rules/TableQuerySchema.d.ts +0 -9
- package/dist/powersync-sdk/sync-rules/TableQuerySchema.js +0 -41
- package/dist/powersync-sdk/sync-rules/errors.d.ts +0 -22
- package/dist/powersync-sdk/sync-rules/errors.js +0 -60
- package/dist/powersync-sdk/sync-rules/generators.d.ts +0 -6
- package/dist/powersync-sdk/sync-rules/generators.js +0 -6
- package/dist/powersync-sdk/sync-rules/index.d.ts +0 -17
- package/dist/powersync-sdk/sync-rules/index.js +0 -17
- package/dist/powersync-sdk/sync-rules/json_schema.d.ts +0 -3
- package/dist/powersync-sdk/sync-rules/json_schema.js +0 -50
- package/dist/powersync-sdk/sync-rules/sql_filters.d.ts +0 -91
- package/dist/powersync-sdk/sync-rules/sql_filters.js +0 -491
- package/dist/powersync-sdk/sync-rules/sql_functions.d.ts +0 -53
- package/dist/powersync-sdk/sync-rules/sql_functions.js +0 -755
- package/dist/powersync-sdk/sync-rules/sql_support.d.ts +0 -22
- package/dist/powersync-sdk/sync-rules/sql_support.js +0 -208
- package/dist/powersync-sdk/sync-rules/types.d.ts +0 -174
- package/dist/powersync-sdk/sync-rules/types.js +0 -9
- package/dist/powersync-sdk/sync-rules/utils.d.ts +0 -45
- package/dist/powersync-sdk/sync-rules/utils.js +0 -163
- package/dist/service/accounts.client.d.ts +0 -26
- package/dist/service/powersync.client.d.ts +0 -269
- package/dist/service/types.d.ts +0 -69
- package/dist/service/utils.d.ts +0 -5
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,37 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,116 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
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 {};
|
|
@@ -1,245 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
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
|
-
}
|