@c9up/atlas 0.1.19 → 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.
Files changed (121) hide show
  1. package/README.md +55 -14
  2. package/db.darwin-arm64.node +0 -0
  3. package/db.darwin-x64.node +0 -0
  4. package/db.linux-arm64-gnu.node +0 -0
  5. package/db.linux-x64-gnu.node +0 -0
  6. package/db.win32-x64-msvc.node +0 -0
  7. package/dist/AtlasProvider.d.ts +6 -0
  8. package/dist/AtlasProvider.d.ts.map +1 -1
  9. package/dist/AtlasProvider.js +2 -2
  10. package/dist/AtlasProvider.js.map +1 -1
  11. package/dist/BaseEntity.d.ts +164 -5
  12. package/dist/BaseEntity.d.ts.map +1 -1
  13. package/dist/BaseEntity.js +339 -33
  14. package/dist/BaseEntity.js.map +1 -1
  15. package/dist/BaseModel.d.ts +91 -0
  16. package/dist/BaseModel.d.ts.map +1 -0
  17. package/dist/BaseModel.js +193 -0
  18. package/dist/BaseModel.js.map +1 -0
  19. package/dist/BaseRepository.d.ts +71 -15
  20. package/dist/BaseRepository.d.ts.map +1 -1
  21. package/dist/BaseRepository.js +1355 -384
  22. package/dist/BaseRepository.js.map +1 -1
  23. package/dist/ModelQuery.d.ts +341 -18
  24. package/dist/ModelQuery.d.ts.map +1 -1
  25. package/dist/ModelQuery.js +1406 -177
  26. package/dist/ModelQuery.js.map +1 -1
  27. package/dist/Transaction.d.ts +17 -0
  28. package/dist/Transaction.d.ts.map +1 -1
  29. package/dist/Transaction.js +57 -5
  30. package/dist/Transaction.js.map +1 -1
  31. package/dist/adapters/NapiDbAdapter.d.ts +33 -4
  32. package/dist/adapters/NapiDbAdapter.d.ts.map +1 -1
  33. package/dist/adapters/NapiDbAdapter.js +101 -11
  34. package/dist/adapters/NapiDbAdapter.js.map +1 -1
  35. package/dist/console/migrationCommands.d.ts +48 -0
  36. package/dist/console/migrationCommands.d.ts.map +1 -0
  37. package/dist/console/migrationCommands.js +220 -0
  38. package/dist/console/migrationCommands.js.map +1 -0
  39. package/dist/decorators/entity.d.ts +37 -6
  40. package/dist/decorators/entity.d.ts.map +1 -1
  41. package/dist/decorators/entity.js +32 -2
  42. package/dist/decorators/entity.js.map +1 -1
  43. package/dist/events.d.ts +64 -0
  44. package/dist/events.d.ts.map +1 -0
  45. package/dist/events.js +82 -0
  46. package/dist/events.js.map +1 -0
  47. package/dist/index.d.ts +5 -1
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +3 -0
  50. package/dist/index.js.map +1 -1
  51. package/dist/metadata-keys.d.ts +3 -2
  52. package/dist/metadata-keys.d.ts.map +1 -1
  53. package/dist/naming/NamingStrategy.d.ts +7 -0
  54. package/dist/naming/NamingStrategy.d.ts.map +1 -1
  55. package/dist/naming/NamingStrategy.js +16 -0
  56. package/dist/naming/NamingStrategy.js.map +1 -1
  57. package/dist/schema/Migration.d.ts +26 -3
  58. package/dist/schema/Migration.d.ts.map +1 -1
  59. package/dist/schema/Migration.js +33 -24
  60. package/dist/schema/Migration.js.map +1 -1
  61. package/dist/schema/MigrationRunner.d.ts +43 -32
  62. package/dist/schema/MigrationRunner.d.ts.map +1 -1
  63. package/dist/schema/MigrationRunner.js +211 -26
  64. package/dist/schema/MigrationRunner.js.map +1 -1
  65. package/dist/schema/Schema.d.ts +57 -0
  66. package/dist/schema/Schema.d.ts.map +1 -1
  67. package/dist/schema/Schema.js +138 -3
  68. package/dist/schema/Schema.js.map +1 -1
  69. package/dist/schema/TableBuilder.d.ts +247 -8
  70. package/dist/schema/TableBuilder.d.ts.map +1 -1
  71. package/dist/schema/TableBuilder.js +607 -41
  72. package/dist/schema/TableBuilder.js.map +1 -1
  73. package/dist/schema/catalog.d.ts +47 -0
  74. package/dist/schema/catalog.d.ts.map +1 -0
  75. package/dist/schema/catalog.js +111 -0
  76. package/dist/schema/catalog.js.map +1 -0
  77. package/dist/schema/types.d.ts +150 -1
  78. package/dist/schema/types.d.ts.map +1 -1
  79. package/dist/schema/types.js +11 -0
  80. package/dist/schema/types.js.map +1 -1
  81. package/dist/services/db.d.ts +6 -0
  82. package/dist/services/db.d.ts.map +1 -1
  83. package/dist/services/db.js +17 -0
  84. package/dist/services/db.js.map +1 -1
  85. package/dist/testing/DatabaseCleanup.d.ts +7 -4
  86. package/dist/testing/DatabaseCleanup.d.ts.map +1 -1
  87. package/dist/testing/DatabaseCleanup.js +21 -18
  88. package/dist/testing/DatabaseCleanup.js.map +1 -1
  89. package/dist/testing/Factory.d.ts +70 -5
  90. package/dist/testing/Factory.d.ts.map +1 -1
  91. package/dist/testing/Factory.js +209 -10
  92. package/dist/testing/Factory.js.map +1 -1
  93. package/index.darwin-arm64.node +0 -0
  94. package/index.darwin-x64.node +0 -0
  95. package/index.linux-arm64-gnu.node +0 -0
  96. package/index.linux-x64-gnu.node +0 -0
  97. package/index.win32-x64-msvc.node +0 -0
  98. package/package.json +3 -1
  99. package/scripts/guard-publish.mjs +15 -0
  100. package/src/AtlasProvider.ts +8 -1
  101. package/src/BaseEntity.ts +431 -41
  102. package/src/BaseModel.ts +324 -0
  103. package/src/BaseRepository.ts +1589 -419
  104. package/src/ModelQuery.ts +1817 -203
  105. package/src/Transaction.ts +68 -5
  106. package/src/adapters/NapiDbAdapter.ts +159 -10
  107. package/src/console/migrationCommands.ts +258 -0
  108. package/src/decorators/entity.ts +53 -6
  109. package/src/events.ts +112 -0
  110. package/src/index.ts +19 -0
  111. package/src/metadata-keys.ts +3 -2
  112. package/src/naming/NamingStrategy.ts +23 -0
  113. package/src/schema/Migration.ts +42 -3
  114. package/src/schema/MigrationRunner.ts +270 -27
  115. package/src/schema/Schema.ts +210 -3
  116. package/src/schema/TableBuilder.ts +735 -41
  117. package/src/schema/catalog.ts +166 -0
  118. package/src/schema/types.ts +137 -2
  119. package/src/services/db.ts +28 -0
  120. package/src/testing/DatabaseCleanup.ts +23 -22
  121. package/src/testing/Factory.ts +332 -15
@@ -0,0 +1,324 @@
1
+ import type { AsyncDatabaseConnection } from "./adapters/NapiDbAdapter.js";
2
+ import { BaseEntity } from "./BaseEntity.js";
3
+ import { BaseRepository, type DatabaseConnection } from "./BaseRepository.js";
4
+ import { ensureEntityMetadata } from "./decorators/entity.js";
5
+ import { AtlasError } from "./errors.js";
6
+ import { getConnection, getDb } from "./services/db.js";
7
+ import { isTransactionClient } from "./utils/transactionBrand.js";
8
+
9
+ /** A concrete BaseModel subclass: a `new()` constructor plus the static façade. */
10
+ type ModelClass<T extends BaseModel> = (new () => T) & typeof BaseModel;
11
+
12
+ /**
13
+ * AdonisJS Lucid–style Active Record façade. Subclass this (instead of
14
+ * `BaseEntity`) to get static finders/creators + `instance.save()/delete()`
15
+ * that delegate to a per-connection {@link BaseRepository}, while still
16
+ * inheriting `fill`/`merge`/`refresh`, dirty-tracking and the model-state flags
17
+ * (`$isPersisted`, `$isNew`, …) from BaseEntity.
18
+ *
19
+ * class User extends BaseModel {
20
+ * @PrimaryKey() declare id: number
21
+ * @Column() declare email: string
22
+ * }
23
+ * const u = await User.find(1)
24
+ * if (u) { u.email = "x@y"; await u.save() }
25
+ *
26
+ * `@Entity('table')` is OPTIONAL on a BaseModel — the table name is inferred
27
+ * from the class name via the naming strategy (or `static table`). The
28
+ * Data-Mapper `BaseRepository` remains available for those who prefer it.
29
+ */
30
+ export abstract class BaseModel extends BaseEntity {
31
+ /** Override the inferred table name (AdonisJS `static table`). */
32
+ static table?: string;
33
+ /** Bind to a named connection (AdonisJS `static connection`); default otherwise. */
34
+ static connection?: string;
35
+ /** Override the primary-key column name (AdonisJS `static primaryKey`); `@PrimaryKey()` otherwise. */
36
+ static primaryKey?: string;
37
+
38
+ /**
39
+ * Ensure `@Entity` metadata exists — infer the table name from the class name
40
+ * (naming strategy) or `static table` when the decorator is absent. Idempotent;
41
+ * runs on first repository access.
42
+ */
43
+ static $boot<T extends BaseModel>(this: ModelClass<T>): void {
44
+ ensureEntityMetadata(this);
45
+ }
46
+
47
+ /** Resolve this model's connection (named via `static connection`, else default). */
48
+ static $connection(): AsyncDatabaseConnection {
49
+ const conn = this.connection ? getConnection(this.connection) : getDb();
50
+ if (conn === undefined) {
51
+ throw new AtlasError(
52
+ "MISSING_CONNECTION",
53
+ this.connection
54
+ ? `No connection named '${this.connection}' is registered for model '${this.name}'.`
55
+ : `No default database connection for model '${this.name}' — is AtlasProvider booted?`,
56
+ {
57
+ hint: "Boot AtlasProvider (config/database.ts) before using models, or register the named connection.",
58
+ },
59
+ );
60
+ }
61
+ return conn;
62
+ }
63
+
64
+ /** The {@link BaseRepository} backing this model on its resolved connection. */
65
+ static $repo<T extends BaseModel>(this: ModelClass<T>): BaseRepository<T> {
66
+ this.$boot();
67
+ return new BaseRepository<T>(this, this.$connection());
68
+ }
69
+
70
+ // — Static finders (AdonisJS Lucid) —
71
+
72
+ static find<T extends BaseModel>(
73
+ this: ModelClass<T>,
74
+ id: string | number,
75
+ ): Promise<T | null> {
76
+ return this.$repo().find(id);
77
+ }
78
+
79
+ static findOrFail<T extends BaseModel>(
80
+ this: ModelClass<T>,
81
+ id: string | number,
82
+ ): Promise<T> {
83
+ return this.$repo().findOrFail(id);
84
+ }
85
+
86
+ static findBy<T extends BaseModel>(
87
+ this: ModelClass<T>,
88
+ column: string,
89
+ value: unknown,
90
+ ): Promise<T | null>;
91
+ static findBy<T extends BaseModel>(
92
+ this: ModelClass<T>,
93
+ clause: Record<string, unknown>,
94
+ ): Promise<T | null>;
95
+ static findBy<T extends BaseModel>(
96
+ this: ModelClass<T>,
97
+ columnOrClause: string | Record<string, unknown>,
98
+ value?: unknown,
99
+ ): Promise<T | null> {
100
+ return typeof columnOrClause === "string"
101
+ ? this.$repo().findBy(columnOrClause, value)
102
+ : this.$repo().findBy(columnOrClause);
103
+ }
104
+
105
+ static findByOrFail<T extends BaseModel>(
106
+ this: ModelClass<T>,
107
+ column: string,
108
+ value: unknown,
109
+ ): Promise<T>;
110
+ static findByOrFail<T extends BaseModel>(
111
+ this: ModelClass<T>,
112
+ clause: Record<string, unknown>,
113
+ ): Promise<T>;
114
+ static findByOrFail<T extends BaseModel>(
115
+ this: ModelClass<T>,
116
+ columnOrClause: string | Record<string, unknown>,
117
+ value?: unknown,
118
+ ): Promise<T> {
119
+ return typeof columnOrClause === "string"
120
+ ? this.$repo().findByOrFail(columnOrClause, value)
121
+ : this.$repo().findByOrFail(columnOrClause);
122
+ }
123
+
124
+ static findMany<T extends BaseModel>(
125
+ this: ModelClass<T>,
126
+ ids: Array<string | number>,
127
+ ): Promise<T[]> {
128
+ return this.$repo().findMany(ids);
129
+ }
130
+
131
+ static findManyBy<T extends BaseModel>(
132
+ this: ModelClass<T>,
133
+ column: string,
134
+ values: Array<string | number>,
135
+ ): Promise<T[]>;
136
+ static findManyBy<T extends BaseModel>(
137
+ this: ModelClass<T>,
138
+ clause: Record<string, unknown>,
139
+ ): Promise<T[]>;
140
+ static findManyBy<T extends BaseModel>(
141
+ this: ModelClass<T>,
142
+ columnOrClause: string | Record<string, unknown>,
143
+ values?: Array<string | number>,
144
+ ): Promise<T[]> {
145
+ return typeof columnOrClause === "string"
146
+ ? this.$repo().findManyBy(columnOrClause, values ?? [])
147
+ : this.$repo().findManyBy(columnOrClause);
148
+ }
149
+
150
+ static all<T extends BaseModel>(this: ModelClass<T>): Promise<T[]> {
151
+ return this.$repo().all();
152
+ }
153
+
154
+ static query<T extends BaseModel>(
155
+ this: ModelClass<T>,
156
+ ): ReturnType<BaseRepository<T>["query"]> {
157
+ return this.$repo().query();
158
+ }
159
+
160
+ static first<T extends BaseModel>(this: ModelClass<T>): Promise<T | null> {
161
+ return this.$repo().query().first();
162
+ }
163
+
164
+ static firstOrFail<T extends BaseModel>(this: ModelClass<T>): Promise<T> {
165
+ return this.$repo().query().firstOrFail();
166
+ }
167
+
168
+ // — Static creators (AdonisJS Lucid) —
169
+
170
+ static create<T extends BaseModel>(
171
+ this: ModelClass<T>,
172
+ data: Partial<Record<string, unknown>>,
173
+ ): Promise<T> {
174
+ return this.$repo().create(data);
175
+ }
176
+
177
+ static createMany<T extends BaseModel>(
178
+ this: ModelClass<T>,
179
+ rows: Array<Partial<Record<string, unknown>>>,
180
+ ): Promise<T[]> {
181
+ return this.$repo().createMany(rows);
182
+ }
183
+
184
+ /** {@link create} without firing lifecycle hooks (AdonisJS Lucid `createQuietly`). */
185
+ static createQuietly<T extends BaseModel>(
186
+ this: ModelClass<T>,
187
+ data: Partial<Record<string, unknown>>,
188
+ ): Promise<T> {
189
+ return this.$repo().createQuietly(data);
190
+ }
191
+
192
+ /** {@link createMany} without firing lifecycle hooks (AdonisJS Lucid `createManyQuietly`). */
193
+ static createManyQuietly<T extends BaseModel>(
194
+ this: ModelClass<T>,
195
+ rows: Array<Partial<Record<string, unknown>>>,
196
+ ): Promise<T[]> {
197
+ return this.$repo().createManyQuietly(rows);
198
+ }
199
+
200
+ static firstOrCreate<T extends BaseModel>(
201
+ this: ModelClass<T>,
202
+ search: Record<string, unknown>,
203
+ values?: Record<string, unknown>,
204
+ ): Promise<T> {
205
+ return this.$repo().firstOrCreate(search, values);
206
+ }
207
+
208
+ static firstOrNew<T extends BaseModel>(
209
+ this: ModelClass<T>,
210
+ search: Record<string, unknown>,
211
+ values?: Record<string, unknown>,
212
+ ): Promise<T> {
213
+ return this.$repo().firstOrNew(search, values);
214
+ }
215
+
216
+ static updateOrCreate<T extends BaseModel>(
217
+ this: ModelClass<T>,
218
+ search: Record<string, unknown>,
219
+ values: Record<string, unknown>,
220
+ ): Promise<T> {
221
+ return this.$repo().updateOrCreate(search, values);
222
+ }
223
+
224
+ static updateOrCreateMany<T extends BaseModel>(
225
+ this: ModelClass<T>,
226
+ key: string | string[],
227
+ rows: Array<Record<string, unknown>>,
228
+ ): Promise<T[]> {
229
+ return this.$repo().updateOrCreateMany(key, rows);
230
+ }
231
+
232
+ static fetchOrCreateMany<T extends BaseModel>(
233
+ this: ModelClass<T>,
234
+ key: string | string[],
235
+ rows: Array<Record<string, unknown>>,
236
+ ): Promise<T[]> {
237
+ return this.$repo().fetchOrCreateMany(key, rows);
238
+ }
239
+
240
+ static fetchOrNewUpMany<T extends BaseModel>(
241
+ this: ModelClass<T>,
242
+ key: string | string[],
243
+ rows: Array<Record<string, unknown>>,
244
+ ): Promise<T[]> {
245
+ return this.$repo().fetchOrNewUpMany(key, rows);
246
+ }
247
+
248
+ /** Empty this model's table (AdonisJS `Model.truncate`). `cascade` is Postgres-only. */
249
+ static truncate<T extends BaseModel>(
250
+ this: ModelClass<T>,
251
+ cascade = false,
252
+ ): Promise<void> {
253
+ return this.$repo().truncate(cascade);
254
+ }
255
+
256
+ // — Instance persistence (AdonisJS Lucid) —
257
+
258
+ /** Transaction bound to this instance via {@link useTransaction}, if any. */
259
+ #trx?: DatabaseConnection;
260
+
261
+ /** The transaction bound to this instance, if any (AdonisJS Lucid `$trx`). */
262
+ get $trx(): DatabaseConnection | undefined {
263
+ return this.#trx;
264
+ }
265
+
266
+ /**
267
+ * Bind this instance to a transaction so subsequent `save()` / `delete()` run
268
+ * inside it (AdonisJS Lucid `model.useTransaction`). Chainable.
269
+ *
270
+ * The binding is released when the transaction settles: Lucid clears `$trx`
271
+ * on commit/rollback so a reused instance falls back to the connection pool
272
+ * instead of a finished client. We mirror that by unbinding iff we are still
273
+ * bound to the same trx (a later `useTransaction` to another trx wins).
274
+ */
275
+ useTransaction(trx: DatabaseConnection): this {
276
+ this.#trx = trx;
277
+ // `.after` lives on the transaction client (a plain pool connection has no
278
+ // settle event). In practice useTransaction always receives a trx; guard so
279
+ // the type narrows and a non-trx connection is simply left bound (Lucid too
280
+ // only resets $trx for real transactions).
281
+ if (isTransactionClient(trx)) {
282
+ const release = () => {
283
+ if (this.#trx === trx) this.#trx = undefined;
284
+ };
285
+ trx.after("commit", release);
286
+ trx.after("rollback", release);
287
+ }
288
+ return this;
289
+ }
290
+
291
+ /**
292
+ * The repository backing this instance — transaction-bound when
293
+ * {@link useTransaction} was called. `this.constructor` is the concrete
294
+ * BaseModel subclass at runtime; TS types it only as `Function`, hence the
295
+ * single unavoidable narrowing (the pattern Lucid's own BaseModel uses).
296
+ */
297
+ #repo(): BaseRepository<this> {
298
+ const model = this.constructor as ModelClass<this>;
299
+ const repo = model.$repo();
300
+ return this.#trx ? repo.useTransaction(this.#trx) : repo;
301
+ }
302
+
303
+ /** INSERT this instance if new, else UPDATE its dirty columns. Returns `this`. */
304
+ async save(): Promise<this> {
305
+ await this.#repo().save(this);
306
+ return this;
307
+ }
308
+
309
+ /** {@link save} without firing lifecycle hooks (AdonisJS Lucid `saveQuietly`). Returns `this`. */
310
+ async saveQuietly(): Promise<this> {
311
+ await this.#repo().saveQuietly(this);
312
+ return this;
313
+ }
314
+
315
+ /** DELETE this instance's row (soft-delete aware). Sets `$isDeleted`. */
316
+ async delete(): Promise<void> {
317
+ await this.#repo().delete(this);
318
+ }
319
+
320
+ /** {@link delete} without firing lifecycle hooks (AdonisJS Lucid `deleteQuietly`). */
321
+ async deleteQuietly(): Promise<void> {
322
+ await this.#repo().deleteQuietly(this);
323
+ }
324
+ }