@c9up/atlas 0.1.18 → 0.2.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/README.md +55 -14
- package/db.darwin-arm64.node +0 -0
- package/db.darwin-x64.node +0 -0
- package/db.linux-arm64-gnu.node +0 -0
- package/db.linux-x64-gnu.node +0 -0
- package/db.win32-x64-msvc.node +0 -0
- package/dist/AtlasProvider.d.ts +6 -0
- package/dist/AtlasProvider.d.ts.map +1 -1
- package/dist/AtlasProvider.js +2 -2
- package/dist/AtlasProvider.js.map +1 -1
- package/dist/BaseEntity.d.ts +171 -7
- package/dist/BaseEntity.d.ts.map +1 -1
- package/dist/BaseEntity.js +339 -31
- package/dist/BaseEntity.js.map +1 -1
- package/dist/BaseModel.d.ts +91 -0
- package/dist/BaseModel.d.ts.map +1 -0
- package/dist/BaseModel.js +193 -0
- package/dist/BaseModel.js.map +1 -0
- package/dist/BaseRepository.d.ts +77 -15
- package/dist/BaseRepository.d.ts.map +1 -1
- package/dist/BaseRepository.js +1423 -354
- package/dist/BaseRepository.js.map +1 -1
- package/dist/ModelQuery.d.ts +429 -11
- package/dist/ModelQuery.d.ts.map +1 -1
- package/dist/ModelQuery.js +1733 -145
- package/dist/ModelQuery.js.map +1 -1
- package/dist/Transaction.d.ts +17 -0
- package/dist/Transaction.d.ts.map +1 -1
- package/dist/Transaction.js +57 -5
- package/dist/Transaction.js.map +1 -1
- package/dist/adapters/NapiDbAdapter.d.ts +33 -4
- package/dist/adapters/NapiDbAdapter.d.ts.map +1 -1
- package/dist/adapters/NapiDbAdapter.js +101 -11
- package/dist/adapters/NapiDbAdapter.js.map +1 -1
- package/dist/console/migrationCommands.d.ts +48 -0
- package/dist/console/migrationCommands.d.ts.map +1 -0
- package/dist/console/migrationCommands.js +220 -0
- package/dist/console/migrationCommands.js.map +1 -0
- package/dist/decorators/entity.d.ts +37 -6
- package/dist/decorators/entity.d.ts.map +1 -1
- package/dist/decorators/entity.js +32 -2
- package/dist/decorators/entity.js.map +1 -1
- package/dist/events.d.ts +64 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +82 -0
- package/dist/events.js.map +1 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/metadata-keys.d.ts +3 -2
- package/dist/metadata-keys.d.ts.map +1 -1
- package/dist/naming/NamingStrategy.d.ts +7 -0
- package/dist/naming/NamingStrategy.d.ts.map +1 -1
- package/dist/naming/NamingStrategy.js +16 -0
- package/dist/naming/NamingStrategy.js.map +1 -1
- package/dist/schema/Migration.d.ts +26 -3
- package/dist/schema/Migration.d.ts.map +1 -1
- package/dist/schema/Migration.js +33 -24
- package/dist/schema/Migration.js.map +1 -1
- package/dist/schema/MigrationRunner.d.ts +43 -32
- package/dist/schema/MigrationRunner.d.ts.map +1 -1
- package/dist/schema/MigrationRunner.js +211 -26
- package/dist/schema/MigrationRunner.js.map +1 -1
- package/dist/schema/Schema.d.ts +57 -0
- package/dist/schema/Schema.d.ts.map +1 -1
- package/dist/schema/Schema.js +138 -3
- package/dist/schema/Schema.js.map +1 -1
- package/dist/schema/SchemaCheck.d.ts.map +1 -1
- package/dist/schema/SchemaCheck.js +3 -1
- package/dist/schema/SchemaCheck.js.map +1 -1
- package/dist/schema/TableBuilder.d.ts +247 -8
- package/dist/schema/TableBuilder.d.ts.map +1 -1
- package/dist/schema/TableBuilder.js +607 -41
- package/dist/schema/TableBuilder.js.map +1 -1
- package/dist/schema/catalog.d.ts +47 -0
- package/dist/schema/catalog.d.ts.map +1 -0
- package/dist/schema/catalog.js +111 -0
- package/dist/schema/catalog.js.map +1 -0
- package/dist/schema/introspect.js.map +1 -1
- package/dist/schema/types.d.ts +150 -1
- package/dist/schema/types.d.ts.map +1 -1
- package/dist/schema/types.js +11 -0
- package/dist/schema/types.js.map +1 -1
- package/dist/services/db.d.ts +6 -0
- package/dist/services/db.d.ts.map +1 -1
- package/dist/services/db.js +17 -0
- package/dist/services/db.js.map +1 -1
- package/dist/testing/DatabaseCleanup.d.ts +7 -4
- package/dist/testing/DatabaseCleanup.d.ts.map +1 -1
- package/dist/testing/DatabaseCleanup.js +21 -18
- package/dist/testing/DatabaseCleanup.js.map +1 -1
- package/dist/testing/Factory.d.ts +70 -5
- package/dist/testing/Factory.d.ts.map +1 -1
- package/dist/testing/Factory.js +209 -10
- package/dist/testing/Factory.js.map +1 -1
- package/index.darwin-arm64.node +0 -0
- package/index.darwin-x64.node +0 -0
- package/index.linux-arm64-gnu.node +0 -0
- package/index.linux-x64-gnu.node +0 -0
- package/index.win32-x64-msvc.node +0 -0
- package/package.json +4 -1
- package/scripts/guard-publish.mjs +15 -0
- package/src/AtlasProvider.ts +8 -1
- package/src/BaseEntity.ts +449 -40
- package/src/BaseModel.ts +324 -0
- package/src/BaseRepository.ts +1659 -371
- package/src/ModelQuery.ts +2290 -203
- package/src/Transaction.ts +68 -5
- package/src/adapters/NapiDbAdapter.ts +159 -10
- package/src/console/migrationCommands.ts +258 -0
- package/src/decorators/entity.ts +53 -6
- package/src/events.ts +112 -0
- package/src/index.ts +19 -0
- package/src/metadata-keys.ts +3 -2
- package/src/naming/NamingStrategy.ts +23 -0
- package/src/schema/Migration.ts +42 -3
- package/src/schema/MigrationRunner.ts +270 -27
- package/src/schema/Schema.ts +210 -3
- package/src/schema/SchemaCheck.ts +7 -2
- package/src/schema/TableBuilder.ts +735 -41
- package/src/schema/catalog.ts +166 -0
- package/src/schema/introspect.ts +3 -4
- package/src/schema/types.ts +137 -2
- package/src/services/db.ts +28 -0
- package/src/testing/DatabaseCleanup.ts +23 -22
- package/src/testing/Factory.ts +332 -15
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dialect-aware catalog queries — "which base tables exist right now".
|
|
3
|
+
*
|
|
4
|
+
* The single source of truth for listing user tables, shared by the migration
|
|
5
|
+
* runner's drop/wipe and the test-time `truncateAll`. Each query hits the
|
|
6
|
+
* dialect's own catalog (`sqlite_master`, `pg_tables`, `information_schema`);
|
|
7
|
+
* they are framework introspection, not user queries, so they stay raw SQL.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { AtlasDialect } from "../query/native.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The minimal connection surface these helpers need — just `query`/`execute`.
|
|
14
|
+
* Narrower than `AsyncDatabaseConnection` on purpose, so the migration runner's
|
|
15
|
+
* `DatabaseAdapter` (which has no `dialect`/`ping`) satisfies it too.
|
|
16
|
+
*/
|
|
17
|
+
export interface CatalogConnection {
|
|
18
|
+
query<T = Record<string, unknown>>(
|
|
19
|
+
sql: string,
|
|
20
|
+
params?: unknown[],
|
|
21
|
+
): Promise<T[]>;
|
|
22
|
+
// Return value is ignored — the two adapter shapes disagree on it
|
|
23
|
+
// (`void` vs `{ rowsAffected }`), and these helpers only run it for effect.
|
|
24
|
+
execute(sql: string, params?: unknown[]): Promise<unknown>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Prefix for framework-private tables (migrations bookkeeping), following the
|
|
29
|
+
* same `ream_`-prefix convention as AdonisJS's `adonis_`.
|
|
30
|
+
*/
|
|
31
|
+
const FRAMEWORK_PREFIX = "ream_";
|
|
32
|
+
|
|
33
|
+
export interface ListTablesOptions {
|
|
34
|
+
/**
|
|
35
|
+
* Include `ream_*` framework tables (the migrations table). Off by default
|
|
36
|
+
* so cleanup helpers leave the bookkeeping intact; `fresh()` turns it on
|
|
37
|
+
* because it re-creates the tracking table afterwards.
|
|
38
|
+
*/
|
|
39
|
+
includeFrameworkTables?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** List every base table in the current schema/database, for `dialect`. */
|
|
43
|
+
export async function listUserTables(
|
|
44
|
+
db: CatalogConnection,
|
|
45
|
+
dialect: AtlasDialect,
|
|
46
|
+
options: ListTablesOptions = {},
|
|
47
|
+
): Promise<string[]> {
|
|
48
|
+
let sql: string;
|
|
49
|
+
switch (dialect) {
|
|
50
|
+
case "sqlite":
|
|
51
|
+
sql =
|
|
52
|
+
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'";
|
|
53
|
+
break;
|
|
54
|
+
case "postgres":
|
|
55
|
+
sql =
|
|
56
|
+
"SELECT tablename AS name FROM pg_tables WHERE schemaname = current_schema()";
|
|
57
|
+
break;
|
|
58
|
+
case "mysql":
|
|
59
|
+
sql =
|
|
60
|
+
"SELECT table_name AS name FROM information_schema.tables WHERE table_schema = DATABASE() AND table_type = 'BASE TABLE'";
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
const rows = await db.query<{ name: unknown }>(sql);
|
|
64
|
+
const names: string[] = [];
|
|
65
|
+
for (const row of rows) {
|
|
66
|
+
if (typeof row.name !== "string") continue;
|
|
67
|
+
if (
|
|
68
|
+
!options.includeFrameworkTables &&
|
|
69
|
+
row.name.startsWith(FRAMEWORK_PREFIX)
|
|
70
|
+
) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
names.push(row.name);
|
|
74
|
+
}
|
|
75
|
+
return names;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Does base table `name` exist right now, for `dialect`? The name is passed as a
|
|
80
|
+
* bound parameter (never interpolated) so this is injection-safe on every
|
|
81
|
+
* dialect. Backs `Schema.hasTable` (Adonis Lucid parity).
|
|
82
|
+
*/
|
|
83
|
+
export async function tableExists(
|
|
84
|
+
db: CatalogConnection,
|
|
85
|
+
dialect: AtlasDialect,
|
|
86
|
+
name: string,
|
|
87
|
+
): Promise<boolean> {
|
|
88
|
+
const ph = dialect === "postgres" ? "$1" : "?";
|
|
89
|
+
let sql: string;
|
|
90
|
+
switch (dialect) {
|
|
91
|
+
case "sqlite":
|
|
92
|
+
sql = `SELECT 1 FROM sqlite_master WHERE type='table' AND name = ${ph}`;
|
|
93
|
+
break;
|
|
94
|
+
case "postgres":
|
|
95
|
+
sql = `SELECT 1 FROM pg_tables WHERE schemaname = current_schema() AND tablename = ${ph}`;
|
|
96
|
+
break;
|
|
97
|
+
case "mysql":
|
|
98
|
+
sql = `SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = ${ph} AND table_type = 'BASE TABLE'`;
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
const rows = await db.query(sql, [name]);
|
|
102
|
+
return rows.length > 0;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Does column `column` exist on table `table` right now, for `dialect`? Both
|
|
107
|
+
* names are bound parameters. Backs `Schema.hasColumn` (Adonis Lucid parity).
|
|
108
|
+
*/
|
|
109
|
+
export async function columnExists(
|
|
110
|
+
db: CatalogConnection,
|
|
111
|
+
dialect: AtlasDialect,
|
|
112
|
+
table: string,
|
|
113
|
+
column: string,
|
|
114
|
+
): Promise<boolean> {
|
|
115
|
+
if (dialect === "sqlite") {
|
|
116
|
+
// PRAGMA introspection via the table-valued function form, which accepts a
|
|
117
|
+
// bound table-name argument (unlike `PRAGMA table_info(x)`).
|
|
118
|
+
const rows = await db.query(
|
|
119
|
+
"SELECT 1 FROM pragma_table_info(?) WHERE name = ?",
|
|
120
|
+
[table, column],
|
|
121
|
+
);
|
|
122
|
+
return rows.length > 0;
|
|
123
|
+
}
|
|
124
|
+
const [p1, p2] = dialect === "postgres" ? ["$1", "$2"] : ["?", "?"];
|
|
125
|
+
const schemaPredicate =
|
|
126
|
+
dialect === "postgres"
|
|
127
|
+
? "table_schema = current_schema()"
|
|
128
|
+
: "table_schema = DATABASE()";
|
|
129
|
+
const rows = await db.query(
|
|
130
|
+
`SELECT 1 FROM information_schema.columns WHERE ${schemaPredicate} AND table_name = ${p1} AND column_name = ${p2}`,
|
|
131
|
+
[table, column],
|
|
132
|
+
);
|
|
133
|
+
return rows.length > 0;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Run `fn` with foreign-key enforcement suspended, then restore it — the toggle
|
|
138
|
+
* runs even if `fn` throws. Lets a bulk drop/truncate ignore inter-table FK
|
|
139
|
+
* order. Postgres has no session-level switch (it uses `TRUNCATE … CASCADE` /
|
|
140
|
+
* `DROP … CASCADE` instead), so this is a no-op there.
|
|
141
|
+
*/
|
|
142
|
+
export async function withoutForeignKeys<T>(
|
|
143
|
+
db: CatalogConnection,
|
|
144
|
+
dialect: AtlasDialect,
|
|
145
|
+
fn: () => Promise<T>,
|
|
146
|
+
): Promise<T> {
|
|
147
|
+
const off =
|
|
148
|
+
dialect === "sqlite"
|
|
149
|
+
? "PRAGMA foreign_keys = OFF"
|
|
150
|
+
: dialect === "mysql"
|
|
151
|
+
? "SET FOREIGN_KEY_CHECKS = 0"
|
|
152
|
+
: null;
|
|
153
|
+
const on =
|
|
154
|
+
dialect === "sqlite"
|
|
155
|
+
? "PRAGMA foreign_keys = ON"
|
|
156
|
+
: dialect === "mysql"
|
|
157
|
+
? "SET FOREIGN_KEY_CHECKS = 1"
|
|
158
|
+
: null;
|
|
159
|
+
|
|
160
|
+
if (off) await db.execute(off);
|
|
161
|
+
try {
|
|
162
|
+
return await fn();
|
|
163
|
+
} finally {
|
|
164
|
+
if (on) await db.execute(on);
|
|
165
|
+
}
|
|
166
|
+
}
|
package/src/schema/introspect.ts
CHANGED
|
@@ -65,9 +65,7 @@ async function introspectSqlite(
|
|
|
65
65
|
db: SchemaIntrospectable,
|
|
66
66
|
table: string,
|
|
67
67
|
): Promise<IntrospectedColumn[] | null> {
|
|
68
|
-
const rows = await db.query(
|
|
69
|
-
`SELECT * FROM pragma_table_info('${table}')`,
|
|
70
|
-
);
|
|
68
|
+
const rows = await db.query(`SELECT * FROM pragma_table_info('${table}')`);
|
|
71
69
|
if (rows.length === 0) return null; // unknown table → no columns
|
|
72
70
|
return rows.map((r) => ({
|
|
73
71
|
name: String(r.name),
|
|
@@ -128,6 +126,7 @@ async function introspectMysql(
|
|
|
128
126
|
hasDefault:
|
|
129
127
|
(r.column_default ?? r.COLUMN_DEFAULT) !== null &&
|
|
130
128
|
(r.column_default ?? r.COLUMN_DEFAULT) !== undefined,
|
|
131
|
-
primaryKey:
|
|
129
|
+
primaryKey:
|
|
130
|
+
String(r.column_key ?? r.COLUMN_KEY ?? "").toUpperCase() === "PRI",
|
|
132
131
|
}));
|
|
133
132
|
}
|
package/src/schema/types.ts
CHANGED
|
@@ -6,33 +6,69 @@
|
|
|
6
6
|
export type ColumnType =
|
|
7
7
|
| "string"
|
|
8
8
|
| "text"
|
|
9
|
+
| "mediumtext"
|
|
10
|
+
| "longtext"
|
|
9
11
|
| "integer"
|
|
12
|
+
| "tinyint"
|
|
13
|
+
| "smallint"
|
|
14
|
+
| "mediumint"
|
|
10
15
|
| "bigInteger"
|
|
11
16
|
| "decimal"
|
|
17
|
+
| "float"
|
|
18
|
+
| "double"
|
|
12
19
|
| "boolean"
|
|
13
20
|
| "date"
|
|
21
|
+
| "time"
|
|
14
22
|
| "timestamp"
|
|
15
23
|
| "timestamptz"
|
|
16
24
|
| "uuid"
|
|
17
25
|
| "json"
|
|
18
|
-
| "
|
|
26
|
+
| "jsonb"
|
|
27
|
+
| "binary"
|
|
28
|
+
| "enum"
|
|
29
|
+
| "specificType";
|
|
19
30
|
|
|
20
31
|
/** Maps our logical type names to the ColumnTypeKind expected by the Rust compiler. */
|
|
21
32
|
export const TYPE_KIND_MAP: Record<ColumnType, string> = {
|
|
22
33
|
string: "string",
|
|
23
34
|
text: "text",
|
|
35
|
+
mediumtext: "mediumText",
|
|
36
|
+
longtext: "longText",
|
|
24
37
|
integer: "integer",
|
|
38
|
+
tinyint: "tinyInt",
|
|
39
|
+
smallint: "smallInt",
|
|
40
|
+
mediumint: "mediumInt",
|
|
25
41
|
bigInteger: "bigInteger",
|
|
26
42
|
decimal: "decimal",
|
|
43
|
+
float: "float",
|
|
44
|
+
double: "double",
|
|
27
45
|
boolean: "boolean",
|
|
28
46
|
date: "date",
|
|
47
|
+
time: "time",
|
|
29
48
|
timestamp: "timestamp",
|
|
30
49
|
timestamptz: "timestamptz",
|
|
31
50
|
uuid: "uuid",
|
|
32
51
|
json: "json",
|
|
52
|
+
jsonb: "jsonb",
|
|
33
53
|
binary: "binary",
|
|
54
|
+
enum: "enum",
|
|
55
|
+
specificType: "specificType",
|
|
34
56
|
};
|
|
35
57
|
|
|
58
|
+
/** MySQL text width (Lucid/Knex `text(name, textType)`). `TEXT` on pg/sqlite. */
|
|
59
|
+
export type TextVariant = "text" | "mediumtext" | "longtext";
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* SQL referential action for a foreign key's `ON DELETE` / `ON UPDATE`
|
|
63
|
+
* (Lucid parity). Validated case-insensitively by the Rust compiler.
|
|
64
|
+
*/
|
|
65
|
+
export type ReferentialAction =
|
|
66
|
+
| "cascade"
|
|
67
|
+
| "restrict"
|
|
68
|
+
| "set null"
|
|
69
|
+
| "set default"
|
|
70
|
+
| "no action";
|
|
71
|
+
|
|
36
72
|
export interface ColumnDefinition {
|
|
37
73
|
name: string;
|
|
38
74
|
type: ColumnType;
|
|
@@ -44,8 +80,29 @@ export interface ColumnDefinition {
|
|
|
44
80
|
/** Dialect-appropriate auto-increment / identity column (Lucid `increments`). Implies primary key. */
|
|
45
81
|
autoIncrement?: boolean;
|
|
46
82
|
unique: boolean;
|
|
83
|
+
/** MySQL `UNSIGNED` numeric modifier (Lucid `unsigned()`). No-op on pg/sqlite. */
|
|
84
|
+
unsigned?: boolean;
|
|
85
|
+
/** Allowed values for an `enum` column. */
|
|
86
|
+
values?: string[];
|
|
87
|
+
/**
|
|
88
|
+
* Verbatim SQL type for a `specificType` column. Validated by the Rust
|
|
89
|
+
* compiler against a narrow grammar before it reaches DDL — it is
|
|
90
|
+
* interpolated, not bound.
|
|
91
|
+
*/
|
|
92
|
+
rawType?: string;
|
|
93
|
+
/** Column comment. Inline on MySQL, `COMMENT ON COLUMN` on pg, dropped on SQLite. */
|
|
94
|
+
comment?: string;
|
|
95
|
+
/** Column collation, e.g. `utf8mb4_unicode_ci`. Validated as an identifier. */
|
|
96
|
+
collate?: string;
|
|
97
|
+
/** MySQL-only placement of an added column (`FIRST` / `AFTER col`). */
|
|
98
|
+
position?: ColumnPosition;
|
|
47
99
|
defaultValue?: string;
|
|
48
|
-
references?: {
|
|
100
|
+
references?: {
|
|
101
|
+
table: string;
|
|
102
|
+
column: string;
|
|
103
|
+
onDelete?: ReferentialAction;
|
|
104
|
+
onUpdate?: ReferentialAction;
|
|
105
|
+
};
|
|
49
106
|
}
|
|
50
107
|
|
|
51
108
|
export interface IndexDefinition {
|
|
@@ -53,3 +110,81 @@ export interface IndexDefinition {
|
|
|
53
110
|
columns: string[];
|
|
54
111
|
unique: boolean;
|
|
55
112
|
}
|
|
113
|
+
|
|
114
|
+
/** MySQL-only placement of an added column. Mirrors the Rust `ColumnPosition`. */
|
|
115
|
+
export type ColumnPosition = { at: "first" } | { at: "after"; column: string };
|
|
116
|
+
|
|
117
|
+
/** A scalar a CHECK constraint can compare against. DDL cannot bind params — these are quoted, not bound. */
|
|
118
|
+
export type CheckValue = string | number | boolean | null;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* A CHECK predicate. Structured rather than free-form SQL so each variant is
|
|
122
|
+
* safe by construction; `raw` is the escape hatch and is exactly as trusted as
|
|
123
|
+
* `schema.raw()`. Mirrors the Rust `CheckExpr` tagged union.
|
|
124
|
+
*/
|
|
125
|
+
export type CheckExpression =
|
|
126
|
+
| { check: "positive"; column: string }
|
|
127
|
+
| { check: "negative"; column: string }
|
|
128
|
+
| { check: "in"; column: string; values: CheckValue[] }
|
|
129
|
+
| { check: "notIn"; column: string; values: CheckValue[] }
|
|
130
|
+
| { check: "between"; column: string; ranges: CheckValue[][] }
|
|
131
|
+
| { check: "length"; column: string; operator: string; length: number }
|
|
132
|
+
| { check: "regex"; column: string; pattern: string }
|
|
133
|
+
| { check: "raw"; predicate: string };
|
|
134
|
+
|
|
135
|
+
/** Comparison operator accepted by `checkLength` (Lucid/Knex). */
|
|
136
|
+
export type CheckOperator = "=" | "!=" | "<>" | "<" | "<=" | ">" | ">=";
|
|
137
|
+
|
|
138
|
+
/** A multi-column foreign key target. Mirrors the Rust `ForeignKeyRefMulti`. */
|
|
139
|
+
export interface ForeignKeyReference {
|
|
140
|
+
table: string;
|
|
141
|
+
columns: string[];
|
|
142
|
+
onDelete?: ReferentialAction;
|
|
143
|
+
onUpdate?: ReferentialAction;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** A table-level constraint. Mirrors the Rust `TableConstraint` tagged union. */
|
|
147
|
+
export type TableConstraintSpec =
|
|
148
|
+
| { constraint: "primary"; name?: string; columns: string[] }
|
|
149
|
+
| { constraint: "unique"; name?: string; columns: string[] }
|
|
150
|
+
| {
|
|
151
|
+
constraint: "foreign";
|
|
152
|
+
name?: string;
|
|
153
|
+
columns: string[];
|
|
154
|
+
references: ForeignKeyReference;
|
|
155
|
+
}
|
|
156
|
+
| { constraint: "check"; name?: string; expr: CheckExpression };
|
|
157
|
+
|
|
158
|
+
/** Storage options for `CREATE TABLE`. Mirrors the Rust `TableOptions`. */
|
|
159
|
+
export interface TableOptionsSpec {
|
|
160
|
+
engine?: string;
|
|
161
|
+
charset?: string;
|
|
162
|
+
collate?: string;
|
|
163
|
+
comment?: string;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* One ALTER TABLE operation, in call order (Lucid/Knex semantics). Mirrors the
|
|
168
|
+
* Rust `AlterOp` tagged union — the `op` tags and field names are the wire
|
|
169
|
+
* contract with `compile_alter_table`.
|
|
170
|
+
*/
|
|
171
|
+
export type AlterOperation =
|
|
172
|
+
| { op: "addColumn"; column: ColumnDefinition }
|
|
173
|
+
| { op: "dropColumn"; name: string }
|
|
174
|
+
| { op: "renameColumn"; from: string; to: string }
|
|
175
|
+
| {
|
|
176
|
+
op: "alterColumn";
|
|
177
|
+
column: ColumnDefinition;
|
|
178
|
+
/**
|
|
179
|
+
* Tri-state. `undefined` leaves nullability untouched so a bare type
|
|
180
|
+
* change never silently adds a NOT NULL — only an explicit
|
|
181
|
+
* `.nullable()` / `.notNullable()` before `.alter()` sets it.
|
|
182
|
+
*/
|
|
183
|
+
setNullable?: boolean;
|
|
184
|
+
}
|
|
185
|
+
| { op: "setNullable"; name: string; nullable: boolean }
|
|
186
|
+
| { op: "addConstraint"; constraint: TableConstraintSpec }
|
|
187
|
+
| { op: "dropConstraint"; name: string }
|
|
188
|
+
| { op: "dropPrimary"; name?: string }
|
|
189
|
+
| { op: "dropUnique"; name: string }
|
|
190
|
+
| { op: "dropForeign"; name: string };
|
package/src/services/db.ts
CHANGED
|
@@ -49,6 +49,34 @@ export function getDb(): AsyncDatabaseConnection | undefined {
|
|
|
49
49
|
return instance;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
// Named-connection registry — backs `BaseModel.connection = 'analytics'` so a
|
|
53
|
+
// model can resolve a non-default connection from a plain import (AdonisJS
|
|
54
|
+
// `static connection`). Populated by AtlasProvider for every opened connection.
|
|
55
|
+
const namedConnections = new Map<string, AsyncDatabaseConnection>();
|
|
56
|
+
|
|
57
|
+
/** @internal Register a named connection (called by AtlasProvider.boot per connection). */
|
|
58
|
+
export function registerConnection(
|
|
59
|
+
name: string,
|
|
60
|
+
connection: AsyncDatabaseConnection,
|
|
61
|
+
): void {
|
|
62
|
+
namedConnections.set(name, connection);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** @internal Unregister a named connection IF it still points at `connection`. */
|
|
66
|
+
export function unregisterConnection(
|
|
67
|
+
name: string,
|
|
68
|
+
connection: AsyncDatabaseConnection,
|
|
69
|
+
): void {
|
|
70
|
+
if (namedConnections.get(name) === connection) namedConnections.delete(name);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** @internal Resolve a named connection (for `BaseModel.connection`), or `undefined`. */
|
|
74
|
+
export function getConnection(
|
|
75
|
+
name: string,
|
|
76
|
+
): AsyncDatabaseConnection | undefined {
|
|
77
|
+
return namedConnections.get(name);
|
|
78
|
+
}
|
|
79
|
+
|
|
52
80
|
const db: DbService = new Proxy({} as DbService, {
|
|
53
81
|
get(_target, prop) {
|
|
54
82
|
// `raw` is a pure builder (no connection needed) — available pre-boot too.
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import type { AsyncDatabaseConnection } from "../adapters/NapiDbAdapter.js";
|
|
8
|
-
import { compileStatementNative
|
|
8
|
+
import { compileStatementNative } from "../query/native.js";
|
|
9
|
+
import { listUserTables, withoutForeignKeys } from "../schema/catalog.js";
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Wrap a test in a savepoint that is rolled back after.
|
|
@@ -21,28 +22,28 @@ export async function useTransaction(
|
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
|
-
*
|
|
25
|
+
* Empty every user table (leaves `ream_*` framework tables and dialect
|
|
26
|
+
* internals alone). Works on all three dialects — the table list comes from the
|
|
27
|
+
* shared dialect-aware catalog helper, not a SQLite-only `sqlite_master` query.
|
|
25
28
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
+
* Foreign keys are suspended for the duration so the delete order doesn't
|
|
30
|
+
* matter, and restored even if a statement throws. The row removal itself goes
|
|
31
|
+
* through the Rust compiler (`DELETE` — cross-dialect and transaction-friendly,
|
|
32
|
+
* unlike `TRUNCATE` which auto-commits on MySQL).
|
|
29
33
|
*/
|
|
30
34
|
export async function truncateAll(db: AsyncDatabaseConnection): Promise<void> {
|
|
31
|
-
//
|
|
32
|
-
//
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
);
|
|
46
|
-
await db.execute(compiled.statements[0], compiled.params);
|
|
47
|
-
}
|
|
35
|
+
// The connection's own dialect, not the module default — correct even when
|
|
36
|
+
// an app runs several connections on different engines.
|
|
37
|
+
const dialect = db.dialect;
|
|
38
|
+
const tables = await listUserTables(db, dialect);
|
|
39
|
+
if (tables.length === 0) return;
|
|
40
|
+
await withoutForeignKeys(db, dialect, async () => {
|
|
41
|
+
for (const name of tables) {
|
|
42
|
+
const compiled = compileStatementNative(
|
|
43
|
+
{ kind: "delete", table: name, wheres: [] },
|
|
44
|
+
dialect,
|
|
45
|
+
);
|
|
46
|
+
await db.execute(compiled.statements[0], compiled.params);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
48
49
|
}
|