dsqlbase 0.2.0 → 0.3.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/CHANGELOG.md +42 -0
- package/README.md +41 -60
- package/dist/client/clients/base.d.ts +20 -0
- package/dist/client/clients/base.d.ts.map +1 -0
- package/dist/client/clients/base.js +52 -0
- package/dist/client/clients/base.js.map +1 -0
- package/dist/client/clients/dsql.d.ts +24 -0
- package/dist/client/clients/dsql.d.ts.map +1 -0
- package/dist/client/clients/dsql.js +31 -0
- package/dist/client/clients/dsql.js.map +1 -0
- package/dist/client/clients/index.d.ts +6 -0
- package/dist/client/clients/index.d.ts.map +1 -0
- package/dist/client/clients/index.js +6 -0
- package/dist/client/clients/index.js.map +1 -0
- package/dist/client/clients/model.d.ts +169 -0
- package/dist/client/clients/model.d.ts.map +1 -0
- package/dist/client/{model/client.js → clients/model.js} +82 -53
- package/dist/client/clients/model.js.map +1 -0
- package/dist/client/clients/transaction.d.ts +9 -0
- package/dist/client/clients/transaction.d.ts.map +1 -0
- package/dist/client/{transaction/transaction-client.js → clients/transaction.js} +7 -7
- package/dist/client/clients/transaction.js.map +1 -0
- package/dist/client/clients/union.d.ts +39 -0
- package/dist/client/clients/union.d.ts.map +1 -0
- package/dist/client/clients/union.js +54 -0
- package/dist/client/clients/union.js.map +1 -0
- package/dist/client/create.d.ts +15 -18
- package/dist/client/create.d.ts.map +1 -1
- package/dist/client/create.js +10 -27
- package/dist/client/create.js.map +1 -1
- package/dist/client/index.d.ts +4 -8
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +3 -6
- package/dist/client/index.js.map +1 -1
- package/dist/client/normalizers/base.d.ts +467 -0
- package/dist/client/normalizers/base.d.ts.map +1 -0
- package/dist/client/normalizers/base.js.map +1 -0
- package/dist/client/normalizers/context.d.ts +14 -0
- package/dist/client/normalizers/context.d.ts.map +1 -0
- package/dist/client/normalizers/context.js +16 -0
- package/dist/client/normalizers/context.js.map +1 -0
- package/dist/client/normalizers/index.d.ts +8 -0
- package/dist/client/normalizers/index.d.ts.map +1 -0
- package/dist/client/normalizers/index.js +8 -0
- package/dist/client/normalizers/index.js.map +1 -0
- package/dist/client/normalizers/pagination.d.ts +87 -0
- package/dist/client/normalizers/pagination.d.ts.map +1 -0
- package/dist/client/normalizers/pagination.js +245 -0
- package/dist/client/normalizers/pagination.js.map +1 -0
- package/dist/client/normalizers/request.d.ts +41 -0
- package/dist/client/normalizers/request.d.ts.map +1 -0
- package/dist/client/normalizers/request.js +346 -0
- package/dist/client/normalizers/request.js.map +1 -0
- package/dist/client/normalizers/rules.d.ts +132 -0
- package/dist/client/normalizers/rules.d.ts.map +1 -0
- package/dist/client/normalizers/rules.js +82 -0
- package/dist/client/normalizers/rules.js.map +1 -0
- package/dist/client/normalizers/utils.d.ts +6 -0
- package/dist/client/normalizers/utils.d.ts.map +1 -0
- package/dist/client/normalizers/utils.js +18 -0
- package/dist/client/normalizers/utils.js.map +1 -0
- package/dist/client/normalizers/where.d.ts +39 -0
- package/dist/client/normalizers/where.d.ts.map +1 -0
- package/dist/client/normalizers/where.js +230 -0
- package/dist/client/normalizers/where.js.map +1 -0
- package/dist/client/utils/occ-retry.d.ts.map +1 -0
- package/dist/client/utils/occ-retry.js.map +1 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/schema/columns/guid.d.ts +21 -21
- package/dist/schema/columns/guid.d.ts.map +1 -1
- package/dist/schema/columns/guid.js +24 -25
- package/dist/schema/columns/guid.js.map +1 -1
- package/dist/schema/columns/numeric.d.ts +43 -2
- package/dist/schema/columns/numeric.d.ts.map +1 -1
- package/dist/schema/columns/numeric.js +42 -12
- package/dist/schema/columns/numeric.js.map +1 -1
- package/dist/schema/index.d.ts +1 -2
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema/index.js +0 -2
- package/dist/schema/index.js.map +1 -1
- package/dist/schema/relations.d.ts +22 -13
- package/dist/schema/relations.d.ts.map +1 -1
- package/dist/schema/relations.js +9 -1
- package/dist/schema/relations.js.map +1 -1
- package/dist/schema/table.d.ts +2 -2
- package/dist/schema/table.d.ts.map +1 -1
- package/dist/schema/table.js.map +1 -1
- package/dist/schema/union.d.ts +13 -8
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +14 -9
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/utils/global-id.d.ts +3 -3
- package/dist/schema/utils/global-id.js +3 -3
- package/dist/schema/utils/global-id.js.map +1 -1
- package/dist/schema/utils/guid-hooks.d.ts +49 -0
- package/dist/schema/utils/guid-hooks.d.ts.map +1 -0
- package/dist/schema/utils/guid-hooks.js +155 -0
- package/dist/schema/utils/guid-hooks.js.map +1 -0
- package/package.json +8 -6
- package/dist/client/database/base.d.ts +0 -99
- package/dist/client/database/base.d.ts.map +0 -1
- package/dist/client/database/base.js +0 -216
- package/dist/client/database/base.js.map +0 -1
- package/dist/client/database/client.d.ts +0 -26
- package/dist/client/database/client.d.ts.map +0 -1
- package/dist/client/database/client.js +0 -47
- package/dist/client/database/client.js.map +0 -1
- package/dist/client/database/index.d.ts +0 -65
- package/dist/client/database/index.d.ts.map +0 -1
- package/dist/client/database/index.js +0 -3
- package/dist/client/database/index.js.map +0 -1
- package/dist/client/model/base.d.ts +0 -747
- package/dist/client/model/base.d.ts.map +0 -1
- package/dist/client/model/base.js.map +0 -1
- package/dist/client/model/client.d.ts +0 -139
- package/dist/client/model/client.d.ts.map +0 -1
- package/dist/client/model/client.js.map +0 -1
- package/dist/client/model/filters.d.ts +0 -276
- package/dist/client/model/filters.d.ts.map +0 -1
- package/dist/client/model/filters.js +0 -470
- package/dist/client/model/filters.js.map +0 -1
- package/dist/client/model/normalizer.d.ts +0 -143
- package/dist/client/model/normalizer.d.ts.map +0 -1
- package/dist/client/model/normalizer.js +0 -641
- package/dist/client/model/normalizer.js.map +0 -1
- package/dist/client/nodes.d.ts +0 -62
- package/dist/client/nodes.d.ts.map +0 -1
- package/dist/client/nodes.js +0 -378
- package/dist/client/nodes.js.map +0 -1
- package/dist/client/pagination/cursor.d.ts +0 -40
- package/dist/client/pagination/cursor.d.ts.map +0 -1
- package/dist/client/pagination/cursor.js +0 -79
- package/dist/client/pagination/cursor.js.map +0 -1
- package/dist/client/pagination/page.d.ts +0 -23
- package/dist/client/pagination/page.d.ts.map +0 -1
- package/dist/client/pagination/page.js +0 -37
- package/dist/client/pagination/page.js.map +0 -1
- package/dist/client/transaction/index.d.ts +0 -2
- package/dist/client/transaction/index.d.ts.map +0 -1
- package/dist/client/transaction/index.js +0 -2
- package/dist/client/transaction/index.js.map +0 -1
- package/dist/client/transaction/occ-retry.d.ts.map +0 -1
- package/dist/client/transaction/occ-retry.js.map +0 -1
- package/dist/client/transaction/transaction-client.d.ts +0 -9
- package/dist/client/transaction/transaction-client.d.ts.map +0 -1
- package/dist/client/transaction/transaction-client.js.map +0 -1
- package/dist/client/union/client.d.ts +0 -55
- package/dist/client/union/client.d.ts.map +0 -1
- package/dist/client/union/client.js +0 -74
- package/dist/client/union/client.js.map +0 -1
- package/dist/schema/embedded.d.ts +0 -31
- package/dist/schema/embedded.d.ts.map +0 -1
- package/dist/schema/embedded.js +0 -33
- package/dist/schema/embedded.js.map +0 -1
- package/dist/schema/tenant.d.ts +0 -31
- package/dist/schema/tenant.d.ts.map +0 -1
- package/dist/schema/tenant.js +0 -33
- package/dist/schema/tenant.js.map +0 -1
- /package/dist/client/{model → normalizers}/base.js +0 -0
- /package/dist/client/{transaction → utils}/occ-retry.d.ts +0 -0
- /package/dist/client/{transaction → utils}/occ-retry.js +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/client/normalizers/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAG9E;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAE9E;AAED,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,GAAG,KAAK,IAAI,CAAC,CAEpE;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,IAAI,GAAG,SAAS,GAAG,KAAK,CAE/E;AAED,eAAO,MAAM,UAAU,GAAI,OAAO,OAAO,KAAG,MAE3C,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function isPlainObject(value) {
|
|
2
|
+
if (typeof value !== "object" || value === null)
|
|
3
|
+
return false;
|
|
4
|
+
return Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null;
|
|
5
|
+
}
|
|
6
|
+
export function isEmptyObject(value) {
|
|
7
|
+
return isPlainObject(value) && Object.keys(value).length === 0;
|
|
8
|
+
}
|
|
9
|
+
export function isDefined(value) {
|
|
10
|
+
return value !== null && value !== undefined;
|
|
11
|
+
}
|
|
12
|
+
export function isNullOrFalse(value) {
|
|
13
|
+
return !isDefined(value) || value === false;
|
|
14
|
+
}
|
|
15
|
+
export const escapeLike = (value) => {
|
|
16
|
+
return String(value).replace(/[\\%_]/g, "\\$&");
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/client/normalizers/utils.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,aAAa,CAAC,KAAc;IAC1C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,KAAK,CAAC;IAC9D,OAAO,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,KAAK,MAAM,CAAC,SAAS,IAAI,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC;AACpG,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,KAAc;IAC1C,OAAO,aAAa,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;AACjE,CAAC;AAED,MAAM,UAAU,SAAS,CAAI,KAA2B;IACtD,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC;AAC/C,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,KAAc;IAC1C,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,KAAK,KAAK,CAAC;AAC9C,CAAC;AAED,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,KAAc,EAAU,EAAE;IACnD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;AAClD,CAAC,CAAC","sourcesContent":["export function isPlainObject(value: unknown): value is Record<string, unknown> {\n if (typeof value !== \"object\" || value === null) return false;\n return Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null;\n}\n\nexport function isEmptyObject(value: unknown): value is Record<string, unknown> {\n return isPlainObject(value) && Object.keys(value).length === 0;\n}\n\nexport function isDefined<T>(value: T | null | undefined): value is T {\n return value !== null && value !== undefined;\n}\n\nexport function isNullOrFalse(value: unknown): value is null | undefined | false {\n return !isDefined(value) || value === false;\n}\n\nexport const escapeLike = (value: unknown): string => {\n return String(value).replace(/[\\\\%_]/g, \"\\\\$&\");\n};\n"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { AnyTable, AnyUnion, OrderByEntry, SQLNode } from "@dsqlbase/core";
|
|
2
|
+
import { ColumnTypeOf, FieldNamesOf, FilterOf, ValueTypeOf, WhereExpressionOf } from "./base.js";
|
|
3
|
+
export declare class WhereBuilder {
|
|
4
|
+
private _getFilterClause;
|
|
5
|
+
private _getPastClause;
|
|
6
|
+
buildColumnCondition<TTable extends AnyTable>(table: TTable, fieldName: FieldNamesOf<TTable>, condition: FilterOf<ColumnTypeOf<TTable, typeof fieldName>["runtimeType"], ValueTypeOf<ColumnTypeOf<TTable, typeof fieldName>>>): SQLNode[];
|
|
7
|
+
build<TTable extends AnyTable>(table: TTable, where: WhereExpressionOf<TTable> | null | undefined): SQLNode | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* The predicate selecting every row that sorts strictly after (or before) a cursor row, under
|
|
10
|
+
* the total order `keys`.
|
|
11
|
+
*
|
|
12
|
+
* Expanded key by key — `k0 > $a OR (k0 = $a AND (k1 > $b OR ...))` — rather than written as a
|
|
13
|
+
* row-value comparison, so mixed directions and nullable keys need no special case and nothing
|
|
14
|
+
* depends on DSQL supporting `(a, b) > ($1, $2)`.
|
|
15
|
+
*
|
|
16
|
+
* `values` are the database's own text for each key, bound as bare parameters: the server
|
|
17
|
+
* parses them against the column's type, so no codec may touch them. A codec that decodes to a
|
|
18
|
+
* JS value can lose precision (a `Date` has milliseconds, a `timestamptz` microseconds), and a
|
|
19
|
+
* cursor rebuilt from the lossy value skips rows. A `null` value is only valid for a nullable key.
|
|
20
|
+
*
|
|
21
|
+
* Per key, in the direction of travel (`before` reads every direction flipped, and flipping an
|
|
22
|
+
* order also flips where its nulls sort, so the table holds for both bounds):
|
|
23
|
+
*
|
|
24
|
+
* | travelling | cursor value | strictly past it | tied with it |
|
|
25
|
+
* | ---------- | ------------ | -------------------------- | ------------- |
|
|
26
|
+
* | ascending | `v` | `k > v`, or `k IS NULL`\* | `k = v` |
|
|
27
|
+
* | ascending | `NULL` | nothing (nulls are last) | `k IS NULL` |
|
|
28
|
+
* | descending | `v` | `k < v` | `k = v` |
|
|
29
|
+
* | descending | `NULL` | `k IS NOT NULL`\* | `k IS NULL` |
|
|
30
|
+
*
|
|
31
|
+
* \* nullable keys only.
|
|
32
|
+
*/
|
|
33
|
+
buildKeyset(table: AnyTable, keyset: OrderByEntry[], values: (string | null)[], projection: "after" | "before"): SQLNode;
|
|
34
|
+
unwrap<T extends AnyUnion>(union: T, where: WhereExpressionOf<T> | null | undefined): WhereExpressionOf<T> | null | undefined;
|
|
35
|
+
}
|
|
36
|
+
export declare const isAndEntry: <T extends AnyTable>(fieldName: string, condition: WhereExpressionOf<T> | WhereExpressionOf<T>[] | null | undefined) => condition is WhereExpressionOf<T>[];
|
|
37
|
+
export declare const isOrEntry: <T extends AnyTable>(fieldName: string, condition: WhereExpressionOf<T> | WhereExpressionOf<T>[] | null | undefined) => condition is WhereExpressionOf<T>[];
|
|
38
|
+
export declare const isNotEntry: <T extends AnyTable>(fieldName: string, condition: WhereExpressionOf<T> | null | undefined) => condition is WhereExpressionOf<T>;
|
|
39
|
+
//# sourceMappingURL=where.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"where.d.ts","sourceRoot":"","sources":["../../../src/client/normalizers/where.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAO,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAU3F,OAAO,EACL,YAAY,EACZ,YAAY,EAEZ,QAAQ,EACR,WAAW,EACX,iBAAiB,EAClB,MAAM,WAAW,CAAC;AAEnB,qBAAa,YAAY;IACvB,OAAO,CAAC,gBAAgB;IA0CxB,OAAO,CAAC,cAAc;IAoBf,oBAAoB,CAAC,MAAM,SAAS,QAAQ,EACjD,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,YAAY,CAAC,MAAM,CAAC,EAC/B,SAAS,EAAE,QAAQ,CACjB,YAAY,CAAC,MAAM,EAAE,OAAO,SAAS,CAAC,CAAC,aAAa,CAAC,EACrD,WAAW,CAAC,YAAY,CAAC,MAAM,EAAE,OAAO,SAAS,CAAC,CAAC,CACpD,GACA,OAAO,EAAE;IAyDL,KAAK,CAAC,MAAM,SAAS,QAAQ,EAClC,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,iBA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|
3
|
+
import { allowPlainObjectFilter, getColumnRules, isFilterObject, isNestedFilter, isRuleOperator, } from "./rules.js";
|
|
4
|
+
export class WhereBuilder {
|
|
5
|
+
_getFilterClause(column, value, operator) {
|
|
6
|
+
// TODO: handle dynamic columns filters, eg. guids
|
|
7
|
+
switch (operator) {
|
|
8
|
+
case "eq":
|
|
9
|
+
return sql.eq(column, column.param(value));
|
|
10
|
+
case "neq":
|
|
11
|
+
return sql.ne(column, column.param(value));
|
|
12
|
+
case "in":
|
|
13
|
+
return Array.isArray(value)
|
|
14
|
+
? sql.in(column, value.map((v) => column.param(v)))
|
|
15
|
+
: sql.in(column, []);
|
|
16
|
+
case "gt":
|
|
17
|
+
return sql.gt(column, column.param(value));
|
|
18
|
+
case "gte":
|
|
19
|
+
return sql.gte(column, column.param(value));
|
|
20
|
+
case "lt":
|
|
21
|
+
return sql.lt(column, column.param(value));
|
|
22
|
+
case "lte":
|
|
23
|
+
return sql.lte(column, column.param(value));
|
|
24
|
+
case "between":
|
|
25
|
+
return sql.between(column, column.param(value[0]), column.param(value[1]));
|
|
26
|
+
case "exists":
|
|
27
|
+
return value ? sql.isNotNull(column) : sql.isNull(column);
|
|
28
|
+
case "beginsWith":
|
|
29
|
+
return sql.like(column, `${escapeLike(value)}%`);
|
|
30
|
+
case "contains":
|
|
31
|
+
return sql.like(column, `%${escapeLike(value)}%`);
|
|
32
|
+
case "endsWith":
|
|
33
|
+
return sql.like(column, `%${escapeLike(value)}`);
|
|
34
|
+
default:
|
|
35
|
+
throw new Error(`Unsupported filter operator: ${operator}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
_getPastClause(node, key, value, projection) {
|
|
39
|
+
const ascending = (key[1][0] === "asc") === (projection === "after");
|
|
40
|
+
const nullable = isDefined(key[1][1]);
|
|
41
|
+
if (value === null) {
|
|
42
|
+
return ascending ? [] : [sql.isNotNull(node)];
|
|
43
|
+
}
|
|
44
|
+
if (!ascending) {
|
|
45
|
+
return [sql.lt(node, value)];
|
|
46
|
+
}
|
|
47
|
+
return nullable ? [sql.gt(node, value), sql.isNull(node)] : [sql.gt(node, value)];
|
|
48
|
+
}
|
|
49
|
+
buildColumnCondition(table, fieldName, condition) {
|
|
50
|
+
if (!isDefined(condition)) {
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
const column = table.getColumn(fieldName);
|
|
54
|
+
if (!column) {
|
|
55
|
+
throw new Error(`Unknown column: ${String(fieldName)}`);
|
|
56
|
+
}
|
|
57
|
+
const rules = getColumnRules(column);
|
|
58
|
+
const subject = `${column.runtimeType} column "${fieldName}" of "${table.name}"`;
|
|
59
|
+
if (!isFilterObject(condition)) {
|
|
60
|
+
if (!rules.shorthand) {
|
|
61
|
+
throw new Error(`Filter the ${subject} with one of its operators (${rules.operators.join(", ")}), ` +
|
|
62
|
+
`not a bare value.`);
|
|
63
|
+
}
|
|
64
|
+
if (isPlainObject(condition) && !allowPlainObjectFilter(column)) {
|
|
65
|
+
throw new Error(`Unknown operator ${Object.keys(condition)
|
|
66
|
+
.map((key) => `"${key}"`)
|
|
67
|
+
.join(", ")} in the filter on the ${subject} (valid: ${rules.operators.join(", ")}).`);
|
|
68
|
+
}
|
|
69
|
+
return [this._getFilterClause(column, condition, "eq")];
|
|
70
|
+
}
|
|
71
|
+
const clauses = [];
|
|
72
|
+
for (const [operator, value] of Object.entries(condition)) {
|
|
73
|
+
if (!isDefined(value)) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (isNestedFilter(operator)) {
|
|
77
|
+
throw new Error(`A nested \`where\` on the ${subject} is not supported yet.`);
|
|
78
|
+
}
|
|
79
|
+
if (!isRuleOperator(rules, operator)) {
|
|
80
|
+
throw new Error(`Operator "${operator}" is not valid on the ${subject} ` +
|
|
81
|
+
`(valid: ${rules.operators.join(", ")}).`);
|
|
82
|
+
}
|
|
83
|
+
clauses.push(this._getFilterClause(column, value, operator));
|
|
84
|
+
}
|
|
85
|
+
return clauses;
|
|
86
|
+
}
|
|
87
|
+
build(table, where) {
|
|
88
|
+
if (!isDefined(where)) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
const clauses = [];
|
|
92
|
+
for (const [fieldName, condition] of Object.entries(where)) {
|
|
93
|
+
if (isAndEntry(fieldName, condition) || isOrEntry(fieldName, condition)) {
|
|
94
|
+
const children = condition.map((expr) => this.build(table, expr)).filter(isDefined);
|
|
95
|
+
// TODO: Handle the case where the group is empty or matches every row.
|
|
96
|
+
// An empty group constrains nothing; left in, it would render as `()`. Nor does an `or`
|
|
97
|
+
// with a branch that matches every row (`{}`): dropping that branch would narrow it.
|
|
98
|
+
// const everyRow = fieldName === "or" && children.length < condition.length;
|
|
99
|
+
if (children.length) {
|
|
100
|
+
clauses.push(sql.wrap(fieldName === "and" ? sql.and(children) : sql.or(children)));
|
|
101
|
+
}
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (isNotEntry(fieldName, condition)) {
|
|
105
|
+
const shouldWrap = Object.values(condition).filter(isDefined).length > 1;
|
|
106
|
+
const clause = this.build(table, condition);
|
|
107
|
+
if (clause) {
|
|
108
|
+
clauses.push(shouldWrap ? sql.wrap(sql.not(clause)) : sql.not(clause));
|
|
109
|
+
}
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (table.hasColumn(fieldName)) {
|
|
113
|
+
clauses.push(...this.buildColumnCondition(table, fieldName, condition));
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
// TODO: Maybe consider adding support for relations or computed fields here.
|
|
117
|
+
throw new Error(`Unknown field name in where clause: ${fieldName}`);
|
|
118
|
+
}
|
|
119
|
+
return clauses.length > 0 ? sql.and(clauses) : undefined;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The predicate selecting every row that sorts strictly after (or before) a cursor row, under
|
|
123
|
+
* the total order `keys`.
|
|
124
|
+
*
|
|
125
|
+
* Expanded key by key — `k0 > $a OR (k0 = $a AND (k1 > $b OR ...))` — rather than written as a
|
|
126
|
+
* row-value comparison, so mixed directions and nullable keys need no special case and nothing
|
|
127
|
+
* depends on DSQL supporting `(a, b) > ($1, $2)`.
|
|
128
|
+
*
|
|
129
|
+
* `values` are the database's own text for each key, bound as bare parameters: the server
|
|
130
|
+
* parses them against the column's type, so no codec may touch them. A codec that decodes to a
|
|
131
|
+
* JS value can lose precision (a `Date` has milliseconds, a `timestamptz` microseconds), and a
|
|
132
|
+
* cursor rebuilt from the lossy value skips rows. A `null` value is only valid for a nullable key.
|
|
133
|
+
*
|
|
134
|
+
* Per key, in the direction of travel (`before` reads every direction flipped, and flipping an
|
|
135
|
+
* order also flips where its nulls sort, so the table holds for both bounds):
|
|
136
|
+
*
|
|
137
|
+
* | travelling | cursor value | strictly past it | tied with it |
|
|
138
|
+
* | ---------- | ------------ | -------------------------- | ------------- |
|
|
139
|
+
* | ascending | `v` | `k > v`, or `k IS NULL`\* | `k = v` |
|
|
140
|
+
* | ascending | `NULL` | nothing (nulls are last) | `k IS NULL` |
|
|
141
|
+
* | descending | `v` | `k < v` | `k = v` |
|
|
142
|
+
* | descending | `NULL` | `k IS NOT NULL`\* | `k IS NULL` |
|
|
143
|
+
*
|
|
144
|
+
* \* nullable keys only.
|
|
145
|
+
*/
|
|
146
|
+
buildKeyset(table, keyset, values, projection) {
|
|
147
|
+
if (keyset.length !== values.length) {
|
|
148
|
+
throw new Error(`A keyset pairs every key with one value (got ${keyset.length} keys, ${values.length} values).`);
|
|
149
|
+
}
|
|
150
|
+
for (const [index, key] of keyset.entries()) {
|
|
151
|
+
if (!key[1][1] && !isDefined(values[index])) {
|
|
152
|
+
throw new Error(`Keyset value ${index} is null, but its key is not nullable.`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// Built from the last key outwards, so each level wraps the one after it. `inner` is the
|
|
156
|
+
// predicate for the keys after this one; `undefined` means no row can satisfy it.
|
|
157
|
+
let inner;
|
|
158
|
+
let isOr = false;
|
|
159
|
+
for (let index = keyset.length - 1; index >= 0; index--) {
|
|
160
|
+
let node = table.getColumn(keyset[index][0]);
|
|
161
|
+
if (!node) {
|
|
162
|
+
node = sql.literal(keyset[index][0]);
|
|
163
|
+
}
|
|
164
|
+
const terms = this._getPastClause(node, keyset[index], values[index], projection);
|
|
165
|
+
if (inner) {
|
|
166
|
+
const tie = values[index] === null ? sql.isNull(node) : sql.eq(node, values[index]);
|
|
167
|
+
// An `OR` below needs parentheses to keep its precedence inside this `AND`.
|
|
168
|
+
const tied = sql.and([tie, isOr ? sql.wrap(inner) : inner]);
|
|
169
|
+
// Alone, the tie needs none: it is an `AND` joining whatever `AND` sits above it.
|
|
170
|
+
terms.push(terms.length > 0 ? sql.wrap(tied) : tied);
|
|
171
|
+
}
|
|
172
|
+
inner = terms.length === 0 ? undefined : terms.length === 1 ? terms[0] : sql.or(terms);
|
|
173
|
+
isOr = terms.length > 1;
|
|
174
|
+
}
|
|
175
|
+
// Nothing can sort past the cursor — only possible when the last key is a nullable one read
|
|
176
|
+
// ascending from a null. A primary key, which every page ends on, is never nullable.
|
|
177
|
+
return inner ?? sql.raw("FALSE");
|
|
178
|
+
}
|
|
179
|
+
unwrap(union, where) {
|
|
180
|
+
if (!isDefined(where)) {
|
|
181
|
+
return where;
|
|
182
|
+
}
|
|
183
|
+
const result = { ...where };
|
|
184
|
+
for (const [fieldName, condition] of Object.entries(where)) {
|
|
185
|
+
if (isAndEntry(fieldName, condition) || isOrEntry(fieldName, condition)) {
|
|
186
|
+
result[fieldName] = condition.map((entry) => this.unwrap(union, entry));
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (isNotEntry(fieldName, condition)) {
|
|
190
|
+
result[fieldName] = this.unwrap(union, condition);
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
const column = union.getColumn(fieldName);
|
|
194
|
+
if (!column || typeof column.hooks?.filter !== "function") {
|
|
195
|
+
result[fieldName] = condition;
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (!isFilterObject(condition)) {
|
|
199
|
+
result[fieldName] = column.hooks.filter(condition, { column });
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const unwrapped = {};
|
|
203
|
+
for (const [operator, value] of Object.entries(condition)) {
|
|
204
|
+
if (!isDefined(value)) {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if ((operator === "in" || operator === "or") && Array.isArray(value)) {
|
|
208
|
+
unwrapped[operator] = value.map((v) => column.hooks?.filter?.(v, { column }));
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
unwrapped[operator] = column.hooks?.filter?.(value, { column });
|
|
212
|
+
}
|
|
213
|
+
result[fieldName] = unwrapped;
|
|
214
|
+
}
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
export const isAndEntry = (fieldName, condition) => {
|
|
219
|
+
return fieldName === "and" && !isNullOrFalse(condition) && Array.isArray(condition);
|
|
220
|
+
};
|
|
221
|
+
export const isOrEntry = (fieldName, condition) => {
|
|
222
|
+
return fieldName === "or" && !isNullOrFalse(condition) && Array.isArray(condition);
|
|
223
|
+
};
|
|
224
|
+
export const isNotEntry = (fieldName, condition) => {
|
|
225
|
+
return (fieldName === "not" &&
|
|
226
|
+
!isNullOrFalse(condition) &&
|
|
227
|
+
typeof condition === "object" &&
|
|
228
|
+
!Array.isArray(condition));
|
|
229
|
+
};
|
|
230
|
+
//# sourceMappingURL=where.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"where.js","sourceRoot":"","sources":["../../../src/client/normalizers/where.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+C,GAAG,EAAW,MAAM,gBAAgB,CAAC;AAC3F,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACjF,OAAO,EACL,sBAAsB,EAEtB,cAAc,EACd,cAAc,EACd,cAAc,EACd,cAAc,GACf,MAAM,Y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ACrC,OAAO,CACL,SAAS,KAAK,KAAK;QACnB,CAAC,aAAa,CAAC,SAAS,CAAC;QACzB,OAAO,SAAS,KAAK,QAAQ;QAC7B,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,CAC1B,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { AnyColumn, AnyTable, AnyUnion, OrderByEntry, sql, SQLNode } from \"@dsqlbase/core\";\nimport { escapeLike, isDefined, isNullOrFalse, isPlainObject } from \"./utils.js\";\nimport {\n allowPlainObjectFilter,\n FilterOperator,\n getColumnRules,\n isFilterObject,\n isNestedFilter,\n isRuleOperator,\n} from \"./rules.js\";\nimport {\n ColumnTypeOf,\n FieldNamesOf,\n FilterCondition,\n FilterOf,\n ValueTypeOf,\n WhereExpressionOf,\n} from \"./base.js\";\n\nexport class WhereBuilder {\n private _getFilterClause<\n T extends AnyColumn,\n TOperator extends FilterOperator,\n TValue extends FilterCondition<ValueTypeOf<T[\"__type\"]>>[TOperator],\n >(column: T, value: TValue, operator: FilterOperator) {\n // TODO: handle dynamic columns filters, eg. guids\n\n switch (operator) {\n case \"eq\":\n return sql.eq(column, column.param(value));\n case \"neq\":\n return sql.ne(column, column.param(value));\n case \"in\":\n return Array.isArray(value)\n ? sql.in(\n column,\n value.map((v: ValueTypeOf<T[\"__type\"]>) => column.param(v))\n )\n : sql.in(column, []);\n case \"gt\":\n return sql.gt(column, column.param(value));\n case \"gte\":\n return sql.gte(column, column.param(value));\n case \"lt\":\n return sql.lt(column, column.param(value));\n case \"lte\":\n return sql.lte(column, column.param(value));\n case \"between\":\n return sql.between(column, column.param(value[0]), column.param(value[1]));\n case \"exists\":\n return value ? sql.isNotNull(column) : sql.isNull(column);\n case \"beginsWith\":\n return sql.like(column, `${escapeLike(value)}%`);\n case \"contains\":\n return sql.like(column, `%${escapeLike(value)}%`);\n case \"endsWith\":\n return sql.like(column, `%${escapeLike(value)}`);\n default:\n throw new Error(`Unsupported filter operator: ${operator}`);\n }\n }\n\n private _getPastClause(\n node: SQLNode,\n key: OrderByEntry,\n value: string | null,\n projection: \"after\" | \"before\"\n ): SQLNode[] {\n const ascending = (key[1][0] === \"asc\") === (projection === \"after\");\n const nullable = isDefined(key[1][1]);\n\n if (value === null) {\n return ascending ? [] : [sql.isNotNull(node)];\n }\n\n if (!ascending) {\n return [sql.lt(node, value)];\n }\n\n return nullable ? [sql.gt(node, value), sql.isNull(node)] : [sql.gt(node, value)];\n }\n\n public buildColumnCondition<TTable extends AnyTable>(\n table: TTable,\n fieldName: FieldNamesOf<TTable>,\n condition: FilterOf<\n ColumnTypeOf<TTable, typeof fieldName>[\"runtimeType\"],\n ValueTypeOf<ColumnTypeOf<TTable, typeof fieldName>>\n >\n ): SQLNode[] {\n if (!isDefined(condition)) {\n return [];\n }\n\n const column = table.getColumn(fieldName);\n\n if (!column) {\n throw new Error(`Unknown column: ${String(fieldName)}`);\n }\n\n const rules = getColumnRules(column);\n const subject = `${column.runtimeType} column \"${fieldName}\" of \"${table.name}\"`;\n\n if (!isFilterObject(condition)) {\n if (!rules.shorthand) {\n throw new Error(\n `Filter the ${subject} with one of its operators (${rules.operators.join(\", \")}), ` +\n `not a bare value.`\n );\n }\n\n if (isPlainObject(condition) && !allowPlainObjectFilter(column)) {\n throw new Error(\n `Unknown operator ${Object.keys(condition)\n .map((key) => `\"${key}\"`)\n .join(\", \")} in the filter on the ${subject} (valid: ${rules.operators.join(\", \")}).`\n );\n }\n\n return [this._getFilterClause(column, condition, \"eq\")];\n }\n\n const clauses: SQLNode[] = [];\n\n for (const [operator, value] of Object.entries(condition)) {\n if (!isDefined(value)) {\n continue;\n }\n\n if (isNestedFilter(operator)) {\n throw new Error(`A nested \\`where\\` on the ${subject} is not supported yet.`);\n }\n\n if (!isRuleOperator(rules, operator)) {\n throw new Error(\n `Operator \"${operator}\" is not valid on the ${subject} ` +\n `(valid: ${rules.operators.join(\", \")}).`\n );\n }\n\n clauses.push(this._getFilterClause(column, value, operator));\n }\n\n return clauses;\n }\n\n public build<TTable extends AnyTable>(\n table: TTable,\n where: WhereExpressionOf<TTable> | null | undefined\n ): SQLNode | undefined {\n if (!isDefined(where)) {\n return undefined;\n }\n\n const clauses: SQLNode[] = [];\n\n for (const [fieldName, condition] of Object.entries(where)) {\n if (isAndEntry(fieldName, condition) || isOrEntry(fieldName, condition)) {\n const children = condition.map((expr) => this.build(table, expr)).filter(isDefined);\n\n // TODO: Handle the case where the group is empty or matches every row.\n // An empty group constrains nothing; left in, it would render as `()`. Nor does an `or`\n // with a branch that matches every row (`{}`): dropping that branch would narrow it.\n // const everyRow = fieldName === \"or\" && children.length < condition.length;\n\n if (children.length) {\n clauses.push(sql.wrap(fieldName === \"and\" ? sql.and(children) : sql.or(children)));\n }\n\n continue;\n }\n\n if (isNotEntry(fieldName, condition)) {\n const shouldWrap = Object.values(condition).filter(isDefined).length > 1;\n const clause = this.build(table, condition);\n\n if (clause) {\n clauses.push(shouldWrap ? sql.wrap(sql.not(clause)) : sql.not(clause));\n }\n\n continue;\n }\n\n if (table.hasColumn(fieldName)) {\n clauses.push(\n ...this.buildColumnCondition(table, fieldName as FieldNamesOf<TTable>, condition)\n );\n continue;\n }\n\n // TODO: Maybe consider adding support for relations or computed fields here.\n\n throw new Error(`Unknown field name in where clause: ${fieldName}`);\n }\n\n return clauses.length > 0 ? sql.and(clauses) : undefined;\n }\n\n /**\n * The predicate selecting every row that sorts strictly after (or before) a cursor row, under\n * the total order `keys`.\n *\n * Expanded key by key — `k0 > $a OR (k0 = $a AND (k1 > $b OR ...))` — rather than written as a\n * row-value comparison, so mixed directions and nullable keys need no special case and nothing\n * depends on DSQL supporting `(a, b) > ($1, $2)`.\n *\n * `values` are the database's own text for each key, bound as bare parameters: the server\n * parses them against the column's type, so no codec may touch them. A codec that decodes to a\n * JS value can lose precision (a `Date` has milliseconds, a `timestamptz` microseconds), and a\n * cursor rebuilt from the lossy value skips rows. A `null` value is only valid for a nullable key.\n *\n * Per key, in the direction of travel (`before` reads every direction flipped, and flipping an\n * order also flips where its nulls sort, so the table holds for both bounds):\n *\n * | travelling | cursor value | strictly past it | tied with it |\n * | ---------- | ------------ | -------------------------- | ------------- |\n * | ascending | `v` | `k > v`, or `k IS NULL`\\* | `k = v` |\n * | ascending | `NULL` | nothing (nulls are last) | `k IS NULL` |\n * | descending | `v` | `k < v` | `k = v` |\n * | descending | `NULL` | `k IS NOT NULL`\\* | `k IS NULL` |\n *\n * \\* nullable keys only.\n */\n\n public buildKeyset(\n table: AnyTable,\n keyset: OrderByEntry[],\n values: (string | null)[],\n projection: \"after\" | \"before\"\n ) {\n if (keyset.length !== values.length) {\n throw new Error(\n `A keyset pairs every key with one value (got ${keyset.length} keys, ${values.length} values).`\n );\n }\n\n for (const [index, key] of keyset.entries()) {\n if (!key[1][1] && !isDefined(values[index])) {\n throw new Error(`Keyset value ${index} is null, but its key is not nullable.`);\n }\n }\n\n // Built from the last key outwards, so each level wraps the one after it. `inner` is the\n // predicate for the keys after this one; `undefined` means no row can satisfy it.\n let inner: SQLNode | undefined;\n let isOr = false;\n\n for (let index = keyset.length - 1; index >= 0; index--) {\n let node: SQLNode | undefined = table.getColumn(keyset[index][0]);\n\n if (!node) {\n node = sql.literal(keyset[index][0]);\n }\n\n const terms = this._getPastClause(node, keyset[index], values[index], projection);\n\n if (inner) {\n const tie = values[index] === null ? sql.isNull(node) : sql.eq(node, values[index]);\n\n // An `OR` below needs parentheses to keep its precedence inside this `AND`.\n const tied = sql.and([tie, isOr ? sql.wrap(inner) : inner]);\n\n // Alone, the tie needs none: it is an `AND` joining whatever `AND` sits above it.\n terms.push(terms.length > 0 ? sql.wrap(tied) : tied);\n }\n\n inner = terms.length === 0 ? undefined : terms.length === 1 ? terms[0] : sql.or(terms);\n isOr = terms.length > 1;\n }\n\n // Nothing can sort past the cursor — only possible when the last key is a nullable one read\n // ascending from a null. A primary key, which every page ends on, is never nullable.\n return inner ?? sql.raw(\"FALSE\");\n }\n\n public unwrap<T extends AnyUnion>(\n union: T,\n where: WhereExpressionOf<T> | null | undefined\n ): WhereExpressionOf<T> | null | undefined {\n if (!isDefined(where)) {\n return where;\n }\n\n const result: Record<string, unknown> = { ...where };\n\n for (const [fieldName, condition] of Object.entries(where)) {\n if (isAndEntry(fieldName, condition) || isOrEntry(fieldName, condition)) {\n result[fieldName] = condition.map((entry) => this.unwrap(union, entry));\n continue;\n }\n\n if (isNotEntry(fieldName, condition)) {\n result[fieldName] = this.unwrap(union, condition);\n continue;\n }\n\n const column = union.getColumn(fieldName);\n\n if (!column || typeof column.hooks?.filter !== \"function\") {\n result[fieldName] = condition;\n continue;\n }\n\n if (!isFilterObject(condition)) {\n result[fieldName] = column.hooks.filter(condition, { column });\n continue;\n }\n\n const unwrapped: Record<string, unknown> = {};\n\n for (const [operator, value] of Object.entries(condition)) {\n if (!isDefined(value)) {\n continue;\n }\n\n if ((operator === \"in\" || operator === \"or\") && Array.isArray(value)) {\n unwrapped[operator] = value.map((v) => column.hooks?.filter?.(v, { column }));\n continue;\n }\n\n unwrapped[operator] = column.hooks?.filter?.(value, { column });\n }\n\n result[fieldName] = unwrapped;\n }\n\n return result as WhereExpressionOf<T>;\n }\n}\n\nexport const isAndEntry = <T extends AnyTable>(\n fieldName: string,\n condition: WhereExpressionOf<T> | WhereExpressionOf<T>[] | null | undefined\n): condition is WhereExpressionOf<T>[] => {\n return fieldName === \"and\" && !isNullOrFalse(condition) && Array.isArray(condition);\n};\n\nexport const isOrEntry = <T extends AnyTable>(\n fieldName: string,\n condition: WhereExpressionOf<T> | WhereExpressionOf<T>[] | null | undefined\n): condition is WhereExpressionOf<T>[] => {\n return fieldName === \"or\" && !isNullOrFalse(condition) && Array.isArray(condition);\n};\n\nexport const isNotEntry = <T extends AnyTable>(\n fieldName: string,\n condition: WhereExpressionOf<T> | null | undefined\n): condition is WhereExpressionOf<T> => {\n return (\n fieldName === \"not\" &&\n !isNullOrFalse(condition) &&\n typeof condition === \"object\" &&\n !Array.isArray(condition)\n );\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"occ-retry.d.ts","sourceRoot":"","sources":["../../../src/client/utils/occ-retry.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,eAAe,GAAG;IAC5B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAIzF;AAED,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE/C;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAGlD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"occ-retry.js","sourceRoot":"","sources":["../../../src/client/utils/occ-retry.ts"],"names":[],"mappings":"AAMA,MAAM,UAAU,YAAY,CAAC,OAAe,EAAE,SAAiB,EAAE,QAAgB;IAC/E,MAAM,KAAK,GAAG,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;IAC/C,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,GAAG,GAAG,CAAC;IAC3C,OAAO,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,MAAM,EAAE,QAAQ,CAAC,CAAC;AAC5C,CAAC;AAED,MAAM,UAAU,KAAK,CAAC,EAAU;IAC9B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAC3D,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,KAAc;IACvC,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IACtD,OAAQ,KAA2B,CAAC,IAAI,KAAK,OAAO,CAAC;AACvD,CAAC","sourcesContent":["export type OCCRetryOptions = {\n maxRetries?: number;\n delay?: number;\n maxDelay?: number;\n};\n\nexport function backoffDelay(attempt: number, baseDelay: number, maxDelay: number): number {\n const delay = baseDelay * Math.pow(2, attempt);\n const jitter = Math.random() * delay * 0.1;\n return Math.min(delay + jitter, maxDelay);\n}\n\nexport function sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nexport function isOccError(error: unknown): boolean {\n if (!error || typeof error !== \"object\") return false;\n return (error as { code?: string }).code === \"40001\";\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
export { sql, SQLQuery,
|
|
1
|
+
export { sql, SQLQuery, PolicyViolationError } from "@dsqlbase/core";
|
|
2
2
|
export type { Session, SQLStatement } from "@dsqlbase/core";
|
|
3
3
|
export { GLOBAL_ID_PREFIX, GlobalIdError, decodeGlobalId, encodeGlobalId, isGlobalId, type GlobalId, type GlobalIdErrorCode, } from "./schema/utils/global-id.js";
|
|
4
4
|
export { ColumnValidationError, type ColumnValidationErrorCode, } from "./schema/utils/column-validation.js";
|
|
5
|
-
export {
|
|
6
|
-
export { createClient, type ClaimsOf, type ClientOptions, type IdentityClient, type QueryClient, } from "./client/index.js";
|
|
5
|
+
export { createClient, type ClientOptions } from "./client/index.js";
|
|
7
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AACrE,YAAY,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EACL,gBAAgB,EAChB,aAAa,EACb,cAAc,EACd,cAAc,EACd,UAAU,EACV,KAAK,QAAQ,EACb,KAAK,iBAAiB,GACvB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EACL,qBAAqB,EACrB,KAAK,yBAAyB,GAC/B,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
export { sql, SQLQuery,
|
|
1
|
+
export { sql, SQLQuery, PolicyViolationError } from "@dsqlbase/core";
|
|
2
2
|
export { GLOBAL_ID_PREFIX, GlobalIdError, decodeGlobalId, encodeGlobalId, isGlobalId, } from "./schema/utils/global-id.js";
|
|
3
3
|
export { ColumnValidationError, } from "./schema/utils/column-validation.js";
|
|
4
|
-
export {
|
|
5
|
-
export { createClient, } from "./client/index.js";
|
|
4
|
+
export { createClient } from "./client/index.js";
|
|
6
5
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAErE,OAAO,EACL,gBAAgB,EAChB,aAAa,EACb,cAAc,EACd,cAAc,EACd,UAAU,GAGX,MAAM,6BAA6B,CAAC;AACrC,OAAO,EACL,qBAAqB,GAEtB,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAsB,MAAM,mBAAmB,CAAC","sourcesContent":["export { sql, SQLQuery, PolicyViolationError } from \"@dsqlbase/core\";\nexport type { Session, SQLStatement } from \"@dsqlbase/core\";\nexport {\n GLOBAL_ID_PREFIX,\n GlobalIdError,\n decodeGlobalId,\n encodeGlobalId,\n isGlobalId,\n type GlobalId,\n type GlobalIdErrorCode,\n} from \"./schema/utils/global-id.js\";\nexport {\n ColumnValidationError,\n type ColumnValidationErrorCode,\n} from \"./schema/utils/column-validation.js\";\nexport { createClient, type ClientOptions } from \"./client/index.js\";\n"]}
|
|
@@ -13,45 +13,45 @@ export type Guid<T extends TypedObject> = T & {
|
|
|
13
13
|
};
|
|
14
14
|
};
|
|
15
15
|
export declare class GuidColumnDefinition<TName extends string, TConfig extends ColumnConfig> extends UUIDColumnDefinition<TName, TConfig> {
|
|
16
|
-
/**
|
|
17
|
-
* The node key this column's values name, when the declaration overrode it.
|
|
18
|
-
*
|
|
19
|
-
* `undefined` means "the table this column is declared on", resolved to that table's schema
|
|
20
|
-
* alias when the client is built — a column cannot know its own alias, since the alias is
|
|
21
|
-
* the key it is exported under.
|
|
22
|
-
*/
|
|
23
|
-
protected _guidKey: string | undefined;
|
|
24
|
-
constructor(name: TName, config?: Partial<TConfig>, key?: string);
|
|
25
16
|
}
|
|
26
17
|
/**
|
|
27
18
|
* Defines a `uuid` column whose values are global ids — opaque strings naming both the row and
|
|
28
19
|
* the table it belongs to, rather than a bare uuid that could have come from anywhere.
|
|
29
20
|
*
|
|
30
|
-
* A
|
|
31
|
-
*
|
|
32
|
-
* table
|
|
21
|
+
* A guid primary key makes a table's rows emit their own ids. They are keyed by the table's
|
|
22
|
+
* schema alias, so `dsql.members` produces ids keyed `members` even though the table is
|
|
23
|
+
* `team_members`. An id resolves by any of the table's unique keys, not only this one.
|
|
24
|
+
*
|
|
25
|
+
* What a column's ids name comes from the relations, through the column's hooks
|
|
26
|
+
* (`../utils/guid-hooks.ts`):
|
|
27
|
+
*
|
|
28
|
+
* - a relation key column — the `from` of a `belongsTo`, the `to` of a `hasMany` / `hasOne` —
|
|
29
|
+
* names the column it is paired with, so `article.authorId === article.author.id` holds
|
|
30
|
+
* without the application unwrapping either;
|
|
31
|
+
* - the `from` of a `belongsTo` a union names whichever member its discriminator says;
|
|
32
|
+
* - any other column names its own table and field: a primary key is the table's node id, and an
|
|
33
|
+
* unrelated `photos.ownerId` reads as `["photos", { ownerId }]`.
|
|
33
34
|
*
|
|
34
35
|
* @param name The database column name.
|
|
35
|
-
* @param key The node this column *points at*, for a column that is not its own table's key.
|
|
36
|
-
* A relation key column takes the alias of the table it references, so
|
|
37
|
-
* `article.authorId === article.author.id` holds without the application unwrapping either.
|
|
38
|
-
* Omit it on a primary key, and on a self reference.
|
|
39
36
|
*
|
|
40
37
|
* @example
|
|
41
38
|
* ```ts
|
|
42
39
|
* export const authors = table("authors", {
|
|
43
|
-
* id: guid("id").primaryKey().defaultRandom(),
|
|
40
|
+
* id: guid("id").primaryKey().defaultRandom(), // node key: "authors"
|
|
44
41
|
* });
|
|
45
42
|
*
|
|
46
43
|
* export const articles = table("articles", {
|
|
47
|
-
* id: guid("id").primaryKey().defaultRandom(),
|
|
48
|
-
* authorId: guid("author_id"
|
|
49
|
-
*
|
|
44
|
+
* id: guid("id").primaryKey().defaultRandom(), // node key: "articles"
|
|
45
|
+
* authorId: guid("author_id").notNull(), // an authors id, through the relation below
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* export const articleRelations = relations(articles, {
|
|
49
|
+
* author: belongsTo(authors, { from: [articles.columns.authorId], to: [authors.columns.id] }),
|
|
50
50
|
* });
|
|
51
51
|
* ```
|
|
52
52
|
*
|
|
53
53
|
* Migration-neutral: a `guid()` column serializes exactly as `uuid()`, so adopting it on an
|
|
54
54
|
* existing column produces no DDL.
|
|
55
55
|
*/
|
|
56
|
-
export declare function guid<const TName extends string>(name: TName
|
|
56
|
+
export declare function guid<const TName extends string>(name: TName): Guid<GuidColumnDefinition<TName, ColumnConfig<string, string, "uuid">>>;
|
|
57
57
|
//# sourceMappingURL=guid.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guid.d.ts","sourceRoot":"","sources":["../../../src/schema/columns/guid.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"guid.d.ts","sourceRoot":"","sources":["../../../src/schema/columns/guid.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAEnD,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAEjD;;;;;GAKG;AACH,MAAM,MAAM,IAAI,CAAC,CAAC,SAAS,WAAW,IAAI,CAAC,GAAG;IAAE,MAAM,EAAE;QAAE,IAAI,EAAE,IAAI,CAAA;KAAE,CAAA;CAAE,CAAC;AAEzE,qBAAa,oBAAoB,CAC/B,KAAK,SAAS,MAAM,EACpB,OAAO,SAAS,YAAY,CAC5B,SAAQ,oBAAoB,CAAC,KAAK,EAAE,OAAO,CAAC;CAAG;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,wBAAgB,IAAI,CAAC,KAAK,CAAC,KAAK,SAAS,MAAM,EAAE,IAAI,EAAE,KAAK,GAMpD,IAAI,CAAC,oBAAoB,CAAC,KAAK,EAAE,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAC9E"}
|
|
@@ -1,53 +1,52 @@
|
|
|
1
|
+
import { guidHooks } from "../utils/guid-hooks.js";
|
|
1
2
|
import { UUIDColumnDefinition } from "./uuid.js";
|
|
2
3
|
export class GuidColumnDefinition extends UUIDColumnDefinition {
|
|
3
|
-
/**
|
|
4
|
-
* The node key this column's values name, when the declaration overrode it.
|
|
5
|
-
*
|
|
6
|
-
* `undefined` means "the table this column is declared on", resolved to that table's schema
|
|
7
|
-
* alias when the client is built — a column cannot know its own alias, since the alias is
|
|
8
|
-
* the key it is exported under.
|
|
9
|
-
*/
|
|
10
|
-
_guidKey;
|
|
11
|
-
constructor(name, config = {}, key) {
|
|
12
|
-
super(name, config);
|
|
13
|
-
this._guidKey = key;
|
|
14
|
-
}
|
|
15
4
|
}
|
|
16
5
|
/**
|
|
17
6
|
* Defines a `uuid` column whose values are global ids — opaque strings naming both the row and
|
|
18
7
|
* the table it belongs to, rather than a bare uuid that could have come from anywhere.
|
|
19
8
|
*
|
|
20
|
-
* A
|
|
21
|
-
*
|
|
22
|
-
* table
|
|
9
|
+
* A guid primary key makes a table's rows emit their own ids. They are keyed by the table's
|
|
10
|
+
* schema alias, so `dsql.members` produces ids keyed `members` even though the table is
|
|
11
|
+
* `team_members`. An id resolves by any of the table's unique keys, not only this one.
|
|
12
|
+
*
|
|
13
|
+
* What a column's ids name comes from the relations, through the column's hooks
|
|
14
|
+
* (`../utils/guid-hooks.ts`):
|
|
15
|
+
*
|
|
16
|
+
* - a relation key column — the `from` of a `belongsTo`, the `to` of a `hasMany` / `hasOne` —
|
|
17
|
+
* names the column it is paired with, so `article.authorId === article.author.id` holds
|
|
18
|
+
* without the application unwrapping either;
|
|
19
|
+
* - the `from` of a `belongsTo` a union names whichever member its discriminator says;
|
|
20
|
+
* - any other column names its own table and field: a primary key is the table's node id, and an
|
|
21
|
+
* unrelated `photos.ownerId` reads as `["photos", { ownerId }]`.
|
|
23
22
|
*
|
|
24
23
|
* @param name The database column name.
|
|
25
|
-
* @param key The node this column *points at*, for a column that is not its own table's key.
|
|
26
|
-
* A relation key column takes the alias of the table it references, so
|
|
27
|
-
* `article.authorId === article.author.id` holds without the application unwrapping either.
|
|
28
|
-
* Omit it on a primary key, and on a self reference.
|
|
29
24
|
*
|
|
30
25
|
* @example
|
|
31
26
|
* ```ts
|
|
32
27
|
* export const authors = table("authors", {
|
|
33
|
-
* id: guid("id").primaryKey().defaultRandom(),
|
|
28
|
+
* id: guid("id").primaryKey().defaultRandom(), // node key: "authors"
|
|
34
29
|
* });
|
|
35
30
|
*
|
|
36
31
|
* export const articles = table("articles", {
|
|
37
|
-
* id: guid("id").primaryKey().defaultRandom(),
|
|
38
|
-
* authorId: guid("author_id"
|
|
39
|
-
*
|
|
32
|
+
* id: guid("id").primaryKey().defaultRandom(), // node key: "articles"
|
|
33
|
+
* authorId: guid("author_id").notNull(), // an authors id, through the relation below
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* export const articleRelations = relations(articles, {
|
|
37
|
+
* author: belongsTo(authors, { from: [articles.columns.authorId], to: [authors.columns.id] }),
|
|
40
38
|
* });
|
|
41
39
|
* ```
|
|
42
40
|
*
|
|
43
41
|
* Migration-neutral: a `guid()` column serializes exactly as `uuid()`, so adopting it on an
|
|
44
42
|
* existing column produces no DDL.
|
|
45
43
|
*/
|
|
46
|
-
export function guid(name
|
|
44
|
+
export function guid(name) {
|
|
47
45
|
return new GuidColumnDefinition(name, {
|
|
48
46
|
dataType: "uuid",
|
|
49
47
|
runtimeType: "uuid",
|
|
50
48
|
codec: { encode: (value) => value, decode: (value) => value },
|
|
51
|
-
|
|
49
|
+
hooks: guidHooks,
|
|
50
|
+
});
|
|
52
51
|
}
|
|
53
52
|
//# sourceMappingURL=guid.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guid.js","sourceRoot":"","sources":["../../../src/schema/columns/guid.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"guid.js","sourceRoot":"","sources":["../../../src/schema/columns/guid.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAUjD,MAAM,OAAO,oBAGX,SAAQ,oBAAoC;CAAG;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAM,UAAU,IAAI,CAA6B,IAAW;IAC1D,OAAO,IAAI,oBAAoB,CAA8C,IAAI,EAAE;QACjF,QAAQ,EAAE,MAAM;QAChB,WAAW,EAAE,MAAM;QACnB,KAAK,EAAE,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,EAAE;QAC7D,KAAK,EAAE,SAAS;KACjB,CAA4E,CAAC;AAChF,CAAC","sourcesContent":["import { ColumnConfig } from \"@dsqlbase/core\";\nimport { TypedObject } from \"@dsqlbase/core/utils\";\nimport { guidHooks } from \"../utils/guid-hooks.js\";\nimport { UUIDColumnDefinition } from \"./uuid.js\";\n\n/**\n * Marks a column definition as carrying global ids.\n *\n * Follows the `NotNull` / `ReadOnly` shape: a refinement written into `__type`, so it survives\n * `.primaryKey()`, `.notNull()` and `.defaultRandom()`, which all return `this` re-typed.\n */\nexport type Guid<T extends TypedObject> = T & { __type: { guid: true } };\n\nexport class GuidColumnDefinition<\n TName extends string,\n TConfig extends ColumnConfig,\n> extends UUIDColumnDefinition<TName, TConfig> {}\n\n/**\n * Defines a `uuid` column whose values are global ids — opaque strings naming both the row and\n * the table it belongs to, rather than a bare uuid that could have come from anywhere.\n *\n * A guid primary key makes a table's rows emit their own ids. They are keyed by the table's\n * schema alias, so `dsql.members` produces ids keyed `members` even though the table is\n * `team_members`. An id resolves by any of the table's unique keys, not only this one.\n *\n * What a column's ids name comes from the relations, through the column's hooks\n * (`../utils/guid-hooks.ts`):\n *\n * - a relation key column — the `from` of a `belongsTo`, the `to` of a `hasMany` / `hasOne` —\n * names the column it is paired with, so `article.authorId === article.author.id` holds\n * without the application unwrapping either;\n * - the `from` of a `belongsTo` a union names whichever member its discriminator says;\n * - any other column names its own table and field: a primary key is the table's node id, and an\n * unrelated `photos.ownerId` reads as `[\"photos\", { ownerId }]`.\n *\n * @param name The database column name.\n *\n * @example\n * ```ts\n * export const authors = table(\"authors\", {\n * id: guid(\"id\").primaryKey().defaultRandom(), // node key: \"authors\"\n * });\n *\n * export const articles = table(\"articles\", {\n * id: guid(\"id\").primaryKey().defaultRandom(), // node key: \"articles\"\n * authorId: guid(\"author_id\").notNull(), // an authors id, through the relation below\n * });\n *\n * export const articleRelations = relations(articles, {\n * author: belongsTo(authors, { from: [articles.columns.authorId], to: [authors.columns.id] }),\n * });\n * ```\n *\n * Migration-neutral: a `guid()` column serializes exactly as `uuid()`, so adopting it on an\n * existing column produces no DDL.\n */\nexport function guid<const TName extends string>(name: TName) {\n return new GuidColumnDefinition<TName, ColumnConfig<string, string, \"uuid\">>(name, {\n dataType: \"uuid\",\n runtimeType: \"uuid\",\n codec: { encode: (value) => value, decode: (value) => value },\n hooks: guidHooks,\n }) as Guid<GuidColumnDefinition<TName, ColumnConfig<string, string, \"uuid\">>>;\n}\n"]}
|