durcno 1.0.0-alpha.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/LICENSE +201 -0
- package/README.md +56 -0
- package/dist/bin.cjs +13522 -0
- package/dist/src/_virtual/_rolldown/runtime.mjs +28 -0
- package/dist/src/cli/helpers.mjs +16 -0
- package/dist/src/columns/bigint.d.mts +19 -0
- package/dist/src/columns/bigint.mjs +32 -0
- package/dist/src/columns/bigserial.d.mts +24 -0
- package/dist/src/columns/bigserial.mjs +35 -0
- package/dist/src/columns/boolean.d.mts +19 -0
- package/dist/src/columns/boolean.mjs +31 -0
- package/dist/src/columns/bytea.d.mts +19 -0
- package/dist/src/columns/bytea.mjs +33 -0
- package/dist/src/columns/char.d.mts +32 -0
- package/dist/src/columns/char.mjs +44 -0
- package/dist/src/columns/cidr.d.mts +19 -0
- package/dist/src/columns/cidr.mjs +31 -0
- package/dist/src/columns/common.d.mts +233 -0
- package/dist/src/columns/common.mjs +276 -0
- package/dist/src/columns/date.d.mts +19 -0
- package/dist/src/columns/date.mjs +32 -0
- package/dist/src/columns/enum.d.mts +21 -0
- package/dist/src/columns/enum.mjs +35 -0
- package/dist/src/columns/inet.d.mts +19 -0
- package/dist/src/columns/inet.mjs +36 -0
- package/dist/src/columns/integer.d.mts +19 -0
- package/dist/src/columns/integer.mjs +32 -0
- package/dist/src/columns/json.d.mts +52 -0
- package/dist/src/columns/json.mjs +67 -0
- package/dist/src/columns/jsonb.d.mts +53 -0
- package/dist/src/columns/jsonb.mjs +68 -0
- package/dist/src/columns/macaddr.d.mts +19 -0
- package/dist/src/columns/macaddr.mjs +31 -0
- package/dist/src/columns/numeric.d.mts +42 -0
- package/dist/src/columns/numeric.mjs +57 -0
- package/dist/src/columns/postgis/geography/index.d.mts +18 -0
- package/dist/src/columns/postgis/geography/index.mjs +17 -0
- package/dist/src/columns/postgis/geography/linestring.d.mts +38 -0
- package/dist/src/columns/postgis/geography/linestring.mjs +70 -0
- package/dist/src/columns/postgis/geography/multilinestring.d.mts +39 -0
- package/dist/src/columns/postgis/geography/multilinestring.mjs +70 -0
- package/dist/src/columns/postgis/geography/multipoint.d.mts +38 -0
- package/dist/src/columns/postgis/geography/multipoint.mjs +70 -0
- package/dist/src/columns/postgis/geography/multipolygon.d.mts +40 -0
- package/dist/src/columns/postgis/geography/multipolygon.mjs +70 -0
- package/dist/src/columns/postgis/geography/point.d.mts +36 -0
- package/dist/src/columns/postgis/geography/point.mjs +69 -0
- package/dist/src/columns/postgis/geography/polygon.d.mts +39 -0
- package/dist/src/columns/postgis/geography/polygon.mjs +70 -0
- package/dist/src/columns/serial.d.mts +24 -0
- package/dist/src/columns/serial.mjs +35 -0
- package/dist/src/columns/smallint.d.mts +19 -0
- package/dist/src/columns/smallint.mjs +32 -0
- package/dist/src/columns/smallserial.d.mts +24 -0
- package/dist/src/columns/smallserial.mjs +35 -0
- package/dist/src/columns/text.d.mts +19 -0
- package/dist/src/columns/text.mjs +30 -0
- package/dist/src/columns/time.d.mts +44 -0
- package/dist/src/columns/time.mjs +47 -0
- package/dist/src/columns/timestamp.d.mts +44 -0
- package/dist/src/columns/timestamp.mjs +47 -0
- package/dist/src/columns/uuid.d.mts +33 -0
- package/dist/src/columns/uuid.mjs +44 -0
- package/dist/src/columns/varchar.d.mts +32 -0
- package/dist/src/columns/varchar.mjs +44 -0
- package/dist/src/connectors/bun.d.mts +18 -0
- package/dist/src/connectors/bun.mjs +97 -0
- package/dist/src/connectors/common.d.mts +148 -0
- package/dist/src/connectors/common.mjs +65 -0
- package/dist/src/connectors/pg.d.mts +18 -0
- package/dist/src/connectors/pg.mjs +103 -0
- package/dist/src/connectors/pglite.d.mts +19 -0
- package/dist/src/connectors/pglite.mjs +94 -0
- package/dist/src/connectors/postgres.d.mts +18 -0
- package/dist/src/connectors/postgres.mjs +93 -0
- package/dist/src/constraints/check.d.mts +88 -0
- package/dist/src/constraints/check.mjs +307 -0
- package/dist/src/constraints/primary-key.d.mts +28 -0
- package/dist/src/constraints/primary-key.mjs +36 -0
- package/dist/src/constraints/unique.d.mts +28 -0
- package/dist/src/constraints/unique.mjs +36 -0
- package/dist/src/db.d.mts +180 -0
- package/dist/src/db.mjs +191 -0
- package/dist/src/entity.mjs +23 -0
- package/dist/src/enumtype.d.mts +27 -0
- package/dist/src/enumtype.mjs +42 -0
- package/dist/src/filters/array.d.mts +97 -0
- package/dist/src/filters/array.mjs +150 -0
- package/dist/src/filters/custom.d.mts +12 -0
- package/dist/src/filters/custom.mjs +7 -0
- package/dist/src/filters/index.d.mts +155 -0
- package/dist/src/filters/index.mjs +318 -0
- package/dist/src/functions/index.d.mts +24 -0
- package/dist/src/functions/index.mjs +29 -0
- package/dist/src/index.d.mts +129 -0
- package/dist/src/index.mjs +78 -0
- package/dist/src/indexes.d.mts +31 -0
- package/dist/src/indexes.mjs +43 -0
- package/dist/src/migration/consts.d.mts +8 -0
- package/dist/src/migration/consts.mjs +8 -0
- package/dist/src/migration/ddl.d.mts +747 -0
- package/dist/src/migration/ddl.mjs +1084 -0
- package/dist/src/migration/index.d.mts +15 -0
- package/dist/src/migration/index.mjs +5 -0
- package/dist/src/migration/snapshot.d.mts +169 -0
- package/dist/src/migration/snapshot.mjs +123 -0
- package/dist/src/migration/statement.d.mts +89 -0
- package/dist/src/migration/statement.mjs +61 -0
- package/dist/src/models.d.mts +34 -0
- package/dist/src/models.mjs +27 -0
- package/dist/src/node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/index.mjs +126 -0
- package/dist/src/node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/utilities.mjs +29 -0
- package/dist/src/node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/vendor/ansi-styles/index.mjs +165 -0
- package/dist/src/node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/vendor/supports-color/index.mjs +92 -0
- package/dist/src/query-builders/aggregates.d.mts +20 -0
- package/dist/src/query-builders/aggregates.mjs +43 -0
- package/dist/src/query-builders/count.d.mts +19 -0
- package/dist/src/query-builders/count.mjs +36 -0
- package/dist/src/query-builders/delete.d.mts +19 -0
- package/dist/src/query-builders/delete.mjs +57 -0
- package/dist/src/query-builders/distinct.d.mts +19 -0
- package/dist/src/query-builders/distinct.mjs +42 -0
- package/dist/src/query-builders/exists.d.mts +19 -0
- package/dist/src/query-builders/exists.mjs +37 -0
- package/dist/src/query-builders/first.d.mts +17 -0
- package/dist/src/query-builders/first.mjs +46 -0
- package/dist/src/query-builders/insert-returning.d.mts +16 -0
- package/dist/src/query-builders/insert-returning.mjs +63 -0
- package/dist/src/query-builders/insert.d.mts +22 -0
- package/dist/src/query-builders/insert.mjs +93 -0
- package/dist/src/query-builders/orderby-clause.d.mts +14 -0
- package/dist/src/query-builders/orderby-clause.mjs +20 -0
- package/dist/src/query-builders/pre.d.mts +36 -0
- package/dist/src/query-builders/pre.mjs +60 -0
- package/dist/src/query-builders/query-promise.d.mts +14 -0
- package/dist/src/query-builders/query-promise.mjs +21 -0
- package/dist/src/query-builders/query.d.mts +14 -0
- package/dist/src/query-builders/query.mjs +17 -0
- package/dist/src/query-builders/raw.d.mts +14 -0
- package/dist/src/query-builders/raw.mjs +26 -0
- package/dist/src/query-builders/rq.d.mts +36 -0
- package/dist/src/query-builders/rq.mjs +231 -0
- package/dist/src/query-builders/select.d.mts +48 -0
- package/dist/src/query-builders/select.mjs +118 -0
- package/dist/src/query-builders/update.d.mts +24 -0
- package/dist/src/query-builders/update.mjs +99 -0
- package/dist/src/sequence.d.mts +48 -0
- package/dist/src/sequence.mjs +53 -0
- package/dist/src/sql.d.mts +15 -0
- package/dist/src/sql.mjs +36 -0
- package/dist/src/symbols.d.mts +4 -0
- package/dist/src/symbols.mjs +4 -0
- package/dist/src/table.d.mts +144 -0
- package/dist/src/table.mjs +134 -0
- package/dist/src/types.d.mts +4 -0
- package/dist/src/utils.mjs +10 -0
- package/dist/src/validators/zod.d.mts +23 -0
- package/dist/src/validators/zod.mjs +27 -0
- package/dist/src/wkx/binaryreader.mjs +56 -0
- package/dist/src/wkx/binarywriter.mjs +110 -0
- package/dist/src/wkx/geometry.mjs +101 -0
- package/dist/src/wkx/geometrycollection.mjs +113 -0
- package/dist/src/wkx/index.mjs +18 -0
- package/dist/src/wkx/linestring.mjs +124 -0
- package/dist/src/wkx/multilinestring.mjs +133 -0
- package/dist/src/wkx/multipoint.mjs +120 -0
- package/dist/src/wkx/multipolygon.mjs +155 -0
- package/dist/src/wkx/parser.mjs +183 -0
- package/dist/src/wkx/point.mjs +159 -0
- package/dist/src/wkx/polygon.mjs +189 -0
- package/dist/src/wkx/types.mjs +37 -0
- package/dist/src/wkx/wktparser.mjs +109 -0
- package/dist/src/wkx/zigzag.mjs +18 -0
- package/package.json +100 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { $Client, $Pool, Connector } from "./common.mjs";
|
|
2
|
+
import { PGlite } from "@electric-sql/pglite";
|
|
3
|
+
//#region src/connectors/pglite.ts
|
|
4
|
+
/**
|
|
5
|
+
* Connector implementation for the `@electric-sql/pglite` library.
|
|
6
|
+
*
|
|
7
|
+
* PGlite is a lightweight, embeddable PostgreSQL implementation that runs
|
|
8
|
+
* entirely in-process using WASM. Perfect for local development, testing,
|
|
9
|
+
* and edge computing scenarios.
|
|
10
|
+
*
|
|
11
|
+
* @see https://pglite.dev/
|
|
12
|
+
* @see https://www.npmjs.com/package/@electric-sql/pglite
|
|
13
|
+
*/
|
|
14
|
+
var PgLiteConnector = class extends Connector {
|
|
15
|
+
getClient() {
|
|
16
|
+
return new PgLiteClient(this.url);
|
|
17
|
+
}
|
|
18
|
+
getPool() {
|
|
19
|
+
return new PgLitePool(this.url, this.config.pool);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Single-instance client wrapper for PGlite.
|
|
24
|
+
*
|
|
25
|
+
* Since PGlite runs in-process, this creates a single PGlite instance.
|
|
26
|
+
* Connection is established automatically during initialization.
|
|
27
|
+
*
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
var PgLiteClient = class extends $Client {
|
|
31
|
+
#client;
|
|
32
|
+
constructor(connectionString) {
|
|
33
|
+
super();
|
|
34
|
+
this.#client = new PGlite(connectionString);
|
|
35
|
+
this.query = this.#client.query.bind(this.#client);
|
|
36
|
+
}
|
|
37
|
+
async connect() {}
|
|
38
|
+
getRows(response) {
|
|
39
|
+
return response.rows;
|
|
40
|
+
}
|
|
41
|
+
async close() {
|
|
42
|
+
await this.#client.close();
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Pool-like wrapper for PGlite.
|
|
47
|
+
*
|
|
48
|
+
* Since PGlite runs in-process, pooling is not applicable in the traditional
|
|
49
|
+
* sense. This class provides API compatibility with other connectors while
|
|
50
|
+
* using a single PGlite instance internally.
|
|
51
|
+
*
|
|
52
|
+
* @internal
|
|
53
|
+
*/
|
|
54
|
+
var PgLitePool = class extends $Pool {
|
|
55
|
+
#pool;
|
|
56
|
+
constructor(connectionString, pool) {
|
|
57
|
+
super();
|
|
58
|
+
this.#pool = new PGlite(connectionString);
|
|
59
|
+
this.query = this.#pool.query.bind(this.#pool);
|
|
60
|
+
}
|
|
61
|
+
async connect() {}
|
|
62
|
+
getRows(response) {
|
|
63
|
+
return response.rows;
|
|
64
|
+
}
|
|
65
|
+
async close() {
|
|
66
|
+
await this.#pool.close();
|
|
67
|
+
}
|
|
68
|
+
async acquireClient() {
|
|
69
|
+
return new PgLitePoolClient(this.#pool);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Client wrapper for a connection acquired from a PGlite Pool.
|
|
74
|
+
*
|
|
75
|
+
* Since PGlite runs in-process, this wraps the shared PGlite instance.
|
|
76
|
+
* The close() method is a no-op to avoid closing the shared instance.
|
|
77
|
+
*
|
|
78
|
+
* @internal
|
|
79
|
+
*/
|
|
80
|
+
var PgLitePoolClient = class extends $Client {
|
|
81
|
+
#client;
|
|
82
|
+
constructor(client) {
|
|
83
|
+
super();
|
|
84
|
+
this.#client = client;
|
|
85
|
+
this.query = this.#client.query.bind(this.#client);
|
|
86
|
+
}
|
|
87
|
+
async connect() {}
|
|
88
|
+
getRows(response) {
|
|
89
|
+
return response.rows;
|
|
90
|
+
}
|
|
91
|
+
async close() {}
|
|
92
|
+
};
|
|
93
|
+
//#endregion
|
|
94
|
+
export { PgLiteConnector };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Connector } from "./common.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/connectors/postgres.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Connector implementation for the `postgres` (postgres.js) library.
|
|
6
|
+
*
|
|
7
|
+
* Postgres.js - The Fastest full featured PostgreSQL client for Node.js,
|
|
8
|
+
* Deno, Bun and CloudFlare.
|
|
9
|
+
*
|
|
10
|
+
* @see https://github.com/porsager/postgres
|
|
11
|
+
* @see https://www.npmjs.com/package/postgres
|
|
12
|
+
*/
|
|
13
|
+
declare class PostgresConnector extends Connector {
|
|
14
|
+
getClient(): PostgresClient;
|
|
15
|
+
getPool(): PostgresPool;
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
export { PostgresConnector };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { $Client, $Pool, Connector } from "./common.mjs";
|
|
2
|
+
import postgres from "postgres";
|
|
3
|
+
//#region src/connectors/postgres.ts
|
|
4
|
+
/**
|
|
5
|
+
* Connector implementation for the `postgres` (postgres.js) library.
|
|
6
|
+
*
|
|
7
|
+
* Postgres.js - The Fastest full featured PostgreSQL client for Node.js,
|
|
8
|
+
* Deno, Bun and CloudFlare.
|
|
9
|
+
*
|
|
10
|
+
* @see https://github.com/porsager/postgres
|
|
11
|
+
* @see https://www.npmjs.com/package/postgres
|
|
12
|
+
*/
|
|
13
|
+
var PostgresConnector = class extends Connector {
|
|
14
|
+
getClient() {
|
|
15
|
+
return new PostgresClient(this.url);
|
|
16
|
+
}
|
|
17
|
+
getPool() {
|
|
18
|
+
return new PostgresPool(this.url, this.config.pool);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Single-connection client wrapper for postgres.js.
|
|
23
|
+
*
|
|
24
|
+
* Configures postgres.js with `max: 1` to simulate single-connection behavior.
|
|
25
|
+
* Connection is established lazily on first query.
|
|
26
|
+
*
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
var PostgresClient = class extends $Client {
|
|
30
|
+
#sql;
|
|
31
|
+
constructor(connectionString) {
|
|
32
|
+
super();
|
|
33
|
+
this.#sql = postgres(connectionString, { max: 1 });
|
|
34
|
+
this.query = this.#sql.unsafe.bind(this.#sql);
|
|
35
|
+
}
|
|
36
|
+
async connect() {}
|
|
37
|
+
getRows(response) {
|
|
38
|
+
return response;
|
|
39
|
+
}
|
|
40
|
+
async close() {
|
|
41
|
+
await this.#sql.end();
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Connection pool wrapper for postgres.js.
|
|
46
|
+
*
|
|
47
|
+
* postgres.js has built-in connection pooling, so this wrapper configures
|
|
48
|
+
* the pool size via the `max` option. Connections are managed automatically.
|
|
49
|
+
*
|
|
50
|
+
* @internal
|
|
51
|
+
*/
|
|
52
|
+
var PostgresPool = class extends $Pool {
|
|
53
|
+
#sql;
|
|
54
|
+
constructor(connectionString, pool) {
|
|
55
|
+
super();
|
|
56
|
+
this.#sql = postgres(connectionString, { max: pool?.max ?? 10 });
|
|
57
|
+
this.query = this.#sql.unsafe.bind(this.#sql);
|
|
58
|
+
}
|
|
59
|
+
async connect() {}
|
|
60
|
+
getRows(response) {
|
|
61
|
+
return response;
|
|
62
|
+
}
|
|
63
|
+
async close() {
|
|
64
|
+
await this.#sql.end();
|
|
65
|
+
}
|
|
66
|
+
async acquireClient() {
|
|
67
|
+
return new PostgresPoolClient(await this.#sql.reserve());
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Client wrapper for a reserved connection from a postgres.js pool.
|
|
72
|
+
*
|
|
73
|
+
* This client uses a reserved connection that's exclusive until released.
|
|
74
|
+
*
|
|
75
|
+
* @internal
|
|
76
|
+
*/
|
|
77
|
+
var PostgresPoolClient = class extends $Client {
|
|
78
|
+
#sql;
|
|
79
|
+
constructor(sql) {
|
|
80
|
+
super();
|
|
81
|
+
this.#sql = sql;
|
|
82
|
+
this.query = this.#sql.unsafe.bind(this.#sql);
|
|
83
|
+
}
|
|
84
|
+
async connect() {}
|
|
85
|
+
getRows(response) {
|
|
86
|
+
return response;
|
|
87
|
+
}
|
|
88
|
+
async close() {
|
|
89
|
+
this.#sql.release();
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
//#endregion
|
|
93
|
+
export { PostgresConnector };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { AnyColumn, TableColumn } from "../table.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/constraints/check.d.ts
|
|
4
|
+
type ComparisonOp = "=" | "<>" | "<" | ">" | "<=" | ">=" | "IN" | "NOT IN";
|
|
5
|
+
type PatternOp = "LIKE" | "SIMILAR TO" | "~";
|
|
6
|
+
type LogicalOp = "AND" | "OR";
|
|
7
|
+
interface ComparisonExpr {
|
|
8
|
+
type: "comparison";
|
|
9
|
+
left: TableColumn<string, string, Key, AnyColumn> | FunctionExpr;
|
|
10
|
+
op: ComparisonOp | PatternOp;
|
|
11
|
+
right: unknown;
|
|
12
|
+
}
|
|
13
|
+
interface LogicalExpr {
|
|
14
|
+
type: "logical";
|
|
15
|
+
op: LogicalOp;
|
|
16
|
+
expressions: CheckExpr[];
|
|
17
|
+
}
|
|
18
|
+
interface FunctionExpr {
|
|
19
|
+
type: "function";
|
|
20
|
+
name: string;
|
|
21
|
+
args: (TableColumn<string, string, Key, AnyColumn> | unknown)[];
|
|
22
|
+
}
|
|
23
|
+
interface RawExpr {
|
|
24
|
+
type: "raw";
|
|
25
|
+
sql: string;
|
|
26
|
+
}
|
|
27
|
+
type CheckExpr = ComparisonExpr | LogicalExpr | FunctionExpr | RawExpr;
|
|
28
|
+
type SnapshotComparisonOp = "=" | "<>" | "<" | ">" | "<=" | ">=" | "IN" | "NOT IN";
|
|
29
|
+
type SnapshotPatternOp = "LIKE" | "SIMILAR TO" | "~";
|
|
30
|
+
type SnapshotLogicalOp = "AND" | "OR";
|
|
31
|
+
interface SnapshotComparisonExpr {
|
|
32
|
+
type: "comparison";
|
|
33
|
+
left: string | SnapshotFunctionExpr;
|
|
34
|
+
op: SnapshotComparisonOp | SnapshotPatternOp;
|
|
35
|
+
right: unknown;
|
|
36
|
+
}
|
|
37
|
+
interface SnapshotLogicalExpr {
|
|
38
|
+
type: "logical";
|
|
39
|
+
op: SnapshotLogicalOp;
|
|
40
|
+
expressions: SnapshotCheckExpr[];
|
|
41
|
+
}
|
|
42
|
+
interface SnapshotFunctionExpr {
|
|
43
|
+
type: "function";
|
|
44
|
+
name: string;
|
|
45
|
+
args: (string | unknown)[];
|
|
46
|
+
}
|
|
47
|
+
interface SnapshotRawExpr {
|
|
48
|
+
type: "raw";
|
|
49
|
+
sql: string;
|
|
50
|
+
}
|
|
51
|
+
type SnapshotCheckExpr = SnapshotComparisonExpr | SnapshotLogicalExpr | SnapshotFunctionExpr | SnapshotRawExpr;
|
|
52
|
+
declare class CheckBuilder {
|
|
53
|
+
eq<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol, value: TCol["ValType"]): ComparisonExpr;
|
|
54
|
+
neq<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol, value: TCol["ValType"]): ComparisonExpr;
|
|
55
|
+
gt<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol, value: TCol["ValType"]): ComparisonExpr;
|
|
56
|
+
gte<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol, value: TCol["ValType"]): ComparisonExpr;
|
|
57
|
+
lt<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol, value: TCol["ValType"]): ComparisonExpr;
|
|
58
|
+
lte<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol, value: TCol["ValType"]): ComparisonExpr;
|
|
59
|
+
like<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol, pattern: string): ComparisonExpr;
|
|
60
|
+
similarTo<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol, pattern: string): ComparisonExpr;
|
|
61
|
+
regex<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol, pattern: string): ComparisonExpr;
|
|
62
|
+
in<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol, values: (TCol["ValType"] & (string | number))[]): ComparisonExpr;
|
|
63
|
+
notIn<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol, values: (TCol["ValType"] & (string | number))[]): ComparisonExpr;
|
|
64
|
+
and(...expressions: CheckExpr[]): LogicalExpr;
|
|
65
|
+
or(...expressions: CheckExpr[]): LogicalExpr;
|
|
66
|
+
length<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol): FunctionExpr;
|
|
67
|
+
lower<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol): FunctionExpr;
|
|
68
|
+
upper<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol): FunctionExpr;
|
|
69
|
+
trim<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol): FunctionExpr;
|
|
70
|
+
coalesce<TCol extends TableColumn<string, string, Key, AnyColumn>>(col: TCol, defaultValue: TCol["ValType"]): FunctionExpr;
|
|
71
|
+
fnEq(fn: FunctionExpr, value: unknown): ComparisonExpr;
|
|
72
|
+
fnNeq(fn: FunctionExpr, value: unknown): ComparisonExpr;
|
|
73
|
+
fnGt(fn: FunctionExpr, value: number): ComparisonExpr;
|
|
74
|
+
fnGte(fn: FunctionExpr, value: number): ComparisonExpr;
|
|
75
|
+
fnLt(fn: FunctionExpr, value: number): ComparisonExpr;
|
|
76
|
+
fnLte(fn: FunctionExpr, value: number): ComparisonExpr;
|
|
77
|
+
raw(sql: string): RawExpr;
|
|
78
|
+
toSQL(expr: CheckExpr): string;
|
|
79
|
+
}
|
|
80
|
+
declare class Check {
|
|
81
|
+
#private;
|
|
82
|
+
constructor(name: string, expr: CheckExpr);
|
|
83
|
+
getName(): string;
|
|
84
|
+
getExpression(): string;
|
|
85
|
+
toSnapshotExpr(): SnapshotCheckExpr;
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
export { Check, CheckBuilder, CheckExpr, SnapshotCheckExpr };
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { isTableCol } from "../entity.mjs";
|
|
2
|
+
//#region src/constraints/check.ts
|
|
3
|
+
var CheckBuilder = class {
|
|
4
|
+
eq(col, value) {
|
|
5
|
+
return {
|
|
6
|
+
type: "comparison",
|
|
7
|
+
left: col,
|
|
8
|
+
op: "=",
|
|
9
|
+
right: value
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
neq(col, value) {
|
|
13
|
+
return {
|
|
14
|
+
type: "comparison",
|
|
15
|
+
left: col,
|
|
16
|
+
op: "<>",
|
|
17
|
+
right: value
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
gt(col, value) {
|
|
21
|
+
return {
|
|
22
|
+
type: "comparison",
|
|
23
|
+
left: col,
|
|
24
|
+
op: ">",
|
|
25
|
+
right: value
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
gte(col, value) {
|
|
29
|
+
return {
|
|
30
|
+
type: "comparison",
|
|
31
|
+
left: col,
|
|
32
|
+
op: ">=",
|
|
33
|
+
right: value
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
lt(col, value) {
|
|
37
|
+
return {
|
|
38
|
+
type: "comparison",
|
|
39
|
+
left: col,
|
|
40
|
+
op: "<",
|
|
41
|
+
right: value
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
lte(col, value) {
|
|
45
|
+
return {
|
|
46
|
+
type: "comparison",
|
|
47
|
+
left: col,
|
|
48
|
+
op: "<=",
|
|
49
|
+
right: value
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
like(col, pattern) {
|
|
53
|
+
return {
|
|
54
|
+
type: "comparison",
|
|
55
|
+
left: col,
|
|
56
|
+
op: "LIKE",
|
|
57
|
+
right: pattern
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
similarTo(col, pattern) {
|
|
61
|
+
return {
|
|
62
|
+
type: "comparison",
|
|
63
|
+
left: col,
|
|
64
|
+
op: "SIMILAR TO",
|
|
65
|
+
right: pattern
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
regex(col, pattern) {
|
|
69
|
+
return {
|
|
70
|
+
type: "comparison",
|
|
71
|
+
left: col,
|
|
72
|
+
op: "~",
|
|
73
|
+
right: pattern
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
in(col, values) {
|
|
77
|
+
if (!Array.isArray(values) || values.length === 0) throw new Error("IN expression requires a non-empty array of values");
|
|
78
|
+
return {
|
|
79
|
+
type: "comparison",
|
|
80
|
+
left: col,
|
|
81
|
+
op: "IN",
|
|
82
|
+
right: values
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
notIn(col, values) {
|
|
86
|
+
if (!Array.isArray(values) || values.length === 0) throw new Error("NOT IN expression requires a non-empty array of values");
|
|
87
|
+
return {
|
|
88
|
+
type: "comparison",
|
|
89
|
+
left: col,
|
|
90
|
+
op: "NOT IN",
|
|
91
|
+
right: values
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
and(...expressions) {
|
|
95
|
+
return {
|
|
96
|
+
type: "logical",
|
|
97
|
+
op: "AND",
|
|
98
|
+
expressions
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
or(...expressions) {
|
|
102
|
+
return {
|
|
103
|
+
type: "logical",
|
|
104
|
+
op: "OR",
|
|
105
|
+
expressions
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
length(col) {
|
|
109
|
+
return {
|
|
110
|
+
type: "function",
|
|
111
|
+
name: "length",
|
|
112
|
+
args: [col]
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
lower(col) {
|
|
116
|
+
return {
|
|
117
|
+
type: "function",
|
|
118
|
+
name: "lower",
|
|
119
|
+
args: [col]
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
upper(col) {
|
|
123
|
+
return {
|
|
124
|
+
type: "function",
|
|
125
|
+
name: "upper",
|
|
126
|
+
args: [col]
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
trim(col) {
|
|
130
|
+
return {
|
|
131
|
+
type: "function",
|
|
132
|
+
name: "trim",
|
|
133
|
+
args: [col]
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
coalesce(col, defaultValue) {
|
|
137
|
+
return {
|
|
138
|
+
type: "function",
|
|
139
|
+
name: "coalesce",
|
|
140
|
+
args: [col, defaultValue]
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
fnEq(fn, value) {
|
|
144
|
+
return {
|
|
145
|
+
type: "comparison",
|
|
146
|
+
left: fn,
|
|
147
|
+
op: "=",
|
|
148
|
+
right: value
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
fnNeq(fn, value) {
|
|
152
|
+
return {
|
|
153
|
+
type: "comparison",
|
|
154
|
+
left: fn,
|
|
155
|
+
op: "<>",
|
|
156
|
+
right: value
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
fnGt(fn, value) {
|
|
160
|
+
return {
|
|
161
|
+
type: "comparison",
|
|
162
|
+
left: fn,
|
|
163
|
+
op: ">",
|
|
164
|
+
right: value
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
fnGte(fn, value) {
|
|
168
|
+
return {
|
|
169
|
+
type: "comparison",
|
|
170
|
+
left: fn,
|
|
171
|
+
op: ">=",
|
|
172
|
+
right: value
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
fnLt(fn, value) {
|
|
176
|
+
return {
|
|
177
|
+
type: "comparison",
|
|
178
|
+
left: fn,
|
|
179
|
+
op: "<",
|
|
180
|
+
right: value
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
fnLte(fn, value) {
|
|
184
|
+
return {
|
|
185
|
+
type: "comparison",
|
|
186
|
+
left: fn,
|
|
187
|
+
op: "<=",
|
|
188
|
+
right: value
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
raw(sql) {
|
|
192
|
+
return {
|
|
193
|
+
type: "raw",
|
|
194
|
+
sql
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
toSQL(expr) {
|
|
198
|
+
return exprToSQL(expr);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
var Check = class {
|
|
202
|
+
#name;
|
|
203
|
+
#expr;
|
|
204
|
+
constructor(name, expr) {
|
|
205
|
+
this.#name = name;
|
|
206
|
+
this.#expr = expr;
|
|
207
|
+
}
|
|
208
|
+
getName() {
|
|
209
|
+
return this.#name.includes("check") ? this.#name : `${this.#name}_check`;
|
|
210
|
+
}
|
|
211
|
+
getExpression() {
|
|
212
|
+
return exprToSQL(this.#expr);
|
|
213
|
+
}
|
|
214
|
+
toSnapshotExpr() {
|
|
215
|
+
return exprToSnapshot(this.#expr);
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
function check(name, expr) {
|
|
219
|
+
return new Check(name, expr);
|
|
220
|
+
}
|
|
221
|
+
function formatValue(value) {
|
|
222
|
+
if (value === null) return "NULL";
|
|
223
|
+
if (typeof value === "string") return `'${value.replace(/'/g, "''")}'`;
|
|
224
|
+
if (typeof value === "number") return String(value);
|
|
225
|
+
if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
|
|
226
|
+
if (value instanceof Date) return `'${value.toISOString()}'`;
|
|
227
|
+
return String(value);
|
|
228
|
+
}
|
|
229
|
+
function exprToSQL(expr) {
|
|
230
|
+
if (expr.type === "raw") return expr.sql;
|
|
231
|
+
if (expr.type === "comparison") {
|
|
232
|
+
let left;
|
|
233
|
+
if (isTableCol(expr.left)) left = `"${expr.left.name}"`;
|
|
234
|
+
else if (expr.left.type === "function") left = exprToSQL(expr.left);
|
|
235
|
+
else left = String(expr.left);
|
|
236
|
+
if (expr.op === "IN" || expr.op === "NOT IN") {
|
|
237
|
+
if (Array.isArray(expr.right)) {
|
|
238
|
+
const list = expr.right.map((v) => isTableCol(v) ? `"${v.name}"` : formatValue(v)).join(", ");
|
|
239
|
+
return `${left} ${expr.op} (${list})`;
|
|
240
|
+
}
|
|
241
|
+
const singleRight = isTableCol(expr.right) ? `"${expr.right.name}"` : formatValue(expr.right);
|
|
242
|
+
return `${left} ${expr.op} (${singleRight})`;
|
|
243
|
+
}
|
|
244
|
+
const right = isTableCol(expr.right) ? `"${expr.right.name}"` : formatValue(expr.right);
|
|
245
|
+
return `${left} ${expr.op} ${right}`;
|
|
246
|
+
}
|
|
247
|
+
if (expr.type === "logical") return `(${expr.expressions.map((e) => exprToSQL(e)).join(` ${expr.op} `)})`;
|
|
248
|
+
if (expr.type === "function") {
|
|
249
|
+
const args = expr.args.map((arg) => isTableCol(arg) ? `"${arg.name}"` : formatValue(arg));
|
|
250
|
+
return `${expr.name}(${args.join(", ")})`;
|
|
251
|
+
}
|
|
252
|
+
throw new Error("Unknown expression type");
|
|
253
|
+
}
|
|
254
|
+
function exprToSnapshot(expr) {
|
|
255
|
+
if (expr.type === "raw") return {
|
|
256
|
+
type: "raw",
|
|
257
|
+
sql: expr.sql
|
|
258
|
+
};
|
|
259
|
+
if (expr.type === "comparison") {
|
|
260
|
+
let left;
|
|
261
|
+
if (isTableCol(expr.left)) left = expr.left.name;
|
|
262
|
+
else if (expr.left.type === "function") left = exprToSnapshot(expr.left);
|
|
263
|
+
else left = String(expr.left);
|
|
264
|
+
const right = isTableCol(expr.right) ? expr.right.name : expr.right;
|
|
265
|
+
return {
|
|
266
|
+
type: "comparison",
|
|
267
|
+
left,
|
|
268
|
+
op: expr.op,
|
|
269
|
+
right
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
if (expr.type === "logical") return {
|
|
273
|
+
type: "logical",
|
|
274
|
+
op: expr.op,
|
|
275
|
+
expressions: expr.expressions.map((e) => exprToSnapshot(e))
|
|
276
|
+
};
|
|
277
|
+
if (expr.type === "function") return {
|
|
278
|
+
type: "function",
|
|
279
|
+
name: expr.name,
|
|
280
|
+
args: expr.args.map((arg) => isTableCol(arg) ? arg.name : arg)
|
|
281
|
+
};
|
|
282
|
+
throw new Error("Unknown expression type");
|
|
283
|
+
}
|
|
284
|
+
function snapshotExprToSQL(expr) {
|
|
285
|
+
if (expr.type === "raw") return expr.sql;
|
|
286
|
+
if (expr.type === "comparison") {
|
|
287
|
+
const left = typeof expr.left === "string" ? `"${expr.left}"` : snapshotExprToSQL(expr.left);
|
|
288
|
+
const right = formatSnapshotValue(expr.right);
|
|
289
|
+
return `${left} ${expr.op} ${right}`;
|
|
290
|
+
}
|
|
291
|
+
if (expr.type === "logical") return expr.expressions.map((e) => snapshotExprToSQL(e)).map((p) => `(${p})`).join(` ${expr.op} `);
|
|
292
|
+
if (expr.type === "function") {
|
|
293
|
+
const args = expr.args.map((a) => typeof a === "string" ? `"${a}"` : formatSnapshotValue(a));
|
|
294
|
+
return `${expr.name}(${args.join(", ")})`;
|
|
295
|
+
}
|
|
296
|
+
throw new Error("Unknown expression type");
|
|
297
|
+
}
|
|
298
|
+
function formatSnapshotValue(value) {
|
|
299
|
+
if (value === null) return "NULL";
|
|
300
|
+
if (Array.isArray(value)) return `(${value.map((v) => formatSnapshotValue(v)).join(", ")})`;
|
|
301
|
+
if (typeof value === "string") return `'${value.replace(/'/g, "''")}'`;
|
|
302
|
+
if (typeof value === "number") return String(value);
|
|
303
|
+
if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
|
|
304
|
+
return String(value);
|
|
305
|
+
}
|
|
306
|
+
//#endregion
|
|
307
|
+
export { CheckBuilder, check, snapshotExprToSQL };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AnyColumn, AnyTableWithColumns, TableColumn } from "../table.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/constraints/primary-key.d.ts
|
|
4
|
+
declare class PrimaryKeyConstraint {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(name: string, columns: TableColumn<string, string, Key, AnyColumn>[]);
|
|
7
|
+
getName<TTable extends AnyTableWithColumns>(table: TTable): string;
|
|
8
|
+
getColumns(): string[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates a named table-level PRIMARY KEY constraint on two or more columns.
|
|
12
|
+
* A table can have at most one primary key constraint.
|
|
13
|
+
* For single-column primary key, use the column-level `pk()` function instead.
|
|
14
|
+
*
|
|
15
|
+
* This function is passed as the second parameter to the `primaryKeyConstraint` callback.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* table("public", "user_roles", { userId: bigint({}), roleId: bigint({}) }, {
|
|
20
|
+
* primaryKeyConstraint: (t, primaryKey) =>
|
|
21
|
+
* primaryKey("pk", [t.userId, t.roleId]),
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function primaryKeyConstraint(name: string, columns: [TableColumn<string, string, Key, AnyColumn>, TableColumn<string, string, Key, AnyColumn>, ...TableColumn<string, string, Key, AnyColumn>[]]): PrimaryKeyConstraint;
|
|
26
|
+
type PrimaryKeyConstraintFn = typeof primaryKeyConstraint;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { PrimaryKeyConstraint, PrimaryKeyConstraintFn, primaryKeyConstraint };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//#region src/constraints/primary-key.ts
|
|
2
|
+
var PrimaryKeyConstraint = class {
|
|
3
|
+
#name;
|
|
4
|
+
#columns;
|
|
5
|
+
constructor(name, columns) {
|
|
6
|
+
if (columns.length < 2) throw new Error("PRIMARY KEY constraint requires at least two columns. For single-column primary key, use the column-level 'pk()' function instead.");
|
|
7
|
+
this.#name = name;
|
|
8
|
+
this.#columns = columns;
|
|
9
|
+
}
|
|
10
|
+
getName(table) {
|
|
11
|
+
return this.#name.startsWith(`${table._.name}_`) ? this.#name : `${table._.name}_${this.#name}`;
|
|
12
|
+
}
|
|
13
|
+
getColumns() {
|
|
14
|
+
return this.#columns.map((col) => col.nameSnake);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Creates a named table-level PRIMARY KEY constraint on two or more columns.
|
|
19
|
+
* A table can have at most one primary key constraint.
|
|
20
|
+
* For single-column primary key, use the column-level `pk()` function instead.
|
|
21
|
+
*
|
|
22
|
+
* This function is passed as the second parameter to the `primaryKeyConstraint` callback.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* table("public", "user_roles", { userId: bigint({}), roleId: bigint({}) }, {
|
|
27
|
+
* primaryKeyConstraint: (t, primaryKey) =>
|
|
28
|
+
* primaryKey("pk", [t.userId, t.roleId]),
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
function primaryKeyConstraint(name, columns) {
|
|
33
|
+
return new PrimaryKeyConstraint(name, columns);
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
export { PrimaryKeyConstraint, primaryKeyConstraint };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AnyColumn, AnyTableWithColumns, TableColumn } from "../table.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/constraints/unique.d.ts
|
|
4
|
+
declare class UniqueConstraint {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(name: string, columns: TableColumn<string, string, Key, AnyColumn>[]);
|
|
7
|
+
getName<TTable extends AnyTableWithColumns>(table: TTable): string;
|
|
8
|
+
getColumns(): string[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates a named table-level UNIQUE constraint on two or more columns.
|
|
12
|
+
* For single-column unique, use the column-level `unique` modifier instead.
|
|
13
|
+
*
|
|
14
|
+
* This function is passed as the second parameter to the `uniqueConstraints` callback.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* table("public", "user_roles", { userId: bigint({}), roleId: bigint({}) }, {
|
|
19
|
+
* uniqueConstraints: (t, unique) => [
|
|
20
|
+
* unique("unique_user_role", [t.userId, t.roleId]),
|
|
21
|
+
* ],
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function uniqueConstraint(name: string, columns: [TableColumn<string, string, Key, AnyColumn>, TableColumn<string, string, Key, AnyColumn>, ...TableColumn<string, string, Key, AnyColumn>[]]): UniqueConstraint;
|
|
26
|
+
type UniqueConstraintFn = typeof uniqueConstraint;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { UniqueConstraint, UniqueConstraintFn, uniqueConstraint };
|