@forestfuture/active-record 0.6.0 → 0.8.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 +5 -4
- package/config-schema.json +60 -0
- package/dist/adapter.d.mts +3 -2
- package/dist/adapter.mjs +5 -3
- package/dist/batch-ReqVoCfa.mjs +9559 -0
- package/dist/cli.d.mts +216 -46
- package/dist/cli.mjs +4 -3
- package/dist/command.mjs +1102 -529
- package/dist/d1.d.mts +60 -0
- package/dist/d1.mjs +334 -0
- package/dist/epoch-storage-CDlQZcBB.mjs +988 -0
- package/dist/for-adapters-B0xMP6Dq.d.mts +23 -0
- package/dist/for-adapters-CfXMGeOu.mjs +31 -0
- package/dist/for-adapters-DhNV1mWf.mjs +276 -0
- package/dist/{generated-model-CfDggoVN.mjs → generated-model-g5oWsDKE.mjs} +732 -540
- package/dist/index-BATasvJa.d.mts +241 -0
- package/dist/index.d.mts +4 -143
- package/dist/index.mjs +5 -3
- package/dist/metadata-CzntpYzC.d.mts +182 -0
- package/dist/plugin-wOSILvSB.mjs +699 -0
- package/dist/plugin-x5-d3TSN.mjs +1035 -0
- package/dist/postgresql.d.mts +64 -0
- package/dist/postgresql.mjs +604 -0
- package/dist/src-D56_3odT.mjs +46 -0
- package/dist/storage-override-n4w1cS_M.mjs +220 -0
- package/dist/validations-DHDVV8hB.d.mts +1996 -0
- package/llms-full.txt +3283 -656
- package/package.json +25 -2
- package/skills/active-record/SKILL.md +24 -16
- package/dist/adapter-BSvFhTDf.d.mts +0 -1293
- package/dist/batch-DBeYk5wx.mjs +0 -4171
- package/dist/epoch-storage-o2aoOH7H.mjs +0 -112
- package/dist/storage-override-BYBYP8Mj.mjs +0 -91
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { $ as PolymorphicBelongsTo, G as BelongsTo, I as NumberAttributeKey, J as HasManyDependent, K as HasMany, O as AnyDatabase, Q as HasOneThrough, W as AssociationOptions, X as HasOne, Y as HasManyThrough, Z as HasOneDependent, et as ThroughDependent, f as Model, g as DatabaseSchema, p as ModelClass, tt as ThroughOptions } from "./validations-DHDVV8hB.mjs";
|
|
2
|
+
/** One of the attributes of `C`'s records, which a counter cache names. */
|
|
3
|
+
type CounterName<C extends ModelClass> = NumberAttributeKey<InstanceType<C>>;
|
|
4
|
+
/** What only a belongsTo takes; `C` is its target, whose attribute a counter cache names. */
|
|
5
|
+
interface BelongsToOptions<C extends ModelClass = ModelClass> {
|
|
6
|
+
/** The record may point at nothing: saving doesn't require its record (#339). */
|
|
7
|
+
optional?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Keeps a count of these records on the target (#61), updated in the same batch as each
|
|
10
|
+
* write: `true` counts them in the attribute named for them (`Reply` → `repliesCount`), or name
|
|
11
|
+
* another of the target's attributes (one every Model a polymorphic belongsTo lists has).
|
|
12
|
+
* `{ attribute, active: false }` keeps it on writes, but `size` doesn't read it yet, so it
|
|
13
|
+
* can be filled in for the rows already there (Rails 7.2's `active: false`).
|
|
14
|
+
*/
|
|
15
|
+
counterCache?: true | CounterName<C> | {
|
|
16
|
+
attribute?: CounterName<C>;
|
|
17
|
+
active?: false;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* What a polymorphic belongsTo takes (#60): no `scope` (#436), and the attribute holding the
|
|
22
|
+
* type, inferred as the association's name + `Type` (`commentable` → `commentableType`).
|
|
23
|
+
*/
|
|
24
|
+
type PolymorphicBelongsToOptions<C extends ModelClass = ModelClass> = Omit<AssociationOptions, "dependent" | "scope"> & BelongsToOptions<C> & {
|
|
25
|
+
foreignType?: string;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* This record points at one record of one of `targets` (#60), by the type and key it holds:
|
|
29
|
+
* `commentable = belongsTo([Post, Photo])`.
|
|
30
|
+
*/
|
|
31
|
+
declare function belongsTo<L extends readonly [ModelClass, ...ModelClass[]]>(targets: L, options?: PolymorphicBelongsToOptions<L[number]>): PolymorphicBelongsTo<L[number]>;
|
|
32
|
+
/** This record points at one `target` record through `foreignKey` (its own attribute). */
|
|
33
|
+
declare function belongsTo<C extends ModelClass>(target: C, options?: Omit<AssociationOptions<C>, "dependent"> & BelongsToOptions<C>): BelongsTo<C>;
|
|
34
|
+
/**
|
|
35
|
+
* What only a hasMany or hasOne takes: `as`, the name of the target's polymorphic belongsTo
|
|
36
|
+
* (#60), so its records point at this one by its type and key.
|
|
37
|
+
*/
|
|
38
|
+
interface ToPolymorphicOptions {
|
|
39
|
+
/**
|
|
40
|
+
* The target's polymorphic belongsTo this one is the other side of (#60): its records'
|
|
41
|
+
* `${as}Type` holds this Model's name and `${as}Id` its key.
|
|
42
|
+
*/
|
|
43
|
+
as?: string;
|
|
44
|
+
/** The target's attribute holding the type, inferred from `as` (`commentableType`). */
|
|
45
|
+
foreignType?: string;
|
|
46
|
+
}
|
|
47
|
+
/** What only a hasMany takes. */
|
|
48
|
+
interface HasManyOptions {
|
|
49
|
+
dependent?: HasManyDependent;
|
|
50
|
+
/**
|
|
51
|
+
* The owner's attribute holding a counter cache of these records (#61), which `size` reads:
|
|
52
|
+
* by default the one named for this association (`replies` → `repliesCount`), as Rails'
|
|
53
|
+
* has_many `counter_cache:`. The records' belongsTo keeps it.
|
|
54
|
+
*/
|
|
55
|
+
counterCache?: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Many `target` records point at this record through `foreignKey` (theirs); or, with `through`,
|
|
59
|
+
* this record reaches them through another of its associations (#59).
|
|
60
|
+
*/
|
|
61
|
+
declare function hasMany<C extends ModelClass>(target: C, options: ThroughOptions<C> & {
|
|
62
|
+
dependent?: ThroughDependent;
|
|
63
|
+
}): HasManyThrough<C>;
|
|
64
|
+
declare function hasMany<C extends ModelClass>(target: C, options?: AssociationOptions<C> & ToPolymorphicOptions & HasManyOptions): HasMany<C>;
|
|
65
|
+
/**
|
|
66
|
+
* One `target` record points at this record through `foreignKey` (its own); or, with
|
|
67
|
+
* `through`, this record reaches it through another of its associations (#59).
|
|
68
|
+
*/
|
|
69
|
+
declare function hasOne<C extends ModelClass>(target: C, options: ThroughOptions<C>): HasOneThrough<C>;
|
|
70
|
+
declare function hasOne<C extends ModelClass>(target: C, options?: AssociationOptions<C> & ToPolymorphicOptions & {
|
|
71
|
+
dependent?: HasOneDependent;
|
|
72
|
+
}): HasOne<C>;
|
|
73
|
+
/** Base class for every error the library throws on purpose. */
|
|
74
|
+
declare class ActiveRecordError extends Error {
|
|
75
|
+
name: string;
|
|
76
|
+
}
|
|
77
|
+
/** A Model's declaration is invalid, e.g. an association's foreign key isn't an attribute. */
|
|
78
|
+
declare class InvalidModel extends ActiveRecordError {
|
|
79
|
+
name: string;
|
|
80
|
+
}
|
|
81
|
+
/** A key that isn't a declared attribute of the Model. */
|
|
82
|
+
declare class UnknownAttribute extends ActiveRecordError {
|
|
83
|
+
name: string;
|
|
84
|
+
}
|
|
85
|
+
/** A record constructed directly (`new User()`) was used for something that needs D1. */
|
|
86
|
+
declare class NoDatabase extends ActiveRecordError {
|
|
87
|
+
name: string;
|
|
88
|
+
}
|
|
89
|
+
/** No record matched a lookup that requires one (`find`, `findByOrThrow`, `sole`). */
|
|
90
|
+
declare class RecordNotFound extends ActiveRecordError {
|
|
91
|
+
name: string;
|
|
92
|
+
}
|
|
93
|
+
/** `sole()` or `findSoleBy()` matched more than one record. */
|
|
94
|
+
declare class SoleRecordExceeded extends ActiveRecordError {
|
|
95
|
+
name: string;
|
|
96
|
+
}
|
|
97
|
+
/** Reading an attribute that wasn't loaded, e.g. one left out of `select(...)`. */
|
|
98
|
+
declare class MissingAttribute extends ActiveRecordError {
|
|
99
|
+
name: string;
|
|
100
|
+
}
|
|
101
|
+
/** Destroying a record whose `dependent: "restrictWithException"` association still has records. */
|
|
102
|
+
declare class DeleteRestriction extends ActiveRecordError {
|
|
103
|
+
name: string;
|
|
104
|
+
}
|
|
105
|
+
/** `last()` can't reverse an order given as an `sql` fragment (Rails' IrreversibleOrderError). */
|
|
106
|
+
declare class IrreversibleOrder extends ActiveRecordError {
|
|
107
|
+
name: string;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Things that can't be combined: relations in `or`/`merge` (e.g. of different Models),
|
|
111
|
+
* `includes` or a write statement with `distinct()`, or statements of two Databases in a batch.
|
|
112
|
+
*/
|
|
113
|
+
declare class IncompatibleRelation extends ActiveRecordError {
|
|
114
|
+
name: string;
|
|
115
|
+
}
|
|
116
|
+
/** Using a destroyed record as a live one: `save`, `update`, `destroy` or `reload` after `destroy()`. */
|
|
117
|
+
declare class RecordDestroyed extends ActiveRecordError {
|
|
118
|
+
name: string;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* D1 rejected a write because of a database constraint. `table`, `columns` and `attributes`
|
|
122
|
+
* are filled in when D1's message names them. `table` is null only for a batch writing to
|
|
123
|
+
* several tables, when D1 doesn't say which one failed. The original D1 error is the `cause`.
|
|
124
|
+
*/
|
|
125
|
+
declare class ConstraintViolation extends ActiveRecordError {
|
|
126
|
+
name: string;
|
|
127
|
+
readonly table: string | null;
|
|
128
|
+
readonly columns: readonly string[];
|
|
129
|
+
readonly attributes: readonly string[];
|
|
130
|
+
constructor(message: string, details: {
|
|
131
|
+
table: string | null;
|
|
132
|
+
columns: readonly string[];
|
|
133
|
+
attributes: readonly string[];
|
|
134
|
+
cause: unknown;
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
/** A UNIQUE constraint failed, e.g. another request won the race after validation passed. */
|
|
138
|
+
declare class RecordNotUnique extends ConstraintViolation {
|
|
139
|
+
name: string;
|
|
140
|
+
}
|
|
141
|
+
/** A FOREIGN KEY constraint failed. SQLite's message doesn't name the column. */
|
|
142
|
+
declare class InvalidForeignKey extends ConstraintViolation {
|
|
143
|
+
name: string;
|
|
144
|
+
}
|
|
145
|
+
/** A NOT NULL constraint failed. */
|
|
146
|
+
declare class NotNullViolation extends ConstraintViolation {
|
|
147
|
+
name: string;
|
|
148
|
+
}
|
|
149
|
+
/** A CHECK constraint failed. */
|
|
150
|
+
declare class CheckViolation extends ConstraintViolation {
|
|
151
|
+
name: string;
|
|
152
|
+
}
|
|
153
|
+
/** A `…OrThrow` save was refused because the record is invalid; `record.errors` says why. */
|
|
154
|
+
declare class RecordInvalid extends ActiveRecordError {
|
|
155
|
+
name: string;
|
|
156
|
+
readonly record: Model;
|
|
157
|
+
constructor(record: Model);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* A rule declared with `strict: true` failed: thrown at once, with the failure's full message
|
|
161
|
+
* ("Email can't be blank"), instead of adding it to `record.errors`.
|
|
162
|
+
*/
|
|
163
|
+
declare class StrictValidationFailed extends ActiveRecordError {
|
|
164
|
+
name: string;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Thrown by a callback to refuse a save or destroy: `throw abort("Account is suspended")`.
|
|
168
|
+
* Only this exact class halts a lifecycle; returning `false` never does.
|
|
169
|
+
*/
|
|
170
|
+
declare class CallbackAbort extends ActiveRecordError {
|
|
171
|
+
name: string;
|
|
172
|
+
readonly reason: string;
|
|
173
|
+
constructor(reason: string);
|
|
174
|
+
}
|
|
175
|
+
/** Makes the sentinel a pre-write callback throws to refuse: `throw abort("reason")`. */
|
|
176
|
+
declare function abort(reason?: string): CallbackAbort;
|
|
177
|
+
/** A `…OrThrow` save was refused by a callback's `abort(reason)`. */
|
|
178
|
+
declare class RecordNotSaved extends ActiveRecordError {
|
|
179
|
+
name: string;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* `abort()` was thrown from an after-write callback (`afterSave`, `afterCreate`, `afterUpdate`,
|
|
183
|
+
* `afterDestroy`). That's a mistake: the save or destroy already succeeded and nothing rolls back.
|
|
184
|
+
*/
|
|
185
|
+
declare class AbortAfterPersistence extends ActiveRecordError {
|
|
186
|
+
name: string;
|
|
187
|
+
readonly callback: string;
|
|
188
|
+
readonly reason: string;
|
|
189
|
+
constructor(callback: string, lifecycle: "save" | "destroy", abortion: CallbackAbort);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* A `destroyOrThrow()` was refused by a `beforeDestroy` callback's `abort(reason)` or a
|
|
193
|
+
* `restrictWithError` association; or a `saveOrThrow()`, by a record it destroys (#37).
|
|
194
|
+
*/
|
|
195
|
+
declare class RecordNotDestroyed extends ActiveRecordError {
|
|
196
|
+
name: string;
|
|
197
|
+
}
|
|
198
|
+
/** A name passed to `includes()` or `loaded()` that isn't one of the Model's associations. */
|
|
199
|
+
declare class UnknownAssociation extends ActiveRecordError {
|
|
200
|
+
name: string;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* A write through an association that goes through another that it can't make (#434):
|
|
204
|
+
* inserting or upserting through it, or any write through one that doesn't go through a join
|
|
205
|
+
* Model (a source that isn't a belongsTo, a hop through a belongsTo, or a nested hop).
|
|
206
|
+
*/
|
|
207
|
+
declare class ReadonlyAssociation extends ActiveRecordError {
|
|
208
|
+
name: string;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* A polymorphic belongsTo's type that names none of the Models it lists (#60), read from the
|
|
212
|
+
* database, as Rails raises when it can't find the class a type names.
|
|
213
|
+
*/
|
|
214
|
+
declare class UnknownModelName extends ActiveRecordError {
|
|
215
|
+
name: string;
|
|
216
|
+
}
|
|
217
|
+
/** `record.loaded(name)` for an association that wasn't preloaded or read yet. */
|
|
218
|
+
declare class AssociationNotLoaded extends ActiveRecordError {
|
|
219
|
+
name: string;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Runs `fn` with `databases` as the request's own: queries started from a Model inside it (and
|
|
223
|
+
* everything it awaits) use the one on that Model's binding, with its Session and `onQuery`.
|
|
224
|
+
* Usually called once per request, in middleware. Nested calls add to the outer ones.
|
|
225
|
+
*
|
|
226
|
+
* Each call is a scope of its own, even with a Database another request shares. An adapter may
|
|
227
|
+
* hold a connection for the scope (the PostgreSQL adapter does, from a connection string). A nested call with
|
|
228
|
+
* the same Database shares the outer call's scope while that call is running. When `fn` settles,
|
|
229
|
+
* the scope ends, and the adapter lets its connection go once the queries still running on it
|
|
230
|
+
* finish. Work that outlives the scope, such as a `waitUntil` save, gets a connection of its own.
|
|
231
|
+
*/
|
|
232
|
+
declare function withDatabase<R>(databases: AnyDatabase | readonly AnyDatabase[], fn: () => R | Promise<R>): Promise<R>;
|
|
233
|
+
/**
|
|
234
|
+
* A generated schema, as `schema.ts` declares it: `export const schema = defineSchema({ … })`.
|
|
235
|
+
* A generated Model's attributes are settled when it's first used, so its enum scopes would
|
|
236
|
+
* only appear then; `Post.published()` is often that first use. So each scope the schema lists
|
|
237
|
+
* is a static of every Model now: on a Model it's listed for, reading it readies the Model,
|
|
238
|
+
* which defines the scope on it; on any other, it's `undefined`.
|
|
239
|
+
*/
|
|
240
|
+
declare function defineSchema<const S extends DatabaseSchema>(schema: S): S;
|
|
241
|
+
export { PolymorphicBelongsToOptions as A, RecordNotUnique as C, UnknownAttribute as D, UnknownAssociation as E, hasMany as M, hasOne as N, UnknownModelName as O, RecordNotSaved as S, StrictValidationFailed as T, ReadonlyAssociation as _, AssociationNotLoaded as a, RecordNotDestroyed as b, ConstraintViolation as c, InvalidForeignKey as d, InvalidModel as f, NotNullViolation as g, NoDatabase as h, ActiveRecordError as i, belongsTo as j, abort as k, DeleteRestriction as l, MissingAttribute as m, withDatabase as n, CallbackAbort as o, IrreversibleOrder as p, AbortAfterPersistence as r, CheckViolation as s, defineSchema as t, IncompatibleRelation as u, RecordDestroyed as v, SoleRecordExceeded as w, RecordNotFound as x, RecordInvalid as y };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,143 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
/** Many `target` records point at this record through `foreignKey` (theirs). */
|
|
6
|
-
export declare function hasMany<C extends ModelClass>(target: C, options?: AssociationOptions<C> & {
|
|
7
|
-
dependent?: HasManyDependent;
|
|
8
|
-
}): HasMany<C>;
|
|
9
|
-
/** One `target` record points at this record through `foreignKey` (its own). */
|
|
10
|
-
export declare function hasOne<C extends ModelClass>(target: C, options?: AssociationOptions<C> & {
|
|
11
|
-
dependent?: HasOneDependent;
|
|
12
|
-
}): HasOne<C>;
|
|
13
|
-
//#endregion
|
|
14
|
-
//#region src/errors.d.ts
|
|
15
|
-
/** Base class for every error the library throws on purpose. */
|
|
16
|
-
export declare class ActiveRecordError extends Error {
|
|
17
|
-
name: string;
|
|
18
|
-
}
|
|
19
|
-
/** A Model's declaration is invalid, e.g. an association's foreign key isn't an attribute. */
|
|
20
|
-
export declare class InvalidModel extends ActiveRecordError {
|
|
21
|
-
name: string;
|
|
22
|
-
}
|
|
23
|
-
/** A key that isn't a declared attribute of the Model. */
|
|
24
|
-
export declare class UnknownAttribute extends ActiveRecordError {
|
|
25
|
-
name: string;
|
|
26
|
-
}
|
|
27
|
-
/** A record constructed directly (`new User()`) was used for something that needs D1. */
|
|
28
|
-
export declare class NoDatabase extends ActiveRecordError {
|
|
29
|
-
name: string;
|
|
30
|
-
}
|
|
31
|
-
/** No record matched a lookup that requires one (`find`, `findByOrThrow`). */
|
|
32
|
-
export declare class RecordNotFound extends ActiveRecordError {
|
|
33
|
-
name: string;
|
|
34
|
-
}
|
|
35
|
-
/** Reading an attribute that wasn't loaded, e.g. one left out of `select(...)`. */
|
|
36
|
-
export declare class MissingAttribute extends ActiveRecordError {
|
|
37
|
-
name: string;
|
|
38
|
-
}
|
|
39
|
-
/** Destroying a record whose `dependent: "restrictWithException"` association still has records. */
|
|
40
|
-
export declare class DeleteRestriction extends ActiveRecordError {
|
|
41
|
-
name: string;
|
|
42
|
-
}
|
|
43
|
-
/** `last()` can't reverse an order given as an `sql` fragment (Rails' IrreversibleOrderError). */
|
|
44
|
-
export declare class IrreversibleOrder extends ActiveRecordError {
|
|
45
|
-
name: string;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Things that can't be combined: relations in `or`/`merge` (e.g. of different Models),
|
|
49
|
-
* `includes` or a write statement with `distinct()`, or statements of two Databases in a batch.
|
|
50
|
-
*/
|
|
51
|
-
export declare class IncompatibleRelation extends ActiveRecordError {
|
|
52
|
-
name: string;
|
|
53
|
-
}
|
|
54
|
-
/** Using a destroyed record as a live one: `save`, `update`, `destroy` or `reload` after `destroy()`. */
|
|
55
|
-
export declare class RecordDestroyed extends ActiveRecordError {
|
|
56
|
-
name: string;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* D1 rejected a write because of a database constraint. `table`, `columns` and `attributes`
|
|
60
|
-
* are filled in when D1's message names them. `table` is null only for a batch writing to
|
|
61
|
-
* several tables, when D1 doesn't say which one failed. The original D1 error is the `cause`.
|
|
62
|
-
*/
|
|
63
|
-
export declare class ConstraintViolation extends ActiveRecordError {
|
|
64
|
-
name: string;
|
|
65
|
-
readonly table: string | null;
|
|
66
|
-
readonly columns: readonly string[];
|
|
67
|
-
readonly attributes: readonly string[];
|
|
68
|
-
constructor(message: string, details: {
|
|
69
|
-
table: string | null;
|
|
70
|
-
columns: readonly string[];
|
|
71
|
-
attributes: readonly string[];
|
|
72
|
-
cause: unknown;
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
/** A UNIQUE constraint failed, e.g. another request won the race after validation passed. */
|
|
76
|
-
export declare class RecordNotUnique extends ConstraintViolation {
|
|
77
|
-
name: string;
|
|
78
|
-
}
|
|
79
|
-
/** A FOREIGN KEY constraint failed. SQLite's message doesn't name the column. */
|
|
80
|
-
export declare class InvalidForeignKey extends ConstraintViolation {
|
|
81
|
-
name: string;
|
|
82
|
-
}
|
|
83
|
-
/** A NOT NULL constraint failed. */
|
|
84
|
-
export declare class NotNullViolation extends ConstraintViolation {
|
|
85
|
-
name: string;
|
|
86
|
-
}
|
|
87
|
-
/** A CHECK constraint failed. */
|
|
88
|
-
export declare class CheckViolation extends ConstraintViolation {
|
|
89
|
-
name: string;
|
|
90
|
-
}
|
|
91
|
-
/** A `…OrThrow` save was refused because the record is invalid; `record.errors` says why. */
|
|
92
|
-
export declare class RecordInvalid extends ActiveRecordError {
|
|
93
|
-
name: string;
|
|
94
|
-
readonly record: Model;
|
|
95
|
-
constructor(record: Model);
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Thrown by a callback to refuse a save or destroy: `throw abort("Account is suspended")`.
|
|
99
|
-
* Only this exact class halts a lifecycle; returning `false` never does.
|
|
100
|
-
*/
|
|
101
|
-
export declare class CallbackAbort extends ActiveRecordError {
|
|
102
|
-
name: string;
|
|
103
|
-
readonly reason: string;
|
|
104
|
-
constructor(reason: string);
|
|
105
|
-
}
|
|
106
|
-
/** Makes the sentinel a pre-write callback throws to refuse: `throw abort("reason")`. */
|
|
107
|
-
export declare function abort(reason?: string): CallbackAbort;
|
|
108
|
-
/** A `…OrThrow` save was refused by a callback's `abort(reason)`. */
|
|
109
|
-
export declare class RecordNotSaved extends ActiveRecordError {
|
|
110
|
-
name: string;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* `abort()` was thrown from an after-write callback (`afterSave`, `afterCreate`, `afterUpdate`,
|
|
114
|
-
* `afterDestroy`). That's a mistake: the save or destroy already succeeded and nothing rolls back.
|
|
115
|
-
*/
|
|
116
|
-
export declare class AbortAfterPersistence extends ActiveRecordError {
|
|
117
|
-
name: string;
|
|
118
|
-
readonly callback: string;
|
|
119
|
-
readonly reason: string;
|
|
120
|
-
constructor(callback: string, lifecycle: "save" | "destroy", abortion: CallbackAbort);
|
|
121
|
-
}
|
|
122
|
-
/** A `destroyOrThrow()` was refused by a `beforeDestroy` callback's `abort(reason)`. */
|
|
123
|
-
export declare class RecordNotDestroyed extends ActiveRecordError {
|
|
124
|
-
name: string;
|
|
125
|
-
}
|
|
126
|
-
/** A name passed to `includes()` or `loaded()` that isn't one of the Model's associations. */
|
|
127
|
-
export declare class UnknownAssociation extends ActiveRecordError {
|
|
128
|
-
name: string;
|
|
129
|
-
}
|
|
130
|
-
/** `record.loaded(name)` for an association that wasn't preloaded or read yet. */
|
|
131
|
-
export declare class AssociationNotLoaded extends ActiveRecordError {
|
|
132
|
-
name: string;
|
|
133
|
-
}
|
|
134
|
-
//#endregion
|
|
135
|
-
//#region src/current-database.d.ts
|
|
136
|
-
/**
|
|
137
|
-
* Runs `fn` with `databases` as the request's own: queries started from a Model inside it (and
|
|
138
|
-
* everything it awaits) use the one on that Model's binding, with its Session and `onQuery`.
|
|
139
|
-
* Usually called once per request, in middleware. Nested calls add to the outer ones.
|
|
140
|
-
*/
|
|
141
|
-
export declare function withDatabase<R>(databases: AnyDatabase | readonly AnyDatabase[], fn: () => R | Promise<R>): Promise<R>;
|
|
142
|
-
//#endregion
|
|
143
|
-
export { type AssociationOptions, type AttributeConversions, type AttributeErrorMessages, type AttributeSpec, type AttributeSpecs, type AttributeType, type BatchEntry, type BatchResults, type BelongsTo, type ColumnReference, type ColumnValue, type Comparison, type Conditions, type CountlessPage, type DatabaseSchema, type EpochStorage, type ErrorMessage, type ErrorMessages, Errors, type FilterAttribute, type HasMany, type HasManyDependent, type HasOne, type HasOneDependent, type InsertOptions, type LengthOptions, Model, type NumericalityOptions, type OrderSpec, type Page, type PageOptions, type QueryEvent, type QueryListener, type QuerySql, type RecordStatement, Relation, type RelationsFor, type RestrictMessages, type ReturnedRow, type Returning, type SafeSql, type Scope, type ScopeMethods, type ScopeStatic, type Scoped, type Statement, type TableAttributes, type TableSchema, type TableSchemas, type UniquenessOptions, type UpsertOptions, type ValidationError, type ValidationOptions, type WriteResult, batch, between, column, gt, gte, lt, lte, sql };
|
|
1
|
+
import { _ as defineType, a as AttributeType, g as CustomType, i as AttributeSpecs, l as EpochStorage, r as AttributeSpec } from "./metadata-CzntpYzC.mjs";
|
|
2
|
+
import { $ as PolymorphicBelongsTo, B as ValidationContexts, C as QueryEvent, Ct as lt, Et as InOrderOfOptions, F as RecordStatement, Ft as column, G as BelongsTo, H as Page, It as sql, J as HasManyDependent, K as HasMany, L as SaveOptions, Lt as Comparable, M as BatchResults, Mt as ColumnReference, N as batch, Nt as ColumnValue, Ot as QuerySql, P as Batch, Pt as SafeSql, Q as HasOneThrough, R as ValidationCallbackOptions, Rt as ComparisonBound, St as gte, Tt as QueryPart, U as PageOptions, V as CountlessPage, W as AssociationOptions, X as HasOne, Y as HasManyThrough, Z as HasOneDependent, _ as TableSchema, _t as Returning, a as ValidationOptions, at as Scoped, bt as between, c as AttributeErrorMessages, ct as Init, d as RestrictMessages, dt as WriteResult, et as ThroughDependent, f as Model, g as DatabaseSchema, gt as OrderSpec, h as TableAttributes, ht as InsertOptions, i as UniquenessOptions, it as ScopeMethods, j as BatchEntry, k as RelationsFor, l as ErrorMessage, lt as ReturnedRow, m as ScopeStatic, mt as Conditions, n as LengthOptions, nt as Relation, o as Errors, ot as IterationOptions, q as HasManyCollection, r as NumericalityOptions, rt as Scope, s as ValidationError, st as GroupedRelation, t as AssociatedValidationOptions, tt as ThroughOptions, u as ErrorMessages, ut as Statement, v as TableSchemas, vt as UpsertOptions, w as QueryListener, wt as lte, x as AttributeConversions, xt as gt, y as FilterAttribute, yt as Comparison, z as ValidationContext, zt as ComparisonOptions } from "./validations-DHDVV8hB.mjs";
|
|
3
|
+
import { A as PolymorphicBelongsToOptions, C as RecordNotUnique, D as UnknownAttribute, E as UnknownAssociation, M as hasMany, N as hasOne, O as UnknownModelName, S as RecordNotSaved, T as StrictValidationFailed, _ as ReadonlyAssociation, a as AssociationNotLoaded, b as RecordNotDestroyed, c as ConstraintViolation, d as InvalidForeignKey, f as InvalidModel, g as NotNullViolation, h as NoDatabase, i as ActiveRecordError, j as belongsTo, k as abort, l as DeleteRestriction, m as MissingAttribute, n as withDatabase, o as CallbackAbort, p as IrreversibleOrder, r as AbortAfterPersistence, s as CheckViolation, t as defineSchema, u as IncompatibleRelation, v as RecordDestroyed, w as SoleRecordExceeded, x as RecordNotFound, y as RecordInvalid } from "./index-BATasvJa.mjs";
|
|
4
|
+
export { AbortAfterPersistence, ActiveRecordError, type AssociatedValidationOptions, AssociationNotLoaded, type AssociationOptions, type AttributeConversions, type AttributeErrorMessages, type AttributeSpec, type AttributeSpecs, type AttributeType, type Batch, type BatchEntry, type BatchResults, type BelongsTo, CallbackAbort, CheckViolation, type ColumnReference, type ColumnValue, type Comparable, type Comparison, type ComparisonBound, type ComparisonOptions, type Conditions, ConstraintViolation, type CountlessPage, type CustomType, type DatabaseSchema, DeleteRestriction, type EpochStorage, type ErrorMessage, type ErrorMessages, Errors, type FilterAttribute, type GroupedRelation, type HasMany, type HasManyCollection, type HasManyDependent, type HasManyThrough, type HasOne, type HasOneDependent, type HasOneThrough, type InOrderOfOptions, IncompatibleRelation, type Init, type InsertOptions, InvalidForeignKey, InvalidModel, IrreversibleOrder, type IterationOptions, type LengthOptions, MissingAttribute, Model, NoDatabase, NotNullViolation, type NumericalityOptions, type OrderSpec, type Page, type PageOptions, type PolymorphicBelongsTo, type PolymorphicBelongsToOptions, type QueryEvent, type QueryListener, type QueryPart, type QuerySql, ReadonlyAssociation, RecordDestroyed, RecordInvalid, RecordNotDestroyed, RecordNotFound, RecordNotSaved, RecordNotUnique, type RecordStatement, Relation, type RelationsFor, type RestrictMessages, type ReturnedRow, type Returning, type SafeSql, type SaveOptions, type Scope, type ScopeMethods, type ScopeStatic, type Scoped, SoleRecordExceeded, type Statement, StrictValidationFailed, type TableAttributes, type TableSchema, type TableSchemas, type ThroughDependent, type ThroughOptions, type UniquenessOptions, UnknownAssociation, UnknownAttribute, UnknownModelName, type UpsertOptions, type ValidationCallbackOptions, type ValidationContext, type ValidationContexts, type ValidationError, type ValidationOptions, type WriteResult, abort, batch, belongsTo, between, column, defineSchema, defineType, gt, gte, hasMany, hasOne, lt, lte, sql, withDatabase };
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
import { $ as CallbackAbort,
|
|
2
|
-
import { _ as
|
|
3
|
-
|
|
1
|
+
import { $ as abort, A as CallbackAbort, B as NotNullViolation, D as AbortAfterPersistence, F as InvalidForeignKey, G as RecordNotFound, H as RecordDestroyed, I as InvalidModel, J as SoleRecordExceeded, K as RecordNotSaved, L as IrreversibleOrder, M as ConstraintViolation, N as DeleteRestriction, O as ActiveRecordError, P as IncompatibleRelation, Q as UnknownModelName, R as MissingAttribute, U as RecordInvalid, V as ReadonlyAssociation, W as RecordNotDestroyed, X as UnknownAssociation, Y as StrictValidationFailed, Z as UnknownAttribute, j as CheckViolation, k as AssociationNotLoaded, q as RecordNotUnique, z as NoDatabase } from "./epoch-storage-CDlQZcBB.mjs";
|
|
2
|
+
import { _ as lte, b as hasOne, c as Relation, d as sql, g as lt, h as gte, m as gt, n as Model, p as between, r as withDatabase, t as batch, u as column, v as belongsTo, y as hasMany } from "./batch-ReqVoCfa.mjs";
|
|
3
|
+
import { U as defineType, f as Errors } from "./generated-model-g5oWsDKE.mjs";
|
|
4
|
+
import { t as defineSchema } from "./src-D56_3odT.mjs";
|
|
5
|
+
export { AbortAfterPersistence, ActiveRecordError, AssociationNotLoaded, CallbackAbort, CheckViolation, ConstraintViolation, DeleteRestriction, Errors, IncompatibleRelation, InvalidForeignKey, InvalidModel, IrreversibleOrder, MissingAttribute, Model, NoDatabase, NotNullViolation, ReadonlyAssociation, RecordDestroyed, RecordInvalid, RecordNotDestroyed, RecordNotFound, RecordNotSaved, RecordNotUnique, Relation, SoleRecordExceeded, StrictValidationFailed, UnknownAssociation, UnknownAttribute, UnknownModelName, abort, batch, belongsTo, between, column, defineSchema, defineType, gt, gte, hasMany, hasOne, lt, lte, sql, withDatabase };
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A type an app defines: what its values are, and how each is kept as a value of the built-in
|
|
3
|
+
* type it's stored as. Made by `defineType`.
|
|
4
|
+
*/
|
|
5
|
+
interface CustomType<T = unknown, S extends AttributeType = AttributeType> {
|
|
6
|
+
/** The built-in type its values are stored as: `"integer"` for money kept as cents. */
|
|
7
|
+
readonly storedAs: S;
|
|
8
|
+
/**
|
|
9
|
+
* An assigned value or a condition's as the type's value. It throws for one it can't hold:
|
|
10
|
+
* assigning that value throws `TypeError`, with this error as its `cause`, and a condition
|
|
11
|
+
* matches nothing. Never called with `null`.
|
|
12
|
+
*/
|
|
13
|
+
cast(input: unknown): T;
|
|
14
|
+
/** A value as the stored type's value: `money.cents`. Never called with `null`. */
|
|
15
|
+
serialize(value: T): BuiltInValue<S>;
|
|
16
|
+
/** A stored type's value, read as the stored type reads a cell, as the type's value. */
|
|
17
|
+
deserialize(stored: BuiltInValue<S>): T;
|
|
18
|
+
}
|
|
19
|
+
/** Any custom type, whatever its values: what a spec takes. */
|
|
20
|
+
type AnyCustomType = CustomType<any, AttributeType>;
|
|
21
|
+
/**
|
|
22
|
+
* Defines an attribute type, used in a spec as a type name is: `price: money`, or
|
|
23
|
+
* `{ type: money, null: false }`. Its values are kept as `storedAs` keeps its own, so they're
|
|
24
|
+
* read strictly, compared and ordered as that type's are.
|
|
25
|
+
*/
|
|
26
|
+
declare function defineType<T, S extends AttributeType>(type: CustomType<T, S>): CustomType<T, S>;
|
|
27
|
+
/**
|
|
28
|
+
* An enum's values: its names, each stored as itself (a `string` attribute's only), or each name
|
|
29
|
+
* with the value it's stored as (`{ draft: 0, published: 1 }`).
|
|
30
|
+
*/
|
|
31
|
+
type EnumValues = readonly string[] | {
|
|
32
|
+
readonly [name: string]: string | number;
|
|
33
|
+
};
|
|
34
|
+
/** A `string` attribute's enum: names stored as themselves, or each with the text it's stored as. */
|
|
35
|
+
type TextEnum = readonly string[] | {
|
|
36
|
+
readonly [name: string]: string;
|
|
37
|
+
};
|
|
38
|
+
/** An `integer` attribute's enum: each name with the integer it's stored as. */
|
|
39
|
+
type IntegerEnum = {
|
|
40
|
+
readonly [name: string]: number;
|
|
41
|
+
};
|
|
42
|
+
/** The names an enum's values give its attribute: `"draft" | "published"`. */
|
|
43
|
+
type EnumNames<E> = E extends readonly (infer N extends string)[] ? N : keyof E & string;
|
|
44
|
+
/** An enum's names, each with the value it's stored as, as `enumValues` gives them. */
|
|
45
|
+
type EnumMapping<E> = E extends readonly string[] ? { readonly [N in E[number]]: N; } : Readonly<E>;
|
|
46
|
+
/** A datetime held in an `INTEGER` column as seconds or milliseconds since 1970. */
|
|
47
|
+
declare const epochStorages: readonly ["unixSeconds", "unixMilliseconds"];
|
|
48
|
+
type EpochStorage = (typeof epochStorages)[number];
|
|
49
|
+
type AttributeType = "string" | "integer" | "real" | "decimal" | "boolean" | "datetime" | "date" | "time" | "json" | "binary";
|
|
50
|
+
/** An attribute's spec written out: its type, and optionally nullability, column and default. */
|
|
51
|
+
interface SpecObject {
|
|
52
|
+
/** A built-in type's name, or a type `defineType` made (#20). */
|
|
53
|
+
type: AttributeType | AnyCustomType;
|
|
54
|
+
null?: boolean;
|
|
55
|
+
column?: string;
|
|
56
|
+
/** A `datetime` held as a Unix epoch integer, as Drizzle's timestamp modes write it (#73). */
|
|
57
|
+
storage?: EpochStorage;
|
|
58
|
+
/** A decimal's most digits in all, as `NUMERIC(10, 2)`'s 10; without a scale, its scale is 0 (#398). */
|
|
59
|
+
precision?: number;
|
|
60
|
+
/** A decimal's digits after the point, as `NUMERIC(10, 2)`'s 2: a cast value is rounded to them (#398). */
|
|
61
|
+
scale?: number;
|
|
62
|
+
/** Evaluated at `build()`, e.g. `() => new Date()`. */
|
|
63
|
+
default?: () => unknown;
|
|
64
|
+
/**
|
|
65
|
+
* Made by the Model's `generateId` when not given, where a default would run: an
|
|
66
|
+
* app-generated key (#99). Generated ids are strings, so only a string attribute has one.
|
|
67
|
+
*/
|
|
68
|
+
generateId?: boolean;
|
|
69
|
+
/** Its only values, by name, and what each is stored as (#340). */
|
|
70
|
+
enum?: EnumValues;
|
|
71
|
+
/** A word before each enum name in its methods' names: the attribute's name for `true` (#340). */
|
|
72
|
+
prefix?: boolean | string;
|
|
73
|
+
/** A word after each enum name in its methods' names: the attribute's name for `true` (#340). */
|
|
74
|
+
suffix?: boolean | string;
|
|
75
|
+
/** `false` to give the Model no scope for each enum name (#340). */
|
|
76
|
+
scopes?: boolean;
|
|
77
|
+
/** `false` to give records no predicate or `saveAs…` setter for each enum name (#340). */
|
|
78
|
+
instanceMethods?: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Validates an enum's value instead of refusing an unknown one when it's assigned, as Rails'
|
|
81
|
+
* `validate: true` does: a record holding another value, or null unless `allowNull`, is invalid.
|
|
82
|
+
*/
|
|
83
|
+
validate?: EnumValidation;
|
|
84
|
+
}
|
|
85
|
+
/** How an enum's value is validated: `true`, or `{ allowNull: true }` to let it be null. */
|
|
86
|
+
type EnumValidation = boolean | {
|
|
87
|
+
readonly allowNull?: boolean;
|
|
88
|
+
};
|
|
89
|
+
/** Where a string attribute's value comes from when it isn't given: a default or `generateId`, never both. */
|
|
90
|
+
type ValueSource = {
|
|
91
|
+
default?: () => unknown;
|
|
92
|
+
generateId?: false;
|
|
93
|
+
} | {
|
|
94
|
+
default?: never;
|
|
95
|
+
generateId: true;
|
|
96
|
+
};
|
|
97
|
+
/** An attribute that isn't an enum: no type but a string or an integer has one (#340). */
|
|
98
|
+
type NoEnum = {
|
|
99
|
+
enum?: never;
|
|
100
|
+
prefix?: never;
|
|
101
|
+
suffix?: never;
|
|
102
|
+
scopes?: never;
|
|
103
|
+
instanceMethods?: never;
|
|
104
|
+
validate?: never;
|
|
105
|
+
};
|
|
106
|
+
/** An attribute that isn't a decimal: only a decimal has a precision and a scale (#398). */
|
|
107
|
+
type NoDigits = {
|
|
108
|
+
precision?: never;
|
|
109
|
+
scale?: never;
|
|
110
|
+
};
|
|
111
|
+
/** The spec's keys that only some types have, each written by the type's own branch below. */
|
|
112
|
+
type PerTypeKeys = "type" | "generateId" | "storage" | "enum" | "precision" | "scale";
|
|
113
|
+
type AttributeSpec = AttributeType | AnyCustomType | (Omit<SpecObject, "default" | PerTypeKeys> & {
|
|
114
|
+
type: "string";
|
|
115
|
+
storage?: never;
|
|
116
|
+
enum?: TextEnum;
|
|
117
|
+
} & ValueSource & NoDigits) | (Omit<SpecObject, PerTypeKeys> & NoDigits & {
|
|
118
|
+
type: "integer";
|
|
119
|
+
generateId?: false;
|
|
120
|
+
storage?: never;
|
|
121
|
+
enum?: IntegerEnum;
|
|
122
|
+
}) | (Omit<SpecObject, PerTypeKeys> & NoEnum & NoDigits & {
|
|
123
|
+
type: Exclude<AttributeType, "string" | "integer" | "datetime" | "decimal">;
|
|
124
|
+
generateId?: false;
|
|
125
|
+
storage?: never;
|
|
126
|
+
}) | (Omit<SpecObject, PerTypeKeys> & NoEnum & {
|
|
127
|
+
type: "decimal";
|
|
128
|
+
generateId?: false;
|
|
129
|
+
storage?: never;
|
|
130
|
+
precision?: number;
|
|
131
|
+
scale?: number;
|
|
132
|
+
}) | (Omit<SpecObject, Exclude<PerTypeKeys, "storage">> & NoEnum & NoDigits & {
|
|
133
|
+
type: "datetime";
|
|
134
|
+
generateId?: false;
|
|
135
|
+
}) | (Omit<SpecObject, PerTypeKeys> & NoEnum & NoDigits & {
|
|
136
|
+
type: AnyCustomType;
|
|
137
|
+
generateId?: false;
|
|
138
|
+
storage?: never;
|
|
139
|
+
});
|
|
140
|
+
type AttributeSpecs = Record<string, AttributeSpec>;
|
|
141
|
+
/**
|
|
142
|
+
* `A`, with any key a spec doesn't have typed `never`, so a misspelled one (`nul: false`) is a
|
|
143
|
+
* compile error: a generic parameter skips TypeScript's own check for unknown keys (#102).
|
|
144
|
+
*/
|
|
145
|
+
type ExactSpecs<A> = { [K in keyof A]: A[K] extends AnyCustomType ? A[K] : A[K] extends object ? A[K] & Record<Exclude<keyof A[K], keyof SpecObject>, never> : A[K]; };
|
|
146
|
+
/** An attribute's resolved metadata. */
|
|
147
|
+
interface AttributeDefinition {
|
|
148
|
+
/** Its built-in type: for a custom type, the type it's stored as. */
|
|
149
|
+
type: AttributeType;
|
|
150
|
+
/** The type `defineType` made, whose values it holds, kept as `type`'s (#20). */
|
|
151
|
+
customType?: AnyCustomType;
|
|
152
|
+
column: string;
|
|
153
|
+
default?: () => unknown;
|
|
154
|
+
generateId?: true;
|
|
155
|
+
storage?: EpochStorage;
|
|
156
|
+
precision?: number;
|
|
157
|
+
scale?: number;
|
|
158
|
+
enum?: EnumValues;
|
|
159
|
+
prefix?: boolean | string;
|
|
160
|
+
suffix?: boolean | string;
|
|
161
|
+
scopes?: boolean;
|
|
162
|
+
instanceMethods?: boolean;
|
|
163
|
+
validate?: EnumValidation;
|
|
164
|
+
}
|
|
165
|
+
/** The JavaScript value an attribute of a built-in type holds: a `Date` for a `datetime`. */
|
|
166
|
+
type BuiltInValue<T extends AttributeType> = T extends "string" ? string : T extends "integer" | "real" ? number : T extends "boolean" ? boolean : T extends "datetime" ? Date : T extends "binary" ? Uint8Array : T extends "decimal" | "date" | "time" ? string : unknown;
|
|
167
|
+
/** A type's JavaScript value: a built-in type's, or a custom type's own (#20). */
|
|
168
|
+
type ValueOf<T> = T extends AttributeType ? BuiltInValue<T> : T extends CustomType<infer V, AttributeType> ? V : never;
|
|
169
|
+
/** A spec's value, not null: an enum's names (#340), or its type's JavaScript value. */
|
|
170
|
+
type SpecValue<S> = S extends {
|
|
171
|
+
enum: infer E extends EnumValues;
|
|
172
|
+
} ? EnumNames<E> : S extends {
|
|
173
|
+
type: infer T;
|
|
174
|
+
} ? ValueOf<T> : never;
|
|
175
|
+
type AttributeValue<S> = S extends AttributeType | AnyCustomType ? ValueOf<S> | null : S extends {
|
|
176
|
+
type: unknown;
|
|
177
|
+
null: false;
|
|
178
|
+
} ? SpecValue<S> : S extends {
|
|
179
|
+
type: unknown;
|
|
180
|
+
} ? SpecValue<S> | null : never;
|
|
181
|
+
type AttributeProperties<A extends AttributeSpecs> = { -readonly [K in keyof A]: AttributeValue<A[K]>; };
|
|
182
|
+
export { defineType as _, AttributeType as a, ValueSource as c, EnumNames as d, EnumValues as f, CustomType as g, AnyCustomType as h, AttributeSpecs as i, EpochStorage as l, TextEnum as m, AttributeProperties as n, ExactSpecs as o, IntegerEnum as p, AttributeSpec as r, SpecObject as s, AttributeDefinition as t, EnumMapping as u };
|