@cslug/database 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/api.md +1 -1
- package/package.json +2 -2
- package/src/encryption.ts +1 -0
- package/src/errors.ts +1 -0
- package/src/factory.ts +1 -0
- package/src/fake.ts +1 -0
- package/src/migrator.ts +8 -2
- package/src/sql.ts +1 -0
- package/src/table.ts +1 -0
package/docs/api.md
CHANGED
|
@@ -112,7 +112,7 @@ SQL files `database/migrations/YYYYMMDD_HHMMSS_name.sql` with `-- up` / `-- down
|
|
|
112
112
|
and `$$` bodies are fine). A first line `-- cslug:no-transaction` runs the file outside a transaction (needed for
|
|
113
113
|
`create index concurrently`).
|
|
114
114
|
|
|
115
|
-
| `Migrator(db, { paths?, env?, log? })` | |
|
|
115
|
+
| `Migrator(db, { paths?, env?, log?, packageMigrations? })` | `packageMigrations` defaults to the folders added with `addMigrations()` |
|
|
116
116
|
|---|---|
|
|
117
117
|
| `migrate()` | Pending migrations, each in its own transaction, recorded in `cslug_migrations` with a batch number. The whole run holds an advisory lock: concurrent runs execute each migration **once** |
|
|
118
118
|
| `rollback({ steps? })` | Undo the last `steps` batches (default 1), newest first; fails clearly without a `-- down` |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cslug/database",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "PostgreSQL on Bun.sql: typed tables, query builder, sql tag, repositories, migrations.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -28,6 +28,6 @@
|
|
|
28
28
|
"bun": ">=1.3.14"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@cslug/core": "0.
|
|
31
|
+
"@cslug/core": "0.2.0"
|
|
32
32
|
}
|
|
33
33
|
}
|
package/src/encryption.ts
CHANGED
|
@@ -27,6 +27,7 @@ export function encrypt(plain: string): string {
|
|
|
27
27
|
)
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
/** Reverse of `encrypt()`; throws `db.decrypt_failed` on a tampered value or a wrong APP_KEY. */
|
|
30
31
|
export function decrypt(stored: string): string {
|
|
31
32
|
const [version, iv, tag, data] = stored.split('.')
|
|
32
33
|
if (version !== 'v1' || !iv || !tag || data === undefined)
|
package/src/errors.ts
CHANGED
|
@@ -23,6 +23,7 @@ export class SerializationError extends QueryError {}
|
|
|
23
23
|
export class RowCountError extends CslugError {}
|
|
24
24
|
|
|
25
25
|
const RETRYABLE = new Set(['40001', '40P01'])
|
|
26
|
+
/** Serialization failure or deadlock (40001, 40P01): the transaction can be run again. */
|
|
26
27
|
export const isRetryable = (e: unknown) =>
|
|
27
28
|
e instanceof SerializationError || RETRYABLE.has((e as { errno?: string })?.errno ?? '')
|
|
28
29
|
|
package/src/factory.ts
CHANGED
|
@@ -101,6 +101,7 @@ const clockOrDefault = (): Clock => {
|
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
/** `factory(users, ({ seq, fake }) => ({ … }))`: builds and inserts test rows (guide 09). */
|
|
104
105
|
export const factory = <T extends Table>(table: T, definition: Definition<T>) => new Factory(table, definition)
|
|
105
106
|
|
|
106
107
|
/** Seeders: `export default seeder(async () => { … })`, run by `cslug db:seed`. */
|
package/src/fake.ts
CHANGED
package/src/migrator.ts
CHANGED
|
@@ -47,9 +47,15 @@ export class Migrator {
|
|
|
47
47
|
|
|
48
48
|
constructor(
|
|
49
49
|
private readonly db: Database,
|
|
50
|
-
options: {
|
|
50
|
+
options: {
|
|
51
|
+
paths?: string[]
|
|
52
|
+
env?: string
|
|
53
|
+
log?: Logger
|
|
54
|
+
/** Package migration folders (default: those added with `addMigrations()` in this process). */
|
|
55
|
+
packageMigrations?: readonly string[]
|
|
56
|
+
} = {},
|
|
51
57
|
) {
|
|
52
|
-
this.paths = [FRAMEWORK_MIGRATIONS, ...packageMigrations, ...(options.paths ?? [])]
|
|
58
|
+
this.paths = [FRAMEWORK_MIGRATIONS, ...(options.packageMigrations ?? packageMigrations), ...(options.paths ?? [])]
|
|
53
59
|
this.env = options.env ?? 'development'
|
|
54
60
|
this.log = options.log ?? Logger.null()
|
|
55
61
|
}
|
package/src/sql.ts
CHANGED
|
@@ -80,6 +80,7 @@ type SqlTag = {
|
|
|
80
80
|
empty: SqlFragment<unknown>
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
/** Tagged template: values become bound parameters, `SqlIdentifier`s and fragments are inlined. */
|
|
83
84
|
export const sql: SqlTag = Object.assign(
|
|
84
85
|
<T = Record<string, unknown>>(strings: TemplateStringsArray, ...values: unknown[]) =>
|
|
85
86
|
new SqlFragment<T>([...strings], values),
|
package/src/table.ts
CHANGED
|
@@ -33,6 +33,7 @@ type IndexBuilder<C extends Columns> = {
|
|
|
33
33
|
unique(...keys: (keyof C & string)[]): IndexDef
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
/** A typed table: columns, keys and indexes; `table()` creates one. */
|
|
36
37
|
export class Table<C extends Columns = Columns> implements SqlIdentifier {
|
|
37
38
|
readonly [IDENT]: string
|
|
38
39
|
/** Column references for the sql tag: `${bookings.c.startDate}` → `"bookings"."start_date"`. */
|