dsqlbase 0.1.6 → 0.2.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 +201 -0
- package/LICENSE +21 -0
- package/README.md +206 -2
- package/dist/client/create.d.ts +24 -4
- package/dist/client/create.d.ts.map +1 -1
- package/dist/client/create.js +21 -9
- package/dist/client/create.js.map +1 -1
- package/dist/client/database/base.d.ts +92 -1
- package/dist/client/database/base.d.ts.map +1 -1
- package/dist/client/database/base.js +196 -1
- package/dist/client/database/base.js.map +1 -1
- package/dist/client/database/client.d.ts +21 -5
- package/dist/client/database/client.d.ts.map +1 -1
- package/dist/client/database/client.js +39 -1
- package/dist/client/database/client.js.map +1 -1
- package/dist/client/database/index.d.ts +60 -4
- package/dist/client/database/index.d.ts.map +1 -1
- package/dist/client/database/index.js.map +1 -1
- package/dist/client/index.d.ts +4 -2
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +4 -2
- package/dist/client/index.js.map +1 -1
- package/dist/client/model/base.d.ts +553 -136
- package/dist/client/model/base.d.ts.map +1 -1
- package/dist/client/model/base.js +1 -6
- package/dist/client/model/base.js.map +1 -1
- package/dist/client/model/client.d.ts +45 -8
- package/dist/client/model/client.d.ts.map +1 -1
- package/dist/client/model/client.js +58 -2
- package/dist/client/model/client.js.map +1 -1
- package/dist/client/model/filters.d.ts +276 -0
- package/dist/client/model/filters.d.ts.map +1 -0
- package/dist/client/model/filters.js +470 -0
- package/dist/client/model/filters.js.map +1 -0
- package/dist/client/model/normalizer.d.ts +127 -3
- package/dist/client/model/normalizer.d.ts.map +1 -1
- package/dist/client/model/normalizer.js +526 -106
- package/dist/client/model/normalizer.js.map +1 -1
- package/dist/client/nodes.d.ts +62 -0
- package/dist/client/nodes.d.ts.map +1 -0
- package/dist/client/nodes.js +378 -0
- package/dist/client/nodes.js.map +1 -0
- package/dist/client/pagination/cursor.d.ts +40 -0
- package/dist/client/pagination/cursor.d.ts.map +1 -0
- package/dist/client/pagination/cursor.js +79 -0
- package/dist/client/pagination/cursor.js.map +1 -0
- package/dist/client/pagination/page.d.ts +23 -0
- package/dist/client/pagination/page.d.ts.map +1 -0
- package/dist/client/pagination/page.js +37 -0
- package/dist/client/pagination/page.js.map +1 -0
- package/dist/client/transaction/index.js.map +1 -1
- package/dist/client/transaction/occ-retry.d.ts +2 -2
- package/dist/client/transaction/occ-retry.d.ts.map +1 -1
- package/dist/client/transaction/occ-retry.js.map +1 -1
- package/dist/client/transaction/transaction-client.d.ts +4 -6
- package/dist/client/transaction/transaction-client.d.ts.map +1 -1
- package/dist/client/transaction/transaction-client.js +7 -18
- package/dist/client/transaction/transaction-client.js.map +1 -1
- package/dist/client/union/client.d.ts +55 -0
- package/dist/client/union/client.d.ts.map +1 -0
- package/dist/client/union/client.js +74 -0
- package/dist/client/union/client.js.map +1 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/pg/index.d.ts +13 -1
- package/dist/pg/index.d.ts.map +1 -1
- package/dist/pg/index.js +52 -5
- package/dist/pg/index.js.map +1 -1
- package/dist/pglite/index.d.ts +7 -6
- package/dist/pglite/index.d.ts.map +1 -1
- package/dist/pglite/index.js +18 -4
- package/dist/pglite/index.js.map +1 -1
- package/dist/schema/columns/array.d.ts +12 -8
- package/dist/schema/columns/array.d.ts.map +1 -1
- package/dist/schema/columns/array.js +11 -14
- package/dist/schema/columns/array.js.map +1 -1
- package/dist/schema/columns/bigint.d.ts +1 -1
- package/dist/schema/columns/bigint.d.ts.map +1 -1
- package/dist/schema/columns/bigint.js +1 -0
- package/dist/schema/columns/bigint.js.map +1 -1
- package/dist/schema/columns/boolean.d.ts +1 -1
- package/dist/schema/columns/boolean.d.ts.map +1 -1
- package/dist/schema/columns/boolean.js +1 -0
- package/dist/schema/columns/boolean.js.map +1 -1
- package/dist/schema/columns/bytea.d.ts +1 -1
- package/dist/schema/columns/bytea.d.ts.map +1 -1
- package/dist/schema/columns/bytea.js +1 -0
- package/dist/schema/columns/bytea.js.map +1 -1
- package/dist/schema/columns/char.d.ts +1 -1
- package/dist/schema/columns/char.d.ts.map +1 -1
- package/dist/schema/columns/char.js +1 -0
- package/dist/schema/columns/char.js.map +1 -1
- package/dist/schema/columns/date.d.ts +3 -3
- package/dist/schema/columns/date.d.ts.map +1 -1
- package/dist/schema/columns/date.js +1 -0
- package/dist/schema/columns/date.js.map +1 -1
- package/dist/schema/columns/double.d.ts +1 -1
- package/dist/schema/columns/double.d.ts.map +1 -1
- package/dist/schema/columns/double.js +1 -0
- package/dist/schema/columns/double.js.map +1 -1
- package/dist/schema/columns/guid.d.ts +57 -0
- package/dist/schema/columns/guid.d.ts.map +1 -0
- package/dist/schema/columns/guid.js +53 -0
- package/dist/schema/columns/guid.js.map +1 -0
- package/dist/schema/columns/identity.d.ts +6 -6
- package/dist/schema/columns/identity.d.ts.map +1 -1
- package/dist/schema/columns/identity.js +2 -0
- package/dist/schema/columns/identity.js.map +1 -1
- package/dist/schema/columns/int.d.ts +1 -1
- package/dist/schema/columns/int.d.ts.map +1 -1
- package/dist/schema/columns/int.js +1 -0
- package/dist/schema/columns/int.js.map +1 -1
- package/dist/schema/columns/interval.d.ts +4 -3
- package/dist/schema/columns/interval.d.ts.map +1 -1
- package/dist/schema/columns/interval.js +1 -0
- package/dist/schema/columns/interval.js.map +1 -1
- package/dist/schema/columns/json.d.ts +79 -2
- package/dist/schema/columns/json.d.ts.map +1 -1
- package/dist/schema/columns/json.js +158 -8
- package/dist/schema/columns/json.js.map +1 -1
- package/dist/schema/columns/numeric.d.ts +20 -3
- package/dist/schema/columns/numeric.d.ts.map +1 -1
- package/dist/schema/columns/numeric.js +60 -4
- package/dist/schema/columns/numeric.js.map +1 -1
- package/dist/schema/columns/real.d.ts +1 -1
- package/dist/schema/columns/real.d.ts.map +1 -1
- package/dist/schema/columns/real.js +1 -0
- package/dist/schema/columns/real.js.map +1 -1
- package/dist/schema/columns/record.d.ts +20 -0
- package/dist/schema/columns/record.d.ts.map +1 -0
- package/dist/schema/columns/record.js +21 -0
- package/dist/schema/columns/record.js.map +1 -0
- package/dist/schema/columns/smallint.d.ts +1 -1
- package/dist/schema/columns/smallint.d.ts.map +1 -1
- package/dist/schema/columns/smallint.js +1 -0
- package/dist/schema/columns/smallint.js.map +1 -1
- package/dist/schema/columns/text.d.ts +1 -1
- package/dist/schema/columns/text.d.ts.map +1 -1
- package/dist/schema/columns/text.js +1 -0
- package/dist/schema/columns/text.js.map +1 -1
- package/dist/schema/columns/time.d.ts +3 -3
- package/dist/schema/columns/time.d.ts.map +1 -1
- package/dist/schema/columns/time.js +1 -0
- package/dist/schema/columns/time.js.map +1 -1
- package/dist/schema/columns/timestamp.d.ts +6 -6
- package/dist/schema/columns/timestamp.d.ts.map +1 -1
- package/dist/schema/columns/timestamp.js +1 -0
- package/dist/schema/columns/timestamp.js.map +1 -1
- package/dist/schema/columns/uuid.d.ts +1 -1
- package/dist/schema/columns/uuid.d.ts.map +1 -1
- package/dist/schema/columns/uuid.js +1 -0
- package/dist/schema/columns/uuid.js.map +1 -1
- package/dist/schema/columns/varchar.d.ts +1 -1
- package/dist/schema/columns/varchar.d.ts.map +1 -1
- package/dist/schema/columns/varchar.js +1 -0
- package/dist/schema/columns/varchar.js.map +1 -1
- package/dist/schema/domain.d.ts +2 -2
- package/dist/schema/domain.d.ts.map +1 -1
- package/dist/schema/domain.js +2 -0
- package/dist/schema/domain.js.map +1 -1
- package/dist/schema/embedded.d.ts +31 -0
- package/dist/schema/embedded.d.ts.map +1 -0
- package/dist/schema/embedded.js +33 -0
- package/dist/schema/embedded.js.map +1 -0
- package/dist/schema/index.d.ts +8 -1
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema/index.js +6 -1
- package/dist/schema/index.js.map +1 -1
- package/dist/schema/namespace.js.map +1 -1
- package/dist/schema/relations.d.ts +17 -5
- package/dist/schema/relations.d.ts.map +1 -1
- package/dist/schema/relations.js +9 -0
- package/dist/schema/relations.js.map +1 -1
- package/dist/schema/sequence.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/tenant.d.ts +31 -0
- package/dist/schema/tenant.d.ts.map +1 -0
- package/dist/schema/tenant.js +33 -0
- package/dist/schema/tenant.js.map +1 -0
- package/dist/schema/union.d.ts +25 -0
- package/dist/schema/union.d.ts.map +1 -0
- package/dist/schema/union.js +27 -0
- package/dist/schema/union.js.map +1 -0
- package/dist/schema/utils/column-validation.d.ts +19 -0
- package/dist/schema/utils/column-validation.d.ts.map +1 -0
- package/dist/schema/utils/column-validation.js +37 -0
- package/dist/schema/utils/column-validation.js.map +1 -0
- package/dist/schema/utils/date.js.map +1 -1
- package/dist/schema/utils/duration.d.ts +2 -2
- package/dist/schema/utils/duration.d.ts.map +1 -1
- package/dist/schema/utils/duration.js.map +1 -1
- package/dist/schema/utils/global-id.d.ts +67 -0
- package/dist/schema/utils/global-id.d.ts.map +1 -0
- package/dist/schema/utils/global-id.js +99 -0
- package/dist/schema/utils/global-id.js.map +1 -0
- package/dist/schema/utils/standard-schema.d.ts +30 -0
- package/dist/schema/utils/standard-schema.d.ts.map +1 -0
- package/dist/schema/utils/standard-schema.js +2 -0
- package/dist/schema/utils/standard-schema.js.map +1 -0
- package/package.json +21 -16
- package/dist/schema/utils/json.d.ts +0 -2
- package/dist/schema/utils/json.d.ts.map +0 -1
- package/dist/schema/utils/json.js +0 -12
- package/dist/schema/utils/json.js.map +0 -1
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { UUIDColumnDefinition } from "./uuid.js";
|
|
2
|
+
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
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Defines a `uuid` column whose values are global ids — opaque strings naming both the row and
|
|
18
|
+
* the table it belongs to, rather than a bare uuid that could have come from anywhere.
|
|
19
|
+
*
|
|
20
|
+
* A table is a **node** when its primary key is exactly one `guid()` column. Its node key is
|
|
21
|
+
* the table's schema alias, so `dsql.members` produces ids keyed `members` even though the
|
|
22
|
+
* table is `team_members`.
|
|
23
|
+
*
|
|
24
|
+
* @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
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* export const authors = table("authors", {
|
|
33
|
+
* id: guid("id").primaryKey().defaultRandom(), // node key: "authors"
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* export const articles = table("articles", {
|
|
37
|
+
* id: guid("id").primaryKey().defaultRandom(), // node key: "articles"
|
|
38
|
+
* authorId: guid("author_id", "authors").notNull(), // points at the authors node
|
|
39
|
+
* parentId: guid("parent_id"), // self reference: "articles"
|
|
40
|
+
* });
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* Migration-neutral: a `guid()` column serializes exactly as `uuid()`, so adopting it on an
|
|
44
|
+
* existing column produces no DDL.
|
|
45
|
+
*/
|
|
46
|
+
export function guid(name, key) {
|
|
47
|
+
return new GuidColumnDefinition(name, {
|
|
48
|
+
dataType: "uuid",
|
|
49
|
+
runtimeType: "uuid",
|
|
50
|
+
codec: { encode: (value) => value, decode: (value) => value },
|
|
51
|
+
}, key);
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=guid.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guid.js","sourceRoot":"","sources":["../../../src/schema/columns/guid.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAUjD,MAAM,OAAO,oBAGX,SAAQ,oBAAoC;IAC5C;;;;;;OAMG;IACO,QAAQ,CAAqB;IAEvC,YAAY,IAAW,EAAE,SAA2B,EAAE,EAAE,GAAY;QAClE,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAEpB,IAAI,CAAC,QAAQ,GAAG,GAAG,CAAC;IACtB,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,UAAU,IAAI,CAA6B,IAAW,EAAE,GAAY;IACxE,OAAO,IAAI,oBAAoB,CAC7B,IAAI,EACJ;QACE,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;KAC9D,EACD,GAAG,CACuE,CAAC;AAC/E,CAAC","sourcesContent":["import { ColumnConfig } from \"@dsqlbase/core\";\nimport { TypedObject } from \"@dsqlbase/core/utils\";\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 * The node key this column's values name, when the declaration overrode it.\n *\n * `undefined` means \"the table this column is declared on\", resolved to that table's schema\n * alias when the client is built — a column cannot know its own alias, since the alias is\n * the key it is exported under.\n */\n protected _guidKey: string | undefined;\n\n constructor(name: TName, config: Partial<TConfig> = {}, key?: string) {\n super(name, config);\n\n this._guidKey = key;\n }\n}\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 table is a **node** when its primary key is exactly one `guid()` column. Its node key is\n * the table's schema alias, so `dsql.members` produces ids keyed `members` even though the\n * table is `team_members`.\n *\n * @param name The database column name.\n * @param key The node this column *points at*, for a column that is not its own table's key.\n * A relation key column takes the alias of the table it references, so\n * `article.authorId === article.author.id` holds without the application unwrapping either.\n * Omit it on a primary key, and on a self reference.\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\", \"authors\").notNull(), // points at the authors node\n * parentId: guid(\"parent_id\"), // self reference: \"articles\"\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, key?: string) {\n return new GuidColumnDefinition<TName, ColumnConfig<string, string, \"uuid\">>(\n name,\n {\n dataType: \"uuid\",\n runtimeType: \"uuid\",\n codec: { encode: (value) => value, decode: (value) => value },\n },\n key\n ) as Guid<GuidColumnDefinition<TName, ColumnConfig<string, string, \"uuid\">>>;\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ColumnConfig, ColumnDefinition, ColumnGeneratedType, ColumnIdentityConfig } from "@dsqlbase/core/definition";
|
|
2
|
-
export
|
|
2
|
+
export type IdentityConfig<T extends IdentityColumnOptions> = {
|
|
3
3
|
type: T["type"] extends "BY DEFAULT" ? "BY DEFAULT" : "ALWAYS";
|
|
4
4
|
sequenceName?: T["sequenceName"];
|
|
5
5
|
options: {
|
|
@@ -11,14 +11,14 @@ export interface IdentityConfig<T extends IdentityColumnOptions> {
|
|
|
11
11
|
maxValue: number;
|
|
12
12
|
cycle: boolean;
|
|
13
13
|
};
|
|
14
|
-
}
|
|
15
|
-
export declare class IdentityColumnDefinition<TName extends string, TIdentity extends IdentityConfig<IdentityColumnOptions>> extends ColumnDefinition<TName, Omit<ColumnConfig<number, string>, "identity"> & {
|
|
14
|
+
};
|
|
15
|
+
export declare class IdentityColumnDefinition<TName extends string, TIdentity extends IdentityConfig<IdentityColumnOptions>> extends ColumnDefinition<TName, Omit<ColumnConfig<number, string, "number">, "identity"> & {
|
|
16
16
|
notNull: true;
|
|
17
17
|
hasDefault: true;
|
|
18
18
|
identity: TIdentity;
|
|
19
19
|
}> {
|
|
20
20
|
protected _identity: ColumnIdentityConfig;
|
|
21
|
-
constructor(name: TName, config: Partial<Omit<ColumnConfig<number, string>, "identity"> & {
|
|
21
|
+
constructor(name: TName, config: Partial<Omit<ColumnConfig<number, string, "number">, "identity"> & {
|
|
22
22
|
notNull: true;
|
|
23
23
|
hasDefault: true;
|
|
24
24
|
identity: TIdentity;
|
|
@@ -31,7 +31,7 @@ export declare class IdentityColumnDefinition<TName extends string, TIdentity ex
|
|
|
31
31
|
minValue(minValue: number): this;
|
|
32
32
|
maxValue(maxValue: number): this;
|
|
33
33
|
}
|
|
34
|
-
export
|
|
34
|
+
export type IdentityColumnOptions = {
|
|
35
35
|
/**
|
|
36
36
|
* Determines how the identity value is generated:
|
|
37
37
|
* - `ALWAYS`: The identity value is always generated by the database, and cannot be overridden by insert statements.
|
|
@@ -44,7 +44,7 @@ export interface IdentityColumnOptions {
|
|
|
44
44
|
* Name of the identity sequence to use for generating values. If not provided, an unused sequence name will be generated automatically.
|
|
45
45
|
*/
|
|
46
46
|
sequenceName?: string;
|
|
47
|
-
}
|
|
47
|
+
};
|
|
48
48
|
/**
|
|
49
49
|
* Defines an identity column in the database schema, which automatically generates unique numeric values for new rows.
|
|
50
50
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../../../src/schema/columns/identity.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,mBAAmB,EACnB,oBAAoB,EACrB,MAAM,2BAA2B,CAAC;AAEnC,MAAM,
|
|
1
|
+
{"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../../../src/schema/columns/identity.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,mBAAmB,EACnB,oBAAoB,EACrB,MAAM,2BAA2B,CAAC;AAEnC,MAAM,MAAM,cAAc,CAAC,CAAC,SAAS,qBAAqB,IAAI;IAC5D,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,SAAS,YAAY,GAAG,YAAY,GAAG,QAAQ,CAAC;IAC/D,YAAY,CAAC,EAAE,CAAC,CAAC,cAAc,CAAC,CAAC;IACjC,OAAO,EAAE;QACP,QAAQ,EAAE,QAAQ,CAAC;QACnB,KAAK,EAAE,MAAM,CAAC;QACd,UAAU,EAAE,MAAM,CAAC;QACnB,SAAS,EAAE,MAAM,CAAC;QAClB,QAAQ,EAAE,MAAM,CAAC;QACjB,QAAQ,EAAE,MAAM,CAAC;QACjB,KAAK,EAAE,OAAO,CAAC;KAChB,CAAC;CACH,CAAC;AAEF,qBAAa,wBAAwB,CACnC,KAAK,SAAS,MAAM,EACpB,SAAS,SAAS,cAAc,CAAC,qBAAqB,CAAC,CACvD,SAAQ,gBAAgB,CACxB,KAAK,EACL,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,UAAU,CAAC,GAAG;IACzD,OAAO,EAAE,IAAI,CAAC;IACd,UAAU,EAAE,IAAI,CAAC;IACjB,QAAQ,EAAE,SAAS,CAAC;CACrB,CACF;IACC,SAAS,CAAC,SAAS,EAAE,oBAAoB,CAAC;gBAGxC,IAAI,EAAE,KAAK,EACX,MAAM,EAAE,OAAO,CACb,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,UAAU,CAAC,GAAG;QACzD,OAAO,EAAE,IAAI,CAAC;QACd,UAAU,EAAE,IAAI,CAAC;QACjB,QAAQ,EAAE,SAAS,CAAC;KACrB,CACF;IAMI,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAKhC,KAAK,CAAC,KAAK,UAAO,GAAG,IAAI;IAKzB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAK1B,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAKpC,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAKlC,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAKhC,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;CAIxC;AAED,MAAM,MAAM,qBAAqB,GAAG;IAClC;;;;;;OAMG;IACH,IAAI,EAAE,mBAAmB,CAAC;IAE1B;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF;;;;;;GAMG;AAEH,wBAAgB,QAAQ,CAAC,KAAK,CAAC,KAAK,SAAS,MAAM,EAAE,KAAK,CAAC,QAAQ,SAAS,qBAAqB,EAC/F,IAAI,EAAE,KAAK,EACX,OAAO,CAAC,EAAE,QAAQ,6DAwBnB"}
|
|
@@ -44,6 +44,7 @@ export class IdentityColumnDefinition extends ColumnDefinition {
|
|
|
44
44
|
export function identity(name, options) {
|
|
45
45
|
const identity = {
|
|
46
46
|
type: options?.type ?? "ALWAYS",
|
|
47
|
+
sequenceName: options?.sequenceName,
|
|
47
48
|
options: {
|
|
48
49
|
dataType: "bigint",
|
|
49
50
|
cache: 1,
|
|
@@ -55,6 +56,7 @@ export function identity(name, options) {
|
|
|
55
56
|
};
|
|
56
57
|
return new IdentityColumnDefinition(name, {
|
|
57
58
|
dataType: "bigint",
|
|
59
|
+
runtimeType: "number",
|
|
58
60
|
identity,
|
|
59
61
|
codec: {
|
|
60
62
|
encode: (value) => value.toString(),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"identity.js","sourceRoot":"","sources":["../../../src/schema/columns/identity.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,gBAAgB,GAGjB,MAAM,2BAA2B,CAAC;AAgBnC,MAAM,OAAO,wBAGX,SAAQ,gBAOT;IACW,SAAS,CAAuB;IAE1C,YACE,IAAW,EACX,MAMC;QAED,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACpB,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,QAAqB,CAAC;IAChD,CAAC;IAEM,YAAY,CAAC,IAAY;QAC9B,IAAI,CAAC,SAAS,CAAC,YAAY,GAAG,IAAI,CAAC;QACnC,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,KAAK,CAAC,KAAK,GAAG,IAAI;QACvB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC;QACrC,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,KAAK,CAAC,KAAa;QACxB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC;QACrC,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,UAAU,CAAC,UAAkB;QAClC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,UAAU,GAAG,UAAU,CAAC;QAC/C,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,SAAS,CAAC,SAAiB;QAChC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC;QAC7C,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,QAAQ,CAAC,QAAgB;QAC9B,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,GAAG,QAAQ,CA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W,EAAE,QAAQ;QACrB,QAAQ;QACR,KAAK,EAAE;YACL,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,QAAQ,EAAE;YACnC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC;SACjC;KACF,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n ColumnConfig,\n ColumnDefinition,\n ColumnGeneratedType,\n ColumnIdentityConfig,\n} from \"@dsqlbase/core/definition\";\n\nexport type IdentityConfig<T extends IdentityColumnOptions> = {\n type: T[\"type\"] extends \"BY DEFAULT\" ? \"BY DEFAULT\" : \"ALWAYS\";\n sequenceName?: T[\"sequenceName\"];\n options: {\n dataType: \"bigint\";\n cache: number;\n startValue: number;\n increment: number;\n minValue: number;\n maxValue: number;\n cycle: boolean;\n };\n};\n\nexport class IdentityColumnDefinition<\n TName extends string,\n TIdentity extends IdentityConfig<IdentityColumnOptions>,\n> extends ColumnDefinition<\n TName,\n Omit<ColumnConfig<number, string, \"number\">, \"identity\"> & {\n notNull: true;\n hasDefault: true;\n identity: TIdentity;\n }\n> {\n protected _identity: ColumnIdentityConfig;\n\n constructor(\n name: TName,\n config: Partial<\n Omit<ColumnConfig<number, string, \"number\">, \"identity\"> & {\n notNull: true;\n hasDefault: true;\n identity: TIdentity;\n }\n >\n ) {\n super(name, config);\n this._identity = config.identity as TIdentity;\n }\n\n public sequenceName(name: string): this {\n this._identity.sequenceName = name;\n return this;\n }\n\n public cycle(cycle = true): this {\n this._identity.options.cycle = cycle;\n return this;\n }\n\n public cache(cache: number): this {\n this._identity.options.cache = cache;\n return this;\n }\n\n public startValue(startValue: number): this {\n this._identity.options.startValue = startValue;\n return this;\n }\n\n public increment(increment: number): this {\n this._identity.options.increment = increment;\n return this;\n }\n\n public minValue(minValue: number): this {\n this._identity.options.minValue = minValue;\n return this;\n }\n\n public maxValue(maxValue: number): this {\n this._identity.options.maxValue = maxValue;\n return this;\n }\n}\n\nexport type IdentityColumnOptions = {\n /**\n * Determines how the identity value is generated:\n * - `ALWAYS`: The identity value is always generated by the database, and cannot be overridden by insert statements.\n * - `BY DEFAULT`: The identity value is generated by the database if no value is provided in the insert statement, but can be overridden by providing a value.\n *\n * @default \"ALWAYS\"\n */\n type: ColumnGeneratedType;\n\n /**\n * Name of the identity sequence to use for generating values. If not provided, an unused sequence name will be generated automatically.\n */\n sequenceName?: string;\n};\n\n/**\n * Defines an identity column in the database schema, which automatically generates unique numeric values for new rows.\n *\n * @param name Column name in the database\n * @param options Configuration options for the identity column\n * @returns Serializable column definition for an identity column.\n */\n\nexport function identity<const TName extends string, const TOptions extends IdentityColumnOptions>(\n name: TName,\n options?: TOptions\n) {\n const identity = {\n type: options?.type ?? \"ALWAYS\",\n sequenceName: options?.sequenceName,\n options: {\n dataType: \"bigint\",\n cache: 1,\n startValue: 1,\n increment: 1,\n minValue: 1,\n cycle: false,\n },\n } as IdentityConfig<TOptions>;\n\n return new IdentityColumnDefinition<TName, IdentityConfig<TOptions>>(name, {\n dataType: \"bigint\",\n runtimeType: \"number\",\n identity,\n codec: {\n encode: (value) => value.toString(),\n decode: (value) => Number(value),\n },\n });\n}\n"]}
|
|
@@ -6,6 +6,6 @@ import { ColumnConfig, ColumnDefinition } from "@dsqlbase/core";
|
|
|
6
6
|
* @param name
|
|
7
7
|
* @returns Serializable column definition for an int column.
|
|
8
8
|
*/
|
|
9
|
-
export declare function int<const TName extends string>(name: TName): ColumnDefinition<TName, ColumnConfig<number, string>>;
|
|
9
|
+
export declare function int<const TName extends string>(name: TName): ColumnDefinition<TName, ColumnConfig<number, string, "number">>;
|
|
10
10
|
export { int as int4 };
|
|
11
11
|
//# sourceMappingURL=int.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"int.d.ts","sourceRoot":"","sources":["../../../src/schema/columns/int.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEhE;;;;;;GAMG;AAEH,wBAAgB,GAAG,CAAC,KAAK,CAAC,KAAK,SAAS,MAAM,EAAE,IAAI,EAAE,KAAK,
|
|
1
|
+
{"version":3,"file":"int.d.ts","sourceRoot":"","sources":["../../../src/schema/columns/int.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEhE;;;;;;GAMG;AAEH,wBAAgB,GAAG,CAAC,KAAK,CAAC,KAAK,SAAS,MAAM,EAAE,IAAI,EAAE,KAAK,mEAS1D;AAED,OAAO,EAAE,GAAG,IAAI,IAAI,EAAE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"int.js","sourceRoot":"","sources":["../../../src/schema/columns/int.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEhE;;;;;;GAMG;AAEH,MAAM,UAAU,GAAG,CAA6B,IAAW;IACzD,OAAO,IAAI,gBAAgB,
|
|
1
|
+
{"version":3,"file":"int.js","sourceRoot":"","sources":["../../../src/schema/columns/int.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEhE;;;;;;GAMG;AAEH,MAAM,UAAU,GAAG,CAA6B,IAAW;IACzD,OAAO,IAAI,gBAAgB,CAAgD,IAAI,EAAE;QAC/E,QAAQ,EAAE,KAAK;QACf,WAAW,EAAE,QAAQ;QACrB,KAAK,EAAE;YACL,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,QAAQ,EAAE;YACnC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC;SACvC;KACF,CAAC,CAAC;AACL,CAAC;AAED,OAAO,EAAE,GAAG,IAAI,IAAI,EAAE,CAAC","sourcesContent":["import { ColumnConfig, ColumnDefinition } from \"@dsqlbase/core\";\n\n/**\n * Defines an `int` (4 bytes) data type column in the database schema.\n *\n * Range: `-2_147_483_648 to +2_147_483_647`\n * @param name\n * @returns Serializable column definition for an int column.\n */\n\nexport function int<const TName extends string>(name: TName) {\n return new ColumnDefinition<TName, ColumnConfig<number, string, \"number\">>(name, {\n dataType: \"int\",\n runtimeType: \"number\",\n codec: {\n encode: (value) => value.toString(),\n decode: (value) => parseInt(value, 10),\n },\n });\n}\n\nexport { int as int4 };\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ColumnConfig, ColumnDefinition } from "@dsqlbase/core";
|
|
2
2
|
import { Duration } from "../utils/duration.js";
|
|
3
|
-
export
|
|
3
|
+
export type IntervalColumnOptions = {
|
|
4
4
|
/**
|
|
5
5
|
* Determines how the interval is parsed and formatted, at runtime:
|
|
6
6
|
* - `"iso"`: The column will handle interval values as ISO 8601 duration strings (e.g., 'P3Y6M4DT12H30M5S').
|
|
@@ -10,14 +10,15 @@ export interface IntervalColumnOptions {
|
|
|
10
10
|
* @default "object"
|
|
11
11
|
*/
|
|
12
12
|
mode?: "iso" | "string" | "object";
|
|
13
|
-
}
|
|
13
|
+
};
|
|
14
14
|
/**
|
|
15
15
|
* Defines an `interval` data type column in the database schema.
|
|
16
16
|
* @param name Column name
|
|
17
17
|
* @param options Config options
|
|
18
18
|
* @returns Serializable column definition for an interval column.
|
|
19
19
|
*/
|
|
20
|
-
export declare function interval<const TName extends string, const TOptions extends IntervalColumnOptions>(name: TName, options?: TOptions): ColumnDefinition<TName,
|
|
20
|
+
export declare function interval<const TName extends string, const TOptions extends IntervalColumnOptions>(name: TName, options?: TOptions): ColumnDefinition<TName, IntervalColumnConfig<TOptions>>;
|
|
21
21
|
export { interval as duration };
|
|
22
|
+
type IntervalColumnConfig<TOptions extends IntervalColumnOptions> = ColumnConfig<IntervalValueType<TOptions>, string, "interval">;
|
|
22
23
|
export type IntervalValueType<TOptions extends IntervalColumnOptions> = TOptions["mode"] extends "iso" | "string" ? string : Duration;
|
|
23
24
|
//# sourceMappingURL=interval.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interval.d.ts","sourceRoot":"","sources":["../../../src/schema/columns/interval.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EACL,QAAQ,EAIT,MAAM,sBAAsB,CAAC;AAE9B,MAAM,
|
|
1
|
+
{"version":3,"file":"interval.d.ts","sourceRoot":"","sources":["../../../src/schema/columns/interval.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EACL,QAAQ,EAIT,MAAM,sBAAsB,CAAC;AAE9B,MAAM,MAAM,qBAAqB,GAAG;IAClC;;;;;;;OAOG;IACH,IAAI,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,QAAQ,CAAC;CACpC,CAAC;AAEF;;;;;GAKG;AAEH,wBAAgB,QAAQ,CAAC,KAAK,CAAC,KAAK,SAAS,MAAM,EAAE,KAAK,CAAC,QAAQ,SAAS,qBAAqB,EAC/F,IAAI,EAAE,KAAK,EACX,OAAO,CAAC,EAAE,QAAQ,2DAiCnB;AAED,OAAO,EAAE,QAAQ,IAAI,QAAQ,EAAE,CAAC;AAEhC,KAAK,oBAAoB,CAAC,QAAQ,SAAS,qBAAqB,IAAI,YAAY,CAC9E,iBAAiB,CAAC,QAAQ,CAAC,EAC3B,MAAM,EACN,UAAU,CACX,CAAC;AAEF,MAAM,MAAM,iBAAiB,CAAC,QAAQ,SAAS,qBAAqB,IAAI,QAAQ,CAAC,MAAM,CAAC,SACpF,KAAK,GACL,QAAQ,GACR,MAAM,GACN,QAAQ,CAAC"}
|
|
@@ -9,6 +9,7 @@ import { formatISODuration, formatStringDuration, safeParseDuration, } from "../
|
|
|
9
9
|
export function interval(name, options) {
|
|
10
10
|
return new ColumnDefinition(name, {
|
|
11
11
|
dataType: "interval",
|
|
12
|
+
runtimeType: "interval",
|
|
12
13
|
codec: {
|
|
13
14
|
encode: (value) => {
|
|
14
15
|
const duration = safeParseDuration(value);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interval.js","sourceRoot":"","sources":["../../../src/schema/columns/interval.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EAEL,iBAAiB,EACjB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,sBAAsB,CAAC;AAc9B;;;;;GAKG;AAEH,MAAM,UAAU,QAAQ,CACtB,IAAW,EACX,OAAkB;IAElB,OAAO,IAAI,gBAAgB,
|
|
1
|
+
{"version":3,"file":"interval.js","sourceRoot":"","sources":["../../../src/schema/columns/interval.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EAEL,iBAAiB,EACjB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,sBAAsB,CAAC;AAc9B;;;;;GAKG;AAEH,MAAM,UAAU,QAAQ,CACtB,IAAW,EACX,OAAkB;IAElB,OAAO,IAAI,gBAAgB,CAAwC,IAAI,EAAE;QACvE,QAAQ,EAAE,UAAU;QACpB,WAAW,EAAE,UAAU;QACvB,KAAK,EAAE;YACL,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;gBAChB,MAAM,QAAQ,GAAG,iBAAiB,CAAC,KAAK,CAAC,CAAC;gBAE1C,QAAQ,OAAO,EAAE,IAAI,EAAE,CAAC;oBACtB,KAAK,KAAK;wBACR,OAAO,iBAAiB,CAAC,QAAQ,CAAC,CAAC;oBACrC,KAAK,QAAQ,CAAC;oBACd,KAAK,QAAQ,CAAC;oBACd;wBACE,OAAO,oBAAoB,CAAC,QAAQ,CAAC,CAAC;gBAC1C,CAAC;YACH,CAAC;YACD,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;gBAChB,MAAM,QAAQ,GAAG,iBAAiB,CAAC,KAAK,CAAC,CAAC;gBAE1C,QAAQ,OAAO,EAAE,IAAI,EAAE,CAAC;oBACtB,KAAK,KAAK;wBACR,OAAO,iBAAiB,CAAC,QAAQ,CAAgC,CAAC;oBACpE,KAAK,QAAQ;wBACX,OAAO,oBAAoB,CAAC,QAAQ,CAAgC,CAAC;oBACvE,KAAK,QAAQ,CAAC;oBACd;wBACE,OAAO,QAAuC,CAAC;gBACnD,CAAC;YACH,CAAC;SACF;KACF,CAAC,CAAC;AACL,CAAC;AAED,OAAO,EAAE,QAAQ,IAAI,QAAQ,EAAE,CAAC","sourcesContent":["import { ColumnConfig, ColumnDefinition } from \"@dsqlbase/core\";\nimport {\n Duration,\n formatISODuration,\n formatStringDuration,\n safeParseDuration,\n} from \"../utils/duration.js\";\n\nexport type IntervalColumnOptions = {\n /**\n * Determines how the interval is parsed and formatted, at runtime:\n * - `\"iso\"`: The column will handle interval values as ISO 8601 duration strings (e.g., 'P3Y6M4DT12H30M5S').\n * - `\"string\"`: The column will handle interval values as human-readable strings (e.g., '3 years 6 months 4 days 12 hours 30 minutes 5 seconds').\n * - `\"object\"`: The column will handle interval values as `Duration` objects with properties for years, months, days, hours, minutes, and seconds (e.g., `{ years: 3, months: 6, days: 4, hours: 12, minutes: 30, seconds: 5 }`).\n *\n * @default \"object\"\n */\n mode?: \"iso\" | \"string\" | \"object\";\n};\n\n/**\n * Defines an `interval` data type column in the database schema.\n * @param name Column name\n * @param options Config options\n * @returns Serializable column definition for an interval column.\n */\n\nexport function interval<const TName extends string, const TOptions extends IntervalColumnOptions>(\n name: TName,\n options?: TOptions\n) {\n return new ColumnDefinition<TName, IntervalColumnConfig<TOptions>>(name, {\n dataType: \"interval\",\n runtimeType: \"interval\",\n codec: {\n encode: (value) => {\n const duration = safeParseDuration(value);\n\n switch (options?.mode) {\n case \"iso\":\n return formatISODuration(duration);\n case \"object\":\n case \"string\":\n default:\n return formatStringDuration(duration);\n }\n },\n decode: (value) => {\n const duration = safeParseDuration(value);\n\n switch (options?.mode) {\n case \"iso\":\n return formatISODuration(duration) as IntervalValueType<TOptions>;\n case \"string\":\n return formatStringDuration(duration) as IntervalValueType<TOptions>;\n case \"object\":\n default:\n return duration as IntervalValueType<TOptions>;\n }\n },\n },\n });\n}\n\nexport { interval as duration };\n\ntype IntervalColumnConfig<TOptions extends IntervalColumnOptions> = ColumnConfig<\n IntervalValueType<TOptions>,\n string,\n \"interval\"\n>;\n\nexport type IntervalValueType<TOptions extends IntervalColumnOptions> = TOptions[\"mode\"] extends\n | \"iso\"\n | \"string\"\n ? string\n : Duration;\n"]}
|
|
@@ -1,9 +1,86 @@
|
|
|
1
1
|
import { ColumnConfig, ColumnDefinition } from "@dsqlbase/core";
|
|
2
|
+
import { HasDefault, TypedObject } from "@dsqlbase/core/utils";
|
|
3
|
+
import type { InferSchemaInput, InferSchemaOutput, StandardSchemaV1 } from "../utils/standard-schema.js";
|
|
4
|
+
/** A JSON column's types once `.schema()` sets them: writes take its input, reads its output. */
|
|
5
|
+
export type WithSchema<T extends TypedObject, S extends StandardSchemaV1> = T & {
|
|
6
|
+
__type: {
|
|
7
|
+
valueType: InferSchemaOutput<S>;
|
|
8
|
+
inputType: InferSchemaInput<S>;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
type JsonColumnConfig = ColumnConfig<unknown, unknown, "json" | "jsonb" | "array" | "object">;
|
|
12
|
+
/** The top-level shape `array()` and `record()` hold, checked on every write and every read. */
|
|
13
|
+
export type JsonShape = "array" | "object";
|
|
2
14
|
/**
|
|
3
|
-
*
|
|
15
|
+
* The schemas a JSON column takes: any, or on `array()` / `record()` one whose output is an array
|
|
16
|
+
* / an object.
|
|
17
|
+
*/
|
|
18
|
+
export type JsonSchemaFor<T extends TypedObject> = T["__type"] extends {
|
|
19
|
+
runtimeType: "array";
|
|
20
|
+
} ? StandardSchemaV1<unknown, readonly unknown[]> : T["__type"] extends {
|
|
21
|
+
runtimeType: "object";
|
|
22
|
+
} ? StandardSchemaV1<unknown, object> : StandardSchemaV1;
|
|
23
|
+
/**
|
|
24
|
+
* A `json` or `jsonb` column. A value may be any JSON value — object, array, string, number or
|
|
25
|
+
* boolean — and `null` is SQL `NULL`, never a JSON `null`.
|
|
26
|
+
*
|
|
27
|
+
* The driver returns these columns parsed, so a read is the value as stored. With `.schema()`,
|
|
28
|
+
* every write and every read is validated.
|
|
29
|
+
*/
|
|
30
|
+
export declare class JsonColumnDefinition<TName extends string, TConfig extends JsonColumnConfig> extends ColumnDefinition<TName, TConfig> {
|
|
31
|
+
/** The value `.default()` was given, kept so a later `.schema()` can validate it. */
|
|
32
|
+
protected _defaultInput?: {
|
|
33
|
+
value: unknown;
|
|
34
|
+
};
|
|
35
|
+
/** The shape every value must have, if the column constrains it. */
|
|
36
|
+
protected _shape?: JsonShape;
|
|
37
|
+
constructor(name: TName, config?: Partial<TConfig>, shape?: JsonShape);
|
|
38
|
+
/**
|
|
39
|
+
* Validates the column with a Standard Schema (zod, valibot, arktype, …), which also types
|
|
40
|
+
* it: a write takes the schema's input, a read returns its output.
|
|
41
|
+
*
|
|
42
|
+
* - **A write** validates the value and stores the schema's **output** — defaults filled,
|
|
43
|
+
* coercions applied — in its JSON form. It then checks that output validates back to
|
|
44
|
+
* itself, so a read returns exactly what was stored. A schema that transforms values fails
|
|
45
|
+
* that check, on the first write that reaches the transform.
|
|
46
|
+
* - **A read** validates the stored value and returns the schema's output, so the read type
|
|
47
|
+
* holds for every row. A row that fails — written by raw SQL, or under an older schema —
|
|
48
|
+
* fails the whole read.
|
|
49
|
+
*
|
|
50
|
+
* Failures throw {@link ColumnValidationError}. The schema must validate synchronously.
|
|
51
|
+
*
|
|
52
|
+
* Filters are not validated: a filter value is compared with stored values, and may be only a
|
|
53
|
+
* fragment of one.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* const Settings = z.object({ theme: z.enum(["light", "dark"]).default("light") });
|
|
58
|
+
*
|
|
59
|
+
* settings: jsonb("settings").schema(Settings)
|
|
60
|
+
* // create({ data: { settings: {} } }) stores {"theme":"light"}
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
schema<S extends JsonSchemaFor<this>>(schema: S): WithSchema<this, S>;
|
|
64
|
+
/** Encoded now, so a default the column's schema refuses fails where it is declared. */
|
|
65
|
+
default(value: this["__type"]["inputType"]): HasDefault<this>;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Defines a `json` column. Prefer {@link jsonb}: `json` stores the text as given and has no
|
|
69
|
+
* equality operator, so it can be neither compared nor used with `distinct`.
|
|
4
70
|
*
|
|
5
71
|
* @param name Column name in database
|
|
6
72
|
* @returns Serializable column definition for a JSON column.
|
|
7
73
|
*/
|
|
8
|
-
export declare function json<const TName extends string>(name: TName):
|
|
74
|
+
export declare function json<const TName extends string>(name: TName): JsonColumnDefinition<TName, ColumnConfig<unknown, unknown, "json">>;
|
|
75
|
+
/**
|
|
76
|
+
* Defines a `jsonb` column: a JSON value stored parsed, which Postgres can compare.
|
|
77
|
+
*
|
|
78
|
+
* In Aurora DSQL a `jsonb` value is limited to 1 MiB compressed, and a `jsonb` column cannot be
|
|
79
|
+
* indexed.
|
|
80
|
+
*
|
|
81
|
+
* @param name Column name in database
|
|
82
|
+
* @returns Serializable column definition for a JSONB column.
|
|
83
|
+
*/
|
|
84
|
+
export declare function jsonb<const TName extends string>(name: TName): JsonColumnDefinition<TName, ColumnConfig<unknown, unknown, "jsonb">>;
|
|
85
|
+
export {};
|
|
9
86
|
//# sourceMappingURL=json.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../../src/schema/columns/json.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../../src/schema/columns/json.ts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A;KAAE,CAAC;IAC7C,oEAAoE;IACpE,SAAS,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC;gBAEjB,IAAI,EAAE,KAAK,EAAE,MAAM,GAAE,OAAO,CAAC,OAAO,CAAM,EAAE,KAAK,CAAC,EAAE,SAAS;IASzE;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACI,MAAM,CAAC,CAAC,SAAS,aAAa,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC;IAc5E,wFAAwF;IACxE,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,WAAW,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC;CAM9E;AAwGD;;;;;;GAMG;AACH,wBAAgB,IAAI,CAAC,KAAK,CAAC,KAAK,SAAS,MAAM,EAAE,IAAI,EAAE,KAAK,uEAK3D;AAED;;;;;;;;GAQG;AACH,wBAAgB,KAAK,CAAC,KAAK,CAAC,KAAK,SAAS,MAAM,EAAE,IAAI,EAAE,KAAK,wEAK5D"}
|
|
@@ -1,18 +1,168 @@
|
|
|
1
|
-
import { ColumnDefinition } from "@dsqlbase/core";
|
|
2
|
-
import {
|
|
1
|
+
import { ColumnDefinition, SQLParam, } from "@dsqlbase/core";
|
|
2
|
+
import { ColumnValidationError } from "../utils/column-validation.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* A `json` or `jsonb` column. A value may be any JSON value — object, array, string, number or
|
|
5
|
+
* boolean — and `null` is SQL `NULL`, never a JSON `null`.
|
|
6
|
+
*
|
|
7
|
+
* The driver returns these columns parsed, so a read is the value as stored. With `.schema()`,
|
|
8
|
+
* every write and every read is validated.
|
|
9
|
+
*/
|
|
10
|
+
export class JsonColumnDefinition extends ColumnDefinition {
|
|
11
|
+
/** The value `.default()` was given, kept so a later `.schema()` can validate it. */
|
|
12
|
+
_defaultInput;
|
|
13
|
+
/** The shape every value must have, if the column constrains it. */
|
|
14
|
+
_shape;
|
|
15
|
+
constructor(name, config = {}, shape) {
|
|
16
|
+
super(name, { ...config, codec: plainJsonCodec });
|
|
17
|
+
if (shape) {
|
|
18
|
+
this._shape = shape;
|
|
19
|
+
this._validator = shapeValidator(name, shape);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Validates the column with a Standard Schema (zod, valibot, arktype, …), which also types
|
|
24
|
+
* it: a write takes the schema's input, a read returns its output.
|
|
25
|
+
*
|
|
26
|
+
* - **A write** validates the value and stores the schema's **output** — defaults filled,
|
|
27
|
+
* coercions applied — in its JSON form. It then checks that output validates back to
|
|
28
|
+
* itself, so a read returns exactly what was stored. A schema that transforms values fails
|
|
29
|
+
* that check, on the first write that reaches the transform.
|
|
30
|
+
* - **A read** validates the stored value and returns the schema's output, so the read type
|
|
31
|
+
* holds for every row. A row that fails — written by raw SQL, or under an older schema —
|
|
32
|
+
* fails the whole read.
|
|
33
|
+
*
|
|
34
|
+
* Failures throw {@link ColumnValidationError}. The schema must validate synchronously.
|
|
35
|
+
*
|
|
36
|
+
* Filters are not validated: a filter value is compared with stored values, and may be only a
|
|
37
|
+
* fragment of one.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* const Settings = z.object({ theme: z.enum(["light", "dark"]).default("light") });
|
|
42
|
+
*
|
|
43
|
+
* settings: jsonb("settings").schema(Settings)
|
|
44
|
+
* // create({ data: { settings: {} } }) stores {"theme":"light"}
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
schema(schema) {
|
|
48
|
+
const validator = schemaValidator(this.name, schema);
|
|
49
|
+
this._validator = (this._shape ? shapeValidator(this.name, this._shape, validator) : validator);
|
|
50
|
+
if (this._defaultInput) {
|
|
51
|
+
this.default(this._defaultInput.value);
|
|
52
|
+
}
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
/** Encoded now, so a default the column's schema refuses fails where it is declared. */
|
|
56
|
+
default(value) {
|
|
57
|
+
this._defaultInput = { value };
|
|
58
|
+
this._defaultValue = new SQLParam(this._codec.encode(this._toValue(value)));
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const plainJsonCodec = {
|
|
63
|
+
encode: (value) => JSON.stringify(value),
|
|
64
|
+
decode: (value) => value,
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Validates writes and reads against `schema`. A write returns the schema's output once it is
|
|
68
|
+
* known to read back as itself: serialized, validated again and serialized to the same text.
|
|
69
|
+
*/
|
|
70
|
+
function schemaValidator(column, schema) {
|
|
71
|
+
const validate = (value, phase) => {
|
|
72
|
+
const result = schema["~standard"].validate(value);
|
|
73
|
+
if (result instanceof Promise) {
|
|
74
|
+
// The pending validation is abandoned; keep its eventual rejection from going unhandled.
|
|
75
|
+
result.catch(() => undefined);
|
|
76
|
+
throw new ColumnValidationError("async", column, phase);
|
|
77
|
+
}
|
|
78
|
+
const settled = result;
|
|
79
|
+
if (settled.issues) {
|
|
80
|
+
throw new ColumnValidationError("invalid", column, phase, settled.issues);
|
|
81
|
+
}
|
|
82
|
+
return settled.value;
|
|
83
|
+
};
|
|
84
|
+
return {
|
|
85
|
+
write(input) {
|
|
86
|
+
const output = validate(input, "write");
|
|
87
|
+
const text = toJson(output, column);
|
|
88
|
+
const again = toJson(validate(JSON.parse(text), "write"), column);
|
|
89
|
+
if (again !== text) {
|
|
90
|
+
throw new ColumnValidationError("unstable", column, "write");
|
|
91
|
+
}
|
|
92
|
+
return output;
|
|
93
|
+
},
|
|
94
|
+
read(stored) {
|
|
95
|
+
return validate(stored, "read");
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Checks that every value is an array, or a plain object, at its top level: a write's value as it
|
|
101
|
+
* will be stored (after `inner`, the schema), a read's before `inner` sees it.
|
|
102
|
+
*/
|
|
103
|
+
function shapeValidator(column, shape, inner) {
|
|
104
|
+
const check = (value, phase) => {
|
|
105
|
+
if (shape === "array" ? !Array.isArray(value) : !isPlainObject(value)) {
|
|
106
|
+
throw new ColumnValidationError("invalid", column, phase, [
|
|
107
|
+
{ message: shape === "array" ? "Expected an array" : "Expected an object" },
|
|
108
|
+
]);
|
|
109
|
+
}
|
|
110
|
+
return value;
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
write: (input) => check(inner ? inner.write(input) : input, "write"),
|
|
114
|
+
read: (stored) => {
|
|
115
|
+
check(stored, "read");
|
|
116
|
+
return inner ? inner.read(stored) : stored;
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function isPlainObject(value) {
|
|
121
|
+
if (typeof value !== "object" || value === null) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
const prototype = Object.getPrototypeOf(value);
|
|
125
|
+
return prototype === Object.prototype || prototype === null;
|
|
126
|
+
}
|
|
127
|
+
function toJson(value, column) {
|
|
128
|
+
let text;
|
|
129
|
+
try {
|
|
130
|
+
text = JSON.stringify(value);
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
text = undefined;
|
|
134
|
+
}
|
|
135
|
+
if (text === undefined) {
|
|
136
|
+
throw new ColumnValidationError("not_json", column, "write");
|
|
137
|
+
}
|
|
138
|
+
return text;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Defines a `json` column. Prefer {@link jsonb}: `json` stores the text as given and has no
|
|
142
|
+
* equality operator, so it can be neither compared nor used with `distinct`.
|
|
5
143
|
*
|
|
6
144
|
* @param name Column name in database
|
|
7
145
|
* @returns Serializable column definition for a JSON column.
|
|
8
146
|
*/
|
|
9
147
|
export function json(name) {
|
|
10
|
-
return new
|
|
148
|
+
return new JsonColumnDefinition(name, {
|
|
11
149
|
dataType: "json",
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
150
|
+
runtimeType: "json",
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Defines a `jsonb` column: a JSON value stored parsed, which Postgres can compare.
|
|
155
|
+
*
|
|
156
|
+
* In Aurora DSQL a `jsonb` value is limited to 1 MiB compressed, and a `jsonb` column cannot be
|
|
157
|
+
* indexed.
|
|
158
|
+
*
|
|
159
|
+
* @param name Column name in database
|
|
160
|
+
* @returns Serializable column definition for a JSONB column.
|
|
161
|
+
*/
|
|
162
|
+
export function jsonb(name) {
|
|
163
|
+
return new JsonColumnDefinition(name, {
|
|
164
|
+
dataType: "jsonb",
|
|
165
|
+
runtimeType: "jsonb",
|
|
16
166
|
});
|
|
17
167
|
}
|
|
18
168
|
//# sourceMappingURL=json.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.js","sourceRoot":"","sources":["../../../src/schema/columns/json.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD;;;;;GAKG;AAEH,MAAM,UAAU,IAAI,CAA6B,IAAW;IAC1D,OAAO,IAAI,gBAAgB,CAAuC,IAAI,EAAE;QACtE,QAAQ,EAAE,MAAM;QAChB,KAAK,EAAE;YACL,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC;YACxC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,aAAa,CAAC,KAAK,CAAC;SACxC;KACF,CAAC,CAAC;AACL,CAAC"}
|
|
1
|
+
{"version":3,"file":"json.js","sourceRoot":"","sources":["../../../src/schema/columns/json.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,gBAAgB,EAEhB,QAAQ,GACT,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AA4BtE;;;;;;GAMG;AACH,MAAM,OAAO,oBAGX,SAAQ,gB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O,SAAS,KAAK,MAAM,CAAC,SAAS,IAAI,SAAS,KAAK,IAAI,CAAC;AAC9D,CAAC;AAED,SAAS,MAAM,CAAC,KAAc,EAAE,MAAc;IAC5C,IAAI,IAAwB,CAAC;IAE7B,IAAI,CAAC;QACH,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;IAAC,MAAM,CAAC;QACP,IAAI,GAAG,SAAS,CAAC;IACnB,CAAC;IAED,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACvB,MAAM,IAAI,qBAAqB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/D,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,IAAI,CAA6B,IAAW;IAC1D,OAAO,IAAI,oBAAoB,CAAgD,IAAI,EAAE;QACnF,QAAQ,EAAE,MAAM;QAChB,WAAW,EAAE,MAAM;KACpB,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,KAAK,CAA6B,IAAW;IAC3D,OAAO,IAAI,oBAAoB,CAAiD,IAAI,EAAE;QACpF,QAAQ,EAAE,OAAO;QACjB,WAAW,EAAE,OAAO;KACrB,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n ColumnCodec,\n ColumnConfig,\n ColumnDefinition,\n ColumnValidator,\n SQLParam,\n} from \"@dsqlbase/core\";\nimport { HasDefault, TypedObject } from \"@dsqlbase/core/utils\";\nimport { ColumnValidationError } from \"../utils/column-validation.js\";\nimport type {\n InferSchemaInput,\n InferSchemaOutput,\n StandardSchemaResult,\n StandardSchemaV1,\n} from \"../utils/standard-schema.js\";\n\n/** A JSON column's types once `.schema()` sets them: writes take its input, reads its output. */\nexport type WithSchema<T extends TypedObject, S extends StandardSchemaV1> = T & {\n __type: { valueType: InferSchemaOutput<S>; inputType: InferSchemaInput<S> };\n};\n\ntype JsonColumnConfig = ColumnConfig<unknown, unknown, \"json\" | \"jsonb\" | \"array\" | \"object\">;\n\n/** The top-level shape `array()` and `record()` hold, checked on every write and every read. */\nexport type JsonShape = \"array\" | \"object\";\n\n/**\n * The schemas a JSON column takes: any, or on `array()` / `record()` one whose output is an array\n * / an object.\n */\nexport type JsonSchemaFor<T extends TypedObject> = T[\"__type\"] extends { runtimeType: \"array\" }\n ? StandardSchemaV1<unknown, readonly unknown[]>\n : T[\"__type\"] extends { runtimeType: \"object\" }\n ? StandardSchemaV1<unknown, object>\n : StandardSchemaV1;\n\n/**\n * A `json` or `jsonb` column. A value may be any JSON value — object, array, string, number or\n * boolean — and `null` is SQL `NULL`, never a JSON `null`.\n *\n * The driver returns these columns parsed, so a read is the value as stored. With `.schema()`,\n * every write and every read is validated.\n */\nexport class JsonColumnDefinition<\n TName extends string,\n TConfig extends JsonColumnConfig,\n> extends ColumnDefinition<TName, TConfig> {\n /** The value `.default()` was given, kept so a later `.schema()` can validate it. */\n protected _defaultInput?: { value: unknown };\n /** The shape every value must have, if the column constrains it. */\n protected _shape?: JsonShape;\n\n constructor(name: TName, config: Partial<TConfig> = {}, shape?: JsonShape) {\n super(name, { ...config, codec: plainJsonCodec } as Partial<TConfig>);\n\n if (shape) {\n this._shape = shape;\n this._validator = shapeValidator(name, shape) as typeof this._validator;\n }\n }\n\n /**\n * Validates the column with a Standard Schema (zod, valibot, arktype, …), which also types\n * it: a write takes the schema's input, a read returns its output.\n *\n * - **A write** validates the value and stores the schema's **output** — defaults filled,\n * coercions applied — in its JSON form. It then checks that output validates back to\n * itself, so a read returns exactly what was stored. A schema that transforms values fails\n * that check, on the first write that reaches the transform.\n * - **A read** validates the stored value and returns the schema's output, so the read type\n * holds for every row. A row that fails — written by raw SQL, or under an older schema —\n * fails the whole read.\n *\n * Failures throw {@link ColumnValidationError}. The schema must validate synchronously.\n *\n * Filters are not validated: a filter value is compared with stored values, and may be only a\n * fragment of one.\n *\n * @example\n * ```ts\n * const Settings = z.object({ theme: z.enum([\"light\", \"dark\"]).default(\"light\") });\n *\n * settings: jsonb(\"settings\").schema(Settings)\n * // create({ data: { settings: {} } }) stores {\"theme\":\"light\"}\n * ```\n */\n public schema<S extends JsonSchemaFor<this>>(schema: S): WithSchema<this, S> {\n const validator = schemaValidator(this.name, schema);\n\n this._validator = (\n this._shape ? shapeValidator(this.name, this._shape, validator) : validator\n ) as typeof this._validator;\n\n if (this._defaultInput) {\n this.default(this._defaultInput.value);\n }\n\n return this as WithSchema<this, S>;\n }\n\n /** Encoded now, so a default the column's schema refuses fails where it is declared. */\n public override default(value: this[\"__type\"][\"inputType\"]): HasDefault<this> {\n this._defaultInput = { value };\n this._defaultValue = new SQLParam(this._codec.encode(this._toValue(value)));\n\n return this as HasDefault<this>;\n }\n}\n\nconst plainJsonCodec: ColumnCodec<unknown, unknown> = {\n encode: (value) => JSON.stringify(value),\n decode: (value) => value,\n};\n\n/**\n * Validates writes and reads against `schema`. A write returns the schema's output once it is\n * known to read back as itself: serialized, validated again and serialized to the same text.\n */\nfunction schemaValidator(\n column: string,\n schema: StandardSchemaV1\n): ColumnValidator<unknown, unknown> {\n const validate = (value: unknown, phase: \"write\" | \"read\") => {\n const result = schema[\"~standard\"].validate(value);\n\n if (result instanceof Promise) {\n // The pending validation is abandoned; keep its eventual rejection from going unhandled.\n result.catch(() => undefined);\n throw new ColumnValidationError(\"async\", column, phase);\n }\n\n const settled = result as StandardSchemaResult<unknown>;\n\n if (settled.issues) {\n throw new ColumnValidationError(\"invalid\", column, phase, settled.issues);\n }\n\n return settled.value;\n };\n\n return {\n write(input) {\n const output = validate(input, \"write\");\n const text = toJson(output, column);\n const again = toJson(validate(JSON.parse(text), \"write\"), column);\n\n if (again !== text) {\n throw new ColumnValidationError(\"unstable\", column, \"write\");\n }\n\n return output;\n },\n read(stored) {\n return validate(stored, \"read\");\n },\n };\n}\n\n/**\n * Checks that every value is an array, or a plain object, at its top level: a write's value as it\n * will be stored (after `inner`, the schema), a read's before `inner` sees it.\n */\nfunction shapeValidator(\n column: string,\n shape: JsonShape,\n inner?: ColumnValidator<unknown, unknown>\n): ColumnValidator<unknown, unknown> {\n const check = (value: unknown, phase: \"write\" | \"read\") => {\n if (shape === \"array\" ? !Array.isArray(value) : !isPlainObject(value)) {\n throw new ColumnValidationError(\"invalid\", column, phase, [\n { message: shape === \"array\" ? \"Expected an array\" : \"Expected an object\" },\n ]);\n }\n\n return value;\n };\n\n return {\n write: (input) => check(inner ? inner.write(input) : input, \"write\"),\n read: (stored) => {\n check(stored, \"read\");\n return inner ? inner.read(stored) : stored;\n },\n };\n}\n\nfunction isPlainObject(value: unknown): boolean {\n if (typeof value !== \"object\" || value === null) {\n return false;\n }\n\n const prototype = Object.getPrototypeOf(value) as unknown;\n return prototype === Object.prototype || prototype === null;\n}\n\nfunction toJson(value: unknown, column: string): string {\n let text: string | undefined;\n\n try {\n text = JSON.stringify(value);\n } catch {\n text = undefined;\n }\n\n if (text === undefined) {\n throw new ColumnValidationError(\"not_json\", column, \"write\");\n }\n\n return text;\n}\n\n/**\n * Defines a `json` column. Prefer {@link jsonb}: `json` stores the text as given and has no\n * equality operator, so it can be neither compared nor used with `distinct`.\n *\n * @param name Column name in database\n * @returns Serializable column definition for a JSON column.\n */\nexport function json<const TName extends string>(name: TName) {\n return new JsonColumnDefinition<TName, ColumnConfig<unknown, unknown, \"json\">>(name, {\n dataType: \"json\",\n runtimeType: \"json\",\n });\n}\n\n/**\n * Defines a `jsonb` column: a JSON value stored parsed, which Postgres can compare.\n *\n * In Aurora DSQL a `jsonb` value is limited to 1 MiB compressed, and a `jsonb` column cannot be\n * indexed.\n *\n * @param name Column name in database\n * @returns Serializable column definition for a JSONB column.\n */\nexport function jsonb<const TName extends string>(name: TName) {\n return new JsonColumnDefinition<TName, ColumnConfig<unknown, unknown, \"jsonb\">>(name, {\n dataType: \"jsonb\",\n runtimeType: \"jsonb\",\n });\n}\n"]}
|
|
@@ -1,11 +1,28 @@
|
|
|
1
1
|
import { ColumnConfig, ColumnDefinition } from "@dsqlbase/core";
|
|
2
|
+
export type NumericOptions = {
|
|
3
|
+
/** Total significant digits, 1–1000. @default 18 */
|
|
4
|
+
precision?: number;
|
|
5
|
+
/** Digits after the decimal point, 0–`precision`. @default 6, or 0 when `precision` is given */
|
|
6
|
+
scale?: number;
|
|
7
|
+
};
|
|
2
8
|
/**
|
|
3
|
-
*
|
|
9
|
+
* DSQL's own precision and scale for a `numeric` declared without them, stated explicitly so
|
|
10
|
+
* Postgres and PGlite store the same type.
|
|
11
|
+
*/
|
|
12
|
+
export declare const DEFAULT_NUMERIC_PRECISION = 18;
|
|
13
|
+
export declare const DEFAULT_NUMERIC_SCALE = 6;
|
|
14
|
+
/**
|
|
15
|
+
* Defines a `numeric(precision, scale)` (exact decimal) column in the database schema.
|
|
16
|
+
*
|
|
17
|
+
* Without options the column is `numeric(18,6)`, which is what DSQL makes of a bare `numeric`:
|
|
18
|
+
* at most 12 integer digits and 6 decimals. A write that would not fit — more decimals than the
|
|
19
|
+
* scale, which the database would round, or more integer digits than `precision - scale`, which
|
|
20
|
+
* it would refuse — throws a {@link ColumnValidationError} instead.
|
|
4
21
|
*
|
|
5
|
-
* - Precision: Exact numeric of selectable precision. The maximum precision is 38 and the maximum scale is 37.
|
|
6
22
|
* @param name Column name in database
|
|
23
|
+
* @param options Precision and scale; `{ precision }` alone means a scale of 0, as `numeric(p)`
|
|
7
24
|
* @returns Serializable column definition for a numeric column.
|
|
8
25
|
*/
|
|
9
|
-
export declare function numeric<const TName extends string>(name: TName): ColumnDefinition<TName, ColumnConfig<number, string>>;
|
|
26
|
+
export declare function numeric<const TName extends string>(name: TName, options?: NumericOptions): ColumnDefinition<TName, ColumnConfig<number, string, "number">>;
|
|
10
27
|
export { numeric as decimal };
|
|
11
28
|
//# 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;
|
|
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;CAChB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,yBAAyB,KAAK,CAAC;AAC5C,eAAO,MAAM,qBAAqB,IAAI,CAAC;AAEvC;;;;;;;;;;;GAWG;AACH,wBAAgB,OAAO,CAAC,KAAK,CAAC,KAAK,SAAS,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,GAAE,cAAmB,mEA4C5F;AA4BD,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,CAAC"}
|