@crombolo/data 0.52.2 → 0.54.1
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 +2 -2
- package/src/cluster.ts +21 -29
- package/src/dictionary-words.ts +67 -0
- package/src/repositories/dictionary.ts +8 -1
- package/src/search.ts +45 -3
- package/src/site-search.ts +6 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/data",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.54.1",
|
|
4
4
|
"description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. SQLite and Postgres storage for crombolo: schema, migrations and repositories.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Crombolo",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@crombolo/core": "0.
|
|
31
|
+
"@crombolo/core": "0.54.1",
|
|
32
32
|
"kysely": "0.29.6"
|
|
33
33
|
}
|
|
34
34
|
}
|
package/src/cluster.ts
CHANGED
|
@@ -276,42 +276,34 @@ export async function touchServer(db: Db, identity: ServerIdentity): Promise<voi
|
|
|
276
276
|
const now = DbDate.toDb(new Date())
|
|
277
277
|
const role = identity.role ?? null
|
|
278
278
|
const crombolo = identity.cromboloVersion ?? null
|
|
279
|
-
const existing = await db.q
|
|
280
|
-
.selectFrom('server')
|
|
281
|
-
.select('node_id')
|
|
282
|
-
.where('node_id', '=', identity.nodeId)
|
|
283
|
-
.executeTakeFirst()
|
|
284
279
|
const served = identity.served?.checkpoint ?? null
|
|
285
280
|
const servedAt = identity.served ? DbDate.toDb(identity.served.at) : null
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
281
|
+
// One statement, so a boot and a poll writing at once can't both find no row and
|
|
282
|
+
// both insert one.
|
|
283
|
+
await db.q
|
|
284
|
+
.insertInto('server')
|
|
285
|
+
.values({
|
|
286
|
+
node_id: identity.nodeId,
|
|
287
|
+
version: identity.version,
|
|
288
|
+
revision: identity.revision,
|
|
289
|
+
role,
|
|
290
|
+
crombolo_version: crombolo,
|
|
291
|
+
last_seen: now,
|
|
292
|
+
served_checkpoint: served,
|
|
293
|
+
served_at: servedAt,
|
|
294
|
+
})
|
|
295
|
+
.onConflict((conflict) =>
|
|
296
|
+
conflict.column('node_id').doUpdateSet({
|
|
290
297
|
version: identity.version,
|
|
291
298
|
revision: identity.revision,
|
|
292
299
|
role,
|
|
293
300
|
crombolo_version: crombolo,
|
|
294
301
|
last_seen: now,
|
|
295
|
-
served_checkpoint: sql<number>`coalesce(${served}, ${sql.ref('served_checkpoint')})`,
|
|
296
|
-
served_at: sql<string>`coalesce(${servedAt}, ${sql.ref('served_at')})`,
|
|
297
|
-
})
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
} else {
|
|
301
|
-
await db.q
|
|
302
|
-
.insertInto('server')
|
|
303
|
-
.values({
|
|
304
|
-
node_id: identity.nodeId,
|
|
305
|
-
version: identity.version,
|
|
306
|
-
revision: identity.revision,
|
|
307
|
-
role,
|
|
308
|
-
crombolo_version: crombolo,
|
|
309
|
-
last_seen: now,
|
|
310
|
-
served_checkpoint: served,
|
|
311
|
-
served_at: servedAt,
|
|
312
|
-
})
|
|
313
|
-
.execute()
|
|
314
|
-
}
|
|
302
|
+
served_checkpoint: sql<number>`coalesce(${served}, ${sql.ref('server.served_checkpoint')})`,
|
|
303
|
+
served_at: sql<string>`coalesce(${servedAt}, ${sql.ref('server.served_at')})`,
|
|
304
|
+
}),
|
|
305
|
+
)
|
|
306
|
+
.execute()
|
|
315
307
|
}
|
|
316
308
|
|
|
317
309
|
export interface LiveNode {
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What `{#name}` reads as in a culture, for what the search index holds (story
|
|
3
|
+
* 297): the dictionary item's words, then its language's fallbacks', then the
|
|
4
|
+
* default language's, then its name. The published cache does the same for pages.
|
|
5
|
+
*/
|
|
6
|
+
import { resolveDictionaryWords } from '@crombolo/core'
|
|
7
|
+
import type { Db } from './database.ts'
|
|
8
|
+
import { fromDbBool } from './dialect.ts'
|
|
9
|
+
|
|
10
|
+
/** Each culture's reader of a text's `{#name}`s. */
|
|
11
|
+
export type DictionaryWords = (culture: string | null) => (text: string) => string
|
|
12
|
+
|
|
13
|
+
const AS_WRITTEN: DictionaryWords = () => (text) => text
|
|
14
|
+
|
|
15
|
+
/** Read once, so an item indexed in many cultures asks the database once. */
|
|
16
|
+
export async function dictionaryWords(db: Db): Promise<DictionaryWords> {
|
|
17
|
+
const rows = await db.q
|
|
18
|
+
.selectFrom('dictionary_item as d')
|
|
19
|
+
.leftJoin('dictionary_text as t', 't.dictionary_key', 'd.key')
|
|
20
|
+
.leftJoin('language as l', 'l.id', 't.language_id')
|
|
21
|
+
.select(['d.item_key', 'l.iso_code', 't.value'])
|
|
22
|
+
.execute()
|
|
23
|
+
if (rows.length === 0) return AS_WRITTEN
|
|
24
|
+
const items = new Map<string, { name: string; words: Map<string, string> }>()
|
|
25
|
+
for (const row of rows) {
|
|
26
|
+
const name = String(row.item_key)
|
|
27
|
+
const item = items.get(name.toLowerCase()) ?? { name, words: new Map() }
|
|
28
|
+
if (row.iso_code && row.value)
|
|
29
|
+
item.words.set(String(row.iso_code).toLowerCase(), String(row.value))
|
|
30
|
+
items.set(name.toLowerCase(), item)
|
|
31
|
+
}
|
|
32
|
+
const languages = await db.q
|
|
33
|
+
.selectFrom('language as l')
|
|
34
|
+
.leftJoin('language as f', 'f.id', 'l.fallback_language_id')
|
|
35
|
+
.select(['l.iso_code', 'l.is_default', 'f.iso_code as fallback'])
|
|
36
|
+
.execute()
|
|
37
|
+
const fallbackOf = new Map(
|
|
38
|
+
languages.map((l) => [
|
|
39
|
+
String(l.iso_code).toLowerCase(),
|
|
40
|
+
l.fallback ? String(l.fallback) : null,
|
|
41
|
+
]),
|
|
42
|
+
)
|
|
43
|
+
const defaultCulture = languages.find((l) => fromDbBool(l.is_default))?.iso_code
|
|
44
|
+
const chain = (culture: string | null): string[] => {
|
|
45
|
+
const out: string[] = []
|
|
46
|
+
let next = (culture ?? (defaultCulture ? String(defaultCulture) : null))?.toLowerCase()
|
|
47
|
+
while (next && !out.includes(next)) {
|
|
48
|
+
out.push(next)
|
|
49
|
+
next = fallbackOf.get(next)?.toLowerCase()
|
|
50
|
+
}
|
|
51
|
+
if (defaultCulture) out.push(String(defaultCulture).toLowerCase())
|
|
52
|
+
return out
|
|
53
|
+
}
|
|
54
|
+
return (culture) => {
|
|
55
|
+
const order = chain(culture)
|
|
56
|
+
const word = (name: string) => {
|
|
57
|
+
const item = items.get(name.toLowerCase())
|
|
58
|
+
if (!item) return undefined
|
|
59
|
+
for (const c of order) {
|
|
60
|
+
const text = item.words.get(c)
|
|
61
|
+
if (text) return text
|
|
62
|
+
}
|
|
63
|
+
return item.name
|
|
64
|
+
}
|
|
65
|
+
return (text) => resolveDictionaryWords(text, word)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/** Dictionary items: a tree of keys, each with a translation per language. */
|
|
2
2
|
import { normaliseUuid } from '@crombolo/core'
|
|
3
3
|
import type { Db } from '../database.ts'
|
|
4
|
+
import { queueDictionaryWordsForSearch } from '../search.ts'
|
|
4
5
|
|
|
5
6
|
export interface DictionaryTranslation {
|
|
6
7
|
isoCode: string
|
|
@@ -201,6 +202,8 @@ export class DictionaryRepository {
|
|
|
201
202
|
})
|
|
202
203
|
.execute()
|
|
203
204
|
await repo.#writeTranslations(key, item.translations, ids)
|
|
205
|
+
// A page may name it already, and showed `{#name}` as written until now.
|
|
206
|
+
await queueDictionaryWordsForSearch(tx, [item.name])
|
|
204
207
|
return 'Success'
|
|
205
208
|
})
|
|
206
209
|
}
|
|
@@ -212,7 +215,8 @@ export class DictionaryRepository {
|
|
|
212
215
|
const k = normaliseUuid(key)
|
|
213
216
|
return this.#db.transaction(async (tx) => {
|
|
214
217
|
const repo = new DictionaryRepository(tx)
|
|
215
|
-
|
|
218
|
+
const before = await repo.get(k)
|
|
219
|
+
if (!before) return 'NotFound'
|
|
216
220
|
const clash = await repo.byName(change.name)
|
|
217
221
|
if (clash && clash.key !== k) return 'DuplicateItemKey'
|
|
218
222
|
const ids = await repo.#languageIds(change.translations)
|
|
@@ -223,6 +227,7 @@ export class DictionaryRepository {
|
|
|
223
227
|
.where('key', '=', k)
|
|
224
228
|
.execute()
|
|
225
229
|
await repo.#writeTranslations(k, change.translations, ids)
|
|
230
|
+
await queueDictionaryWordsForSearch(tx, [before.name, change.name])
|
|
226
231
|
return 'Success'
|
|
227
232
|
})
|
|
228
233
|
}
|
|
@@ -253,10 +258,12 @@ export class DictionaryRepository {
|
|
|
253
258
|
const repo = new DictionaryRepository(tx)
|
|
254
259
|
if (!(await repo.get(k))) return 'NotFound'
|
|
255
260
|
const doomed = [...(await repo.#descendantKeys(k)).reverse(), k]
|
|
261
|
+
const names = (await repo.many(doomed)).map((item) => item.name)
|
|
256
262
|
for (const d of doomed) {
|
|
257
263
|
await tx.q.deleteFrom('dictionary_text').where('dictionary_key', '=', d).execute()
|
|
258
264
|
await tx.q.deleteFrom('dictionary_item').where('key', '=', d).execute()
|
|
259
265
|
}
|
|
266
|
+
await queueDictionaryWordsForSearch(tx, names)
|
|
260
267
|
return 'Success'
|
|
261
268
|
})
|
|
262
269
|
}
|
package/src/search.ts
CHANGED
|
@@ -14,7 +14,8 @@ import {
|
|
|
14
14
|
} from '@crombolo/core'
|
|
15
15
|
import type { Db } from './database.ts'
|
|
16
16
|
import { fromDbBool } from './dialect.ts'
|
|
17
|
-
import {
|
|
17
|
+
import { dictionaryWords } from './dictionary-words.ts'
|
|
18
|
+
import { type Expression, now, rowLock, sql } from './query.ts'
|
|
18
19
|
import {
|
|
19
20
|
forgetSearchRemovals,
|
|
20
21
|
isInDatabase,
|
|
@@ -23,7 +24,7 @@ import {
|
|
|
23
24
|
searchItemFor,
|
|
24
25
|
searchProvidersOf,
|
|
25
26
|
} from './search-providers.ts'
|
|
26
|
-
import { searchWords, type UnsearchableProperty, valueText } from './search-text.ts'
|
|
27
|
+
import { escapeLike, searchWords, type UnsearchableProperty, valueText } from './search-text.ts'
|
|
27
28
|
import { siteEntriesFor } from './site-search.ts'
|
|
28
29
|
|
|
29
30
|
/** Umbraco's names for the two backoffice indexes, which the dashboard shows. */
|
|
@@ -115,6 +116,38 @@ export async function queueContentTypesForSearch(
|
|
|
115
116
|
)
|
|
116
117
|
}
|
|
117
118
|
|
|
119
|
+
/**
|
|
120
|
+
* Queues the items whose values name these dictionary items as `{#name}`, whose
|
|
121
|
+
* words in the index change with the item's (story 297).
|
|
122
|
+
*/
|
|
123
|
+
export async function queueDictionaryWordsForSearch(
|
|
124
|
+
db: Db,
|
|
125
|
+
names: Iterable<string>,
|
|
126
|
+
): Promise<void> {
|
|
127
|
+
const patterns = [...new Set([...names].map((name) => name.toLowerCase()))].map(
|
|
128
|
+
(name) => `%{#${escapeLike(name)}}%`,
|
|
129
|
+
)
|
|
130
|
+
if (patterns.length === 0) return
|
|
131
|
+
const rows = await db.q
|
|
132
|
+
.selectFrom('property_value')
|
|
133
|
+
.select('node_id')
|
|
134
|
+
.distinct()
|
|
135
|
+
.where('is_current', '=', db.dialect.boolValue(true) as boolean)
|
|
136
|
+
.where((eb) =>
|
|
137
|
+
eb.or(
|
|
138
|
+
patterns.flatMap((pattern) => [
|
|
139
|
+
sql<boolean>`lower(${eb.ref('text_value')}) like ${pattern} escape '\\'`,
|
|
140
|
+
sql<boolean>`lower(${eb.ref('varchar_value')}) like ${pattern} escape '\\'`,
|
|
141
|
+
]),
|
|
142
|
+
),
|
|
143
|
+
)
|
|
144
|
+
.execute()
|
|
145
|
+
await queueForSearch(
|
|
146
|
+
db,
|
|
147
|
+
rows.map((row) => Number(row.node_id)),
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
|
|
118
151
|
/**
|
|
119
152
|
* Queues every page, media item and member. An element type's properties are
|
|
120
153
|
* read inside blocks wherever they are used, which no query can narrow.
|
|
@@ -407,6 +440,12 @@ export async function entriesFor(db: Db, nodeId: number): Promise<SearchEntry[]>
|
|
|
407
440
|
.orderBy('p.id')
|
|
408
441
|
.execute()
|
|
409
442
|
const unsearchable = await unsearchableElementProperties(db)
|
|
443
|
+
// A text naming a dictionary item is found by the item's words, in each culture.
|
|
444
|
+
const words = values.some((row) =>
|
|
445
|
+
String(row.text_value ?? row.varchar_value ?? '').includes('{#'),
|
|
446
|
+
)
|
|
447
|
+
? await dictionaryWords(db)
|
|
448
|
+
: undefined
|
|
410
449
|
const names = await db.q
|
|
411
450
|
.selectFrom('content_version as cv')
|
|
412
451
|
.innerJoin('content_version_culture_variation as cvcv', 'cvcv.version_id', 'cv.id')
|
|
@@ -433,10 +472,13 @@ export async function entriesFor(db: Db, nodeId: number): Promise<SearchEntry[]>
|
|
|
433
472
|
|
|
434
473
|
const textOf = (culture: string | null) => {
|
|
435
474
|
const fields: Record<string, string[]> = {}
|
|
475
|
+
const read = words?.(culture)
|
|
436
476
|
for (const row of values) {
|
|
437
477
|
const iso = row.iso_code === null || row.iso_code === undefined ? null : String(row.iso_code)
|
|
438
478
|
if (iso !== null && iso.toLowerCase() !== culture?.toLowerCase()) continue
|
|
439
|
-
const texts = valueText(row.text_value ?? row.varchar_value, unsearchable)
|
|
479
|
+
const texts = valueText(row.text_value ?? row.varchar_value, unsearchable).map(
|
|
480
|
+
(text) => read?.(text) ?? text,
|
|
481
|
+
)
|
|
440
482
|
if (texts.length === 0) continue
|
|
441
483
|
const name = iso ? `${String(row.alias)}_${iso.toLowerCase()}` : String(row.alias)
|
|
442
484
|
fields[name] = [...(fields[name] ?? []), ...texts]
|
package/src/site-search.ts
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
} from '@crombolo/core'
|
|
15
15
|
import type { Db } from './database.ts'
|
|
16
16
|
import { DbDate, fromDbBool } from './dialect.ts'
|
|
17
|
+
import { dictionaryWords } from './dictionary-words.ts'
|
|
17
18
|
import { sql } from './query.ts'
|
|
18
19
|
import { publishedValueRows } from './repositories/documents.ts'
|
|
19
20
|
import { hiddenElementProperties } from './search.ts'
|
|
@@ -139,12 +140,16 @@ export async function siteEntriesFor(db: Db, nodeId: number): Promise<SiteSearch
|
|
|
139
140
|
(row) => row.segment === null,
|
|
140
141
|
)
|
|
141
142
|
const hidden = await hiddenElementProperties(db, true)
|
|
143
|
+
const words = shown.some((row) => String(row.raw ?? '').includes('{#'))
|
|
144
|
+
? await dictionaryWords(db)
|
|
145
|
+
: undefined
|
|
142
146
|
|
|
143
147
|
const entry = (culture: string, languageId: number | null, name: string): SiteSearchEntry => {
|
|
144
148
|
const text: SiteSearchText = { name, high: [], normal: [], low: [] }
|
|
149
|
+
const read = words?.(culture || null)
|
|
145
150
|
for (const row of shown) {
|
|
146
151
|
if (row.languageId !== null && row.languageId !== languageId) continue
|
|
147
|
-
text[row.level].push(...valueText(row.raw, hidden))
|
|
152
|
+
text[row.level].push(...valueText(row.raw, hidden).map((t) => read?.(t) ?? t))
|
|
148
153
|
}
|
|
149
154
|
return { culture, text }
|
|
150
155
|
}
|