@c9up/atlas 0.2.0 → 0.2.1
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 +8 -0
- 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 +60 -7
- package/dist/AtlasProvider.d.ts.map +1 -1
- package/dist/AtlasProvider.js +204 -38
- package/dist/AtlasProvider.js.map +1 -1
- package/dist/BaseEntity.d.ts +17 -0
- package/dist/BaseEntity.d.ts.map +1 -1
- package/dist/BaseEntity.js.map +1 -1
- package/dist/BaseModel.d.ts +63 -27
- package/dist/BaseModel.d.ts.map +1 -1
- package/dist/BaseModel.js +108 -51
- package/dist/BaseModel.js.map +1 -1
- package/dist/BaseRepository.d.ts +11 -1
- package/dist/BaseRepository.d.ts.map +1 -1
- package/dist/BaseRepository.js +95 -20
- package/dist/BaseRepository.js.map +1 -1
- package/dist/ConnectionManager.d.ts +106 -0
- package/dist/ConnectionManager.d.ts.map +1 -0
- package/dist/ConnectionManager.js +228 -0
- package/dist/ConnectionManager.js.map +1 -0
- package/dist/ModelQuery.d.ts +256 -43
- package/dist/ModelQuery.d.ts.map +1 -1
- package/dist/ModelQuery.js +899 -231
- package/dist/ModelQuery.js.map +1 -1
- package/dist/Transaction.d.ts +54 -1
- package/dist/Transaction.d.ts.map +1 -1
- package/dist/Transaction.js +121 -59
- package/dist/Transaction.js.map +1 -1
- package/dist/adapters/NapiDbAdapter.d.ts +11 -0
- package/dist/adapters/NapiDbAdapter.d.ts.map +1 -1
- package/dist/adapters/NapiDbAdapter.js +35 -7
- package/dist/adapters/NapiDbAdapter.js.map +1 -1
- package/dist/console/contract.d.ts +62 -0
- package/dist/console/contract.d.ts.map +1 -0
- package/dist/console/contract.js +38 -0
- package/dist/console/contract.js.map +1 -0
- package/dist/console/factoryCommands.d.ts +23 -0
- package/dist/console/factoryCommands.d.ts.map +1 -0
- package/dist/console/factoryCommands.js +62 -0
- package/dist/console/factoryCommands.js.map +1 -0
- package/dist/console/migrationCommands.d.ts +38 -8
- package/dist/console/migrationCommands.d.ts.map +1 -1
- package/dist/console/migrationCommands.js +157 -50
- package/dist/console/migrationCommands.js.map +1 -1
- package/dist/console/schemaCheckCommand.d.ts +11 -17
- package/dist/console/schemaCheckCommand.d.ts.map +1 -1
- package/dist/console/schemaCheckCommand.js +21 -15
- package/dist/console/schemaCheckCommand.js.map +1 -1
- package/dist/console/schemaDumpCommand.d.ts +30 -0
- package/dist/console/schemaDumpCommand.d.ts.map +1 -0
- package/dist/console/schemaDumpCommand.js +69 -0
- package/dist/console/schemaDumpCommand.js.map +1 -0
- package/dist/console/schemaGenerateCommand.d.ts +100 -0
- package/dist/console/schemaGenerateCommand.d.ts.map +1 -0
- package/dist/console/schemaGenerateCommand.js +246 -0
- package/dist/console/schemaGenerateCommand.js.map +1 -0
- package/dist/console/seederCommands.d.ts +46 -0
- package/dist/console/seederCommands.d.ts.map +1 -0
- package/dist/console/seederCommands.js +136 -0
- package/dist/console/seederCommands.js.map +1 -0
- package/dist/decorators/entity.d.ts +13 -2
- package/dist/decorators/entity.d.ts.map +1 -1
- package/dist/decorators/entity.js +10 -0
- package/dist/decorators/entity.js.map +1 -1
- package/dist/decorators/hooks.d.ts +3 -3
- package/dist/decorators/hooks.d.ts.map +1 -1
- package/dist/decorators/hooks.js.map +1 -1
- package/dist/events.d.ts +5 -0
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js.map +1 -1
- package/dist/factories.d.ts +8 -0
- package/dist/factories.d.ts.map +1 -0
- package/dist/factories.js +8 -0
- package/dist/factories.js.map +1 -0
- package/dist/index.d.ts +11 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -4
- package/dist/index.js.map +1 -1
- package/dist/lucid-schema.d.ts +15 -0
- package/dist/lucid-schema.d.ts.map +1 -0
- package/dist/lucid-schema.js +16 -0
- package/dist/lucid-schema.js.map +1 -0
- package/dist/orm.d.ts +14 -0
- package/dist/orm.d.ts.map +1 -0
- package/dist/orm.js +14 -0
- package/dist/orm.js.map +1 -0
- package/dist/query/DatabaseQueryBuilder.d.ts +557 -0
- package/dist/query/DatabaseQueryBuilder.d.ts.map +1 -0
- package/dist/query/DatabaseQueryBuilder.js +1798 -0
- package/dist/query/DatabaseQueryBuilder.js.map +1 -0
- package/dist/query/DmlBuilder.d.ts +62 -0
- package/dist/query/DmlBuilder.d.ts.map +1 -0
- package/dist/query/DmlBuilder.js +84 -0
- package/dist/query/DmlBuilder.js.map +1 -0
- package/dist/query/QueryBuilder.d.ts +5 -0
- package/dist/query/QueryBuilder.d.ts.map +1 -1
- package/dist/query/QueryBuilder.js +7 -0
- package/dist/query/QueryBuilder.js.map +1 -1
- package/dist/query/RawQueryBuilder.d.ts +47 -0
- package/dist/query/RawQueryBuilder.d.ts.map +1 -0
- package/dist/query/RawQueryBuilder.js +138 -0
- package/dist/query/RawQueryBuilder.js.map +1 -0
- package/dist/query/interpolate.d.ts +45 -0
- package/dist/query/interpolate.d.ts.map +1 -0
- package/dist/query/interpolate.js +51 -0
- package/dist/query/interpolate.js.map +1 -0
- package/dist/query/native.d.ts +8 -0
- package/dist/query/native.d.ts.map +1 -1
- package/dist/query/native.js +14 -0
- package/dist/query/native.js.map +1 -1
- package/dist/query/operators.d.ts +4 -0
- package/dist/query/operators.d.ts.map +1 -0
- package/dist/query/operators.js +27 -0
- package/dist/query/operators.js.map +1 -0
- package/dist/schema/Migration.d.ts +31 -7
- package/dist/schema/Migration.d.ts.map +1 -1
- package/dist/schema/Migration.js +34 -7
- package/dist/schema/Migration.js.map +1 -1
- package/dist/schema/MigrationRunner.d.ts +56 -6
- package/dist/schema/MigrationRunner.d.ts.map +1 -1
- package/dist/schema/MigrationRunner.js +382 -112
- package/dist/schema/MigrationRunner.js.map +1 -1
- package/dist/schema/Schema.d.ts +59 -2
- package/dist/schema/Schema.d.ts.map +1 -1
- package/dist/schema/Schema.js +143 -9
- package/dist/schema/Schema.js.map +1 -1
- package/dist/schema/SchemaDumper.d.ts +121 -0
- package/dist/schema/SchemaDumper.d.ts.map +1 -0
- package/dist/schema/SchemaDumper.js +365 -0
- package/dist/schema/SchemaDumper.js.map +1 -0
- package/dist/schema/Seeder.d.ts +13 -0
- package/dist/schema/Seeder.d.ts.map +1 -1
- package/dist/schema/Seeder.js +28 -5
- package/dist/schema/Seeder.js.map +1 -1
- package/dist/schema/TableBuilder.d.ts +27 -8
- package/dist/schema/TableBuilder.d.ts.map +1 -1
- package/dist/schema/TableBuilder.js +50 -12
- package/dist/schema/TableBuilder.js.map +1 -1
- package/dist/schema/catalog.d.ts +25 -1
- package/dist/schema/catalog.d.ts.map +1 -1
- package/dist/schema/catalog.js +69 -22
- package/dist/schema/catalog.js.map +1 -1
- package/dist/schema/introspect.d.ts +1 -1
- package/dist/schema/introspect.d.ts.map +1 -1
- package/dist/schema/introspect.js +26 -11
- package/dist/schema/introspect.js.map +1 -1
- package/dist/seeders.d.ts +7 -0
- package/dist/seeders.d.ts.map +1 -0
- package/dist/seeders.js +7 -0
- package/dist/seeders.js.map +1 -0
- package/dist/services/db.d.ts +120 -9
- package/dist/services/db.d.ts.map +1 -1
- package/dist/services/db.js +182 -26
- package/dist/services/db.js.map +1 -1
- package/dist/testing/DatabaseCleanup.d.ts +29 -6
- package/dist/testing/DatabaseCleanup.d.ts.map +1 -1
- package/dist/testing/DatabaseCleanup.js +44 -17
- package/dist/testing/DatabaseCleanup.js.map +1 -1
- package/dist/testing/DbAssertions.d.ts +48 -0
- package/dist/testing/DbAssertions.d.ts.map +1 -0
- package/dist/testing/DbAssertions.js +80 -0
- package/dist/testing/DbAssertions.js.map +1 -0
- package/dist/testing/Factory.d.ts +111 -25
- package/dist/testing/Factory.d.ts.map +1 -1
- package/dist/testing/Factory.js +331 -56
- package/dist/testing/Factory.js.map +1 -1
- package/dist/testing/TestUtils.d.ts +49 -0
- package/dist/testing/TestUtils.d.ts.map +1 -0
- package/dist/testing/TestUtils.js +79 -0
- package/dist/testing/TestUtils.js.map +1 -0
- package/dist/testing/index.d.ts +3 -1
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +3 -1
- package/dist/testing/index.js.map +1 -1
- package/dist/types/relations.d.ts +31 -0
- package/dist/types/relations.d.ts.map +1 -0
- package/dist/types/relations.js +20 -0
- package/dist/types/relations.js.map +1 -0
- 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 +30 -2
- package/src/AtlasProvider.ts +297 -51
- package/src/BaseEntity.ts +29 -0
- package/src/BaseModel.ts +174 -37
- package/src/BaseRepository.ts +136 -17
- package/src/ConnectionManager.ts +298 -0
- package/src/ModelQuery.ts +1309 -260
- package/src/Transaction.ts +194 -60
- package/src/adapters/NapiDbAdapter.ts +72 -14
- package/src/console/contract.ts +96 -0
- package/src/console/factoryCommands.ts +73 -0
- package/src/console/migrationCommands.ts +253 -63
- package/src/console/schemaCheckCommand.ts +26 -22
- package/src/console/schemaDumpCommand.ts +96 -0
- package/src/console/schemaGenerateCommand.ts +380 -0
- package/src/console/seederCommands.ts +174 -0
- package/src/decorators/entity.ts +18 -2
- package/src/decorators/hooks.ts +5 -3
- package/src/events.ts +5 -0
- package/src/factories.ts +10 -0
- package/src/index.ts +50 -3
- package/src/lucid-schema.ts +22 -0
- package/src/orm.ts +13 -0
- package/src/query/DatabaseQueryBuilder.ts +2724 -0
- package/src/query/DmlBuilder.ts +131 -0
- package/src/query/QueryBuilder.ts +8 -0
- package/src/query/RawQueryBuilder.ts +173 -0
- package/src/query/interpolate.ts +73 -0
- package/src/query/native.ts +29 -0
- package/src/query/operators.ts +27 -0
- package/src/schema/Migration.ts +36 -7
- package/src/schema/MigrationRunner.ts +483 -119
- package/src/schema/Schema.ts +168 -19
- package/src/schema/SchemaDumper.ts +518 -0
- package/src/schema/Seeder.ts +47 -10
- package/src/schema/TableBuilder.ts +48 -11
- package/src/schema/catalog.ts +94 -24
- package/src/schema/introspect.ts +26 -9
- package/src/seeders.ts +16 -0
- package/src/services/db.ts +343 -31
- package/src/testing/DatabaseCleanup.ts +73 -21
- package/src/testing/DbAssertions.ts +127 -0
- package/src/testing/Factory.ts +559 -79
- package/src/testing/TestUtils.ts +123 -0
- package/src/testing/index.ts +12 -1
- package/src/types/relations.ts +43 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `testUtils.db()` — the AdonisJS `testUtils.db()` facade for test state
|
|
3
|
+
* management, wired to atlas's migration/seed/truncate/transaction bricks:
|
|
4
|
+
*
|
|
5
|
+
* const t = testUtils(connection, { migrationsDir, seedersDir });
|
|
6
|
+
* test.group("users", (group) => {
|
|
7
|
+
* group.each.setup(() => t.db().truncate()); // teardown truncates
|
|
8
|
+
* });
|
|
9
|
+
*
|
|
10
|
+
* Each method returns the TEARDOWN function a Japa/helix hook uses (Adonis
|
|
11
|
+
* parity): `group.each.setup(() => testUtils.db().truncate())`.
|
|
12
|
+
*
|
|
13
|
+
* NOTE (named atlas deviation): Lucid's `withGlobalTransaction()` transparently
|
|
14
|
+
* routes EVERY query through a hidden connection-global transaction. atlas
|
|
15
|
+
* deliberately does NOT bind a global transaction (it caused silent pool-scatter
|
|
16
|
+
* bugs — see `useTransaction`); instead it hands back a PINNED trx the test
|
|
17
|
+
* drives explicitly. So `withGlobalTransaction()` returns `{ trx, rollback }` —
|
|
18
|
+
* run the test's queries through `trx`, not the pooled connection.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import type { AsyncDatabaseConnection } from "../adapters/NapiDbAdapter.js";
|
|
22
|
+
import {
|
|
23
|
+
type DatabaseAdapter,
|
|
24
|
+
MigrationRunner,
|
|
25
|
+
} from "../schema/MigrationRunner.js";
|
|
26
|
+
import { runSeederDirectory } from "../schema/Seeder.js";
|
|
27
|
+
import {
|
|
28
|
+
type TestTransaction,
|
|
29
|
+
truncateAll,
|
|
30
|
+
useTransaction,
|
|
31
|
+
} from "./DatabaseCleanup.js";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Adapt a real connection to the {@link DatabaseAdapter} shape MigrationRunner
|
|
35
|
+
* needs — the only mismatch is `execute` (the connection returns row counts,
|
|
36
|
+
* the adapter wants `void`). Forwards the interactive-transaction hooks so
|
|
37
|
+
* transactional migrations + FK suspension keep working.
|
|
38
|
+
*/
|
|
39
|
+
function connToAdapter(conn: AsyncDatabaseConnection): DatabaseAdapter {
|
|
40
|
+
const adapter: DatabaseAdapter = {
|
|
41
|
+
execute: async (sql, params) => {
|
|
42
|
+
await conn.execute(sql, params);
|
|
43
|
+
},
|
|
44
|
+
query: (sql, params) => conn.query(sql, params),
|
|
45
|
+
close: () => conn.close(),
|
|
46
|
+
};
|
|
47
|
+
if (conn.runInTransaction) {
|
|
48
|
+
adapter.runInTransaction = (batch) => conn.runInTransaction(batch);
|
|
49
|
+
}
|
|
50
|
+
// `conn.transaction()` is the MANUAL (pinned) form; DatabaseAdapter.transaction
|
|
51
|
+
// is the MANAGED (callback) form — different shapes, so it isn't forwarded.
|
|
52
|
+
// runInTransaction covers atomic migration batches; the managed hook is only
|
|
53
|
+
// needed for `defer()`/MySQL-FK migrations, which a bare test connection skips.
|
|
54
|
+
return adapter;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Where migrations/seeders live, for `migrate()`/`seed()`. */
|
|
58
|
+
export interface DbTestUtilsOptions {
|
|
59
|
+
/** Directory of migration files (Adonis `database/migrations`). */
|
|
60
|
+
migrationsDir?: string;
|
|
61
|
+
/** Directory of seeder files (Adonis `database/seeders`). */
|
|
62
|
+
seedersDir?: string;
|
|
63
|
+
/** Tables `truncate()` leaves alone (in addition to `ream_*` internals). */
|
|
64
|
+
ignoreTables?: readonly string[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The `testUtils.db()` surface. */
|
|
68
|
+
export interface DbTestUtils {
|
|
69
|
+
/** Run migrations now; returns a teardown that rolls them back. */
|
|
70
|
+
migrate(): Promise<() => Promise<void>>;
|
|
71
|
+
/** Returns a teardown that empties every user table (test isolation). */
|
|
72
|
+
truncate(): () => Promise<void>;
|
|
73
|
+
/** Run seeders now (optionally a `files` subset). */
|
|
74
|
+
seed(files?: readonly string[]): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Begin a pinned transaction for the test; returns `{ trx, rollback }`. Run
|
|
77
|
+
* the test's queries through `trx` (atlas has no transparent global trx).
|
|
78
|
+
*/
|
|
79
|
+
withGlobalTransaction(): Promise<TestTransaction>;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** AdonisJS `testUtils` facade (db slice) bound to a connection. */
|
|
83
|
+
export function testUtils(
|
|
84
|
+
conn: AsyncDatabaseConnection,
|
|
85
|
+
options: DbTestUtilsOptions = {},
|
|
86
|
+
): { db(): DbTestUtils } {
|
|
87
|
+
return {
|
|
88
|
+
db(): DbTestUtils {
|
|
89
|
+
return {
|
|
90
|
+
async migrate() {
|
|
91
|
+
const runner = new MigrationRunner(connToAdapter(conn), {
|
|
92
|
+
migrationsDir: options.migrationsDir,
|
|
93
|
+
dialect: conn.dialect,
|
|
94
|
+
});
|
|
95
|
+
await runner.migrate();
|
|
96
|
+
return async () => {
|
|
97
|
+
await runner.rollback();
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
truncate() {
|
|
101
|
+
return async () => {
|
|
102
|
+
await truncateAll(conn, options.ignoreTables);
|
|
103
|
+
};
|
|
104
|
+
},
|
|
105
|
+
async seed(files) {
|
|
106
|
+
if (!options.seedersDir) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
"testUtils.db().seed() needs a `seedersDir` (pass it to testUtils()).",
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
await runSeederDirectory(
|
|
112
|
+
options.seedersDir,
|
|
113
|
+
conn,
|
|
114
|
+
files ? { files } : undefined,
|
|
115
|
+
);
|
|
116
|
+
},
|
|
117
|
+
withGlobalTransaction() {
|
|
118
|
+
return useTransaction(conn);
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
package/src/testing/index.ts
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
1
|
export { truncateAll, useTransaction } from "./DatabaseCleanup.js";
|
|
2
|
-
export {
|
|
2
|
+
export {
|
|
3
|
+
createDbAssertions,
|
|
4
|
+
type DbAssertions,
|
|
5
|
+
type DbConnectionLike,
|
|
6
|
+
db,
|
|
7
|
+
} from "./DbAssertions.js";
|
|
8
|
+
export { Factory, type FactoryBuilder, factory } from "./Factory.js";
|
|
3
9
|
export { Database } from "./TestDatabase.js";
|
|
10
|
+
export {
|
|
11
|
+
type DbTestUtils,
|
|
12
|
+
type DbTestUtilsOptions,
|
|
13
|
+
testUtils,
|
|
14
|
+
} from "./TestUtils.js";
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@c9up/atlas/types/relations` — Adonis Lucid `@adonisjs/lucid/types/relations`
|
|
3
|
+
* parity subpath.
|
|
4
|
+
*
|
|
5
|
+
* Lucid declares relation properties as `declare posts: HasMany<typeof Post>`.
|
|
6
|
+
* Atlas declares them as the plain related shape (`declare posts: Post[]`), so
|
|
7
|
+
* these helpers simply RESOLVE to that shape — a Lucid-style declaration
|
|
8
|
+
* type-checks against atlas's decorators and hydration unchanged:
|
|
9
|
+
*
|
|
10
|
+
* import type { HasMany, BelongsTo } from '@c9up/atlas/types/relations'
|
|
11
|
+
*
|
|
12
|
+
* class Post extends BaseModel {
|
|
13
|
+
* @belongsTo(() => User) declare author: BelongsTo<typeof User>
|
|
14
|
+
* }
|
|
15
|
+
* class User extends BaseModel {
|
|
16
|
+
* @hasMany(() => Post) declare posts: HasMany<typeof Post>
|
|
17
|
+
* }
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** A `hasOne` related row, or `null` when absent (resolves to `InstanceType<Related>`). */
|
|
21
|
+
export type HasOne<Related extends new (...args: never[]) => unknown> =
|
|
22
|
+
InstanceType<Related> | null;
|
|
23
|
+
|
|
24
|
+
/** A `belongsTo` owner row, or `null` when absent. */
|
|
25
|
+
export type BelongsTo<Related extends new (...args: never[]) => unknown> =
|
|
26
|
+
InstanceType<Related> | null;
|
|
27
|
+
|
|
28
|
+
/** A `hasMany` collection. */
|
|
29
|
+
export type HasMany<Related extends new (...args: never[]) => unknown> = Array<
|
|
30
|
+
InstanceType<Related>
|
|
31
|
+
>;
|
|
32
|
+
|
|
33
|
+
/** A `manyToMany` collection. */
|
|
34
|
+
export type ManyToMany<Related extends new (...args: never[]) => unknown> =
|
|
35
|
+
Array<InstanceType<Related>>;
|
|
36
|
+
|
|
37
|
+
/** A `hasOneThrough` related row, or `null` when absent. */
|
|
38
|
+
export type HasOneThrough<Related extends new (...args: never[]) => unknown> =
|
|
39
|
+
InstanceType<Related> | null;
|
|
40
|
+
|
|
41
|
+
/** A `hasManyThrough` collection. */
|
|
42
|
+
export type HasManyThrough<Related extends new (...args: never[]) => unknown> =
|
|
43
|
+
Array<InstanceType<Related>>;
|