@fabrika/db 0.9.0-dev.4 → 0.9.0-dev.41
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/package.json +39 -4
- package/src/admissions.ts +78 -12
- package/src/application-rows.ts +108 -0
- package/src/authority.ts +560 -0
- package/src/builtins.ts +21 -0
- package/src/capability.ts +56 -2
- package/src/codegen-sqlite.ts +194 -0
- package/src/codegen.ts +230 -41
- package/src/condition-nodes.ts +119 -0
- package/src/conditions.ts +683 -0
- package/src/database.ts +227 -61
- package/src/description.ts +37 -8
- package/src/errors.ts +10 -6
- package/src/file-columns.ts +56 -11
- package/src/framework-tables.ts +28 -8
- package/src/framework.ts +15 -5
- package/src/guard.ts +373 -15
- package/src/index.ts +17 -1
- package/src/job-leases.ts +228 -55
- package/src/job-wakes.ts +600 -0
- package/src/json.ts +124 -0
- package/src/links.ts +8 -1
- package/src/list-cursor.ts +82 -0
- package/src/migrations-sqlite.ts +210 -0
- package/src/migrations.ts +73 -0
- package/src/nodes.ts +84 -14
- package/src/orm-types.ts +206 -71
- package/src/orm.ts +425 -170
- package/src/policy.ts +1979 -141
- package/src/postgres.ts +26 -0
- package/src/registry.ts +55 -3
- package/src/request-receipts.ts +107 -0
- package/src/sql.ts +247 -476
- package/src/sqlite-bun.ts +149 -0
- package/src/sqlite-migrate.ts +242 -0
- package/src/sqlite.ts +269 -0
- package/src/store.ts +2292 -239
- package/src/stored-files.ts +188 -52
- package/src/tables.ts +172 -6
- package/src/temporal.ts +122 -9
- package/src/testing-shared.ts +1351 -0
- package/src/testing-sqlite.ts +685 -0
- package/src/testing.ts +310 -494
- package/src/vocabulary-tables.ts +159 -27
- package/types/src/application-rows.d.ts +58 -0
- package/types/src/authority.d.ts +147 -0
- package/types/src/builtins.d.ts +2 -0
- package/types/src/capability.d.ts +55 -2
- package/types/src/codegen-sqlite.d.ts +13 -0
- package/types/src/codegen.d.ts +79 -5
- package/types/src/condition-nodes.d.ts +9 -0
- package/types/src/conditions.d.ts +151 -0
- package/types/src/database.d.ts +81 -20
- package/types/src/description.d.ts +26 -8
- package/types/src/errors.d.ts +9 -6
- package/types/src/file-columns.d.ts +6 -4
- package/types/src/framework.d.ts +12 -5
- package/types/src/guard.d.ts +24 -0
- package/types/src/index.d.ts +5 -4
- package/types/src/job-leases.d.ts +40 -5
- package/types/src/job-wakes.d.ts +124 -0
- package/types/src/json.d.ts +18 -0
- package/types/src/list-cursor.d.ts +13 -0
- package/types/src/migrations-sqlite.d.ts +4 -0
- package/types/src/migrations.d.ts +4 -0
- package/types/src/nodes.d.ts +17 -2
- package/types/src/orm-types.d.ts +163 -71
- package/types/src/orm.d.ts +21 -0
- package/types/src/policy.d.ts +351 -23
- package/types/src/postgres.d.ts +12 -0
- package/types/src/registry.d.ts +40 -3
- package/types/src/request-receipts.d.ts +22 -0
- package/types/src/sql.d.ts +29 -21
- package/types/src/sqlite-bun.d.ts +21 -0
- package/types/src/sqlite-migrate.d.ts +28 -0
- package/types/src/sqlite.d.ts +24 -0
- package/types/src/store.d.ts +143 -34
- package/types/src/stored-files.d.ts +6 -2
- package/types/src/tables.d.ts +18 -2
- package/types/src/temporal.d.ts +24 -0
- package/types/src/testing-shared.d.ts +290 -0
- package/types/src/testing-sqlite.d.ts +37 -0
- package/types/src/testing.d.ts +2 -93
- package/types/src/vocabulary-tables.d.ts +169 -22
package/src/temporal.ts
CHANGED
|
@@ -3,12 +3,16 @@
|
|
|
3
3
|
// the database's own text (`cast(<column> as text)`, `nodes.ts`), never through the `Date` `Bun.SQL` decodes: outside
|
|
4
4
|
// 0001–9999 that one drifts by the process's offset and can land on a neighbouring valid day. Only an aggregate's
|
|
5
5
|
// `min`/`max` and a store's joined child column still arrive as a `Date`. The generated column list names the `date`
|
|
6
|
-
// columns (`TableColumns.dates`); every read of a table's rows passes through `answerDays
|
|
6
|
+
// columns (`TableColumns.dates`); every read of a table's rows passes through `answerRows` (`answerDays` on Postgres). A write's text for a day or a
|
|
7
7
|
// timestamp is checked here before anything is sent: Postgres would also take `now`, `today` or `10/12/2026`, and read
|
|
8
|
-
// a timestamp without an offset in the session's time zone.
|
|
8
|
+
// a timestamp without an offset in the session's time zone. On SQLite (ADR-0065) a timestamp is stored as UTC ISO text
|
|
9
|
+
// and a boolean as 0 or 1: `storedTimestamp` is what a write binds and `answerRows` turns both back into what Postgres's
|
|
10
|
+
// driver answers.
|
|
9
11
|
|
|
10
12
|
import { defineValue, inSet, type NameSet, ownDescriptor, ownKeys } from './builtins'
|
|
13
|
+
import type { SqlDialect } from './database'
|
|
11
14
|
import { QueryRefusedError } from './errors'
|
|
15
|
+
import { sqliteJson } from './json'
|
|
12
16
|
|
|
13
17
|
/** The text Postgres prints for a day in 0001–9999 under its default `DateStyle` (ISO). */
|
|
14
18
|
const DAY_TEXT = /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/
|
|
@@ -61,7 +65,7 @@ export function answerDays<R>(table: string, days: NameSet, rows: R[]): R[] {
|
|
|
61
65
|
}
|
|
62
66
|
|
|
63
67
|
/** An instant in strict ISO 8601: a day, `T`, the time to the minute or finer, and `Z` or an offset. */
|
|
64
|
-
const TIMESTAMP_TEXT = /^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2})(?::([0-9]{2})(?:\.[0-9]{1,6})?)?(?:Z|[+-]([0-9]{2}):([0-9]{2}))$/
|
|
68
|
+
const TIMESTAMP_TEXT = /^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2})(?::([0-9]{2})(?:\.([0-9]{1,6}))?)?(?:Z|([+-])([0-9]{2}):([0-9]{2}))$/
|
|
65
69
|
|
|
66
70
|
/** Whether `year-month-day` is a day of the calendar: no 30 February, no month 13. */
|
|
67
71
|
function isCalendarDay(year: number, month: number, day: number): boolean {
|
|
@@ -78,14 +82,123 @@ export function dayInput(value: string, where: string): string {
|
|
|
78
82
|
return value
|
|
79
83
|
}
|
|
80
84
|
|
|
81
|
-
/**
|
|
82
|
-
|
|
85
|
+
/** The instant of strict ISO 8601 text, in milliseconds since the epoch (digits past the millisecond dropped), or `undefined`. */
|
|
86
|
+
function instantOf(value: string): number | undefined {
|
|
83
87
|
const match = TIMESTAMP_TEXT.exec(value)
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
88
|
+
if (match === null) {
|
|
89
|
+
return undefined
|
|
90
|
+
}
|
|
91
|
+
const [, year, month, day, hour, minute, second = '0', fraction = '', sign = '+', offsetHour = '0', offsetMinute = '0'] = match
|
|
92
|
+
const valid = isCalendarDay(Number(year), Number(month), Number(day)) && Number(hour) < 24 && Number(minute) < 60 && Number(second) < 60
|
|
93
|
+
&& Number(offsetHour) < 16 && Number(offsetMinute) < 60
|
|
87
94
|
if (!valid) {
|
|
88
|
-
|
|
95
|
+
return undefined
|
|
96
|
+
}
|
|
97
|
+
// setUTCFullYear, unlike Date.UTC, keeps the years 0001–0099.
|
|
98
|
+
const date = new Date(0)
|
|
99
|
+
date.setUTCFullYear(Number(year), Number(month) - 1, Number(day))
|
|
100
|
+
date.setUTCHours(Number(hour), Number(minute), Number(second), Number(fraction.padEnd(3, '0').slice(0, 3)))
|
|
101
|
+
const offset = (Number(offsetHour) * 60 + Number(offsetMinute)) * 60_000
|
|
102
|
+
return date.getTime() - (sign === '-' ? -offset : offset)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function timestampRefused(where: string): QueryRefusedError {
|
|
106
|
+
return new QueryRefusedError('invalid_value', `${where} takes a Date or ISO 8601 text with Z or an offset, such as 2026-10-12T08:30:00.000Z`)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** A timestamp a write binds as text: strict ISO 8601 with `Z` or an offset, every field in range. */
|
|
110
|
+
export function timestampInput(value: string, where: string): string {
|
|
111
|
+
if (instantOf(value) === undefined) {
|
|
112
|
+
throw timestampRefused(where)
|
|
89
113
|
}
|
|
90
114
|
return value
|
|
91
115
|
}
|
|
116
|
+
|
|
117
|
+
/** A timestamp as SQLite stores it: UTC `Date#toISOString()` text, whose text order is time order. */
|
|
118
|
+
const STORED_TIMESTAMP = /^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}Z$/
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* What a write or a comparison binds for a timestamp on SQLite: the instant of a `Date`, or of the text `timestampInput`
|
|
122
|
+
* admits, as stored text. Digits past the millisecond are dropped; an instant outside the years 0000–9999 UTC is
|
|
123
|
+
* refused, since its text would not sort.
|
|
124
|
+
*/
|
|
125
|
+
export function storedTimestamp(value: string | Date, where: string): string {
|
|
126
|
+
const time = typeof value === 'string' ? instantOf(value) : value.getTime()
|
|
127
|
+
if (time === undefined) {
|
|
128
|
+
throw timestampRefused(where)
|
|
129
|
+
}
|
|
130
|
+
const text = Number.isNaN(time) ? '' : new Date(time).toISOString()
|
|
131
|
+
if (!STORED_TIMESTAMP.test(text)) {
|
|
132
|
+
throw new QueryRefusedError(
|
|
133
|
+
'invalid_value',
|
|
134
|
+
`${where} holds an instant outside the years 0000-9999 UTC, which SQLite cannot store as sortable text`,
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
return text
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** The columns of a read's rows the layer answers in a form of its own, by the name each arrives under. */
|
|
141
|
+
export interface RowColumns {
|
|
142
|
+
/** `date` columns: `YYYY-MM-DD` text. */
|
|
143
|
+
readonly days: NameSet
|
|
144
|
+
/** Timestamps: a `Date` (SQLite answers its stored text). */
|
|
145
|
+
readonly timestamps: NameSet
|
|
146
|
+
/** Booleans: `true` or `false` (SQLite answers 1 or 0). */
|
|
147
|
+
readonly booleans: NameSet
|
|
148
|
+
/** JSON columns: the parsed value (SQLite answers its text). */
|
|
149
|
+
readonly json: NameSet
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function unexpected(where: string, expected: string): QueryRefusedError {
|
|
153
|
+
return new QueryRefusedError('unexpected_row', `${where} came back as something other than ${expected}`)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** One SQLite value of `key` as Postgres's driver answers it; a value the migrations' types never store is refused. */
|
|
157
|
+
function sqliteValue(columns: RowColumns, where: string, key: string, value: unknown): unknown {
|
|
158
|
+
if (value === null) {
|
|
159
|
+
return value
|
|
160
|
+
}
|
|
161
|
+
if (inSet(columns.timestamps, key)) {
|
|
162
|
+
if (typeof value !== 'string' || !STORED_TIMESTAMP.test(value)) {
|
|
163
|
+
throw unexpected(where, 'UTC ISO text (YYYY-MM-DDTHH:MM:SS.sssZ), the form a timestamp is stored in on SQLite')
|
|
164
|
+
}
|
|
165
|
+
return new Date(value)
|
|
166
|
+
}
|
|
167
|
+
if (inSet(columns.booleans, key)) {
|
|
168
|
+
if (value !== 0 && value !== 1) {
|
|
169
|
+
throw unexpected(where, '0 or 1, the form a boolean is stored in on SQLite')
|
|
170
|
+
}
|
|
171
|
+
return value === 1
|
|
172
|
+
}
|
|
173
|
+
if (inSet(columns.json, key)) {
|
|
174
|
+
return sqliteJson(value, where)
|
|
175
|
+
}
|
|
176
|
+
return typeof value === 'string' && inSet(columns.days, key) ? dayText(value, where) : value
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Every row of a read as the layer answers it, in place: each day as `YYYY-MM-DD` text, and on SQLite each timestamp as
|
|
181
|
+
* a `Date`, each boolean as `true` or `false` and each JSON column as its parsed value, so a handler reads the same
|
|
182
|
+
* values on both dialects.
|
|
183
|
+
*/
|
|
184
|
+
export function answerRows<R>(dialect: SqlDialect, table: string, columns: RowColumns, rows: R[]): R[] {
|
|
185
|
+
if (dialect === 'postgres') {
|
|
186
|
+
return answerDays(table, columns.days, rows)
|
|
187
|
+
}
|
|
188
|
+
for (let index = 0; index < rows.length; index++) {
|
|
189
|
+
const row = rows[index]
|
|
190
|
+
if (typeof row !== 'object' || row === null) {
|
|
191
|
+
continue
|
|
192
|
+
}
|
|
193
|
+
const keys = ownKeys(row)
|
|
194
|
+
for (let position = 0; position < keys.length; position++) {
|
|
195
|
+
const key = keys[position] ?? ''
|
|
196
|
+
const value: unknown = ownDescriptor(row, key)?.value
|
|
197
|
+
const answered = sqliteValue(columns, `${table}.${key}`, key, value)
|
|
198
|
+
if (answered !== value) {
|
|
199
|
+
defineValue(row, key, answered)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return rows
|
|
204
|
+
}
|