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,47 @@
|
|
|
1
|
+
import { Column } from "./common.mjs";
|
|
2
|
+
import { Sql } from "../sql.mjs";
|
|
3
|
+
import * as z from "zod";
|
|
4
|
+
//#region src/columns/timestamp.ts
|
|
5
|
+
var TimestampColumn = class extends Column {
|
|
6
|
+
static id = "Column.Timestamp";
|
|
7
|
+
#withTimezone;
|
|
8
|
+
#precision;
|
|
9
|
+
constructor(config) {
|
|
10
|
+
super(config);
|
|
11
|
+
this.#withTimezone = config.withTimezone ?? true;
|
|
12
|
+
this.#precision = config.precision;
|
|
13
|
+
}
|
|
14
|
+
get sqlTypeScalar() {
|
|
15
|
+
return `timestamp${this.#precision !== void 0 ? `(${this.#precision})` : ""}${this.#withTimezone ? " with time zone" : ""}`;
|
|
16
|
+
}
|
|
17
|
+
get zodTypeScaler() {
|
|
18
|
+
return z.iso.datetime({ precision: this.#precision });
|
|
19
|
+
}
|
|
20
|
+
toDriverScalar(value) {
|
|
21
|
+
if (value === null) return null;
|
|
22
|
+
return value instanceof Sql ? value.string : value.toISOString();
|
|
23
|
+
}
|
|
24
|
+
toSQLScalar(value) {
|
|
25
|
+
if (value === null) return "NULL";
|
|
26
|
+
return value instanceof Sql ? value.string : `'${value.toISOString()}'`;
|
|
27
|
+
}
|
|
28
|
+
fromDriverScalar(value) {
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Creates a `timestamp` column. Date and time, maps to `Date`.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* timestamp({ withTimezone: true, precision: 3, notNull }) // timestamp(3) with time zone NOT NULL
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @param config.withTimezone - Include timezone info (default: `true`)
|
|
41
|
+
* @param config.precision - Fractional seconds precision (0–6)
|
|
42
|
+
*/
|
|
43
|
+
function timestamp(config) {
|
|
44
|
+
return new TimestampColumn(config);
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
export { timestamp };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Sql } from "../sql.mjs";
|
|
2
|
+
import { Column, ColumnConfig } from "./common.mjs";
|
|
3
|
+
import * as z from "zod";
|
|
4
|
+
|
|
5
|
+
//#region src/columns/uuid.d.ts
|
|
6
|
+
type UuidValType = string;
|
|
7
|
+
/** Supported UUID versions for validation. */
|
|
8
|
+
type UuidVersion = "v1" | "v2" | "v3" | "v4" | "v5" | "v6" | "v7" | "v8";
|
|
9
|
+
type UuidConfig = ColumnConfig & {
|
|
10
|
+
/** UUID version to validate against in the Zod schema (e.g. `"v4"`, `"v7"`). */version?: UuidVersion;
|
|
11
|
+
};
|
|
12
|
+
declare class UuidColumn<TConfig extends UuidConfig> extends Column<TConfig, UuidValType> {
|
|
13
|
+
static readonly id = "Column.Uuid";
|
|
14
|
+
get sqlTypeScalar(): string;
|
|
15
|
+
get zodTypeScaler(): z.ZodUUID;
|
|
16
|
+
toDriverScalar(value: UuidValType | Sql | null): string | null;
|
|
17
|
+
toSQLScalar(value: UuidValType | Sql | null): string;
|
|
18
|
+
fromDriverScalar(value: string | null): UuidValType | null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates a `uuid` column. PostgreSQL universally unique identifier type, maps to `string`.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* uuid({ notNull }) // uuid NOT NULL, zod validates v7 (default)
|
|
26
|
+
* uuid({ notNull, version: "v4" }) // uuid NOT NULL, zod validates v4
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @param config.version - UUID version to validate in the Zod schema (default: `"v7"`)
|
|
30
|
+
*/
|
|
31
|
+
declare function uuid<TConfig extends UuidConfig>(config: TConfig): UuidColumn<TConfig>;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { UuidVersion, uuid };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Column } from "./common.mjs";
|
|
2
|
+
import { Sql } from "../sql.mjs";
|
|
3
|
+
import * as z from "zod";
|
|
4
|
+
//#region src/columns/uuid.ts
|
|
5
|
+
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
6
|
+
var UuidColumn = class extends Column {
|
|
7
|
+
static id = "Column.Uuid";
|
|
8
|
+
get sqlTypeScalar() {
|
|
9
|
+
return "uuid";
|
|
10
|
+
}
|
|
11
|
+
get zodTypeScaler() {
|
|
12
|
+
const version = this.config.version ?? "v7";
|
|
13
|
+
return z.uuid({ version });
|
|
14
|
+
}
|
|
15
|
+
toDriverScalar(value) {
|
|
16
|
+
if (value === null) return null;
|
|
17
|
+
return value instanceof Sql ? value.string : value;
|
|
18
|
+
}
|
|
19
|
+
toSQLScalar(value) {
|
|
20
|
+
if (value === null) return "NULL";
|
|
21
|
+
if (value instanceof Sql) return value.string;
|
|
22
|
+
if (!UUID_REGEX.test(value)) throw new Error(`Invalid UUID value: ${value}`);
|
|
23
|
+
return `'${value}'`;
|
|
24
|
+
}
|
|
25
|
+
fromDriverScalar(value) {
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Creates a `uuid` column. PostgreSQL universally unique identifier type, maps to `string`.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* uuid({ notNull }) // uuid NOT NULL, zod validates v7 (default)
|
|
35
|
+
* uuid({ notNull, version: "v4" }) // uuid NOT NULL, zod validates v4
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @param config.version - UUID version to validate in the Zod schema (default: `"v7"`)
|
|
39
|
+
*/
|
|
40
|
+
function uuid(config) {
|
|
41
|
+
return new UuidColumn(config);
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
export { uuid };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Sql } from "../sql.mjs";
|
|
2
|
+
import { Column, ColumnConfig } from "./common.mjs";
|
|
3
|
+
import * as z from "zod";
|
|
4
|
+
|
|
5
|
+
//#region src/columns/varchar.d.ts
|
|
6
|
+
type VarcharValType = string;
|
|
7
|
+
type VarcharConfig = ColumnConfig & {
|
|
8
|
+
/** Maximum character length (default: 255) */length?: number;
|
|
9
|
+
};
|
|
10
|
+
declare class VarcharColumn<TConfig extends VarcharConfig> extends Column<TConfig, VarcharValType> {
|
|
11
|
+
#private;
|
|
12
|
+
static readonly id = "Column.Varchar";
|
|
13
|
+
constructor(config: TConfig);
|
|
14
|
+
get sqlTypeScalar(): string;
|
|
15
|
+
get zodTypeScaler(): z.ZodString;
|
|
16
|
+
toDriverScalar(value: VarcharValType | Sql | null): string | null;
|
|
17
|
+
toSQLScalar(value: string | Sql | null): string;
|
|
18
|
+
fromDriverScalar(value: string | null): VarcharValType | null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates a `varchar` column. Variable-length string with a maximum length, maps to `string`.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* varchar({ length: 255, notNull }) // varchar(255) NOT NULL
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param config.length - Maximum character length (default: 255)
|
|
29
|
+
*/
|
|
30
|
+
declare function varchar<TConfig extends VarcharConfig>(config: TConfig): VarcharColumn<TConfig>;
|
|
31
|
+
//#endregion
|
|
32
|
+
export { VarcharColumn, varchar };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Column } from "./common.mjs";
|
|
2
|
+
import { Sql } from "../sql.mjs";
|
|
3
|
+
import * as z from "zod";
|
|
4
|
+
//#region src/columns/varchar.ts
|
|
5
|
+
var VarcharColumn = class extends Column {
|
|
6
|
+
static id = "Column.Varchar";
|
|
7
|
+
#length;
|
|
8
|
+
constructor(config) {
|
|
9
|
+
super(config);
|
|
10
|
+
this.#length = config.length ?? 255;
|
|
11
|
+
}
|
|
12
|
+
get sqlTypeScalar() {
|
|
13
|
+
return `varchar(${this.#length})`;
|
|
14
|
+
}
|
|
15
|
+
get zodTypeScaler() {
|
|
16
|
+
return z.string().max(this.#length);
|
|
17
|
+
}
|
|
18
|
+
toDriverScalar(value) {
|
|
19
|
+
if (value === null) return null;
|
|
20
|
+
return value instanceof Sql ? value.string : value;
|
|
21
|
+
}
|
|
22
|
+
toSQLScalar(value) {
|
|
23
|
+
if (value === null) return "NULL";
|
|
24
|
+
return value instanceof Sql ? value.string : `'${value.replace(/'/g, "''")}'`;
|
|
25
|
+
}
|
|
26
|
+
fromDriverScalar(value) {
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Creates a `varchar` column. Variable-length string with a maximum length, maps to `string`.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* varchar({ length: 255, notNull }) // varchar(255) NOT NULL
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @param config.length - Maximum character length (default: 255)
|
|
39
|
+
*/
|
|
40
|
+
function varchar(config) {
|
|
41
|
+
return new VarcharColumn(config);
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
export { varchar };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Connector } from "./common.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/connectors/bun.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Connector implementation for the Bun built-in SQL client.
|
|
6
|
+
*
|
|
7
|
+
* Bun provides a native, high-performance PostgreSQL client as part of
|
|
8
|
+
* its runtime. This connector leverages Bun's built-in SQL API for
|
|
9
|
+
* optimal performance in Bun environments.
|
|
10
|
+
*
|
|
11
|
+
* @see https://bun.sh/docs/api/sql
|
|
12
|
+
*/
|
|
13
|
+
declare class BunConnector extends Connector {
|
|
14
|
+
getClient(): BunClient;
|
|
15
|
+
getPool(): BunPool;
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
export { BunConnector };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { $Client, $Pool, Connector } from "./common.mjs";
|
|
2
|
+
import { SQL } from "bun";
|
|
3
|
+
//#region src/connectors/bun.ts
|
|
4
|
+
/**
|
|
5
|
+
* Connector implementation for the Bun built-in SQL client.
|
|
6
|
+
*
|
|
7
|
+
* Bun provides a native, high-performance PostgreSQL client as part of
|
|
8
|
+
* its runtime. This connector leverages Bun's built-in SQL API for
|
|
9
|
+
* optimal performance in Bun environments.
|
|
10
|
+
*
|
|
11
|
+
* @see https://bun.sh/docs/api/sql
|
|
12
|
+
*/
|
|
13
|
+
var BunConnector = class extends Connector {
|
|
14
|
+
getClient() {
|
|
15
|
+
return new BunClient(this.url);
|
|
16
|
+
}
|
|
17
|
+
getPool() {
|
|
18
|
+
return new BunPool(this.url, this.config.pool);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Single-connection client wrapper for Bun's SQL API.
|
|
23
|
+
*
|
|
24
|
+
* Configures Bun's SQL client with `max: 1` to simulate single-connection
|
|
25
|
+
* behavior. Uses Bun's native PostgreSQL protocol implementation.
|
|
26
|
+
*
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
var BunClient = class extends $Client {
|
|
30
|
+
#client;
|
|
31
|
+
constructor(connectionString) {
|
|
32
|
+
super();
|
|
33
|
+
this.#client = new SQL(connectionString, { max: 1 });
|
|
34
|
+
this.query = this.#client.unsafe.bind(this.#client);
|
|
35
|
+
}
|
|
36
|
+
async connect() {
|
|
37
|
+
await this.#client.connect();
|
|
38
|
+
}
|
|
39
|
+
getRows(response) {
|
|
40
|
+
return response;
|
|
41
|
+
}
|
|
42
|
+
async close() {
|
|
43
|
+
await this.#client.end();
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Connection pool wrapper for Bun's SQL API.
|
|
48
|
+
*
|
|
49
|
+
* Bun's SQL client has built-in connection pooling. This wrapper configures
|
|
50
|
+
* the pool size via the `max` option for optimal concurrent query handling.
|
|
51
|
+
*
|
|
52
|
+
* @internal
|
|
53
|
+
*/
|
|
54
|
+
var BunPool = class extends $Pool {
|
|
55
|
+
#pool;
|
|
56
|
+
constructor(connectionString, pool) {
|
|
57
|
+
super();
|
|
58
|
+
this.#pool = new SQL(connectionString, { max: pool?.max ?? 10 });
|
|
59
|
+
this.query = this.#pool.unsafe.bind(this.#pool);
|
|
60
|
+
}
|
|
61
|
+
async connect() {
|
|
62
|
+
await this.#pool.connect();
|
|
63
|
+
}
|
|
64
|
+
getRows(response) {
|
|
65
|
+
return response;
|
|
66
|
+
}
|
|
67
|
+
async close() {
|
|
68
|
+
await this.#pool.end();
|
|
69
|
+
}
|
|
70
|
+
async acquireClient() {
|
|
71
|
+
return new BunPoolClient(await this.#pool.reserve());
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Client wrapper for a reserved connection from a Bun SQL pool.
|
|
76
|
+
*
|
|
77
|
+
* This client uses a reserved connection that's exclusive until released.
|
|
78
|
+
*
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
var BunPoolClient = class extends $Client {
|
|
82
|
+
#sql;
|
|
83
|
+
constructor(sql) {
|
|
84
|
+
super();
|
|
85
|
+
this.#sql = sql;
|
|
86
|
+
this.query = this.#sql.unsafe.bind(this.#sql);
|
|
87
|
+
}
|
|
88
|
+
async connect() {}
|
|
89
|
+
getRows(response) {
|
|
90
|
+
return response;
|
|
91
|
+
}
|
|
92
|
+
async close() {
|
|
93
|
+
this.#sql.release();
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
//#endregion
|
|
97
|
+
export { BunConnector };
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { Config } from "../index.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/connectors/common.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Abstract base class for all database connectors.
|
|
6
|
+
*
|
|
7
|
+
* Connectors provide a unified interface for creating database clients
|
|
8
|
+
* and connection pools across different PostgreSQL client libraries.
|
|
9
|
+
* Each connector implementation wraps a specific library (pg, postgres.js,
|
|
10
|
+
* PGlite, Bun SQL) while exposing a consistent API.
|
|
11
|
+
*
|
|
12
|
+
* @abstract
|
|
13
|
+
*/
|
|
14
|
+
declare abstract class Connector {
|
|
15
|
+
/** The configuration object containing file paths, database credentials, client configs etc. */
|
|
16
|
+
config: Config;
|
|
17
|
+
/** The PostgreSQL connection URL derived from the configuration. */
|
|
18
|
+
url: string;
|
|
19
|
+
constructor(config: Config);
|
|
20
|
+
/**
|
|
21
|
+
* Creates a single-connection client.
|
|
22
|
+
*
|
|
23
|
+
* Used by the Transaction class and CLI commands where connection
|
|
24
|
+
* pooling is not required.
|
|
25
|
+
*
|
|
26
|
+
* @returns A client instance for executing queries.
|
|
27
|
+
*/
|
|
28
|
+
abstract getClient(): $Client;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a connection pool.
|
|
31
|
+
*
|
|
32
|
+
* Used by the Database class which handles multiple concurrent
|
|
33
|
+
* database requests.
|
|
34
|
+
*
|
|
35
|
+
* @returns A pool instance for executing queries with connection reuse.
|
|
36
|
+
*/
|
|
37
|
+
abstract getPool(): $Pool;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Abstract base class for single-connection database clients.
|
|
41
|
+
*
|
|
42
|
+
* Implementations wrap specific PostgreSQL client libraries to provide
|
|
43
|
+
* a unified interface for query execution, connection management, and
|
|
44
|
+
* result parsing.
|
|
45
|
+
*
|
|
46
|
+
* @abstract
|
|
47
|
+
*/
|
|
48
|
+
declare abstract class $Client {
|
|
49
|
+
/**
|
|
50
|
+
* Executes a SQL query with optional parameterized arguments.
|
|
51
|
+
*
|
|
52
|
+
* @param query - The SQL query string to execute.
|
|
53
|
+
* @param args - Optional array of parameter values for parameterized queries.
|
|
54
|
+
* @returns A promise that resolves with the query result.
|
|
55
|
+
*/
|
|
56
|
+
query: (query: string, args?: (string | number | null)[]) => Promise<unknown>;
|
|
57
|
+
/**
|
|
58
|
+
* Establishes a connection to the database.
|
|
59
|
+
*
|
|
60
|
+
* @returns A promise that resolves when the connection is established.
|
|
61
|
+
*/
|
|
62
|
+
abstract connect(): Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* Extracts the rows array from a query response.
|
|
65
|
+
*
|
|
66
|
+
* Different client libraries return results in different formats.
|
|
67
|
+
* This method normalizes the response to a standard array of rows.
|
|
68
|
+
*
|
|
69
|
+
* @param response - The raw query response from the underlying client.
|
|
70
|
+
* @returns An array of row objects.
|
|
71
|
+
*/
|
|
72
|
+
abstract getRows(response: any): any[];
|
|
73
|
+
/**
|
|
74
|
+
* Closes the database connection and releases resources.
|
|
75
|
+
*
|
|
76
|
+
* @returns A promise that resolves when the connection is closed.
|
|
77
|
+
*/
|
|
78
|
+
abstract close(): Promise<void>;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Abstract base class for database connection pools.
|
|
82
|
+
*
|
|
83
|
+
* Connection pools manage multiple reusable connections to improve
|
|
84
|
+
* performance for applications with concurrent database access.
|
|
85
|
+
* Implementations wrap specific PostgreSQL client libraries.
|
|
86
|
+
*
|
|
87
|
+
* @abstract
|
|
88
|
+
*/
|
|
89
|
+
declare abstract class $Pool {
|
|
90
|
+
/**
|
|
91
|
+
* Executes a SQL query with optional parameterized arguments.
|
|
92
|
+
*
|
|
93
|
+
* The pool automatically acquires a connection, executes the query,
|
|
94
|
+
* and returns the connection to the pool.
|
|
95
|
+
*
|
|
96
|
+
* @param query - The SQL query string to execute.
|
|
97
|
+
* @param args - Optional array of parameter values for parameterized queries.
|
|
98
|
+
* @returns A promise that resolves with the query result.
|
|
99
|
+
*/
|
|
100
|
+
query: (query: string, args?: (string | number | null)[]) => Promise<unknown>;
|
|
101
|
+
/**
|
|
102
|
+
* Initializes the connection pool.
|
|
103
|
+
*
|
|
104
|
+
* Some pool implementations establish connections lazily, so this
|
|
105
|
+
* method may be a no-op in certain cases.
|
|
106
|
+
*
|
|
107
|
+
* @returns A promise that resolves when the pool is ready.
|
|
108
|
+
*/
|
|
109
|
+
abstract connect(): Promise<void>;
|
|
110
|
+
/**
|
|
111
|
+
* Extracts the rows array from a query response.
|
|
112
|
+
*
|
|
113
|
+
* Different client libraries return results in different formats.
|
|
114
|
+
* This method normalizes the response to a standard array of rows.
|
|
115
|
+
*
|
|
116
|
+
* @param response - The raw query response from the underlying client.
|
|
117
|
+
* @returns An array of row objects.
|
|
118
|
+
*/
|
|
119
|
+
abstract getRows(response: any): any[];
|
|
120
|
+
/**
|
|
121
|
+
* Closes all connections in the pool and releases resources.
|
|
122
|
+
*
|
|
123
|
+
* @returns A promise that resolves when all connections are closed.
|
|
124
|
+
*/
|
|
125
|
+
abstract close(): Promise<void>;
|
|
126
|
+
/**
|
|
127
|
+
* Acquires a client connection from the pool.
|
|
128
|
+
*
|
|
129
|
+
* The returned client has exclusive use of a connection from the pool
|
|
130
|
+
* until it is released by calling `close()`. This is useful for
|
|
131
|
+
* transactions or operations that require connection affinity.
|
|
132
|
+
*
|
|
133
|
+
* **Important:** Always call `close()` on the returned client when done
|
|
134
|
+
* to return the connection to the pool.
|
|
135
|
+
*
|
|
136
|
+
* @returns A promise that resolves with a client instance.
|
|
137
|
+
*/
|
|
138
|
+
abstract acquireClient(): Promise<$Client>;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Union type representing any query executor (client or pool).
|
|
142
|
+
*
|
|
143
|
+
* This type is used in contexts where either a single client or a
|
|
144
|
+
* connection pool can be used interchangeably for query execution.
|
|
145
|
+
*/
|
|
146
|
+
type QueryExecutor = $Client | $Pool;
|
|
147
|
+
//#endregion
|
|
148
|
+
export { $Pool, Connector, QueryExecutor };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { getUrlFromDbCredentials } from "../cli/helpers.mjs";
|
|
2
|
+
//#region src/connectors/common.ts
|
|
3
|
+
/**
|
|
4
|
+
* Abstract base class for all database connectors.
|
|
5
|
+
*
|
|
6
|
+
* Connectors provide a unified interface for creating database clients
|
|
7
|
+
* and connection pools across different PostgreSQL client libraries.
|
|
8
|
+
* Each connector implementation wraps a specific library (pg, postgres.js,
|
|
9
|
+
* PGlite, Bun SQL) while exposing a consistent API.
|
|
10
|
+
*
|
|
11
|
+
* @abstract
|
|
12
|
+
*/
|
|
13
|
+
var Connector = class {
|
|
14
|
+
/** The configuration object containing file paths, database credentials, client configs etc. */
|
|
15
|
+
config;
|
|
16
|
+
/** The PostgreSQL connection URL derived from the configuration. */
|
|
17
|
+
url;
|
|
18
|
+
constructor(config) {
|
|
19
|
+
this.config = config;
|
|
20
|
+
this.url = getUrlFromDbCredentials(config.dbCredentials);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Abstract base class for single-connection database clients.
|
|
25
|
+
*
|
|
26
|
+
* Implementations wrap specific PostgreSQL client libraries to provide
|
|
27
|
+
* a unified interface for query execution, connection management, and
|
|
28
|
+
* result parsing.
|
|
29
|
+
*
|
|
30
|
+
* @abstract
|
|
31
|
+
*/
|
|
32
|
+
var $Client = class {
|
|
33
|
+
/**
|
|
34
|
+
* Executes a SQL query with optional parameterized arguments.
|
|
35
|
+
*
|
|
36
|
+
* @param query - The SQL query string to execute.
|
|
37
|
+
* @param args - Optional array of parameter values for parameterized queries.
|
|
38
|
+
* @returns A promise that resolves with the query result.
|
|
39
|
+
*/
|
|
40
|
+
query;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Abstract base class for database connection pools.
|
|
44
|
+
*
|
|
45
|
+
* Connection pools manage multiple reusable connections to improve
|
|
46
|
+
* performance for applications with concurrent database access.
|
|
47
|
+
* Implementations wrap specific PostgreSQL client libraries.
|
|
48
|
+
*
|
|
49
|
+
* @abstract
|
|
50
|
+
*/
|
|
51
|
+
var $Pool = class {
|
|
52
|
+
/**
|
|
53
|
+
* Executes a SQL query with optional parameterized arguments.
|
|
54
|
+
*
|
|
55
|
+
* The pool automatically acquires a connection, executes the query,
|
|
56
|
+
* and returns the connection to the pool.
|
|
57
|
+
*
|
|
58
|
+
* @param query - The SQL query string to execute.
|
|
59
|
+
* @param args - Optional array of parameter values for parameterized queries.
|
|
60
|
+
* @returns A promise that resolves with the query result.
|
|
61
|
+
*/
|
|
62
|
+
query;
|
|
63
|
+
};
|
|
64
|
+
//#endregion
|
|
65
|
+
export { $Client, $Pool, Connector };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Connector } from "./common.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/connectors/pg.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Connector implementation for the `pg` (node-postgres) library.
|
|
6
|
+
*
|
|
7
|
+
* Non-blocking PostgreSQL client for Node.js. Pure JavaScript and
|
|
8
|
+
* optional native libpq bindings.
|
|
9
|
+
*
|
|
10
|
+
* @see https://node-postgres.com/
|
|
11
|
+
* @see https://www.npmjs.com/package/pg
|
|
12
|
+
*/
|
|
13
|
+
declare class PgConnector extends Connector {
|
|
14
|
+
getClient(): PgClient;
|
|
15
|
+
getPool(): PgPool;
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
export { PgConnector };
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { $Client, $Pool, Connector } from "./common.mjs";
|
|
2
|
+
import { Client, Pool } from "pg";
|
|
3
|
+
//#region src/connectors/pg.ts
|
|
4
|
+
/**
|
|
5
|
+
* Connector implementation for the `pg` (node-postgres) library.
|
|
6
|
+
*
|
|
7
|
+
* Non-blocking PostgreSQL client for Node.js. Pure JavaScript and
|
|
8
|
+
* optional native libpq bindings.
|
|
9
|
+
*
|
|
10
|
+
* @see https://node-postgres.com/
|
|
11
|
+
* @see https://www.npmjs.com/package/pg
|
|
12
|
+
*/
|
|
13
|
+
var PgConnector = class extends Connector {
|
|
14
|
+
getClient() {
|
|
15
|
+
return new PgClient(this.url);
|
|
16
|
+
}
|
|
17
|
+
getPool() {
|
|
18
|
+
return new PgPool(this.url, this.config.pool);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Single-connection client wrapper for the `pg` library.
|
|
23
|
+
*
|
|
24
|
+
* Uses a single dedicated connection to the PostgreSQL database.
|
|
25
|
+
* Best suited for CLI tools, scripts, or scenarios where connection
|
|
26
|
+
* pooling is not required.
|
|
27
|
+
*
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
var PgClient = class extends $Client {
|
|
31
|
+
#client;
|
|
32
|
+
constructor(connectionString) {
|
|
33
|
+
super();
|
|
34
|
+
this.#client = new Client({ connectionString });
|
|
35
|
+
this.query = this.#client.query.bind(this.#client);
|
|
36
|
+
}
|
|
37
|
+
async connect() {
|
|
38
|
+
await this.#client.connect();
|
|
39
|
+
}
|
|
40
|
+
getRows(response) {
|
|
41
|
+
return response.rows;
|
|
42
|
+
}
|
|
43
|
+
async close() {
|
|
44
|
+
await this.#client.end();
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Connection pool wrapper for the `pg` library.
|
|
49
|
+
*
|
|
50
|
+
* Manages a pool of reusable connections to the PostgreSQL database.
|
|
51
|
+
* Recommended for web applications and services that handle multiple
|
|
52
|
+
* concurrent database requests.
|
|
53
|
+
*
|
|
54
|
+
* @internal
|
|
55
|
+
*/
|
|
56
|
+
var PgPool = class extends $Pool {
|
|
57
|
+
#pool;
|
|
58
|
+
constructor(connectionString, pool) {
|
|
59
|
+
super();
|
|
60
|
+
this.#pool = new Pool({
|
|
61
|
+
connectionString,
|
|
62
|
+
max: pool?.max ?? 10
|
|
63
|
+
});
|
|
64
|
+
this.query = this.#pool.query.bind(this.#pool);
|
|
65
|
+
}
|
|
66
|
+
async connect() {
|
|
67
|
+
await this.#pool.connect();
|
|
68
|
+
}
|
|
69
|
+
getRows(response) {
|
|
70
|
+
return response.rows;
|
|
71
|
+
}
|
|
72
|
+
async close() {
|
|
73
|
+
await this.#pool.end();
|
|
74
|
+
}
|
|
75
|
+
async acquireClient() {
|
|
76
|
+
return new PgPoolClient(await this.#pool.connect());
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Client wrapper for a connection acquired from a pg Pool.
|
|
81
|
+
*
|
|
82
|
+
* This client wraps a PoolClient and releases it back to the pool
|
|
83
|
+
* when closed.
|
|
84
|
+
*
|
|
85
|
+
* @internal
|
|
86
|
+
*/
|
|
87
|
+
var PgPoolClient = class extends $Client {
|
|
88
|
+
#client;
|
|
89
|
+
constructor(client) {
|
|
90
|
+
super();
|
|
91
|
+
this.#client = client;
|
|
92
|
+
this.query = this.#client.query.bind(this.#client);
|
|
93
|
+
}
|
|
94
|
+
async connect() {}
|
|
95
|
+
getRows(response) {
|
|
96
|
+
return response.rows;
|
|
97
|
+
}
|
|
98
|
+
async close() {
|
|
99
|
+
this.#client.release();
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
//#endregion
|
|
103
|
+
export { PgConnector };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Connector } from "./common.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/connectors/pglite.d.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
|
+
declare class PgLiteConnector extends Connector {
|
|
15
|
+
getClient(): PgLiteClient;
|
|
16
|
+
getPool(): PgLitePool;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { PgLiteConnector };
|