Skip to content

Commit f379bd8

Browse files
committed
Revert more sync-rule changes
1 parent 83e99e5 commit f379bd8

File tree

3 files changed

+20
-130
lines changed

3 files changed

+20
-130
lines changed

packages/sync-rules/src/json_schema.ts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,37 +49,6 @@ export const syncRulesSchema: ajvModule.Schema = {
4949
}
5050
}
5151
},
52-
streams: {
53-
type: 'object',
54-
description: 'Stream definitions',
55-
patternProperties: {
56-
'.*': {
57-
type: 'object',
58-
required: ['query'],
59-
examples: [{ query: ['select * from mytable'] }],
60-
properties: {
61-
accept_potentially_dangerous_queries: {
62-
description: 'If true, disables warnings on potentially dangerous queries',
63-
type: 'boolean'
64-
},
65-
priority: {
66-
description:
67-
'Default priority for the stream (lower values indicate higher priority). Clients can override the priority when subscribing.',
68-
type: 'integer'
69-
},
70-
default: {
71-
type: 'boolean',
72-
description: 'Whether the stream should be subscribed to by default.'
73-
},
74-
query: {
75-
description: 'The SQL query to sync to clients.',
76-
type: 'string'
77-
}
78-
},
79-
additionalProperties: false
80-
}
81-
}
82-
},
8352
event_definitions: {
8453
type: 'object',
8554
description: 'Record of sync replication event definitions',
@@ -110,7 +79,7 @@ export const syncRulesSchema: ajvModule.Schema = {
11079
}
11180
}
11281
},
113-
// required: ['bucket_definitions'],
82+
required: ['bucket_definitions'],
11483
additionalProperties: false
11584
} as const;
11685

packages/sync-rules/src/request_functions.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,10 @@ const request_user_id: SqlParameterFunction = {
5656
usesUnauthenticatedRequestParameters: false
5757
};
5858

59-
export const REQUEST_FUNCTIONS: Record<string, SqlParameterFunction> = {
59+
export const REQUEST_FUNCTIONS_NAMED = {
6060
parameters: request_parameters,
6161
jwt: request_jwt,
6262
user_id: request_user_id
6363
};
6464

65-
export const QUERY_FUNCTIONS: Record<string, SqlParameterFunction> = {
66-
params: {
67-
debugName: 'stream.params',
68-
call(parameters: ParameterValueSet) {
69-
return parameters.rawUserParameters;
70-
},
71-
getReturnType() {
72-
return ExpressionType.TEXT;
73-
},
74-
detail: 'Unauthenticated stream parameters as JSON',
75-
documentation:
76-
'Returns stream passed by the client when opening the stream. These parameters are not authenticated - any value can be passed in by the client.',
77-
usesAuthenticatedRequestParameters: false,
78-
usesUnauthenticatedRequestParameters: true
79-
}
80-
};
65+
export const REQUEST_FUNCTIONS: Record<string, SqlParameterFunction> = REQUEST_FUNCTIONS_NAMED;

packages/sync-rules/src/sql_filters.ts

Lines changed: 17 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { nil } from 'pgsql-ast-parser/src/utils.js';
44
import { BucketPriority, isValidPriority } from './BucketDescription.js';
55
import { ExpressionType } from './ExpressionType.js';
66
import { SqlRuleError } from './errors.js';
7-
import { QUERY_FUNCTIONS, REQUEST_FUNCTIONS } from './request_functions.js';
7+
import { REQUEST_FUNCTIONS } from './request_functions.js';
88
import {
99
BASIC_OPERATORS,
1010
OPERATOR_IN,
@@ -94,11 +94,6 @@ export interface SqlToolsOptions {
9494
*/
9595
supportsParameterExpressions?: boolean;
9696

97-
/**
98-
* true if expressions on stream parameters are supported.
99-
*/
100-
supportsStreamInputs?: boolean;
101-
10297
/**
10398
* Schema for validations.
10499
*/
@@ -118,9 +113,6 @@ export class SqlTools {
118113

119114
readonly supportsExpandingParameters: boolean;
120115
readonly supportsParameterExpressions: boolean;
121-
readonly supportsStreamInputs: boolean;
122-
123-
private inferredStaticParameters: Map<string, InferredBucketParameter> = new Map();
124116

125117
schema?: QuerySchema;
126118

@@ -139,7 +131,6 @@ export class SqlTools {
139131
this.sql = options.sql;
140132
this.supportsExpandingParameters = options.supportsExpandingParameters ?? false;
141133
this.supportsParameterExpressions = options.supportsParameterExpressions ?? false;
142-
this.supportsStreamInputs = options.supportsStreamInputs ?? false;
143134
}
144135

145136
error(message: string, expr: NodeLocation | Expr | undefined): ClauseError {
@@ -280,7 +271,7 @@ export class SqlTools {
280271
return compileStaticOperator(op, leftFilter as RowValueClause, rightFilter as RowValueClause);
281272
} else if (isParameterValueClause(otherFilter)) {
282273
// 2. row value = parameter value
283-
const inputParam = this.basicInputParameter(otherFilter);
274+
const inputParam = basicInputParameter(otherFilter);
284275

285276
return {
286277
error: false,
@@ -327,7 +318,7 @@ export class SqlTools {
327318
} else if (isParameterValueClause(leftFilter) && isRowValueClause(rightFilter)) {
328319
// token_parameters.value IN table.some_array
329320
// bucket.param IN table.some_array
330-
const inputParam = this.basicInputParameter(leftFilter);
321+
const inputParam = basicInputParameter(leftFilter);
331322

332323
return {
333324
error: false,
@@ -434,25 +425,7 @@ export class SqlTools {
434425
if (fn in REQUEST_FUNCTIONS) {
435426
const fnImpl = REQUEST_FUNCTIONS[fn];
436427
return {
437-
key: `stream.${fn}()`,
438-
lookupParameterValue(parameters) {
439-
return fnImpl.call(parameters);
440-
},
441-
usesAuthenticatedRequestParameters: fnImpl.usesAuthenticatedRequestParameters,
442-
usesUnauthenticatedRequestParameters: fnImpl.usesUnauthenticatedRequestParameters
443-
} satisfies ParameterValueClause;
444-
} else {
445-
return this.error(`Function '${schema}.${fn}' is not defined`, expr);
446-
}
447-
} else if (schema == 'stream') {
448-
if (!this.supportsStreamInputs) {
449-
return this.error(`${schema} schema is only available in stream definitions`, expr);
450-
}
451-
452-
if (fn in QUERY_FUNCTIONS) {
453-
const fnImpl = QUERY_FUNCTIONS[fn];
454-
return {
455-
key: `stream.${fn}()`,
428+
key: 'request.parameters()',
456429
lookupParameterValue(parameters) {
457430
return fnImpl.call(parameters);
458431
},
@@ -784,58 +757,8 @@ export class SqlTools {
784757

785758
return value as BucketPriority;
786759
}
787-
788-
private basicInputParameter(clause: ParameterValueClause): InputParameter {
789-
if (this.supportsStreamInputs) {
790-
let key = this.inferredStaticParameters.get(clause.key)?.name;
791-
if (key == null) {
792-
key = this.newInferredBucketParameterName();
793-
this.inferredStaticParameters.set(clause.key, {
794-
name: key,
795-
variant: 'static',
796-
clause
797-
});
798-
}
799-
800-
return {
801-
key,
802-
expands: false,
803-
filteredRowToLookupValue: () => {
804-
return SQLITE_FALSE; // Only relevant for parameter queries, but this is a stream query.
805-
},
806-
parametersToLookupValue: () => {
807-
return SQLITE_FALSE;
808-
}
809-
};
810-
}
811-
812-
return {
813-
key: clause.key,
814-
expands: false,
815-
filteredRowToLookupValue: (filterParameters) => {
816-
return filterParameters[clause.key];
817-
},
818-
parametersToLookupValue: (parameters) => {
819-
return clause.lookupParameterValue(parameters);
820-
}
821-
};
822-
}
823-
824-
public get inferredParameters(): InferredBucketParameter[] {
825-
return [...this.inferredStaticParameters.values()];
826-
}
827-
828-
private newInferredBucketParameterName() {
829-
return `p${this.inferredStaticParameters.size}`;
830-
}
831760
}
832761

833-
export type InferredBucketParameter = {
834-
name: string;
835-
} & StaticBucketParameter;
836-
837-
export type StaticBucketParameter = { variant: 'static'; clause: ParameterValueClause };
838-
839762
function isStatic(expr: Expr) {
840763
return ['integer', 'string', 'numeric', 'boolean', 'null'].includes(expr.type);
841764
}
@@ -872,3 +795,16 @@ function staticValueClause(value: SqliteValue): StaticValueClause {
872795
usesUnauthenticatedRequestParameters: false
873796
};
874797
}
798+
799+
function basicInputParameter(clause: ParameterValueClause): InputParameter {
800+
return {
801+
key: clause.key,
802+
expands: false,
803+
filteredRowToLookupValue: (filterParameters) => {
804+
return filterParameters[clause.key];
805+
},
806+
parametersToLookupValue: (parameters) => {
807+
return clause.lookupParameterValue(parameters);
808+
}
809+
};
810+
}

0 commit comments

Comments
 (0)