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
+ import { isCol } from "./entity.mjs";
2
+ //#region src/sql.ts
3
+ var Sql = class {
4
+ #string;
5
+ constructor(sql) {
6
+ this.#string = sql;
7
+ }
8
+ get string() {
9
+ return this.#string;
10
+ }
11
+ toSQL() {
12
+ return this.#string;
13
+ }
14
+ toQuery(query) {
15
+ query.sql += this.#string;
16
+ }
17
+ };
18
+ function sql(strings, ...params) {
19
+ let s = "";
20
+ strings.forEach((str, i) => {
21
+ s += str;
22
+ const param = params[i];
23
+ if (param !== void 0) s += toSqlValue(param);
24
+ });
25
+ return new Sql(s);
26
+ }
27
+ function toSqlValue(value) {
28
+ if (value === null || value === void 0) return "NULL";
29
+ else if (typeof value === "string") return `'${value.replace(/'/g, "''")}'`;
30
+ else if (typeof value === "number" || typeof value === "bigint") return value.toString();
31
+ else if (typeof value === "boolean") return value ? "'1'" : "'0'";
32
+ else if (isCol(value)) return value.fullName;
33
+ else return "NULL";
34
+ }
35
+ //#endregion
36
+ export { Sql, sql };
@@ -0,0 +1,4 @@
1
+ //#region src/symbols.d.ts
2
+ declare const entityType: unique symbol;
3
+ //#endregion
4
+ export { entityType };
@@ -0,0 +1,4 @@
1
+ //#region src/symbols.ts
2
+ const entityType = Symbol.for("durcno:entityType");
3
+ //#endregion
4
+ export { entityType };
@@ -0,0 +1,144 @@
1
+ import { entityType } from "./symbols.mjs";
2
+ import { Column } from "./columns/common.mjs";
3
+ import { Check, CheckBuilder, CheckExpr } from "./constraints/check.mjs";
4
+ import { PrimaryKeyConstraint, PrimaryKeyConstraintFn } from "./constraints/primary-key.mjs";
5
+ import { UniqueConstraint, UniqueConstraintFn } from "./constraints/unique.mjs";
6
+ import { Index } from "./indexes.mjs";
7
+ import { CamelToSnake } from "./types.mjs";
8
+ import { Config } from "./index.mjs";
9
+
10
+ //#region src/table.d.ts
11
+ type AnyColumn = Column<any, any>;
12
+ type InferTColArgs<TColumns extends Record<string, unknown>> = { [ColName in keyof TColumns]: TColumns[ColName] extends TableColumn<infer TTSchema, infer TTName, infer TCName, infer TColumn> ? [TTSchema, TTName, TCName, TColumn] : never };
13
+ type TColsToLeftRight<TColumns extends Record<string, AnyTableColumn>> = { [ColName in keyof TColumns]: TColumns[ColName] extends TableColumn<infer UTSchema, infer UTName, infer UCName, infer UColumn> ? {
14
+ left: [UTSchema, UTName, UCName, UColumn];
15
+ right: InferTColArgs<Omit<TColumns, UCName>>;
16
+ } : never };
17
+ type TableExtra<TSchema extends string, TName extends string, TColumns extends Record<string, AnyColumn>> = {
18
+ indexes?: (...args: [TableWithColumns<TSchema, TName, TColumns>]) => Index<TColumns[keyof TColumns]>[];
19
+ primaryKeyConstraint?: (table: TableWithColumns<TSchema, TName, TColumns>, primaryKey: PrimaryKeyConstraintFn) => PrimaryKeyConstraint;
20
+ uniqueConstraints?: (table: TableWithColumns<TSchema, TName, TColumns>, unique: UniqueConstraintFn) => UniqueConstraint[];
21
+ checkConstraints?: (table: TableWithColumns<TSchema, TName, TColumns>, check: (name: string, expr: CheckExpr) => Check, c: CheckBuilder) => Check[];
22
+ };
23
+ type TableConfig<TSchema extends string, TName extends string, TColumns extends Record<string, AnyColumn>> = {
24
+ readonly name: TName;
25
+ readonly schema: TSchema;
26
+ readonly fullName: `"${TSchema}"."${TName}"`;
27
+ readonly cols: TColumns;
28
+ readonly columns: { [ColName in keyof TColumns]: TableColumn<TSchema, TName, ColName, TColumns[ColName]> };
29
+ readonly extra: TableExtra<TSchema, TName, TColumns>;
30
+ config?: Config;
31
+ };
32
+ type TableColumn<TTSchema extends string, TTName extends string, TName extends Key, TColumn extends AnyColumn> = TColumn & {
33
+ table: StdTable;
34
+ name: TName;
35
+ nameSnake: CamelToSnake<Extract<TName, string>>;
36
+ tableName: TTName;
37
+ schemaName: TTSchema;
38
+ };
39
+ type StdTableColumn = TableColumn<string, string, Key, AnyColumn>;
40
+ type AnyTableColumn = TableColumn<any, any, any, any>;
41
+ type TableColumnArgs = [string, string, Key, AnyColumn];
42
+ type BuildScmTblColumns<TSchema extends string, TName extends string, TColumns extends Record<string, AnyColumn>> = { [ColName in keyof TColumns & string as `${TSchema}_${TName}_${ColName}`]: TableColumn<TSchema, TName, ColName, TColumns[ColName]> };
43
+ declare class Table<TSchema extends string, TName extends string, TColumns extends Record<string, AnyColumn>> {
44
+ static readonly [entityType] = "Table";
45
+ readonly $: {
46
+ inferSelect: { [ColName in keyof TColumns]: TColumns[ColName]["ValTypeSelect"] };
47
+ _: {
48
+ scmTblColumns: BuildScmTblColumns<TSchema, TName, TColumns>;
49
+ };
50
+ };
51
+ _: TableConfig<TSchema, TName, TColumns>;
52
+ constructor(schema: TSchema, name: TName, columns: TColumns, extra: TableExtra<TSchema, TName, TColumns>);
53
+ }
54
+ type StdTable = Table<string, string, Record<string, AnyColumn>>;
55
+ type TableWithColumns<TTSchema extends string, TTName extends string, TColumns extends Record<string, AnyColumn>> = Table<TTSchema, TTName, TColumns> & Table<TTSchema, TTName, TColumns>["_"]["columns"];
56
+ type AnyTableWithColumns = TableWithColumns<any, any, any>;
57
+ type TableWCorNever<T> = T extends TableWithColumns<infer TTSchema, infer TTName, infer TColumns> ? TColumns extends Record<string, AnyColumn> ? TableWithColumns<TTSchema, TTName, TColumns> : never : never;
58
+ type IsTableWC<T> = T extends TableWithColumns<any, any, any> ? true : false;
59
+ /**
60
+ * Creates a typed table definition with column accessors.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * import { table, pk, varchar } from "durcno";
65
+ *
66
+ * export const Users = table("public", "users", {
67
+ * id: pk(),
68
+ * email: varchar({ length: 255 }),
69
+ * });
70
+ * ```
71
+ */
72
+ declare function table<TSchema extends string, TName extends string, TColumns extends Record<string, AnyColumn>>(schema: TSchema, name: TName, columns: TColumns, extra?: TableExtra<TSchema, TName, TColumns>): TableWithColumns<TSchema, TName, TColumns>;
73
+ declare class One<TTSchema extends string, TTName extends string, TTColumns extends Record<string, AnyColumn>, TCol extends TableWithColumns<TTSchema, TTName, TTColumns>[keyof TTColumns]> {
74
+ readonly t: "One";
75
+ readonly table: TableWithColumns<TTSchema, TTName, TTColumns>;
76
+ readonly col: TCol;
77
+ constructor(table: TableWithColumns<TTSchema, TTName, TTColumns>, col: TCol);
78
+ }
79
+ /**
80
+ * Defines a one-to-one relation where the foreign key is on the **related** table.
81
+ *
82
+ * @param table - The related table that holds the foreign key column
83
+ * @param col - The foreign key column on the related table
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * export const UsersRelations = relations(Users, () => ({
88
+ * profile: one(UserProfiles, UserProfiles.userId),
89
+ * }));
90
+ * ```
91
+ */
92
+ declare function one<TTSchema extends string, TTName extends string, TTColumns extends Record<string, AnyColumn>, TCol extends TableWithColumns<TTSchema, TTName, TTColumns>[keyof TTColumns]>(table: TableWithColumns<TTSchema, TTName, TTColumns>, col: TCol): One<TTSchema, TTName, TTColumns, TCol>;
93
+ declare class Many<TTSchema extends string, TTName extends string, TTColumns extends Record<string, AnyColumn>, TCol extends TableWithColumns<TTSchema, TTName, TTColumns>[keyof TTColumns]> {
94
+ readonly t: "Many";
95
+ readonly table: TableWithColumns<TTSchema, TTName, TTColumns>;
96
+ readonly col: TCol;
97
+ constructor(table: TableWithColumns<TTSchema, TTName, TTColumns>, col: TCol);
98
+ }
99
+ /**
100
+ * Defines a one-to-many relation where the foreign key is on the **target** table.
101
+ *
102
+ * @param table - The target table that contains the foreign key
103
+ * @param col - The foreign key column on the target table referencing this table
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * export const UsersRelations = relations(Users, () => ({
108
+ * posts: many(Posts, Posts.userId),
109
+ * }));
110
+ * ```
111
+ */
112
+ declare function many<TTSchema extends string, TTName extends string, TTColumns extends Record<string, AnyColumn>, TCol extends TableWithColumns<TTSchema, TTName, TTColumns>[keyof TTColumns]>(table: TableWithColumns<TTSchema, TTName, TTColumns>, col: TCol): Many<TTSchema, TTName, TTColumns, TCol>;
113
+ declare class Fk<TTSchema extends string, TTName extends string, TTColumns extends Record<string, AnyColumn>, TCol extends TableColumn<any, any, any, AnyColumn>> {
114
+ readonly t: "Fk";
115
+ readonly col: TCol;
116
+ readonly table: TableWithColumns<TTSchema, TTName, TTColumns>;
117
+ constructor(col: TCol, table: TableWithColumns<TTSchema, TTName, TTColumns>);
118
+ }
119
+ /**
120
+ * Defines a many-to-one relation where the foreign key is on the **current** table.
121
+ *
122
+ * @param col - The foreign key column on the current table
123
+ * @param table - The referenced table that the foreign key points to
124
+ *
125
+ * @example
126
+ * ```ts
127
+ * export const PostsRelations = relations(Posts, () => ({
128
+ * author: fk(Posts.userId, Users),
129
+ * }));
130
+ * ```
131
+ */
132
+ declare function fk<TTSchema extends string, TTName extends string, TTColumns extends Record<string, AnyColumn>, TCol extends TableColumn<any, any, any, AnyColumn>>(col: TCol, table: TableWithColumns<TTSchema, TTName, TTColumns>): Fk<TTSchema, TTName, TTColumns, TCol>;
133
+ type AnyRelation = Many<any, any, any, any> | One<any, any, any, any> | Fk<any, any, any, any>;
134
+ declare class Relations<TTSchema extends string, TTName extends string, TTColumns extends Record<string, AnyColumn>, TRelations extends Record<string, AnyRelation>> {
135
+ static readonly [entityType] = "Relation";
136
+ readonly table: TableWithColumns<TTSchema, TTName, TTColumns>;
137
+ readonly map: TRelations;
138
+ constructor(table: TableWithColumns<TTSchema, TTName, TTColumns>, relations: TRelations);
139
+ }
140
+ type StdRelations = Relations<string, string, Record<string, AnyColumn>, Record<string, Many<string, string, Record<string, AnyColumn>, StdTableColumn> | One<string, string, Record<string, AnyColumn>, StdTableColumn> | Fk<string, string, Record<string, AnyColumn>, StdTableColumn>>>;
141
+ type AnyRelations = Relations<any, any, Record<any, any>, Record<any, any>>;
142
+ declare function relations<TTSchema extends string, TTName extends string, TColumns extends Record<string, AnyColumn>, TRelations extends Record<string, AnyRelation>>(table: TableWithColumns<TTSchema, TTName, TColumns>, relations: () => TRelations): () => Relations<TTSchema, TTName, TColumns, TRelations>;
143
+ //#endregion
144
+ export { AnyColumn, AnyRelation, AnyRelations, AnyTableColumn, AnyTableWithColumns, BuildScmTblColumns, Fk, IsTableWC, Many, Relations, StdRelations, StdTable, StdTableColumn, TColsToLeftRight, Table, TableColumn, TableColumnArgs, TableWCorNever, TableWithColumns, fk, many, one, relations, table };
@@ -0,0 +1,134 @@
1
+ import { entityType } from "./symbols.mjs";
2
+ //#region src/table.ts
3
+ function bindNameNTable(table, columns) {
4
+ for (const [ColName, col] of Object.entries(columns)) {
5
+ col._.setName(ColName);
6
+ col._.setTable(table);
7
+ }
8
+ return columns;
9
+ }
10
+ var Table = class {
11
+ static [entityType] = "Table";
12
+ $;
13
+ _;
14
+ constructor(schema, name, columns, extra) {
15
+ bindNameNTable(this, columns);
16
+ this._ = {
17
+ schema,
18
+ name,
19
+ fullName: `"${schema}"."${name}"`,
20
+ cols: columns,
21
+ columns,
22
+ extra
23
+ };
24
+ }
25
+ };
26
+ /**
27
+ * Creates a typed table definition with column accessors.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * import { table, pk, varchar } from "durcno";
32
+ *
33
+ * export const Users = table("public", "users", {
34
+ * id: pk(),
35
+ * email: varchar({ length: 255 }),
36
+ * });
37
+ * ```
38
+ */
39
+ function table(schema, name, columns, extra) {
40
+ const table = new Table(schema, name, columns, extra ?? {});
41
+ for (const colName in table._.columns) Object.defineProperty(table, colName, { get() {
42
+ return table._.columns[colName];
43
+ } });
44
+ return table;
45
+ }
46
+ var One = class {
47
+ t = "One";
48
+ table;
49
+ col;
50
+ constructor(table, col) {
51
+ this.table = table;
52
+ this.col = col;
53
+ }
54
+ };
55
+ /**
56
+ * Defines a one-to-one relation where the foreign key is on the **related** table.
57
+ *
58
+ * @param table - The related table that holds the foreign key column
59
+ * @param col - The foreign key column on the related table
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * export const UsersRelations = relations(Users, () => ({
64
+ * profile: one(UserProfiles, UserProfiles.userId),
65
+ * }));
66
+ * ```
67
+ */
68
+ function one(table, col) {
69
+ return new One(table, col);
70
+ }
71
+ var Many = class {
72
+ t = "Many";
73
+ table;
74
+ col;
75
+ constructor(table, col) {
76
+ this.table = table;
77
+ this.col = col;
78
+ }
79
+ };
80
+ /**
81
+ * Defines a one-to-many relation where the foreign key is on the **target** table.
82
+ *
83
+ * @param table - The target table that contains the foreign key
84
+ * @param col - The foreign key column on the target table referencing this table
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * export const UsersRelations = relations(Users, () => ({
89
+ * posts: many(Posts, Posts.userId),
90
+ * }));
91
+ * ```
92
+ */
93
+ function many(table, col) {
94
+ return new Many(table, col);
95
+ }
96
+ var Fk = class {
97
+ t = "Fk";
98
+ col;
99
+ table;
100
+ constructor(col, table) {
101
+ this.col = col;
102
+ this.table = table;
103
+ }
104
+ };
105
+ /**
106
+ * Defines a many-to-one relation where the foreign key is on the **current** table.
107
+ *
108
+ * @param col - The foreign key column on the current table
109
+ * @param table - The referenced table that the foreign key points to
110
+ *
111
+ * @example
112
+ * ```ts
113
+ * export const PostsRelations = relations(Posts, () => ({
114
+ * author: fk(Posts.userId, Users),
115
+ * }));
116
+ * ```
117
+ */
118
+ function fk(col, table) {
119
+ return new Fk(col, table);
120
+ }
121
+ var Relations = class {
122
+ static [entityType] = "Relation";
123
+ table;
124
+ map;
125
+ constructor(table, relations) {
126
+ this.table = table;
127
+ this.map = relations;
128
+ }
129
+ };
130
+ function relations(table, relations) {
131
+ return () => new Relations(table, relations());
132
+ }
133
+ //#endregion
134
+ export { Relations, Table, fk, many, one, relations, table };
@@ -0,0 +1,4 @@
1
+ //#region src/types.d.ts
2
+ type CamelToSnake<S extends string> = S extends `${infer Head}${infer Tail}` ? Head extends Uppercase<Head> ? `_${Lowercase<Head>}${CamelToSnake<Tail>}` : `${Head}${CamelToSnake<Tail>}` : S;
3
+ //#endregion
4
+ export { CamelToSnake };
@@ -0,0 +1,10 @@
1
+ //#region src/utils.ts
2
+ /** biome-ignore-all lint/suspicious/noExplicitAny: <> */
3
+ function camelToSnake(str) {
4
+ return str.replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2").replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
5
+ }
6
+ function snakeToCamel(str) {
7
+ return str.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
8
+ }
9
+ //#endregion
10
+ export { camelToSnake, snakeToCamel };
@@ -0,0 +1,23 @@
1
+ import { AnyColumn, TableWithColumns } from "../table.mjs";
2
+ import * as z from "zod";
3
+
4
+ //#region src/validators/zod.d.ts
5
+ type BuildZodRefine<TColumns extends Record<string, AnyColumn>> = { [K in keyof TColumns]?: (...args: [TColumns[K]["zodType"]]) => z.ZodType };
6
+ type InferColInsertZodType<TColumn extends AnyColumn, ZType extends z.ZodType> = TColumn extends {
7
+ generated: "ALWAYS";
8
+ } ? never : TColumn extends {
9
+ generated: "BY DEFAULT";
10
+ } ? z.ZodOptional<ZType> : TColumn extends {
11
+ hasDefault: true;
12
+ } ? z.ZodOptional<ZType> : TColumn extends {
13
+ hasDefaultFn: true;
14
+ } ? z.ZodOptional<ZType> : TColumn extends {
15
+ isNotNull: true;
16
+ } ? ZType : z.ZodOptional<ZType>;
17
+ type InferInsertZodShape<TColumns extends Record<string, AnyColumn>, TRefine extends BuildZodRefine<TColumns> | undefined> = { [K in keyof TColumns]: TRefine extends {} ? TRefine[K] extends ((...args: any[]) => infer U) ? U extends z.ZodType ? InferColInsertZodType<TColumns[K], U> : never : InferColInsertZodType<TColumns[K], TColumns[K]["zodType"]> : InferColInsertZodType<TColumns[K], TColumns[K]["zodType"]> } extends infer U ? { [K in keyof U as U[K] extends never ? never : K]: U[K] } : never;
18
+ declare function createInsertSchema<TTSchema extends string, TTName extends string, TColumns extends Record<string, AnyColumn>, TRefine extends BuildZodRefine<TColumns>>(table: TableWithColumns<TTSchema, TTName, TColumns>, refine?: TRefine): z.ZodObject<InferInsertZodShape<TColumns, TRefine>>;
19
+ type InferColUpdateZodType<TColumn extends AnyColumn, ZType extends z.ZodType> = TColumn["isPrimaryKey"] extends true ? never : TColumn["isNotNull"] extends true ? z.ZodOptional<ZType> : z.ZodOptional<z.ZodNullable<ZType>>;
20
+ type InferUpdateZodShape<TColumns extends Record<string, AnyColumn>, TRefine extends BuildZodRefine<TColumns> | undefined> = { [K in keyof TColumns]: TRefine extends {} ? TRefine[K] extends ((...args: any[]) => infer U) ? U extends z.ZodType ? InferColUpdateZodType<TColumns[K], U> : never : InferColUpdateZodType<TColumns[K], TColumns[K]["zodType"]> : InferColUpdateZodType<TColumns[K], TColumns[K]["zodType"]> } extends infer U ? { [K in keyof U as U[K] extends never ? never : K]: U[K] } : never;
21
+ declare function createUpdateSchema<TTSchema extends string, TTName extends string, TColumns extends Record<string, AnyColumn>, TRefine extends BuildZodRefine<TColumns>>(table: TableWithColumns<TTSchema, TTName, TColumns>, refine?: TRefine): z.ZodObject<InferUpdateZodShape<TColumns, TRefine>>;
22
+ //#endregion
23
+ export { BuildZodRefine, createInsertSchema, createUpdateSchema };
@@ -0,0 +1,27 @@
1
+ import * as z from "zod";
2
+ //#region src/validators/zod.ts
3
+ function createInsertSchema(table, refine) {
4
+ const shape = {};
5
+ for (const [columnName, column] of Object.entries(table._.columns)) {
6
+ if (column.generated === "ALWAYS") continue;
7
+ let zodSchema = column.zodType;
8
+ if (refine?.[columnName]) zodSchema = refine[columnName](column.zodType);
9
+ if (column.generated === "BY DEFAULT" || !column.isNotNull || column.hasDefault || column.hasInsertFn) zodSchema = zodSchema.optional();
10
+ shape[columnName] = zodSchema;
11
+ }
12
+ return z.object(shape);
13
+ }
14
+ function createUpdateSchema(table, refine) {
15
+ const shape = {};
16
+ for (const [columnName, column] of Object.entries(table._.columns)) {
17
+ if (column.isPrimaryKey) continue;
18
+ let zodSchema = column.zodType;
19
+ if (refine?.[columnName]) zodSchema = refine[columnName](column.zodType);
20
+ if (!column.isNotNull) zodSchema = zodSchema.nullable();
21
+ zodSchema = zodSchema.optional();
22
+ shape[columnName] = zodSchema;
23
+ }
24
+ return z.object(shape);
25
+ }
26
+ //#endregion
27
+ export { createInsertSchema, createUpdateSchema };
@@ -0,0 +1,56 @@
1
+ //#region src/wkx/binaryreader.ts
2
+ var BinaryReader = class {
3
+ buffer;
4
+ position;
5
+ isBigEndian;
6
+ constructor(buffer, isBigEndian = false) {
7
+ this.buffer = buffer;
8
+ this.position = 0;
9
+ this.isBigEndian = isBigEndian;
10
+ }
11
+ _read(readLE, readBE, size) {
12
+ let value;
13
+ if (this.isBigEndian) value = readBE.call(this.buffer, this.position);
14
+ else value = readLE.call(this.buffer, this.position);
15
+ this.position += size;
16
+ return value;
17
+ }
18
+ readUInt8() {
19
+ return this._read(Buffer.prototype.readUInt8, Buffer.prototype.readUInt8, 1);
20
+ }
21
+ readUInt16() {
22
+ return this._read(Buffer.prototype.readUInt16LE, Buffer.prototype.readUInt16BE, 2);
23
+ }
24
+ readUInt32() {
25
+ return this._read(Buffer.prototype.readUInt32LE, Buffer.prototype.readUInt32BE, 4);
26
+ }
27
+ readInt8() {
28
+ return this._read(Buffer.prototype.readInt8, Buffer.prototype.readInt8, 1);
29
+ }
30
+ readInt16() {
31
+ return this._read(Buffer.prototype.readInt16LE, Buffer.prototype.readInt16BE, 2);
32
+ }
33
+ readInt32() {
34
+ return this._read(Buffer.prototype.readInt32LE, Buffer.prototype.readInt32BE, 4);
35
+ }
36
+ readFloat() {
37
+ return this._read(Buffer.prototype.readFloatLE, Buffer.prototype.readFloatBE, 4);
38
+ }
39
+ readDouble() {
40
+ return this._read(Buffer.prototype.readDoubleLE, Buffer.prototype.readDoubleBE, 8);
41
+ }
42
+ readVarInt() {
43
+ let nextByte;
44
+ let result = 0;
45
+ let bytesRead = 0;
46
+ do {
47
+ nextByte = this.buffer[this.position + bytesRead];
48
+ result += (nextByte & 127) << 7 * bytesRead;
49
+ bytesRead++;
50
+ } while (nextByte >= 128);
51
+ this.position += bytesRead;
52
+ return result;
53
+ }
54
+ };
55
+ //#endregion
56
+ export { BinaryReader };
@@ -0,0 +1,110 @@
1
+ import { __esmMin } from "../_virtual/_rolldown/runtime.mjs";
2
+ //#region src/wkx/binarywriter.ts
3
+ var BinaryWriter;
4
+ var init_binarywriter = __esmMin((() => {
5
+ BinaryWriter = class {
6
+ buffer;
7
+ position;
8
+ allowResize;
9
+ constructor(size, allowResize = false) {
10
+ this.buffer = Buffer.alloc(size);
11
+ this.position = 0;
12
+ this.allowResize = allowResize;
13
+ }
14
+ ensureSize(size) {
15
+ if (this.buffer.length < this.position + size) if (this.allowResize) {
16
+ const tempBuffer = Buffer.alloc(this.position + size);
17
+ this.buffer.copy(tempBuffer, 0, 0, this.buffer.length);
18
+ this.buffer = tempBuffer;
19
+ } else throw new RangeError("index out of range");
20
+ }
21
+ writeUInt8(value) {
22
+ this.ensureSize(1);
23
+ this.buffer.writeUInt8(value, this.position);
24
+ this.position += 1;
25
+ }
26
+ writeUInt16LE(value) {
27
+ this.ensureSize(2);
28
+ this.buffer.writeUInt16LE(value, this.position);
29
+ this.position += 2;
30
+ }
31
+ writeUInt16BE(value) {
32
+ this.ensureSize(2);
33
+ this.buffer.writeUInt16BE(value, this.position);
34
+ this.position += 2;
35
+ }
36
+ writeUInt32LE(value, noAssert) {
37
+ this.ensureSize(4);
38
+ this.buffer.writeUInt32LE(value, this.position);
39
+ this.position += 4;
40
+ }
41
+ writeUInt32BE(value) {
42
+ this.ensureSize(4);
43
+ this.buffer.writeUInt32BE(value, this.position);
44
+ this.position += 4;
45
+ }
46
+ writeInt8(value) {
47
+ this.ensureSize(1);
48
+ this.buffer.writeInt8(value, this.position);
49
+ this.position += 1;
50
+ }
51
+ writeInt16LE(value) {
52
+ this.ensureSize(2);
53
+ this.buffer.writeInt16LE(value, this.position);
54
+ this.position += 2;
55
+ }
56
+ writeInt16BE(value) {
57
+ this.ensureSize(2);
58
+ this.buffer.writeInt16BE(value, this.position);
59
+ this.position += 2;
60
+ }
61
+ writeInt32LE(value) {
62
+ this.ensureSize(4);
63
+ this.buffer.writeInt32LE(value, this.position);
64
+ this.position += 4;
65
+ }
66
+ writeInt32BE(value) {
67
+ this.ensureSize(4);
68
+ this.buffer.writeInt32BE(value, this.position);
69
+ this.position += 4;
70
+ }
71
+ writeFloatLE(value) {
72
+ this.ensureSize(4);
73
+ this.buffer.writeFloatLE(value, this.position);
74
+ this.position += 4;
75
+ }
76
+ writeFloatBE(value) {
77
+ this.ensureSize(4);
78
+ this.buffer.writeFloatBE(value, this.position);
79
+ this.position += 4;
80
+ }
81
+ writeDoubleLE(value) {
82
+ this.ensureSize(8);
83
+ this.buffer.writeDoubleLE(value, this.position);
84
+ this.position += 8;
85
+ }
86
+ writeDoubleBE(value) {
87
+ this.ensureSize(8);
88
+ this.buffer.writeDoubleBE(value, this.position);
89
+ this.position += 8;
90
+ }
91
+ writeBuffer(buffer) {
92
+ this.ensureSize(buffer.length);
93
+ buffer.copy(this.buffer, this.position, 0, buffer.length);
94
+ this.position += buffer.length;
95
+ }
96
+ writeVarInt(value) {
97
+ let length = 1;
98
+ while ((value & 4294967168) !== 0) {
99
+ this.writeUInt8(value & 127 | 128);
100
+ value >>>= 7;
101
+ length++;
102
+ }
103
+ this.writeUInt8(value & 127);
104
+ return length;
105
+ }
106
+ };
107
+ }));
108
+ //#endregion
109
+ init_binarywriter();
110
+ export { BinaryWriter, init_binarywriter };
@@ -0,0 +1,101 @@
1
+ import { __esmMin } from "../_virtual/_rolldown/runtime.mjs";
2
+ import { BinaryWriter, init_binarywriter } from "./binarywriter.mjs";
3
+ import { ZigZag, init_zigzag } from "./zigzag.mjs";
4
+ //#region src/wkx/geometry.ts
5
+ var Geometry;
6
+ var init_geometry = __esmMin((() => {
7
+ init_binarywriter();
8
+ init_zigzag();
9
+ Geometry = class {
10
+ srid;
11
+ hasZ = false;
12
+ hasM = false;
13
+ static parse;
14
+ static parseTwkb;
15
+ static parseGeoJSON;
16
+ static _parseWkt;
17
+ static _parseWkb;
18
+ static _parseGeoJSON;
19
+ toEwkt() {
20
+ return "SRID=" + this.srid + ";" + this.toWkt();
21
+ }
22
+ toEwkb() {
23
+ const ewkb = new BinaryWriter(this._getWkbSize() + 4);
24
+ const wkb = this.toWkb();
25
+ ewkb.writeInt8(1);
26
+ ewkb.writeUInt32LE((wkb.slice(1, 5).readUInt32LE(0) | 536870912) >>> 0, true);
27
+ ewkb.writeUInt32LE(this.srid || 0);
28
+ ewkb.writeBuffer(wkb.slice(5));
29
+ return ewkb.buffer;
30
+ }
31
+ _getWktType(wktType, isEmpty) {
32
+ let wkt = wktType;
33
+ if (this.hasZ && this.hasM) wkt += " ZM ";
34
+ else if (this.hasZ) wkt += " Z ";
35
+ else if (this.hasM) wkt += " M ";
36
+ if (isEmpty && !this.hasZ && !this.hasM) wkt += " ";
37
+ if (isEmpty) wkt += "EMPTY";
38
+ return wkt;
39
+ }
40
+ _getWktCoordinate(point) {
41
+ let coordinates = point.x + " " + point.y;
42
+ if (this.hasZ) coordinates += " " + point.z;
43
+ if (this.hasM) coordinates += " " + point.m;
44
+ return coordinates;
45
+ }
46
+ _writeWkbType(wkb, geometryType, parentOptions) {
47
+ let dimensionType = 0;
48
+ if (typeof this.srid === "undefined" && (!parentOptions || typeof parentOptions.srid === "undefined")) {
49
+ if (this.hasZ && this.hasM) dimensionType += 3e3;
50
+ else if (this.hasZ) dimensionType += 1e3;
51
+ else if (this.hasM) dimensionType += 2e3;
52
+ } else {
53
+ if (this.hasZ) dimensionType |= 2147483648;
54
+ if (this.hasM) dimensionType |= 1073741824;
55
+ }
56
+ wkb.writeUInt32LE(dimensionType + geometryType >>> 0, true);
57
+ }
58
+ static getTwkbPrecision(xyPrecision, zPrecision, mPrecision) {
59
+ return {
60
+ xy: xyPrecision,
61
+ z: zPrecision,
62
+ m: mPrecision,
63
+ xyFactor: 10 ** xyPrecision,
64
+ zFactor: 10 ** zPrecision,
65
+ mFactor: 10 ** mPrecision
66
+ };
67
+ }
68
+ _writeTwkbHeader(twkb, geometryType, precision, isEmpty) {
69
+ const type = (ZigZag.encode(precision.xy) << 4) + geometryType;
70
+ let metadataHeader = (this.hasZ || this.hasM ? 1 : 0) << 3;
71
+ metadataHeader += (isEmpty ? 1 : 0) << 4;
72
+ twkb.writeUInt8(type);
73
+ twkb.writeUInt8(metadataHeader);
74
+ if (this.hasZ || this.hasM) {
75
+ let extendedPrecision = 0;
76
+ if (this.hasZ) extendedPrecision |= 1;
77
+ if (this.hasM) extendedPrecision |= 2;
78
+ twkb.writeUInt8(extendedPrecision);
79
+ }
80
+ }
81
+ toGeoJSON(options) {
82
+ const geoJSON = {};
83
+ if (this.srid) {
84
+ if (options) {
85
+ if (options.shortCrs) geoJSON.crs = {
86
+ type: "name",
87
+ properties: { name: "EPSG:" + this.srid }
88
+ };
89
+ else if (options.longCrs) geoJSON.crs = {
90
+ type: "name",
91
+ properties: { name: "urn:ogc:def:crs:EPSG::" + this.srid }
92
+ };
93
+ }
94
+ }
95
+ return geoJSON;
96
+ }
97
+ };
98
+ }));
99
+ //#endregion
100
+ init_geometry();
101
+ export { Geometry, init_geometry };