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.
Files changed (174) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +56 -0
  3. package/dist/bin.cjs +13522 -0
  4. package/dist/src/_virtual/_rolldown/runtime.mjs +28 -0
  5. package/dist/src/cli/helpers.mjs +16 -0
  6. package/dist/src/columns/bigint.d.mts +19 -0
  7. package/dist/src/columns/bigint.mjs +32 -0
  8. package/dist/src/columns/bigserial.d.mts +24 -0
  9. package/dist/src/columns/bigserial.mjs +35 -0
  10. package/dist/src/columns/boolean.d.mts +19 -0
  11. package/dist/src/columns/boolean.mjs +31 -0
  12. package/dist/src/columns/bytea.d.mts +19 -0
  13. package/dist/src/columns/bytea.mjs +33 -0
  14. package/dist/src/columns/char.d.mts +32 -0
  15. package/dist/src/columns/char.mjs +44 -0
  16. package/dist/src/columns/cidr.d.mts +19 -0
  17. package/dist/src/columns/cidr.mjs +31 -0
  18. package/dist/src/columns/common.d.mts +233 -0
  19. package/dist/src/columns/common.mjs +276 -0
  20. package/dist/src/columns/date.d.mts +19 -0
  21. package/dist/src/columns/date.mjs +32 -0
  22. package/dist/src/columns/enum.d.mts +21 -0
  23. package/dist/src/columns/enum.mjs +35 -0
  24. package/dist/src/columns/inet.d.mts +19 -0
  25. package/dist/src/columns/inet.mjs +36 -0
  26. package/dist/src/columns/integer.d.mts +19 -0
  27. package/dist/src/columns/integer.mjs +32 -0
  28. package/dist/src/columns/json.d.mts +52 -0
  29. package/dist/src/columns/json.mjs +67 -0
  30. package/dist/src/columns/jsonb.d.mts +53 -0
  31. package/dist/src/columns/jsonb.mjs +68 -0
  32. package/dist/src/columns/macaddr.d.mts +19 -0
  33. package/dist/src/columns/macaddr.mjs +31 -0
  34. package/dist/src/columns/numeric.d.mts +42 -0
  35. package/dist/src/columns/numeric.mjs +57 -0
  36. package/dist/src/columns/postgis/geography/index.d.mts +18 -0
  37. package/dist/src/columns/postgis/geography/index.mjs +17 -0
  38. package/dist/src/columns/postgis/geography/linestring.d.mts +38 -0
  39. package/dist/src/columns/postgis/geography/linestring.mjs +70 -0
  40. package/dist/src/columns/postgis/geography/multilinestring.d.mts +39 -0
  41. package/dist/src/columns/postgis/geography/multilinestring.mjs +70 -0
  42. package/dist/src/columns/postgis/geography/multipoint.d.mts +38 -0
  43. package/dist/src/columns/postgis/geography/multipoint.mjs +70 -0
  44. package/dist/src/columns/postgis/geography/multipolygon.d.mts +40 -0
  45. package/dist/src/columns/postgis/geography/multipolygon.mjs +70 -0
  46. package/dist/src/columns/postgis/geography/point.d.mts +36 -0
  47. package/dist/src/columns/postgis/geography/point.mjs +69 -0
  48. package/dist/src/columns/postgis/geography/polygon.d.mts +39 -0
  49. package/dist/src/columns/postgis/geography/polygon.mjs +70 -0
  50. package/dist/src/columns/serial.d.mts +24 -0
  51. package/dist/src/columns/serial.mjs +35 -0
  52. package/dist/src/columns/smallint.d.mts +19 -0
  53. package/dist/src/columns/smallint.mjs +32 -0
  54. package/dist/src/columns/smallserial.d.mts +24 -0
  55. package/dist/src/columns/smallserial.mjs +35 -0
  56. package/dist/src/columns/text.d.mts +19 -0
  57. package/dist/src/columns/text.mjs +30 -0
  58. package/dist/src/columns/time.d.mts +44 -0
  59. package/dist/src/columns/time.mjs +47 -0
  60. package/dist/src/columns/timestamp.d.mts +44 -0
  61. package/dist/src/columns/timestamp.mjs +47 -0
  62. package/dist/src/columns/uuid.d.mts +33 -0
  63. package/dist/src/columns/uuid.mjs +44 -0
  64. package/dist/src/columns/varchar.d.mts +32 -0
  65. package/dist/src/columns/varchar.mjs +44 -0
  66. package/dist/src/connectors/bun.d.mts +18 -0
  67. package/dist/src/connectors/bun.mjs +97 -0
  68. package/dist/src/connectors/common.d.mts +148 -0
  69. package/dist/src/connectors/common.mjs +65 -0
  70. package/dist/src/connectors/pg.d.mts +18 -0
  71. package/dist/src/connectors/pg.mjs +103 -0
  72. package/dist/src/connectors/pglite.d.mts +19 -0
  73. package/dist/src/connectors/pglite.mjs +94 -0
  74. package/dist/src/connectors/postgres.d.mts +18 -0
  75. package/dist/src/connectors/postgres.mjs +93 -0
  76. package/dist/src/constraints/check.d.mts +88 -0
  77. package/dist/src/constraints/check.mjs +307 -0
  78. package/dist/src/constraints/primary-key.d.mts +28 -0
  79. package/dist/src/constraints/primary-key.mjs +36 -0
  80. package/dist/src/constraints/unique.d.mts +28 -0
  81. package/dist/src/constraints/unique.mjs +36 -0
  82. package/dist/src/db.d.mts +180 -0
  83. package/dist/src/db.mjs +191 -0
  84. package/dist/src/entity.mjs +23 -0
  85. package/dist/src/enumtype.d.mts +27 -0
  86. package/dist/src/enumtype.mjs +42 -0
  87. package/dist/src/filters/array.d.mts +97 -0
  88. package/dist/src/filters/array.mjs +150 -0
  89. package/dist/src/filters/custom.d.mts +12 -0
  90. package/dist/src/filters/custom.mjs +7 -0
  91. package/dist/src/filters/index.d.mts +155 -0
  92. package/dist/src/filters/index.mjs +318 -0
  93. package/dist/src/functions/index.d.mts +24 -0
  94. package/dist/src/functions/index.mjs +29 -0
  95. package/dist/src/index.d.mts +129 -0
  96. package/dist/src/index.mjs +78 -0
  97. package/dist/src/indexes.d.mts +31 -0
  98. package/dist/src/indexes.mjs +43 -0
  99. package/dist/src/migration/consts.d.mts +8 -0
  100. package/dist/src/migration/consts.mjs +8 -0
  101. package/dist/src/migration/ddl.d.mts +747 -0
  102. package/dist/src/migration/ddl.mjs +1084 -0
  103. package/dist/src/migration/index.d.mts +15 -0
  104. package/dist/src/migration/index.mjs +5 -0
  105. package/dist/src/migration/snapshot.d.mts +169 -0
  106. package/dist/src/migration/snapshot.mjs +123 -0
  107. package/dist/src/migration/statement.d.mts +89 -0
  108. package/dist/src/migration/statement.mjs +61 -0
  109. package/dist/src/models.d.mts +34 -0
  110. package/dist/src/models.mjs +27 -0
  111. package/dist/src/node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/index.mjs +126 -0
  112. package/dist/src/node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/utilities.mjs +29 -0
  113. package/dist/src/node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/vendor/ansi-styles/index.mjs +165 -0
  114. package/dist/src/node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/vendor/supports-color/index.mjs +92 -0
  115. package/dist/src/query-builders/aggregates.d.mts +20 -0
  116. package/dist/src/query-builders/aggregates.mjs +43 -0
  117. package/dist/src/query-builders/count.d.mts +19 -0
  118. package/dist/src/query-builders/count.mjs +36 -0
  119. package/dist/src/query-builders/delete.d.mts +19 -0
  120. package/dist/src/query-builders/delete.mjs +57 -0
  121. package/dist/src/query-builders/distinct.d.mts +19 -0
  122. package/dist/src/query-builders/distinct.mjs +42 -0
  123. package/dist/src/query-builders/exists.d.mts +19 -0
  124. package/dist/src/query-builders/exists.mjs +37 -0
  125. package/dist/src/query-builders/first.d.mts +17 -0
  126. package/dist/src/query-builders/first.mjs +46 -0
  127. package/dist/src/query-builders/insert-returning.d.mts +16 -0
  128. package/dist/src/query-builders/insert-returning.mjs +63 -0
  129. package/dist/src/query-builders/insert.d.mts +22 -0
  130. package/dist/src/query-builders/insert.mjs +93 -0
  131. package/dist/src/query-builders/orderby-clause.d.mts +14 -0
  132. package/dist/src/query-builders/orderby-clause.mjs +20 -0
  133. package/dist/src/query-builders/pre.d.mts +36 -0
  134. package/dist/src/query-builders/pre.mjs +60 -0
  135. package/dist/src/query-builders/query-promise.d.mts +14 -0
  136. package/dist/src/query-builders/query-promise.mjs +21 -0
  137. package/dist/src/query-builders/query.d.mts +14 -0
  138. package/dist/src/query-builders/query.mjs +17 -0
  139. package/dist/src/query-builders/raw.d.mts +14 -0
  140. package/dist/src/query-builders/raw.mjs +26 -0
  141. package/dist/src/query-builders/rq.d.mts +36 -0
  142. package/dist/src/query-builders/rq.mjs +231 -0
  143. package/dist/src/query-builders/select.d.mts +48 -0
  144. package/dist/src/query-builders/select.mjs +118 -0
  145. package/dist/src/query-builders/update.d.mts +24 -0
  146. package/dist/src/query-builders/update.mjs +99 -0
  147. package/dist/src/sequence.d.mts +48 -0
  148. package/dist/src/sequence.mjs +53 -0
  149. package/dist/src/sql.d.mts +15 -0
  150. package/dist/src/sql.mjs +36 -0
  151. package/dist/src/symbols.d.mts +4 -0
  152. package/dist/src/symbols.mjs +4 -0
  153. package/dist/src/table.d.mts +144 -0
  154. package/dist/src/table.mjs +134 -0
  155. package/dist/src/types.d.mts +4 -0
  156. package/dist/src/utils.mjs +10 -0
  157. package/dist/src/validators/zod.d.mts +23 -0
  158. package/dist/src/validators/zod.mjs +27 -0
  159. package/dist/src/wkx/binaryreader.mjs +56 -0
  160. package/dist/src/wkx/binarywriter.mjs +110 -0
  161. package/dist/src/wkx/geometry.mjs +101 -0
  162. package/dist/src/wkx/geometrycollection.mjs +113 -0
  163. package/dist/src/wkx/index.mjs +18 -0
  164. package/dist/src/wkx/linestring.mjs +124 -0
  165. package/dist/src/wkx/multilinestring.mjs +133 -0
  166. package/dist/src/wkx/multipoint.mjs +120 -0
  167. package/dist/src/wkx/multipolygon.mjs +155 -0
  168. package/dist/src/wkx/parser.mjs +183 -0
  169. package/dist/src/wkx/point.mjs +159 -0
  170. package/dist/src/wkx/polygon.mjs +189 -0
  171. package/dist/src/wkx/types.mjs +37 -0
  172. package/dist/src/wkx/wktparser.mjs +109 -0
  173. package/dist/src/wkx/zigzag.mjs +18 -0
  174. package/package.json +100 -0
@@ -0,0 +1,36 @@
1
+ //#region src/constraints/unique.ts
2
+ var UniqueConstraint = class {
3
+ #name;
4
+ #columns;
5
+ constructor(name, columns) {
6
+ if (columns.length < 2) throw new Error("UNIQUE constraint requires at least two columns. For single-column unique, use the column-level 'unique' modifier 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 UNIQUE constraint on two or more columns.
19
+ * For single-column unique, use the column-level `unique` modifier instead.
20
+ *
21
+ * This function is passed as the second parameter to the `uniqueConstraints` callback.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * table("public", "user_roles", { userId: bigint({}), roleId: bigint({}) }, {
26
+ * uniqueConstraints: (t, unique) => [
27
+ * unique("unique_user_role", [t.userId, t.roleId]),
28
+ * ],
29
+ * });
30
+ * ```
31
+ */
32
+ function uniqueConstraint(name, columns) {
33
+ return new UniqueConstraint(name, columns);
34
+ }
35
+ //#endregion
36
+ export { UniqueConstraint, uniqueConstraint };
@@ -0,0 +1,180 @@
1
+ import { SelectBuilder } from "./query-builders/select.mjs";
2
+ import { BuildFilterExpression } from "./filters/index.mjs";
3
+ import { AggregateQuery } from "./query-builders/aggregates.mjs";
4
+ import { CountQuery } from "./query-builders/count.mjs";
5
+ import { DeleteQuery } from "./query-builders/delete.mjs";
6
+ import { DistinctQuery } from "./query-builders/distinct.mjs";
7
+ import { ExistsQuery } from "./query-builders/exists.mjs";
8
+ import { FirstQuery } from "./query-builders/first.mjs";
9
+ import { InsertBuilder } from "./query-builders/insert.mjs";
10
+ import { InsertReturningQuery } from "./query-builders/insert-returning.mjs";
11
+ import { RawQuery } from "./query-builders/raw.mjs";
12
+ import { RelationQueryBuilder } from "./query-builders/rq.mjs";
13
+ import { UpdateBuilder } from "./query-builders/update.mjs";
14
+ import { AnyColumn, AnyRelations, IsTableWC, Relations, TColsToLeftRight, TableWCorNever, TableWithColumns } from "./table.mjs";
15
+ import { Config, DurcnoSetup } from "./index.mjs";
16
+ import { $Pool, Connector, QueryExecutor } from "./connectors/common.mjs";
17
+
18
+ //#region src/db.d.ts
19
+ type AnyDBorTX = Base<any, any, any, false>;
20
+ type StdTableFullName = `"${string}"."${string}"`;
21
+ declare class Base<TTName extends string, TTables extends Record<TTName, TableWithColumns<string, string, Record<string, AnyColumn>>>, TAllRelations extends Record<StdTableFullName, AnyRelations>, TPrepare extends boolean> {
22
+ #private;
23
+ _: {
24
+ getConnector: () => Connector;
25
+ getExecutor: () => QueryExecutor;
26
+ };
27
+ $: {
28
+ tables: TTables;
29
+ allRelations: TAllRelations;
30
+ config: Config;
31
+ dbUrl: string;
32
+ pre: TPrepare;
33
+ };
34
+ constructor(tables: TTables, allRelations: TAllRelations, executor: QueryExecutor | null, connector: Connector, prepare: TPrepare);
35
+ /**
36
+ * Start building an INSERT query for the specified table.
37
+ * @param table The table to insert into
38
+ * @returns An InsertBuilder instance to chain `.values()` and execute the insert
39
+ */
40
+ insert<TTable extends TTables[keyof TTables]>(table: TTable): InsertBuilder<TTable>;
41
+ /**
42
+ * Start building a SELECT query from the specified table.
43
+ * @param table The table to select from
44
+ * @returns A FromBuilder instance to chain `.select()`, `.where()`, `.orderBy()`, etc.
45
+ */
46
+ from<UTSchema extends string, UTName extends string, UTColumns extends Record<string, AnyColumn>>(table: TableWithColumns<UTSchema, UTName, UTColumns>): SelectBuilder<UTSchema, UTName, UTColumns, TPrepare, null>;
47
+ /**
48
+ * Start building an UPDATE query for the specified table.
49
+ * @param table The table to update
50
+ * @returns An UpdateBuilder instance to chain `.set()` and `.where()` clauses
51
+ */
52
+ update<TTable extends TTables[keyof TTables]>(table: TTable): UpdateBuilder<TTable, TPrepare>;
53
+ /**
54
+ * Start building a DELETE query for the specified table.
55
+ * @param table The table to delete from
56
+ * @returns A DeleteQuery instance to chain `.where()` and execute the deletion
57
+ */
58
+ delete<TTable extends TTables[keyof TTables]>(table: TTable): DeleteQuery<TTable, TPrepare, undefined, null>;
59
+ /**
60
+ * Count the number of rows in a table, optionally filtered by a where clause.
61
+ * @param table The table to count rows from
62
+ * @param where Optional where clause to filter rows
63
+ * @returns Promise<number> - the count of matching rows
64
+ */
65
+ $count<TTable extends TTables[keyof TTables]>(table: TTable): CountQuery<TTable>;
66
+ $count<TTable extends TTables[keyof TTables], TWhere extends BuildFilterExpression<TColsToLeftRight<TTable["_"]["columns"]>>>(table: TTable, where: TWhere): CountQuery<TTable>;
67
+ /**
68
+ * Check if any rows exist in a table, optionally filtered by a where clause.
69
+ * @param table The table to check
70
+ * @param where Optional where clause to filter rows
71
+ * @returns Promise<boolean> - true if at least one row exists
72
+ */
73
+ $exists<TTable extends TTables[keyof TTables]>(table: TTable): ExistsQuery<TTable>;
74
+ $exists<TTable extends TTables[keyof TTables], TWhere extends BuildFilterExpression<TColsToLeftRight<TTable["_"]["columns"]>>>(table: TTable, where: TWhere): ExistsQuery<TTable>;
75
+ /**
76
+ * Get the first row from a table, optionally filtered by a where clause.
77
+ * @param table The table to query
78
+ * @param where Optional where clause to filter rows
79
+ * @returns Promise<T | null> - the first row or null if no rows match
80
+ */
81
+ $first<TTable extends TTables[keyof TTables]>(table: TTable): FirstQuery<TTable>;
82
+ $first<TTable extends TTables[keyof TTables], TWhere extends BuildFilterExpression<TColsToLeftRight<TTable["_"]["columns"]>>>(table: TTable, where: TWhere): FirstQuery<TTable>;
83
+ /**
84
+ * Calculate the sum of a numeric column.
85
+ * @param table The table to query
86
+ * @param column The numeric column to sum
87
+ * @param where Optional where clause to filter rows
88
+ * @returns Promise<number | null> - the sum or null if no rows match
89
+ */
90
+ $sum<TTable extends TTables[keyof TTables], TColumn extends TTable["_"]["columns"][keyof TTable["_"]["columns"]]>(table: TTable, column: TColumn): AggregateQuery<TTable, TColumn, number | null>;
91
+ $sum<TTable extends TTables[keyof TTables], TColumn extends TTable["_"]["columns"][keyof TTable["_"]["columns"]], TWhere extends BuildFilterExpression<TColsToLeftRight<TTable["_"]["columns"]>>>(table: TTable, column: TColumn, where: TWhere): AggregateQuery<TTable, TColumn, number | null>;
92
+ /**
93
+ * Calculate the average of a numeric column.
94
+ * @param table The table to query
95
+ * @param column The numeric column to average
96
+ * @param where Optional where clause to filter rows
97
+ * @returns Promise<number | null> - the average or null if no rows match
98
+ */
99
+ $avg<TTable extends TTables[keyof TTables], TColumn extends TTable["_"]["columns"][keyof TTable["_"]["columns"]]>(table: TTable, column: TColumn): AggregateQuery<TTable, TColumn, number | null>;
100
+ $avg<TTable extends TTables[keyof TTables], TColumn extends TTable["_"]["columns"][keyof TTable["_"]["columns"]], TWhere extends BuildFilterExpression<TColsToLeftRight<TTable["_"]["columns"]>>>(table: TTable, column: TColumn, where: TWhere): AggregateQuery<TTable, TColumn, number | null>;
101
+ /**
102
+ * Find the minimum value of a column.
103
+ * @param table The table to query
104
+ * @param column The column to find the minimum of
105
+ * @param where Optional where clause to filter rows
106
+ * @returns Promise<number | null> - the minimum value or null if no rows match
107
+ */
108
+ $min<TTable extends TTables[keyof TTables], TColumn extends TTable["_"]["columns"][keyof TTable["_"]["columns"]]>(table: TTable, column: TColumn): AggregateQuery<TTable, TColumn, number | null>;
109
+ $min<TTable extends TTables[keyof TTables], TColumn extends TTable["_"]["columns"][keyof TTable["_"]["columns"]], TWhere extends BuildFilterExpression<TColsToLeftRight<TTable["_"]["columns"]>>>(table: TTable, column: TColumn, where: TWhere): AggregateQuery<TTable, TColumn, number | null>;
110
+ /**
111
+ * Find the maximum value of a column.
112
+ * @param table The table to query
113
+ * @param column The column to find the maximum of
114
+ * @param where Optional where clause to filter rows
115
+ * @returns Promise<number | null> - the maximum value or null if no rows match
116
+ */
117
+ $max<TTable extends TTables[keyof TTables], TColumn extends TTable["_"]["columns"][keyof TTable["_"]["columns"]]>(table: TTable, column: TColumn): AggregateQuery<TTable, TColumn, number | null>;
118
+ $max<TTable extends TTables[keyof TTables], TColumn extends TTable["_"]["columns"][keyof TTable["_"]["columns"]], TWhere extends BuildFilterExpression<TColsToLeftRight<TTable["_"]["columns"]>>>(table: TTable, column: TColumn, where: TWhere): AggregateQuery<TTable, TColumn, number | null>;
119
+ /**
120
+ * Get distinct values of a column.
121
+ * @param table The table to query
122
+ * @param column The column to get distinct values from
123
+ * @param where Optional where clause to filter rows
124
+ * @returns Promise<T[]> - array of distinct values
125
+ */
126
+ $distinct<TTable extends TTables[keyof TTables], TColumn extends TTable["_"]["columns"][keyof TTable["_"]["columns"]]>(table: TTable, column: TColumn): DistinctQuery<TTable, TColumn, TColumn["ValTypeSelect"][]>;
127
+ $distinct<TTable extends TTables[keyof TTables], TColumn extends TTable["_"]["columns"][keyof TTable["_"]["columns"]], TWhere extends BuildFilterExpression<TColsToLeftRight<TTable["_"]["columns"]>>>(table: TTable, column: TColumn, where: TWhere): DistinctQuery<TTable, TColumn, TColumn["ValTypeSelect"][]>;
128
+ /**
129
+ * Insert a row and return the inserted row with all columns.
130
+ * @param table The table to insert into
131
+ * @param values The values to insert
132
+ * @returns Promise<T> - the inserted row with all columns including generated values
133
+ */
134
+ $insertReturning<TTable extends TTables[keyof TTables]>(table: TTable, values: { [colName in keyof TTable["_"]["columns"] as TTable["_"]["columns"][colName]["ValTypeInsert"] extends never ? never : undefined extends TTable["_"]["columns"][colName]["ValTypeInsert"] ? never : colName]: TTable["_"]["columns"][colName]["ValTypeInsert"] } & { [colName in keyof TTable["_"]["columns"] as TTable["_"]["columns"][colName]["ValTypeInsert"] extends never ? never : undefined extends TTable["_"]["columns"][colName]["ValTypeInsert"] ? colName : never]?: Exclude<TTable["_"]["columns"][colName]["ValTypeInsert"], undefined> }): InsertReturningQuery<TTable, { [ColName in keyof TTable["_"]["columns"]]: TTable["_"]["columns"][ColName]["ValTypeSelect"] }>;
135
+ /**
136
+ * Start building a relational query for the specified table.
137
+ *
138
+ * Returns a builder with two methods:
139
+ * - `.findMany(options)` — fetch all matching rows
140
+ * - `.findFirst(options)` — fetch the first matching row (or `null`)
141
+ *
142
+ * Column selection works in two exclusive modes:
143
+ * - **Include mode** — `columns: { id: true, name: true }` returns only these columns.
144
+ * - **Exclude mode** — `columns: { password: false }` returns all columns except these.
145
+ *
146
+ * Use the `with` option to include related rows from other tables.
147
+ * You can nest `with` to load deeply related data.
148
+ *
149
+ * @param table The table to query — must have relations registered via `relations()`.
150
+ * @returns A `RelationQueryBuilder` with `.findMany()` and `.findFirst()` methods.
151
+ */
152
+ query<UTSchema extends string, UTName extends string, TColumns extends Record<string, AnyColumn>>(table: TableWithColumns<UTSchema, UTName, TColumns>): RelationQueryBuilder<UTSchema, UTName, TColumns, TAllRelations[`"${UTSchema}"."${UTName}"`], TAllRelations>;
153
+ /**
154
+ * Execute a raw SQL query with optional parameter binding and custom result handler.
155
+ * @param query The raw SQL query string with $1, $2, etc. placeholders for parameters
156
+ * @param args Array of parameter values to bind to the query placeholders
157
+ * @param rowsHandler Optional function to transform the raw result rows
158
+ * @returns A RawQuery instance that resolves to the query result (or transformed result if handler provided)
159
+ */
160
+ raw<TReturn = unknown>(query: string, args: (string | number | null)[] | undefined, rowsHandler: ((rows: any[]) => TReturn) | undefined): RawQuery<TReturn>;
161
+ }
162
+ declare class DB<TTName extends string, TTables extends Record<TTName, TableWithColumns<string, string, Record<string, AnyColumn>>>, TAllRelations extends Record<StdTableFullName, AnyRelations>> extends Base<TTName, TTables, TAllRelations, false> {
163
+ #private;
164
+ constructor(tables: TTables, allRelations: TAllRelations, pool: $Pool, connector: Connector);
165
+ prepare(): Preparer<string, TTables, TAllRelations>;
166
+ /**
167
+ * Execute multiple queries within a single database transaction in a new connection.
168
+ * @param callback Function that receives a transaction context and to be used instead of db.
169
+ * @returns Promise that resolves to the callback's return value
170
+ */
171
+ transaction<T>(callback: (...args: [Transaction<TTName, TTables, TAllRelations>]) => Promise<T>): Promise<T>;
172
+ close(): Promise<true>;
173
+ }
174
+ declare class Transaction<TTNames extends string, TTables extends Record<TTNames, TableWithColumns<string, string, Record<string, AnyColumn>>>, TAllRelations extends Record<StdTableFullName, AnyRelations>> extends Base<TTNames, TTables, TAllRelations, false> {}
175
+ declare class Preparer<TTNames extends string, TTables extends Record<TTNames, TableWithColumns<string, string, Record<string, AnyColumn>>>, TAllRelations extends Record<StdTableFullName, AnyRelations>> extends Base<TTNames, TTables, TAllRelations, true> {
176
+ constructor(tables: TTables, allRelations: TAllRelations, connector: Connector);
177
+ }
178
+ declare function database<TEntities extends Record<string, unknown>>(entities: TEntities, setup: DurcnoSetup): DB<string, { [Export in keyof TEntities as IsTableWC<TEntities[Export]> extends true ? Export : never]: TableWCorNever<TEntities[Export]> }, { [Export in keyof TEntities as TEntities[Export] extends ((...args: unknown[]) => infer TRelations) ? TRelations extends Relations<infer TSchema, infer TName, infer _x, infer _y> ? `\"${TSchema}\".\"${TName}\"` : never : never]: TEntities[Export] extends ((...args: unknown[]) => infer TRelations) ? TRelations extends Relations<infer TTSchema, infer TTName, infer TTColumns, infer TTRelations> ? Relations<TTSchema, TTName, TTColumns, TTRelations> : never : never }>;
179
+ //#endregion
180
+ export { AnyDBorTX, database };
@@ -0,0 +1,191 @@
1
+ import { is } from "./entity.mjs";
2
+ import { getUrlFromDbCredentials } from "./cli/helpers.mjs";
3
+ import { AggregateQuery } from "./query-builders/aggregates.mjs";
4
+ import { CountQuery } from "./query-builders/count.mjs";
5
+ import { DeleteQuery } from "./query-builders/delete.mjs";
6
+ import { DistinctQuery } from "./query-builders/distinct.mjs";
7
+ import { ExistsQuery } from "./query-builders/exists.mjs";
8
+ import { FirstQuery } from "./query-builders/first.mjs";
9
+ import { InsertBuilder } from "./query-builders/insert.mjs";
10
+ import { InsertReturningQuery } from "./query-builders/insert-returning.mjs";
11
+ import { RawQuery } from "./query-builders/raw.mjs";
12
+ import { RelationQueryBuilder } from "./query-builders/rq.mjs";
13
+ import { SelectBuilder } from "./query-builders/select.mjs";
14
+ import { UpdateBuilder } from "./query-builders/update.mjs";
15
+ import { Relations, Table } from "./table.mjs";
16
+ //#region src/db.ts
17
+ var Base = class {
18
+ #allRelations;
19
+ #config;
20
+ #connector;
21
+ #executor;
22
+ _ = {
23
+ getConnector: () => this.#connector,
24
+ getExecutor: this.#getExecutor.bind(this)
25
+ };
26
+ $;
27
+ constructor(tables, allRelations, executor, connector, prepare) {
28
+ this.#allRelations = allRelations;
29
+ this.#config = connector.config;
30
+ this.#executor = executor;
31
+ this.#connector = connector;
32
+ this.$ = {
33
+ tables,
34
+ allRelations,
35
+ config: connector.config,
36
+ dbUrl: getUrlFromDbCredentials(connector.config.dbCredentials),
37
+ pre: prepare
38
+ };
39
+ }
40
+ #getExecutor() {
41
+ if (this.$.pre === true) return null;
42
+ else if (this.#executor === null) throw new Error("Cannot execute query: no database connection available");
43
+ return this.#executor;
44
+ }
45
+ /**
46
+ * Start building an INSERT query for the specified table.
47
+ * @param table The table to insert into
48
+ * @returns An InsertBuilder instance to chain `.values()` and execute the insert
49
+ */
50
+ insert(table) {
51
+ return new InsertBuilder(table, this.#config, this.#getExecutor());
52
+ }
53
+ /**
54
+ * Start building a SELECT query from the specified table.
55
+ * @param table The table to select from
56
+ * @returns A FromBuilder instance to chain `.select()`, `.where()`, `.orderBy()`, etc.
57
+ */
58
+ from(table) {
59
+ return new SelectBuilder(table, null, void 0, this.#config, this.#getExecutor(), this.$.pre);
60
+ }
61
+ /**
62
+ * Start building an UPDATE query for the specified table.
63
+ * @param table The table to update
64
+ * @returns An UpdateBuilder instance to chain `.set()` and `.where()` clauses
65
+ */
66
+ update(table) {
67
+ return new UpdateBuilder(table, this.#config, this.#getExecutor(), this.$.pre);
68
+ }
69
+ /**
70
+ * Start building a DELETE query for the specified table.
71
+ * @param table The table to delete from
72
+ * @returns A DeleteQuery instance to chain `.where()` and execute the deletion
73
+ */
74
+ delete(table) {
75
+ return new DeleteQuery(table, void 0, void 0, this.#config, this.#getExecutor(), this.$.pre);
76
+ }
77
+ $count(table, where) {
78
+ return new CountQuery(table, where, this.#config, this.#getExecutor());
79
+ }
80
+ $exists(table, where) {
81
+ return new ExistsQuery(table, where, this.#config, this.#getExecutor());
82
+ }
83
+ $first(table, where) {
84
+ return new FirstQuery(table, where, this.#config, this.#getExecutor());
85
+ }
86
+ $sum(table, column, where) {
87
+ return new AggregateQuery(table, column, "SUM", where, this.#config, this.#getExecutor());
88
+ }
89
+ $avg(table, column, where) {
90
+ return new AggregateQuery(table, column, "AVG", where, this.#config, this.#getExecutor());
91
+ }
92
+ $min(table, column, where) {
93
+ return new AggregateQuery(table, column, "MIN", where, this.#config, this.#getExecutor());
94
+ }
95
+ $max(table, column, where) {
96
+ return new AggregateQuery(table, column, "MAX", where, this.#config, this.#getExecutor());
97
+ }
98
+ $distinct(table, column, where) {
99
+ return new DistinctQuery(table, column, where, this.#config, this.#getExecutor());
100
+ }
101
+ /**
102
+ * Insert a row and return the inserted row with all columns.
103
+ * @param table The table to insert into
104
+ * @param values The values to insert
105
+ * @returns Promise<T> - the inserted row with all columns including generated values
106
+ */
107
+ $insertReturning(table, values) {
108
+ return new InsertReturningQuery(table, values, this.#config, this.#getExecutor());
109
+ }
110
+ /**
111
+ * Start building a relational query for the specified table.
112
+ *
113
+ * Returns a builder with two methods:
114
+ * - `.findMany(options)` — fetch all matching rows
115
+ * - `.findFirst(options)` — fetch the first matching row (or `null`)
116
+ *
117
+ * Column selection works in two exclusive modes:
118
+ * - **Include mode** — `columns: { id: true, name: true }` returns only these columns.
119
+ * - **Exclude mode** — `columns: { password: false }` returns all columns except these.
120
+ *
121
+ * Use the `with` option to include related rows from other tables.
122
+ * You can nest `with` to load deeply related data.
123
+ *
124
+ * @param table The table to query — must have relations registered via `relations()`.
125
+ * @returns A `RelationQueryBuilder` with `.findMany()` and `.findFirst()` methods.
126
+ */
127
+ query(table) {
128
+ return new RelationQueryBuilder(table, this.#allRelations[table._.fullName], this.#allRelations, this.#config, this.#getExecutor());
129
+ }
130
+ /**
131
+ * Execute a raw SQL query with optional parameter binding and custom result handler.
132
+ * @param query The raw SQL query string with $1, $2, etc. placeholders for parameters
133
+ * @param args Array of parameter values to bind to the query placeholders
134
+ * @param rowsHandler Optional function to transform the raw result rows
135
+ * @returns A RawQuery instance that resolves to the query result (or transformed result if handler provided)
136
+ */
137
+ raw(query, args = [], rowsHandler) {
138
+ return new RawQuery(query, args, rowsHandler, this.#getExecutor());
139
+ }
140
+ };
141
+ var DB = class extends Base {
142
+ #pool;
143
+ constructor(tables, allRelations, pool, connector) {
144
+ super(tables, allRelations, pool, connector, false);
145
+ this.#pool = pool;
146
+ }
147
+ prepare() {
148
+ return new Preparer(this.$.tables, this.$.allRelations, this._.getConnector());
149
+ }
150
+ /**
151
+ * Execute multiple queries within a single database transaction in a new connection.
152
+ * @param callback Function that receives a transaction context and to be used instead of db.
153
+ * @returns Promise that resolves to the callback's return value
154
+ */
155
+ async transaction(callback) {
156
+ const client = await this.#pool.acquireClient();
157
+ try {
158
+ await client.query("BEGIN;");
159
+ const result = await callback(new Transaction(this.$.tables, this.$.allRelations, client, this._.getConnector(), false));
160
+ await client.query("COMMIT;");
161
+ await client.close();
162
+ return result;
163
+ } catch (error) {
164
+ await client.query("ROLLBACK;");
165
+ await client.close();
166
+ throw error;
167
+ }
168
+ }
169
+ async close() {
170
+ await this._.getExecutor()?.close();
171
+ return true;
172
+ }
173
+ };
174
+ var Transaction = class extends Base {};
175
+ var Preparer = class extends Base {
176
+ constructor(tables, allRelations, connector) {
177
+ super(tables, allRelations, null, connector, true);
178
+ }
179
+ };
180
+ function database(entities, setup) {
181
+ return new DB(Object.fromEntries(Object.entries(entities).map(([name, entity]) => {
182
+ if (is(entity, Table)) {
183
+ entity._.config = setup.config;
184
+ return [name, entity];
185
+ }
186
+ }).filter((entry) => entry !== void 0)), Object.fromEntries(Object.entries(entities).map(([_, entity]) => {
187
+ if (typeof entity === "function" && is(entity(), Relations)) return [entity().table._.fullName, entity()];
188
+ }).filter((entry) => entry !== void 0)), setup.connector.getPool(), setup.connector);
189
+ }
190
+ //#endregion
191
+ export { database };
@@ -0,0 +1,23 @@
1
+ import { entityType } from "./symbols.mjs";
2
+ import { Column } from "./columns/common.mjs";
3
+ //#region src/entity.ts
4
+ /** biome-ignore-all lint/suspicious/noExplicitAny: <> */
5
+ function is(value, type) {
6
+ if (value === null || value === void 0) return false;
7
+ if (value instanceof type) return true;
8
+ let cls = Object.getPrototypeOf(value).constructor;
9
+ if (cls) while (cls) {
10
+ if (entityType in cls && cls[entityType] === type[entityType]) return true;
11
+ cls = Object.getPrototypeOf(cls);
12
+ }
13
+ return false;
14
+ }
15
+ function isCol(value) {
16
+ return is(value, Column);
17
+ }
18
+ function isTableCol(value) {
19
+ return isCol(value) && value.table !== void 0;
20
+ }
21
+ const isTCol = isTableCol;
22
+ //#endregion
23
+ export { is, isCol, isTCol, isTableCol };
@@ -0,0 +1,27 @@
1
+ import { entityType } from "./symbols.mjs";
2
+ import { EnumedColumn, EnumedConfig } from "./columns/enum.mjs";
3
+
4
+ //#region src/enumtype.d.ts
5
+ declare class Enum<U extends string = string, TValues extends Readonly<[U, ...U[]]> = Readonly<[U, ...U[]]>> {
6
+ #private;
7
+ static readonly [entityType] = "Enum";
8
+ readonly TValue: TValues[number];
9
+ constructor(schema: string, name: string, values: TValues);
10
+ get schema(): string;
11
+ get name(): string;
12
+ get values(): TValues;
13
+ enumed<TConfig extends EnumedConfig>(config: TConfig): EnumedColumn<TValues[number], TConfig>;
14
+ }
15
+ /**
16
+ * Creates a typed enum definition.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * import { enumtype } from "durcno";
21
+ *
22
+ * export const UserTypeEnm = enumtype("public", "user_type", ["admin", "user"]);
23
+ * ```
24
+ */
25
+ declare function enumtype<U extends string, TValues extends Readonly<[U, ...U[]]>>(schema: string, name: string, values: TValues): Enum<string, TValues>;
26
+ //#endregion
27
+ export { Enum, enumtype };
@@ -0,0 +1,42 @@
1
+ import { entityType } from "./symbols.mjs";
2
+ import { enumed } from "./columns/enum.mjs";
3
+ //#region src/enumtype.ts
4
+ var Enum = class {
5
+ static [entityType] = "Enum";
6
+ TValue;
7
+ #schema;
8
+ #name;
9
+ #values;
10
+ constructor(schema, name, values) {
11
+ this.#schema = schema;
12
+ this.#name = name;
13
+ this.#values = values;
14
+ }
15
+ get schema() {
16
+ return this.#schema;
17
+ }
18
+ get name() {
19
+ return this.#name;
20
+ }
21
+ get values() {
22
+ return this.#values;
23
+ }
24
+ enumed(config) {
25
+ return enumed(this, config);
26
+ }
27
+ };
28
+ /**
29
+ * Creates a typed enum definition.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * import { enumtype } from "durcno";
34
+ *
35
+ * export const UserTypeEnm = enumtype("public", "user_type", ["admin", "user"]);
36
+ * ```
37
+ */
38
+ function enumtype(schema, name, values) {
39
+ return new Enum(schema, name, values);
40
+ }
41
+ //#endregion
42
+ export { Enum, enumtype };
@@ -0,0 +1,97 @@
1
+ import { Query } from "../query-builders/query.mjs";
2
+ import { Filter } from "./custom.mjs";
3
+ import { AnyColumn, TableColumn } from "../table.mjs";
4
+
5
+ //#region src/filters/array.d.ts
6
+ /** Shorthand for the Filter's TableColumn constraint. */
7
+ type AnyTC = TableColumn<any, any, any, AnyColumn>;
8
+ /**
9
+ * Helper type to extract the element type from an array type.
10
+ * Works with regular arrays, tuples, and nested arrays.
11
+ */
12
+ type ArrayElement<T> = T extends readonly (infer E)[] ? E extends readonly unknown[] ? ArrayElement<E> : E : T;
13
+ /**
14
+ * ArrayContains filter: col @> ARRAY[values]
15
+ * Returns true if the array column contains all the specified values.
16
+ */
17
+ declare class ArrayContainsFilter<TCol extends AnyTC, TVal extends ArrayElement<TCol["ValType"]>> extends Filter<TCol> {
18
+ readonly left: TCol;
19
+ readonly right: TVal[];
20
+ constructor(column: TCol, values: TVal[]);
21
+ toSQL(): string;
22
+ toQuery(query: Query): void;
23
+ }
24
+ /**
25
+ * Creates a condition that checks if an array column contains all specified values.
26
+ * SQL: column @> ARRAY[values]
27
+ *
28
+ * @example
29
+ * db.from(Posts).select().where(arrayContains(Posts.tags, ['typescript', 'postgres']))
30
+ */
31
+ declare function arrayContains<TCol extends AnyTC, TVal extends ArrayElement<TCol["ValType"]>>(column: TCol, values: TVal[]): ArrayContainsFilter<TCol, TVal>;
32
+ /**
33
+ * ArrayContainedBy filter: col <@ ARRAY[values]
34
+ * Returns true if the array column is contained by (is a subset of) the specified values.
35
+ */
36
+ declare class ArrayContainedByFilter<TCol extends AnyTC, TVal extends ArrayElement<TCol["ValType"]>> extends Filter<TCol> {
37
+ readonly left: TCol;
38
+ readonly right: TVal[];
39
+ constructor(column: TCol, values: TVal[]);
40
+ toSQL(): string;
41
+ toQuery(query: Query): void;
42
+ }
43
+ /**
44
+ * Creates a condition that checks if an array column is contained by the specified values.
45
+ * SQL: column <@ ARRAY[values]
46
+ */
47
+ declare function arrayContainedBy<TCol extends AnyTC, TVal extends ArrayElement<TCol["ValType"]>>(column: TCol, values: TVal[]): ArrayContainedByFilter<TCol, TVal>;
48
+ /**
49
+ * ArrayOverlaps filter: col && ARRAY[values]
50
+ * Returns true if the arrays have any elements in common.
51
+ */
52
+ declare class ArrayOverlapsFilter<TCol extends AnyTC, TVal extends ArrayElement<TCol["ValType"]>> extends Filter<TCol> {
53
+ readonly left: TCol;
54
+ readonly right: TVal[];
55
+ constructor(column: TCol, values: TVal[]);
56
+ toSQL(): string;
57
+ toQuery(query: Query): void;
58
+ }
59
+ /**
60
+ * Creates a condition that checks if an array column overlaps with the specified values.
61
+ * SQL: column && ARRAY[values]
62
+ */
63
+ declare function arrayOverlaps<TCol extends AnyTC, TVal extends ArrayElement<TCol["ValType"]>>(column: TCol, values: TVal[]): ArrayOverlapsFilter<TCol, TVal>;
64
+ /**
65
+ * ArrayHas filter: value = ANY(col)
66
+ * Returns true if the value exists in the array column.
67
+ */
68
+ declare class ArrayHasFilter<TCol extends AnyTC, TVal extends ArrayElement<TCol["ValType"]>> extends Filter<TCol> {
69
+ readonly left: TCol;
70
+ readonly right: TVal;
71
+ constructor(column: TCol, value: TVal);
72
+ toSQL(): string;
73
+ toQuery(query: Query): void;
74
+ }
75
+ /**
76
+ * Creates a condition that checks if a value exists in an array column.
77
+ * SQL: value = ANY(column)
78
+ */
79
+ declare function arrayHas<TCol extends AnyTC, TVal extends ArrayElement<TCol["ValType"]>>(column: TCol, value: TVal): ArrayHasFilter<TCol, TVal>;
80
+ /**
81
+ * ArrayAll filter: value = ALL(col)
82
+ * Returns true if all elements in the array column equal the value.
83
+ */
84
+ declare class ArrayAllFilter<TCol extends AnyTC, TVal extends ArrayElement<TCol["ValType"]>> extends Filter<TCol> {
85
+ readonly left: TCol;
86
+ readonly right: TVal;
87
+ constructor(column: TCol, value: TVal);
88
+ toSQL(): string;
89
+ toQuery(query: Query): void;
90
+ }
91
+ /**
92
+ * Creates a condition that checks if all elements in an array column equal a value.
93
+ * SQL: value = ALL(column)
94
+ */
95
+ declare function arrayAll<TCol extends AnyTC, TVal extends ArrayElement<TCol["ValType"]>>(column: TCol, value: TVal): ArrayAllFilter<TCol, TVal>;
96
+ //#endregion
97
+ export { arrayAll, arrayContainedBy, arrayContains, arrayHas, arrayOverlaps };