@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/dist/cli.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- //#region cli/generate/columns.d.ts
2
- type ColumnType = "string" | "integer" | "real" | "boolean" | "datetime" | "json" | "references";
1
+ import { a as AttributeType } from "./metadata-CzntpYzC.mjs";
2
+ type ColumnType = "string" | "integer" | "real" | "decimal" | "boolean" | "datetime" | "date" | "time" | "json" | "binary" | "references";
3
3
  /** One column as the generators' syntax gives it: `email:string!:unique`, `user:references`. */
4
4
  interface ColumnArg {
5
5
  /** The column's name, in snake_case (`user_id` for `user:references`). */
@@ -9,46 +9,69 @@ interface ColumnArg {
9
9
  index: "none" | "index" | "unique";
10
10
  /** A literal default, from `=value`. */
11
11
  default?: string | number | boolean;
12
+ /** A decimal's digits in all and after the point, from `decimal{10,2}` (#398). */
13
+ precision?: number;
14
+ scale?: number;
12
15
  /** For `references`: the table it points at, and the `belongsTo` it suggests. */
13
16
  references?: string;
14
17
  association?: string;
15
18
  }
16
- //#endregion
17
- //#region cli/project/migrations.d.ts
18
19
  interface Migration {
19
- /** The file's name, as Wrangler orders and records it. */
20
+ /** The file's name, relative to the migrations folder, which orders it. */
20
21
  name: string;
21
22
  sql: string;
22
23
  }
23
24
  /** Plain character order, the same on every machine (unlike `localeCompare`). */
24
25
  export declare function byName(a: string, b: string): number;
25
- //#endregion
26
- //#region cli/adapter.d.ts
26
+ /**
27
+ * Migrations in the order they're applied, as migration tools order them: folder by folder, by
28
+ * their leading numbers (`9_…` before `10_…`), those without one after them, and by name between
29
+ * equals; a path that's the start of another comes first.
30
+ */
31
+ export declare function byVersion(a: string, b: string): number;
27
32
  /** One database a project declares, and where its migrations are. */
28
33
  interface DatabaseConfig {
29
34
  /** Its name in the app's config, which a generated schema names: its **Binding** (`DB`). */
30
35
  binding: string;
31
- /** Relative to the config file. */
36
+ /** Relative to its config file's folder. */
32
37
  migrationsDir: string;
33
- /** A glob for nested migration layouts (D1's `migrations_pattern`). */
38
+ /** A glob for migrations in nested folders, from the config's `patternSetting`. */
34
39
  migrationsPattern?: string;
35
40
  /** The adapter's record of applied migrations, left out of the schema. */
36
41
  migrationsTable?: string;
37
42
  /** The database's entry as the config has it. */
38
43
  entry: Record<string, unknown>;
44
+ /**
45
+ * The file that declares it, when it isn't the config's own: the adapter's config file that its
46
+ * entry in `active-record.jsonc` names (#306).
47
+ */
48
+ configFile?: string;
39
49
  }
40
- /** A project's config: the file, its folder, and its databases, or why it can't be read. */
50
+ /**
51
+ * The databases a project's `active-record.jsonc` names, each with its entry as the file has it
52
+ * (its `adapter` included), and the file and its folder.
53
+ */
54
+ interface ProjectDatabases {
55
+ file: string;
56
+ dir: string;
57
+ /** Each database's entry, by binding, in the file's order. */
58
+ databases: Readonly<Record<string, Record<string, unknown>>>;
59
+ }
60
+ /**
61
+ * A project's config: the file, its folder, and its databases, or why it can't be read. A
62
+ * database's own `configFile`, when it has one, is its file in place of this one.
63
+ */
41
64
  type ConfigResult = {
42
65
  ok: true;
43
66
  file: string;
44
67
  dir: string;
45
68
  databases: DatabaseConfig[];
46
- accountId?: string;
69
+ /** For a person, such as databases the adapter's own config has that the file doesn't name. */
70
+ notes?: string[];
47
71
  } | {
48
72
  ok: false;
49
73
  error: string;
50
74
  };
51
- type AttributeType = "string" | "integer" | "real" | "boolean" | "datetime" | "json";
52
75
  /** A default the generated spec carries: a literal value, or a date. */
53
76
  type Default = {
54
77
  literal: unknown;
@@ -63,6 +86,16 @@ interface Spec {
63
86
  default?: Default;
64
87
  /** A key made in code by the Model's `generateId` (#99). */
65
88
  generateId?: true;
89
+ /** A decimal's digits in all, from `NUMERIC(10, 2)` (#398). */
90
+ precision?: number;
91
+ /** A decimal's digits after the point, from `NUMERIC(10, 2)` (#398). */
92
+ scale?: number;
93
+ /** A generated enum's names, which the database restricts the column to (#64). */
94
+ enum?: readonly string[];
95
+ /** `false` on a generated enum: its scopes wait for the Model's override (#64). */
96
+ scopes?: false;
97
+ /** `false` on a generated enum: its record methods wait for the Model's override (#64). */
98
+ instanceMethods?: false;
66
99
  }
67
100
  /** One table, ready to generate: its attributes in column order. */
68
101
  interface GeneratedTable {
@@ -73,6 +106,8 @@ interface GeneratedTable {
73
106
  /** The migration that created or last changed it. */
74
107
  lastChangedBy: string;
75
108
  attributes: Record<string, Spec>;
109
+ /** Its key column's type, as the database declares it (`uuid`), when the adapter reads one. */
110
+ keyType?: string;
76
111
  }
77
112
  /** A database's tables, read from its migrations, with notes for a person, or why they can't be. */
78
113
  type SchemaResult = {
@@ -84,28 +119,36 @@ type SchemaResult = {
84
119
  ok: false;
85
120
  errors: string[];
86
121
  };
87
- /** What `g model` and `g migration` ask an adapter for: a migration's SQL, and its file's name. */
122
+ /** What an adapter reads a database's schema with, besides its migrations. */
123
+ interface SchemaOptions {
124
+ /** The adapter's record of applied migrations, left out of the schema. */
125
+ migrationsTable?: string;
126
+ /** The database's entry, as the config has it, for an adapter that reads more of it. */
127
+ entry?: Record<string, unknown>;
128
+ }
129
+ /** What `g model` and `g migration` ask an adapter for: a migration's SQL. */
88
130
  interface MigrationChange {
89
131
  /** The migration's name, as Rails reads it (`CreateProducts`, `AddSkuToProducts`, …). */
90
132
  name: string;
91
133
  columns: readonly ColumnArg[];
92
- /** Each existing table's key type, for the columns that reference it. */
93
- keys: ReadonlyMap<string, KeyType>;
94
- /** The migrations already in the folder, by path, for the new one's number. */
95
- existing: readonly string[];
134
+ /**
135
+ * Each existing table's key column's type, as its adapter declares it (`INTEGER`, `uuid`), for
136
+ * the columns that reference it.
137
+ */
138
+ keys: ReadonlyMap<string, string>;
96
139
  /** `--id integer`, and `--no-timestamps` as `timestamps: false`. */
97
140
  options: {
98
141
  integerId?: boolean;
99
142
  timestamps?: boolean;
100
143
  };
101
144
  }
102
- /** A table's key type, which a column referencing it takes. */
103
- type KeyType = "TEXT" | "INTEGER";
104
- /** A migration's file name and SQL, or why it can't be written. */
105
- type MigrationResult = {
145
+ /** A migration's SQL, or why it can't be written. */
146
+ type MigrationResult =
147
+ /** `notes` say what was guessed, such as the key type of a table the migrations don't make yet. */
148
+ {
106
149
  ok: true;
107
- file: string;
108
150
  sql: string;
151
+ notes?: string[];
109
152
  } | {
110
153
  ok: false;
111
154
  error: string;
@@ -116,8 +159,8 @@ interface ChosenDatabase {
116
159
  /** Its entry, as the config has it. */
117
160
  entry: Record<string, unknown>;
118
161
  configFile: string;
119
- /** The config's account, which a config of the adapter's own would lose (D1's `account_id`). */
120
- accountId?: string;
162
+ /** Its migrations folder, as an absolute path. */
163
+ migrationsDir: string;
121
164
  }
122
165
  /** A remote database, named for a person to confirm before anything reaches it. */
123
166
  interface RemoteTarget {
@@ -130,32 +173,62 @@ interface RemoteTarget {
130
173
  }
131
174
  /** An open database for the console, what to tell the person about it, and how to close it. */
132
175
  interface OpenBinding {
133
- /** What the project's `connect` takes: on D1, the `D1Database` binding, as `env.DB`. */
176
+ /** What the project's `connect` takes, as a Worker's `env` holds it. */
134
177
  binding: unknown;
135
178
  description: string;
136
179
  dispose: () => Promise<void>;
137
180
  }
181
+ /**
182
+ * The databases `init` writes for an adapter, as `active-record.jsonc` entries (its `adapter` is
183
+ * added), and notes for a person; or why it can't write them; or undefined when nothing in the
184
+ * project points to this adapter and `--adapter` didn't name it.
185
+ */
186
+ type InitResult = {
187
+ databases: Record<string, Record<string, unknown>>;
188
+ /** A note for a person about a database, such as a guess it holds, by its binding. */
189
+ notes?: Record<string, string>;
190
+ } | string | undefined;
138
191
  /** Where `migrate` writes what the adapter's own tool prints. */
139
192
  interface MigrateOutput {
140
193
  output: (text: string) => void;
141
194
  }
142
- /** An adapter's plugin for the `active-record` command, from its `/cli` entry (ADR-0016). */
195
+ /** An adapter's plugin for the `active-record` command, which ships with the command (ADR-0019). */
143
196
  interface CliAdapter {
144
- /** The name `--adapter` takes: `d1`. */
197
+ /** The name a database's `adapter` takes, which a generated schema imports its factory by. */
145
198
  readonly name: string;
146
- /** The project's databases, from its config, or why they can't be read. */
147
- databases(cwd: string, configPath?: string): ConfigResult;
199
+ /**
200
+ * The config setting a database's `migrationsPattern` comes from, as a person writes it in the
201
+ * config; none when the adapter reads no pattern, and migrations in folders go unread.
202
+ */
203
+ readonly patternSetting?: string;
204
+ /** Why this Node can't run the plugin, if it can't; the command refuses before it starts. */
205
+ unsupported?(): string | undefined;
206
+ /**
207
+ * The entries `init` writes, from what's in `dir` (the folder `active-record.jsonc` goes in),
208
+ * such as another tool's config. Undefined when nothing there points to this adapter, unless
209
+ * `--adapter` `named` it as an adapter the file doesn't use yet, when it writes what it can (a
210
+ * guess included).
211
+ */
212
+ init(dir: string, named: boolean): InitResult;
213
+ /**
214
+ * The project's databases, from the entries its `active-record.jsonc` has for this adapter (and
215
+ * the adapter's own config, if it reads one), or why they can't be read.
216
+ */
217
+ databases(project: ProjectDatabases): ConfigResult;
148
218
  /** A database's tables, from its migrations applied in order. */
149
- readSchema(migrations: readonly Migration[], options: {
150
- migrationsTable?: string;
151
- }): SchemaResult;
152
- /** A migration's SQL, and its file name after the existing ones. */
219
+ readSchema(migrations: readonly Migration[], options: SchemaOptions): Promise<SchemaResult>;
220
+ /** A migration's SQL; the command names its file. */
153
221
  migration(change: MigrationChange): MigrationResult;
154
222
  /**
155
223
  * The remote database `chosen` names, or why it has none: the one the console opens, or the one
156
- * `migrate` applies migrations to, which needn't be the same (D1's preview database).
224
+ * `migrate` applies migrations to, which needn't be the same (a preview database, say).
157
225
  */
158
226
  remote(chosen: ChosenDatabase, use: "open" | "migrate"): RemoteTarget | string;
227
+ /**
228
+ * The folder for the command's own local files, such as the console's history: one the
229
+ * project's tools already keep out of version control.
230
+ */
231
+ stateFolder(chosen: ChosenDatabase): string;
159
232
  /** Opens the database for the console: the local one, or with `remote`, the real one. */
160
233
  open(cwd: string, chosen: ChosenDatabase, options: {
161
234
  remote: boolean;
@@ -165,8 +238,49 @@ interface CliAdapter {
165
238
  remote: boolean;
166
239
  }, io: MigrateOutput): Promise<number>;
167
240
  }
168
- //#endregion
169
- //#region cli/project/names.d.ts
241
+ /** What a migration's name says it does (`CreateProducts`, `AddSkuToProducts`, …), with its table plural. */
242
+ type MigrationPlan = {
243
+ kind: "create" | "add" | "remove";
244
+ table: string;
245
+ } | {
246
+ kind: "empty";
247
+ table?: undefined;
248
+ };
249
+ /** What to do, read from a migration's name: `create_products`, `add_sku_to_products`, …; else empty. */
250
+ export declare function migrationPlan(name: string): MigrationPlan;
251
+ /** The index the generators name a column's index by: `index_products_on_sku`. */
252
+ export declare function indexName(table: string, column: string): string;
253
+ /** What a migration's SQL takes from its database: its names, types and keys, and what it can't do. */
254
+ interface MigrationDialect {
255
+ /** The words the database reserves, which a table or column named one is quoted for. */
256
+ reserved: ReadonlySet<string>;
257
+ /** Each column type's SQL; a reference takes its table's key type. */
258
+ types: Readonly<Record<Exclude<ColumnArg["type"], "references">, string>>;
259
+ /** The key column a new table gets: one made in code, or with `--id integer`, the database's. */
260
+ key: (integerId: boolean) => string;
261
+ /** The type of `created_at` and `updated_at`. */
262
+ timestampType: string;
263
+ /** The key type a reference takes when its table isn't in the migrations yet. */
264
+ guessedKeyType: string;
265
+ /** A check a column gets beside its type, such as one that its JSON is valid. */
266
+ check?: (column: ColumnArg, name: string) => string | undefined;
267
+ /** Why the database can't add this column to an existing table, if it can't. */
268
+ addRefusal?: (table: string, column: ColumnArg) => string | undefined;
269
+ /** The statements that drop a column (and what has to go first), each ending in `;\n`. */
270
+ dropColumn: (table: string, column: string) => string;
271
+ }
272
+ /** A name as SQL: quoted only when the database reserves it, so migrations stay readable. */
273
+ export declare function quotedName(dialect: MigrationDialect, name: string): string;
274
+ /**
275
+ * A migration's SQL in the dialect given: a table created, columns added or removed, or an empty
276
+ * migration, as its name says (`CreateProducts`, `AddSkuToProducts`, …), with a note for each key
277
+ * type it guessed.
278
+ */
279
+ export declare function migrationSql(change: MigrationChange, dialect: MigrationDialect): MigrationResult;
280
+ /** The project's config file's name, which `--config` replaces. */
281
+ export declare const configFileName = "active-record.jsonc";
282
+ /** JSON with `//` and `/* *\/` comments and trailing commas. */
283
+ export declare function parseJsonc(text: string): unknown;
170
284
  /** A column's attribute: camelCase, keeping any leading underscores (`_secret` stays). */
171
285
  export declare function attributeName(column: string): string;
172
286
  /**
@@ -175,23 +289,79 @@ export declare function attributeName(column: string): string;
175
289
  * `table2fa`).
176
290
  */
177
291
  export declare function exportName(table: string): string;
178
- /** `"A"`, `"A or B"`, `"A, B or C"`. */
179
- export declare function listed(names: readonly string[]): string;
180
- //#endregion
181
- //#region cli/schema/storage-override.d.ts
292
+ /** `"A"`, `"A or B"`, `"A, B or C"`, or with `"and"`, `"A, B and C"`. */
293
+ export declare function listed(names: readonly string[], last?: "or" | "and"): string;
294
+ /** A decimal's precision and scale, as `NUMERIC(10, 2)` gives them (#398). */
295
+ interface DecimalDigits {
296
+ precision?: number;
297
+ scale?: number;
298
+ }
299
+ declare const castDefaultTypes: readonly ["decimal", "date", "time"];
300
+ /** The attribute types whose literal default is cast as an assigned value is. */
301
+ type CastDefaultType = (typeof castDefaultTypes)[number];
302
+ /** Whether a type's literal default is cast as an assigned value is. */
303
+ export declare function isCastDefaultType(type: string): type is CastDefaultType;
304
+ /**
305
+ * A literal default's text as the attribute's value, with a decimal's `digits` (#398) (`1.50` → `"1.5"`, `'09:30'` → `"09:30:00"`),
306
+ * or undefined for text the type can't hold, whose default then stays with the database.
307
+ */
308
+ export declare function castDefault(type: CastDefaultType, text: string, digits?: DecimalDigits): {
309
+ literal: string;
310
+ } | undefined;
311
+ /**
312
+ * `spec` with a decimal's precision and scale, as a column's type gives them: neither when the
313
+ * column has no precision, and the scale only when it's given. Digits no `NUMERIC(p, s)` has (a
314
+ * precision past 1000, a scale past the precision) leave both off, so the Model stays valid.
315
+ */
316
+ export declare function withDigits(spec: Spec, precision?: number | null, scale?: number | null): Spec;
317
+ /** A string spec with the names its column is restricted to, as a generated enum; any other spec as it is. */
318
+ export declare function withGeneratedEnum(spec: Spec, names: readonly string[] | undefined): Spec;
319
+ /** A CHECK's list of SQL string literals as their text, each once: `'a', 'it''s', 'a'` → `["a", "it's"]`. */
320
+ export declare function namesInList(list: string): string[];
182
321
  /** The override that reads a Unix time as a `Date`, for a note; with Drizzle's modes named if asked. */
183
322
  export declare function storageOverride(withDrizzleModes: boolean): string;
184
- //#endregion
185
- //#region src/inflections.d.ts
323
+ /** What a column became, or why it can't be one, plus a note when its type was guessed. */
324
+ type Converted = {
325
+ ok: true;
326
+ attribute: string;
327
+ spec: Spec;
328
+ note?: string;
329
+ textDate?: true;
330
+ } | {
331
+ ok: false;
332
+ error: string;
333
+ };
334
+ /** The least a reader knows of a column: its table, its name, and whether it's in the key. */
335
+ interface ReadColumn {
336
+ table: string;
337
+ name: string;
338
+ primaryKey: boolean;
339
+ }
340
+ /** A table an adapter read: its columns in order, and the migration that last changed it. */
341
+ interface ReadTable<C extends ReadColumn> {
342
+ name: string;
343
+ columns: readonly C[];
344
+ lastChangedBy: string;
345
+ /** Its key column's type, as the database declares it, for the columns that reference it. */
346
+ keyType?: string;
347
+ }
348
+ /** How an adapter reads its columns: each one converted, and what to say of a table without a key. */
349
+ interface Reading<C extends ReadColumn> {
350
+ convert: (column: C) => Converted;
351
+ /** The error for a table with no primary key, saying what to give it. */
352
+ missingKey: (table: string) => string;
353
+ }
354
+ /**
355
+ * The generated schema's tables, from the tables an adapter read, in alphabetical order (so the
356
+ * result only changes with the schema), or every error that stops them.
357
+ */
358
+ export declare function generatedSchema<C extends ReadColumn>(read: readonly ReadTable<C>[], reading: Reading<C>): SchemaResult;
186
359
  /** Rails' `pluralize`: `category` → `categories`, `person` → `people`, `sheep` → `sheep`. */
187
360
  export declare function pluralize(word: string): string;
188
- //#endregion
189
- //#region src/naming.d.ts
190
361
  /**
191
362
  * Converts a camelCase attribute name to its snake_case column name, treating
192
363
  * a run of capitals as one word (Rails' `underscore`): `htmlURL` → `html_url`,
193
364
  * `userID` → `user_id`, `sha256Hash` → `sha256_hash`.
194
365
  */
195
366
  export declare function underscore(name: string): string;
196
- //#endregion
197
- export type { ChosenDatabase, CliAdapter, ColumnArg, ConfigResult, DatabaseConfig, Default, GeneratedTable, KeyType, MigrateOutput, Migration, MigrationChange, MigrationResult, OpenBinding, RemoteTarget, SchemaResult, Spec };
367
+ export type { CastDefaultType, ChosenDatabase, CliAdapter, ColumnArg, ConfigResult, Converted, DatabaseConfig, Default, GeneratedTable, InitResult, MigrateOutput, Migration, MigrationChange, MigrationDialect, MigrationPlan, MigrationResult, OpenBinding, ProjectDatabases, ReadColumn, ReadTable, Reading, RemoteTarget, SchemaOptions, SchemaResult, Spec };
package/dist/cli.mjs CHANGED
@@ -1,3 +1,4 @@
1
- import { a as underscore, n as pluralize } from "./epoch-storage-o2aoOH7H.mjs";
2
- import { c as byName, i as exportName, n as attributeName, o as listed, t as storageOverride } from "./storage-override-BYBYP8Mj.mjs";
3
- export { attributeName, byName, exportName, listed, pluralize, storageOverride, underscore };
1
+ import { C as pluralize, E as underscore } from "./epoch-storage-CDlQZcBB.mjs";
2
+ import { a as migrationSql, c as migrationPlan, i as withDigits, n as namesInList, o as quotedName, r as withGeneratedEnum, s as indexName, t as generatedSchema } from "./for-adapters-DhNV1mWf.mjs";
3
+ import { f as byName, i as attributeName, l as configFileName, n as castDefault, o as exportName, p as byVersion, r as isCastDefaultType, s as listed, t as storageOverride, u as parseJsonc } from "./storage-override-n4w1cS_M.mjs";
4
+ export { attributeName, byName, byVersion, castDefault, configFileName, exportName, generatedSchema, indexName, isCastDefaultType, listed, migrationPlan, migrationSql, namesInList, parseJsonc, pluralize, quotedName, storageOverride, underscore, withDigits, withGeneratedEnum };