@ghom/orm 1.10.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/test.yml +9 -9
- package/biome.json +45 -0
- package/bun.lock +488 -0
- package/dist/app/backup.d.ts +3 -3
- package/dist/app/backup.js +10 -11
- package/dist/app/column.d.ts +225 -0
- package/dist/app/column.js +342 -0
- package/dist/app/orm.d.ts +5 -4
- package/dist/app/orm.js +19 -15
- package/dist/app/table.d.ts +51 -21
- package/dist/app/table.js +35 -18
- package/dist/app/util.d.ts +1 -1
- package/dist/app/util.js +4 -4
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/package.json +43 -47
- package/tests/{test.js → orm.test.ts} +112 -53
- package/tests/tables/a.ts +16 -0
- package/tests/tables/b.ts +16 -0
- package/tests/tables/c.ts +14 -0
- package/tests/tables/d.ts +32 -0
- package/tsconfig.json +1 -0
- package/tests/tables/a.js +0 -25
- package/tests/tables/b.js +0 -30
- package/tests/tables/c.js +0 -17
package/dist/app/backup.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Knex } from "knex";
|
|
2
|
-
import { ORM } from "./orm.js";
|
|
3
|
-
import { Table } from "./table.js";
|
|
1
|
+
import type { Knex } from "knex";
|
|
2
|
+
import type { ORM } from "./orm.js";
|
|
3
|
+
import type { Table } from "./table.js";
|
|
4
4
|
export declare function backupTable(table: Table, dirname?: string): Promise<void>;
|
|
5
5
|
export declare function restoreBackup(table: Table, trx: Knex.Transaction | Knex, dirname?: string): Promise<void>;
|
|
6
6
|
export declare function enableForeignKeys(orm: ORM, trx?: Knex.Transaction | Knex): Promise<void>;
|
package/dist/app/backup.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import csv from "json-2-csv";
|
|
4
3
|
import csvParser from "csv-parser";
|
|
5
|
-
import
|
|
4
|
+
import csv from "json-2-csv";
|
|
5
|
+
import { DEFAULT_BACKUP_CHUNK_SIZE, DEFAULT_BACKUP_LOCATION, styled } from "./util.js";
|
|
6
6
|
function getConfig(orm) {
|
|
7
7
|
if (orm.config === false)
|
|
8
8
|
throw new Error("ORM config is not available");
|
|
@@ -75,7 +75,7 @@ export async function restoreBackup(table, trx, dirname) {
|
|
|
75
75
|
const totalChunks = chunkFiles.length;
|
|
76
76
|
let processedChunks = 0;
|
|
77
77
|
let totalRowsRestored = 0;
|
|
78
|
-
for (
|
|
78
|
+
for (const chunkFile of chunkFiles) {
|
|
79
79
|
const filePath = path.join(chunkDir, chunkFile);
|
|
80
80
|
let rows = [];
|
|
81
81
|
let chunkRowsCount = 0;
|
|
@@ -119,30 +119,29 @@ export async function enableForeignKeys(orm, trx) {
|
|
|
119
119
|
});
|
|
120
120
|
}
|
|
121
121
|
export async function disableForeignKeys(orm, run) {
|
|
122
|
-
if (!orm.
|
|
122
|
+
if (!orm._client)
|
|
123
123
|
throw new Error("ORM client is not initialized");
|
|
124
124
|
const trx = orm.clientBasedOperation({
|
|
125
|
-
sqlite3: () => orm.
|
|
126
|
-
}) ?? (await orm.
|
|
127
|
-
const
|
|
125
|
+
sqlite3: () => orm._client,
|
|
126
|
+
}) ?? (await orm._client.transaction());
|
|
127
|
+
const _ran = await orm.clientBasedOperation({
|
|
128
128
|
mysql2: async () => {
|
|
129
129
|
const result = await trx.raw("SELECT @@FOREIGN_KEY_CHECKS;");
|
|
130
|
-
const check = result?.[0] && result[0]["@@FOREIGN_KEY_CHECKS"]
|
|
130
|
+
const check = result?.[0] && result[0]["@@FOREIGN_KEY_CHECKS"] !== 0;
|
|
131
131
|
if (check)
|
|
132
132
|
await trx.raw("SET FOREIGN_KEY_CHECKS = 0;");
|
|
133
133
|
return check;
|
|
134
134
|
},
|
|
135
135
|
sqlite3: async () => {
|
|
136
136
|
const result = await trx.raw("PRAGMA foreign_keys;");
|
|
137
|
-
const check = result?.[0] && result[0].foreign_keys
|
|
137
|
+
const check = result?.[0] && result[0].foreign_keys !== 0;
|
|
138
138
|
if (check)
|
|
139
139
|
await trx.raw("PRAGMA foreign_keys = 0;");
|
|
140
140
|
return check;
|
|
141
141
|
},
|
|
142
142
|
pg: async () => {
|
|
143
143
|
const result = await trx.raw("SHOW session_replication_role;");
|
|
144
|
-
const check = result?.rows?.[0] &&
|
|
145
|
-
result.rows[0].session_replication_role !== "replica";
|
|
144
|
+
const check = result?.rows?.[0] && result.rows[0].session_replication_role !== "replica";
|
|
146
145
|
if (check)
|
|
147
146
|
await trx.raw("SET session_replication_role = replica;");
|
|
148
147
|
return check;
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import type { Knex } from "knex";
|
|
2
|
+
/**
|
|
3
|
+
* Represents the inferred TypeScript type for a column definition.
|
|
4
|
+
*/
|
|
5
|
+
export type InferColumnType<T extends ColumnDef<any, any>> = T extends ColumnDef<infer Type, infer Nullable> ? Nullable extends true ? Type | null : Type : never;
|
|
6
|
+
/**
|
|
7
|
+
* Infers the full record type from a columns definition.
|
|
8
|
+
*/
|
|
9
|
+
export type InferColumns<T extends Record<string, ColumnDef<any, any>>> = {
|
|
10
|
+
[K in keyof T]: InferColumnType<T[K]>;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Base column definition that tracks the TypeScript type and nullability.
|
|
14
|
+
*/
|
|
15
|
+
export declare class ColumnDef<Type, Nullable extends boolean = false> {
|
|
16
|
+
/** @internal */
|
|
17
|
+
readonly _buildFn: (tableBuilder: Knex.CreateTableBuilder, name: string) => Knex.ColumnBuilder;
|
|
18
|
+
/** @internal */
|
|
19
|
+
readonly _isNullable: Nullable;
|
|
20
|
+
/** @internal */
|
|
21
|
+
readonly _modifiers: Array<(col: Knex.ColumnBuilder) => Knex.ColumnBuilder>;
|
|
22
|
+
/** @internal */
|
|
23
|
+
readonly _type: Type;
|
|
24
|
+
/** @internal */
|
|
25
|
+
readonly _nullable: Nullable;
|
|
26
|
+
/** @internal */
|
|
27
|
+
_knexBuilder?: Knex.ColumnBuilder;
|
|
28
|
+
constructor(
|
|
29
|
+
/** @internal */
|
|
30
|
+
_buildFn: (tableBuilder: Knex.CreateTableBuilder, name: string) => Knex.ColumnBuilder,
|
|
31
|
+
/** @internal */
|
|
32
|
+
_isNullable?: Nullable,
|
|
33
|
+
/** @internal */
|
|
34
|
+
_modifiers?: Array<(col: Knex.ColumnBuilder) => Knex.ColumnBuilder>);
|
|
35
|
+
/**
|
|
36
|
+
* Makes this column nullable.
|
|
37
|
+
*/
|
|
38
|
+
nullable(): ColumnDef<Type, true>;
|
|
39
|
+
/**
|
|
40
|
+
* Sets a default value for this column.
|
|
41
|
+
*/
|
|
42
|
+
defaultTo(value: Type | Knex.Raw): ColumnDef<Type, Nullable>;
|
|
43
|
+
/**
|
|
44
|
+
* Makes this column unique.
|
|
45
|
+
*/
|
|
46
|
+
unique(): ColumnDef<Type, Nullable>;
|
|
47
|
+
/**
|
|
48
|
+
* Makes this column the primary key.
|
|
49
|
+
*/
|
|
50
|
+
primary(): ColumnDef<Type, Nullable>;
|
|
51
|
+
/**
|
|
52
|
+
* Adds an index to this column.
|
|
53
|
+
*/
|
|
54
|
+
index(indexName?: string): ColumnDef<Type, Nullable>;
|
|
55
|
+
/**
|
|
56
|
+
* Adds a comment to this column.
|
|
57
|
+
*/
|
|
58
|
+
comment(comment: string): ColumnDef<Type, Nullable>;
|
|
59
|
+
/**
|
|
60
|
+
* Sets the collation for this column.
|
|
61
|
+
*/
|
|
62
|
+
collate(collation: string): ColumnDef<Type, Nullable>;
|
|
63
|
+
/**
|
|
64
|
+
* Adds a references constraint to this column.
|
|
65
|
+
*/
|
|
66
|
+
references(columnName: string): ForeignColumnDef<Type, Nullable>;
|
|
67
|
+
/** @internal */
|
|
68
|
+
_build(tableBuilder: Knex.CreateTableBuilder, name: string): Knex.ColumnBuilder;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Column definition with foreign key support.
|
|
72
|
+
*/
|
|
73
|
+
export declare class ForeignColumnDef<Type, Nullable extends boolean> extends ColumnDef<Type, Nullable> {
|
|
74
|
+
private _referencesColumn;
|
|
75
|
+
private _inTable?;
|
|
76
|
+
private _onDelete?;
|
|
77
|
+
private _onUpdate?;
|
|
78
|
+
constructor(buildFn: (tableBuilder: Knex.CreateTableBuilder, name: string) => Knex.ColumnBuilder, isNullable: Nullable, modifiers: Array<(col: Knex.ColumnBuilder) => Knex.ColumnBuilder>, _referencesColumn: string, _inTable?: string | undefined, _onDelete?: string | undefined, _onUpdate?: string | undefined);
|
|
79
|
+
/**
|
|
80
|
+
* Sets the table for the foreign key reference.
|
|
81
|
+
*/
|
|
82
|
+
inTable(tableName: string): ForeignColumnDef<Type, Nullable>;
|
|
83
|
+
/**
|
|
84
|
+
* Sets the ON DELETE action.
|
|
85
|
+
*/
|
|
86
|
+
onDelete(action: "CASCADE" | "SET NULL" | "RESTRICT" | "NO ACTION"): ForeignColumnDef<Type, Nullable>;
|
|
87
|
+
/**
|
|
88
|
+
* Sets the ON UPDATE action.
|
|
89
|
+
*/
|
|
90
|
+
onUpdate(action: "CASCADE" | "SET NULL" | "RESTRICT" | "NO ACTION"): ForeignColumnDef<Type, Nullable>;
|
|
91
|
+
/** @internal */
|
|
92
|
+
_build(tableBuilder: Knex.CreateTableBuilder, name: string): Knex.ColumnBuilder;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Number column definition with additional numeric options.
|
|
96
|
+
*/
|
|
97
|
+
export declare class NumberColumnDef<Nullable extends boolean = false> extends ColumnDef<number, Nullable> {
|
|
98
|
+
/**
|
|
99
|
+
* Makes this column unsigned (only positive values).
|
|
100
|
+
*/
|
|
101
|
+
unsigned(): NumberColumnDef<Nullable>;
|
|
102
|
+
nullable(): NumberColumnDef<true>;
|
|
103
|
+
defaultTo(value: number | Knex.Raw): NumberColumnDef<Nullable>;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Column factory for creating typed column definitions.
|
|
107
|
+
*/
|
|
108
|
+
export declare const col: {
|
|
109
|
+
/**
|
|
110
|
+
* Auto-incrementing primary key (integer).
|
|
111
|
+
*/
|
|
112
|
+
increments(): ColumnDef<number>;
|
|
113
|
+
/**
|
|
114
|
+
* Big auto-incrementing primary key (bigint).
|
|
115
|
+
*/
|
|
116
|
+
bigIncrements(): ColumnDef<bigint>;
|
|
117
|
+
/**
|
|
118
|
+
* Integer column.
|
|
119
|
+
*/
|
|
120
|
+
integer(): NumberColumnDef;
|
|
121
|
+
/**
|
|
122
|
+
* Big integer column.
|
|
123
|
+
*/
|
|
124
|
+
bigInteger(): ColumnDef<bigint>;
|
|
125
|
+
/**
|
|
126
|
+
* Tiny integer column (0-255).
|
|
127
|
+
*/
|
|
128
|
+
tinyint(): NumberColumnDef;
|
|
129
|
+
/**
|
|
130
|
+
* Small integer column.
|
|
131
|
+
*/
|
|
132
|
+
smallint(): NumberColumnDef;
|
|
133
|
+
/**
|
|
134
|
+
* Medium integer column.
|
|
135
|
+
*/
|
|
136
|
+
mediumint(): NumberColumnDef;
|
|
137
|
+
/**
|
|
138
|
+
* Floating-point number column.
|
|
139
|
+
* @param precision - Total number of digits
|
|
140
|
+
* @param scale - Number of decimal places
|
|
141
|
+
*/
|
|
142
|
+
float(precision?: number, scale?: number): NumberColumnDef;
|
|
143
|
+
/**
|
|
144
|
+
* Double precision floating-point column.
|
|
145
|
+
* @param precision - Total number of digits
|
|
146
|
+
* @param scale - Number of decimal places
|
|
147
|
+
*/
|
|
148
|
+
double(precision?: number, scale?: number): NumberColumnDef;
|
|
149
|
+
/**
|
|
150
|
+
* Decimal column for exact numeric values.
|
|
151
|
+
* @param precision - Total number of digits
|
|
152
|
+
* @param scale - Number of decimal places
|
|
153
|
+
*/
|
|
154
|
+
decimal(precision?: number, scale?: number): NumberColumnDef;
|
|
155
|
+
/**
|
|
156
|
+
* String column.
|
|
157
|
+
* @param length - Maximum length (default: 255)
|
|
158
|
+
*/
|
|
159
|
+
string(length?: number): ColumnDef<string>;
|
|
160
|
+
/**
|
|
161
|
+
* Text column for longer strings.
|
|
162
|
+
* @param textType - Type of text column
|
|
163
|
+
*/
|
|
164
|
+
text(textType?: "text" | "mediumtext" | "longtext"): ColumnDef<string>;
|
|
165
|
+
/**
|
|
166
|
+
* Boolean column.
|
|
167
|
+
*/
|
|
168
|
+
boolean(): ColumnDef<boolean>;
|
|
169
|
+
/**
|
|
170
|
+
* Date column (without time).
|
|
171
|
+
*/
|
|
172
|
+
date(): ColumnDef<Date>;
|
|
173
|
+
/**
|
|
174
|
+
* DateTime column.
|
|
175
|
+
* @param options - DateTime options
|
|
176
|
+
*/
|
|
177
|
+
datetime(options?: {
|
|
178
|
+
useTz?: boolean;
|
|
179
|
+
precision?: number;
|
|
180
|
+
}): ColumnDef<Date>;
|
|
181
|
+
/**
|
|
182
|
+
* Timestamp column.
|
|
183
|
+
* @param options - Timestamp options
|
|
184
|
+
*/
|
|
185
|
+
timestamp(options?: {
|
|
186
|
+
useTz?: boolean;
|
|
187
|
+
precision?: number;
|
|
188
|
+
}): ColumnDef<Date>;
|
|
189
|
+
/**
|
|
190
|
+
* Time column.
|
|
191
|
+
*/
|
|
192
|
+
time(): ColumnDef<string>;
|
|
193
|
+
/**
|
|
194
|
+
* Binary column.
|
|
195
|
+
* @param length - Maximum length
|
|
196
|
+
*/
|
|
197
|
+
binary(length?: number): ColumnDef<Buffer>;
|
|
198
|
+
/**
|
|
199
|
+
* Enum column with specific allowed values.
|
|
200
|
+
* @param values - Array of allowed values
|
|
201
|
+
*/
|
|
202
|
+
enum<T extends readonly string[]>(values: T): ColumnDef<T[number]>;
|
|
203
|
+
/**
|
|
204
|
+
* JSON column.
|
|
205
|
+
*/
|
|
206
|
+
json<T = unknown>(): ColumnDef<T>;
|
|
207
|
+
/**
|
|
208
|
+
* JSONB column (PostgreSQL).
|
|
209
|
+
*/
|
|
210
|
+
jsonb<T = unknown>(): ColumnDef<T>;
|
|
211
|
+
/**
|
|
212
|
+
* UUID column.
|
|
213
|
+
*/
|
|
214
|
+
uuid(): ColumnDef<string>;
|
|
215
|
+
/**
|
|
216
|
+
* Specific database type column.
|
|
217
|
+
* @param type - The database-specific type
|
|
218
|
+
*/
|
|
219
|
+
specificType<T = unknown>(type: string): ColumnDef<T>;
|
|
220
|
+
};
|
|
221
|
+
/**
|
|
222
|
+
* Builds the Knex table schema from column definitions.
|
|
223
|
+
* @internal
|
|
224
|
+
*/
|
|
225
|
+
export declare function buildColumnsSchema(tableBuilder: Knex.CreateTableBuilder, columns: Record<string, ColumnDef<any, any>>): void;
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base column definition that tracks the TypeScript type and nullability.
|
|
3
|
+
*/
|
|
4
|
+
export class ColumnDef {
|
|
5
|
+
_buildFn;
|
|
6
|
+
_isNullable;
|
|
7
|
+
_modifiers;
|
|
8
|
+
/** @internal */
|
|
9
|
+
_type;
|
|
10
|
+
/** @internal */
|
|
11
|
+
_nullable;
|
|
12
|
+
/** @internal */
|
|
13
|
+
_knexBuilder;
|
|
14
|
+
constructor(
|
|
15
|
+
/** @internal */
|
|
16
|
+
_buildFn,
|
|
17
|
+
/** @internal */
|
|
18
|
+
_isNullable = false,
|
|
19
|
+
/** @internal */
|
|
20
|
+
_modifiers = []) {
|
|
21
|
+
this._buildFn = _buildFn;
|
|
22
|
+
this._isNullable = _isNullable;
|
|
23
|
+
this._modifiers = _modifiers;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Makes this column nullable.
|
|
27
|
+
*/
|
|
28
|
+
nullable() {
|
|
29
|
+
return new ColumnDef(this._buildFn, true, [
|
|
30
|
+
...this._modifiers,
|
|
31
|
+
(col) => col.nullable(),
|
|
32
|
+
]);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Sets a default value for this column.
|
|
36
|
+
*/
|
|
37
|
+
defaultTo(value) {
|
|
38
|
+
return new ColumnDef(this._buildFn, this._isNullable, [
|
|
39
|
+
...this._modifiers,
|
|
40
|
+
(col) => col.defaultTo(value),
|
|
41
|
+
]);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Makes this column unique.
|
|
45
|
+
*/
|
|
46
|
+
unique() {
|
|
47
|
+
return new ColumnDef(this._buildFn, this._isNullable, [
|
|
48
|
+
...this._modifiers,
|
|
49
|
+
(col) => col.unique(),
|
|
50
|
+
]);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Makes this column the primary key.
|
|
54
|
+
*/
|
|
55
|
+
primary() {
|
|
56
|
+
return new ColumnDef(this._buildFn, this._isNullable, [
|
|
57
|
+
...this._modifiers,
|
|
58
|
+
(col) => col.primary(),
|
|
59
|
+
]);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Adds an index to this column.
|
|
63
|
+
*/
|
|
64
|
+
index(indexName) {
|
|
65
|
+
return new ColumnDef(this._buildFn, this._isNullable, [
|
|
66
|
+
...this._modifiers,
|
|
67
|
+
(col) => col.index(indexName),
|
|
68
|
+
]);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Adds a comment to this column.
|
|
72
|
+
*/
|
|
73
|
+
comment(comment) {
|
|
74
|
+
return new ColumnDef(this._buildFn, this._isNullable, [
|
|
75
|
+
...this._modifiers,
|
|
76
|
+
(col) => col.comment(comment),
|
|
77
|
+
]);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Sets the collation for this column.
|
|
81
|
+
*/
|
|
82
|
+
collate(collation) {
|
|
83
|
+
return new ColumnDef(this._buildFn, this._isNullable, [
|
|
84
|
+
...this._modifiers,
|
|
85
|
+
(col) => col.collate(collation),
|
|
86
|
+
]);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Adds a references constraint to this column.
|
|
90
|
+
*/
|
|
91
|
+
references(columnName) {
|
|
92
|
+
return new ForeignColumnDef(this._buildFn, this._isNullable, [...this._modifiers], columnName);
|
|
93
|
+
}
|
|
94
|
+
/** @internal */
|
|
95
|
+
_build(tableBuilder, name) {
|
|
96
|
+
let col = this._buildFn(tableBuilder, name);
|
|
97
|
+
if (!this._isNullable) {
|
|
98
|
+
col = col.notNullable();
|
|
99
|
+
}
|
|
100
|
+
for (const modifier of this._modifiers) {
|
|
101
|
+
col = modifier(col);
|
|
102
|
+
}
|
|
103
|
+
return col;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Column definition with foreign key support.
|
|
108
|
+
*/
|
|
109
|
+
export class ForeignColumnDef extends ColumnDef {
|
|
110
|
+
_referencesColumn;
|
|
111
|
+
_inTable;
|
|
112
|
+
_onDelete;
|
|
113
|
+
_onUpdate;
|
|
114
|
+
constructor(buildFn, isNullable, modifiers, _referencesColumn, _inTable, _onDelete, _onUpdate) {
|
|
115
|
+
super(buildFn, isNullable, modifiers);
|
|
116
|
+
this._referencesColumn = _referencesColumn;
|
|
117
|
+
this._inTable = _inTable;
|
|
118
|
+
this._onDelete = _onDelete;
|
|
119
|
+
this._onUpdate = _onUpdate;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Sets the table for the foreign key reference.
|
|
123
|
+
*/
|
|
124
|
+
inTable(tableName) {
|
|
125
|
+
return new ForeignColumnDef(this._buildFn, this._isNullable, this._modifiers, this._referencesColumn, tableName, this._onDelete, this._onUpdate);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Sets the ON DELETE action.
|
|
129
|
+
*/
|
|
130
|
+
onDelete(action) {
|
|
131
|
+
return new ForeignColumnDef(this._buildFn, this._isNullable, this._modifiers, this._referencesColumn, this._inTable, action, this._onUpdate);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Sets the ON UPDATE action.
|
|
135
|
+
*/
|
|
136
|
+
onUpdate(action) {
|
|
137
|
+
return new ForeignColumnDef(this._buildFn, this._isNullable, this._modifiers, this._referencesColumn, this._inTable, this._onDelete, action);
|
|
138
|
+
}
|
|
139
|
+
/** @internal */
|
|
140
|
+
_build(tableBuilder, name) {
|
|
141
|
+
const col = super._build(tableBuilder, name);
|
|
142
|
+
let ref = col.references(this._referencesColumn);
|
|
143
|
+
if (this._inTable)
|
|
144
|
+
ref = ref.inTable(this._inTable);
|
|
145
|
+
if (this._onDelete)
|
|
146
|
+
ref = ref.onDelete(this._onDelete);
|
|
147
|
+
if (this._onUpdate)
|
|
148
|
+
ref = ref.onUpdate(this._onUpdate);
|
|
149
|
+
return col;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Number column definition with additional numeric options.
|
|
154
|
+
*/
|
|
155
|
+
export class NumberColumnDef extends ColumnDef {
|
|
156
|
+
/**
|
|
157
|
+
* Makes this column unsigned (only positive values).
|
|
158
|
+
*/
|
|
159
|
+
unsigned() {
|
|
160
|
+
return new NumberColumnDef((tb, name) => {
|
|
161
|
+
const col = this._buildFn(tb, name);
|
|
162
|
+
return col.unsigned();
|
|
163
|
+
}, this._isNullable, this._modifiers);
|
|
164
|
+
}
|
|
165
|
+
nullable() {
|
|
166
|
+
return new NumberColumnDef(this._buildFn, true, [
|
|
167
|
+
...this._modifiers,
|
|
168
|
+
(col) => col.nullable(),
|
|
169
|
+
]);
|
|
170
|
+
}
|
|
171
|
+
defaultTo(value) {
|
|
172
|
+
return new NumberColumnDef(this._buildFn, this._isNullable, [
|
|
173
|
+
...this._modifiers,
|
|
174
|
+
(col) => col.defaultTo(value),
|
|
175
|
+
]);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Column factory for creating typed column definitions.
|
|
180
|
+
*/
|
|
181
|
+
export const col = {
|
|
182
|
+
/**
|
|
183
|
+
* Auto-incrementing primary key (integer).
|
|
184
|
+
*/
|
|
185
|
+
increments() {
|
|
186
|
+
return new ColumnDef((tb, name) => tb.increments(name));
|
|
187
|
+
},
|
|
188
|
+
/**
|
|
189
|
+
* Big auto-incrementing primary key (bigint).
|
|
190
|
+
*/
|
|
191
|
+
bigIncrements() {
|
|
192
|
+
return new ColumnDef((tb, name) => tb.bigIncrements(name));
|
|
193
|
+
},
|
|
194
|
+
/**
|
|
195
|
+
* Integer column.
|
|
196
|
+
*/
|
|
197
|
+
integer() {
|
|
198
|
+
return new NumberColumnDef((tb, name) => tb.integer(name));
|
|
199
|
+
},
|
|
200
|
+
/**
|
|
201
|
+
* Big integer column.
|
|
202
|
+
*/
|
|
203
|
+
bigInteger() {
|
|
204
|
+
return new ColumnDef((tb, name) => tb.bigInteger(name));
|
|
205
|
+
},
|
|
206
|
+
/**
|
|
207
|
+
* Tiny integer column (0-255).
|
|
208
|
+
*/
|
|
209
|
+
tinyint() {
|
|
210
|
+
return new NumberColumnDef((tb, name) => tb.tinyint(name));
|
|
211
|
+
},
|
|
212
|
+
/**
|
|
213
|
+
* Small integer column.
|
|
214
|
+
*/
|
|
215
|
+
smallint() {
|
|
216
|
+
return new NumberColumnDef((tb, name) => tb.smallint(name));
|
|
217
|
+
},
|
|
218
|
+
/**
|
|
219
|
+
* Medium integer column.
|
|
220
|
+
*/
|
|
221
|
+
mediumint() {
|
|
222
|
+
return new NumberColumnDef((tb, name) => tb.mediumint(name));
|
|
223
|
+
},
|
|
224
|
+
/**
|
|
225
|
+
* Floating-point number column.
|
|
226
|
+
* @param precision - Total number of digits
|
|
227
|
+
* @param scale - Number of decimal places
|
|
228
|
+
*/
|
|
229
|
+
float(precision, scale) {
|
|
230
|
+
return new NumberColumnDef((tb, name) => tb.float(name, precision, scale));
|
|
231
|
+
},
|
|
232
|
+
/**
|
|
233
|
+
* Double precision floating-point column.
|
|
234
|
+
* @param precision - Total number of digits
|
|
235
|
+
* @param scale - Number of decimal places
|
|
236
|
+
*/
|
|
237
|
+
double(precision, scale) {
|
|
238
|
+
return new NumberColumnDef((tb, name) => tb.double(name, precision, scale));
|
|
239
|
+
},
|
|
240
|
+
/**
|
|
241
|
+
* Decimal column for exact numeric values.
|
|
242
|
+
* @param precision - Total number of digits
|
|
243
|
+
* @param scale - Number of decimal places
|
|
244
|
+
*/
|
|
245
|
+
decimal(precision, scale) {
|
|
246
|
+
return new NumberColumnDef((tb, name) => tb.decimal(name, precision, scale));
|
|
247
|
+
},
|
|
248
|
+
/**
|
|
249
|
+
* String column.
|
|
250
|
+
* @param length - Maximum length (default: 255)
|
|
251
|
+
*/
|
|
252
|
+
string(length) {
|
|
253
|
+
return new ColumnDef((tb, name) => tb.string(name, length));
|
|
254
|
+
},
|
|
255
|
+
/**
|
|
256
|
+
* Text column for longer strings.
|
|
257
|
+
* @param textType - Type of text column
|
|
258
|
+
*/
|
|
259
|
+
text(textType) {
|
|
260
|
+
return new ColumnDef((tb, name) => tb.text(name, textType));
|
|
261
|
+
},
|
|
262
|
+
/**
|
|
263
|
+
* Boolean column.
|
|
264
|
+
*/
|
|
265
|
+
boolean() {
|
|
266
|
+
return new ColumnDef((tb, name) => tb.boolean(name));
|
|
267
|
+
},
|
|
268
|
+
/**
|
|
269
|
+
* Date column (without time).
|
|
270
|
+
*/
|
|
271
|
+
date() {
|
|
272
|
+
return new ColumnDef((tb, name) => tb.date(name));
|
|
273
|
+
},
|
|
274
|
+
/**
|
|
275
|
+
* DateTime column.
|
|
276
|
+
* @param options - DateTime options
|
|
277
|
+
*/
|
|
278
|
+
datetime(options) {
|
|
279
|
+
return new ColumnDef((tb, name) => tb.datetime(name, options));
|
|
280
|
+
},
|
|
281
|
+
/**
|
|
282
|
+
* Timestamp column.
|
|
283
|
+
* @param options - Timestamp options
|
|
284
|
+
*/
|
|
285
|
+
timestamp(options) {
|
|
286
|
+
return new ColumnDef((tb, name) => tb.timestamp(name, options));
|
|
287
|
+
},
|
|
288
|
+
/**
|
|
289
|
+
* Time column.
|
|
290
|
+
*/
|
|
291
|
+
time() {
|
|
292
|
+
return new ColumnDef((tb, name) => tb.time(name));
|
|
293
|
+
},
|
|
294
|
+
/**
|
|
295
|
+
* Binary column.
|
|
296
|
+
* @param length - Maximum length
|
|
297
|
+
*/
|
|
298
|
+
binary(length) {
|
|
299
|
+
return new ColumnDef((tb, name) => tb.binary(name, length));
|
|
300
|
+
},
|
|
301
|
+
/**
|
|
302
|
+
* Enum column with specific allowed values.
|
|
303
|
+
* @param values - Array of allowed values
|
|
304
|
+
*/
|
|
305
|
+
enum(values) {
|
|
306
|
+
return new ColumnDef((tb, name) => tb.enum(name, [...values]));
|
|
307
|
+
},
|
|
308
|
+
/**
|
|
309
|
+
* JSON column.
|
|
310
|
+
*/
|
|
311
|
+
json() {
|
|
312
|
+
return new ColumnDef((tb, name) => tb.json(name));
|
|
313
|
+
},
|
|
314
|
+
/**
|
|
315
|
+
* JSONB column (PostgreSQL).
|
|
316
|
+
*/
|
|
317
|
+
jsonb() {
|
|
318
|
+
return new ColumnDef((tb, name) => tb.jsonb(name));
|
|
319
|
+
},
|
|
320
|
+
/**
|
|
321
|
+
* UUID column.
|
|
322
|
+
*/
|
|
323
|
+
uuid() {
|
|
324
|
+
return new ColumnDef((tb, name) => tb.uuid(name));
|
|
325
|
+
},
|
|
326
|
+
/**
|
|
327
|
+
* Specific database type column.
|
|
328
|
+
* @param type - The database-specific type
|
|
329
|
+
*/
|
|
330
|
+
specificType(type) {
|
|
331
|
+
return new ColumnDef((tb, name) => tb.specificType(name, type));
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
/**
|
|
335
|
+
* Builds the Knex table schema from column definitions.
|
|
336
|
+
* @internal
|
|
337
|
+
*/
|
|
338
|
+
export function buildColumnsSchema(tableBuilder, columns) {
|
|
339
|
+
for (const [name, columnDef] of Object.entries(columns)) {
|
|
340
|
+
columnDef._build(tableBuilder, name);
|
|
341
|
+
}
|
|
342
|
+
}
|
package/dist/app/orm.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Handler } from "@ghom/handler";
|
|
2
|
-
import { Knex } from "knex";
|
|
3
|
-
import { TextStyle } from "./util.js";
|
|
4
|
-
import { Table } from "./table.js";
|
|
5
2
|
import { CachedQuery } from "@ghom/query";
|
|
3
|
+
import { type Knex } from "knex";
|
|
4
|
+
import { Table } from "./table.js";
|
|
5
|
+
import { type TextStyle } from "./util.js";
|
|
6
6
|
export interface ILogger {
|
|
7
7
|
log: (message: string) => void;
|
|
8
8
|
error: (error: string | Error) => void;
|
|
@@ -63,7 +63,7 @@ export interface ORMConfig {
|
|
|
63
63
|
export declare class ORM {
|
|
64
64
|
config: ORMConfig | false;
|
|
65
65
|
private _ready;
|
|
66
|
-
|
|
66
|
+
_client?: Knex;
|
|
67
67
|
handler?: Handler<Table<any>>;
|
|
68
68
|
_rawCache?: CachedQuery<[raw: string], Knex.Raw>;
|
|
69
69
|
/**
|
|
@@ -80,6 +80,7 @@ export declare class ORM {
|
|
|
80
80
|
*/
|
|
81
81
|
constructor(config: ORMConfig | false);
|
|
82
82
|
private requireClient;
|
|
83
|
+
get client(): Knex;
|
|
83
84
|
/**
|
|
84
85
|
* Returns true if the ORM has a database client connected.
|
|
85
86
|
*/
|