@forestfuture/active-record 0.0.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 ADDED
@@ -0,0 +1,345 @@
1
+ //#region cli/generate/columns.d.ts
2
+ type ColumnType = "string" | "integer" | "real" | "boolean" | "datetime" | "json" | "references";
3
+ /** One column as the generators' syntax gives it: `email:string!:unique`, `user:references`. */
4
+ interface ColumnArg {
5
+ /** The column's name, in snake_case (`user_id` for `user:references`). */
6
+ name: string;
7
+ type: ColumnType;
8
+ notNull: boolean;
9
+ index: "none" | "index" | "unique";
10
+ /** A literal default, from `=value`. */
11
+ default?: string | number | boolean;
12
+ /** For `references`: the table it points at, and the `belongsTo` it suggests. */
13
+ references?: string;
14
+ association?: string;
15
+ }
16
+ //#endregion
17
+ //#region cli/project/migrations.d.ts
18
+ interface Migration {
19
+ /** The file's name, relative to the migrations folder, which orders it. */
20
+ name: string;
21
+ sql: string;
22
+ }
23
+ /** Plain character order, the same on every machine (unlike `localeCompare`). */
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;
31
+ //#endregion
32
+ //#region cli/adapter.d.ts
33
+ /** One database a project declares, and where its migrations are. */
34
+ interface DatabaseConfig {
35
+ /** Its name in the app's config, which a generated schema names: its **Binding** (`DB`). */
36
+ binding: string;
37
+ /** Relative to the config file. */
38
+ migrationsDir: string;
39
+ /** A glob for migrations in nested folders, from the config's `patternSetting`. */
40
+ migrationsPattern?: string;
41
+ /** The adapter's record of applied migrations, left out of the schema. */
42
+ migrationsTable?: string;
43
+ /** The database's entry as the config has it. */
44
+ entry: Record<string, unknown>;
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
+ }
56
+ /** A project's config: the file, its folder, and its databases, or why it can't be read. */
57
+ type ConfigResult = {
58
+ ok: true;
59
+ file: string;
60
+ dir: string;
61
+ databases: DatabaseConfig[];
62
+ /** For a person, such as databases the adapter's own config has that the file doesn't name. */
63
+ notes?: string[];
64
+ } | {
65
+ ok: false;
66
+ error: string;
67
+ };
68
+ type AttributeType = "string" | "integer" | "real" | "boolean" | "datetime" | "json";
69
+ /** A default the generated spec carries: a literal value, or a date. */
70
+ type Default = {
71
+ literal: unknown;
72
+ } | {
73
+ date: string;
74
+ };
75
+ /** One attribute as the generated schema declares it, the shape `Model.define({...})` takes. */
76
+ interface Spec {
77
+ type: AttributeType;
78
+ null?: false;
79
+ column?: string;
80
+ default?: Default;
81
+ /** A key made in code by the Model's `generateId` (#99). */
82
+ generateId?: true;
83
+ }
84
+ /** One table, ready to generate: its attributes in column order. */
85
+ interface GeneratedTable {
86
+ name: string;
87
+ /** The `const` it's exported as in the generated file. */
88
+ exportName: string;
89
+ primaryKey: string;
90
+ /** The migration that created or last changed it. */
91
+ lastChangedBy: string;
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;
95
+ }
96
+ /** A database's tables, read from its migrations, with notes for a person, or why they can't be. */
97
+ type SchemaResult = {
98
+ ok: true;
99
+ tables: GeneratedTable[];
100
+ notes: string[];
101
+ textDates: string[];
102
+ } | {
103
+ ok: false;
104
+ errors: string[];
105
+ };
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. */
114
+ interface MigrationChange {
115
+ /** The migration's name, as Rails reads it (`CreateProducts`, `AddSkuToProducts`, …). */
116
+ name: string;
117
+ columns: readonly ColumnArg[];
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>;
123
+ /** `--id integer`, and `--no-timestamps` as `timestamps: false`. */
124
+ options: {
125
+ integerId?: boolean;
126
+ timestamps?: boolean;
127
+ };
128
+ }
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
+ {
133
+ ok: true;
134
+ sql: string;
135
+ notes?: string[];
136
+ } | {
137
+ ok: false;
138
+ error: string;
139
+ };
140
+ /** The database a command acts on, and the config file that declares it. */
141
+ interface ChosenDatabase {
142
+ binding: string;
143
+ /** Its entry, as the config has it. */
144
+ entry: Record<string, unknown>;
145
+ configFile: string;
146
+ /** Its migrations folder, as an absolute path. */
147
+ migrationsDir: string;
148
+ }
149
+ /** A remote database, named for a person to confirm before anything reaches it. */
150
+ interface RemoteTarget {
151
+ /** What the person types to confirm: its name. */
152
+ name: string;
153
+ /** "Remote database DB (shop, id 1a2b)". */
154
+ description: string;
155
+ /** A line more, when the remote database isn't the one a person would expect. */
156
+ note?: string;
157
+ }
158
+ /** An open database for the console, what to tell the person about it, and how to close it. */
159
+ interface OpenBinding {
160
+ /** What the project's `connect` takes, as a Worker's `env` holds it. */
161
+ binding: unknown;
162
+ description: string;
163
+ dispose: () => Promise<void>;
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;
175
+ /** Where `migrate` writes what the adapter's own tool prints. */
176
+ interface MigrateOutput {
177
+ output: (text: string) => void;
178
+ }
179
+ /** An adapter's plugin for the `active-record` command, which ships with the command (ADR-0019). */
180
+ interface CliAdapter {
181
+ /** The name a database's `adapter` takes, which a generated schema imports its factory by. */
182
+ readonly name: string;
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;
201
+ /** A database's tables, from its migrations applied in order. */
202
+ readSchema(migrations: readonly Migration[], options: SchemaOptions): Promise<SchemaResult>;
203
+ /** A migration's SQL; the command names its file. */
204
+ migration(change: MigrationChange): MigrationResult;
205
+ /**
206
+ * The remote database `chosen` names, or why it has none: the one the console opens, or the one
207
+ * `migrate` applies migrations to, which needn't be the same (a preview database, say).
208
+ */
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;
215
+ /** Opens the database for the console: the local one, or with `remote`, the real one. */
216
+ open(cwd: string, chosen: ChosenDatabase, options: {
217
+ remote: boolean;
218
+ }): Promise<OpenBinding | string>;
219
+ /** Applies the pending migrations, locally or with `remote` to the real database; the exit code. */
220
+ migrate(cwd: string, chosen: ChosenDatabase, options: {
221
+ remote: boolean;
222
+ }, io: MigrateOutput): Promise<number>;
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;
278
+ //#endregion
279
+ //#region cli/project/names.d.ts
280
+ /** A column's attribute: camelCase, keeping any leading underscores (`_secret` stays). */
281
+ export declare function attributeName(column: string): string;
282
+ /**
283
+ * The `const` a table is exported as: its words in camelCase (`line_items` → `lineItems`),
284
+ * never a reserved word (`default` → `defaultTable`) or starting with a digit (`2fa` →
285
+ * `table2fa`).
286
+ */
287
+ export declare function exportName(table: 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;
290
+ //#endregion
291
+ //#region cli/schema/storage-override.d.ts
292
+ /** The override that reads a Unix time as a `Date`, for a note; with Drizzle's modes named if asked. */
293
+ export declare function storageOverride(withDrizzleModes: boolean): string;
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
333
+ //#region src/inflections.d.ts
334
+ /** Rails' `pluralize`: `category` → `categories`, `person` → `people`, `sheep` → `sheep`. */
335
+ export declare function pluralize(word: string): string;
336
+ //#endregion
337
+ //#region src/naming.d.ts
338
+ /**
339
+ * Converts a camelCase attribute name to its snake_case column name, treating
340
+ * a run of capitals as one word (Rails' `underscore`): `htmlURL` → `html_url`,
341
+ * `userID` → `user_id`, `sha256Hash` → `sha256_hash`.
342
+ */
343
+ export declare function underscore(name: string): string;
344
+ //#endregion
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 ADDED
@@ -0,0 +1,4 @@
1
+ import { a as underscore, n as pluralize } from "./epoch-storage-o2aoOH7H.mjs";
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 };
@@ -0,0 +1 @@
1
+ export {}