@yaks/sqlite 0.0.0-stage → 0.2.36

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.
Files changed (289) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +1596 -2
  3. package/_dist/archetype.d.ts +75 -0
  4. package/_dist/archetype.d.ts.map +1 -0
  5. package/_dist/branches.d.ts +23 -0
  6. package/_dist/branches.d.ts.map +1 -0
  7. package/_dist/budget.d.ts +52 -0
  8. package/_dist/budget.d.ts.map +1 -0
  9. package/_dist/bundle.d.ts +2 -0
  10. package/_dist/bundle.d.ts.map +1 -0
  11. package/_dist/cache.d.ts +24 -0
  12. package/_dist/cache.d.ts.map +1 -0
  13. package/_dist/catalog.d.ts +9 -0
  14. package/_dist/catalog.d.ts.map +1 -0
  15. package/_dist/check.d.ts +4 -0
  16. package/_dist/check.d.ts.map +1 -0
  17. package/_dist/db.d.ts +36 -0
  18. package/_dist/db.d.ts.map +1 -0
  19. package/_dist/ddl.d.ts +143 -0
  20. package/_dist/ddl.d.ts.map +1 -0
  21. package/_dist/doom.d.ts +22 -0
  22. package/_dist/doom.d.ts.map +1 -0
  23. package/_dist/follow.d.ts +74 -0
  24. package/_dist/follow.d.ts.map +1 -0
  25. package/_dist/history-shapes.d.ts +3 -0
  26. package/_dist/history-shapes.d.ts.map +1 -0
  27. package/_dist/history.d.ts +18 -0
  28. package/_dist/history.d.ts.map +1 -0
  29. package/_dist/inspect.d.ts +43 -0
  30. package/_dist/inspect.d.ts.map +1 -0
  31. package/_dist/jsonb.d.ts +17 -0
  32. package/_dist/jsonb.d.ts.map +1 -0
  33. package/_dist/layout.d.ts +7 -0
  34. package/_dist/layout.d.ts.map +1 -0
  35. package/_dist/ledger.d.ts +41 -0
  36. package/_dist/ledger.d.ts.map +1 -0
  37. package/_dist/meta.d.ts +89 -0
  38. package/_dist/meta.d.ts.map +1 -0
  39. package/_dist/migration.d.ts +41 -0
  40. package/_dist/migration.d.ts.map +1 -0
  41. package/_dist/mod.d.ts +201 -0
  42. package/_dist/mod.d.ts.map +1 -0
  43. package/_dist/mode.d.ts +14 -0
  44. package/_dist/mode.d.ts.map +1 -0
  45. package/_dist/overlay.d.ts +30 -0
  46. package/_dist/overlay.d.ts.map +1 -0
  47. package/_dist/owe.d.ts +180 -0
  48. package/_dist/owe.d.ts.map +1 -0
  49. package/_dist/physical.d.ts +89 -0
  50. package/_dist/physical.d.ts.map +1 -0
  51. package/_dist/point.d.ts +10 -0
  52. package/_dist/point.d.ts.map +1 -0
  53. package/_dist/raise.d.ts +22 -0
  54. package/_dist/raise.d.ts.map +1 -0
  55. package/_dist/read-db.d.ts +9 -0
  56. package/_dist/read-db.d.ts.map +1 -0
  57. package/_dist/read-store.d.ts +10 -0
  58. package/_dist/read-store.d.ts.map +1 -0
  59. package/_dist/read.d.ts +59 -0
  60. package/_dist/read.d.ts.map +1 -0
  61. package/_dist/reader.d.ts +11 -0
  62. package/_dist/reader.d.ts.map +1 -0
  63. package/_dist/rules.d.ts +19 -0
  64. package/_dist/rules.d.ts.map +1 -0
  65. package/_dist/service.d.ts +12 -0
  66. package/_dist/service.d.ts.map +1 -0
  67. package/_dist/shape.d.ts +12 -0
  68. package/_dist/shape.d.ts.map +1 -0
  69. package/_dist/statements.d.ts +4 -0
  70. package/_dist/statements.d.ts.map +1 -0
  71. package/_dist/survey.d.ts +31 -0
  72. package/_dist/survey.d.ts.map +1 -0
  73. package/_dist/tools.d.ts +15 -0
  74. package/_dist/tools.d.ts.map +1 -0
  75. package/_dist/trash.d.ts +18 -0
  76. package/_dist/trash.d.ts.map +1 -0
  77. package/_dist/unit.d.ts +11 -0
  78. package/_dist/unit.d.ts.map +1 -0
  79. package/_dist/vocab.d.ts +6 -0
  80. package/_dist/vocab.d.ts.map +1 -0
  81. package/_dist/write.d.ts +169 -0
  82. package/_dist/write.d.ts.map +1 -0
  83. package/answer.js +110 -0
  84. package/answer.js.map +1 -0
  85. package/answer.ts +122 -0
  86. package/archetype.js +730 -0
  87. package/archetype.js.map +1 -0
  88. package/archetype.ts +790 -0
  89. package/binary_cases.ts +240 -0
  90. package/branches.js +195 -0
  91. package/branches.js.map +1 -0
  92. package/branches.ts +257 -0
  93. package/budget.js +89 -0
  94. package/budget.js.map +1 -0
  95. package/budget.ts +117 -0
  96. package/builtin.js +34 -0
  97. package/builtin.js.map +1 -0
  98. package/builtin.ts +31 -0
  99. package/bundle.js +21 -0
  100. package/bundle.js.map +1 -0
  101. package/bundle.ts +22 -0
  102. package/buried.js +95 -0
  103. package/buried.js.map +1 -0
  104. package/buried.ts +124 -0
  105. package/cache.js +153 -0
  106. package/cache.js.map +1 -0
  107. package/cache.ts +154 -0
  108. package/cache_worker.ts +16 -0
  109. package/catalog.js +173 -0
  110. package/catalog.js.map +1 -0
  111. package/catalog.ts +224 -0
  112. package/check.js +80 -0
  113. package/check.js.map +1 -0
  114. package/check.ts +92 -0
  115. package/checkpoint.js +98 -0
  116. package/checkpoint.js.map +1 -0
  117. package/checkpoint.ts +129 -0
  118. package/checkpoint_worker.ts +37 -0
  119. package/db.js +170 -0
  120. package/db.js.map +1 -0
  121. package/db.ts +173 -0
  122. package/ddl.js +1441 -0
  123. package/ddl.js.map +1 -0
  124. package/ddl.ts +1508 -0
  125. package/deno.json +1 -0
  126. package/doom.js +59 -0
  127. package/doom.js.map +1 -0
  128. package/doom.ts +75 -0
  129. package/driver.js +69 -0
  130. package/driver.js.map +1 -0
  131. package/driver.ts +101 -0
  132. package/error.js +30 -0
  133. package/error.js.map +1 -0
  134. package/error.ts +46 -0
  135. package/ffi/LICENSE +202 -0
  136. package/ffi/blob.js +99 -0
  137. package/ffi/blob.js.map +1 -0
  138. package/ffi/blob.ts +156 -0
  139. package/ffi/constants.js +65 -0
  140. package/ffi/constants.js.map +1 -0
  141. package/ffi/constants.ts +67 -0
  142. package/ffi/database.js +613 -0
  143. package/ffi/database.js.map +1 -0
  144. package/ffi/database.ts +931 -0
  145. package/ffi/ffi.js +535 -0
  146. package/ffi/ffi.js.map +1 -0
  147. package/ffi/ffi.ts +604 -0
  148. package/ffi/mod.js +15 -0
  149. package/ffi/mod.js.map +1 -0
  150. package/ffi/mod.ts +28 -0
  151. package/ffi/statement.js +555 -0
  152. package/ffi/statement.js.map +1 -0
  153. package/ffi/statement.ts +788 -0
  154. package/ffi/util.js +32 -0
  155. package/ffi/util.js.map +1 -0
  156. package/ffi/util.ts +48 -0
  157. package/follow.js +342 -0
  158. package/follow.js.map +1 -0
  159. package/follow.ts +401 -0
  160. package/history-branch.js +166 -0
  161. package/history-branch.js.map +1 -0
  162. package/history-branch.ts +140 -0
  163. package/history-shapes.js +254 -0
  164. package/history-shapes.js.map +1 -0
  165. package/history-shapes.ts +268 -0
  166. package/history.js +620 -0
  167. package/history.js.map +1 -0
  168. package/history.ts +674 -0
  169. package/inspect.js +120 -0
  170. package/inspect.js.map +1 -0
  171. package/inspect.ts +164 -0
  172. package/jsonb.js +73 -0
  173. package/jsonb.js.map +1 -0
  174. package/jsonb.ts +108 -0
  175. package/layout.js +22 -0
  176. package/layout.js.map +1 -0
  177. package/layout.ts +26 -0
  178. package/ledger.js +210 -0
  179. package/ledger.js.map +1 -0
  180. package/ledger.ts +210 -0
  181. package/legacy.js +4 -0
  182. package/legacy.js.map +1 -0
  183. package/legacy.ts +3 -0
  184. package/maintenance.js +148 -0
  185. package/maintenance.js.map +1 -0
  186. package/maintenance.ts +154 -0
  187. package/measure.js +133 -0
  188. package/measure.js.map +1 -0
  189. package/measure.ts +167 -0
  190. package/measure_worker.ts +18 -0
  191. package/memo.js +1250 -0
  192. package/memo.js.map +1 -0
  193. package/memo.ts +1480 -0
  194. package/meta.js +265 -0
  195. package/meta.js.map +1 -0
  196. package/meta.ts +321 -0
  197. package/migration.js +272 -0
  198. package/migration.js.map +1 -0
  199. package/migration.ts +286 -0
  200. package/mod.js +1103 -0
  201. package/mod.js.map +1 -0
  202. package/mod.ts +1762 -0
  203. package/mode.js +192 -0
  204. package/mode.js.map +1 -0
  205. package/mode.ts +207 -0
  206. package/native.js +544 -0
  207. package/native.js.map +1 -0
  208. package/native.ts +544 -0
  209. package/overlay.js +408 -0
  210. package/overlay.js.map +1 -0
  211. package/overlay.ts +485 -0
  212. package/owe.js +719 -0
  213. package/owe.js.map +1 -0
  214. package/owe.ts +873 -0
  215. package/package.json +99 -4
  216. package/physical.js +350 -0
  217. package/physical.js.map +1 -0
  218. package/physical.ts +434 -0
  219. package/plan.js +78 -0
  220. package/plan.js.map +1 -0
  221. package/plan.ts +104 -0
  222. package/point.js +81 -0
  223. package/point.js.map +1 -0
  224. package/point.ts +52 -0
  225. package/population.js +639 -0
  226. package/population.js.map +1 -0
  227. package/population.ts +630 -0
  228. package/purge.js +117 -0
  229. package/purge.js.map +1 -0
  230. package/purge.ts +127 -0
  231. package/raise.js +270 -0
  232. package/raise.js.map +1 -0
  233. package/raise.ts +316 -0
  234. package/read-db.js +381 -0
  235. package/read-db.js.map +1 -0
  236. package/read-db.ts +282 -0
  237. package/read-store.js +74 -0
  238. package/read-store.js.map +1 -0
  239. package/read-store.ts +86 -0
  240. package/read.js +1100 -0
  241. package/read.js.map +1 -0
  242. package/read.ts +1441 -0
  243. package/reader.js +141 -0
  244. package/reader.js.map +1 -0
  245. package/reader.ts +159 -0
  246. package/rules.js +88 -0
  247. package/rules.js.map +1 -0
  248. package/rules.ts +113 -0
  249. package/service.js +27 -0
  250. package/service.js.map +1 -0
  251. package/service.ts +41 -0
  252. package/shape.js +76 -0
  253. package/shape.js.map +1 -0
  254. package/shape.ts +108 -0
  255. package/sparse_presence_fixture.ts +45 -0
  256. package/sqlitepath.js +24 -0
  257. package/sqlitepath.js.map +1 -0
  258. package/sqlitepath.ts +29 -0
  259. package/statements.js +78 -0
  260. package/statements.js.map +1 -0
  261. package/statements.ts +96 -0
  262. package/survey.js +135 -0
  263. package/survey.js.map +1 -0
  264. package/survey.ts +196 -0
  265. package/tools.js +17 -0
  266. package/tools.js.map +1 -0
  267. package/tools.ts +50 -0
  268. package/trash.js +144 -0
  269. package/trash.js.map +1 -0
  270. package/trash.ts +187 -0
  271. package/unit.js +215 -0
  272. package/unit.js.map +1 -0
  273. package/unit.ts +211 -0
  274. package/versions.js +115 -0
  275. package/versions.js.map +1 -0
  276. package/versions.ts +110 -0
  277. package/vocab.js +19 -0
  278. package/vocab.js.map +1 -0
  279. package/vocab.json +27 -0
  280. package/vocab.ts +19 -0
  281. package/watch.js +33 -0
  282. package/watch.js.map +1 -0
  283. package/watch.ts +42 -0
  284. package/write.js +1164 -0
  285. package/write.js.map +1 -0
  286. package/write.ts +1356 -0
  287. package/written.js +200 -0
  288. package/written.js.map +1 -0
  289. package/written.ts +202 -0
package/survey.ts ADDED
@@ -0,0 +1,196 @@
1
+ // What a look at a file finds against a vocabulary's schema, kept in the file
2
+ // for every opener after the one that looked. A reader reads what the file
3
+ // holds (./reader.ts): a property whose column the file's table lacks reads
4
+ // as never given, a component whose table it lacks is worn by nothing, and a
5
+ // declared index it lacks is not forced. Memory follows other connections'
6
+ // writes through the log only where every table it keeps has the log's
7
+ // triggers (./written.ts). Finding all that out reads the whole of
8
+ // `sqlite_schema` and renders every table's and trigger's statement to
9
+ // compare: milliseconds in every process that opens a graph, for an answer
10
+ // that changes only when the schema does.
11
+ //
12
+ // So a writer opening the file records what it finds, as the `server_meta`
13
+ // row `surveyed:<print>` (the store's print, ./plan.ts), with the version the
14
+ // schema stood at (`pragma schema_version`), and an opener of the same
15
+ // vocabulary reads the survey there instead. SQLite moves that version, kept
16
+ // in the file's header, on every change to the schema, whoever makes it, so a
17
+ // survey is never read past a column dropped, an index built or a table refit
18
+ // after it: those find it out of date, and look again, until a writer records
19
+ // the next one.
20
+
21
+ import {
22
+ catalogue,
23
+ type Derived,
24
+ type Driver,
25
+ lit,
26
+ revision,
27
+ tagOf,
28
+ } from '@yaks/sql'
29
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
30
+ import { lacking } from './follow.js'
31
+ import { built } from './ddl.js'
32
+ import { tables } from './layout.js'
33
+ import { meta, SURVEYED, surveyedAt } from './meta.js'
34
+ import { plan } from './plan.js'
35
+ import { unit } from './unit.js'
36
+ import { follows } from './written.js'
37
+
38
+ /** What a look at a file finds against a vocabulary's schema. */
39
+ export type Survey = {
40
+ /** each stored property, as `comp.prop`, whose column the file's table
41
+ * lacks */
42
+ lacking: string[]
43
+ /** each component whose table the file lacks */
44
+ absent: string[]
45
+ /** each declared index the file lacks */
46
+ unindexed: string[]
47
+ /** each of the spine, the graves and the component tables whose writes the
48
+ * log does not follow */
49
+ unlogged: string[]
50
+ }
51
+
52
+ // The tables whose writes the log is asked to follow: the store's own.
53
+ let logs = (vocab: Vocab) => ['entity', 'tombstone', ...tables(vocab)]
54
+
55
+ /** What a reader binds over the file: the survey's properties, read as never
56
+ * given. */
57
+ export let lacked = (
58
+ vocab: Vocab,
59
+ survey: Pick<Survey, 'lacking'>,
60
+ ): Derived =>
61
+ Object.fromEntries(survey.lacking.map((key) => {
62
+ let at = key.indexOf('.')
63
+ let c = vocab.prop(key.slice(0, at), key.slice(at + 1))!
64
+ return [key, { tag: tagOf(c), expr: () => lit(null) }]
65
+ }))
66
+
67
+ /** What a reader asks of the file itself: its tables, columns and indexes,
68
+ * and not the log's triggers. */
69
+ export let glance = (
70
+ driver: Driver,
71
+ vocab: Vocab,
72
+ derived?: Derived,
73
+ ): Omit<Survey, 'unlogged'> => {
74
+ let behind = lacking(driver, vocab, derived, true)
75
+ let objects = new Set(catalogue(driver).map((o) => o.name))
76
+ return {
77
+ lacking: Object.keys(behind.derived),
78
+ absent: behind.absent,
79
+ unindexed: built(driver, vocab).flatMap((s) =>
80
+ s.t == 'create index' && !objects.has(s.name) ? [s.name] : []
81
+ ),
82
+ }
83
+ }
84
+
85
+ /** The file looked at, all of it. */
86
+ export let look = (
87
+ driver: Driver,
88
+ vocab: Vocab,
89
+ derived?: Derived,
90
+ ): Survey => ({
91
+ ...glance(driver, vocab, derived),
92
+ unlogged: logs(vocab).filter((name) => !follows(driver, [name])),
93
+ })
94
+
95
+ // The version the file's schema stands at now.
96
+ let version = (driver: Driver) =>
97
+ Number(
98
+ driver.query({ t: 'pragma', name: 'schema_version' })[0]?.schema_version,
99
+ )
100
+
101
+ // Per driver and print, the survey read while the schema stood at a revision
102
+ // (@yaks/sql `revision`): a reader and memory both ask, and one read of the
103
+ // row answers both.
104
+ let seen = new WeakMap<
105
+ Driver,
106
+ Map<string, { schema: number; survey?: Survey }>
107
+ >()
108
+ let hold = (driver: Driver, print: string, survey?: Survey) => {
109
+ let by = seen.get(driver) ?? new Map()
110
+ seen.set(driver, by)
111
+ by.set(print, { schema: revision(driver, 'schema'), survey })
112
+ return survey
113
+ }
114
+
115
+ /** The survey the file holds of this vocabulary's schema, where it was taken
116
+ * at the version the schema stands at now; undefined where there is none, or
117
+ * the schema has moved since. Read in the snapshot it is asked in. */
118
+ export let surveyed = (
119
+ driver: Driver,
120
+ vocab: Vocab,
121
+ derived?: Derived,
122
+ ): Survey | undefined => {
123
+ if (!driver.file) return
124
+ let print = plan(vocab, derived, true).print
125
+ let held = seen.get(driver)?.get(print)
126
+ if (held && held.schema === revision(driver, 'schema')) return held.survey
127
+ let row = surveyedAt(driver, print)
128
+ let { at, ...survey } = row ? JSON.parse(row) as Survey & { at: number } : {}
129
+ return hold(
130
+ driver,
131
+ print,
132
+ at === version(driver) ? survey as Survey : undefined,
133
+ )
134
+ }
135
+
136
+ /** Record what a look at the file finds against this vocabulary's schema,
137
+ * for every opener after this one. Nothing where the file is in memory, or
138
+ * holds a survey of the schema as it stands. */
139
+ export let survey = (
140
+ driver: Driver,
141
+ vocab: Vocab,
142
+ derived?: Derived,
143
+ ): void => {
144
+ if (!driver.file || surveyed(driver, vocab, derived)) return
145
+ unit(driver, () => {
146
+ // Another writer may have recorded it while this one waited for the lock.
147
+ if (surveyed(driver, vocab, derived)) return
148
+ let found = look(driver, vocab, derived)
149
+ let print = plan(vocab, derived, true).print
150
+ meta(driver).set(
151
+ SURVEYED + print,
152
+ JSON.stringify({ at: version(driver), ...found }),
153
+ )
154
+ hold(driver, print, found)
155
+ })
156
+ }
157
+
158
+ /** What a look found, `before`, across the tables a write just raised
159
+ * (./raise.ts): `raised` no longer absent, nor the indexes made with them. A
160
+ * table raised is made whole, its indexes and the log's triggers with it, so
161
+ * nothing else the look found moves. Its indexes kept missing, every read of
162
+ * the component was planned as though it had none: a person's messages walked
163
+ * every titled row for the few knocks a seek of `knock.target` reads (bug
164
+ * 73244693). */
165
+ export let raisedOver = <T extends Omit<Survey, 'unlogged'>>(
166
+ driver: Driver,
167
+ before: T,
168
+ raised: readonly string[],
169
+ ): T => {
170
+ let gone = new Set(raised)
171
+ let made = new Set(catalogue(driver).map((o) => o.name))
172
+ return {
173
+ ...before,
174
+ absent: before.absent.filter((c) => !gone.has(c)),
175
+ unindexed: before.unindexed.filter((name) => !made.has(name)),
176
+ }
177
+ }
178
+
179
+ /** Keep the survey the file holds standing across the tables a write just
180
+ * raised ({@link raisedOver}). Nothing where there was no survey to keep. */
181
+ export let raisedSurvey = (
182
+ driver: Driver,
183
+ vocab: Vocab,
184
+ derived: Derived | undefined,
185
+ before: Survey | undefined,
186
+ raised: readonly string[],
187
+ ): void => {
188
+ if (!driver.file || !before || !raised.length) return
189
+ let found = raisedOver(driver, before, raised)
190
+ let print = plan(vocab, derived, true).print
191
+ meta(driver).set(
192
+ SURVEYED + print,
193
+ JSON.stringify({ at: version(driver), ...found }),
194
+ )
195
+ hold(driver, print, found)
196
+ }
package/tools.js ADDED
@@ -0,0 +1,17 @@
1
+ // The check tools declared in ./vocab.json. The storage adapter supplies bound
2
+ // diagnostics; the portable host exposes no SQL driver. A storage adapter that
3
+ // supplies no diagnostics cannot establish a verdict, so its checks warn.
4
+ import { checked } from "@yaks/tools/check";
5
+ let SAMPLE = 12;
6
+ let unavailable = (diagnostic)=>[
7
+ {
8
+ level: 'warn',
9
+ text: `this storage does not provide ${diagnostic} diagnostics, so this ` + 'check is unavailable'
10
+ }
11
+ ];
12
+ /** The implementations behind the tools ./vocab.json declares, using the
13
+ * calling application's bound storage diagnostics. */ export let runs = (host, options = {})=>({
14
+ storage_check: (call)=>checked(call.entity.eid, 'the file holds no orphaned row and no broken reference', host.storage.checks?.storage() ?? unavailable('storage')),
15
+ archetype_check: (call)=>checked(call.entity.eid, 'every archetype pointer matches the components its owner carries', host.storage.checks?.archetypes(options.sample ?? SAMPLE) ?? unavailable('archetype'))
16
+ });
17
+ //# sourceMappingURL=tools.js.map
package/tools.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tools.js","sources":["./tools.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,+EAA+E;AAC/E,0EAA0E;AAI1E,SAAS,OAAO,iCAAsC;AAQtD,IAAI,SAAS;AAEb,IAAI,cAAc,CAAC,aAAkC;IAAC;MACpD,OAAO;MACP,MAAM,CAAC,8BAA8B,EAAE,WAAW,sBAAsB,CAAC,GACvE;IACJ;GAAE;AAEF;oDACoD,GACpD,OAAO,IAAI,OAAO,CAChB,MAQA,UAAmB,CAAC,CAAC,GACZ,CAAC;IACV,eAAe,CAAC,OACd,QACE,KAAK,MAAM,CAAC,GAAG,EACf,0DACA,KAAK,OAAO,CAAC,MAAM,EAAE,aAAa,YAAY;IAGlD,iBAAiB,CAAC,OAChB,QACE,KAAK,MAAM,CAAC,GAAG,EACf,oEACA,KAAK,OAAO,CAAC,MAAM,EAAE,WAAW,QAAQ,MAAM,IAAI,WAChD,YAAY;EAEpB,CAAC,EAAC"}
package/tools.ts ADDED
@@ -0,0 +1,50 @@
1
+ // The check tools declared in ./vocab.json. The storage adapter supplies bound
2
+ // diagnostics; the portable host exposes no SQL driver. A storage adapter that
3
+ // supplies no diagnostics cannot establish a verdict, so its checks warn.
4
+
5
+ import type { Runs } from 'jsr:@yaks/graph@0.2.36/tools'
6
+ import type { Finding } from 'jsr:@yaks/tools@0.2.36'
7
+ import { checked } from '@yaks/tools/check'
8
+
9
+ /** What configuration this package's checks accept. */
10
+ export type Options = {
11
+ /** how many drifted entities to name (default 12) */
12
+ sample?: number
13
+ }
14
+
15
+ let SAMPLE = 12
16
+
17
+ let unavailable = (diagnostic: string): Finding[] => [{
18
+ level: 'warn',
19
+ text: `this storage does not provide ${diagnostic} diagnostics, so this ` +
20
+ 'check is unavailable',
21
+ }]
22
+
23
+ /** The implementations behind the tools ./vocab.json declares, using the
24
+ * calling application's bound storage diagnostics. */
25
+ export let runs = (
26
+ host: {
27
+ storage: {
28
+ checks?: {
29
+ storage(): Finding[]
30
+ archetypes(sample: number): Finding[]
31
+ }
32
+ }
33
+ },
34
+ options: Options = {},
35
+ ): Runs => ({
36
+ storage_check: (call) =>
37
+ checked(
38
+ call.entity.eid,
39
+ 'the file holds no orphaned row and no broken reference',
40
+ host.storage.checks?.storage() ?? unavailable('storage'),
41
+ ),
42
+
43
+ archetype_check: (call) =>
44
+ checked(
45
+ call.entity.eid,
46
+ 'every archetype pointer matches the components its owner carries',
47
+ host.storage.checks?.archetypes(options.sample ?? SAMPLE) ??
48
+ unavailable('archetype'),
49
+ ),
50
+ })
package/trash.js ADDED
@@ -0,0 +1,144 @@
1
+ // Recoverable before-images are separate from optional edit history. The
2
+ // foreign key keeps a trashed identity out of background orphan collection.
3
+ import { among, and, array, as, col, desc, each, eq, exists, fn, from, gt, join, lit, lt, or, select, sub, table, val } from "@yaks/sql";
4
+ import { trashLimit } from "@yaks/graph/trash";
5
+ import { decode, stringify } from "@yaks/vocab/wire";
6
+ export let TRASH = '_yaks_trash';
7
+ /** Walk before-images in bounded pages for an in-place security rewrite. */ export let retainedBodies = (driver, after, limit)=>driver.query(select({
8
+ cols: [
9
+ as(col('rowid'), 'id'),
10
+ col('body')
11
+ ],
12
+ from: table(TRASH),
13
+ where: gt(col('rowid'), val(after)),
14
+ order: [
15
+ col('rowid')
16
+ ],
17
+ limit: val(limit)
18
+ })).map((r)=>({
19
+ id: Number(r.id),
20
+ body: String(r.body)
21
+ }));
22
+ /** A replacement keeps the identity, batch and date of the before-image. */ export let rewriteRetained = (driver, was, body)=>void driver.query({
23
+ t: 'update',
24
+ table: TRASH,
25
+ set: {
26
+ body: val(body)
27
+ },
28
+ where: and(eq(col('rowid'), val(was.id)), eq(col('body'), val(was.body)))
29
+ });
30
+ /** Save each row once per destructive transaction. */ export let retainSql = (id, at, bundles)=>!bundles.length ? [] : [
31
+ {
32
+ t: 'insert',
33
+ or: 'ignore',
34
+ into: TRASH,
35
+ cols: [
36
+ 'batch',
37
+ 'entity',
38
+ 'at',
39
+ 'body'
40
+ ],
41
+ q: select({
42
+ cols: [
43
+ val(id),
44
+ col('id', 'e'),
45
+ val(at),
46
+ col('value', '__trash')
47
+ ],
48
+ from: from(each(bundles.map((b)=>stringify(b))), '__trash'),
49
+ joins: [
50
+ join(table('entity', 'e'), eq(col('eid', 'e'), fn('json_extract', col('value', '__trash'), val('$.entity.eid'))))
51
+ ]
52
+ })
53
+ }
54
+ ];
55
+ /** Select every snapshot in a retained transaction touching these identities. */ export let trashSql = (eids, page = {})=>{
56
+ let before = page.before ? val(page.before.at) : page.after ? sub(select({
57
+ cols: [
58
+ col('at')
59
+ ],
60
+ from: table(TRASH),
61
+ where: eq(col('batch'), val(page.after)),
62
+ limit: val(1)
63
+ })) : undefined;
64
+ let batches = select({
65
+ cols: [
66
+ col('batch')
67
+ ],
68
+ from: table(TRASH),
69
+ distinct: true,
70
+ order: [
71
+ desc(col('at')),
72
+ desc(col('batch'))
73
+ ],
74
+ limit: val(trashLimit(page)),
75
+ where: and(...eids ? [
76
+ among(col('entity'), select({
77
+ cols: [
78
+ col('id')
79
+ ],
80
+ from: table('entity'),
81
+ where: among(col('eid'), array(eids))
82
+ }))
83
+ ] : [], ...page.id ? [
84
+ eq(col('batch'), val(page.id))
85
+ ] : [], ...before ? [
86
+ or(lt(col('at'), before), and(eq(col('at'), before), lt(col('batch'), val(page.before?.id ?? page.after))))
87
+ ] : [])
88
+ });
89
+ return select({
90
+ cols: [
91
+ col('batch'),
92
+ col('at'),
93
+ col('body')
94
+ ],
95
+ from: table(TRASH),
96
+ where: among(col('batch'), batches),
97
+ order: [
98
+ desc(col('at')),
99
+ desc(col('batch'))
100
+ ]
101
+ });
102
+ };
103
+ /** Decode retained transactions, preserving binary properties. */ export let trashRows = (vocab, rows)=>{
104
+ let found = new Map();
105
+ for (let row of rows){
106
+ let id = String(row.batch);
107
+ let entry = found.get(id) ?? {
108
+ id,
109
+ at: String(row.at),
110
+ bundles: []
111
+ };
112
+ entry.bundles.push(decode(vocab, JSON.parse(String(row.body))));
113
+ found.set(id, entry);
114
+ }
115
+ return [
116
+ ...found.values()
117
+ ];
118
+ };
119
+ /** Retained transactions touching an identity, newest first. */ export let trashed = (driver, vocab, eids, page)=>eids?.length === 0 ? [] : trashRows(vocab, driver.query(trashSql(eids, page)));
120
+ /** Erase one retained transaction, called only for a deliberate trash purge. */ export let purgeTrashSql = (id)=>({
121
+ t: 'delete',
122
+ from: TRASH,
123
+ where: typeof id == 'string' ? eq(col('batch'), val(id)) : among(col('entity'), select({
124
+ cols: [
125
+ col('id')
126
+ ],
127
+ from: table('entity'),
128
+ where: among(col('eid'), array(id))
129
+ }))
130
+ });
131
+ /** Cleanup candidates still held by retained before-images. */ export let trashTargetsSql = (eids)=>select({
132
+ cols: [
133
+ col('eid')
134
+ ],
135
+ from: table('entity'),
136
+ where: and(among(col('eid'), array(eids)), exists(select({
137
+ cols: [
138
+ lit(1)
139
+ ],
140
+ from: table(TRASH),
141
+ where: eq(col('entity'), col('id', 'entity'))
142
+ })))
143
+ });
144
+ //# sourceMappingURL=trash.js.map
package/trash.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trash.js","sources":["./trash.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,4EAA4E;AAC5E,SACE,KAAK,EACL,GAAG,EACH,KAAK,EACL,EAAE,EACF,GAAG,EAEH,IAAI,EAEJ,IAAI,EACJ,EAAE,EACF,MAAM,EACN,EAAE,EACF,IAAI,EACJ,EAAE,EAEF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I,KAAK,OAAO,IAAI,KAAK;IACzB,IAAI,QAAQ,MAAM,GAAG,CAAC,OAAO;MAAE;MAAI,IAAI,OAAO,IAAI,EAAE;MAAG,SAAS,EAAE;IAAC;IACnE,MAAM,OAAO,CAAC,IAAI,CAAC,OAAO,OAAO,KAAK,KAAK,CAAC,OAAO,IAAI,IAAI;IAC3D,MAAM,GAAG,CAAC,IAAI;EAChB;EACA,OAAO;OAAI,MAAM,MAAM;GAAG;AAC5B,EAAC;AAED,8DAA8D,GAC9D,OAAO,IAAI,UAAU,CACnB,QACA,OACA,MACA,OAEA,MAAM,WAAW,IAAI,EAAE,GAAG,UAAU,OAAO,OAAO,KAAK,CAAC,SAAS,MAAM,QAAO;AAEhF,8EAA8E,GAC9E,OAAO,IAAI,gBAAgB,CAAC,KAAkC,CAAC;IAC7D,GAAG;IACH,MAAM;IACN,OAAO,OAAO,MAAM,WAAW,GAAG,IAAI,UAAU,IAAI,OAAO,MACzD,IAAI,WACJ,OAAO;MACL,MAAM;QAAC,IAAI;OAAM;MACjB,MAAM,MAAM;MACZ,OAAO,MAAM,IAAI,QAAQ,MAAM;IACjC;EAEJ,CAAC,EAAC;AAEF,6DAA6D,GAC7D,OAAO,IAAI,kBAAkB,CAAC,OAC5B,OAAO;IACL,MAAM;MAAC,IAAI;KAAO;IAClB,MAAM,MAAM;IACZ,OAAO,IACL,MAAM,IAAI,QAAQ,MAAM,QACxB,OAAO,OAAO;MACZ,MAAM;QAAC,IAAI;OAAG;MACd,MAAM,MAAM;MACZ,OAAO,GAAG,IAAI,WAAW,IAAI,MAAM;IACrC;EAEJ,GAAE"}
package/trash.ts ADDED
@@ -0,0 +1,187 @@
1
+ // Recoverable before-images are separate from optional edit history. The
2
+ // foreign key keeps a trashed identity out of background orphan collection.
3
+ import {
4
+ among,
5
+ and,
6
+ array,
7
+ as,
8
+ col,
9
+ type Delete,
10
+ desc,
11
+ type Driver,
12
+ each,
13
+ eq,
14
+ exists,
15
+ fn,
16
+ from,
17
+ gt,
18
+ type Insert,
19
+ join,
20
+ lit,
21
+ lt,
22
+ or,
23
+ type Row,
24
+ type Select,
25
+ select,
26
+ sub,
27
+ table,
28
+ val,
29
+ } from '@yaks/sql'
30
+ import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
31
+ import { type Trash, trashLimit, type TrashPage } from '@yaks/graph/trash'
32
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
33
+ import { decode, stringify } from '@yaks/vocab/wire'
34
+
35
+ export let TRASH = '_yaks_trash'
36
+
37
+ type RetainedBody = { id: number; body: string }
38
+
39
+ /** Walk before-images in bounded pages for an in-place security rewrite. */
40
+ export let retainedBodies = (
41
+ driver: Driver,
42
+ after: number,
43
+ limit: number,
44
+ ): RetainedBody[] =>
45
+ driver.query(select({
46
+ cols: [as(col('rowid'), 'id'), col('body')],
47
+ from: table(TRASH),
48
+ where: gt(col('rowid'), val(after)),
49
+ order: [col('rowid')],
50
+ limit: val(limit),
51
+ })).map((r) => ({ id: Number(r.id), body: String(r.body) }))
52
+
53
+ /** A replacement keeps the identity, batch and date of the before-image. */
54
+ export let rewriteRetained = (
55
+ driver: Driver,
56
+ was: RetainedBody,
57
+ body: string,
58
+ ): void =>
59
+ void driver.query({
60
+ t: 'update',
61
+ table: TRASH,
62
+ set: { body: val(body) },
63
+ where: and(eq(col('rowid'), val(was.id)), eq(col('body'), val(was.body))),
64
+ })
65
+
66
+ /** Save each row once per destructive transaction. */
67
+ export let retainSql = (id: string, at: string, bundles: Bundle[]): Insert[] =>
68
+ !bundles.length ? [] : [{
69
+ t: 'insert',
70
+ or: 'ignore',
71
+ into: TRASH,
72
+ cols: ['batch', 'entity', 'at', 'body'],
73
+ q: select({
74
+ cols: [val(id), col('id', 'e'), val(at), col('value', '__trash')],
75
+ from: from(each(bundles.map((b) => stringify(b))), '__trash'),
76
+ joins: [join(
77
+ table('entity', 'e'),
78
+ eq(
79
+ col('eid', 'e'),
80
+ fn('json_extract', col('value', '__trash'), val('$.entity.eid')),
81
+ ),
82
+ )],
83
+ }),
84
+ }]
85
+
86
+ /** Select every snapshot in a retained transaction touching these identities. */
87
+ export let trashSql = (eids?: string[], page: TrashPage = {}): Select => {
88
+ let before = page.before ? val(page.before.at) : page.after
89
+ ? sub(
90
+ select({
91
+ cols: [col('at')],
92
+ from: table(TRASH),
93
+ where: eq(col('batch'), val(page.after)),
94
+ limit: val(1),
95
+ }),
96
+ )
97
+ : undefined
98
+ let batches = select({
99
+ cols: [col('batch')],
100
+ from: table(TRASH),
101
+ distinct: true,
102
+ order: [desc(col('at')), desc(col('batch'))],
103
+ limit: val(trashLimit(page)),
104
+ where: and(
105
+ ...eids
106
+ ? [
107
+ among(
108
+ col('entity'),
109
+ select({
110
+ cols: [col('id')],
111
+ from: table('entity'),
112
+ where: among(col('eid'), array(eids)),
113
+ }),
114
+ ),
115
+ ]
116
+ : [],
117
+ ...page.id ? [eq(col('batch'), val(page.id))] : [],
118
+ ...before
119
+ ? [
120
+ or(
121
+ lt(col('at'), before),
122
+ and(
123
+ eq(col('at'), before),
124
+ lt(col('batch'), val(page.before?.id ?? page.after!)),
125
+ ),
126
+ ),
127
+ ]
128
+ : [],
129
+ ),
130
+ })
131
+ return select({
132
+ cols: [col('batch'), col('at'), col('body')],
133
+ from: table(TRASH),
134
+ where: among(col('batch'), batches),
135
+ order: [desc(col('at')), desc(col('batch'))],
136
+ })
137
+ }
138
+
139
+ /** Decode retained transactions, preserving binary properties. */
140
+ export let trashRows = (vocab: Vocab, rows: Row[]): Trash[] => {
141
+ let found = new Map<string, Trash>()
142
+ for (let row of rows) {
143
+ let id = String(row.batch)
144
+ let entry = found.get(id) ?? { id, at: String(row.at), bundles: [] }
145
+ entry.bundles.push(decode(vocab, JSON.parse(String(row.body))))
146
+ found.set(id, entry)
147
+ }
148
+ return [...found.values()]
149
+ }
150
+
151
+ /** Retained transactions touching an identity, newest first. */
152
+ export let trashed = (
153
+ driver: Driver,
154
+ vocab: Vocab,
155
+ eids?: string[],
156
+ page?: TrashPage,
157
+ ): Trash[] =>
158
+ eids?.length === 0 ? [] : trashRows(vocab, driver.query(trashSql(eids, page)))
159
+
160
+ /** Erase one retained transaction, called only for a deliberate trash purge. */
161
+ export let purgeTrashSql = (id: string | string[]): Delete => ({
162
+ t: 'delete',
163
+ from: TRASH,
164
+ where: typeof id == 'string' ? eq(col('batch'), val(id)) : among(
165
+ col('entity'),
166
+ select({
167
+ cols: [col('id')],
168
+ from: table('entity'),
169
+ where: among(col('eid'), array(id)),
170
+ }),
171
+ ),
172
+ })
173
+
174
+ /** Cleanup candidates still held by retained before-images. */
175
+ export let trashTargetsSql = (eids: string[]): Select =>
176
+ select({
177
+ cols: [col('eid')],
178
+ from: table('entity'),
179
+ where: and(
180
+ among(col('eid'), array(eids)),
181
+ exists(select({
182
+ cols: [lit(1)],
183
+ from: table(TRASH),
184
+ where: eq(col('entity'), col('id', 'entity')),
185
+ })),
186
+ ),
187
+ })