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
|
@@ -1,10 +1,50 @@
|
|
|
1
1
|
import { ColumnConfig, ColumnDefinition } from "@dsqlbase/core";
|
|
2
|
+
/**
|
|
3
|
+
* An exact decimal value from a library such as decimal.js, big.js or bignumber.js. Its
|
|
4
|
+
* `toString()` must print the value in a form Postgres reads as `numeric` — plain or exponent
|
|
5
|
+
* notation.
|
|
6
|
+
*/
|
|
7
|
+
export type DecimalLike = {
|
|
8
|
+
toString(): string;
|
|
9
|
+
toJSON(): string;
|
|
10
|
+
};
|
|
11
|
+
/** A class that builds a {@link DecimalLike} from the database's decimal text. */
|
|
12
|
+
export type DecimalConstructor<T extends DecimalLike = DecimalLike> = new (value: string) => T;
|
|
13
|
+
/**
|
|
14
|
+
* How a `numeric` value is presented: a JavaScript `number` (may lose precision), the exact
|
|
15
|
+
* decimal text, or an instance of a decimal class.
|
|
16
|
+
*/
|
|
17
|
+
export type NumericMode = "number" | "string" | DecimalConstructor;
|
|
2
18
|
export type NumericOptions = {
|
|
3
19
|
/** Total significant digits, 1–1000. @default 18 */
|
|
4
20
|
precision?: number;
|
|
5
21
|
/** Digits after the decimal point, 0–`precision`. @default 6, or 0 when `precision` is given */
|
|
6
22
|
scale?: number;
|
|
23
|
+
/**
|
|
24
|
+
* How values are read:
|
|
25
|
+
* - `"number"`: a JavaScript `number`. Values past ~15 significant digits lose precision.
|
|
26
|
+
* - `"string"`: the exact decimal text, e.g. `"12.500000"`.
|
|
27
|
+
* - a decimal class (`Decimal` from decimal.js, `Big`, `BigNumber`, …): an instance of it,
|
|
28
|
+
* built from the exact decimal text.
|
|
29
|
+
*
|
|
30
|
+
* In `"string"` and class modes a write also accepts a `number` or a decimal string.
|
|
31
|
+
*
|
|
32
|
+
* @default "number"
|
|
33
|
+
*/
|
|
34
|
+
mode?: NumericMode;
|
|
7
35
|
};
|
|
36
|
+
/** What a read of a `numeric` column with options `O` returns. */
|
|
37
|
+
export type NumericValueType<O> = O extends {
|
|
38
|
+
mode: "string";
|
|
39
|
+
} ? string : O extends {
|
|
40
|
+
mode: DecimalConstructor<infer T>;
|
|
41
|
+
} ? T : number;
|
|
42
|
+
/** What a write to a `numeric` column with options `O` accepts. */
|
|
43
|
+
export type NumericInputType<O> = O extends {
|
|
44
|
+
mode: "string";
|
|
45
|
+
} ? string | number : O extends {
|
|
46
|
+
mode: DecimalConstructor<infer T>;
|
|
47
|
+
} ? T | string | number : number;
|
|
8
48
|
/**
|
|
9
49
|
* DSQL's own precision and scale for a `numeric` declared without them, stated explicitly so
|
|
10
50
|
* Postgres and PGlite store the same type.
|
|
@@ -20,9 +60,10 @@ export declare const DEFAULT_NUMERIC_SCALE = 6;
|
|
|
20
60
|
* it would refuse — throws a {@link ColumnValidationError} instead.
|
|
21
61
|
*
|
|
22
62
|
* @param name Column name in database
|
|
23
|
-
* @param options Precision and scale; `{ precision }` alone means a scale of 0, as `numeric(p)
|
|
63
|
+
* @param options Precision and scale; `{ precision }` alone means a scale of 0, as `numeric(p)`.
|
|
64
|
+
* `mode` picks how values are read; see {@link NumericOptions.mode}.
|
|
24
65
|
* @returns Serializable column definition for a numeric column.
|
|
25
66
|
*/
|
|
26
|
-
export declare function numeric<const TName extends string>(name: TName, options?:
|
|
67
|
+
export declare function numeric<const TName extends string, const TOptions extends NumericOptions = NumericOptions>(name: TName, options?: TOptions): ColumnDefinition<TName, ColumnConfig<NumericValueType<TOptions>, string, "number", NumericInputType<TOptions>>>;
|
|
27
68
|
export { numeric as decimal };
|
|
28
69
|
//# sourceMappingURL=numeric.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"numeric.d.ts","sourceRoot":"","sources":["../../../src/schema/columns/numeric.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAGhE,MAAM,MAAM,cAAc,GAAG;IAC3B,oDAAoD;IACpD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,gGAAgG;IAChG,KAAK,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"numeric.d.ts","sourceRoot":"","sources":["../../../src/schema/columns/numeric.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAGhE;;;;GAIG;AACH,MAAM,MAAM,WAAW,GAA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AK,EAAE,OAAO,GAAE,QAAyB,mHAwEhD;AAqCD,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,CAAC"}
|
|
@@ -15,12 +15,14 @@ export const DEFAULT_NUMERIC_SCALE = 6;
|
|
|
15
15
|
* it would refuse — throws a {@link ColumnValidationError} instead.
|
|
16
16
|
*
|
|
17
17
|
* @param name Column name in database
|
|
18
|
-
* @param options Precision and scale; `{ precision }` alone means a scale of 0, as `numeric(p)
|
|
18
|
+
* @param options Precision and scale; `{ precision }` alone means a scale of 0, as `numeric(p)`.
|
|
19
|
+
* `mode` picks how values are read; see {@link NumericOptions.mode}.
|
|
19
20
|
* @returns Serializable column definition for a numeric column.
|
|
20
21
|
*/
|
|
21
22
|
export function numeric(name, options = {}) {
|
|
22
23
|
const precision = options.precision ?? DEFAULT_NUMERIC_PRECISION;
|
|
23
24
|
const scale = options.scale ?? (options.precision === undefined ? DEFAULT_NUMERIC_SCALE : 0);
|
|
25
|
+
const mode = options.mode ?? "number";
|
|
24
26
|
if (!Number.isInteger(precision) || precision < 1 || precision > 1000) {
|
|
25
27
|
throw new Error(`Column "${name}": numeric precision must be an integer from 1 to 1000.`);
|
|
26
28
|
}
|
|
@@ -28,21 +30,42 @@ export function numeric(name, options = {}) {
|
|
|
28
30
|
throw new Error(`Column "${name}": numeric scale must be an integer from 0 to ${precision}.`);
|
|
29
31
|
}
|
|
30
32
|
const dataType = `numeric(${precision},${scale})`;
|
|
33
|
+
const decode = (text) => {
|
|
34
|
+
if (mode === "number")
|
|
35
|
+
return parseFloat(text);
|
|
36
|
+
if (mode === "string")
|
|
37
|
+
return text;
|
|
38
|
+
return new mode(text);
|
|
39
|
+
};
|
|
40
|
+
/** A write value in the column's read form: a decimal string or number converted, if needed. */
|
|
41
|
+
const toValue = (value) => {
|
|
42
|
+
if (mode === "number")
|
|
43
|
+
return value;
|
|
44
|
+
if (typeof value === "number" || typeof value === "string") {
|
|
45
|
+
const text = String(value);
|
|
46
|
+
if (!isNumericText(text)) {
|
|
47
|
+
throw refusal(name, `${JSON.stringify(value)} is not a decimal number`);
|
|
48
|
+
}
|
|
49
|
+
return mode === "string" ? text : new mode(text);
|
|
50
|
+
}
|
|
51
|
+
return value;
|
|
52
|
+
};
|
|
31
53
|
return new ColumnDefinition(name, {
|
|
32
54
|
dataType,
|
|
33
55
|
runtimeType: "number",
|
|
34
56
|
codec: {
|
|
35
|
-
encode: (value) => value
|
|
36
|
-
decode: (value) =>
|
|
57
|
+
encode: (value) => String(value),
|
|
58
|
+
decode: (value) => decode(value),
|
|
37
59
|
},
|
|
38
60
|
validator: {
|
|
39
|
-
write: (
|
|
40
|
-
const
|
|
61
|
+
write: (input) => {
|
|
62
|
+
const value = toValue(input);
|
|
63
|
+
const digits = decimalDigits(String(value));
|
|
41
64
|
if (digits && digits.fraction > scale) {
|
|
42
|
-
throw refusal(name, `${value} has ${digits.fraction} decimal places; ${dataType} would round it`);
|
|
65
|
+
throw refusal(name, `${String(value)} has ${digits.fraction} decimal places; ${dataType} would round it`);
|
|
43
66
|
}
|
|
44
67
|
if (digits && digits.integer > precision - scale) {
|
|
45
|
-
throw refusal(name, `${value} has ${digits.integer} integer digits; ${dataType} allows at most ${precision - scale}`);
|
|
68
|
+
throw refusal(name, `${String(value)} has ${digits.integer} integer digits; ${dataType} allows at most ${precision - scale}`);
|
|
46
69
|
}
|
|
47
70
|
return value;
|
|
48
71
|
},
|
|
@@ -51,14 +74,21 @@ export function numeric(name, options = {}) {
|
|
|
51
74
|
});
|
|
52
75
|
}
|
|
53
76
|
const refusal = (column, message) => new ColumnValidationError("invalid", column, "write", [{ message }]);
|
|
77
|
+
const DECIMAL_TEXT = /^[+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i;
|
|
78
|
+
const SPECIAL_TEXT = /^([+-]?infinity|nan)$/i;
|
|
79
|
+
/** Whether Postgres reads `text` as a `numeric`: a decimal, exponent notation, `NaN`, `Infinity`. */
|
|
80
|
+
function isNumericText(text) {
|
|
81
|
+
return DECIMAL_TEXT.test(text) || SPECIAL_TEXT.test(text);
|
|
82
|
+
}
|
|
54
83
|
/**
|
|
55
|
-
* How many integer and decimal digits a
|
|
56
|
-
* `1e-7` has 7 decimals. `null` for a value with no digits to count (`NaN`,
|
|
84
|
+
* How many integer and decimal digits a decimal text is written with, exponent notation
|
|
85
|
+
* expanded — `1e-7` has 7 decimals. `null` for a value with no digits to count (`NaN`,
|
|
86
|
+
* `Infinity`).
|
|
57
87
|
*/
|
|
58
|
-
function decimalDigits(
|
|
59
|
-
if (!
|
|
88
|
+
function decimalDigits(text) {
|
|
89
|
+
if (!DECIMAL_TEXT.test(text))
|
|
60
90
|
return null;
|
|
61
|
-
const [mantissa = "0", exponentText = "0"] =
|
|
91
|
+
const [mantissa = "0", exponentText = "0"] = text.replace(/^[+-]/, "").toLowerCase().split("e");
|
|
62
92
|
const [whole = "", decimals = ""] = mantissa.split(".");
|
|
63
93
|
const exponent = Number(exponentText);
|
|
64
94
|
const digits = whole + decimals;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"numeric.js","sourceRoot":"","sources":["../../../src/schema/columns/numeric.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAStE;;;GAGG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,EAAE,CAAC;AAC5C,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,CAAC;AAEvC;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,OAAO,CAA6B,IAAW,EAAE,UAA0B,EAAE;IAC3F,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,yBAAyB,CAAC;IACjE,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,CAAC,OAAO,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC,CAAC,C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M,CAAC;IAEpF,OAAO;QACL,OAAO,EAAE,WAAW,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,MAAM;QAC9C,QAAQ,EAAE,YAAY,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,MAAM;KACjD,CAAC;AACJ,CAAC;AAED,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,CAAC","sourcesContent":["import { ColumnConfig, ColumnDefinition } from \"@dsqlbase/core\";\nimport { ColumnValidationError } from \"../utils/column-validation.js\";\n\nexport type NumericOptions = {\n /** Total significant digits, 1–1000. @default 18 */\n precision?: number;\n /** Digits after the decimal point, 0–`precision`. @default 6, or 0 when `precision` is given */\n scale?: number;\n};\n\n/**\n * DSQL's own precision and scale for a `numeric` declared without them, stated explicitly so\n * Postgres and PGlite store the same type.\n */\nexport const DEFAULT_NUMERIC_PRECISION = 18;\nexport const DEFAULT_NUMERIC_SCALE = 6;\n\n/**\n * Defines a `numeric(precision, scale)` (exact decimal) column in the database schema.\n *\n * Without options the column is `numeric(18,6)`, which is what DSQL makes of a bare `numeric`:\n * at most 12 integer digits and 6 decimals. A write that would not fit — more decimals than the\n * scale, which the database would round, or more integer digits than `precision - scale`, which\n * it would refuse — throws a {@link ColumnValidationError} instead.\n *\n * @param name Column name in database\n * @param options Precision and scale; `{ precision }` alone means a scale of 0, as `numeric(p)`\n * @returns Serializable column definition for a numeric column.\n */\nexport function numeric<const TName extends string>(name: TName, options: NumericOptions = {}) {\n const precision = options.precision ?? DEFAULT_NUMERIC_PRECISION;\n const scale = options.scale ?? (options.precision === undefined ? DEFAULT_NUMERIC_SCALE : 0);\n\n if (!Number.isInteger(precision) || precision < 1 || precision > 1000) {\n throw new Error(`Column \"${name}\": numeric precision must be an integer from 1 to 1000.`);\n }\n\n if (!Number.isInteger(scale) || scale < 0 || scale > precision) {\n throw new Error(`Column \"${name}\": numeric scale must be an integer from 0 to ${precision}.`);\n }\n\n const dataType = `numeric(${precision},${scale})`;\n\n return new ColumnDefinition<TName, ColumnConfig<number, string, \"number\">>(name, {\n dataType,\n runtimeType: \"number\",\n codec: {\n encode: (value) => value.toString(),\n decode: (value) => parseFloat(value),\n },\n validator: {\n write: (value) => {\n const digits = decimalDigits(value);\n\n if (digits && digits.fraction > scale) {\n throw refusal(\n name,\n `${value} has ${digits.fraction} decimal places; ${dataType} would round it`\n );\n }\n\n if (digits && digits.integer > precision - scale) {\n throw refusal(\n name,\n `${value} has ${digits.integer} integer digits; ${dataType} allows at most ${precision - scale}`\n );\n }\n\n return value;\n },\n read: (value) => value,\n },\n });\n}\n\nconst refusal = (column: string, message: string) =>\n new ColumnValidationError(\"invalid\", column, \"write\", [{ message }]);\n\n/**\n * How many integer and decimal digits a number is written with, exponent notation expanded —\n * `1e-7` has 7 decimals. `null` for a value with no digits to count (`NaN`, `Infinity`).\n */\nfunction decimalDigits(value: number): { integer: number; fraction: number } | null {\n if (!Number.isFinite(value)) return null;\n\n const [mantissa = \"0\", exponentText = \"0\"] = Math.abs(value).toString().split(\"e\");\n const [whole = \"\", decimals = \"\"] = mantissa.split(\".\");\n const exponent = Number(exponentText);\n const digits = whole + decimals;\n // Where the decimal point falls in `digits` once the exponent is applied.\n const point = whole.length + exponent;\n\n const integerPart = point > 0 ? digits.slice(0, point).padEnd(point, \"0\") : \"\";\n const fractionPart = point >= 0 ? digits.slice(point) : \"0\".repeat(-point) + digits;\n\n return {\n integer: integerPart.replace(/^0+/, \"\").length,\n fraction: fractionPart.replace(/0+$/, \"\").length,\n };\n}\n\nexport { numeric as decimal };\n"]}
|
|
1
|
+
{"version":3,"file":"numeric.js","sourceRoot":"","sources":["../../../src/schema/columns/numeric.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAsDtE;;;GAGG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,EAAE,CAAC;AA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IAEtC,MAAM,WAAW,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAC/E,MAAM,YAAY,GAAG,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC;IAEpF,OAAO;QACL,OAAO,EAAE,WAAW,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,MAAM;QAC9C,QAAQ,EAAE,YAAY,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,MAAM;KACjD,CAAC;AACJ,CAAC;AAED,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,CAAC","sourcesContent":["import { ColumnConfig, ColumnDefinition } from \"@dsqlbase/core\";\nimport { ColumnValidationError } from \"../utils/column-validation.js\";\n\n/**\n * An exact decimal value from a library such as decimal.js, big.js or bignumber.js. Its\n * `toString()` must print the value in a form Postgres reads as `numeric` — plain or exponent\n * notation.\n */\nexport type DecimalLike = {\n toString(): string;\n toJSON(): string;\n};\n\n/** A class that builds a {@link DecimalLike} from the database's decimal text. */\nexport type DecimalConstructor<T extends DecimalLike = DecimalLike> = new (value: string) => T;\n\n/**\n * How a `numeric` value is presented: a JavaScript `number` (may lose precision), the exact\n * decimal text, or an instance of a decimal class.\n */\nexport type NumericMode = \"number\" | \"string\" | DecimalConstructor;\n\nexport type NumericOptions = {\n /** Total significant digits, 1–1000. @default 18 */\n precision?: number;\n /** Digits after the decimal point, 0–`precision`. @default 6, or 0 when `precision` is given */\n scale?: number;\n /**\n * How values are read:\n * - `\"number\"`: a JavaScript `number`. Values past ~15 significant digits lose precision.\n * - `\"string\"`: the exact decimal text, e.g. `\"12.500000\"`.\n * - a decimal class (`Decimal` from decimal.js, `Big`, `BigNumber`, …): an instance of it,\n * built from the exact decimal text.\n *\n * In `\"string\"` and class modes a write also accepts a `number` or a decimal string.\n *\n * @default \"number\"\n */\n mode?: NumericMode;\n};\n\n/** What a read of a `numeric` column with options `O` returns. */\nexport type NumericValueType<O> = O extends { mode: \"string\" }\n ? string\n : O extends { mode: DecimalConstructor<infer T> }\n ? T\n : number;\n\n/** What a write to a `numeric` column with options `O` accepts. */\nexport type NumericInputType<O> = O extends { mode: \"string\" }\n ? string | number\n : O extends { mode: DecimalConstructor<infer T> }\n ? T | string | number\n : number;\n\n/**\n * DSQL's own precision and scale for a `numeric` declared without them, stated explicitly so\n * Postgres and PGlite store the same type.\n */\nexport const DEFAULT_NUMERIC_PRECISION = 18;\nexport const DEFAULT_NUMERIC_SCALE = 6;\n\n/**\n * Defines a `numeric(precision, scale)` (exact decimal) column in the database schema.\n *\n * Without options the column is `numeric(18,6)`, which is what DSQL makes of a bare `numeric`:\n * at most 12 integer digits and 6 decimals. A write that would not fit — more decimals than the\n * scale, which the database would round, or more integer digits than `precision - scale`, which\n * it would refuse — throws a {@link ColumnValidationError} instead.\n *\n * @param name Column name in database\n * @param options Precision and scale; `{ precision }` alone means a scale of 0, as `numeric(p)`.\n * `mode` picks how values are read; see {@link NumericOptions.mode}.\n * @returns Serializable column definition for a numeric column.\n */\nexport function numeric<\n const TName extends string,\n const TOptions extends NumericOptions = NumericOptions,\n>(name: TName, options: TOptions = {} as TOptions) {\n type TValue = NumericValueType<TOptions>;\n type TInput = NumericInputType<TOptions>;\n\n const precision = options.precision ?? DEFAULT_NUMERIC_PRECISION;\n const scale = options.scale ?? (options.precision === undefined ? DEFAULT_NUMERIC_SCALE : 0);\n const mode = options.mode ?? \"number\";\n\n if (!Number.isInteger(precision) || precision < 1 || precision > 1000) {\n throw new Error(`Column \"${name}\": numeric precision must be an integer from 1 to 1000.`);\n }\n\n if (!Number.isInteger(scale) || scale < 0 || scale > precision) {\n throw new Error(`Column \"${name}\": numeric scale must be an integer from 0 to ${precision}.`);\n }\n\n const dataType = `numeric(${precision},${scale})`;\n\n const decode = (text: string): unknown => {\n if (mode === \"number\") return parseFloat(text);\n if (mode === \"string\") return text;\n return new mode(text);\n };\n\n /** A write value in the column's read form: a decimal string or number converted, if needed. */\n const toValue = (value: unknown): unknown => {\n if (mode === \"number\") return value;\n\n if (typeof value === \"number\" || typeof value === \"string\") {\n const text = String(value);\n\n if (!isNumericText(text)) {\n throw refusal(name, `${JSON.stringify(value)} is not a decimal number`);\n }\n\n return mode === \"string\" ? text : new mode(text);\n }\n\n return value;\n };\n\n return new ColumnDefinition<TName, ColumnConfig<TValue, string, \"number\", TInput>>(name, {\n dataType,\n runtimeType: \"number\",\n codec: {\n encode: (value) => String(value),\n decode: (value) => decode(value) as TValue,\n },\n validator: {\n write: (input) => {\n const value = toValue(input);\n const digits = decimalDigits(String(value));\n\n if (digits && digits.fraction > scale) {\n throw refusal(\n name,\n `${String(value)} has ${digits.fraction} decimal places; ${dataType} would round it`\n );\n }\n\n if (digits && digits.integer > precision - scale) {\n throw refusal(\n name,\n `${String(value)} has ${digits.integer} integer digits; ${dataType} allows at most ${precision - scale}`\n );\n }\n\n return value as TValue;\n },\n read: (value) => value,\n },\n });\n}\n\nconst refusal = (column: string, message: string) =>\n new ColumnValidationError(\"invalid\", column, \"write\", [{ message }]);\n\nconst DECIMAL_TEXT = /^[+-]?(\\d+\\.?\\d*|\\.\\d+)(e[+-]?\\d+)?$/i;\nconst SPECIAL_TEXT = /^([+-]?infinity|nan)$/i;\n\n/** Whether Postgres reads `text` as a `numeric`: a decimal, exponent notation, `NaN`, `Infinity`. */\nfunction isNumericText(text: string): boolean {\n return DECIMAL_TEXT.test(text) || SPECIAL_TEXT.test(text);\n}\n\n/**\n * How many integer and decimal digits a decimal text is written with, exponent notation\n * expanded — `1e-7` has 7 decimals. `null` for a value with no digits to count (`NaN`,\n * `Infinity`).\n */\nfunction decimalDigits(text: string): { integer: number; fraction: number } | null {\n if (!DECIMAL_TEXT.test(text)) return null;\n\n const [mantissa = \"0\", exponentText = \"0\"] = text.replace(/^[+-]/, \"\").toLowerCase().split(\"e\");\n const [whole = \"\", decimals = \"\"] = mantissa.split(\".\");\n const exponent = Number(exponentText);\n const digits = whole + decimals;\n // Where the decimal point falls in `digits` once the exponent is applied.\n const point = whole.length + exponent;\n\n const integerPart = point > 0 ? digits.slice(0, point).padEnd(point, \"0\") : \"\";\n const fractionPart = point >= 0 ? digits.slice(point) : \"0\".repeat(-point) + digits;\n\n return {\n integer: integerPart.replace(/^0+/, \"\").length,\n fraction: fractionPart.replace(/0+$/, \"\").length,\n };\n}\n\nexport { numeric as decimal };\n"]}
|
package/dist/schema/index.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ export { interval, duration, type IntervalColumnOptions, type IntervalValueType,
|
|
|
12
12
|
export { json, jsonb, JsonColumnDefinition, type JsonSchemaFor, type JsonShape, type WithSchema, } from "./columns/json.js";
|
|
13
13
|
export { record } from "./columns/record.js";
|
|
14
14
|
export { decimal, numeric } from "./columns/numeric.js";
|
|
15
|
+
export type { DecimalConstructor, DecimalLike, NumericInputType, NumericMode, NumericOptions, NumericValueType, } from "./columns/numeric.js";
|
|
15
16
|
export { real, float4 } from "./columns/real.js";
|
|
16
17
|
export { smallint, int2 } from "./columns/smallint.js";
|
|
17
18
|
export { text } from "./columns/text.js";
|
|
@@ -20,11 +21,9 @@ export { timestamp, datetime, TimestampColumnDefinition, type DateTimeColumnOpti
|
|
|
20
21
|
export { uuid, UUIDColumnDefinition } from "./columns/uuid.js";
|
|
21
22
|
export { varchar } from "./columns/varchar.js";
|
|
22
23
|
export { domain, $enum } from "./domain.js";
|
|
23
|
-
export { embedded } from "./embedded.js";
|
|
24
24
|
export { namespace, schema } from "./namespace.js";
|
|
25
25
|
export { sequence } from "./sequence.js";
|
|
26
26
|
export { table } from "./table.js";
|
|
27
|
-
export { tenantScope } from "./tenant.js";
|
|
28
27
|
export { union } from "./union.js";
|
|
29
28
|
export { relations, belongsTo, hasMany, hasOne } from "./relations.js";
|
|
30
29
|
export { type Duration } from "./utils/duration.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/schema/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,KAAK,aAAa,EAAE,KAAK,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACrF,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EACL,QAAQ,EACR,wBAAwB,EACxB,KAAK,qBAAqB,EAC1B,KAAK,cAAc,GACpB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,IAAI,EAAE,oBAAoB,EAAE,KAAK,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAC1E,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EACL,QAAQ,EACR,QAAQ,EACR,KAAK,qBAAqB,EAC1B,KAAK,iBAAiB,GACvB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,IAAI,EACJ,KAAK,EACL,oBAAoB,EACpB,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,UAAU,GAChB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AACxD,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AE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAEvE,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAC7E,YAAY,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AACnE,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/schema/index.js
CHANGED
|
@@ -20,11 +20,9 @@ export { timestamp, datetime, TimestampColumnDefinition, } from "./columns/times
|
|
|
20
20
|
export { uuid, UUIDColumnDefinition } from "./columns/uuid.js";
|
|
21
21
|
export { varchar } from "./columns/varchar.js";
|
|
22
22
|
export { domain, $enum } from "./domain.js";
|
|
23
|
-
export { embedded } from "./embedded.js";
|
|
24
23
|
export { namespace, schema } from "./namespace.js";
|
|
25
24
|
export { sequence } from "./sequence.js";
|
|
26
25
|
export { table } from "./table.js";
|
|
27
|
-
export { tenantScope } from "./tenant.js";
|
|
28
26
|
export { union } from "./union.js";
|
|
29
27
|
export { relations, belongsTo, hasMany, hasOne } from "./relations.js";
|
|
30
28
|
//# sourceMappingURL=index.js.map
|
package/dist/schema/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/schema/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,O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,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EACL,QAAQ,EACR,QAAQ,GAGT,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,IAAI,EACJ,KAAK,EACL,oBAAoB,GAIrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AASxD,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACzC,OAAO,EAAE,IAAI,EAA0B,MAAM,mBAAmB,CAAC;AACjE,OAAO,EACL,SAAS,EACT,QAAQ,EACR,yBAAyB,GAG1B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,IAAI,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC","sourcesContent":["export { array } from \"./columns/array.js\";\nexport { bigint, int8 } from \"./columns/bigint.js\";\nexport { boolean, bool } from \"./columns/boolean.js\";\nexport { bytea } from \"./columns/bytea.js\";\nexport { char } from \"./columns/char.js\";\nexport { date, type DateValueType, type DateColumnOptions } from \"./columns/date.js\";\nexport { double, float8 } from \"./columns/double.js\";\nexport {\n identity,\n IdentityColumnDefinition,\n type IdentityColumnOptions,\n type IdentityConfig,\n} from \"./columns/identity.js\";\nexport { guid, GuidColumnDefinition, type Guid } from \"./columns/guid.js\";\nexport { int, int4 } from \"./columns/int.js\";\nexport {\n interval,\n duration,\n type IntervalColumnOptions,\n type IntervalValueType,\n} from \"./columns/interval.js\";\nexport {\n json,\n jsonb,\n JsonColumnDefinition,\n type JsonSchemaFor,\n type JsonShape,\n type WithSchema,\n} from \"./columns/json.js\";\nexport { record } from \"./columns/record.js\";\nexport { decimal, numeric } from \"./columns/numeric.js\";\nexport type {\n DecimalConstructor,\n DecimalLike,\n NumericInputType,\n NumericMode,\n NumericOptions,\n NumericValueType,\n} from \"./columns/numeric.js\";\nexport { real, float4 } from \"./columns/real.js\";\nexport { smallint, int2 } from \"./columns/smallint.js\";\nexport { text } from \"./columns/text.js\";\nexport { time, type TimeColumnOptions } from \"./columns/time.js\";\nexport {\n timestamp,\n datetime,\n TimestampColumnDefinition,\n type DateTimeColumnOptions,\n type TimestampColumnConfig,\n} from \"./columns/timestamp.js\";\nexport { uuid, UUIDColumnDefinition } from \"./columns/uuid.js\";\nexport { varchar } from \"./columns/varchar.js\";\n\nexport { domain, $enum } from \"./domain.js\";\nexport { namespace, schema } from \"./namespace.js\";\nexport { sequence } from \"./sequence.js\";\nexport { table } from \"./table.js\";\nexport { union } from \"./union.js\";\n\nexport { relations, belongsTo, hasMany, hasOne } from \"./relations.js\";\n\nexport { type Duration } from \"./utils/duration.js\";\nexport { type GlobalId, type GlobalIdErrorCode } from \"./utils/global-id.js\";\nexport type { StandardSchemaV1 } from \"./utils/standard-schema.js\";\nexport { type DateTimeMode } from \"./utils/date.js\";\n"]}
|
|
@@ -1,24 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { AnyUnionDefinition, FieldRelation, RelationsDefinition, RelationType } from "@dsqlbase/core/definition";
|
|
2
|
+
import { AnyRelationObject } from "@dsqlbase/core/definition/relations";
|
|
3
|
+
/**
|
|
4
|
+
* Declares the relations of a table, or of a union. A union's relations are every member's,
|
|
5
|
+
* each from its own column: `relations(posts, { comments: hasMany(comments, { from:
|
|
6
|
+
* [posts.columns.id], to: [comments.columns.postId] }) })` joins comments from a photo, from a
|
|
7
|
+
* video, and from the union. Its target must be a table.
|
|
8
|
+
*/
|
|
9
|
+
export declare function relations<TSource extends AnyRelationObject, TRelations extends Record<string, FieldRelation<RelationType, TSource, AnyRelationObject>>>(table: TSource, relations: TRelations): RelationsDefinition<TSource, TRelations>;
|
|
3
10
|
/**
|
|
4
11
|
* `to` names the target's columns. For a union target it is either shared fields from
|
|
5
12
|
* `union.columns`, or one column list per member when members store the key differently.
|
|
6
13
|
*/
|
|
7
|
-
export declare function hasMany<TSource extends
|
|
8
|
-
from: FieldRelation<TSource, TTarget
|
|
9
|
-
to: FieldRelation<TSource, TTarget
|
|
10
|
-
}): FieldRelation<TSource, TTarget
|
|
14
|
+
export declare function hasMany<TSource extends AnyRelationObject, TTarget extends AnyRelationObject>(target: TTarget, config: {
|
|
15
|
+
from: FieldRelation<"has_many", TSource, TTarget>["from"];
|
|
16
|
+
to: FieldRelation<"has_many", TSource, TTarget>["to"];
|
|
17
|
+
}): FieldRelation<"has_many", TSource, TTarget>;
|
|
11
18
|
/**
|
|
12
|
-
* The belongs-to side of a relation. A belongs-to a union also
|
|
19
|
+
* The belongs-to side of a relation. A belongs-to a union may also take `discriminator`: the
|
|
13
20
|
* source column holding which member each row points at, as that member's schema alias.
|
|
21
|
+
* Without one, the member is the first, in declaration order, holding the key — computed when
|
|
22
|
+
* the query runs, one key lookup per member per row.
|
|
14
23
|
*/
|
|
15
|
-
export declare function belongsTo<TSource extends
|
|
16
|
-
from: FieldRelation<TSource, TTarget
|
|
17
|
-
to: FieldRelation<TSource, TTarget
|
|
24
|
+
export declare function belongsTo<TSource extends AnyRelationObject, TTarget extends AnyRelationObject>(target: TTarget, config: {
|
|
25
|
+
from: FieldRelation<"belongs_to", TSource, TTarget>["from"];
|
|
26
|
+
to: FieldRelation<"belongs_to", TSource, TTarget>["to"];
|
|
18
27
|
} & (TTarget extends AnyUnionDefinition ? {
|
|
19
|
-
discriminator
|
|
28
|
+
discriminator?: NonNullable<FieldRelation<"belongs_to", TSource, TTarget>["discriminator"]>;
|
|
20
29
|
} : {
|
|
21
30
|
discriminator?: never;
|
|
22
|
-
})): FieldRelation<TSource, TTarget
|
|
23
|
-
export declare function hasOne<TSource extends
|
|
31
|
+
})): FieldRelation<"belongs_to", TSource, TTarget>;
|
|
32
|
+
export declare function hasOne<TSource extends AnyRelationObject, TTarget extends AnyRelationObject>(target: TTarget, config: Pick<FieldRelation<"has_one", TSource, TTarget>, "from" | "to">): FieldRelation<"has_one", TSource, TTarget>;
|
|
24
33
|
//# sourceMappingURL=relations.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relations.d.ts","sourceRoot":"","sources":["../../src/schema/relations.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,
|
|
1
|
+
{"version":3,"file":"relations.d.ts","sourceRoot":"","sources":["../../src/schema/relations.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,aAAa,EAEb,mBAAmB,EACnB,YAAY,EACb,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AAExE;;;;;GAKG;AACH,wBAAgB,SAAS,CACvB,OAAO,SAAS,iBAAiB,EACjC,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,YAAY,EAAE,OAAO,EAAE,iBAAiB,CAAC,CAAC,EAC1F,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,4CAEtC;AAED;;;GAGG;AACH,wBAAgB,OAAO,CAAC,OAAO,SAAS,iBAAiB,EAAE,OAAO,SAAS,iBAAiB,EAC1F,MAAM,EAAE,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
2
|
+
/**
|
|
3
|
+
* Declares the relations of a table, or of a union. A union's relations are every member's,
|
|
4
|
+
* each from its own column: `relations(posts, { comments: hasMany(comments, { from:
|
|
5
|
+
* [posts.columns.id], to: [comments.columns.postId] }) })` joins comments from a photo, from a
|
|
6
|
+
* video, and from the union. Its target must be a table.
|
|
7
|
+
*/
|
|
2
8
|
export function relations(table, relations) {
|
|
3
9
|
return new RelationsDefinition(table, relations);
|
|
4
10
|
}
|
|
@@ -15,8 +21,10 @@ export function hasMany(target, config) {
|
|
|
15
21
|
};
|
|
16
22
|
}
|
|
17
23
|
/**
|
|
18
|
-
* The belongs-to side of a relation. A belongs-to a union also
|
|
24
|
+
* The belongs-to side of a relation. A belongs-to a union may also take `discriminator`: the
|
|
19
25
|
* source column holding which member each row points at, as that member's schema alias.
|
|
26
|
+
* Without one, the member is the first, in declaration order, holding the key — computed when
|
|
27
|
+
* the query runs, one key lookup per member per row.
|
|
20
28
|
*/
|
|
21
29
|
export function belongsTo(target, config) {
|
|
22
30
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relations.js","sourceRoot":"","sources":["../../src/schema/relations.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"relations.js","sourceRoot":"","sources":["../../src/schema/relations.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,QAAQ,EACR,mBAAmB,GAEpB,MAAM,2BAA2B,CAAC;AAGnC;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAGvB,KAAc,EAAE,SAAqB;IACrC,OAAO,IAAI,mBAAmB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;AACnD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,OAAO,CACrB,MAAe,EACf,MAGC;IAED,OAAO;QACL,IAAI,EAAE,QAAQ,CAAC,QAAQ;QACvB,MAAM;QACN,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,EAAE,EAAE,MAAM,CAAC,EAAE;KAC0C,CAAC;AAC5D,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CACvB,MAAe,EACf,MAO8B;IAE9B,OAAO;QACL,IAAI,EAAE,QAAQ,CAAC,UAAU;QACzB,MAAM;QACN,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,EAAE,EAAE,MAAM,CAAC,EAAE;QACb,GAAG,CAAC,MAAM,CAAC,aAAa,IAAI,EAAE,aAAa,EAAE,MAAM,CAAC,aAAa,EAAE,CAAC;KACX,CAAC;AAC9D,CAAC;AAED,MAAM,UAAU,MAAM,CACpB,MAAe,EACf,MAAuE;IAEvE,OAAO;QACL,IAAI,EAAE,QAAQ,CAAC,OAAO;QACtB,MAAM;QACN,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,EAAE,EAAE,MAAM,CAAC,EAAE;KACyC,CAAC;AAC3D,CAAC","sourcesContent":["import {\n AnyUnionDefinition,\n FieldRelation,\n Relation,\n RelationsDefinition,\n RelationType,\n} from \"@dsqlbase/core/definition\";\nimport { AnyRelationObject } from \"@dsqlbase/core/definition/relations\";\n\n/**\n * Declares the relations of a table, or of a union. A union's relations are every member's,\n * each from its own column: `relations(posts, { comments: hasMany(comments, { from:\n * [posts.columns.id], to: [comments.columns.postId] }) })` joins comments from a photo, from a\n * video, and from the union. Its target must be a table.\n */\nexport function relations<\n TSource extends AnyRelationObject,\n TRelations extends Record<string, FieldRelation<RelationType, TSource, AnyRelationObject>>,\n>(table: TSource, relations: TRelations) {\n return new RelationsDefinition(table, relations);\n}\n\n/**\n * `to` names the target's columns. For a union target it is either shared fields from\n * `union.columns`, or one column list per member when members store the key differently.\n */\nexport function hasMany<TSource extends AnyRelationObject, TTarget extends AnyRelationObject>(\n target: TTarget,\n config: {\n from: FieldRelation<\"has_many\", TSource, TTarget>[\"from\"];\n to: FieldRelation<\"has_many\", TSource, TTarget>[\"to\"];\n }\n): FieldRelation<\"has_many\", TSource, TTarget> {\n return {\n type: Relation.HAS_MANY,\n target,\n from: config.from,\n to: config.to,\n } as const as FieldRelation<\"has_many\", TSource, TTarget>;\n}\n\n/**\n * The belongs-to side of a relation. A belongs-to a union may also take `discriminator`: the\n * source column holding which member each row points at, as that member's schema alias.\n * Without one, the member is the first, in declaration order, holding the key — computed when\n * the query runs, one key lookup per member per row.\n */\nexport function belongsTo<TSource extends AnyRelationObject, TTarget extends AnyRelationObject>(\n target: TTarget,\n config: {\n from: FieldRelation<\"belongs_to\", TSource, TTarget>[\"from\"];\n to: FieldRelation<\"belongs_to\", TSource, TTarget>[\"to\"];\n } & (TTarget extends AnyUnionDefinition\n ? {\n discriminator?: NonNullable<FieldRelation<\"belongs_to\", TSource, TTarget>[\"discriminator\"]>;\n }\n : { discriminator?: never })\n): FieldRelation<\"belongs_to\", TSource, TTarget> {\n return {\n type: Relation.BELONGS_TO,\n target,\n from: config.from,\n to: config.to,\n ...(config.discriminator && { discriminator: config.discriminator }),\n } as const as FieldRelation<\"belongs_to\", TSource, TTarget>;\n}\n\nexport function hasOne<TSource extends AnyRelationObject, TTarget extends AnyRelationObject>(\n target: TTarget,\n config: Pick<FieldRelation<\"has_one\", TSource, TTarget>, \"from\" | \"to\">\n): FieldRelation<\"has_one\", TSource, TTarget> {\n return {\n type: Relation.HAS_ONE,\n target,\n from: config.from,\n to: config.to,\n } as const as FieldRelation<\"has_one\", TSource, TTarget>;\n}\n"]}
|
package/dist/schema/table.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TableDefinition,
|
|
1
|
+
import { TableDefinition, AnyColumnDefinition, AnyNamespaceDefinition } from "@dsqlbase/core/definition";
|
|
2
2
|
/**
|
|
3
3
|
* Defines a table schema with specified columns and optional schema association.
|
|
4
4
|
*
|
|
@@ -16,5 +16,5 @@ import { TableDefinition, AnyNamespaceDefinition, TableColumnDefinitions } from
|
|
|
16
16
|
* });
|
|
17
17
|
* ```
|
|
18
18
|
*/
|
|
19
|
-
export declare function table<TName extends string, TColumns extends
|
|
19
|
+
export declare function table<TName extends string, TColumns extends Record<string, AnyColumnDefinition>>(name: TName, columns: TColumns): TableDefinition<TName, TColumns, AnyNamespaceDefinition>;
|
|
20
20
|
//# sourceMappingURL=table.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../src/schema/table.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,
|
|
1
|
+
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../src/schema/table.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,sBAAsB,EACvB,MAAM,2BAA2B,CAAC;AAEnC;;;;;;;;;;;;;;;;GAgBG;AAEH,wBAAgB,KAAK,CAAC,KAAK,SAAS,MAAM,EAAE,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,EAC9F,IAAI,EAAE,KAAK,EACX,OAAO,EAAE,QAAQ,GAChB,eAAe,CAAC,KAAK,EAAE,QAAQ,EAAE,sBAAsB,CAAC,CAE1D"}
|
package/dist/schema/table.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table.js","sourceRoot":"","sources":["../../src/schema/table.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,GAGhB,MAAM,2BAA2B,CAAC;AAEnC;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,UAAU,KAAK,CACnB,IAAW,EACX,OAAiB;IAEjB,OAAO,IAAI,eAAe,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;AAChD,CAAC","sourcesContent":["import {\n TableDefinition,\n
|
|
1
|
+
{"version":3,"file":"table.js","sourceRoot":"","sources":["../../src/schema/table.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,GAGhB,MAAM,2BAA2B,CAAC;AAEnC;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,UAAU,KAAK,CACnB,IAAW,EACX,OAAiB;IAEjB,OAAO,IAAI,eAAe,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;AAChD,CAAC","sourcesContent":["import {\n TableDefinition,\n AnyColumnDefinition,\n AnyNamespaceDefinition,\n} from \"@dsqlbase/core/definition\";\n\n/**\n * Defines a table schema with specified columns and optional schema association.\n *\n * @param name Table name in the database\n * @param columns An object defining the columns of the table, where keys are field names used at runtime and values are ColumnDefinition instances that specify the column's properties and constraints.\n * @returns A new instance of TableDefinition representing the defined table schema.\n *\n * @example\n *\n * ```ts\n * const users = table(\"users\", {\n * id: uuid(\"id\").primaryKey(),\n * name: text(\"name\").notNull(),\n * email: text(\"email\").unique(),\n * });\n * ```\n */\n\nexport function table<TName extends string, TColumns extends Record<string, AnyColumnDefinition>>(\n name: TName,\n columns: TColumns\n): TableDefinition<TName, TColumns, AnyNamespaceDefinition> {\n return new TableDefinition(name, { columns });\n}\n"]}
|
package/dist/schema/union.d.ts
CHANGED
|
@@ -1,25 +1,30 @@
|
|
|
1
|
-
import { AnyUnionMembers, UnionDefinition } from "@dsqlbase/core/definition";
|
|
1
|
+
import { AnyUnionMembers, UnionColumnsConfig, UnionConfig, UnionDefinition } from "@dsqlbase/core/definition";
|
|
2
2
|
/**
|
|
3
3
|
* Defines a set of tables that can stand in for one another — a GraphQL union or interface.
|
|
4
4
|
*
|
|
5
5
|
* Key every member by the alias it is exported under in the schema; the client refuses any
|
|
6
|
-
* other key when it is built.
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* other key when it is built. The second argument declares the fields that work across the
|
|
7
|
+
* union — a relation's `to`, and shared `select` / `where` / `orderBy` — as `union.columns`.
|
|
8
|
+
* Each is the column every member stores under the same field: the same database name and
|
|
9
|
+
* type, and `NOT NULL` on every member when it is declared so. A union produces no DDL.
|
|
9
10
|
*
|
|
10
11
|
* @param members The member tables, keyed by schema alias.
|
|
12
|
+
* @param columns The shared fields, keyed by field name, each declared as its column.
|
|
11
13
|
*
|
|
12
14
|
* @example
|
|
13
15
|
* ```ts
|
|
14
|
-
* export const photos = table("photos", { id: guid("id").primaryKey(), userId:
|
|
15
|
-
* export const videos = table("videos", { id: guid("id").primaryKey(), userId:
|
|
16
|
+
* export const photos = table("photos", { id: guid("id").primaryKey(), userId: guid("user_id") });
|
|
17
|
+
* export const videos = table("videos", { id: guid("id").primaryKey(), userId: guid("user_id") });
|
|
16
18
|
*
|
|
17
|
-
* export const posts = union(
|
|
19
|
+
* export const posts = union("posts", {
|
|
20
|
+
* members: { photos, videos },
|
|
21
|
+
* columns: { id: guid("id"), userId: guid("user_id") }
|
|
22
|
+
* });
|
|
18
23
|
*
|
|
19
24
|
* export const userRelations = relations(users, {
|
|
20
25
|
* feed: hasMany(posts, { from: [users.columns.id], to: [posts.columns.userId] }),
|
|
21
26
|
* });
|
|
22
27
|
* ```
|
|
23
28
|
*/
|
|
24
|
-
export declare function union<TMembers extends AnyUnionMembers
|
|
29
|
+
export declare function union<TName extends string, TMembers extends AnyUnionMembers, TColumns extends UnionColumnsConfig<TMembers>>(name: TName, config: UnionConfig<TMembers, TColumns>): UnionDefinition<TName, TMembers, TColumns>;
|
|
25
30
|
//# sourceMappingURL=union.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"union.d.ts","sourceRoot":"","sources":["../../src/schema/union.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"union.d.ts","sourceRoot":"","sources":["../../src/schema/union.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,kBAAkB,EAClB,WAAW,EACX,eAAe,EAChB,MAAM,2BAA2B,CAAC;AAEnC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,KAAK,CACnB,KAAK,SAAS,MAAM,EACpB,QAAQ,SAAS,eAAe,EAChC,QAAQ,SAAS,kBAAkB,CAAC,QAAQ,CAAC,EAE7C,IAAI,EAAE,KAAK,EACX,MAAM,EAAE,WAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,GACtC,eAAe,CAAC,KAAK,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAE5C"}
|
package/dist/schema/union.js
CHANGED
|
@@ -1,27 +1,32 @@
|
|
|
1
|
-
import { UnionDefinition } from "@dsqlbase/core/definition";
|
|
1
|
+
import { UnionDefinition, } from "@dsqlbase/core/definition";
|
|
2
2
|
/**
|
|
3
3
|
* Defines a set of tables that can stand in for one another — a GraphQL union or interface.
|
|
4
4
|
*
|
|
5
5
|
* Key every member by the alias it is exported under in the schema; the client refuses any
|
|
6
|
-
* other key when it is built.
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* other key when it is built. The second argument declares the fields that work across the
|
|
7
|
+
* union — a relation's `to`, and shared `select` / `where` / `orderBy` — as `union.columns`.
|
|
8
|
+
* Each is the column every member stores under the same field: the same database name and
|
|
9
|
+
* type, and `NOT NULL` on every member when it is declared so. A union produces no DDL.
|
|
9
10
|
*
|
|
10
11
|
* @param members The member tables, keyed by schema alias.
|
|
12
|
+
* @param columns The shared fields, keyed by field name, each declared as its column.
|
|
11
13
|
*
|
|
12
14
|
* @example
|
|
13
15
|
* ```ts
|
|
14
|
-
* export const photos = table("photos", { id: guid("id").primaryKey(), userId:
|
|
15
|
-
* export const videos = table("videos", { id: guid("id").primaryKey(), userId:
|
|
16
|
+
* export const photos = table("photos", { id: guid("id").primaryKey(), userId: guid("user_id") });
|
|
17
|
+
* export const videos = table("videos", { id: guid("id").primaryKey(), userId: guid("user_id") });
|
|
16
18
|
*
|
|
17
|
-
* export const posts = union(
|
|
19
|
+
* export const posts = union("posts", {
|
|
20
|
+
* members: { photos, videos },
|
|
21
|
+
* columns: { id: guid("id"), userId: guid("user_id") }
|
|
22
|
+
* });
|
|
18
23
|
*
|
|
19
24
|
* export const userRelations = relations(users, {
|
|
20
25
|
* feed: hasMany(posts, { from: [users.columns.id], to: [posts.columns.userId] }),
|
|
21
26
|
* });
|
|
22
27
|
* ```
|
|
23
28
|
*/
|
|
24
|
-
export function union(
|
|
25
|
-
return new UnionDefinition(
|
|
29
|
+
export function union(name, config) {
|
|
30
|
+
return new UnionDefinition(name, config);
|
|
26
31
|
}
|
|
27
32
|
//# sourceMappingURL=union.js.map
|
package/dist/schema/union.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"union.js","sourceRoot":"","sources":["../../src/schema/union.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"union.js","sourceRoot":"","sources":["../../src/schema/union.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,eAAe,GAChB,MAAM,2BAA2B,CAAC;AAEnC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,KAAK,CAKnB,IAAW,EACX,MAAuC;IAEvC,OAAO,IAAI,eAAe,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;AAC3C,CAAC","sourcesContent":["import {\n AnyUnionMembers,\n UnionColumnsConfig,\n UnionConfig,\n UnionDefinition,\n} from \"@dsqlbase/core/definition\";\n\n/**\n * Defines a set of tables that can stand in for one another — a GraphQL union or interface.\n *\n * Key every member by the alias it is exported under in the schema; the client refuses any\n * other key when it is built. The second argument declares the fields that work across the\n * union — a relation's `to`, and shared `select` / `where` / `orderBy` — as `union.columns`.\n * Each is the column every member stores under the same field: the same database name and\n * type, and `NOT NULL` on every member when it is declared so. A union produces no DDL.\n *\n * @param members The member tables, keyed by schema alias.\n * @param columns The shared fields, keyed by field name, each declared as its column.\n *\n * @example\n * ```ts\n * export const photos = table(\"photos\", { id: guid(\"id\").primaryKey(), userId: guid(\"user_id\") });\n * export const videos = table(\"videos\", { id: guid(\"id\").primaryKey(), userId: guid(\"user_id\") });\n *\n * export const posts = union(\"posts\", {\n * members: { photos, videos },\n * columns: { id: guid(\"id\"), userId: guid(\"user_id\") }\n * });\n *\n * export const userRelations = relations(users, {\n * feed: hasMany(posts, { from: [users.columns.id], to: [posts.columns.userId] }),\n * });\n * ```\n */\nexport function union<\n TName extends string,\n TMembers extends AnyUnionMembers,\n TColumns extends UnionColumnsConfig<TMembers>,\n>(\n name: TName,\n config: UnionConfig<TMembers, TColumns>\n): UnionDefinition<TName, TMembers, TColumns> {\n return new UnionDefinition(name, config);\n}\n"]}
|
|
@@ -42,9 +42,9 @@ export type GlobalId = {
|
|
|
42
42
|
* Wraps a primary key as an opaque global id.
|
|
43
43
|
*
|
|
44
44
|
* The payload is `[key, pk]`, JSON-encoded and base64url-wrapped. `pk` is an object rather
|
|
45
|
-
* than a bare value so the key field is named rather than assumed to be `id`, and so
|
|
46
|
-
*
|
|
47
|
-
*
|
|
45
|
+
* than a bare value so the key field is named rather than assumed to be `id`, and so a
|
|
46
|
+
* composite key is one object carrying every field of it. Rows only emit single-field ids, from
|
|
47
|
+
* their `guid()` columns; a composite one is built here by the caller.
|
|
48
48
|
*
|
|
49
49
|
* Both the table key and the pk field names are **aliases**: the names the client addresses
|
|
50
50
|
* (`members`, not `team_members`), so an id follows the client-visible identity rather than
|
|
@@ -31,9 +31,9 @@ export const GLOBAL_ID_PREFIX = "guid:";
|
|
|
31
31
|
* Wraps a primary key as an opaque global id.
|
|
32
32
|
*
|
|
33
33
|
* The payload is `[key, pk]`, JSON-encoded and base64url-wrapped. `pk` is an object rather
|
|
34
|
-
* than a bare value so the key field is named rather than assumed to be `id`, and so
|
|
35
|
-
*
|
|
36
|
-
*
|
|
34
|
+
* than a bare value so the key field is named rather than assumed to be `id`, and so a
|
|
35
|
+
* composite key is one object carrying every field of it. Rows only emit single-field ids, from
|
|
36
|
+
* their `guid()` columns; a composite one is built here by the caller.
|
|
37
37
|
*
|
|
38
38
|
* Both the table key and the pk field names are **aliases**: the names the client addresses
|
|
39
39
|
* (`members`, not `team_members`), so an id follows the client-visible identity rather than
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"global-id.js","sourceRoot":"","sources":["../../../src/schema/utils/global-id.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAWH;;;;;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AAC;QACP,MAAM,IAAI,aAAa,CAAC,QAAQ,EAAE,IAAI,KAAK,sCAAsC,CAAC,CAAC;IACrF,CAAC;AACH,CAAC;AAED,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC","sourcesContent":["/**\n * Global ids: the opaque form a row's primary key takes when it leaves the ORM.\n *\n * Deliberately **not** in `@dsqlbase/core`. Nothing below the client has an opinion about the\n * shape of an id — core sees a `uuid` column with a codec, exactly as it sees an `interval`\n * column with a `Duration` codec (`./duration.ts`). The wrapping is a client concern, so it\n * lives here with the rest of the codec helpers.\n */\n\n/** What went wrong with a global id. */\nexport type GlobalIdErrorCode =\n /** The value is not a well-formed `guid:` string. */\n | \"format\"\n /** The value decoded, but names a table or key fields the caller did not expect. */\n | \"key_mismatch\"\n /** The value decoded, but its table key is not a node in this schema. */\n | \"unknown_node\";\n\n/**\n * A global id could not be read, or does not point where it was used.\n *\n * `key_mismatch` is the one that earns the format its keep: a wrapped `workspaces` id handed\n * to a `users.id` filter is caught here, where a bare uuid would simply have matched nothing.\n */\nexport class GlobalIdError extends Error {\n readonly code: GlobalIdErrorCode;\n\n constructor(code: GlobalIdErrorCode, message: string) {\n super(message);\n\n this.name = \"GlobalIdError\";\n this.code = code;\n }\n}\n\n/**\n * The prefix every global id carries.\n *\n * It is what makes \"is this already wrapped?\" an exact question rather than a heuristic: a\n * uuid never starts with `guid:`, so a column can accept both forms without guessing.\n */\nexport const GLOBAL_ID_PREFIX = \"guid:\";\n\n/** A decoded global id: the table it names, and the primary key it carries. */\nexport type GlobalId = {\n /** The node key — the schema alias of the table, unless the column overrode it. */\n readonly key: string;\n\n /** The primary key, by **field** name (`teamId`), not by database column name (`team_id`). */\n readonly pk: Record<string, string>;\n};\n\n/**\n * Wraps a primary key as an opaque global id.\n *\n * The payload is `[key, pk]`, JSON-encoded and base64url-wrapped. `pk` is an object rather\n * than a bare value so the key field is named rather than assumed to be `id`, and so
|
|
1
|
+
{"version":3,"file":"global-id.js","sourceRoot":"","sources":["../../../src/schema/utils/global-id.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAWH;;;;;GAKG;AACH,MAAM,OAAO,aAAc,SAAQ,KAAK;IAC7B,IAAI,CAAoB;IAEjC,YAAY,IAAuB,EAAE,OAAe;QAClD,KAAK,CAAC,OAAO,CAAC,CAAC;QAEf,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;QAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,OAAO,CAAC;AAWxC;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,cAAc,CAAC,GAAW,EAAE,EAA0B;IACpE,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC;IAE1C,OAAO,GAAG,gBAAgB,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;AACpF,CAAC;AAED,qEAAqE;AACrE,MAAM,UAAU,UAAU,CAAC,KAAc;IACvC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC;AACzE,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAAC,KAAa,EAAE,WAAoB;IAChE,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,aAAa,CACrB,QAAQ,EACR,IAAI,KAAK,oCAAoC,gBAAgB,WAAW,CACzE,CAAC;IACJ,CAAC;IAED,MAAM,OAAO,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IAErC,IAAI,CAAC,KAAK,C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C,gBAAgB,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;IAE7F,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,aAAa,CAAC,QAAQ,EAAE,IAAI,KAAK,sCAAsC,CAAC,CAAC;IACrF,CAAC;AACH,CAAC;AAED,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC","sourcesContent":["/**\n * Global ids: the opaque form a row's primary key takes when it leaves the ORM.\n *\n * Deliberately **not** in `@dsqlbase/core`. Nothing below the client has an opinion about the\n * shape of an id — core sees a `uuid` column with a codec, exactly as it sees an `interval`\n * column with a `Duration` codec (`./duration.ts`). The wrapping is a client concern, so it\n * lives here with the rest of the codec helpers.\n */\n\n/** What went wrong with a global id. */\nexport type GlobalIdErrorCode =\n /** The value is not a well-formed `guid:` string. */\n | \"format\"\n /** The value decoded, but names a table or key fields the caller did not expect. */\n | \"key_mismatch\"\n /** The value decoded, but its table key is not a node in this schema. */\n | \"unknown_node\";\n\n/**\n * A global id could not be read, or does not point where it was used.\n *\n * `key_mismatch` is the one that earns the format its keep: a wrapped `workspaces` id handed\n * to a `users.id` filter is caught here, where a bare uuid would simply have matched nothing.\n */\nexport class GlobalIdError extends Error {\n readonly code: GlobalIdErrorCode;\n\n constructor(code: GlobalIdErrorCode, message: string) {\n super(message);\n\n this.name = \"GlobalIdError\";\n this.code = code;\n }\n}\n\n/**\n * The prefix every global id carries.\n *\n * It is what makes \"is this already wrapped?\" an exact question rather than a heuristic: a\n * uuid never starts with `guid:`, so a column can accept both forms without guessing.\n */\nexport const GLOBAL_ID_PREFIX = \"guid:\";\n\n/** A decoded global id: the table it names, and the primary key it carries. */\nexport type GlobalId = {\n /** The node key — the schema alias of the table, unless the column overrode it. */\n readonly key: string;\n\n /** The primary key, by **field** name (`teamId`), not by database column name (`team_id`). */\n readonly pk: Record<string, string>;\n};\n\n/**\n * Wraps a primary key as an opaque global id.\n *\n * The payload is `[key, pk]`, JSON-encoded and base64url-wrapped. `pk` is an object rather\n * than a bare value so the key field is named rather than assumed to be `id`, and so a\n * composite key is one object carrying every field of it. Rows only emit single-field ids, from\n * their `guid()` columns; a composite one is built here by the caller.\n *\n * Both the table key and the pk field names are **aliases**: the names the client addresses\n * (`members`, not `team_members`), so an id follows the client-visible identity rather than\n * the physical one and a database rename does not invalidate ids in the wild.\n */\nexport function encodeGlobalId(key: string, pk: Record<string, string>): string {\n const payload = JSON.stringify([key, pk]);\n\n return `${GLOBAL_ID_PREFIX}${Buffer.from(payload, \"utf8\").toString(\"base64url\")}`;\n}\n\n/** Whether a value is a global id rather than a raw column value. */\nexport function isGlobalId(value: unknown): value is string {\n return typeof value === \"string\" && value.startsWith(GLOBAL_ID_PREFIX);\n}\n\n/**\n * Reads a global id back into its table key and primary key.\n *\n * `expectedKey` is the wrong-table safety net: pass it wherever the caller already knows which\n * table the id has to name — a `guid` column's own key, say — and a wrapped id from another\n * table throws instead of quietly matching nothing.\n *\n * The payload is never trusted. Whether the decoded fields are actually that table's primary\n * key is a separate question, answered against the registry by the caller.\n */\nexport function decodeGlobalId(value: string, expectedKey?: string): GlobalId {\n if (!isGlobalId(value)) {\n throw new GlobalIdError(\n \"format\",\n `\"${value}\" is not a global id: it has no \"${GLOBAL_ID_PREFIX}\" prefix.`\n );\n }\n\n const payload = decodePayload(value);\n\n if (!Array.isArray(payload) || payload.length !== 2) {\n throw new GlobalIdError(\"format\", `\"${value}\" does not carry a [key, pk] payload.`);\n }\n\n const [key, pk] = payload as [unknown, unknown];\n\n if (typeof key !== \"string\" || key.length === 0) {\n throw new GlobalIdError(\"format\", `\"${value}\" does not name a table.`);\n }\n\n if (!isPlainRecord(pk) || Object.keys(pk).length === 0) {\n throw new GlobalIdError(\"format\", `\"${value}\" does not carry a primary key.`);\n }\n\n for (const [field, fieldValue] of Object.entries(pk)) {\n if (typeof fieldValue !== \"string\") {\n throw new GlobalIdError(\n \"format\",\n `\"${value}\" carries a non-string value for key field \"${field}\".`\n );\n }\n }\n\n if (expectedKey !== undefined && key !== expectedKey) {\n throw new GlobalIdError(\n \"key_mismatch\",\n `Global id names \"${key}\", but \"${expectedKey}\" was expected here.`\n );\n }\n\n return { key, pk: pk as Record<string, string> };\n}\n\nfunction decodePayload(value: string): unknown {\n // `Buffer.from(..., \"base64url\")` never throws — it drops whatever it cannot read — so\n // malformed base64 surfaces here as JSON that does not parse rather than as a decode error.\n const json = Buffer.from(value.slice(GLOBAL_ID_PREFIX.length), \"base64url\").toString(\"utf8\");\n\n try {\n return JSON.parse(json);\n } catch {\n throw new GlobalIdError(\"format\", `\"${value}\" does not decode to a JSON payload.`);\n }\n}\n\nfunction isPlainRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n"]}
|