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,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global ids: the opaque form a row's primary key takes when it leaves the ORM.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately **not** in `@dsqlbase/core`. Nothing below the client has an opinion about the
|
|
5
|
+
* shape of an id — core sees a `uuid` column with a codec, exactly as it sees an `interval`
|
|
6
|
+
* column with a `Duration` codec (`./duration.ts`). The wrapping is a client concern, so it
|
|
7
|
+
* lives here with the rest of the codec helpers.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* A global id could not be read, or does not point where it was used.
|
|
11
|
+
*
|
|
12
|
+
* `key_mismatch` is the one that earns the format its keep: a wrapped `workspaces` id handed
|
|
13
|
+
* to a `users.id` filter is caught here, where a bare uuid would simply have matched nothing.
|
|
14
|
+
*/
|
|
15
|
+
export class GlobalIdError extends Error {
|
|
16
|
+
code;
|
|
17
|
+
constructor(code, message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "GlobalIdError";
|
|
20
|
+
this.code = code;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The prefix every global id carries.
|
|
25
|
+
*
|
|
26
|
+
* It is what makes "is this already wrapped?" an exact question rather than a heuristic: a
|
|
27
|
+
* uuid never starts with `guid:`, so a column can accept both forms without guessing.
|
|
28
|
+
*/
|
|
29
|
+
export const GLOBAL_ID_PREFIX = "guid:";
|
|
30
|
+
/**
|
|
31
|
+
* Wraps a primary key as an opaque global id.
|
|
32
|
+
*
|
|
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 the
|
|
35
|
+
* format never has to change to carry a composite key — today nothing produces one, because
|
|
36
|
+
* only a single-column `guid()` key can be declared.
|
|
37
|
+
*
|
|
38
|
+
* Both the table key and the pk field names are **aliases**: the names the client addresses
|
|
39
|
+
* (`members`, not `team_members`), so an id follows the client-visible identity rather than
|
|
40
|
+
* the physical one and a database rename does not invalidate ids in the wild.
|
|
41
|
+
*/
|
|
42
|
+
export function encodeGlobalId(key, pk) {
|
|
43
|
+
const payload = JSON.stringify([key, pk]);
|
|
44
|
+
return `${GLOBAL_ID_PREFIX}${Buffer.from(payload, "utf8").toString("base64url")}`;
|
|
45
|
+
}
|
|
46
|
+
/** Whether a value is a global id rather than a raw column value. */
|
|
47
|
+
export function isGlobalId(value) {
|
|
48
|
+
return typeof value === "string" && value.startsWith(GLOBAL_ID_PREFIX);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Reads a global id back into its table key and primary key.
|
|
52
|
+
*
|
|
53
|
+
* `expectedKey` is the wrong-table safety net: pass it wherever the caller already knows which
|
|
54
|
+
* table the id has to name — a `guid` column's own key, say — and a wrapped id from another
|
|
55
|
+
* table throws instead of quietly matching nothing.
|
|
56
|
+
*
|
|
57
|
+
* The payload is never trusted. Whether the decoded fields are actually that table's primary
|
|
58
|
+
* key is a separate question, answered against the registry by the caller.
|
|
59
|
+
*/
|
|
60
|
+
export function decodeGlobalId(value, expectedKey) {
|
|
61
|
+
if (!isGlobalId(value)) {
|
|
62
|
+
throw new GlobalIdError("format", `"${value}" is not a global id: it has no "${GLOBAL_ID_PREFIX}" prefix.`);
|
|
63
|
+
}
|
|
64
|
+
const payload = decodePayload(value);
|
|
65
|
+
if (!Array.isArray(payload) || payload.length !== 2) {
|
|
66
|
+
throw new GlobalIdError("format", `"${value}" does not carry a [key, pk] payload.`);
|
|
67
|
+
}
|
|
68
|
+
const [key, pk] = payload;
|
|
69
|
+
if (typeof key !== "string" || key.length === 0) {
|
|
70
|
+
throw new GlobalIdError("format", `"${value}" does not name a table.`);
|
|
71
|
+
}
|
|
72
|
+
if (!isPlainRecord(pk) || Object.keys(pk).length === 0) {
|
|
73
|
+
throw new GlobalIdError("format", `"${value}" does not carry a primary key.`);
|
|
74
|
+
}
|
|
75
|
+
for (const [field, fieldValue] of Object.entries(pk)) {
|
|
76
|
+
if (typeof fieldValue !== "string") {
|
|
77
|
+
throw new GlobalIdError("format", `"${value}" carries a non-string value for key field "${field}".`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (expectedKey !== undefined && key !== expectedKey) {
|
|
81
|
+
throw new GlobalIdError("key_mismatch", `Global id names "${key}", but "${expectedKey}" was expected here.`);
|
|
82
|
+
}
|
|
83
|
+
return { key, pk: pk };
|
|
84
|
+
}
|
|
85
|
+
function decodePayload(value) {
|
|
86
|
+
// `Buffer.from(..., "base64url")` never throws — it drops whatever it cannot read — so
|
|
87
|
+
// malformed base64 surfaces here as JSON that does not parse rather than as a decode error.
|
|
88
|
+
const json = Buffer.from(value.slice(GLOBAL_ID_PREFIX.length), "base64url").toString("utf8");
|
|
89
|
+
try {
|
|
90
|
+
return JSON.parse(json);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
throw new GlobalIdError("format", `"${value}" does not decode to a JSON payload.`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function isPlainRecord(value) {
|
|
97
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=global-id.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"global-id.js","sourceRoot":"","sources":["../../../src/schema/utils/global-id.ts"],"names":[],"mappings":"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","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 the\n * format never has to change to carry a composite key — today nothing produces one, because\n * only a single-column `guid()` key can be declared.\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"]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parts of Standard Schema v1 (https://standardschema.dev) a JSON column uses, copied in as
|
|
3
|
+
* the spec invites, so zod, valibot, arktype and the rest work with no dependency on any of them.
|
|
4
|
+
*/
|
|
5
|
+
export type StandardSchemaV1<Input = unknown, Output = Input> = {
|
|
6
|
+
readonly "~standard": {
|
|
7
|
+
readonly version: 1;
|
|
8
|
+
readonly vendor: string;
|
|
9
|
+
readonly validate: (value: unknown) => StandardSchemaResult<Output> | Promise<StandardSchemaResult<Output>>;
|
|
10
|
+
readonly types?: {
|
|
11
|
+
readonly input: Input;
|
|
12
|
+
readonly output: Output;
|
|
13
|
+
} | undefined;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export type StandardSchemaResult<Output> = {
|
|
17
|
+
readonly value: Output;
|
|
18
|
+
readonly issues?: undefined;
|
|
19
|
+
} | {
|
|
20
|
+
readonly issues: readonly StandardSchemaIssue[];
|
|
21
|
+
};
|
|
22
|
+
export type StandardSchemaIssue = {
|
|
23
|
+
readonly message: string;
|
|
24
|
+
readonly path?: readonly (PropertyKey | {
|
|
25
|
+
readonly key: PropertyKey;
|
|
26
|
+
})[] | undefined;
|
|
27
|
+
};
|
|
28
|
+
export type InferSchemaInput<S extends StandardSchemaV1> = NonNullable<S["~standard"]["types"]>["input"];
|
|
29
|
+
export type InferSchemaOutput<S extends StandardSchemaV1> = NonNullable<S["~standard"]["types"]>["output"];
|
|
30
|
+
//# sourceMappingURL=standard-schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard-schema.d.ts","sourceRoot":"","sources":["../../../src/schema/utils/standard-schema.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,gBAAgB,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK,IAAI;IAC9D,QAAQ,CAAC,WAAW,EAAE;QACpB,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;QACpB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;QACxB,QAAQ,CAAC,QAAQ,EAAE,CACjB,KAAK,EAAE,OAAO,KACX,oBAAoB,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC,CAAC;QAC1E,QAAQ,CAAC,KAAK,CAAC,EAAE;YAAE,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;YAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;SAAE,GAAG,SAAS,CAAC;KACjF,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,oBAAoB,CAAC,MAAM,IACnC;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,CAAA;CAAE,GACvD;IAAE,QAAQ,CAAC,MAAM,EAAE,SAAS,mBAAmB,EAAE,CAAA;CAAE,CAAC;AAExD,MAAM,MAAM,mBAAmB,GAAG;IAChC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,CAAC,WAAW,GAAG;QAAE,QAAQ,CAAC,GAAG,EAAE,WAAW,CAAA;KAAE,CAAC,EAAE,GAAG,SAAS,CAAC;CACtF,CAAC;AAEF,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,gBAAgB,IAAI,WAAW,CACpE,CAAC,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CACxB,CAAC,OAAO,CAAC,CAAC;AAEX,MAAM,MAAM,iBAAiB,CAAC,CAAC,SAAS,gBAAgB,IAAI,WAAW,CACrE,CAAC,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CACxB,CAAC,QAAQ,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard-schema.js","sourceRoot":"","sources":["../../../src/schema/utils/standard-schema.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * The parts of Standard Schema v1 (https://standardschema.dev) a JSON column uses, copied in as\n * the spec invites, so zod, valibot, arktype and the rest work with no dependency on any of them.\n */\nexport type StandardSchemaV1<Input = unknown, Output = Input> = {\n readonly \"~standard\": {\n readonly version: 1;\n readonly vendor: string;\n readonly validate: (\n value: unknown\n ) => StandardSchemaResult<Output> | Promise<StandardSchemaResult<Output>>;\n readonly types?: { readonly input: Input; readonly output: Output } | undefined;\n };\n};\n\nexport type StandardSchemaResult<Output> =\n | { readonly value: Output; readonly issues?: undefined }\n | { readonly issues: readonly StandardSchemaIssue[] };\n\nexport type StandardSchemaIssue = {\n readonly message: string;\n readonly path?: readonly (PropertyKey | { readonly key: PropertyKey })[] | undefined;\n};\n\nexport type InferSchemaInput<S extends StandardSchemaV1> = NonNullable<\n S[\"~standard\"][\"types\"]\n>[\"input\"];\n\nexport type InferSchemaOutput<S extends StandardSchemaV1> = NonNullable<\n S[\"~standard\"][\"types\"]\n>[\"output\"];\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsqlbase",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Query client and schema definition for distributed SQL databases",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"type": "module",
|
|
@@ -18,7 +18,10 @@
|
|
|
18
18
|
"publishConfig": {
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
|
-
"
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=22"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/slsdotdev/dsqlbase#readme",
|
|
22
25
|
"repository": {
|
|
23
26
|
"type": "git",
|
|
24
27
|
"url": "git+https://github.com/slsdotdev/dsqlbase.git",
|
|
@@ -31,39 +34,41 @@
|
|
|
31
34
|
"dist",
|
|
32
35
|
"README.md",
|
|
33
36
|
"CHANGELOG.md",
|
|
34
|
-
"
|
|
37
|
+
"LICENSE"
|
|
35
38
|
],
|
|
36
39
|
"main": "dist/index.js",
|
|
37
40
|
"types": "dist/index.d.ts",
|
|
38
41
|
"exports": {
|
|
39
42
|
".": {
|
|
40
|
-
"
|
|
41
|
-
"
|
|
43
|
+
"types": "./dist/index.d.ts",
|
|
44
|
+
"default": "./dist/index.js"
|
|
42
45
|
},
|
|
43
46
|
"./client": {
|
|
44
|
-
"
|
|
45
|
-
"
|
|
47
|
+
"types": "./dist/client/index.d.ts",
|
|
48
|
+
"default": "./dist/client/index.js"
|
|
46
49
|
},
|
|
47
50
|
"./pg": {
|
|
48
|
-
"
|
|
49
|
-
"
|
|
51
|
+
"types": "./dist/pg/index.d.ts",
|
|
52
|
+
"default": "./dist/pg/index.js"
|
|
50
53
|
},
|
|
51
54
|
"./pglite": {
|
|
52
|
-
"
|
|
53
|
-
"
|
|
55
|
+
"types": "./dist/pglite/index.d.ts",
|
|
56
|
+
"default": "./dist/pglite/index.js"
|
|
54
57
|
},
|
|
55
58
|
"./schema": {
|
|
56
|
-
"
|
|
57
|
-
"
|
|
59
|
+
"types": "./dist/schema/index.d.ts",
|
|
60
|
+
"default": "./dist/schema/index.js"
|
|
58
61
|
}
|
|
59
62
|
},
|
|
60
63
|
"scripts": {
|
|
64
|
+
"prebuild": "node -e \"const fs=require('fs');fs.rmSync('dist',{recursive:true,force:true});fs.rmSync('tsconfig.tsbuildinfo',{force:true})\"",
|
|
61
65
|
"build": "tsc -b",
|
|
62
66
|
"test": "vitest run",
|
|
63
|
-
"lint": "eslint src --ext .ts"
|
|
67
|
+
"lint": "eslint src --ext .ts",
|
|
68
|
+
"prepack": "node -e \"require('fs').copyFileSync('../../LICENSE','LICENSE')\""
|
|
64
69
|
},
|
|
65
70
|
"dependencies": {
|
|
66
|
-
"@dsqlbase/core": "^0.
|
|
71
|
+
"@dsqlbase/core": "^0.2.0"
|
|
67
72
|
},
|
|
68
73
|
"peerDependencies": {
|
|
69
74
|
"@electric-sql/pglite": "^0.4.5",
|
|
@@ -78,6 +83,6 @@
|
|
|
78
83
|
}
|
|
79
84
|
},
|
|
80
85
|
"devDependencies": {
|
|
81
|
-
"@types/pg": "^8.
|
|
86
|
+
"@types/pg": "^8.23.1"
|
|
82
87
|
}
|
|
83
88
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../../src/schema/utils/json.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,aAAa,GAAI,OAAO,OAAO,QAU3C,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"json.js","sourceRoot":"","sources":["../../../src/schema/utils/json.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,KAAc,EAAE,EAAE;IAC9C,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC3B,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC,CAAC"}
|