@crombolo/data 0.37.0 → 0.39.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/package.json +3 -2
- package/src/audit.ts +164 -134
- package/src/change-report.ts +66 -43
- package/src/cluster.ts +119 -59
- package/src/content-query.ts +205 -200
- package/src/{bundle-tables → custom-tables}/builder.ts +104 -12
- package/src/custom-tables/chain.ts +150 -0
- package/src/custom-tables/ddl.ts +159 -0
- package/src/custom-tables/handle.ts +141 -0
- package/src/{bundle-tables → custom-tables}/index.ts +3 -2
- package/src/custom-tables/names.ts +94 -0
- package/src/custom-tables/owners.ts +299 -0
- package/src/{bundle-tables → custom-tables}/schema.ts +263 -90
- package/src/custom-tables/values.ts +181 -0
- package/src/database.ts +40 -5
- package/src/dialect.ts +43 -16
- package/src/index.ts +9 -1
- package/src/migrations.ts +130 -88
- package/src/query.ts +158 -0
- package/src/relations.ts +49 -28
- package/src/repositories/approvals.ts +130 -89
- package/src/repositories/bundle-grants.ts +105 -0
- package/src/repositories/bundles.ts +85 -76
- package/src/repositories/content-applied.ts +47 -28
- package/src/repositories/content-events.ts +114 -67
- package/src/repositories/content-notifications.ts +102 -74
- package/src/repositories/content-types.ts +449 -320
- package/src/repositories/data-types.ts +105 -56
- package/src/repositories/delivery-member-tokens.ts +37 -28
- package/src/repositories/dictionary.ts +77 -58
- package/src/repositories/documents.ts +956 -580
- package/src/repositories/domains.ts +28 -16
- package/src/repositories/dynamic-roots.ts +32 -17
- package/src/repositories/external-logins.ts +38 -24
- package/src/repositories/folders.ts +7 -4
- package/src/repositories/form-entries.ts +120 -103
- package/src/repositories/form-workflows.ts +87 -62
- package/src/repositories/jobs.ts +296 -0
- package/src/repositories/languages.ts +51 -38
- package/src/repositories/log-searches.ts +7 -5
- package/src/repositories/media-access.ts +173 -123
- package/src/repositories/media-listing.ts +155 -92
- package/src/repositories/members.ts +189 -162
- package/src/repositories/nodes.ts +227 -181
- package/src/repositories/notifications.ts +22 -17
- package/src/repositories/public-access.ts +77 -61
- package/src/repositories/redirects.ts +113 -70
- package/src/repositories/references.ts +55 -44
- package/src/repositories/remote-jobs.ts +45 -35
- package/src/repositories/tags.ts +11 -10
- package/src/repositories/templates.ts +24 -14
- package/src/repositories/transfer-answers.ts +58 -50
- package/src/repositories/transfer-runs.ts +93 -75
- package/src/repositories/two-factor.ts +74 -55
- package/src/repositories/user-data.ts +54 -36
- package/src/repositories/users.ts +345 -300
- package/src/repositories/webhooks.ts +196 -137
- package/src/schema/bundle-grants.ts +32 -0
- package/src/schema/bundles-disabled.ts +29 -0
- package/src/schema/jobs.ts +56 -0
- package/src/schema/plan.ts +6 -0
- package/src/schema-ops.ts +15 -12
- package/src/schema-state.ts +53 -38
- package/src/search.ts +175 -129
- package/src/setup.ts +17 -13
- package/src/site-search.ts +134 -83
- package/src/table-types.ts +152 -0
- package/src/tables.gen.ts +861 -0
- package/src/tree-invariants.ts +21 -12
- package/src/url-options.ts +17 -13
- package/src/workflow-store.ts +17 -13
- package/src/bundle-tables/capability.ts +0 -377
- package/src/bundle-tables/chain.ts +0 -105
- package/src/bundle-tables/ddl.ts +0 -124
- package/src/bundle-tables/guard.ts +0 -116
- package/src/bundle-tables/names.ts +0 -58
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Values in custom tables read and write the same on both engines (story 189).
|
|
3
|
+
* The drivers disagree: SQLite hands back a boolean as 0 or 1, a timestamp as
|
|
4
|
+
* text and a decimal as a number, and Bun's SQLite driver writes a `Date` as
|
|
5
|
+
* null. So a write's values are made portable as they are bound, and a read's
|
|
6
|
+
* columns are converted by the declared type of the column each one selects.
|
|
7
|
+
*
|
|
8
|
+
* Only a column the query names, or `*`, is converted: an expression arrives as
|
|
9
|
+
* the engine computes it.
|
|
10
|
+
*/
|
|
11
|
+
import type {
|
|
12
|
+
KyselyPlugin,
|
|
13
|
+
OperationNode,
|
|
14
|
+
PluginTransformQueryArgs,
|
|
15
|
+
PluginTransformResultArgs,
|
|
16
|
+
QueryResult,
|
|
17
|
+
RootOperationNode,
|
|
18
|
+
UnknownRow,
|
|
19
|
+
} from 'kysely'
|
|
20
|
+
import { DbDate, type Dialect, fromDbBool } from '../dialect.ts'
|
|
21
|
+
import type { ColumnSpec } from './builder.ts'
|
|
22
|
+
import type { ResolvedSchema } from './schema.ts'
|
|
23
|
+
|
|
24
|
+
/** A value as either engine binds it: dates as ISO text, booleans as the dialect writes them, JSON as text. */
|
|
25
|
+
export function portableParam(dialect: Dialect, value: unknown): unknown {
|
|
26
|
+
if (value instanceof Date) return DbDate.toDb(value)
|
|
27
|
+
if (typeof value === 'boolean') return dialect.boolValue(value)
|
|
28
|
+
if (value instanceof Uint8Array || value instanceof ArrayBuffer) return value
|
|
29
|
+
if (Array.isArray(value) || (value !== null && typeof value === 'object'))
|
|
30
|
+
return JSON.stringify(value)
|
|
31
|
+
return value
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A stored value as its declared type reads it. */
|
|
35
|
+
export function readValue(spec: ColumnSpec, value: unknown): unknown {
|
|
36
|
+
if (value === null || value === undefined) return null
|
|
37
|
+
switch (spec.type) {
|
|
38
|
+
case 'uuid':
|
|
39
|
+
return String(value).toLowerCase()
|
|
40
|
+
case 'varchar':
|
|
41
|
+
case 'text':
|
|
42
|
+
return String(value)
|
|
43
|
+
case 'integer':
|
|
44
|
+
case 'bigint':
|
|
45
|
+
return Number(value)
|
|
46
|
+
case 'boolean':
|
|
47
|
+
return fromDbBool(value)
|
|
48
|
+
case 'timestamp':
|
|
49
|
+
return DbDate.fromDb(value) ?? null
|
|
50
|
+
case 'json':
|
|
51
|
+
return typeof value === 'string' ? JSON.parse(value) : value
|
|
52
|
+
case 'decimal':
|
|
53
|
+
return typeof value === 'number' ? value.toFixed(spec.scale ?? 0) : String(value)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type Node = OperationNode & Record<string, unknown>
|
|
58
|
+
|
|
59
|
+
const identifier = (node: unknown): string | undefined =>
|
|
60
|
+
(node as { name?: string } | undefined)?.name
|
|
61
|
+
|
|
62
|
+
/** A table node's name: `TableNode { table: { identifier } }`. */
|
|
63
|
+
function tableName(node: unknown): string | undefined {
|
|
64
|
+
const table = node as Node | undefined
|
|
65
|
+
if (table?.kind !== 'TableNode') return undefined
|
|
66
|
+
return identifier((table.table as Node | undefined)?.identifier)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The tables a query reads from, by the name or alias each is referred to by. */
|
|
70
|
+
function scopeOf(node: Node): Map<string, string> {
|
|
71
|
+
const scope = new Map<string, string>()
|
|
72
|
+
const add = (item: unknown) => {
|
|
73
|
+
const n = item as Node | undefined
|
|
74
|
+
if (!n) return
|
|
75
|
+
if (n.kind === 'AliasNode') {
|
|
76
|
+
const table = tableName(n.node)
|
|
77
|
+
const alias = identifier(n.alias)
|
|
78
|
+
if (table && alias) scope.set(alias, table)
|
|
79
|
+
return
|
|
80
|
+
}
|
|
81
|
+
const table = tableName(n)
|
|
82
|
+
if (table) scope.set(table, table)
|
|
83
|
+
}
|
|
84
|
+
for (const from of ((node.from as Node | undefined)?.froms as unknown[]) ?? []) add(from)
|
|
85
|
+
for (const join of (node.joins as Node[] | undefined) ?? []) add(join.table)
|
|
86
|
+
add(node.into)
|
|
87
|
+
add(node.table)
|
|
88
|
+
return scope
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Each output column of a query that is a column of one of the owner's
|
|
93
|
+
* tables, with its declaration; undefined when there is nothing to convert.
|
|
94
|
+
*/
|
|
95
|
+
export function outputsOf(
|
|
96
|
+
node: RootOperationNode,
|
|
97
|
+
schema: ResolvedSchema,
|
|
98
|
+
): Map<string, ColumnSpec> | undefined {
|
|
99
|
+
const root = node as unknown as Node
|
|
100
|
+
const selections =
|
|
101
|
+
root.kind === 'SelectQueryNode'
|
|
102
|
+
? (root.selections as Node[] | undefined)
|
|
103
|
+
: ((root.returning as Node | undefined)?.selections as Node[] | undefined)
|
|
104
|
+
if (!selections?.length) return undefined
|
|
105
|
+
const scope = scopeOf(root)
|
|
106
|
+
const outputs = new Map<string, ColumnSpec>()
|
|
107
|
+
const columnsOf = (table: string | undefined) => schema.tables.get(table ?? '')?.columns
|
|
108
|
+
const all = (table?: string) => {
|
|
109
|
+
const tables = table ? [table] : [...scope.values()]
|
|
110
|
+
for (const name of tables)
|
|
111
|
+
for (const [column, resolved] of columnsOf(name) ?? []) outputs.set(column, resolved.spec)
|
|
112
|
+
}
|
|
113
|
+
/** The declaration of a referenced column, looking it up in the one table that has it. */
|
|
114
|
+
const specOf = (reference: Node): { column: string; spec?: ColumnSpec } | undefined => {
|
|
115
|
+
const column = reference.column as Node
|
|
116
|
+
const qualifier = tableName(reference.table)
|
|
117
|
+
if (column?.kind === 'SelectAllNode') {
|
|
118
|
+
all(qualifier ? (scope.get(qualifier) ?? qualifier) : undefined)
|
|
119
|
+
return undefined
|
|
120
|
+
}
|
|
121
|
+
const name = identifier((column as Node | undefined)?.column)
|
|
122
|
+
if (!name) return undefined
|
|
123
|
+
if (qualifier) return { column: name, spec: columnsOf(scope.get(qualifier))?.get(name)?.spec }
|
|
124
|
+
const owners = [...new Set(scope.values())].filter((table) => columnsOf(table)?.has(name))
|
|
125
|
+
return {
|
|
126
|
+
column: name,
|
|
127
|
+
spec: owners.length === 1 ? columnsOf(owners[0])?.get(name)?.spec : undefined,
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
for (const selection of selections) {
|
|
131
|
+
const item = (selection.selection ?? selection) as Node
|
|
132
|
+
if (item.kind === 'SelectAllNode') all()
|
|
133
|
+
else if (item.kind === 'ReferenceNode') {
|
|
134
|
+
const found = specOf(item)
|
|
135
|
+
if (found?.spec) outputs.set(found.column, found.spec)
|
|
136
|
+
} else if (item.kind === 'ColumnNode') {
|
|
137
|
+
const name = identifier(item.column)
|
|
138
|
+
const owners = [...new Set(scope.values())].filter((table) =>
|
|
139
|
+
columnsOf(table)?.has(name ?? ''),
|
|
140
|
+
)
|
|
141
|
+
const spec = owners.length === 1 ? columnsOf(owners[0])?.get(name ?? '')?.spec : undefined
|
|
142
|
+
if (name && spec) outputs.set(name, spec)
|
|
143
|
+
} else if (item.kind === 'AliasNode') {
|
|
144
|
+
const inner = item.node as Node
|
|
145
|
+
const alias = identifier(item.alias)
|
|
146
|
+
if (alias && inner?.kind === 'ReferenceNode') {
|
|
147
|
+
const found = specOf(inner)
|
|
148
|
+
if (found?.spec) outputs.set(alias, found.spec)
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return outputs.size > 0 ? outputs : undefined
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** The plugin that converts what a query reads by what its columns are declared as. */
|
|
156
|
+
export class PortableValues implements KyselyPlugin {
|
|
157
|
+
readonly #schema: ResolvedSchema
|
|
158
|
+
readonly #outputs = new WeakMap<object, Map<string, ColumnSpec>>()
|
|
159
|
+
|
|
160
|
+
constructor(schema: ResolvedSchema) {
|
|
161
|
+
this.#schema = schema
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
transformQuery(args: PluginTransformQueryArgs): RootOperationNode {
|
|
165
|
+
const outputs = outputsOf(args.node, this.#schema)
|
|
166
|
+
if (outputs) this.#outputs.set(args.queryId, outputs)
|
|
167
|
+
return args.node
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async transformResult(args: PluginTransformResultArgs): Promise<QueryResult<UnknownRow>> {
|
|
171
|
+
const outputs = this.#outputs.get(args.queryId)
|
|
172
|
+
if (!outputs) return args.result
|
|
173
|
+
const rows = args.result.rows.map((row) => {
|
|
174
|
+
const out: UnknownRow = { ...row }
|
|
175
|
+
for (const [column, spec] of outputs)
|
|
176
|
+
if (Object.hasOwn(out, column)) out[column] = readValue(spec, out[column])
|
|
177
|
+
return out
|
|
178
|
+
})
|
|
179
|
+
return { ...args.result, rows }
|
|
180
|
+
}
|
|
181
|
+
}
|
package/src/database.ts
CHANGED
|
@@ -5,10 +5,13 @@ import { existsSync } from 'node:fs'
|
|
|
5
5
|
import { ambient, ambientFor } from './ambient.ts'
|
|
6
6
|
import { type Dialect, type DialectName, dialectFor } from './dialect.ts'
|
|
7
7
|
import { createLockManager, DEFAULT_LOCK_TIMEOUT_MS, type LockManager } from './locks.ts'
|
|
8
|
+
import { type Query, queryFor } from './query.ts'
|
|
8
9
|
|
|
9
10
|
export interface Db {
|
|
10
11
|
readonly dialect: Dialect
|
|
11
12
|
readonly locks: LockManager
|
|
13
|
+
/** The query builder over this handle (story 221); inside a transaction, the transaction's. */
|
|
14
|
+
readonly q: Query
|
|
12
15
|
query<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<T[]>
|
|
13
16
|
exec(sql: string, params?: unknown[]): Promise<void>
|
|
14
17
|
transaction<T>(fn: (tx: Db) => Promise<T>): Promise<T>
|
|
@@ -19,6 +22,12 @@ export interface Db {
|
|
|
19
22
|
*/
|
|
20
23
|
readSnapshot<T>(fn: (reader: Db) => Promise<T>): Promise<T>
|
|
21
24
|
close(): Promise<void>
|
|
25
|
+
/**
|
|
26
|
+
* Postgres only: a handle on the same pool whose every transaction runs as
|
|
27
|
+
* `role` with `search_path` set to `schema`, a statement on its own being a
|
|
28
|
+
* transaction of its own (story 189). Closing it leaves the pool open.
|
|
29
|
+
*/
|
|
30
|
+
confine?(role: string, schema: string): Db
|
|
22
31
|
/** Postgres only: calls `onNotify` for each `NOTIFY` on `channel`; resolves to the way to stop. */
|
|
23
32
|
listen?(channel: string, onNotify: (payload: string) => void): Promise<() => Promise<void>>
|
|
24
33
|
}
|
|
@@ -56,6 +65,9 @@ export function resolveLockTimeoutMs(options: ConnectOptions = {}): number {
|
|
|
56
65
|
class SqliteDb implements Db {
|
|
57
66
|
readonly dialect = dialectFor('sqlite')
|
|
58
67
|
readonly locks: LockManager
|
|
68
|
+
get q(): Query {
|
|
69
|
+
return queryFor(this)
|
|
70
|
+
}
|
|
59
71
|
#db: BunSqlite
|
|
60
72
|
#inTransaction: boolean
|
|
61
73
|
#gate: { tail: Promise<void> }
|
|
@@ -165,10 +177,17 @@ interface SqlTag {
|
|
|
165
177
|
class PostgresDb implements Db {
|
|
166
178
|
readonly dialect = dialectFor('postgres')
|
|
167
179
|
readonly locks: LockManager
|
|
180
|
+
get q(): Query {
|
|
181
|
+
return queryFor(this)
|
|
182
|
+
}
|
|
168
183
|
#sql: SqlTag
|
|
169
184
|
#inTransaction: boolean
|
|
170
185
|
|
|
171
186
|
#readOnly: boolean
|
|
187
|
+
/** What every transaction begins with; a confined handle's role and search path. */
|
|
188
|
+
#prelude: readonly string[]
|
|
189
|
+
/** A confined handle borrows the pool, so closing it closes nothing. */
|
|
190
|
+
#borrowed: boolean
|
|
172
191
|
|
|
173
192
|
constructor(
|
|
174
193
|
sql: SqlTag,
|
|
@@ -176,25 +195,39 @@ class PostgresDb implements Db {
|
|
|
176
195
|
inTransaction = false,
|
|
177
196
|
lockTimeoutMs?: number,
|
|
178
197
|
readOnly = false,
|
|
198
|
+
prelude: readonly string[] = [],
|
|
179
199
|
) {
|
|
180
200
|
this.#sql = sql
|
|
181
201
|
this.locks = locks ?? createLockManager(this, { timeoutMs: lockTimeoutMs })
|
|
182
202
|
this.#inTransaction = inTransaction
|
|
183
203
|
this.#readOnly = readOnly
|
|
204
|
+
this.#prelude = prelude
|
|
205
|
+
this.#borrowed = prelude.length > 0
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** A statement outside a transaction that needs one: read-only, or confined. */
|
|
209
|
+
get #wraps(): boolean {
|
|
210
|
+
return !this.#inTransaction && (this.#readOnly || this.#prelude.length > 0)
|
|
184
211
|
}
|
|
185
212
|
|
|
186
213
|
async query<T = Record<string, unknown>>(sql: string, params: unknown[] = []): Promise<T[]> {
|
|
187
|
-
if (this.#
|
|
188
|
-
return this.transaction((tx) => tx.query<T>(sql, params))
|
|
214
|
+
if (this.#wraps) return this.transaction((tx) => tx.query<T>(sql, params))
|
|
189
215
|
return (await this.#sql.unsafe(this.dialect.render(sql), params)) as T[]
|
|
190
216
|
}
|
|
191
217
|
|
|
192
218
|
async exec(sql: string, params: unknown[] = []): Promise<void> {
|
|
193
|
-
if (this.#
|
|
194
|
-
return this.transaction((tx) => tx.exec(sql, params))
|
|
219
|
+
if (this.#wraps) return this.transaction((tx) => tx.exec(sql, params))
|
|
195
220
|
await this.#sql.unsafe(this.dialect.render(sql), params)
|
|
196
221
|
}
|
|
197
222
|
|
|
223
|
+
confine(role: string, schema: string): Db {
|
|
224
|
+
const q = (id: string) => this.dialect.quote(id)
|
|
225
|
+
return new PostgresDb(this.#sql, this.locks, false, undefined, this.#readOnly, [
|
|
226
|
+
`SET LOCAL ROLE ${q(role)}`,
|
|
227
|
+
`SET LOCAL search_path TO ${q(schema)}`,
|
|
228
|
+
])
|
|
229
|
+
}
|
|
230
|
+
|
|
198
231
|
/**
|
|
199
232
|
* Nested calls join the enclosing transaction, as they do on SQLite. The
|
|
200
233
|
* chain is marked as inside one, so a deadlock is not retried into a
|
|
@@ -204,6 +237,7 @@ class PostgresDb implements Db {
|
|
|
204
237
|
if (this.#inTransaction) return fn(this)
|
|
205
238
|
return this.#sql.begin(async (sql) => {
|
|
206
239
|
if (this.#readOnly) await sql.unsafe('SET TRANSACTION READ ONLY')
|
|
240
|
+
for (const statement of this.#prelude) await sql.unsafe(statement)
|
|
207
241
|
const tx = new PostgresDb(sql, this.locks, true)
|
|
208
242
|
const store = { owner: this, tx: tx as unknown }
|
|
209
243
|
try {
|
|
@@ -219,12 +253,13 @@ class PostgresDb implements Db {
|
|
|
219
253
|
if (this.#inTransaction) return fn(this)
|
|
220
254
|
return this.#sql.begin(async (sql) => {
|
|
221
255
|
await sql.unsafe('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ, READ ONLY')
|
|
256
|
+
for (const statement of this.#prelude) await sql.unsafe(statement)
|
|
222
257
|
return fn(new PostgresDb(sql, this.locks, true))
|
|
223
258
|
})
|
|
224
259
|
}
|
|
225
260
|
|
|
226
261
|
async close(): Promise<void> {
|
|
227
|
-
await this.#sql.close()
|
|
262
|
+
if (!this.#borrowed) await this.#sql.close()
|
|
228
263
|
}
|
|
229
264
|
|
|
230
265
|
async listen(channel: string, onNotify: (payload: string) => void): Promise<() => Promise<void>> {
|
package/src/dialect.ts
CHANGED
|
@@ -3,8 +3,20 @@
|
|
|
3
3
|
* `?` placeholders and rendered per dialect, so no service ever needs to know
|
|
4
4
|
* which database it is talking to.
|
|
5
5
|
*/
|
|
6
|
+
import { type RawBuilder, sql } from 'kysely'
|
|
7
|
+
|
|
6
8
|
export type DialectName = 'sqlite' | 'postgres'
|
|
7
9
|
|
|
10
|
+
export interface FullTextHit {
|
|
11
|
+
node_id: number
|
|
12
|
+
score: number
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface SiteSearchHitRow {
|
|
16
|
+
row_id: number
|
|
17
|
+
score: number
|
|
18
|
+
}
|
|
19
|
+
|
|
8
20
|
export interface ColumnTypes {
|
|
9
21
|
identity: string
|
|
10
22
|
integer: string
|
|
@@ -32,7 +44,7 @@ export interface Dialect {
|
|
|
32
44
|
/** Rewrites `?` placeholders into the dialect's own form. */
|
|
33
45
|
render(sql: string): string
|
|
34
46
|
now(): string
|
|
35
|
-
boolValue(value: boolean):
|
|
47
|
+
boolValue(value: boolean): boolean | number
|
|
36
48
|
/** Appended to a SELECT that must block a concurrent state change; empty where the engine is single-writer. */
|
|
37
49
|
readonly forShare: string
|
|
38
50
|
readonly forUpdate: string
|
|
@@ -41,15 +53,16 @@ export interface Dialect {
|
|
|
41
53
|
/**
|
|
42
54
|
* The full-text match over `search_document`: a subquery of `node_id` and
|
|
43
55
|
* `score`, higher being better, for each item holding every term as a word
|
|
44
|
-
* prefix
|
|
56
|
+
* prefix, for `query` from `fullTextQuery(terms)`. Parenthesised, to alias
|
|
57
|
+
* with `.as(…)` in a FROM or JOIN. docs/search.md.
|
|
45
58
|
*/
|
|
46
|
-
|
|
59
|
+
fullTextMatches(query: string): RawBuilder<FullTextHit>
|
|
47
60
|
/**
|
|
48
61
|
* Site search's matching rows of `site_search_document`, as `row_id` and
|
|
49
|
-
* `score`, weighted name 10, high 5, normal 2, low 1 on both engines
|
|
50
|
-
*
|
|
62
|
+
* `score`, weighted name 10, high 5, normal 2, low 1 on both engines, for
|
|
63
|
+
* `query` from `fullTextQuery(terms)`. Parenthesised, like `fullTextMatches`.
|
|
51
64
|
*/
|
|
52
|
-
|
|
65
|
+
siteSearchMatches(query: string): RawBuilder<SiteSearchHitRow>
|
|
53
66
|
/** Terms are already folded words: letters and digits only. */
|
|
54
67
|
fullTextQuery(terms: readonly string[]): string
|
|
55
68
|
/**
|
|
@@ -57,6 +70,10 @@ export interface Dialect {
|
|
|
57
70
|
* undefined where the engine does that itself.
|
|
58
71
|
*/
|
|
59
72
|
syncIdentity(table: string): string | undefined
|
|
73
|
+
/** The names of a table's columns, as rows of `name`; its one parameter is the table. */
|
|
74
|
+
readonly columnsOf: string
|
|
75
|
+
/** The names of the tables in the database, as rows of `name`. */
|
|
76
|
+
readonly tableNames: string
|
|
60
77
|
}
|
|
61
78
|
|
|
62
79
|
const RESERVED_RENDER = /\?/g
|
|
@@ -91,14 +108,17 @@ export const sqliteDialect: Dialect = {
|
|
|
91
108
|
// Name hits weigh ten times body hits; bm25 is lower-is-better, so negated.
|
|
92
109
|
// bm25 cannot be called under an aggregate, and `LIMIT -1` (no limit) keeps
|
|
93
110
|
// SQLite from flattening the inner query into the outer one, which would.
|
|
94
|
-
|
|
111
|
+
fullTextMatches: (query) => sql<FullTextHit>`(SELECT sd.node_id AS node_id, MAX(f.score) AS score
|
|
95
112
|
FROM (SELECT rowid AS id, -bm25(search_document_fts, 10.0, 1.0) AS score
|
|
96
|
-
FROM search_document_fts WHERE search_document_fts MATCH
|
|
97
|
-
JOIN search_document sd ON sd.id = f.id GROUP BY sd.node_id`,
|
|
113
|
+
FROM search_document_fts WHERE search_document_fts MATCH ${query} LIMIT -1) f
|
|
114
|
+
JOIN search_document sd ON sd.id = f.id GROUP BY sd.node_id)`,
|
|
98
115
|
fullTextQuery: (terms) => terms.map((term) => `"${term}"*`).join(' AND '),
|
|
99
|
-
|
|
100
|
-
|
|
116
|
+
siteSearchMatches: (query) => sql<SiteSearchHitRow>`(SELECT rowid AS row_id,
|
|
117
|
+
-bm25(site_search_document_fts, 10.0, 5.0, 2.0, 1.0) AS score
|
|
118
|
+
FROM site_search_document_fts WHERE site_search_document_fts MATCH ${query} LIMIT -1)`,
|
|
101
119
|
syncIdentity: () => undefined,
|
|
120
|
+
columnsOf: 'SELECT name FROM pragma_table_info(?)',
|
|
121
|
+
tableNames: "SELECT name FROM sqlite_master WHERE type = 'table'",
|
|
102
122
|
}
|
|
103
123
|
|
|
104
124
|
export const postgresDialect: Dialect = {
|
|
@@ -130,13 +150,20 @@ export const postgresDialect: Dialect = {
|
|
|
130
150
|
forShare: 'FOR SHARE',
|
|
131
151
|
forUpdate: 'FOR UPDATE',
|
|
132
152
|
forUpdateSkipLocked: 'FOR UPDATE SKIP LOCKED',
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
153
|
+
fullTextMatches: (
|
|
154
|
+
query,
|
|
155
|
+
) => sql<FullTextHit>`(SELECT sd.node_id AS node_id, MAX(ts_rank(sd.terms, q)) AS score
|
|
156
|
+
FROM search_document sd, to_tsquery('simple', ${query}) q
|
|
157
|
+
WHERE sd.terms @@ q GROUP BY sd.node_id)`,
|
|
136
158
|
fullTextQuery: (terms) => terms.map((term) => `${term}:*`).join(' & '),
|
|
137
159
|
// ts_rank's weights run D, C, B, A: the same 1, 2, 5, 10 as SQLite's bm25 weights.
|
|
138
|
-
|
|
139
|
-
|
|
160
|
+
siteSearchMatches: (query) => sql<SiteSearchHitRow>`(SELECT s.id AS row_id,
|
|
161
|
+
ts_rank('{0.1, 0.2, 0.5, 1.0}', s.terms, q) AS score
|
|
162
|
+
FROM site_search_document s, to_tsquery('simple', ${query}) q WHERE s.terms @@ q)`,
|
|
163
|
+
columnsOf:
|
|
164
|
+
'SELECT column_name AS name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = ?',
|
|
165
|
+
tableNames:
|
|
166
|
+
"SELECT table_name AS name FROM information_schema.tables WHERE table_schema = current_schema() AND table_type = 'BASE TABLE'",
|
|
140
167
|
syncIdentity: (table) =>
|
|
141
168
|
`SELECT setval(pg_get_serial_sequence('${table}', 'id'), (SELECT COALESCE(MAX(id), 1) FROM ${table}))`,
|
|
142
169
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export { insideTransaction } from './ambient.ts'
|
|
2
2
|
export * from './audit.ts'
|
|
3
|
-
export * from './bundle-tables/index.ts'
|
|
4
3
|
export * from './busy.ts'
|
|
5
4
|
export * from './change-report.ts'
|
|
6
5
|
export * from './cluster.ts'
|
|
7
6
|
export * from './content-query.ts'
|
|
7
|
+
export * from './custom-tables/index.ts'
|
|
8
8
|
export * from './database.ts'
|
|
9
9
|
export * from './dialect.ts'
|
|
10
10
|
export * from './locks.ts'
|
|
@@ -12,8 +12,10 @@ export * from './maintenance.ts'
|
|
|
12
12
|
export * from './migrations.ts'
|
|
13
13
|
export * from './node-ref.ts'
|
|
14
14
|
export * from './pending.ts'
|
|
15
|
+
export * from './query.ts'
|
|
15
16
|
export * from './relations.ts'
|
|
16
17
|
export * from './repositories/approvals.ts'
|
|
18
|
+
export * from './repositories/bundle-grants.ts'
|
|
17
19
|
export * from './repositories/bundles.ts'
|
|
18
20
|
export * from './repositories/content-applied.ts'
|
|
19
21
|
export * from './repositories/content-events.ts'
|
|
@@ -29,6 +31,7 @@ export * from './repositories/external-logins.ts'
|
|
|
29
31
|
export * from './repositories/folders.ts'
|
|
30
32
|
export * from './repositories/form-entries.ts'
|
|
31
33
|
export * from './repositories/form-workflows.ts'
|
|
34
|
+
export * from './repositories/jobs.ts'
|
|
32
35
|
export * from './repositories/languages.ts'
|
|
33
36
|
export * from './repositories/log-searches.ts'
|
|
34
37
|
export * from './repositories/media-access.ts'
|
|
@@ -55,7 +58,9 @@ export * from './schema/audit-log.ts'
|
|
|
55
58
|
export * from './schema/built-in-content-types.ts'
|
|
56
59
|
export * from './schema/built-in-media-types.ts'
|
|
57
60
|
export * from './schema/built-in-member-types.ts'
|
|
61
|
+
export * from './schema/bundle-grants.ts'
|
|
58
62
|
export * from './schema/bundle-package.ts'
|
|
63
|
+
export * from './schema/bundles-disabled.ts'
|
|
59
64
|
export * from './schema/bundles-rename.ts'
|
|
60
65
|
export * from './schema/change-report.ts'
|
|
61
66
|
export * from './schema/client-id-prefix.ts'
|
|
@@ -73,6 +78,7 @@ export * from './schema/elements.ts'
|
|
|
73
78
|
export * from './schema/form-permission-prefix.ts'
|
|
74
79
|
export * from './schema/group-permissions.ts'
|
|
75
80
|
export * from './schema/identity.ts'
|
|
81
|
+
export * from './schema/jobs.ts'
|
|
76
82
|
export * from './schema/log-viewer.ts'
|
|
77
83
|
export * from './schema/media-access.ts'
|
|
78
84
|
export * from './schema/media-cache.ts'
|
|
@@ -106,6 +112,8 @@ export * from './search.ts'
|
|
|
106
112
|
export * from './search-text.ts'
|
|
107
113
|
export * from './setup.ts'
|
|
108
114
|
export * from './site-search.ts'
|
|
115
|
+
export * from './table-types.ts'
|
|
116
|
+
export type { CoreTables } from './tables.gen.ts'
|
|
109
117
|
export * from './tree-invariants.ts'
|
|
110
118
|
export * from './url-options.ts'
|
|
111
119
|
export * from './workflow-store.ts'
|