@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,288 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { dbError } from '../../errors.js';
3
+ import { executeGraphql } from '../../graphql/index.js';
4
+ import { handleRestRequest } from '../../rest/handler.js';
5
+ import { normalizeOperationTemplate, operationRequest, } from '../../shared/operations.js';
6
+ import { buildOperationRegistry } from './index.js';
7
+ import { operationMapFromEntries } from './maps.js';
8
+ export function createDbOperationHandler(db, options = {}) {
9
+ const operationOptions = normalizeOperationOptions(db, options);
10
+ return {
11
+ enabled: operationOptions.enabled === true,
12
+ options: operationOptions,
13
+ async resolve(ref) {
14
+ return resolveOperationRef(db, operationOptions, ref);
15
+ },
16
+ async execute(ref, variables = {}, executionOptions = {}) {
17
+ if (operationOptions.enabled !== true) {
18
+ throw dbError('OPERATIONS_DISABLED', 'Registered operations are not enabled.', {
19
+ status: 404,
20
+ hint: 'Set operations.enabled to true and provide operations.registry or outputs.operationRegistry.',
21
+ });
22
+ }
23
+ const operation = await resolveOperationRef(db, operationOptions, ref);
24
+ if (!operation) {
25
+ throw operationNotFound(ref);
26
+ }
27
+ const operationResult = operationRequest(operation, variables);
28
+ if (operationResult.kind === 'graphql') {
29
+ if (db.config.graphql?.enabled === false) {
30
+ return {
31
+ kind: 'graphql',
32
+ status: 404,
33
+ headers: {
34
+ 'content-type': 'application/json; charset=utf-8',
35
+ },
36
+ body: {
37
+ error: {
38
+ code: 'GRAPHQL_DISABLED',
39
+ message: 'GraphQL endpoint is disabled.',
40
+ hint: 'Set graphql.enabled to true in db.config.mjs to enable registered GraphQL operations.',
41
+ details: {
42
+ graphqlEnabled: false,
43
+ ref: decodeOperationRef(ref),
44
+ },
45
+ },
46
+ },
47
+ };
48
+ }
49
+ return {
50
+ kind: 'graphql',
51
+ status: 200,
52
+ headers: {
53
+ 'content-type': 'application/json; charset=utf-8',
54
+ },
55
+ body: await executeGraphql(db, {
56
+ query: operationResult.query,
57
+ variables: operationResult.variables,
58
+ operationName: operationResult.operationName,
59
+ }),
60
+ };
61
+ }
62
+ const response = makeResultResponse();
63
+ await handleRestRequest(db, internalRestRequest(operationResult), response, new URL(operationResult.path, 'http://db.local'), executionOptions.routes
64
+ ? { ...executionOptions.routes, trace: executionOptions.trace }
65
+ : { trace: executionOptions.trace });
66
+ return {
67
+ kind: 'rest',
68
+ status: response.status,
69
+ headers: response.headers,
70
+ body: response.bodyValue(),
71
+ rawBody: response.body,
72
+ };
73
+ },
74
+ async executeRequest(ref, body = {}, executionOptions = {}) {
75
+ return this.execute(ref, body?.variables ?? {}, executionOptions);
76
+ },
77
+ };
78
+ }
79
+ function normalizeOperationOptions(db, options = {}) {
80
+ const globalOptions = db.config.operations ?? {};
81
+ const localOptions = isOperationOptionsWrapper(options)
82
+ ? options.operations
83
+ : options;
84
+ if (localOptions === false) {
85
+ return {
86
+ ...globalOptions,
87
+ enabled: false,
88
+ acceptRefs: normalizeAcceptRefs(globalOptions.acceptRefs),
89
+ };
90
+ }
91
+ if (localOptions === true || localOptions === 'auto' || localOptions === undefined || localOptions === null) {
92
+ return {
93
+ ...globalOptions,
94
+ enabled: globalOptions.enabled === true,
95
+ acceptRefs: normalizeAcceptRefs(globalOptions.acceptRefs),
96
+ };
97
+ }
98
+ if (typeof localOptions !== 'object') {
99
+ return {
100
+ ...globalOptions,
101
+ enabled: globalOptions.enabled === true,
102
+ acceptRefs: normalizeAcceptRefs(globalOptions.acceptRefs),
103
+ };
104
+ }
105
+ const hasLocalRegistry = Boolean(localOptions.registry && Object.keys(localOptions.registry).length > 0)
106
+ || typeof localOptions.resolveRef === 'function'
107
+ || Boolean(localOptions.outFile);
108
+ return {
109
+ ...globalOptions,
110
+ ...localOptions,
111
+ enabled: localOptions.enabled ?? (hasLocalRegistry ? true : globalOptions.enabled === true),
112
+ acceptRefs: normalizeAcceptRefs(localOptions.acceptRefs ?? globalOptions.acceptRefs),
113
+ };
114
+ }
115
+ function normalizeAcceptRefs(value) {
116
+ if (value === 'ref' || value === 'name' || value === 'both') {
117
+ return value;
118
+ }
119
+ if (value === 'hash') {
120
+ return 'ref';
121
+ }
122
+ return 'both';
123
+ }
124
+ async function resolveOperationRef(db, options, ref) {
125
+ const decodedRef = decodeOperationRef(ref);
126
+ const registry = await operationRegistry(db.config, options);
127
+ const context = {
128
+ ref,
129
+ decodedRef,
130
+ acceptRefs: options.acceptRefs,
131
+ registry,
132
+ operation: null,
133
+ };
134
+ let operation = null;
135
+ if (typeof options.resolveRef === 'function') {
136
+ const resolvedOperation = await options.resolveRef(decodedRef, context);
137
+ if (resolvedOperation) {
138
+ operation = normalizeRegistryOperation(decodedRef, resolvedOperation);
139
+ }
140
+ }
141
+ operation ??= defaultOperationForRef(registry, decodedRef, options.acceptRefs);
142
+ context.operation = operation ?? null;
143
+ if (typeof options.validateRef === 'function') {
144
+ const result = await options.validateRef(context);
145
+ if (result && (typeof result === 'object' || typeof result === 'string')) {
146
+ return normalizeRegistryOperation(decodedRef, result);
147
+ }
148
+ if (result === true) {
149
+ return context.operation;
150
+ }
151
+ return null;
152
+ }
153
+ return operation;
154
+ }
155
+ function defaultOperationForRef(registry, decodedRef, acceptRefs) {
156
+ for (const [key, operation] of Object.entries(registry)) {
157
+ if (!operation || typeof operation !== 'object') {
158
+ continue;
159
+ }
160
+ const name = operation.name ?? key;
161
+ const ref = operation.ref ?? key;
162
+ const acceptsRef = acceptRefs === 'ref' || acceptRefs === 'both';
163
+ const acceptsName = acceptRefs === 'name' || acceptRefs === 'both';
164
+ if (acceptsRef && ref && decodedRef === ref) {
165
+ return operation;
166
+ }
167
+ if (acceptsName && name && decodedRef === name) {
168
+ return operation;
169
+ }
170
+ }
171
+ return null;
172
+ }
173
+ async function operationRegistry(config, options) {
174
+ if (options.registry && Object.keys(options.registry).length > 0) {
175
+ return normalizeOperationRegistry(options.registry);
176
+ }
177
+ if (options.outFile) {
178
+ try {
179
+ const manifest = JSON.parse(await readFile(options.outFile, 'utf8'));
180
+ return normalizeOperationRegistry(manifest.operations ?? {});
181
+ }
182
+ catch (error) {
183
+ throw operationRegistryLoadFailed(options.outFile, error);
184
+ }
185
+ }
186
+ if (typeof options.resolveRef === 'function') {
187
+ return operationMapFromEntries();
188
+ }
189
+ if (options.sourceDir) {
190
+ return buildOperationRegistry({
191
+ ...config,
192
+ operations: {
193
+ ...(config.operations ?? {}),
194
+ sourceDir: options.sourceDir,
195
+ },
196
+ });
197
+ }
198
+ return operationMapFromEntries();
199
+ }
200
+ function normalizeOperationRegistry(registry) {
201
+ return operationMapFromEntries(Object.entries(registry ?? {}).map(([key, operation]) => [key, normalizeRegistryOperation(key, operation)]));
202
+ }
203
+ function normalizeRegistryOperation(key, operation) {
204
+ const normalized = normalizeOperationTemplate(operation);
205
+ const source = operation && typeof operation === 'object' && !Array.isArray(operation) ? operation : {};
206
+ if (source.name || normalized.name || key) {
207
+ normalized.name = String(source.name ?? normalized.name ?? key);
208
+ }
209
+ if (source.ref || normalized.ref || key) {
210
+ normalized.ref = String(source.ref ?? normalized.ref ?? key);
211
+ }
212
+ return normalized;
213
+ }
214
+ function operationNotFound(ref) {
215
+ const decodedRef = decodeOperationRef(ref);
216
+ return dbError('OPERATION_NOT_FOUND', `Unknown registered operation "${decodedRef}".`, {
217
+ status: 404,
218
+ hint: 'Register the operation name or ref in operations.registry, provide a custom operations.resolveRef, or generate an operations manifest.',
219
+ details: { ref: decodedRef },
220
+ });
221
+ }
222
+ function operationRegistryLoadFailed(outFile, error) {
223
+ const reason = 'code' in error && error.code === 'ENOENT'
224
+ ? 'missing'
225
+ : error instanceof SyntaxError
226
+ ? 'invalid-json'
227
+ : 'read-failed';
228
+ return dbError('OPERATION_REGISTRY_LOAD_FAILED', 'Registered operation registry could not be loaded.', {
229
+ status: 500,
230
+ hint: reason === 'missing'
231
+ ? 'Run async-db operations build or check outputs.operationRegistry points at the generated registry.'
232
+ : 'Check outputs.operationRegistry and regenerate the operation registry.',
233
+ details: {
234
+ outFile: String(outFile),
235
+ reason,
236
+ },
237
+ });
238
+ }
239
+ function decodeOperationRef(ref) {
240
+ return decodeURIComponent(String(ref ?? ''));
241
+ }
242
+ function internalRestRequest(restRequest) {
243
+ return {
244
+ method: restRequest.method,
245
+ headers: {
246
+ 'content-type': 'application/json',
247
+ },
248
+ async *[Symbol.asyncIterator]() {
249
+ if (restRequest.body !== undefined) {
250
+ yield Buffer.from(JSON.stringify(restRequest.body));
251
+ }
252
+ },
253
+ };
254
+ }
255
+ function makeResultResponse() {
256
+ return {
257
+ status: 200,
258
+ headers: {},
259
+ body: '',
260
+ writeHead(status, headers = {}) {
261
+ this.status = status;
262
+ this.headers = normalizeHeaders(headers);
263
+ },
264
+ end(chunk = '') {
265
+ this.body += chunk;
266
+ },
267
+ bodyValue() {
268
+ if (!this.body) {
269
+ return null;
270
+ }
271
+ if (String(this.headers['content-type'] ?? '').includes('application/json')) {
272
+ try {
273
+ return JSON.parse(this.body);
274
+ }
275
+ catch {
276
+ return this.body;
277
+ }
278
+ }
279
+ return this.body;
280
+ },
281
+ };
282
+ }
283
+ function normalizeHeaders(headers) {
284
+ return Object.fromEntries(Object.entries(headers ?? {}).map(([key, value]) => [key.toLowerCase(), value]));
285
+ }
286
+ function isOperationOptionsWrapper(options) {
287
+ return Boolean(options) && typeof options === 'object' && Object.hasOwn(options, 'operations');
288
+ }
@@ -0,0 +1,51 @@
1
+ type RuntimeRecord = Record<string, unknown>;
2
+ type RuntimeConfig = {
3
+ schema?: {
4
+ unknownFields?: string;
5
+ };
6
+ defaults?: {
7
+ applyOnCreate?: boolean;
8
+ };
9
+ cwd?: string;
10
+ stateDir?: string;
11
+ [key: string]: unknown;
12
+ };
13
+ type RuntimeResource = {
14
+ name: string;
15
+ kind?: string;
16
+ idField?: string;
17
+ [key: string]: unknown;
18
+ };
19
+ type RuntimeAdapter = {
20
+ statePath?: (resource: RuntimeResource) => unknown;
21
+ readResource?: (resource: RuntimeResource, fallback: unknown) => Promise<unknown> | unknown;
22
+ writeResource?: (resource: RuntimeResource, value: unknown) => Promise<unknown> | unknown;
23
+ withResourceWrite?: <T>(resource: RuntimeResource, operation: () => T | Promise<T>) => Promise<T> | T;
24
+ };
25
+ type RuntimeFacade = {
26
+ adapterFor(resource: RuntimeResource): RuntimeAdapter;
27
+ emit(change: Record<string, unknown>): unknown;
28
+ };
29
+ type DbLike = {
30
+ config: RuntimeConfig;
31
+ runtime: RuntimeFacade;
32
+ assertResourceWritable?: (resourceName: string) => void;
33
+ };
34
+ export declare class DbCollection {
35
+ db: DbLike;
36
+ config: RuntimeConfig;
37
+ resource: RuntimeResource;
38
+ path: unknown;
39
+ constructor(db: DbLike | RuntimeConfig, resource: RuntimeResource);
40
+ all(): Promise<RuntimeRecord[]>;
41
+ get(id: unknown): Promise<RuntimeRecord | null>;
42
+ exists(id: unknown): Promise<boolean>;
43
+ create(record: RuntimeRecord): Promise<RuntimeRecord>;
44
+ update(id: unknown, patch: RuntimeRecord): Promise<RuntimeRecord | null>;
45
+ patch(id: unknown, patch: RuntimeRecord): Promise<RuntimeRecord | null>;
46
+ delete(id: unknown): Promise<boolean>;
47
+ replaceAll(records: RuntimeRecord[]): Promise<RuntimeRecord[]>;
48
+ adapter(): RuntimeAdapter;
49
+ emit(op: string, details?: Record<string, unknown>): void;
50
+ }
51
+ export {};
@@ -0,0 +1,198 @@
1
+ import { dbError } from '../../errors.js';
2
+ import { createSchemaValidator, validateUniqueCollectionFields } from '../../schema.js';
3
+ import { applyDefaultsToRecord } from '../../sync.js';
4
+ import { createRuntime } from '../storage/runtime.js';
5
+ export class DbCollection {
6
+ db;
7
+ config;
8
+ resource;
9
+ path;
10
+ constructor(db, resource) {
11
+ this.db = normalizeDb(db, resource);
12
+ this.config = this.db.config;
13
+ this.resource = resource;
14
+ this.path = this.db.runtime.adapterFor(resource).statePath?.(resource);
15
+ }
16
+ async all() {
17
+ return await this.adapter().readResource?.(this.resource, []);
18
+ }
19
+ async get(id) {
20
+ const records = await this.all();
21
+ return records.find((record) => idMatches(record?.[this.resource.idField], id)) ?? null;
22
+ }
23
+ async exists(id) {
24
+ return await this.get(id) !== null;
25
+ }
26
+ async create(record) {
27
+ this.db.assertResourceWritable?.(this.resource.name);
28
+ return this.adapter().withResourceWrite(this.resource, async () => {
29
+ const records = await this.all();
30
+ const nextRecord = this.config.defaults?.applyOnCreate === false
31
+ ? { ...record }
32
+ : applyDefaultsToRecord(record, this.resource);
33
+ let id = nextRecord[this.resource.idField];
34
+ if (id === undefined || id === null || id === '') {
35
+ id = nextCollectionId(records, this.resource.idField);
36
+ nextRecord[this.resource.idField] = id;
37
+ }
38
+ const validatedRecord = await assertRuntimeRecord(nextRecord, this.resource, this.config, {
39
+ mode: 'create',
40
+ source: `${this.resource.name} create body`,
41
+ });
42
+ id = validatedRecord[this.resource.idField];
43
+ if (records.some((existing) => idMatches(existing?.[this.resource.idField], id))) {
44
+ throw dbError('DB_CREATE_DUPLICATE_ID', `Cannot create "${this.resource.name}" record because id "${id}" already exists.`, {
45
+ status: 409,
46
+ hint: 'Use a unique id, or call patch/update if you intended to modify the existing record.',
47
+ details: {
48
+ resource: this.resource.name,
49
+ idField: this.resource.idField,
50
+ id,
51
+ },
52
+ });
53
+ }
54
+ assertUniqueCollectionRecords([...records, validatedRecord], this.resource);
55
+ const nextRecords = [...records, validatedRecord];
56
+ await this.adapter().writeResource?.(this.resource, nextRecords);
57
+ this.emit('create', { id });
58
+ return validatedRecord;
59
+ });
60
+ }
61
+ async update(id, patch) {
62
+ this.db.assertResourceWritable?.(this.resource.name);
63
+ return this.adapter().withResourceWrite(this.resource, async () => {
64
+ const records = await this.all();
65
+ const index = records.findIndex((record) => idMatches(record?.[this.resource.idField], id));
66
+ if (index === -1) {
67
+ return null;
68
+ }
69
+ const existingId = records[index]?.[this.resource.idField];
70
+ const nextRecord = {
71
+ ...records[index],
72
+ ...patch,
73
+ [this.resource.idField]: existingId,
74
+ };
75
+ const nextRecords = [...records];
76
+ nextRecords[index] = await assertRuntimeRecord(nextRecord, this.resource, this.config, {
77
+ mode: 'replace',
78
+ source: `${this.resource.name} patch body`,
79
+ });
80
+ assertUniqueCollectionRecords(nextRecords, this.resource);
81
+ await this.adapter().writeResource?.(this.resource, nextRecords);
82
+ this.emit('update', { id: existingId });
83
+ return nextRecords[index];
84
+ });
85
+ }
86
+ async patch(id, patch) {
87
+ return this.update(id, patch);
88
+ }
89
+ async delete(id) {
90
+ this.db.assertResourceWritable?.(this.resource.name);
91
+ return this.adapter().withResourceWrite(this.resource, async () => {
92
+ const records = await this.all();
93
+ const nextRecords = records.filter((record) => !idMatches(record?.[this.resource.idField], id));
94
+ const deleted = nextRecords.length !== records.length;
95
+ await this.adapter().writeResource?.(this.resource, nextRecords);
96
+ if (deleted) {
97
+ this.emit('delete', { id });
98
+ }
99
+ return deleted;
100
+ });
101
+ }
102
+ async replaceAll(records) {
103
+ this.db.assertResourceWritable?.(this.resource.name);
104
+ return this.adapter().withResourceWrite(this.resource, async () => {
105
+ const validatedRecords = [];
106
+ for (const [index, record] of records.entries()) {
107
+ validatedRecords.push(await assertRuntimeRecord(record, this.resource, this.config, {
108
+ mode: 'replace',
109
+ source: `${this.resource.name}[${index}] replaceAll body`,
110
+ }));
111
+ }
112
+ assertUniqueCollectionRecords(validatedRecords, this.resource);
113
+ await this.adapter().writeResource?.(this.resource, validatedRecords);
114
+ this.emit('replaceAll');
115
+ return validatedRecords;
116
+ });
117
+ }
118
+ adapter() {
119
+ return this.db.runtime.adapterFor(this.resource);
120
+ }
121
+ emit(op, details = {}) {
122
+ this.db.runtime.emit({
123
+ resource: this.resource.name,
124
+ kind: 'collection',
125
+ op,
126
+ ...details,
127
+ });
128
+ }
129
+ }
130
+ function assertRuntimeRecord(record, resource, config, options = {}) {
131
+ const validator = createSchemaValidator(resource, config, {
132
+ mode: options.mode,
133
+ unknownFields: config.schema?.unknownFields ?? 'warn',
134
+ applyDefaults: false,
135
+ });
136
+ return validator.validateAsync(record, {
137
+ source: options.source,
138
+ applyDefaults: false,
139
+ }).then((result) => {
140
+ if (result.ok) {
141
+ return result.value;
142
+ }
143
+ throw dbError('DB_SCHEMA_VALIDATION_FAILED', `${resource.name} record does not match its schema: ${result.errors[0].message}`, {
144
+ status: 400,
145
+ hint: 'Update the record to match the schema field types, required fields, enum values, and constraints.',
146
+ details: {
147
+ resource: resource.name,
148
+ diagnostics: result.errors,
149
+ },
150
+ });
151
+ });
152
+ }
153
+ function normalizeDb(db, resource) {
154
+ if (isDbLike(db)) {
155
+ return db;
156
+ }
157
+ const config = db;
158
+ const runtime = createRuntime(config, [resource]);
159
+ return {
160
+ config,
161
+ runtime,
162
+ };
163
+ }
164
+ function isDbLike(value) {
165
+ return Boolean(value?.runtime && value?.config);
166
+ }
167
+ function assertUniqueCollectionRecords(records, resource) {
168
+ const diagnostics = validateUniqueCollectionFields(resource, records)
169
+ .filter((diagnostic) => diagnostic.severity === 'error');
170
+ if (diagnostics.length === 0) {
171
+ return;
172
+ }
173
+ throw dbError('DB_SCHEMA_VALIDATION_FAILED', `${resource.name} record does not match its schema: ${diagnostics[0].message}`, {
174
+ status: 400,
175
+ hint: 'Update the record to satisfy unique schema fields.',
176
+ details: {
177
+ resource: resource.name,
178
+ diagnostics,
179
+ },
180
+ });
181
+ }
182
+ function idMatches(left, right) {
183
+ return left !== undefined && left !== null && right !== undefined && right !== null && String(left) === String(right);
184
+ }
185
+ function nextCollectionId(records, idField) {
186
+ const usedIds = new Set(records
187
+ .map((record) => record?.[idField])
188
+ .filter((id) => id !== undefined && id !== null && id !== '')
189
+ .map((id) => String(id)));
190
+ const numericIds = [...usedIds]
191
+ .map((id) => Number(id))
192
+ .filter((id) => Number.isInteger(id) && id > 0);
193
+ let next = numericIds.length > 0 ? Math.max(...numericIds) + 1 : records.length + 1;
194
+ while (usedIds.has(String(next))) {
195
+ next += 1;
196
+ }
197
+ return String(next);
198
+ }
@@ -0,0 +1,152 @@
1
+ import { createRuntime } from '../storage/runtime.js';
2
+ import { DbCollection } from './collection.js';
3
+ import { DbDocument } from './document.js';
4
+ import { type DbScope, type MigrationLock } from './scope-state.js';
5
+ type DbConfig = {
6
+ cwd: string;
7
+ stateDir: string;
8
+ schemaLoadMode?: string;
9
+ resources?: Record<string, unknown>;
10
+ __asyncDbScope?: DbScope;
11
+ [key: string]: unknown;
12
+ };
13
+ type DbResource = {
14
+ name: string;
15
+ kind: 'collection' | 'document' | string;
16
+ [key: string]: unknown;
17
+ };
18
+ type OpenDbOptions = Record<string, unknown> & {
19
+ from?: string;
20
+ schema?: unknown;
21
+ syncOnOpen?: boolean;
22
+ allowSourceErrors?: boolean;
23
+ load?: string;
24
+ };
25
+ type RuntimeFacade = ReturnType<typeof createRuntime>;
26
+ type ForkSource = 'main' | {
27
+ fork?: string | null;
28
+ branch: string;
29
+ } | {
30
+ fork?: string | null;
31
+ snapshot: string;
32
+ };
33
+ type ForkCreateOptions = {
34
+ from?: ForkSource;
35
+ metadata?: Record<string, unknown>;
36
+ };
37
+ type BranchCreateOptions = {
38
+ from?: string;
39
+ metadata?: Record<string, unknown>;
40
+ };
41
+ type SnapshotCreateOptions = {
42
+ label?: string;
43
+ resources?: string[];
44
+ };
45
+ type SnapshotRestoreOptions = {
46
+ resources?: string[];
47
+ };
48
+ type MigrationStartOptions = {
49
+ resources: string[];
50
+ mode?: 'read-only';
51
+ };
52
+ type MigrationVerifyOptions = {
53
+ resources: string[];
54
+ checks?: Array<'count' | 'schema' | 'checksum'>;
55
+ };
56
+ type ResourceMigrateOptions = {
57
+ from: string;
58
+ to: string;
59
+ };
60
+ type SnapshotResult = {
61
+ id: string;
62
+ label?: string;
63
+ fork: string | null;
64
+ branch: string;
65
+ resources: string[];
66
+ path: string;
67
+ };
68
+ declare class DbResourceRegistry extends Map<string, DbResource> {
69
+ private readonly migrateResource;
70
+ constructor(entries: Array<[string, DbResource]>, migrateResource: (name: string, options: ResourceMigrateOptions) => Promise<void>);
71
+ migrate(name: string, options: ResourceMigrateOptions): Promise<void>;
72
+ }
73
+ export declare function openDb(options?: OpenDbOptions | string): Promise<Db>;
74
+ export declare class Db {
75
+ config: DbConfig;
76
+ resources: DbResourceRegistry;
77
+ diagnostics: unknown[];
78
+ schemaVersion: number;
79
+ runtime: RuntimeFacade;
80
+ events: RuntimeFacade['events'];
81
+ scope: DbScope;
82
+ forks: {
83
+ create: (name: string, options?: ForkCreateOptions) => Promise<Db>;
84
+ open: (name: string) => Promise<Db>;
85
+ ensure: (name: string, options?: ForkCreateOptions) => Promise<Db>;
86
+ list: () => Promise<Array<Record<string, unknown>>>;
87
+ delete: (name: string) => Promise<boolean>;
88
+ };
89
+ branches: {
90
+ create: (name: string, options?: BranchCreateOptions) => Promise<Db>;
91
+ open: (name: string) => Promise<Db>;
92
+ ensure: (name: string, options?: BranchCreateOptions) => Promise<Db>;
93
+ list: () => Promise<Array<Record<string, unknown>>>;
94
+ delete: (name: string) => Promise<boolean>;
95
+ };
96
+ snapshots: {
97
+ create: (options?: SnapshotCreateOptions) => Promise<SnapshotResult>;
98
+ restore: (id: string, options?: SnapshotRestoreOptions) => Promise<void>;
99
+ };
100
+ migrations: {
101
+ start: (name: string, options: MigrationStartOptions) => Promise<MigrationLock>;
102
+ verify: (name: string, options: MigrationVerifyOptions) => Promise<void>;
103
+ finish: (name: string) => Promise<void>;
104
+ };
105
+ routing: {
106
+ set: (routes: Record<string, string>) => Promise<Record<string, string>>;
107
+ };
108
+ private migrationLocks;
109
+ constructor(config: DbConfig, resources: DbResource[], diagnostics?: unknown[], scope?: DbScope);
110
+ collection(name: string): DbCollection;
111
+ document(name: string): DbDocument;
112
+ operation(template: string, variables?: Record<string, unknown>): Promise<unknown>;
113
+ query(template: string, variables?: Record<string, unknown>): Promise<unknown>;
114
+ fork(name: string): Db;
115
+ branch(name: string): Db;
116
+ assertResourceWritable(resourceName: string): void;
117
+ requireResource(name: string, kind: 'collection' | 'document'): DbResource;
118
+ resourceNames(): string[];
119
+ close(): Promise<void>;
120
+ private scopedDb;
121
+ private createFork;
122
+ private openFork;
123
+ private ensureFork;
124
+ private listForks;
125
+ private deleteFork;
126
+ private createBranch;
127
+ private openBranch;
128
+ private ensureBranch;
129
+ private listBranches;
130
+ private deleteBranch;
131
+ private createSnapshot;
132
+ private restoreSnapshot;
133
+ private startMigration;
134
+ private migrateResource;
135
+ private verifyMigration;
136
+ private finishMigration;
137
+ private setRouting;
138
+ private activeMigrationForResource;
139
+ private assertRootForkLifecycle;
140
+ private assertForkExists;
141
+ private assertBranchExists;
142
+ private resolveForkSource;
143
+ private sourceScope;
144
+ private writeBranchRegistry;
145
+ private recordMigrationCopy;
146
+ private persistMigrationLocks;
147
+ resourceForName(name: string): DbResource;
148
+ private migrationLocksPath;
149
+ private routingPath;
150
+ }
151
+ export declare function stateFileForDebug(db: Db, resourceName: string): string;
152
+ export {};