@async/db 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (398) hide show
  1. package/CHANGELOG.md +167 -0
  2. package/README.md +431 -0
  3. package/SPEC.md +1429 -0
  4. package/db.config.example.mjs +128 -0
  5. package/dist/cli/args.d.ts +8 -0
  6. package/dist/cli/args.js +16 -0
  7. package/dist/cli/commands/create.d.ts +3 -0
  8. package/dist/cli/commands/create.js +13 -0
  9. package/dist/cli/commands/doctor.d.ts +3 -0
  10. package/dist/cli/commands/doctor.js +31 -0
  11. package/dist/cli/commands/generate.d.ts +6 -0
  12. package/dist/cli/commands/generate.js +24 -0
  13. package/dist/cli/commands/operations.d.ts +12 -0
  14. package/dist/cli/commands/operations.js +61 -0
  15. package/dist/cli/commands/schema.d.ts +11 -0
  16. package/dist/cli/commands/schema.js +1086 -0
  17. package/dist/cli/commands/serve.d.ts +9 -0
  18. package/dist/cli/commands/serve.js +18 -0
  19. package/dist/cli/commands/sync.d.ts +3 -0
  20. package/dist/cli/commands/sync.js +11 -0
  21. package/dist/cli/commands/types.d.ts +7 -0
  22. package/dist/cli/commands/types.js +37 -0
  23. package/dist/cli/commands/viewer.d.ts +6 -0
  24. package/dist/cli/commands/viewer.js +29 -0
  25. package/dist/cli/index.d.ts +2 -0
  26. package/dist/cli/index.js +108 -0
  27. package/dist/cli/output.d.ts +25 -0
  28. package/dist/cli/output.js +149 -0
  29. package/dist/cli/schema-prompt.d.ts +20 -0
  30. package/dist/cli/schema-prompt.js +66 -0
  31. package/dist/cli.d.ts +2 -0
  32. package/dist/cli.js +3 -0
  33. package/dist/client-cache.d.ts +105 -0
  34. package/dist/client-cache.js +916 -0
  35. package/dist/client.d.ts +64 -0
  36. package/dist/client.js +405 -0
  37. package/dist/config-public.d.ts +1 -0
  38. package/dist/config-public.js +1 -0
  39. package/dist/config.d.ts +54 -0
  40. package/dist/config.js +2 -0
  41. package/dist/csv.d.ts +1 -0
  42. package/dist/csv.js +1 -0
  43. package/dist/db.d.ts +3 -0
  44. package/dist/db.js +3 -0
  45. package/dist/doctor.d.ts +1 -0
  46. package/dist/doctor.js +1 -0
  47. package/dist/errors.d.ts +1 -0
  48. package/dist/errors.js +1 -0
  49. package/dist/features/config/defaults.d.ts +98 -0
  50. package/dist/features/config/defaults.js +95 -0
  51. package/dist/features/config/load.d.ts +11 -0
  52. package/dist/features/config/load.js +265 -0
  53. package/dist/features/config/public.d.ts +17 -0
  54. package/dist/features/config/public.js +75 -0
  55. package/dist/features/doctor/duplicate-ids.d.ts +18 -0
  56. package/dist/features/doctor/duplicate-ids.js +79 -0
  57. package/dist/features/doctor/field-consistency.d.ts +17 -0
  58. package/dist/features/doctor/field-consistency.js +48 -0
  59. package/dist/features/doctor/index.d.ts +39 -0
  60. package/dist/features/doctor/index.js +177 -0
  61. package/dist/features/doctor/relations.d.ts +22 -0
  62. package/dist/features/doctor/relations.js +90 -0
  63. package/dist/features/doctor/schema-guidance.d.ts +35 -0
  64. package/dist/features/doctor/schema-guidance.js +184 -0
  65. package/dist/features/generate/registry.d.ts +14 -0
  66. package/dist/features/generate/registry.js +37 -0
  67. package/dist/features/http/registry.d.ts +46 -0
  68. package/dist/features/http/registry.js +86 -0
  69. package/dist/features/operations/index.d.ts +49 -0
  70. package/dist/features/operations/index.js +199 -0
  71. package/dist/features/operations/maps.d.ts +1 -0
  72. package/dist/features/operations/maps.js +10 -0
  73. package/dist/features/operations/readiness.d.ts +30 -0
  74. package/dist/features/operations/readiness.js +228 -0
  75. package/dist/features/operations/runtime.d.ts +57 -0
  76. package/dist/features/operations/runtime.js +288 -0
  77. package/dist/features/runtime/collection.d.ts +51 -0
  78. package/dist/features/runtime/collection.js +198 -0
  79. package/dist/features/runtime/db.d.ts +152 -0
  80. package/dist/features/runtime/db.js +824 -0
  81. package/dist/features/runtime/document.d.ts +43 -0
  82. package/dist/features/runtime/document.js +111 -0
  83. package/dist/features/runtime/fanout.d.ts +24 -0
  84. package/dist/features/runtime/fanout.js +77 -0
  85. package/dist/features/runtime/json-pointer.d.ts +5 -0
  86. package/dist/features/runtime/json-pointer.js +49 -0
  87. package/dist/features/runtime/scope-state.d.ts +44 -0
  88. package/dist/features/runtime/scope-state.js +185 -0
  89. package/dist/features/runtime/state.d.ts +1 -0
  90. package/dist/features/runtime/state.js +1 -0
  91. package/dist/features/schema/api.d.ts +107 -0
  92. package/dist/features/schema/api.js +460 -0
  93. package/dist/features/schema/builders.d.ts +86 -0
  94. package/dist/features/schema/builders.js +110 -0
  95. package/dist/features/schema/fields.d.ts +38 -0
  96. package/dist/features/schema/fields.js +296 -0
  97. package/dist/features/schema/generated.d.ts +29 -0
  98. package/dist/features/schema/generated.js +32 -0
  99. package/dist/features/schema/locator.d.ts +16 -0
  100. package/dist/features/schema/locator.js +135 -0
  101. package/dist/features/schema/manifest.d.ts +91 -0
  102. package/dist/features/schema/manifest.js +384 -0
  103. package/dist/features/schema/metadata.d.ts +30 -0
  104. package/dist/features/schema/metadata.js +75 -0
  105. package/dist/features/schema/project.d.ts +46 -0
  106. package/dist/features/schema/project.js +442 -0
  107. package/dist/features/schema/relations.d.ts +38 -0
  108. package/dist/features/schema/relations.js +109 -0
  109. package/dist/features/schema/resolvers.d.ts +36 -0
  110. package/dist/features/schema/resolvers.js +111 -0
  111. package/dist/features/schema/resource.d.ts +75 -0
  112. package/dist/features/schema/resource.js +253 -0
  113. package/dist/features/schema/source-definitions.d.ts +21 -0
  114. package/dist/features/schema/source-definitions.js +29 -0
  115. package/dist/features/schema/sources.d.ts +83 -0
  116. package/dist/features/schema/sources.js +689 -0
  117. package/dist/features/schema/standard-schema.d.ts +57 -0
  118. package/dist/features/schema/standard-schema.js +232 -0
  119. package/dist/features/schema/validation.d.ts +69 -0
  120. package/dist/features/schema/validation.js +434 -0
  121. package/dist/features/storage/events.d.ts +12 -0
  122. package/dist/features/storage/events.js +30 -0
  123. package/dist/features/storage/json.d.ts +112 -0
  124. package/dist/features/storage/json.js +239 -0
  125. package/dist/features/storage/memory.d.ts +30 -0
  126. package/dist/features/storage/memory.js +44 -0
  127. package/dist/features/storage/resource-json.d.ts +31 -0
  128. package/dist/features/storage/resource-json.js +76 -0
  129. package/dist/features/storage/runtime.d.ts +37 -0
  130. package/dist/features/storage/runtime.js +184 -0
  131. package/dist/features/storage/source-metadata.d.ts +20 -0
  132. package/dist/features/storage/source-metadata.js +25 -0
  133. package/dist/features/storage/source.d.ts +37 -0
  134. package/dist/features/storage/source.js +60 -0
  135. package/dist/features/storage/static.d.ts +29 -0
  136. package/dist/features/storage/static.js +42 -0
  137. package/dist/features/sync/defaults.d.ts +21 -0
  138. package/dist/features/sync/defaults.js +21 -0
  139. package/dist/features/sync/index.d.ts +35 -0
  140. package/dist/features/sync/index.js +85 -0
  141. package/dist/features/sync/mirror-state.d.ts +14 -0
  142. package/dist/features/sync/mirror-state.js +4 -0
  143. package/dist/features/sync/runtime-dirs.d.ts +5 -0
  144. package/dist/features/sync/runtime-dirs.js +9 -0
  145. package/dist/features/sync/source-writes.d.ts +15 -0
  146. package/dist/features/sync/source-writes.js +27 -0
  147. package/dist/features/sync/synthetic-seed.d.ts +26 -0
  148. package/dist/features/sync/synthetic-seed.js +83 -0
  149. package/dist/features/viewer/manifest.d.ts +148 -0
  150. package/dist/features/viewer/manifest.js +165 -0
  151. package/dist/fs-utils.d.ts +1 -0
  152. package/dist/fs-utils.js +1 -0
  153. package/dist/generate/hono/app.d.ts +6 -0
  154. package/dist/generate/hono/app.js +51 -0
  155. package/dist/generate/hono/graphql.d.ts +7 -0
  156. package/dist/generate/hono/graphql.js +53 -0
  157. package/dist/generate/hono/index.d.ts +55 -0
  158. package/dist/generate/hono/index.js +140 -0
  159. package/dist/generate/hono/package.d.ts +6 -0
  160. package/dist/generate/hono/package.js +44 -0
  161. package/dist/generate/hono/readme.d.ts +13 -0
  162. package/dist/generate/hono/readme.js +28 -0
  163. package/dist/generate/hono/repository.d.ts +1 -0
  164. package/dist/generate/hono/repository.js +27 -0
  165. package/dist/generate/hono/rest.d.ts +1 -0
  166. package/dist/generate/hono/rest.js +38 -0
  167. package/dist/generate/hono/schema.d.ts +13 -0
  168. package/dist/generate/hono/schema.js +18 -0
  169. package/dist/generate/hono/sqlite.d.ts +20 -0
  170. package/dist/generate/hono/sqlite.js +266 -0
  171. package/dist/generate/hono/validators.d.ts +1 -0
  172. package/dist/generate/hono/validators.js +141 -0
  173. package/dist/generate/hono.d.ts +1 -0
  174. package/dist/generate/hono.js +1 -0
  175. package/dist/graphql/execute.d.ts +14 -0
  176. package/dist/graphql/execute.js +719 -0
  177. package/dist/graphql/http.d.ts +15 -0
  178. package/dist/graphql/http.js +29 -0
  179. package/dist/graphql/index.d.ts +3 -0
  180. package/dist/graphql/index.js +3 -0
  181. package/dist/graphql/parser.d.ts +54 -0
  182. package/dist/graphql/parser.js +433 -0
  183. package/dist/hono.d.ts +77 -0
  184. package/dist/hono.js +1 -0
  185. package/dist/index.d.ts +1065 -0
  186. package/dist/index.js +14 -0
  187. package/dist/integrations/hono.d.ts +136 -0
  188. package/dist/integrations/hono.js +508 -0
  189. package/dist/integrations/kv.d.ts +69 -0
  190. package/dist/integrations/kv.js +69 -0
  191. package/dist/integrations/postgres.d.ts +52 -0
  192. package/dist/integrations/postgres.js +113 -0
  193. package/dist/integrations/sqlite.d.ts +112 -0
  194. package/dist/integrations/sqlite.js +489 -0
  195. package/dist/integrations/vite.d.ts +45 -0
  196. package/dist/integrations/vite.js +111 -0
  197. package/dist/json.d.ts +48 -0
  198. package/dist/json.js +1 -0
  199. package/dist/jsonc.d.ts +1 -0
  200. package/dist/jsonc.js +1 -0
  201. package/dist/kv.d.ts +24 -0
  202. package/dist/kv.js +1 -0
  203. package/dist/mock.d.ts +1 -0
  204. package/dist/mock.js +1 -0
  205. package/dist/names.d.ts +1 -0
  206. package/dist/names.js +1 -0
  207. package/dist/operations.d.ts +3 -0
  208. package/dist/operations.js +3 -0
  209. package/dist/postgres.d.ts +24 -0
  210. package/dist/postgres.js +1 -0
  211. package/dist/redis.d.ts +14 -0
  212. package/dist/redis.js +1 -0
  213. package/dist/rest/formats.d.ts +80 -0
  214. package/dist/rest/formats.js +318 -0
  215. package/dist/rest/handler.d.ts +111 -0
  216. package/dist/rest/handler.js +833 -0
  217. package/dist/rest/shape.d.ts +33 -0
  218. package/dist/rest/shape.js +218 -0
  219. package/dist/schema-builders.d.ts +1 -0
  220. package/dist/schema-builders.js +1 -0
  221. package/dist/schema-manifest.d.ts +1 -0
  222. package/dist/schema-manifest.js +1 -0
  223. package/dist/schema.d.ts +193 -0
  224. package/dist/schema.js +6 -0
  225. package/dist/server.d.ts +116 -0
  226. package/dist/server.js +601 -0
  227. package/dist/shared/csv.d.ts +8 -0
  228. package/dist/shared/csv.js +149 -0
  229. package/dist/shared/errors.d.ts +40 -0
  230. package/dist/shared/errors.js +55 -0
  231. package/dist/shared/fs-utils.d.ts +4 -0
  232. package/dist/shared/fs-utils.js +30 -0
  233. package/dist/shared/jsonc.d.ts +2 -0
  234. package/dist/shared/jsonc.js +99 -0
  235. package/dist/shared/mock.d.ts +40 -0
  236. package/dist/shared/mock.js +83 -0
  237. package/dist/shared/names.d.ts +28 -0
  238. package/dist/shared/names.js +127 -0
  239. package/dist/shared/operations.d.ts +32 -0
  240. package/dist/shared/operations.js +302 -0
  241. package/dist/sqlite.d.ts +24 -0
  242. package/dist/sqlite.js +1 -0
  243. package/dist/state.d.ts +1 -0
  244. package/dist/state.js +1 -0
  245. package/dist/sync.d.ts +1 -0
  246. package/dist/sync.js +1 -0
  247. package/dist/tracing.d.ts +95 -0
  248. package/dist/tracing.js +260 -0
  249. package/dist/types.d.ts +51 -0
  250. package/dist/types.js +285 -0
  251. package/dist/viewer-manifest.d.ts +1 -0
  252. package/dist/viewer-manifest.js +1 -0
  253. package/dist/vite.d.ts +59 -0
  254. package/dist/vite.js +1 -0
  255. package/dist/web/json-viewer.d.ts +5 -0
  256. package/dist/web/json-viewer.js +176 -0
  257. package/dist/web/viewer.d.ts +12 -0
  258. package/dist/web/viewer.js +1015 -0
  259. package/docs/README.md +42 -0
  260. package/docs/architecture.md +112 -0
  261. package/docs/ci-and-release.md +177 -0
  262. package/docs/cms-storage-patterns.md +108 -0
  263. package/docs/concepts.md +141 -0
  264. package/docs/configuration.md +552 -0
  265. package/docs/fixtures-and-schemas.md +527 -0
  266. package/docs/fork-branch-workflows.md +108 -0
  267. package/docs/generated-files.md +174 -0
  268. package/docs/getting-started.md +165 -0
  269. package/docs/integrations.md +206 -0
  270. package/docs/json-production.md +120 -0
  271. package/docs/package-api.md +418 -0
  272. package/docs/prototype-to-production.md +378 -0
  273. package/docs/server-and-viewer.md +466 -0
  274. package/docs/store-graduation.md +120 -0
  275. package/docs/typescript-schema-sources.md +79 -0
  276. package/examples/advanced/README.md +55 -0
  277. package/examples/advanced/db/projects.schema.jsonc +44 -0
  278. package/examples/advanced/db/settings.jsonc +9 -0
  279. package/examples/advanced/db/users.json +23 -0
  280. package/examples/advanced/db/users.schema.mjs +31 -0
  281. package/examples/advanced/db.config.mjs +18 -0
  282. package/examples/advanced/example.json +5 -0
  283. package/examples/advanced/src/generated/db.types.d.ts +64 -0
  284. package/examples/basic/README.md +95 -0
  285. package/examples/basic/db/operations/get-user.jsonc +8 -0
  286. package/examples/basic/db/settings.json +7 -0
  287. package/examples/basic/db/users.schema.jsonc +36 -0
  288. package/examples/basic/db.config.mjs +68 -0
  289. package/examples/basic/example.json +5 -0
  290. package/examples/basic/src/generated/db.types.d.ts +39 -0
  291. package/examples/cms-json-publish/README.md +21 -0
  292. package/examples/cms-json-publish/db/navigation.json +7 -0
  293. package/examples/cms-json-publish/db/pages.json +18 -0
  294. package/examples/cms-json-publish/example.json +5 -0
  295. package/examples/cms-json-publish/src/cms.mjs +104 -0
  296. package/examples/computed-fields/README.md +93 -0
  297. package/examples/computed-fields/db/orders.schema.mjs +62 -0
  298. package/examples/computed-fields/db/posts.schema.mjs +59 -0
  299. package/examples/computed-fields/db/products.schema.mjs +39 -0
  300. package/examples/computed-fields/db/users.schema.mjs +43 -0
  301. package/examples/computed-fields/db.config.mjs +15 -0
  302. package/examples/computed-fields/example.json +5 -0
  303. package/examples/computed-fields/src/generated/db.types.d.ts +81 -0
  304. package/examples/content-collections/README.md +91 -0
  305. package/examples/content-collections/db/authors.json +12 -0
  306. package/examples/content-collections/db/authors.schema.mjs +20 -0
  307. package/examples/content-collections/db/blog/draft-roadmap.mdx +12 -0
  308. package/examples/content-collections/db/blog/index.schema.mjs +61 -0
  309. package/examples/content-collections/db/blog/launch-notes.mdx +15 -0
  310. package/examples/content-collections/db/docs/index.schema.mjs +32 -0
  311. package/examples/content-collections/db/docs/intro.mdx +11 -0
  312. package/examples/content-collections/db/docs/schema-workflow.mdx +10 -0
  313. package/examples/content-collections/db/site.schema.jsonc +21 -0
  314. package/examples/content-collections/db.config.mjs +26 -0
  315. package/examples/content-collections/example.json +5 -0
  316. package/examples/content-collections/src/content-preview.mjs +66 -0
  317. package/examples/content-collections/src/generated/db.types.d.ts +81 -0
  318. package/examples/csv/README.md +52 -0
  319. package/examples/csv/db/customers.csv +4 -0
  320. package/examples/csv/db.config.mjs +13 -0
  321. package/examples/csv/example.json +5 -0
  322. package/examples/data-first/README.md +54 -0
  323. package/examples/data-first/db/posts.json +16 -0
  324. package/examples/data-first/db/settings.json +8 -0
  325. package/examples/data-first/db/users.json +14 -0
  326. package/examples/data-first/db.config.mjs +13 -0
  327. package/examples/data-first/example.json +5 -0
  328. package/examples/diagnostics/README.md +55 -0
  329. package/examples/diagnostics/db/projects.schema.jsonc +27 -0
  330. package/examples/diagnostics/db/users.json +9 -0
  331. package/examples/diagnostics/db/users.schema.jsonc +23 -0
  332. package/examples/diagnostics/db.config.mjs +16 -0
  333. package/examples/diagnostics/example.json +5 -0
  334. package/examples/free-plan-upgrade/README.md +22 -0
  335. package/examples/free-plan-upgrade/db/appSettings.json +4 -0
  336. package/examples/free-plan-upgrade/db/projects.json +7 -0
  337. package/examples/free-plan-upgrade/example.json +5 -0
  338. package/examples/free-plan-upgrade/src/upgrade-tenant-to-paid.mjs +105 -0
  339. package/examples/hono-auth/README.md +74 -0
  340. package/examples/hono-auth/db/pages.schema.jsonc +44 -0
  341. package/examples/hono-auth/db/users.schema.jsonc +42 -0
  342. package/examples/hono-auth/db.config.mjs +17 -0
  343. package/examples/hono-auth/example.json +5 -0
  344. package/examples/hono-auth/package.json +14 -0
  345. package/examples/hono-auth/src/app.mjs +79 -0
  346. package/examples/hono-auth/src/server.mjs +13 -0
  347. package/examples/production-json/README.md +102 -0
  348. package/examples/production-json/db/appSettings.schema.jsonc +41 -0
  349. package/examples/production-json/db/featureFlags.schema.jsonc +84 -0
  350. package/examples/production-json/db/operations/get-control-plane.jsonc +6 -0
  351. package/examples/production-json/db/operations/get-feature-flag.jsonc +9 -0
  352. package/examples/production-json/db/operations/list-feature-flags.jsonc +8 -0
  353. package/examples/production-json/db/operations/read-public-settings.jsonc +8 -0
  354. package/examples/production-json/db.config.mjs +33 -0
  355. package/examples/production-json/example.json +5 -0
  356. package/examples/production-json/src/client-demo.mjs +28 -0
  357. package/examples/production-json/src/generated/db.types.d.ts +60 -0
  358. package/examples/relations/README.md +56 -0
  359. package/examples/relations/db/posts.schema.jsonc +46 -0
  360. package/examples/relations/db/users.schema.jsonc +34 -0
  361. package/examples/relations/db.config.mjs +13 -0
  362. package/examples/relations/example.json +5 -0
  363. package/examples/rest-client/README.md +54 -0
  364. package/examples/rest-client/db/settings.json +5 -0
  365. package/examples/rest-client/db/users.schema.jsonc +42 -0
  366. package/examples/rest-client/db.config.mjs +13 -0
  367. package/examples/rest-client/example.json +5 -0
  368. package/examples/rest-client/src/client-demo.mjs +24 -0
  369. package/examples/schema-first/README.md +55 -0
  370. package/examples/schema-first/db/auditEvents.schema.jsonc +24 -0
  371. package/examples/schema-first/db/settings.schema.jsonc +29 -0
  372. package/examples/schema-first/db/users.schema.jsonc +36 -0
  373. package/examples/schema-first/db.config.mjs +15 -0
  374. package/examples/schema-first/example.json +5 -0
  375. package/examples/schema-first/src/generated/db.types.d.ts +47 -0
  376. package/examples/schema-manifest/README.md +50 -0
  377. package/examples/schema-manifest/db/projects.schema.jsonc +48 -0
  378. package/examples/schema-manifest/db/users.schema.jsonc +35 -0
  379. package/examples/schema-manifest/db.config.mjs +41 -0
  380. package/examples/schema-manifest/example.json +5 -0
  381. package/examples/schema-manifest/src/generated/db.schema.json +130 -0
  382. package/examples/schema-manifest/src/generated/db.types.d.ts +50 -0
  383. package/examples/schema-ui/README.md +103 -0
  384. package/examples/schema-ui/db/pages.schema.jsonc +53 -0
  385. package/examples/schema-ui/db/users.schema.jsonc +30 -0
  386. package/examples/schema-ui/db.config.mjs +55 -0
  387. package/examples/schema-ui/example.json +5 -0
  388. package/examples/schema-ui/src/cms-ssr.mjs +276 -0
  389. package/examples/schema-ui/src/generated/db.schema.json +133 -0
  390. package/examples/schema-ui/src/generated/db.types.d.ts +46 -0
  391. package/examples/schema-ui/src/render-admin.mjs +175 -0
  392. package/examples/schema-ui/src/schema-ui-ssr-handler.mjs +149 -0
  393. package/examples/schema-ui/src/start-schema-ui-server.mjs +140 -0
  394. package/examples/standard-schema/README.md +55 -0
  395. package/examples/standard-schema/db/settings.schema.mjs +22 -0
  396. package/examples/standard-schema/db/users.schema.mjs +72 -0
  397. package/examples/standard-schema/example.json +5 -0
  398. package/package.json +108 -0
@@ -0,0 +1,1065 @@
1
+ import type { IncomingMessage, Server, ServerResponse } from 'node:http';
2
+
3
+ export type DbTypeMap = {
4
+ collections: Record<string, unknown>;
5
+ documents: Record<string, unknown>;
6
+ };
7
+
8
+ export type DbSchemaLoadMode = 'schema' | 'data' | 'runtime';
9
+
10
+ export type DbSchemaLocator = {
11
+ cwd: string;
12
+ sourceDir: string;
13
+ mode: 'project' | 'source-dir' | 'root-schema' | 'schema-file';
14
+ file: string | null;
15
+ baseDir: string;
16
+ resourceName: string | null;
17
+ };
18
+
19
+ export type DbSchemaValidatorMode = 'create' | 'replace' | 'patch';
20
+
21
+ export type DbSchemaValidatorUnknownFields = 'error' | 'strip' | 'allow' | 'warn' | 'ignore';
22
+
23
+ export type DbSchemaValidatorOptions = {
24
+ /** Validation shape. create allows an omitted collection id; patch allows missing required fields. */
25
+ mode?: DbSchemaValidatorMode;
26
+ /** How unknown input fields are handled. Defaults to "error" for schema validators. */
27
+ unknownFields?: DbSchemaValidatorUnknownFields;
28
+ /** Apply schema defaults in create mode. Defaults to true. */
29
+ applyDefaults?: boolean;
30
+ /** Human-readable source label used in diagnostics. */
31
+ source?: string;
32
+ };
33
+
34
+ export type DbSchemaValidationResult<TValue = unknown> = {
35
+ ok: boolean;
36
+ value: TValue;
37
+ diagnostics: Array<Record<string, unknown>>;
38
+ errors: Array<Record<string, unknown>>;
39
+ resource: string;
40
+ mode: DbSchemaValidatorMode;
41
+ };
42
+
43
+ export type DbSchemaValidator<TValue = Record<string, unknown>> = {
44
+ resource: string;
45
+ mode: DbSchemaValidatorMode;
46
+ unknownFields: Exclude<DbSchemaValidatorUnknownFields, 'ignore'>;
47
+ validate(value: unknown, options?: DbSchemaValidatorOptions): DbSchemaValidationResult<TValue>;
48
+ validateAsync(value: unknown, options?: DbSchemaValidatorOptions): Promise<DbSchemaValidationResult<TValue>>;
49
+ assert(value: unknown, options?: DbSchemaValidatorOptions): TValue;
50
+ assertAsync(value: unknown, options?: DbSchemaValidatorOptions): Promise<TValue>;
51
+ };
52
+
53
+ export type DbSchemaResolverContext = Record<string, unknown> | Map<string, unknown>;
54
+
55
+ export type DbSchemaResolverOptions = {
56
+ /** User-provided values exposed on resolver this. These override internal values with the same key. */
57
+ context?: DbSchemaResolverContext;
58
+ /** Shared cache exposed as this.cache and this.get("cache"). */
59
+ cache?: Map<string, unknown>;
60
+ /** Optional value exposed as this.value when no call-time record/value is provided. */
61
+ value?: unknown;
62
+ /** Optional db-like object exposed to resolver this. Runtime REST/GraphQL pass the live db. */
63
+ db?: unknown;
64
+ /** Optional service map exposed as this.services and individual this.get(name) entries. */
65
+ services?: Record<string, unknown>;
66
+ };
67
+
68
+ export type DbSchemaFieldResolver<TArgs = Record<string, unknown>, TValue = unknown> = ((args?: TArgs) => Promise<TValue>) & {
69
+ resolve?: (args?: TArgs) => Promise<TValue>;
70
+ resolveMany?: (args?: { records?: unknown[]; [key: string]: unknown } | unknown[]) => Promise<unknown>;
71
+ };
72
+
73
+ export type DbLoadedSchema = {
74
+ kind: 'DbSchema';
75
+ config: DbOptions;
76
+ loadMode: DbSchemaLoadMode;
77
+ locator: DbSchemaLocator | null;
78
+ rootSchema?: unknown;
79
+ resources: Map<string, Record<string, unknown>>;
80
+ diagnostics: Array<Record<string, unknown>>;
81
+ schema: Record<string, unknown>;
82
+ resource(name: string): Record<string, unknown>;
83
+ resourceNames(): string[];
84
+ validator<TValue = Record<string, unknown>>(name: string, options?: DbSchemaValidatorOptions): DbSchemaValidator<TValue>;
85
+ resolver<TArgs = Record<string, unknown>, TValue = unknown>(
86
+ selector: string,
87
+ options?: DbSchemaResolverOptions,
88
+ ): DbSchemaFieldResolver<TArgs, TValue> | Record<string, DbSchemaFieldResolver<TArgs, TValue>>;
89
+ validate<TValue = Record<string, unknown>>(name: string, value: unknown, options?: DbSchemaValidatorOptions): DbSchemaValidationResult<TValue>;
90
+ validateAsync<TValue = Record<string, unknown>>(name: string, value: unknown, options?: DbSchemaValidatorOptions): Promise<DbSchemaValidationResult<TValue>>;
91
+ assert<TValue = Record<string, unknown>>(name: string, value: unknown, options?: DbSchemaValidatorOptions): TValue;
92
+ assertAsync<TValue = Record<string, unknown>>(name: string, value: unknown, options?: DbSchemaValidatorOptions): Promise<TValue>;
93
+ toJSON(): Record<string, unknown>;
94
+ };
95
+
96
+ export type DbGeneratedTypesOptions = {
97
+ /** Generate TypeScript types during sync. */
98
+ enabled?: boolean;
99
+ /** Backwards-compatible alias for outputs.types. Defaults to "./.db/types/index.d.ts". */
100
+ outFile?: string | null;
101
+ /** Backwards-compatible alias for outputs.committedTypes. */
102
+ commitOutFile?: string | null;
103
+ /** Emit readonly object properties in generated types. */
104
+ useReadonly?: boolean;
105
+ /** Emit JSDoc from schema field descriptions. */
106
+ emitComments?: boolean;
107
+ /** Export DbCollections, DbDocuments, and DbTypes helpers. */
108
+ exportRuntimeHelpers?: boolean;
109
+ };
110
+
111
+ export type DbOutputOptions = {
112
+ /** Generated runtime output folder. Defaults to "./.db". */
113
+ stateDir?: string;
114
+ /** Gitignored generated TypeScript type output. Defaults to "./.db/types/index.d.ts". */
115
+ types?: string | null;
116
+ /** Optional committed TypeScript type copy for app/CI imports. */
117
+ committedTypes?: string | null;
118
+ /** Optional committed generated JSON schema manifest for admin/CMS UI generation. */
119
+ schemaManifest?: string | null;
120
+ /** Optional committed generated JSON viewer manifest for custom data UIs. */
121
+ viewerManifest?: string | null;
122
+ /** Optional full registered operation registry output path. */
123
+ operationRegistry?: string | null;
124
+ /** Optional client-safe registered operation refs output path. */
125
+ operationRefs?: string | null;
126
+ /** Output folder for generated Hono starter code. Defaults to "./db-api". */
127
+ honoStarterDir?: string;
128
+ };
129
+
130
+ export type DbSchemaManifestFieldContext = {
131
+ field: Record<string, unknown>;
132
+ fieldName: string;
133
+ resource: Record<string, unknown>;
134
+ resourceName: string;
135
+ path: string;
136
+ file: string | null;
137
+ sourceFile: string | null;
138
+ defaultManifest: Record<string, unknown>;
139
+ };
140
+
141
+ export type DbSchemaManifestResourceContext = {
142
+ resource: Record<string, unknown>;
143
+ resourceName: string;
144
+ file: string | null;
145
+ sourceFile: string | null;
146
+ defaultManifest: Record<string, unknown>;
147
+ };
148
+
149
+ export type DbSchemaManifestOptions = {
150
+ /** Customize generated resource manifest entries. */
151
+ customizeResource?: (context: DbSchemaManifestResourceContext) => Record<string, unknown>;
152
+ /** Customize or omit generated field manifest entries. Return null to omit a field. */
153
+ customizeField?: (context: DbSchemaManifestFieldContext) => Record<string, unknown> | null;
154
+ };
155
+
156
+ export type DbResourceNamingStrategy = 'basename' | 'folder-prefixed' | 'path';
157
+
158
+ export type DbResourceCustomizeContext = {
159
+ file: string;
160
+ sourceFile: string;
161
+ basename: string;
162
+ folder: string | null;
163
+ folders: string[];
164
+ extension: string;
165
+ defaultName: string;
166
+ defaultResource: {
167
+ name: string;
168
+ };
169
+ };
170
+
171
+ export type DbResourceOptions = {
172
+ /** How fixture paths become resource names. Defaults to "basename". */
173
+ naming?: DbResourceNamingStrategy;
174
+ /** Customize fixture path -> resource identity. */
175
+ customizeResource?: (context: DbResourceCustomizeContext) => { name?: string } | null | undefined;
176
+ /** Per-resource storage settings keyed by normalized resource name. */
177
+ [resourceName: string]: DbResourceNamingStrategy
178
+ | ((context: DbResourceCustomizeContext) => { name?: string } | null | undefined)
179
+ | DbPerResourceOptions
180
+ | undefined;
181
+ };
182
+
183
+ export type DbStoreName = 'json' | 'memory' | 'static' | 'sourceFile' | string;
184
+
185
+ export type DbStoreOptions = {
186
+ driver?: DbStoreName;
187
+ [key: string]: unknown;
188
+ };
189
+
190
+ export type DbCustomStore = {
191
+ name?: string;
192
+ capabilities?: DbRuntimeCapabilities;
193
+ statePath?: (resource: Record<string, unknown>) => string | undefined;
194
+ hydrate?: (resources: Array<Record<string, unknown>>) => void | Promise<void>;
195
+ readResource?: (resource: Record<string, unknown>, fallback: unknown) => unknown | Promise<unknown>;
196
+ writeResource?: (resource: Record<string, unknown>, value: unknown) => void | Promise<void>;
197
+ read?: (resource: Record<string, unknown>, fallback: unknown) => unknown | Promise<unknown>;
198
+ write?: (resource: Record<string, unknown>, value: unknown) => void | Promise<void>;
199
+ get?: (resource: Record<string, unknown>, fallback: unknown) => unknown | Promise<unknown>;
200
+ set?: (resource: Record<string, unknown>, value: unknown) => void | Promise<void>;
201
+ withResourceWrite?: <Result>(
202
+ resource: Record<string, unknown>,
203
+ operation: () => Result | Promise<Result>,
204
+ ) => Result | Promise<Result>;
205
+ close?: () => void | Promise<void>;
206
+ };
207
+
208
+ export type DbCustomStoreFactory =
209
+ (context: { config: DbOptions; resources: unknown[]; storeName: string }) => DbCustomStore;
210
+
211
+ export type DbStoresOptions = {
212
+ /** Default public store for resources without an explicit store. Defaults to "json". */
213
+ default?: DbStoreName;
214
+ /** Named public store definitions. */
215
+ [storeName: string]: DbStoreName | DbStoreOptions | DbCustomStore | DbCustomStoreFactory | undefined;
216
+ };
217
+
218
+ export type DbRuntimeCapabilities = {
219
+ writable?: boolean;
220
+ persistence?: 'local-file' | 'memory' | 'static' | 'remote' | string;
221
+ atomicity?: 'resource' | 'process' | 'none' | 'request' | string;
222
+ liveEvents?: boolean;
223
+ staticExport?: boolean;
224
+ production?: boolean | 'small-local' | 'small-app' | string;
225
+ };
226
+
227
+ export type DbRuntimeAdapter = {
228
+ name: string;
229
+ capabilities?: DbRuntimeCapabilities;
230
+ };
231
+
232
+ export type DbDoctorOptions = {
233
+ /** Include production-readiness diagnostics for JSON-backed resources. */
234
+ production?: boolean;
235
+ };
236
+
237
+ export type DbRuntimeAdapterFactory =
238
+ | DbRuntimeAdapter
239
+ | ((context: { config: DbOptions; resources: unknown[] }) => DbRuntimeAdapter);
240
+
241
+ export type DbPerResourceOptions = {
242
+ /** Public store name for this resource. Defaults to stores.default. */
243
+ store?: DbStoreName;
244
+ /** Query intent metadata for stores and diagnostics. */
245
+ indexes?: Array<{
246
+ fields: string[];
247
+ name?: string;
248
+ unique?: boolean;
249
+ }>;
250
+ };
251
+
252
+ export type DbResourceChangeEvent = {
253
+ version: number;
254
+ timestamp: string;
255
+ resource: string;
256
+ kind: 'collection' | 'document';
257
+ op: string;
258
+ id?: string | number;
259
+ pointer?: string;
260
+ };
261
+
262
+ export type DbRequestTracePhase = {
263
+ name: string;
264
+ durationMs: number;
265
+ [key: string]: unknown;
266
+ };
267
+
268
+ export type DbRequestTraceEvent = {
269
+ version: number;
270
+ timestamp: string;
271
+ type: 'request-trace';
272
+ requestId: string;
273
+ method: string;
274
+ pathname: string;
275
+ queryKeys: string[];
276
+ route?: string;
277
+ resource?: string;
278
+ operation?: string;
279
+ id?: string | number;
280
+ status?: number | null;
281
+ handled: boolean;
282
+ durationMs: number;
283
+ slow: boolean;
284
+ hook?: string;
285
+ shortCircuit?: boolean;
286
+ phases?: DbRequestTracePhase[];
287
+ error?: {
288
+ code?: string;
289
+ message: string;
290
+ };
291
+ };
292
+
293
+ export type DbRuntimeEvent = DbResourceChangeEvent | DbRequestTraceEvent;
294
+
295
+ export type DbRuntimeEvents = {
296
+ readonly version: number;
297
+ subscribe(subscriber: (event: DbRuntimeEvent) => void): () => void;
298
+ };
299
+
300
+ export type DbRouteExposure = 'open' | 'registered-only' | 'dev' | 'disabled' | false;
301
+
302
+ export type DbTraceConfig = {
303
+ /** Enable tracing. Object form defaults to enabled unless set to false. */
304
+ enabled?: boolean;
305
+ /** Mark traces slow at or above this duration in ms. Defaults to 0. */
306
+ slowMs?: number;
307
+ /** Print concise request summaries to the console. Defaults to true when tracing is enabled. */
308
+ console?: boolean;
309
+ /** Emit request trace events through db.events for /__db/log. Defaults to true when tracing is enabled. */
310
+ events?: boolean;
311
+ /** Response header used for the request id. Defaults to "x-async-db-request-id". */
312
+ header?: string;
313
+ };
314
+
315
+ export type DbTraceOptions = boolean | DbTraceConfig;
316
+
317
+ export type DbRestOperationTemplate = {
318
+ name?: string;
319
+ /** Callable operation identifier. Defaults to hashOperation(template) when omitted. */
320
+ ref?: string;
321
+ method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | string;
322
+ path: string;
323
+ query?: string | Record<string, unknown>;
324
+ body?: unknown;
325
+ variables?: Record<string, unknown>;
326
+ };
327
+
328
+ export type DbGraphqlOperationTemplate = {
329
+ name?: string;
330
+ /** Callable operation identifier. Defaults to hashOperation(template) when omitted. */
331
+ ref?: string;
332
+ query: string;
333
+ variables?: Record<string, unknown>;
334
+ operationName?: string | null;
335
+ };
336
+
337
+ export type DbOperationTemplate = string | DbRestOperationTemplate | DbGraphqlOperationTemplate;
338
+
339
+ export type DbOperationRef = {
340
+ name?: string;
341
+ /** Callable ref for client query(). */
342
+ ref: string;
343
+ };
344
+
345
+ export type DbRegisteredOperation = Exclude<DbOperationTemplate, string> & {
346
+ ref?: string;
347
+ };
348
+
349
+ export type DbOperationRegistryValue = DbOperationTemplate | DbRegisteredOperation;
350
+
351
+ export type DbOperationAcceptRefs = 'ref' | 'name' | 'both';
352
+
353
+ export type DbOperationRefContext = {
354
+ ref: string;
355
+ decodedRef: string;
356
+ acceptRefs: DbOperationAcceptRefs;
357
+ registry: Record<string, DbRegisteredOperation>;
358
+ operation: DbRegisteredOperation | null;
359
+ };
360
+
361
+ export type DbOperationResolveRef = (
362
+ ref: string,
363
+ context: Omit<DbOperationRefContext, 'operation'>,
364
+ ) => DbOperationRegistryValue | null | undefined | Promise<DbOperationRegistryValue | null | undefined>;
365
+
366
+ export type DbOperationValidateRef = (
367
+ context: DbOperationRefContext,
368
+ ) => boolean | null | undefined | DbOperationRegistryValue | Promise<boolean | null | undefined | DbOperationRegistryValue>;
369
+
370
+ export type DbOperationsOptions = {
371
+ /** Enable registered operation execution. Defaults to false. */
372
+ enabled?: boolean;
373
+ /** Folder containing operation source templates. Defaults to "./db/operations". */
374
+ sourceDir?: string;
375
+ /** Backwards-compatible alias for outputs.operationRegistry. */
376
+ outFile?: string | null;
377
+ /** Backwards-compatible alias for outputs.operationRefs. */
378
+ refsOutFile?: string | null;
379
+ /** Controls which default refs the server accepts. Defaults to "both". */
380
+ acceptRefs?: DbOperationAcceptRefs;
381
+ /** Custom server-side operation lookup for framework adapters or app registries. */
382
+ resolveRef?: DbOperationResolveRef;
383
+ /** Custom server-side validation or mapping for operation refs. */
384
+ validateRef?: DbOperationValidateRef;
385
+ /** Inline server registry keyed by operation ref or operation name. */
386
+ registry?: Record<string, DbOperationRegistryValue>;
387
+ };
388
+
389
+ export type DbOperationResult = {
390
+ kind: 'rest' | 'graphql';
391
+ status: number;
392
+ headers: Record<string, string>;
393
+ body: unknown;
394
+ rawBody?: string;
395
+ };
396
+
397
+ export type DbOperationRequestBody = {
398
+ variables?: Record<string, unknown>;
399
+ };
400
+
401
+ export type DbOperationHandler = {
402
+ enabled: boolean;
403
+ resolve(ref: string): Promise<DbRegisteredOperation | null | undefined>;
404
+ execute(ref: string, variables?: Record<string, unknown>): Promise<DbOperationResult>;
405
+ executeRequest(ref: string, body?: DbOperationRequestBody | null): Promise<DbOperationResult>;
406
+ };
407
+
408
+ export type DbSourceReaderContext = {
409
+ /** Repo-relative source path, such as "db/users.json". */
410
+ file: string;
411
+ /** Absolute source file path. */
412
+ sourceFile: string;
413
+ filename: string;
414
+ basename: string;
415
+ extension: string;
416
+ folder: string | null;
417
+ folders: string[];
418
+ /** SHA-256 hash of the source file bytes. */
419
+ hash: string;
420
+ config: DbOptions;
421
+ readText(): Promise<string>;
422
+ readBuffer(): Promise<Buffer>;
423
+ };
424
+
425
+ export type DbSourceReaderDataResult = {
426
+ kind: 'data';
427
+ data: unknown;
428
+ /** Format label stored in source metadata. Defaults to the reader name. */
429
+ format?: string;
430
+ /** Explicit resource name. Required when one file returns multiple sources. */
431
+ resourceName?: string;
432
+ };
433
+
434
+ export type DbSourceReaderSchemaResult = {
435
+ kind: 'schema';
436
+ schema: unknown;
437
+ /** Schema source label, such as "jsonc", "mjs", or a custom format. */
438
+ format?: string;
439
+ /** Explicit resource name. Required when one file returns multiple sources. */
440
+ resourceName?: string;
441
+ };
442
+
443
+ export type DbSourceReaderSingleResult = DbSourceReaderDataResult | DbSourceReaderSchemaResult;
444
+
445
+ export type DbSourceReaderResult =
446
+ | DbSourceReaderSingleResult
447
+ | Array<DbSourceReaderResult>
448
+ | null
449
+ | undefined;
450
+
451
+ export type DbSourceReader = {
452
+ name: string;
453
+ match(context: DbSourceReaderContext): boolean | Promise<boolean>;
454
+ read(context: DbSourceReaderContext): DbSourceReaderResult | Promise<DbSourceReaderResult>;
455
+ };
456
+
457
+ export type DbSourcesOptions = {
458
+ /** Custom source readers. They run before built-in JSON, JSONC, CSV, and executable schema readers. */
459
+ readers?: DbSourceReader[];
460
+ /** How db handles writes back to source fixtures. Defaults to "preserve". */
461
+ writePolicy?: 'preserve' | 'allow';
462
+ };
463
+
464
+ export type DbRestResourceFormatContext = {
465
+ db: unknown;
466
+ target?: 'resource';
467
+ resource: Record<string, unknown>;
468
+ resourceName: string;
469
+ data: unknown;
470
+ format: string;
471
+ request: IncomingMessage | Record<string, unknown>;
472
+ url: URL;
473
+ };
474
+
475
+ export type DbRestManifestFormatContext = {
476
+ db: unknown;
477
+ target?: 'manifest';
478
+ data: unknown;
479
+ manifest: unknown;
480
+ format: string;
481
+ request: IncomingMessage | Record<string, unknown>;
482
+ url: URL;
483
+ routes?: Record<string, string>;
484
+ };
485
+
486
+ export type DbRestFormatContext = DbRestResourceFormatContext;
487
+ export type DbRestAnyFormatContext = DbRestResourceFormatContext | DbRestManifestFormatContext;
488
+
489
+ export type DbRestFormatResult = string | Buffer | {
490
+ status?: number;
491
+ body?: string | Buffer;
492
+ contentType?: string;
493
+ headers?: Record<string, string>;
494
+ };
495
+
496
+ export type DbRestFormatRenderer = (context: DbRestFormatContext) => DbRestFormatResult | Promise<DbRestFormatResult>;
497
+ export type DbRestAnyFormatRenderer = (context: DbRestAnyFormatContext) => DbRestFormatResult | Promise<DbRestFormatResult>;
498
+ export type DbRestManifestFormatRenderer = (context: DbRestManifestFormatContext) => DbRestFormatResult | Promise<DbRestFormatResult>;
499
+
500
+ export type DbRestFormatDefinition = {
501
+ /** Media types used by extensionless Accept negotiation. */
502
+ mediaTypes?: string | string[];
503
+ /** Default response content type when the renderer returns a string or Buffer. */
504
+ contentType?: string;
505
+ /** Generic renderer used for resource and manifest responses unless a target-specific renderer is provided. */
506
+ render?: DbRestAnyFormatRenderer;
507
+ /** Renderer for REST resource routes such as /users.yaml. */
508
+ renderResource?: DbRestFormatRenderer;
509
+ /** Renderer for viewer manifest routes such as /__db/manifest.yaml. */
510
+ renderManifest?: DbRestManifestFormatRenderer;
511
+ };
512
+
513
+ export type DbSchemaConfig = {
514
+ /** Which inputs define schemas. "auto" uses schema files when present and otherwise infers from data. */
515
+ source?: 'auto' | 'data' | 'schema';
516
+ /** Allow JSONC source files. */
517
+ allowJsonc?: boolean;
518
+ /** Create db/package.json with "type": "module" for .schema.js files when the project root is not already ESM. */
519
+ autoModulePackageJson?: boolean;
520
+ /** Prefer Standard Schema-first generated .schema.mjs output for resources with validators. */
521
+ standardSchema?: boolean;
522
+ /** How schema-backed resources handle fields not declared by schema. */
523
+ unknownFields?: 'allow' | 'warn' | 'error';
524
+ /** Future migration policy for safe additive changes. */
525
+ additiveChanges?: 'auto' | 'manual';
526
+ /** Future migration policy for destructive changes. */
527
+ destructiveChanges?: 'manual';
528
+ /** Future migration policy for field type changes. */
529
+ typeChanges?: 'manual';
530
+ };
531
+
532
+ export type DbOptions = {
533
+ /** Project root used to resolve relative config paths. Defaults to process.cwd(). */
534
+ cwd?: string;
535
+ /** Package API locator for a project root, db folder, root schema file, or individual schema file. */
536
+ from?: string;
537
+ /** Schema loading mode. Defaults to "data" for current low-level loaders and "runtime" for openDb. */
538
+ load?: DbSchemaLoadMode;
539
+ /** Explicit config file path. Defaults to db.config.mjs/js lookup from cwd. */
540
+ configPath?: string;
541
+ /** Fixture source folder. Defaults to "./db". */
542
+ dbDir?: string;
543
+ /** Backwards-compatible fixture source folder alias. If set, it wins over dbDir. */
544
+ sourceDir?: string;
545
+ /** Backwards-compatible alias for outputs.stateDir. */
546
+ stateDir?: string;
547
+ /** Preferred generated output locations for state, types, manifests, operations, and generated starter code. */
548
+ outputs?: DbOutputOptions;
549
+ /** Backwards-compatible alias for outputs.schemaManifest. */
550
+ schemaOutFile?: string | null;
551
+ /** Backwards-compatible alias for outputs.viewerManifest. */
552
+ viewerManifestOutFile?: string | null;
553
+ /** Optional visitor hooks for customizing generated schema manifest output. */
554
+ schemaManifest?: DbSchemaManifestOptions;
555
+ /** Optional source readers for custom schema or data file formats. */
556
+ sources?: DbSourcesOptions;
557
+ /** Values made available to computed field resolvers through this.services and this.get(name). */
558
+ services?: Record<string, unknown>;
559
+ /** Run sync automatically when opening the package API. */
560
+ syncOnOpen?: boolean;
561
+ /** Keep valid resources available when one source file has diagnostics. */
562
+ allowSourceErrors?: boolean;
563
+ types?: DbGeneratedTypesOptions;
564
+ /** Schema and validation config. */
565
+ schema?: DbSchemaConfig;
566
+ defaults?: {
567
+ /** Apply schema defaults on create through package, REST, and GraphQL writes. */
568
+ applyOnCreate?: boolean;
569
+ /** Apply defaults during safe additive store hydration. */
570
+ applyOnSafeMigration?: boolean;
571
+ };
572
+ seed?: {
573
+ /** Generate mock runtime rows for schema-only resources with empty seed data. */
574
+ generateFromSchema?: boolean;
575
+ /** Number of mock rows to generate when generateFromSchema is true. */
576
+ generatedCount?: number;
577
+ };
578
+ /** Per-collection overrides such as custom id field names. */
579
+ collections?: Record<string, { idField?: string }>;
580
+ /** Resource naming and fixture path identity options. */
581
+ resources?: DbResourceOptions;
582
+ /** Public storage options. Defaults to the JSON store. */
583
+ stores?: DbStoresOptions;
584
+ /** Doctor and check command options. */
585
+ doctor?: DbDoctorOptions;
586
+ server?: {
587
+ /** Scoped base for local db dev tools. Defaults to "/__db". */
588
+ apiBase?: string;
589
+ /** App-facing REST data route alias. Defaults to "/db"; set false to disable. */
590
+ dataPath?: string | false;
591
+ /** Local HTTP host. Defaults to "127.0.0.1". */
592
+ host?: string;
593
+ /** Local HTTP port. Defaults to 7331. */
594
+ port?: number;
595
+ /** Maximum JSON request body size in bytes. Defaults to 1048576. */
596
+ maxBodyBytes?: number;
597
+ /** Opt-in request tracing and timing for handled db HTTP requests. */
598
+ trace?: DbTraceOptions;
599
+ /** Optional links to custom data viewers shown in discovery and the viewer manifest. */
600
+ viewerLinks?: Array<{
601
+ label?: string;
602
+ href: string;
603
+ }>;
604
+ /** Route exposure policy for hardened local/prod-like servers. */
605
+ expose?: {
606
+ rest?: DbRouteExposure;
607
+ graphql?: DbRouteExposure;
608
+ viewer?: DbRouteExposure;
609
+ schema?: DbRouteExposure;
610
+ manifest?: DbRouteExposure;
611
+ };
612
+ };
613
+ rest?: {
614
+ /** Enable generated REST routes. */
615
+ enabled?: boolean;
616
+ /** GET response formats by extension. "default" controls extensionless resource routes. */
617
+ formats?: Record<string, DbRestFormatRenderer | DbRestFormatDefinition | string | undefined>;
618
+ };
619
+ graphql?: {
620
+ /** Enable the focused dependency-free GraphQL endpoint. */
621
+ enabled?: boolean;
622
+ /** GraphQL HTTP path. Defaults to "/graphql". */
623
+ path?: string;
624
+ };
625
+ /** Optional registered REST operation settings. */
626
+ operations?: DbOperationsOptions;
627
+ mock?: {
628
+ /** Local response delay in ms, [minMs, maxMs], or an object range. Defaults to [30, 100]. Use 0 to disable. */
629
+ delay?: number | [number, number] | {
630
+ minMs?: number;
631
+ maxMs?: number;
632
+ min?: number;
633
+ max?: number;
634
+ } | null;
635
+ /** Random local error rate or detailed error settings. Defaults to no random errors. */
636
+ errors?: number | {
637
+ rate?: number;
638
+ probability?: number;
639
+ status?: number;
640
+ message?: string;
641
+ } | null;
642
+ };
643
+ generate?: {
644
+ hono?: {
645
+ /** Backwards-compatible alias for outputs.honoStarterDir. */
646
+ outDir?: string;
647
+ /** API modules to generate. */
648
+ api?: Array<'rest' | 'graphql'> | 'rest' | 'graphql' | 'rest,graphql' | 'none';
649
+ db?: 'sqlite';
650
+ app?: 'standalone' | 'module';
651
+ runtime?: 'node-sqlite';
652
+ /** Include fixture seed support in generated starter code. */
653
+ seed?: false | 'fixtures';
654
+ };
655
+ };
656
+ };
657
+
658
+ export type DbOpenOptions = Omit<DbOptions, 'schema'> & {
659
+ /** Schema config, or a loaded schema object returned by loadDbSchema(). */
660
+ schema?: DbSchemaConfig | DbLoadedSchema;
661
+ };
662
+
663
+ export type DbCollection<RecordType> = {
664
+ all(): Promise<RecordType[]>;
665
+ get(id: string): Promise<RecordType | null>;
666
+ exists(id: string): Promise<boolean>;
667
+ create(record: RecordType): Promise<RecordType>;
668
+ update(id: string, patch: Partial<RecordType>): Promise<RecordType | null>;
669
+ patch(id: string, patch: Partial<RecordType>): Promise<RecordType | null>;
670
+ delete(id: string): Promise<boolean>;
671
+ replaceAll(records: RecordType[]): Promise<RecordType[]>;
672
+ };
673
+
674
+ export type DbDocument<DocumentType> = {
675
+ all(): Promise<DocumentType>;
676
+ get(): Promise<DocumentType>;
677
+ get(pointer: string): Promise<unknown>;
678
+ put(value: DocumentType): Promise<DocumentType>;
679
+ set(pointer: string, value: unknown): Promise<unknown>;
680
+ update(patch: Partial<DocumentType>): Promise<DocumentType>;
681
+ };
682
+
683
+ export type DbForkSource =
684
+ | 'main'
685
+ | { fork?: string | null; branch: string }
686
+ | { fork?: string | null; snapshot: string };
687
+
688
+ export type DbForkCreateOptions = {
689
+ from?: DbForkSource;
690
+ metadata?: Record<string, unknown>;
691
+ };
692
+
693
+ export type DbBranchCreateOptions = {
694
+ from?: string;
695
+ metadata?: Record<string, unknown>;
696
+ };
697
+
698
+ export type DbSnapshotCreateOptions = {
699
+ label?: string;
700
+ resources?: string[];
701
+ };
702
+
703
+ export type DbSnapshotRestoreOptions = {
704
+ resources?: string[];
705
+ };
706
+
707
+ export type DbSnapshotResult = {
708
+ id: string;
709
+ label?: string;
710
+ fork: string | null;
711
+ branch: string;
712
+ resources: string[];
713
+ path: string;
714
+ };
715
+
716
+ export type DbMigrationStartOptions = {
717
+ resources: string[];
718
+ mode?: 'read-only';
719
+ };
720
+
721
+ export type DbMigrationLock = {
722
+ name: string;
723
+ resources: string[];
724
+ mode: 'read-only';
725
+ startedAt: string;
726
+ copies?: Record<string, {
727
+ resource: string;
728
+ from: string;
729
+ to: string;
730
+ copiedAt: string;
731
+ }>;
732
+ };
733
+
734
+ export type DbMigrationVerifyOptions = {
735
+ resources: string[];
736
+ checks?: Array<'count' | 'schema' | 'checksum'>;
737
+ };
738
+
739
+ export type DbResourceMigrateOptions = {
740
+ from: string;
741
+ to: string;
742
+ };
743
+
744
+ export type DbResourceRegistry = Map<string, unknown> & {
745
+ migrate(resource: string, options: DbResourceMigrateOptions): Promise<void>;
746
+ };
747
+
748
+ export type DbForkManager<Types extends DbTypeMap = DbTypeMap> = {
749
+ create(name: string, options?: DbForkCreateOptions): Promise<Db<Types>>;
750
+ open(name: string): Promise<Db<Types>>;
751
+ ensure(name: string, options?: DbForkCreateOptions): Promise<Db<Types>>;
752
+ list(): Promise<Array<Record<string, unknown>>>;
753
+ delete(name: string): Promise<boolean>;
754
+ };
755
+
756
+ export type DbBranchManager<Types extends DbTypeMap = DbTypeMap> = {
757
+ create(name: string, options?: DbBranchCreateOptions): Promise<Db<Types>>;
758
+ open(name: string): Promise<Db<Types>>;
759
+ ensure(name: string, options?: DbBranchCreateOptions): Promise<Db<Types>>;
760
+ list(): Promise<Array<Record<string, unknown>>>;
761
+ delete(name: string): Promise<boolean>;
762
+ };
763
+
764
+ export type Db<Types extends DbTypeMap = DbTypeMap> = {
765
+ events: DbRuntimeEvents;
766
+ resources: DbResourceRegistry;
767
+ forks: DbForkManager<Types>;
768
+ branches: DbBranchManager<Types>;
769
+ snapshots: {
770
+ create(options?: DbSnapshotCreateOptions): Promise<DbSnapshotResult>;
771
+ restore(id: string, options?: DbSnapshotRestoreOptions): Promise<void>;
772
+ };
773
+ migrations: {
774
+ start(name: string, options: DbMigrationStartOptions): Promise<DbMigrationLock>;
775
+ verify(name: string, options: DbMigrationVerifyOptions): Promise<void>;
776
+ finish(name: string): Promise<void>;
777
+ };
778
+ routing: {
779
+ set(routes: Record<string, string>): Promise<Record<string, string>>;
780
+ };
781
+ fork(name: string): Db<Types>;
782
+ branch(name: string): Db<Types>;
783
+ collection<Name extends keyof Types['collections'] & string>(name: Name): DbCollection<Types['collections'][Name]>;
784
+ document<Name extends keyof Types['documents'] & string>(name: Name): DbDocument<Types['documents'][Name]>;
785
+ operation(ref: string, variables?: Record<string, unknown>): Promise<unknown>;
786
+ query(ref: string, variables?: Record<string, unknown>): Promise<unknown>;
787
+ resourceNames(): string[];
788
+ close(): Promise<void>;
789
+ };
790
+
791
+ export type GraphqlRequest = {
792
+ query: string;
793
+ variables?: Record<string, unknown>;
794
+ operationName?: string | null;
795
+ };
796
+
797
+ export type GraphqlError = {
798
+ message: string;
799
+ extensions?: {
800
+ code?: string;
801
+ hint?: string;
802
+ details?: unknown;
803
+ };
804
+ };
805
+
806
+ export type GraphqlResult = {
807
+ data: unknown;
808
+ errors?: GraphqlError[];
809
+ };
810
+
811
+ export type RestBatchRequest = {
812
+ method?: string;
813
+ path: string;
814
+ body?: unknown;
815
+ };
816
+
817
+ export type RestBatchResult = {
818
+ status: number;
819
+ headers: Record<string, string>;
820
+ body: unknown;
821
+ };
822
+
823
+ export type DbCacheReadPolicy = 'cache-first' | 'cache-and-network' | 'network-first' | 'network-only' | 'cache-only';
824
+ export type DbCacheWritePolicy = 'merge-and-invalidate' | 'invalidate' | 'refetch';
825
+ export type DbCacheEventPolicy = 'invalidate' | 'refetch' | false;
826
+
827
+ export type DbCacheStorageContext = {
828
+ baseNamespace: string;
829
+ namespace: string;
830
+ manifestFingerprint: string | null;
831
+ };
832
+
833
+ export type DbCacheStorage = {
834
+ load?(context?: DbCacheStorageContext): unknown | Promise<unknown>;
835
+ save?(snapshot: unknown, context?: DbCacheStorageContext): void | Promise<void>;
836
+ clear?(context?: DbCacheStorageContext): void | Promise<void>;
837
+ };
838
+
839
+ export type DbCacheSnapshotQuery = {
840
+ key: string;
841
+ value: unknown;
842
+ stale: boolean;
843
+ resources: string[];
844
+ lists: string[];
845
+ };
846
+
847
+ export type DbClientCacheOptions = boolean | {
848
+ enabled?: boolean;
849
+ storage?: 'memory' | DbCacheStorage;
850
+ readPolicy?: DbCacheReadPolicy;
851
+ writePolicy?: DbCacheWritePolicy;
852
+ eventPolicy?: DbCacheEventPolicy;
853
+ /** Preloaded viewer manifest. When omitted, the client fetches <apiBase>/manifest.json on first cache use. */
854
+ manifest?: unknown;
855
+ };
856
+
857
+ export type DbClientCacheRequestOptions = false | DbCacheReadPolicy | {
858
+ readPolicy?: DbCacheReadPolicy;
859
+ };
860
+
861
+ export type DbCacheWatchRequest =
862
+ | {
863
+ kind?: 'rest';
864
+ method?: string;
865
+ path: string;
866
+ }
867
+ | ({
868
+ kind: 'graphql';
869
+ } & GraphqlRequest);
870
+
871
+ export type DbCacheSnapshot = {
872
+ data: unknown;
873
+ stale: boolean;
874
+ source: 'cache' | string;
875
+ };
876
+
877
+ export type DbClientCache = {
878
+ readonly enabled: boolean;
879
+ clear(): void;
880
+ invalidate(resourceName?: string): void;
881
+ snapshot(): {
882
+ namespace?: string;
883
+ manifestFingerprint?: string | null;
884
+ manifest: unknown;
885
+ queries: DbCacheSnapshotQuery[];
886
+ resources: Record<string, Record<string, unknown>>;
887
+ entities?: Record<string, Record<string, unknown>>;
888
+ };
889
+ watch(request: DbCacheWatchRequest, subscriber: (snapshot: DbCacheSnapshot) => void): () => void;
890
+ };
891
+
892
+ export type DbClientOptions = {
893
+ baseUrl?: string;
894
+ /** Scoped base for default batch, manifest, and operation paths. Defaults to "/__db". */
895
+ apiBase?: string;
896
+ restBasePath?: string;
897
+ graphqlPath?: string;
898
+ restBatchPath?: string;
899
+ manifestPath?: string;
900
+ cache?: DbClientCacheOptions;
901
+ batching?: boolean | {
902
+ enabled?: boolean;
903
+ delayMs?: number;
904
+ dedupe?: boolean | 'reads' | 'all';
905
+ };
906
+ };
907
+
908
+ export type DbClientRequestOptions = {
909
+ batch?: boolean;
910
+ cache?: DbClientCacheRequestOptions;
911
+ };
912
+
913
+ export type DbClient = {
914
+ cache: DbClientCache;
915
+ graphql: {
916
+ (query: string | GraphqlRequest, variables?: Record<string, unknown>, options?: DbClientRequestOptions): Promise<GraphqlResult>;
917
+ request(query: string | GraphqlRequest, variables?: Record<string, unknown>, options?: DbClientRequestOptions): Promise<GraphqlResult>;
918
+ batch(requests: GraphqlRequest[]): Promise<GraphqlResult[]>;
919
+ };
920
+ rest: {
921
+ (method: string | RestBatchRequest, path?: string, body?: unknown, options?: DbClientRequestOptions): Promise<RestBatchResult>;
922
+ request(method: string | RestBatchRequest, path?: string, body?: unknown, options?: DbClientRequestOptions): Promise<RestBatchResult>;
923
+ batch(requests: RestBatchRequest[]): Promise<RestBatchResult[]>;
924
+ get(path: string, options?: DbClientRequestOptions): Promise<RestBatchResult>;
925
+ post(path: string, body?: unknown, options?: DbClientRequestOptions): Promise<RestBatchResult>;
926
+ patch(path: string, body?: unknown, options?: DbClientRequestOptions): Promise<RestBatchResult>;
927
+ put(path: string, body?: unknown, options?: DbClientRequestOptions): Promise<RestBatchResult>;
928
+ delete(path: string, options?: DbClientRequestOptions): Promise<RestBatchResult>;
929
+ };
930
+ operation(
931
+ operation: DbOperationTemplate | DbOperationRef,
932
+ variables?: Record<string, unknown>,
933
+ options?: DbClientRequestOptions,
934
+ ): Promise<unknown>;
935
+ query(
936
+ operation: DbOperationTemplate | DbOperationRef,
937
+ variables?: Record<string, unknown>,
938
+ options?: DbClientRequestOptions,
939
+ ): Promise<unknown>;
940
+ };
941
+
942
+ export type DbDoctorSeverity = 'error' | 'warn' | 'info';
943
+
944
+ export type DbDoctorFinding = {
945
+ code: string;
946
+ severity: DbDoctorSeverity;
947
+ source?: 'schema' | 'doctor' | string;
948
+ resource?: string;
949
+ field?: string;
950
+ message: string;
951
+ hint?: string;
952
+ details?: Record<string, unknown>;
953
+ };
954
+
955
+ export type DbDoctorResult = {
956
+ summary: {
957
+ error: number;
958
+ warn: number;
959
+ info: number;
960
+ };
961
+ findings: DbDoctorFinding[];
962
+ };
963
+
964
+ export type DbRequestHandlerOptions = {
965
+ /** Scoped base for db dev tools. Defaults to "/__db". */
966
+ apiBase?: string;
967
+ /** App-facing REST data route alias. Defaults to configured server.dataPath. */
968
+ dataPath?: string | false;
969
+ /** Serve root REST routes such as "/users". Defaults to true for standalone handlers. */
970
+ rootRoutes?: boolean;
971
+ /** Scoped REST resource base, such as "/__db/rest". */
972
+ restBasePath?: string;
973
+ /** GraphQL endpoint path. Defaults to configured graphql.path or "/graphql". */
974
+ graphqlPath?: string;
975
+ /** Explicit request trace option. Wins over db.config.mjs server.trace. */
976
+ trace?: DbTraceOptions;
977
+ };
978
+
979
+ export type DbRequestHandler = (
980
+ request: IncomingMessage,
981
+ response: ServerResponse,
982
+ next?: () => void,
983
+ ) => Promise<boolean>;
984
+
985
+ export type DbServer = {
986
+ server: Server;
987
+ db: Db;
988
+ url: string;
989
+ };
990
+
991
+ export function openDb<Types extends DbTypeMap = DbTypeMap>(options?: DbOpenOptions | string): Promise<Db<Types>>;
992
+ export function createDbClient(options?: DbClientOptions): DbClient;
993
+ export function createIndexedDbCacheStorage(options?: {
994
+ name?: string;
995
+ storeName?: string;
996
+ key?: string;
997
+ indexedDB?: unknown;
998
+ }): DbCacheStorage;
999
+ export function createDbRequestHandler(db: Db, options?: DbRequestHandlerOptions): DbRequestHandler;
1000
+ export function createDbOperationHandler(db: Db, options?: DbOperationsOptions | { operations?: boolean | 'auto' | DbOperationsOptions }): DbOperationHandler;
1001
+ export function loadConfig(options?: DbOptions): Promise<DbOptions>;
1002
+ export function loadDbSchema(options?: DbOptions | string): Promise<DbLoadedSchema>;
1003
+ export function createDbSchema(project: unknown, config: DbOptions): DbLoadedSchema;
1004
+ export function createSchemaValidator<TValue = Record<string, unknown>>(
1005
+ resource: Record<string, unknown>,
1006
+ config: DbOptions,
1007
+ options?: DbSchemaValidatorOptions,
1008
+ ): DbSchemaValidator<TValue>;
1009
+ export function resolveSchemaLocator(options?: DbOptions | string): Promise<DbSchemaLocator>;
1010
+ export function normalizeSchemaLoadMode(value?: unknown): DbSchemaLoadMode;
1011
+ export function loadProjectSchema(config: DbOptions, options?: { load?: DbSchemaLoadMode }): Promise<unknown>;
1012
+ export function runDbDoctor(config: DbOptions): Promise<DbDoctorResult>;
1013
+ export function startDbServer(options?: DbOpenOptions & { host?: string; port?: number }): Promise<DbServer>;
1014
+ export function syncDb(config: DbOptions, options?: { allowErrors?: boolean }): Promise<unknown>;
1015
+ export function generateTypes(config: DbOptions, options?: { outFile?: string }): Promise<{ content: string; outFiles: string[] }>;
1016
+ export function generateSchemaManifest(config: DbOptions, options?: { outFile?: string }): Promise<{ manifest: unknown; content: string; outFiles: string[] }>;
1017
+ export function renderSchemaManifest(resources: unknown[], config?: DbOptions): unknown;
1018
+ export function generateViewerManifest(config: DbOptions, options?: { outFile?: string }): Promise<{ manifest: unknown; content: string; outFiles: string[] }>;
1019
+ export function renderViewerManifest(resources: unknown[], config?: DbOptions): unknown;
1020
+ export function hashOperation(operation: DbOperationTemplate): string;
1021
+ export function buildOperationManifest(
1022
+ config: DbOptions,
1023
+ options?: {
1024
+ outFile?: string;
1025
+ refsOutFile?: string;
1026
+ generatedAt?: string;
1027
+ operations?: DbOperationTemplate[];
1028
+ },
1029
+ ): Promise<{
1030
+ manifest: unknown;
1031
+ refs: unknown;
1032
+ outFiles: string[];
1033
+ refsOutFiles: string[];
1034
+ }>;
1035
+ export function mergeManifest(base: unknown, patch: unknown): unknown;
1036
+ export function resourceNameFromPath(file: string, options?: { strategy?: DbResourceNamingStrategy }): string;
1037
+ export function parseFixturePath(file: string): {
1038
+ file: string;
1039
+ folders: string[];
1040
+ folder: string | null;
1041
+ filename: string;
1042
+ basename: string;
1043
+ extension: string;
1044
+ };
1045
+ export function generateHonoStarter(
1046
+ config: DbOptions,
1047
+ options?: {
1048
+ outDir?: string;
1049
+ api?: Array<'rest' | 'graphql'> | 'rest' | 'graphql' | 'rest,graphql' | 'none';
1050
+ db?: 'sqlite';
1051
+ app?: 'standalone' | 'module';
1052
+ seed?: false | 'fixtures';
1053
+ allowWarnings?: boolean;
1054
+ },
1055
+ ): Promise<{ outDir: string; files: string[]; diagnostics: unknown[] }>;
1056
+ export function executeGraphql(
1057
+ db: Db,
1058
+ request: string | GraphqlRequest,
1059
+ ): Promise<GraphqlResult>;
1060
+ export function executeGraphql(
1061
+ db: Db,
1062
+ request: GraphqlRequest[],
1063
+ ): Promise<GraphqlResult[]>;
1064
+ export function executeGraphqlBatch(db: Db, requests: GraphqlRequest[]): Promise<GraphqlResult[]>;
1065
+ export function parseGraphql(query: string): unknown;