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