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,15 @@
1
+ import { MIGRATION_NAME_REGEX } from "./consts.mjs";
2
+ import { Snapshot, SnapshotColumn, SnapshotColumnRef, SnapshotEnum, SnapshotSequence, SnapshotTable, SnapshotTableCheck, SnapshotTableIndex, SnapshotTablePrimaryKey, SnapshotTableUnique, createEmptySnapshot, snapshot } from "./snapshot.mjs";
3
+ import { CustomStatement, DDLStatement } from "./statement.mjs";
4
+ import { ddl } from "./ddl.mjs";
5
+
6
+ //#region src/migration/index.d.ts
7
+ /**
8
+ * Migration options that can be exported from up.ts/down.ts
9
+ */
10
+ interface MigrationOptions {
11
+ /** Whether to wrap statements in BEGIN...COMMIT (default: true) */
12
+ transaction?: boolean;
13
+ }
14
+ //#endregion
15
+ export { CustomStatement, DDLStatement, MIGRATION_NAME_REGEX, MigrationOptions, type Snapshot, type SnapshotColumn, type SnapshotColumnRef, type SnapshotEnum, type SnapshotSequence, type SnapshotTable, type SnapshotTableCheck, type SnapshotTableIndex, type SnapshotTablePrimaryKey, type SnapshotTableUnique, createEmptySnapshot, ddl, snapshot };
@@ -0,0 +1,5 @@
1
+ import { MIGRATION_NAME_REGEX } from "./consts.mjs";
2
+ import { CustomStatement, DDLStatement } from "./statement.mjs";
3
+ import { ddl } from "./ddl.mjs";
4
+ import { createEmptySnapshot, snapshot } from "./snapshot.mjs";
5
+ export { CustomStatement, DDLStatement, MIGRATION_NAME_REGEX, createEmptySnapshot, ddl, snapshot };
@@ -0,0 +1,169 @@
1
+ import { OnDeleteAction } from "../columns/common.mjs";
2
+ import { SnapshotCheckExpr } from "../constraints/check.mjs";
3
+ import { IndexType } from "../indexes.mjs";
4
+
5
+ //#region src/migration/snapshot.d.ts
6
+ /**
7
+ * Full database snapshot containing all tracked schema objects.
8
+ *
9
+ * A snapshot is a point-in-time representation of the database schema
10
+ * used by the migration system to compute diffs and generate DDL.
11
+ */
12
+ interface Snapshot {
13
+ /** All tracked tables, keyed by `"schema.name"`. */
14
+ tables: Record<string, SnapshotTable>;
15
+ /** All tracked enum types, keyed by `"schema.name"`. */
16
+ enums: Record<string, SnapshotEnum>;
17
+ /** All tracked sequences, keyed by `"schema.name"`. */
18
+ sequences: Record<string, SnapshotSequence>;
19
+ }
20
+ /**
21
+ * Snapshot of a single table including columns, indexes, and constraints.
22
+ */
23
+ interface SnapshotTable {
24
+ /** The PostgreSQL schema this table belongs to (e.g. `"public"`). */
25
+ schema: string;
26
+ /** The table name. */
27
+ name: string;
28
+ /** Columns in the table, keyed by column name. */
29
+ columns: Record<string, SnapshotColumn>;
30
+ /** Indexes on the table, keyed by index name. */
31
+ indexes: Record<string, SnapshotTableIndex>;
32
+ /** CHECK constraints, keyed by constraint name. */
33
+ checkConstraints: Record<string, SnapshotTableCheck>;
34
+ /** UNIQUE constraints (table-level), keyed by constraint name. */
35
+ uniqueConstraints: Record<string, SnapshotTableUnique>;
36
+ /** Composite PRIMARY KEY constraint, if defined. */
37
+ primaryKeyConstraint?: SnapshotTablePrimaryKey;
38
+ }
39
+ /**
40
+ * A foreign-key column reference used in {@link SnapshotColumn["references"]}.
41
+ */
42
+ interface SnapshotColumnRef {
43
+ /** Schema of the referenced table. */
44
+ schema: string;
45
+ /** Name of the referenced table. */
46
+ table: string;
47
+ /** Name of the referenced column. */
48
+ column: string;
49
+ /** The `ON DELETE` action for this foreign key. */
50
+ onDelete: OnDeleteAction;
51
+ }
52
+ /**
53
+ * Snapshot of a single column's metadata.
54
+ */
55
+ interface SnapshotColumn {
56
+ /** The SQL type of the column (e.g. `"varchar(255)"`, `"integer"`). */
57
+ type: string;
58
+ /** Whether the column has a `NOT NULL` constraint. */
59
+ notNull?: true;
60
+ /** Whether the column is the table's primary key. */
61
+ primaryKey?: true;
62
+ /** Whether the column has a `UNIQUE` constraint. */
63
+ unique?: true;
64
+ /** The column's `DEFAULT` expression, as a raw SQL string. */
65
+ default?: string;
66
+ /** The `GENERATED` clause (e.g. `"ALWAYS"` or `"BY DEFAULT"`). */
67
+ generated?: string;
68
+ /** The `AS` expression for generated columns. */
69
+ as?: string;
70
+ /** Foreign key reference, if this column references another table. */
71
+ references?: SnapshotColumnRef;
72
+ }
73
+ /**
74
+ * Snapshot of a PostgreSQL `ENUM` type.
75
+ */
76
+ interface SnapshotEnum {
77
+ /** The schema the enum belongs to. */
78
+ schema: string;
79
+ /** The enum type name. */
80
+ name: string;
81
+ /** Ordered list of allowed values. */
82
+ values: string[];
83
+ }
84
+ /**
85
+ * Snapshot of a PostgreSQL `SEQUENCE`.
86
+ */
87
+ interface SnapshotSequence {
88
+ /** The schema the sequence belongs to. */
89
+ schema: string;
90
+ /** The sequence name. */
91
+ name: string;
92
+ /** `START WITH` value. */
93
+ startWith?: number;
94
+ /** `INCREMENT BY` value. */
95
+ increment?: number;
96
+ /** `MINVALUE` value. */
97
+ minValue?: number;
98
+ /** `MAXVALUE` value. */
99
+ maxValue?: number;
100
+ /** Whether the sequence wraps around (`CYCLE`). */
101
+ cycle?: boolean;
102
+ /** Number of sequence values to pre-allocate (`CACHE`). */
103
+ cache?: number;
104
+ }
105
+ /**
106
+ * Snapshot of a table index.
107
+ */
108
+ interface SnapshotTableIndex {
109
+ /** The index name. */
110
+ name: string;
111
+ /** The `"schema.table"` key of the owning table. */
112
+ table: string;
113
+ /** Column names included in the index. */
114
+ columns: string[];
115
+ /** The index method (e.g. `"btree"`, `"hash"`, `"gin"`, `"gist"`). */
116
+ type: IndexType;
117
+ /** Whether this is a unique index. */
118
+ unique: boolean;
119
+ }
120
+ /**
121
+ * Snapshot of a CHECK constraint.
122
+ */
123
+ interface SnapshotTableCheck {
124
+ /** The constraint name. */
125
+ name: string;
126
+ /** The `"schema.table"` key of the owning table. */
127
+ table: string;
128
+ /** The check expression. */
129
+ expr: SnapshotCheckExpr;
130
+ }
131
+ /**
132
+ * Snapshot of a table-level UNIQUE constraint.
133
+ */
134
+ interface SnapshotTableUnique {
135
+ /** The constraint name. */
136
+ name: string;
137
+ /** The `"schema.table"` key of the owning table. */
138
+ table: string;
139
+ /** Column names that form the unique key. */
140
+ columns: string[];
141
+ }
142
+ /**
143
+ * Snapshot of a table-level PRIMARY KEY constraint.
144
+ */
145
+ interface SnapshotTablePrimaryKey {
146
+ /** The constraint name. */
147
+ name: string;
148
+ /** The `"schema.table"` key of the owning table. */
149
+ table: string;
150
+ /** Column names that form the primary key. */
151
+ columns: string[];
152
+ }
153
+ /**
154
+ * Creates an empty {@link Snapshot} with no tables, enums, or sequences.
155
+ *
156
+ * @returns A new empty snapshot object.
157
+ */
158
+ declare function createEmptySnapshot(): Snapshot;
159
+ /**
160
+ * Generates a {@link Snapshot} from an array of Durcno entities (tables, enums, sequences).
161
+ *
162
+ * This function introspects the provided entities to extract their schema metadata
163
+ * and constructs a snapshot object that represents the current state of the database schema.
164
+ * @param entities - An array of Durcno entities (tables, enums, sequences) to include in the snapshot.
165
+ * @returns A {@link Snapshot} object representing the current state of the database schema.
166
+ */
167
+ declare function snapshot(entities: unknown[]): Snapshot;
168
+ //#endregion
169
+ export { Snapshot, SnapshotColumn, SnapshotColumnRef, SnapshotEnum, SnapshotSequence, SnapshotTable, SnapshotTableCheck, SnapshotTableIndex, SnapshotTablePrimaryKey, SnapshotTableUnique, createEmptySnapshot, snapshot };
@@ -0,0 +1,123 @@
1
+ import { is } from "../entity.mjs";
2
+ import { primaryKeyConstraint } from "../constraints/primary-key.mjs";
3
+ import { uniqueConstraint } from "../constraints/unique.mjs";
4
+ import { Table } from "../table.mjs";
5
+ import { Enum } from "../enumtype.mjs";
6
+ import { Sequence } from "../sequence.mjs";
7
+ import { CheckBuilder, check } from "../constraints/check.mjs";
8
+ import chalk from "../node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/index.mjs";
9
+ //#region src/migration/snapshot.ts
10
+ const { red } = chalk;
11
+ /**
12
+ * Creates an empty {@link Snapshot} with no tables, enums, or sequences.
13
+ *
14
+ * @returns A new empty snapshot object.
15
+ */
16
+ function createEmptySnapshot() {
17
+ return {
18
+ enums: {},
19
+ tables: {},
20
+ sequences: {}
21
+ };
22
+ }
23
+ /**
24
+ * Generates a {@link Snapshot} from an array of Durcno entities (tables, enums, sequences).
25
+ *
26
+ * This function introspects the provided entities to extract their schema metadata
27
+ * and constructs a snapshot object that represents the current state of the database schema.
28
+ * @param entities - An array of Durcno entities (tables, enums, sequences) to include in the snapshot.
29
+ * @returns A {@link Snapshot} object representing the current state of the database schema.
30
+ */
31
+ function snapshot(entities) {
32
+ const ss = {
33
+ enums: {},
34
+ tables: {},
35
+ sequences: {}
36
+ };
37
+ for (const entity of entities) if (is(entity, Enum)) {
38
+ const enm = entity;
39
+ ss.enums[`${enm.schema}.${enm.name}`] = {
40
+ schema: enm.schema,
41
+ name: enm.name,
42
+ values: [...enm.values]
43
+ };
44
+ } else if (is(entity, Table)) {
45
+ const table = entity;
46
+ ss.tables[`${table._.schema}.${table._.name}`] = {
47
+ schema: table._.schema,
48
+ name: table._.name,
49
+ columns: {},
50
+ indexes: {},
51
+ checkConstraints: {},
52
+ uniqueConstraints: {}
53
+ };
54
+ Object.entries(table._.columns).forEach(([_, col]) => {
55
+ const { referencesCol, referencesOnDelete } = col;
56
+ ss.tables[`${table._.schema}.${table._.name}`].columns[col.nameSnake] = {
57
+ type: col.sqlType,
58
+ default: col.defaultToSQL(),
59
+ primaryKey: col.isPrimaryKey || void 0,
60
+ references: referencesCol !== null ? {
61
+ schema: referencesCol.table._.schema,
62
+ table: referencesCol.table._.name,
63
+ column: referencesCol.nameSnake,
64
+ onDelete: referencesOnDelete ?? "CASCADE"
65
+ } : void 0,
66
+ unique: col.isUnique || void 0,
67
+ notNull: col.isNotNull || void 0,
68
+ generated: col.generated,
69
+ as: col.as ?? (col.generated ? "IDENTITY" : void 0)
70
+ };
71
+ });
72
+ (table._.extra.indexes?.(table) ?? []).forEach((index) => {
73
+ ss.tables[`${table._.schema}.${table._.name}`].indexes[index._.getName(table)] = {
74
+ name: index._.getName(table),
75
+ table: table._.fullName,
76
+ columns: index._.getColumns().map((col) => col.nameSnake),
77
+ type: index._.getUsing(),
78
+ unique: index._.getUnique()
79
+ };
80
+ });
81
+ (table._.extra.checkConstraints?.(table, check, new CheckBuilder()) ?? []).forEach((chk) => {
82
+ ss.tables[`${table._.schema}.${table._.name}`].checkConstraints[chk.getName()] = {
83
+ name: chk.getName(),
84
+ table: table._.fullName,
85
+ expr: chk.toSnapshotExpr()
86
+ };
87
+ });
88
+ (table._.extra.uniqueConstraints?.(table, uniqueConstraint) ?? []).forEach((uc) => {
89
+ ss.tables[`${table._.schema}.${table._.name}`].uniqueConstraints[uc.getName(table)] = {
90
+ name: uc.getName(table),
91
+ table: table._.fullName,
92
+ columns: uc.getColumns()
93
+ };
94
+ });
95
+ const pkConstraint = table._.extra.primaryKeyConstraint?.(table, primaryKeyConstraint);
96
+ if (pkConstraint) {
97
+ if (Object.values(ss.tables[`${table._.schema}.${table._.name}`].columns).some((col) => col.primaryKey)) {
98
+ console.error(red(`[Error] Table "${table._.schema}"."${table._.name}" has both a column-level primaryKey and a table-level primaryKeyConstraint. Remove one of them.`));
99
+ process.exit(1);
100
+ }
101
+ ss.tables[`${table._.schema}.${table._.name}`].primaryKeyConstraint = {
102
+ name: pkConstraint.getName(table),
103
+ table: table._.fullName,
104
+ columns: pkConstraint.getColumns()
105
+ };
106
+ }
107
+ } else if (is(entity, Sequence)) {
108
+ const seq = entity;
109
+ ss.sequences[`${seq.schema}.${seq.name}`] = {
110
+ schema: seq.schema,
111
+ name: seq.name,
112
+ startWith: seq.config.startWith,
113
+ increment: seq.config.increment,
114
+ minValue: seq.config.minValue,
115
+ maxValue: seq.config.maxValue,
116
+ cycle: seq.config.cycle,
117
+ cache: seq.config.cache
118
+ };
119
+ }
120
+ return ss;
121
+ }
122
+ //#endregion
123
+ export { createEmptySnapshot, snapshot };
@@ -0,0 +1,89 @@
1
+ import { Snapshot } from "./snapshot.mjs";
2
+
3
+ //#region src/migration/statement.d.ts
4
+ /**
5
+ * All possible DDL statement types supported by the migration system.
6
+ *
7
+ * Each type corresponds to a concrete {@link DDLStatement} subclass:
8
+ *
9
+ * - `"createSchema"` — {@link CreateSchemaStatement}
10
+ * - `"dropSchema"` — {@link DropSchemaStatement}
11
+ * - `"createTable"` — {@link CreateTableBuilder}
12
+ * - `"dropTable"` — {@link DropTableStatement}
13
+ * - `"renameTable"` — {@link RenameTableStatement}
14
+ * - `"alterTable"` — {@link AlterTableBuilder}
15
+ * - `"createEnum"` — {@link CreateEnumStatement}
16
+ * - `"alterEnum"` — {@link AlterEnumAddValueStatement}
17
+ * - `"dropEnum"` — {@link DropEnumStatement}
18
+ * - `"createSequence"` — {@link CreateSequenceStatement}
19
+ * - `"dropSequence"` — {@link DropSequenceStatement}
20
+ * - `"createIndex"` — {@link CreateIndexBuilder}
21
+ * - `"dropIndex"` — {@link DropIndexStatement}
22
+ * - `"custom"` — {@link CustomStatement}
23
+ */
24
+ type DDLStatementType = "createSchema" | "dropSchema" | "createTable" | "dropTable" | "renameTable" | "alterTable" | "createEnum" | "alterEnum" | "dropEnum" | "createSequence" | "dropSequence" | "createIndex" | "dropIndex" | "custom";
25
+ /**
26
+ * Abstract base class for all DDL (Data Definition Language) statements.
27
+ *
28
+ * Every migration statement extends this class and must implement:
29
+ * - {@link DDLStatement.type | type} — a discriminant for the statement kind
30
+ * - {@link DDLStatements["toSQL"] | toSQL()} — generates the raw SQL string
31
+ * - {@link DDLStatement.applyToSnapshot | applyToSnapshot()} — mutates a {@link Snapshot} in place
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * import { ddl, type DDLStatement } from 'durcno/migration';
36
+ *
37
+ * const statements: DDLStatement[] = [
38
+ * ddl.createTable('public', 'users')
39
+ * .column('id', 'serial', { primaryKey: true })
40
+ * .column('name', 'varchar(255)', { notNull: true }),
41
+ * ];
42
+ * ```
43
+ */
44
+ declare abstract class DDLStatement {
45
+ /** Discriminant identifying the kind of DDL operation. */
46
+ abstract readonly type: DDLStatementType;
47
+ /** Whether this is a custom (user-defined) statement. */
48
+ get isCustom(): boolean;
49
+ /** Generate the SQL DDL string for this statement. */
50
+ abstract toSQL(): string;
51
+ /**
52
+ * Apply this DDL statement's changes to a snapshot object.
53
+ * Mutates the snapshot in place.
54
+ *
55
+ * @param snapshot - The database snapshot to mutate.
56
+ */
57
+ abstract applyToSnapshot(snapshot: Snapshot): void;
58
+ }
59
+ /**
60
+ * A wrapper for user-defined custom SQL statements.
61
+ *
62
+ * Custom statements are preserved during migration regeneration and
63
+ * cannot be automatically reflected in the schema snapshot.
64
+ *
65
+ * @example
66
+ * ```typescript
67
+ * import { ddl } from 'durcno/migration';
68
+ *
69
+ * const seedData = ddl.custom("INSERT INTO users (name) VALUES ('admin')");
70
+ * console.log(seedData.toSQL());
71
+ * // INSERT INTO users (name) VALUES ('admin')
72
+ * ```
73
+ */
74
+ declare class CustomStatement extends DDLStatement {
75
+ private readonly sql;
76
+ readonly type: "custom";
77
+ /**
78
+ * @param sql - The raw SQL string to execute.
79
+ */
80
+ constructor(sql: string);
81
+ toSQL(): string;
82
+ /**
83
+ * Custom statements cannot be parsed, so this is a no-op.
84
+ * Users are responsible for ensuring custom SQL is reflected in the schema.
85
+ */
86
+ applyToSnapshot(_snapshot: Snapshot): void;
87
+ }
88
+ //#endregion
89
+ export { CustomStatement, DDLStatement };
@@ -0,0 +1,61 @@
1
+ //#region src/migration/statement.ts
2
+ /**
3
+ * Abstract base class for all DDL (Data Definition Language) statements.
4
+ *
5
+ * Every migration statement extends this class and must implement:
6
+ * - {@link DDLStatement.type | type} — a discriminant for the statement kind
7
+ * - {@link DDLStatements["toSQL"] | toSQL()} — generates the raw SQL string
8
+ * - {@link DDLStatement.applyToSnapshot | applyToSnapshot()} — mutates a {@link Snapshot} in place
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { ddl, type DDLStatement } from 'durcno/migration';
13
+ *
14
+ * const statements: DDLStatement[] = [
15
+ * ddl.createTable('public', 'users')
16
+ * .column('id', 'serial', { primaryKey: true })
17
+ * .column('name', 'varchar(255)', { notNull: true }),
18
+ * ];
19
+ * ```
20
+ */
21
+ var DDLStatement = class {
22
+ /** Whether this is a custom (user-defined) statement. */
23
+ get isCustom() {
24
+ return this.type === "custom";
25
+ }
26
+ };
27
+ /**
28
+ * A wrapper for user-defined custom SQL statements.
29
+ *
30
+ * Custom statements are preserved during migration regeneration and
31
+ * cannot be automatically reflected in the schema snapshot.
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * import { ddl } from 'durcno/migration';
36
+ *
37
+ * const seedData = ddl.custom("INSERT INTO users (name) VALUES ('admin')");
38
+ * console.log(seedData.toSQL());
39
+ * // INSERT INTO users (name) VALUES ('admin')
40
+ * ```
41
+ */
42
+ var CustomStatement = class extends DDLStatement {
43
+ type = "custom";
44
+ /**
45
+ * @param sql - The raw SQL string to execute.
46
+ */
47
+ constructor(sql) {
48
+ super();
49
+ this.sql = sql;
50
+ }
51
+ toSQL() {
52
+ return this.sql;
53
+ }
54
+ /**
55
+ * Custom statements cannot be parsed, so this is a no-op.
56
+ * Users are responsible for ensuring custom SQL is reflected in the schema.
57
+ */
58
+ applyToSnapshot(_snapshot) {}
59
+ };
60
+ //#endregion
61
+ export { CustomStatement, DDLStatement };
@@ -0,0 +1,34 @@
1
+ import { TableWithColumns } from "./table.mjs";
2
+ import { BigintColumn } from "./columns/bigint.mjs";
3
+ import { TimestampColumn } from "./columns/timestamp.mjs";
4
+ import { VarcharColumn } from "./columns/varchar.mjs";
5
+
6
+ //#region src/models.d.ts
7
+ /**
8
+ * Returns a BigintColumn with { primaryKey, generated: "ALWAYS", as: "IDENTITY" }.
9
+ * @returns BigintColumn
10
+ */
11
+ declare const pk: () => BigintColumn<{
12
+ primaryKey: true;
13
+ generated: "ALWAYS";
14
+ as: "IDENTITY";
15
+ }>;
16
+ declare const Migrations: TableWithColumns<"durcno", "migrations", {
17
+ id: BigintColumn<{
18
+ primaryKey: true;
19
+ generated: "ALWAYS";
20
+ as: "IDENTITY";
21
+ }>;
22
+ name: VarcharColumn<{
23
+ length: number;
24
+ unique: true;
25
+ notNull: true;
26
+ }>;
27
+ createdAt: TimestampColumn<{
28
+ notNull: true;
29
+ }> & {
30
+ hasDefault: true;
31
+ };
32
+ }>;
33
+ //#endregion
34
+ export { Migrations, pk };
@@ -0,0 +1,27 @@
1
+ import "./columns/common.mjs";
2
+ import { bigint } from "./columns/bigint.mjs";
3
+ import { timestamp } from "./columns/timestamp.mjs";
4
+ import { varchar } from "./columns/varchar.mjs";
5
+ import { table } from "./table.mjs";
6
+ import { now } from "./functions/index.mjs";
7
+ //#region src/models.ts
8
+ /**
9
+ * Returns a BigintColumn with { primaryKey, generated: "ALWAYS", as: "IDENTITY" }.
10
+ * @returns BigintColumn
11
+ */
12
+ const pk = () => bigint({
13
+ primaryKey: true,
14
+ generated: "ALWAYS",
15
+ as: "IDENTITY"
16
+ });
17
+ const Migrations = table("durcno", "migrations", {
18
+ id: pk(),
19
+ name: varchar({
20
+ length: 100,
21
+ unique: true,
22
+ notNull: true
23
+ }),
24
+ createdAt: timestamp({ notNull: true }).default(now())
25
+ });
26
+ //#endregion
27
+ export { Migrations, pk };
@@ -0,0 +1,126 @@
1
+ import ansiStyles from "./vendor/ansi-styles/index.mjs";
2
+ import supportsColor from "./vendor/supports-color/index.mjs";
3
+ import { stringEncaseCRLFWithFirstIndex, stringReplaceAll } from "./utilities.mjs";
4
+ //#region node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/index.js
5
+ const { stdout: stdoutColor, stderr: stderrColor } = supportsColor;
6
+ const GENERATOR = Symbol("GENERATOR");
7
+ const STYLER = Symbol("STYLER");
8
+ const IS_EMPTY = Symbol("IS_EMPTY");
9
+ const levelMapping = [
10
+ "ansi",
11
+ "ansi",
12
+ "ansi256",
13
+ "ansi16m"
14
+ ];
15
+ const styles = Object.create(null);
16
+ const applyOptions = (object, options = {}) => {
17
+ if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) throw new Error("The `level` option should be an integer from 0 to 3");
18
+ const colorLevel = stdoutColor ? stdoutColor.level : 0;
19
+ object.level = options.level === void 0 ? colorLevel : options.level;
20
+ };
21
+ const chalkFactory = (options) => {
22
+ const chalk = (...strings) => strings.join(" ");
23
+ applyOptions(chalk, options);
24
+ Object.setPrototypeOf(chalk, createChalk.prototype);
25
+ return chalk;
26
+ };
27
+ function createChalk(options) {
28
+ return chalkFactory(options);
29
+ }
30
+ Object.setPrototypeOf(createChalk.prototype, Function.prototype);
31
+ for (const [styleName, style] of Object.entries(ansiStyles)) styles[styleName] = { get() {
32
+ const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]);
33
+ Object.defineProperty(this, styleName, { value: builder });
34
+ return builder;
35
+ } };
36
+ styles.visible = { get() {
37
+ const builder = createBuilder(this, this[STYLER], true);
38
+ Object.defineProperty(this, "visible", { value: builder });
39
+ return builder;
40
+ } };
41
+ const getModelAnsi = (model, level, type, ...arguments_) => {
42
+ if (model === "rgb") {
43
+ if (level === "ansi16m") return ansiStyles[type].ansi16m(...arguments_);
44
+ if (level === "ansi256") return ansiStyles[type].ansi256(ansiStyles.rgbToAnsi256(...arguments_));
45
+ return ansiStyles[type].ansi(ansiStyles.rgbToAnsi(...arguments_));
46
+ }
47
+ if (model === "hex") return getModelAnsi("rgb", level, type, ...ansiStyles.hexToRgb(...arguments_));
48
+ return ansiStyles[type][model](...arguments_);
49
+ };
50
+ for (const model of [
51
+ "rgb",
52
+ "hex",
53
+ "ansi256"
54
+ ]) {
55
+ styles[model] = { get() {
56
+ const { level } = this;
57
+ return function(...arguments_) {
58
+ const styler = createStyler(getModelAnsi(model, levelMapping[level], "color", ...arguments_), ansiStyles.color.close, this[STYLER]);
59
+ return createBuilder(this, styler, this[IS_EMPTY]);
60
+ };
61
+ } };
62
+ const bgModel = "bg" + model[0].toUpperCase() + model.slice(1);
63
+ styles[bgModel] = { get() {
64
+ const { level } = this;
65
+ return function(...arguments_) {
66
+ const styler = createStyler(getModelAnsi(model, levelMapping[level], "bgColor", ...arguments_), ansiStyles.bgColor.close, this[STYLER]);
67
+ return createBuilder(this, styler, this[IS_EMPTY]);
68
+ };
69
+ } };
70
+ }
71
+ const proto = Object.defineProperties(() => {}, {
72
+ ...styles,
73
+ level: {
74
+ enumerable: true,
75
+ get() {
76
+ return this[GENERATOR].level;
77
+ },
78
+ set(level) {
79
+ this[GENERATOR].level = level;
80
+ }
81
+ }
82
+ });
83
+ const createStyler = (open, close, parent) => {
84
+ let openAll;
85
+ let closeAll;
86
+ if (parent === void 0) {
87
+ openAll = open;
88
+ closeAll = close;
89
+ } else {
90
+ openAll = parent.openAll + open;
91
+ closeAll = close + parent.closeAll;
92
+ }
93
+ return {
94
+ open,
95
+ close,
96
+ openAll,
97
+ closeAll,
98
+ parent
99
+ };
100
+ };
101
+ const createBuilder = (self, _styler, _isEmpty) => {
102
+ const builder = (...arguments_) => applyStyle(builder, arguments_.length === 1 ? "" + arguments_[0] : arguments_.join(" "));
103
+ Object.setPrototypeOf(builder, proto);
104
+ builder[GENERATOR] = self;
105
+ builder[STYLER] = _styler;
106
+ builder[IS_EMPTY] = _isEmpty;
107
+ return builder;
108
+ };
109
+ const applyStyle = (self, string) => {
110
+ if (self.level <= 0 || !string) return self[IS_EMPTY] ? "" : string;
111
+ let styler = self[STYLER];
112
+ if (styler === void 0) return string;
113
+ const { openAll, closeAll } = styler;
114
+ if (string.includes("\x1B")) while (styler !== void 0) {
115
+ string = stringReplaceAll(string, styler.close, styler.open);
116
+ styler = styler.parent;
117
+ }
118
+ const lfIndex = string.indexOf("\n");
119
+ if (lfIndex !== -1) string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
120
+ return openAll + string + closeAll;
121
+ };
122
+ Object.defineProperties(createChalk.prototype, styles);
123
+ const chalk = createChalk();
124
+ createChalk({ level: stderrColor ? stderrColor.level : 0 });
125
+ //#endregion
126
+ export { chalk as default };
@@ -0,0 +1,29 @@
1
+ //#region node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/utilities.js
2
+ function stringReplaceAll(string, substring, replacer) {
3
+ let index = string.indexOf(substring);
4
+ if (index === -1) return string;
5
+ const substringLength = substring.length;
6
+ let endIndex = 0;
7
+ let returnValue = "";
8
+ do {
9
+ returnValue += string.slice(endIndex, index) + substring + replacer;
10
+ endIndex = index + substringLength;
11
+ index = string.indexOf(substring, endIndex);
12
+ } while (index !== -1);
13
+ returnValue += string.slice(endIndex);
14
+ return returnValue;
15
+ }
16
+ function stringEncaseCRLFWithFirstIndex(string, prefix, postfix, index) {
17
+ let endIndex = 0;
18
+ let returnValue = "";
19
+ do {
20
+ const gotCR = string[index - 1] === "\r";
21
+ returnValue += string.slice(endIndex, gotCR ? index - 1 : index) + prefix + (gotCR ? "\r\n" : "\n") + postfix;
22
+ endIndex = index + 1;
23
+ index = string.indexOf("\n", endIndex);
24
+ } while (index !== -1);
25
+ returnValue += string.slice(endIndex);
26
+ return returnValue;
27
+ }
28
+ //#endregion
29
+ export { stringEncaseCRLFWithFirstIndex, stringReplaceAll };