@pikku/kysely 0.13.23 → 0.13.27

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 (80) hide show
  1. package/CHANGELOG.md +248 -0
  2. package/dist/src/index.d.ts +4 -1
  3. package/dist/src/index.js +3 -1
  4. package/dist/src/kysely-analytics-service.d.ts +30 -0
  5. package/dist/src/kysely-analytics-service.js +80 -0
  6. package/dist/src/kysely-feature-flag-store.d.ts +37 -0
  7. package/dist/src/kysely-feature-flag-store.js +239 -0
  8. package/dist/src/kysely-tables.d.ts +45 -0
  9. package/dist/src/kysely-webhook-service.d.ts +1 -1
  10. package/dist/src/kysely-webhook-service.js +1 -1
  11. package/dist/src/schema/analytics.schema.d.ts +17 -0
  12. package/dist/src/schema/analytics.schema.js +48 -0
  13. package/dist/src/schema/flag.schema.d.ts +16 -0
  14. package/dist/src/schema/flag.schema.js +62 -0
  15. package/dist/src/schema/index.d.ts +2 -0
  16. package/dist/src/schema/index.js +6 -0
  17. package/package.json +11 -6
  18. package/run-tests.sh +9 -1
  19. package/bin/pikku-kysely-pure.ts +0 -59
  20. package/dist/src/kysely-lock-vault.d.ts +0 -34
  21. package/dist/src/kysely-lock-vault.js +0 -77
  22. package/dist/src/schema/data-lock.schema.d.ts +0 -15
  23. package/dist/src/schema/data-lock.schema.js +0 -27
  24. package/dist/tsconfig.tsbuildinfo +0 -1
  25. package/src/classification-crypto.test.ts +0 -143
  26. package/src/classification-crypto.ts +0 -163
  27. package/src/classification-plugin.pglite.test.ts +0 -226
  28. package/src/classification-plugin.test.ts +0 -265
  29. package/src/classification-plugin.ts +0 -249
  30. package/src/coercion-plugin.test.ts +0 -160
  31. package/src/coercion-plugin.ts +0 -187
  32. package/src/create-audited-kysely.test.ts +0 -560
  33. package/src/create-audited-kysely.ts +0 -254
  34. package/src/index.ts +0 -81
  35. package/src/kysely-agent-run-scores.ts +0 -47
  36. package/src/kysely-agent-run-service.ts +0 -244
  37. package/src/kysely-agent-run-state-service.ts +0 -200
  38. package/src/kysely-agent-storage-service.ts +0 -632
  39. package/src/kysely-audit-service.test.ts +0 -352
  40. package/src/kysely-audit-service.ts +0 -237
  41. package/src/kysely-channel-store.ts +0 -107
  42. package/src/kysely-credential-service.ts +0 -286
  43. package/src/kysely-deployment-service.ts +0 -163
  44. package/src/kysely-eventhub-store.ts +0 -53
  45. package/src/kysely-json.ts +0 -5
  46. package/src/kysely-scope-service.test.ts +0 -418
  47. package/src/kysely-scope-service.ts +0 -425
  48. package/src/kysely-secret-service.ts +0 -242
  49. package/src/kysely-services.test.ts +0 -405
  50. package/src/kysely-session-store.ts +0 -57
  51. package/src/kysely-tables.test.ts +0 -110
  52. package/src/kysely-tables.ts +0 -434
  53. package/src/kysely-virtual-user-run-store.test.ts +0 -369
  54. package/src/kysely-virtual-user-run-store.ts +0 -235
  55. package/src/kysely-virtual-user-schedule-store.test.ts +0 -187
  56. package/src/kysely-virtual-user-schedule-store.ts +0 -187
  57. package/src/kysely-webhook-service.test.ts +0 -124
  58. package/src/kysely-webhook-service.ts +0 -137
  59. package/src/kysely-workflow-mirror.test.ts +0 -259
  60. package/src/kysely-workflow-mirror.ts +0 -374
  61. package/src/kysely-workflow-run-service.ts +0 -305
  62. package/src/kysely-workflow-service.test.ts +0 -544
  63. package/src/kysely-workflow-service.ts +0 -820
  64. package/src/schema/agent.schema.ts +0 -162
  65. package/src/schema/audit.schema.ts +0 -57
  66. package/src/schema/channel.schema.ts +0 -38
  67. package/src/schema/credential.schema.ts +0 -58
  68. package/src/schema/deployment.schema.ts +0 -47
  69. package/src/schema/index.ts +0 -441
  70. package/src/schema/pikku-schema.types.ts +0 -169
  71. package/src/schema/schema.test.ts +0 -591
  72. package/src/schema/scope.schema.ts +0 -110
  73. package/src/schema/secret.schema.ts +0 -49
  74. package/src/schema/session.schema.ts +0 -20
  75. package/src/schema/virtual-user-schedule.schema.ts +0 -48
  76. package/src/schema/virtual-user.schema.ts +0 -106
  77. package/src/schema/webhook.schema.ts +0 -58
  78. package/src/schema/workflow.schema.ts +0 -150
  79. package/src/serialize-plugin.ts +0 -233
  80. package/tsconfig.json +0 -17
@@ -1,187 +0,0 @@
1
- import type { KyselyPlugin, RootOperationNode, UnknownRow } from 'kysely'
2
-
3
- /**
4
- * The kinds a column can be coerced *to* at runtime. This is the value type of
5
- * the generated `CoercionMap`, so it is a public artifact contract — a kind may
6
- * only appear here if `fromDb` below actually does something with it.
7
- *
8
- * `uuid` is deliberately absent: it is an annotation kind (it selects the `Uuid`
9
- * TypeScript type in the generated schema) but not a coercion kind, and the
10
- * codegen filters it out of `coercion.gen.ts` for exactly that reason — a UUID
11
- * is a string in both Postgres and SQLite.
12
- */
13
- export type ColumnKind = 'date' | 'bool' | 'json'
14
-
15
- /**
16
- * Per-table column kind map. Keys are the table names queries are written
17
- * against; inner keys are snake_case column names.
18
- * Generated into `outDir/db/coercion.gen.ts` by `pikku db migrate` from the
19
- * explicit `kind` entries in `db/annotations.ts`.
20
- */
21
- export type CoercionMap = Record<string, Record<string, ColumnKind>>
22
-
23
- export interface CreateCoercionPluginOptions {
24
- map: CoercionMap
25
- }
26
-
27
- function fromDb(value: unknown, kind: ColumnKind): unknown {
28
- if (value == null) return value
29
- switch (kind) {
30
- case 'date':
31
- if (typeof value === 'string') {
32
- const d = new Date(value)
33
- return Number.isNaN(d.getTime()) ? value : d
34
- }
35
- return value
36
- case 'bool':
37
- if (typeof value === 'number') return value !== 0
38
- if (typeof value === 'bigint') return value !== 0n
39
- return value
40
- case 'json':
41
- if (typeof value !== 'string') return value
42
- try {
43
- return JSON.parse(value)
44
- } catch {
45
- return value
46
- }
47
- }
48
- }
49
-
50
- function snakeToCamel(name: string): string {
51
- return name.replace(/_([a-z])/g, (_, c) => c.toUpperCase())
52
- }
53
-
54
- interface ResolvedMaps {
55
- /** `table.column` → kind, for the exact per-query resolution. */
56
- qualified: Record<string, ColumnKind>
57
- /** `column` → kind, the fallback for when the query's tables say nothing. */
58
- flat: Record<string, ColumnKind>
59
- }
60
-
61
- /**
62
- * Flatten the per-table map into a table-qualified lookup plus a bare
63
- * column-name fallback. Both are indexed by BOTH snake_case and camelCase so
64
- * they work regardless of CamelCasePlugin ordering in the plugin array.
65
- *
66
- * When two tables disagree on the kind for the same column name, the bare name
67
- * is ambiguous (a joined/aliased query could carry either) and is dropped from
68
- * the flat map rather than letting whichever table was processed last win. The
69
- * qualified map still resolves it whenever the query names its tables.
70
- */
71
- function buildMaps(map: CoercionMap): ResolvedMaps {
72
- const qualified: Record<string, ColumnKind> = {}
73
- const flat: Record<string, ColumnKind> = {}
74
- const ambiguous = new Set<string>()
75
- const assignFlat = (key: string, kind: ColumnKind) => {
76
- const existing = flat[key]
77
- if (existing !== undefined && existing !== kind) {
78
- ambiguous.add(key)
79
- return
80
- }
81
- flat[key] = kind
82
- }
83
- for (const [table, cols] of Object.entries(map)) {
84
- for (const [col, kind] of Object.entries(cols)) {
85
- const camel = snakeToCamel(col)
86
- qualified[`${table}.${col}`] = kind
87
- qualified[`${table}.${camel}`] = kind
88
- assignFlat(col, kind)
89
- assignFlat(camel, kind)
90
- }
91
- }
92
- for (const key of ambiguous) delete flat[key]
93
- return { qualified, flat }
94
- }
95
-
96
- /**
97
- * Collect every table named anywhere in the query's operation node, so a result
98
- * column can be resolved against the tables the query actually touched instead
99
- * of against every table in the schema.
100
- */
101
- function collectQueryTables(node: unknown, out: Set<string>): void {
102
- if (!node || typeof node !== 'object') return
103
-
104
- const op = node as {
105
- kind?: string
106
- table?: { identifier?: { name?: string } }
107
- }
108
- if (op.kind === 'TableNode') {
109
- const tableName = op.table?.identifier?.name
110
- if (typeof tableName === 'string' && tableName.length > 0)
111
- out.add(tableName)
112
- }
113
-
114
- for (const value of Object.values(node)) {
115
- if (Array.isArray(value)) {
116
- for (const item of value) collectQueryTables(item, out)
117
- } else {
118
- collectQueryTables(value, out)
119
- }
120
- }
121
- }
122
-
123
- /**
124
- * Resolve a result column's kind against the query's tables, falling back to
125
- * the bare column name. Tables that disagree with each other are as ambiguous
126
- * as two schema tables are, so they fall back too.
127
- */
128
- function lookupKind(
129
- maps: ResolvedMaps,
130
- tables: readonly string[],
131
- col: string
132
- ): ColumnKind | undefined {
133
- let matchedKind: ColumnKind | undefined
134
- for (const table of tables) {
135
- const kind = maps.qualified[`${table}.${col}`]
136
- if (!kind) continue
137
- if (matchedKind && matchedKind !== kind) return maps.flat[col]
138
- matchedKind = kind
139
- }
140
- return matchedKind ?? maps.flat[col]
141
- }
142
-
143
- /**
144
- * Convert values stored in a dialect's on-disk representation back into the
145
- * logical types app code expects (Date / boolean / parsed JSON).
146
- *
147
- * SQLite is what makes this necessary — it stores dates as TEXT, booleans as
148
- * INTEGER and JSON as TEXT — but the plugin is dialect-neutral: on Postgres the
149
- * driver already returns Date/boolean/object, and every branch of `fromDb`
150
- * passes a non-string, non-number value straight through. That is why it lives
151
- * in `@pikku/kysely` next to `SerializePlugin` rather than in a SQLite package:
152
- * the CLI installs it on its local Postgres database too.
153
- *
154
- * Write-side coercion (Date → ISO string, boolean → 0/1, object → JSON) is
155
- * handled in the SQLite database adapters (always-on), so this plugin is
156
- * read-only.
157
- *
158
- * Place AFTER CamelCasePlugin in the plugin array (or it handles both
159
- * orderings via the dual-keyed maps).
160
- */
161
- export function createCoercionPlugin(
162
- options: CreateCoercionPluginOptions
163
- ): KyselyPlugin {
164
- const maps = buildMaps(options.map)
165
- const queryTables = new WeakMap<object, readonly string[]>()
166
- return {
167
- transformQuery(args) {
168
- const tables = new Set<string>()
169
- collectQueryTables(args.node as RootOperationNode, tables)
170
- queryTables.set(args.queryId, [...tables])
171
- return args.node
172
- },
173
- async transformResult(args) {
174
- const tables = queryTables.get(args.queryId) ?? []
175
- const out: UnknownRow[] = []
176
- for (const row of args.result.rows as UnknownRow[]) {
177
- const next: UnknownRow = { ...row }
178
- for (const [col, val] of Object.entries(row)) {
179
- const kind = lookupKind(maps, tables, col)
180
- if (kind) next[col] = fromDb(val, kind)
181
- }
182
- out.push(next)
183
- }
184
- return { ...args.result, rows: out }
185
- },
186
- }
187
- }