@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/inspect.js ADDED
@@ -0,0 +1,120 @@
1
+ // Bounded physical facts about a SQLite store. The caller sees names and
2
+ // counts, never definitions, statement text, or row values.
3
+ import { mainTables } from "./physical.js";
4
+ import { heldBy } from "./archetype.js";
5
+ import { ledger } from "./ledger.js";
6
+ import { populations } from "./population.js";
7
+ import { by, col, eq, gt, join, lit, select, table } from "@yaks/sql";
8
+ /** Count deletions in [since, until), reading at most 1,000 graves from the
9
+ * primary key. Continue with `next` until null; timestamps need no new index.
10
+ * With `eids`, each grave in the window is named too. */ export let deletions = (db, window)=>{
11
+ let since = Date.parse(window.since), until = Date.parse(window.until);
12
+ let after = window.after ?? 0;
13
+ if (!Number.isFinite(since) || !Number.isFinite(until) || since >= until || !Number.isSafeInteger(after) || after < 0) {
14
+ throw new RangeError('deletions needs since < until and a nonnegative cursor');
15
+ }
16
+ let tables = new Set(mainTables(db).map((r)=>r.name));
17
+ let rows = !tables.has('tombstone') || window.eids && !tables.has('entity') ? [] : db.query(select({
18
+ cols: [
19
+ col('entity', 't'),
20
+ col('deleted_at', 't'),
21
+ ...window.eids ? [
22
+ col('eid', 'e')
23
+ ] : []
24
+ ],
25
+ from: table('tombstone', 't'),
26
+ joins: window.eids ? [
27
+ join(table('entity', 'e'), eq(col('id', 'e'), col('entity', 't')))
28
+ ] : [],
29
+ where: gt(col('entity', 't'), lit(after)),
30
+ order: [
31
+ col('entity', 't')
32
+ ],
33
+ limit: lit(1000)
34
+ }));
35
+ let minutes = {}, count = 0;
36
+ let graves = [];
37
+ for (let row of rows){
38
+ let at = Date.parse(String(row.deleted_at));
39
+ if (!(at >= since && at < until)) continue;
40
+ let minute = new Date(Math.floor(at / 60_000) * 60_000).toISOString();
41
+ minutes[minute] = (minutes[minute] ?? 0) + 1;
42
+ count++;
43
+ if (window.eids) {
44
+ graves.push({
45
+ eid: String(row.eid),
46
+ at: new Date(at).toISOString()
47
+ });
48
+ }
49
+ }
50
+ return {
51
+ scanned: rows.length,
52
+ count,
53
+ minutes,
54
+ next: rows.length == 1000 ? Number(rows.at(-1).entity) : null,
55
+ ...window.eids ? {
56
+ graves
57
+ } : {}
58
+ };
59
+ };
60
+ /** The ordinary tables and indexes the file holds, including tables no
61
+ * longer named by the current vocabulary. Counting is capped so a diagnostic
62
+ * remains bounded even after a store has seen many schemas. */ export let inspect = (db, limit = 160, preferred = [])=>{
63
+ let first = new Set([
64
+ 'entity',
65
+ 'journal',
66
+ 'yak_writes',
67
+ ...preferred
68
+ ]);
69
+ let names = mainTables(db).filter((r)=>r.type == 'table').map((r)=>r.name).sort((a, b)=>Number(first.has(b)) - Number(first.has(a)) || a.localeCompare(b));
70
+ let buckets = names.includes('entity') && names.includes('archetype') ? populations(db) : undefined;
71
+ let shown = names.slice(0, Math.max(0, Math.min(limit, 160))).map((name)=>{
72
+ let indexes = db.query({
73
+ t: 'pragma',
74
+ name: 'index_list',
75
+ arg: name
76
+ }).map((r)=>String(r.name));
77
+ return {
78
+ name,
79
+ rows: buckets && (name == 'entity' || buckets.some((b)=>b.tables.includes(name))) ? buckets.reduce((n, b)=>n + (name == 'entity' || b.tables.includes(name) ? b.n : 0), 0) : null,
80
+ indexCount: indexes.length,
81
+ indexes: indexes.slice(0, 16)
82
+ };
83
+ });
84
+ return {
85
+ tables: names.length,
86
+ shown,
87
+ omitted: names.length - shown.length
88
+ };
89
+ };
90
+ /** Identity and its archetype table set, never component values. */ export let identity = (db, eid)=>{
91
+ let [row] = mainTables(db).some((r)=>r.name == 'entity') ? db.query(select({
92
+ cols: [
93
+ col('id')
94
+ ],
95
+ from: table('entity'),
96
+ where: by({
97
+ eid
98
+ })
99
+ })) : [];
100
+ if (!row) {
101
+ return {
102
+ eid,
103
+ exists: false,
104
+ tombstoned: false,
105
+ tables: [],
106
+ omitted: 0
107
+ };
108
+ }
109
+ let tables = heldBy(db, [
110
+ eid
111
+ ], ledger(db)).sort();
112
+ return {
113
+ eid,
114
+ exists: true,
115
+ tombstoned: tables.includes('tombstone'),
116
+ tables: tables.slice(0, 160),
117
+ omitted: Math.max(0, tables.length - 160)
118
+ };
119
+ };
120
+ //# sourceMappingURL=inspect.js.map
package/inspect.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inspect.js","sources":["./inspect.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,4DAA4D;AAE5D,SAAS,UAAU,wBAAuB;AAC1C,SAAS,MAAM,yBAAwB;AACvC,SAAS,MAAM,sBAAqB;AACpC,SAAS,WAAW,0BAAyB;AAC7C,SACE,EAAE,EACF,GAAG,EAEH,EAAE,EACF,EAAE,EACF,IAAI,EACJ,GAAG,EACH,MAAM,EACN,KAAK,yBACsB;AAiB7B;;uDAEuD,GACvD,OAAO,IAAI,YAAY,CAAC,IAAY;EAClC,IAAI,QAAQ,KAAK,KAAK,CAAC,OAAO,KAAK,GAAG,QAAQ,KAAK,KAAK,CAAC,OAAO,KAAK;EACrE,IAAI,QAAQ,OAAO,KAAK,IAAI;EAC5B,IACE,CAAC,OAAO,QAAQ,CAAC,UAAU,CAAC,OAAO,QAAQ,CAAC,UAAU,SAAS,SAC/D,CAAC,OAAO,aAAa,CAAC,UAAU,QAAQ,GACxC;IACA,MAAM,IAAI,WACR;EAEJ;EACA,IAAI,SAAS,IAAI,IAAI,WAAW,IAAI,GAAG,CAAC,CAAC,IAA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AAI,IAAI,YAC7C,GAAG,KAAK,CAAC,OAAO;IAChB,MAAM;MAAC,IAAI;KAAM;IACjB,MAAM,MAAM;IACZ,OAAO,GAAG;MAAE;IAAI;EAClB,MACE,EAAE;EACN,IAAI,CAAC,KAAK;IACR,OAAO;MAAE;MAAK,QAAQ;MAAO,YAAY;MAAO,QAAQ,EAAE;MAAE,SAAS;IAAE;EACzE;EACA,IAAI,SAAS,OAAO,IAAI;IAAC;GAAI,EAAE,OAAO,KAAK,IAAI;EAC/C,OAAO;IACL;IACA,QAAQ;IACR,YAAY,OAAO,QAAQ,CAAC;IAC5B,QAAQ,OAAO,KAAK,CAAC,GAAG;IACxB,SAAS,KAAK,GAAG,CAAC,GAAG,OAAO,MAAM,GAAG;EACvC;AACF,EAAC"}
package/inspect.ts ADDED
@@ -0,0 +1,164 @@
1
+ // Bounded physical facts about a SQLite store. The caller sees names and
2
+ // counts, never definitions, statement text, or row values.
3
+
4
+ import { mainTables } from './physical.js'
5
+ import { heldBy } from './archetype.js'
6
+ import { ledger } from './ledger.js'
7
+ import { populations } from './population.js'
8
+ import {
9
+ by,
10
+ col,
11
+ type Driver,
12
+ eq,
13
+ gt,
14
+ join,
15
+ lit,
16
+ select,
17
+ table,
18
+ } from '@yaks/sql'
19
+
20
+ export type DeletionWindow = {
21
+ since: string
22
+ until: string
23
+ after?: number
24
+ /** name each grave in the window, its eid and when it was laid */
25
+ eids?: boolean
26
+ }
27
+ export type Deletions = {
28
+ scanned: number
29
+ count: number
30
+ minutes: Record<string, number>
31
+ next: number | null
32
+ graves?: { eid: string; at: string }[]
33
+ }
34
+
35
+ /** Count deletions in [since, until), reading at most 1,000 graves from the
36
+ * primary key. Continue with `next` until null; timestamps need no new index.
37
+ * With `eids`, each grave in the window is named too. */
38
+ export let deletions = (db: Driver, window: DeletionWindow): Deletions => {
39
+ let since = Date.parse(window.since), until = Date.parse(window.until)
40
+ let after = window.after ?? 0
41
+ if (
42
+ !Number.isFinite(since) || !Number.isFinite(until) || since >= until ||
43
+ !Number.isSafeInteger(after) || after < 0
44
+ ) {
45
+ throw new RangeError(
46
+ 'deletions needs since < until and a nonnegative cursor',
47
+ )
48
+ }
49
+ let tables = new Set(mainTables(db).map((r) => r.name))
50
+ let rows = !tables.has('tombstone') || (window.eids && !tables.has('entity'))
51
+ ? []
52
+ : db.query(select({
53
+ cols: [
54
+ col('entity', 't'),
55
+ col('deleted_at', 't'),
56
+ ...window.eids ? [col('eid', 'e')] : [],
57
+ ],
58
+ from: table('tombstone', 't'),
59
+ joins: window.eids
60
+ ? [join(table('entity', 'e'), eq(col('id', 'e'), col('entity', 't')))]
61
+ : [],
62
+ where: gt(col('entity', 't'), lit(after)),
63
+ order: [col('entity', 't')],
64
+ limit: lit(1000),
65
+ }))
66
+ let minutes: Record<string, number> = {}, count = 0
67
+ let graves: { eid: string; at: string }[] = []
68
+ for (let row of rows) {
69
+ let at = Date.parse(String(row.deleted_at))
70
+ if (!(at >= since && at < until)) continue
71
+ let minute = new Date(Math.floor(at / 60_000) * 60_000).toISOString()
72
+ minutes[minute] = (minutes[minute] ?? 0) + 1
73
+ count++
74
+ if (window.eids) {
75
+ graves.push({ eid: String(row.eid), at: new Date(at).toISOString() })
76
+ }
77
+ }
78
+ return {
79
+ scanned: rows.length,
80
+ count,
81
+ minutes,
82
+ next: rows.length == 1000 ? Number(rows.at(-1)!.entity) : null,
83
+ ...window.eids ? { graves } : {},
84
+ }
85
+ }
86
+
87
+ export type TableSize = {
88
+ name: string
89
+ /** Exact live owners for components, unknown for physical infrastructure. */
90
+ rows: number | null
91
+ indexCount: number
92
+ indexes: string[]
93
+ }
94
+
95
+ export type StoreSize = {
96
+ tables: number
97
+ shown: TableSize[]
98
+ omitted: number
99
+ }
100
+
101
+ /** The ordinary tables and indexes the file holds, including tables no
102
+ * longer named by the current vocabulary. Counting is capped so a diagnostic
103
+ * remains bounded even after a store has seen many schemas. */
104
+ export let inspect = (
105
+ db: Driver,
106
+ limit = 160,
107
+ preferred: readonly string[] = [],
108
+ ): StoreSize => {
109
+ let first = new Set(['entity', 'journal', 'yak_writes', ...preferred])
110
+ let names = mainTables(db).filter((r) => r.type == 'table').map((r) => r.name)
111
+ .sort((a, b) =>
112
+ Number(first.has(b)) - Number(first.has(a)) ||
113
+ a.localeCompare(b)
114
+ )
115
+ let buckets = names.includes('entity') && names.includes('archetype')
116
+ ? populations(db)
117
+ : undefined
118
+ let shown = names.slice(0, Math.max(0, Math.min(limit, 160))).map((name) => {
119
+ let indexes = db.query({ t: 'pragma', name: 'index_list', arg: name })
120
+ .map((r) => String(r.name))
121
+ return {
122
+ name,
123
+ rows: buckets && (name == 'entity' || buckets.some((b) =>
124
+ b.tables.includes(name)
125
+ ))
126
+ ? buckets.reduce((n, b) =>
127
+ n + (name == 'entity' || b.tables.includes(name) ? b.n : 0), 0)
128
+ : null,
129
+ indexCount: indexes.length,
130
+ indexes: indexes.slice(0, 16),
131
+ }
132
+ })
133
+ return { tables: names.length, shown, omitted: names.length - shown.length }
134
+ }
135
+
136
+ export type Identity = {
137
+ eid: string
138
+ exists: boolean
139
+ tombstoned: boolean
140
+ tables: string[]
141
+ omitted: number
142
+ }
143
+
144
+ /** Identity and its archetype table set, never component values. */
145
+ export let identity = (db: Driver, eid: string): Identity => {
146
+ let [row] = mainTables(db).some((r) => r.name == 'entity')
147
+ ? db.query(select({
148
+ cols: [col('id')],
149
+ from: table('entity'),
150
+ where: by({ eid }),
151
+ }))
152
+ : []
153
+ if (!row) {
154
+ return { eid, exists: false, tombstoned: false, tables: [], omitted: 0 }
155
+ }
156
+ let tables = heldBy(db, [eid], ledger(db)).sort()
157
+ return {
158
+ eid,
159
+ exists: true,
160
+ tombstoned: tables.includes('tombstone'),
161
+ tables: tables.slice(0, 160),
162
+ omitted: Math.max(0, tables.length - 160),
163
+ }
164
+ }
package/jsonb.js ADDED
@@ -0,0 +1,73 @@
1
+ // A property holding a JSON value — @yaks/vocab's `jsonb` scalar, declared
2
+ // `object`, `array` or a union of types — kept as SQLite's binary JSON. This
3
+ // file is the one place that says how such a value crosses the boundary, so
4
+ // every SQLite-shaped adapter (this package, @yaks/d1, a Durable Object's)
5
+ // writes and reads it the same way:
6
+ //
7
+ // in the value's JSON text, bound through `jsonb(?)`
8
+ // out `json(col)`, read as text and parsed here
9
+ //
10
+ // The `|| ''` on the way out is deliberate. `json()` marks its result with
11
+ // SQLite's JSON subtype, and a driver that honors the subtype (@db/sqlite)
12
+ // parses the value itself, so a JSON string would come back as the text it
13
+ // holds and be parsed a second time here. Concatenation drops the subtype, and
14
+ // every engine then returns the same text.
15
+ //
16
+ // `decoded` is also where a boolean comes back. SQLite has no boolean type, so
17
+ // a boolean property stores as the 0/1 of an integer column (./write.ts), and
18
+ // reads back as `false`/`true`, the type its vocabulary declares. `projected`
19
+ // reads a `.fields` projection in a query's raw rows the same way, so a value
20
+ // comes back as one type whichever door asked for it.
21
+ import { fn, lit, op } from "@yaks/sql/core";
22
+ /** Whether `comp.prop` holds a JSON value. */ export let isJsonb = (v, comp, prop)=>v.prop(comp, prop)?.scalar == 'jsonb';
23
+ /** A JSON value as the parameter `jsonb(?)` binds; null clears the column. */ export let jsonIn = (value)=>value == null ? null : JSON.stringify(value);
24
+ /** A stored JSON value read back as its JSON text. */ export let jsonOut = (e)=>op('||', fn('json', e), lit(''));
25
+ /** A JSON value's text as what a JSON column stores. */ export let jsonb = (e)=>fn('jsonb', e);
26
+ // One value as read: a JSON column's text parsed back into its value, a
27
+ // boolean's 0/1 as `false`/`true`, anything else as stored.
28
+ let value = (v, comp, prop, raw)=>{
29
+ let scalar = v.prop(comp, prop)?.scalar;
30
+ return typeof raw == 'string' && scalar == 'jsonb' ? JSON.parse(raw) : raw != null && scalar == 'bool' ? !!Number(raw) : raw;
31
+ };
32
+ /** A component row as read: absent SQL columns are omitted, each JSON column
33
+ * is parsed back into its value, and booleans read as `true`/`false`. A null
34
+ * patch clears a property; it is not a stored property value. */ export let decoded = (v, comp, row)=>{
35
+ for (let [k, raw] of Object.entries(row)){
36
+ if (raw == null) delete row[k];
37
+ else row[k] = value(v, comp, k, raw);
38
+ }
39
+ return row;
40
+ };
41
+ // The property a projected column's path ends on, kept per vocabulary: every
42
+ // row of an answer names the same few.
43
+ let leaves = new WeakMap();
44
+ let leafOf = (v, path)=>{
45
+ let known = leaves.get(v);
46
+ if (!known) leaves.set(v, known = new Map());
47
+ if (!known.has(path)) {
48
+ let hops = [];
49
+ try {
50
+ hops = v.aim(path);
51
+ } catch {}
52
+ known.set(path, hops.at(-1) ?? null);
53
+ }
54
+ return known.get(path);
55
+ };
56
+ /** A row of a query's raw answer, each `.fields` projection read as
57
+ * {@link decoded} reads it in a component. A projected column is named by its
58
+ * path (`comp.prop`, or one through references, `review.book.book.price`),
59
+ * and read as the property it ends on. A `.distinct` or `.tally` row's `value`
60
+ * is read as the property the query counts (`counted`, a boolean as
61
+ * `true`/`false`); the rest of the row (`eid`, `n`) is left as the statement
62
+ * returned it. */ export let projected = (v, row, counted)=>{
63
+ let agg = counted?.clauses.find((c)=>c.kind == 'distinct' || c.kind == 'tally');
64
+ for (let [k, raw] of Object.entries(row)){
65
+ let leaf = k.includes('.') ? leafOf(v, k) : k == 'value' && agg ? leafOf(v, agg.path.join('.')) : null;
66
+ if (leaf) {
67
+ ;
68
+ row[k] = value(v, leaf.comp, leaf.prop, raw);
69
+ }
70
+ }
71
+ return row;
72
+ };
73
+ //# sourceMappingURL=jsonb.js.map
package/jsonb.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonb.js","sources":["./jsonb.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,6EAA6E;AAC7E,4EAA4E;AAC5E,2EAA2E;AAC3E,oCAAoC;AACpC,EAAE;AACF,0DAA0D;AAC1D,oDAAoD;AACpD,EAAE;AACF,2EAA2E;AAC3E,2EAA2E;AAC3E,2EAA2E;AAC3E,+EAA+E;AAC/E,2CAA2C;AAC3C,EAAE;AACF,+EAA+E;AAC/E,8EAA8E;AAC9E,8EAA8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E,GAAG,CAAC;IACf,EAAE,OAAM,CAAkD;IAC1D,MAAM,GAAG,CAAC,MAAM,KAAK,EAAE,CAAC,CAAC,MAAM;EACjC;EACA,OAAO,MAAM,GAAG,CAAC;AACnB;AAEA;;;;;;gBAMgB,GAChB,OAAO,IAAI,YAAY,CACrB,GACA,KACA;EAEA,IAAI,MAAM,SAAS,QAAQ,KAAK,CAAC,IAC/B,EAAE,IAAI,IAAI,cAAc,EAAE,IAAI,IAAI;EAEpC,KAAK,IAAI,CAAC,GAAG,IAAI,IAAI,OAAO,OAAO,CAAC,KAAM;IACxC,IAAI,OAAO,EAAE,QAAQ,CAAC,OAClB,OAAO,GAAG,KACV,KAAK,WAAW,MAChB,OAAO,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,QACxB;IACJ,IAAI,MAAM;;MACN,GAA+B,CAAC,EAAE,GAAG,MAAM,GAAG,KAAK,IAAI,EAAE,KAAK,IAAI,EAAE;IACxE;EACF;EACA,OAAO;AACT,EAAC"}
package/jsonb.ts ADDED
@@ -0,0 +1,108 @@
1
+ // A property holding a JSON value — @yaks/vocab's `jsonb` scalar, declared
2
+ // `object`, `array` or a union of types — kept as SQLite's binary JSON. This
3
+ // file is the one place that says how such a value crosses the boundary, so
4
+ // every SQLite-shaped adapter (this package, @yaks/d1, a Durable Object's)
5
+ // writes and reads it the same way:
6
+ //
7
+ // in the value's JSON text, bound through `jsonb(?)`
8
+ // out `json(col)`, read as text and parsed here
9
+ //
10
+ // The `|| ''` on the way out is deliberate. `json()` marks its result with
11
+ // SQLite's JSON subtype, and a driver that honors the subtype (@db/sqlite)
12
+ // parses the value itself, so a JSON string would come back as the text it
13
+ // holds and be parsed a second time here. Concatenation drops the subtype, and
14
+ // every engine then returns the same text.
15
+ //
16
+ // `decoded` is also where a boolean comes back. SQLite has no boolean type, so
17
+ // a boolean property stores as the 0/1 of an integer column (./write.ts), and
18
+ // reads back as `false`/`true`, the type its vocabulary declares. `projected`
19
+ // reads a `.fields` projection in a query's raw rows the same way, so a value
20
+ // comes back as one type whichever door asked for it.
21
+
22
+ import type { And } from 'jsr:@yaks/query@0.2.36'
23
+ import type { Hop, Vocab } from 'jsr:@yaks/vocab@0.2.36'
24
+ import { type Expr, fn, lit, op, type Param } from '@yaks/sql/core'
25
+
26
+ /** Whether `comp.prop` holds a JSON value. */
27
+ export let isJsonb = (v: Vocab, comp: string, prop: string): boolean =>
28
+ v.prop(comp, prop)?.scalar == 'jsonb'
29
+
30
+ /** A JSON value as the parameter `jsonb(?)` binds; null clears the column. */
31
+ export let jsonIn = (value: unknown): Param =>
32
+ value == null ? null : JSON.stringify(value)
33
+
34
+ /** A stored JSON value read back as its JSON text. */
35
+ export let jsonOut = (e: Expr): Expr => op('||', fn('json', e), lit(''))
36
+
37
+ /** A JSON value's text as what a JSON column stores. */
38
+ export let jsonb = (e: Expr): Expr => fn('jsonb', e)
39
+
40
+ // One value as read: a JSON column's text parsed back into its value, a
41
+ // boolean's 0/1 as `false`/`true`, anything else as stored.
42
+ let value = (v: Vocab, comp: string, prop: string, raw: unknown): unknown => {
43
+ let scalar = v.prop(comp, prop)?.scalar
44
+ return typeof raw == 'string' && scalar == 'jsonb'
45
+ ? JSON.parse(raw)
46
+ : raw != null && scalar == 'bool'
47
+ ? !!Number(raw)
48
+ : raw
49
+ }
50
+
51
+ /** A component row as read: absent SQL columns are omitted, each JSON column
52
+ * is parsed back into its value, and booleans read as `true`/`false`. A null
53
+ * patch clears a property; it is not a stored property value. */
54
+ export let decoded = <R extends Record<string, unknown>>(
55
+ v: Vocab,
56
+ comp: string,
57
+ row: R,
58
+ ): R => {
59
+ for (let [k, raw] of Object.entries(row)) {
60
+ if (raw == null) delete row[k]
61
+ else (row as Record<string, unknown>)[k] = value(v, comp, k, raw)
62
+ }
63
+ return row
64
+ }
65
+
66
+ // The property a projected column's path ends on, kept per vocabulary: every
67
+ // row of an answer names the same few.
68
+ let leaves = new WeakMap<Vocab, Map<string, Hop | null>>()
69
+ let leafOf = (v: Vocab, path: string): Hop | null => {
70
+ let known = leaves.get(v)
71
+ if (!known) leaves.set(v, known = new Map())
72
+ if (!known.has(path)) {
73
+ let hops: Hop[] = []
74
+ try {
75
+ hops = v.aim(path)
76
+ } catch { /* a column no path names is left as it came */ }
77
+ known.set(path, hops.at(-1) ?? null)
78
+ }
79
+ return known.get(path)!
80
+ }
81
+
82
+ /** A row of a query's raw answer, each `.fields` projection read as
83
+ * {@link decoded} reads it in a component. A projected column is named by its
84
+ * path (`comp.prop`, or one through references, `review.book.book.price`),
85
+ * and read as the property it ends on. A `.distinct` or `.tally` row's `value`
86
+ * is read as the property the query counts (`counted`, a boolean as
87
+ * `true`/`false`); the rest of the row (`eid`, `n`) is left as the statement
88
+ * returned it. */
89
+ export let projected = <R extends Record<string, unknown>>(
90
+ v: Vocab,
91
+ row: R,
92
+ counted?: And,
93
+ ): R => {
94
+ let agg = counted?.clauses.find((c) =>
95
+ c.kind == 'distinct' || c.kind == 'tally'
96
+ ) as { path: string[] } | undefined
97
+ for (let [k, raw] of Object.entries(row)) {
98
+ let leaf = k.includes('.')
99
+ ? leafOf(v, k)
100
+ : k == 'value' && agg
101
+ ? leafOf(v, agg.path.join('.'))
102
+ : null
103
+ if (leaf) {
104
+ ;(row as Record<string, unknown>)[k] = value(v, leaf.comp, leaf.prop, raw)
105
+ }
106
+ }
107
+ return row
108
+ }
package/layout.js ADDED
@@ -0,0 +1,22 @@
1
+ // Names in the stored layout, shared by reading and schema installation.
2
+ /**
3
+ * The key/value table's name. Named `server_meta`, not `meta`, because a
4
+ * vocabulary may well declare a `meta` component and the two must never collide
5
+ * — and because that is the name the fleet's live table already carries, so
6
+ * installing over one adopts it instead of creating a second.
7
+ */ export let META = 'server_meta';
8
+ export let BUILDING = '_yaks_refit_';
9
+ let tableNames = new WeakMap();
10
+ /** The components that have a table of their own: every one but the spine
11
+ * and the computed ones, whose rows another package keeps (@yaks/sql
12
+ * `Backing`). */ export let tables = (vocab)=>{
13
+ let names = tableNames.get(vocab);
14
+ if (!names) {
15
+ names = vocab.all.filter((name)=>name != 'entity' && !vocab.comp(name)?.computed);
16
+ tableNames.set(vocab, names);
17
+ }
18
+ return [
19
+ ...names
20
+ ];
21
+ };
22
+ //# sourceMappingURL=layout.js.map
package/layout.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"layout.js","sources":["./layout.ts"],"names":[],"mappings":"AAAA,yEAAyE;AAGzE;;;;;CAKC,GACD,OAAO,IAAI,OAAO,cAAa;AAC/B,OAAO,IAAI,WAAW,eAAc;AAEpC,IAAI,aAAa,IAAI;AACrB;;eAEe,GACf,OAAO,IAAI,SAAS,CAAC;EACnB,IAAI,QAAQ,WAAW,GAAG,CAAC;EAC3B,IAAI,CAAC,OAAO;IACV,QAAQ,MAAM,GAAG,CAAC,MAAM,CAAC,CAAC,OACxB,QAAQ,YAAY,CAAC,MAAM,IAAI,CAAC,OAAO;IAEzC,WAAW,GAAG,CAAC,OAAO;EACxB;EACA,OAAO;OAAI;GAAM;AACnB,EAAC"}
package/layout.ts ADDED
@@ -0,0 +1,26 @@
1
+ // Names in the stored layout, shared by reading and schema installation.
2
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
3
+
4
+ /**
5
+ * The key/value table's name. Named `server_meta`, not `meta`, because a
6
+ * vocabulary may well declare a `meta` component and the two must never collide
7
+ * — and because that is the name the fleet's live table already carries, so
8
+ * installing over one adopts it instead of creating a second.
9
+ */
10
+ export let META = 'server_meta'
11
+ export let BUILDING = '_yaks_refit_'
12
+
13
+ let tableNames = new WeakMap<Vocab, string[]>()
14
+ /** The components that have a table of their own: every one but the spine
15
+ * and the computed ones, whose rows another package keeps (@yaks/sql
16
+ * `Backing`). */
17
+ export let tables = (vocab: Vocab): string[] => {
18
+ let names = tableNames.get(vocab)
19
+ if (!names) {
20
+ names = vocab.all.filter((name) =>
21
+ name != 'entity' && !vocab.comp(name)?.computed
22
+ )
23
+ tableNames.set(vocab, names)
24
+ }
25
+ return [...names]
26
+ }