@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,33 @@
1
+ type RestRecord = Record<string, unknown>;
2
+ type RestField = {
3
+ computed?: boolean;
4
+ [key: string]: unknown;
5
+ };
6
+ type RestRelation = {
7
+ name: string;
8
+ sourceField: string;
9
+ targetResource: string;
10
+ targetField: string;
11
+ cardinality: string;
12
+ [key: string]: unknown;
13
+ };
14
+ type RestResource = {
15
+ name: string;
16
+ fields?: Record<string, RestField>;
17
+ relations?: RestRelation[];
18
+ [key: string]: unknown;
19
+ };
20
+ type RestCollection = {
21
+ all: () => Promise<RestRecord[]>;
22
+ };
23
+ type RestDb = {
24
+ resources: Map<string, RestResource>;
25
+ collection: (resourceName: string) => RestCollection;
26
+ config?: unknown;
27
+ [key: string]: unknown;
28
+ };
29
+ type ShapeOptions = {
30
+ allowPagination?: boolean;
31
+ };
32
+ export declare function shapeCollectionRead(db: RestDb, resource: RestResource, records: RestRecord[], url: URL, options?: ShapeOptions): Promise<RestRecord[]>;
33
+ export {};
@@ -0,0 +1,218 @@
1
+ import { dbError, listChoices } from '../errors.js';
2
+ import { resolveSelectedComputedFields } from '../features/runtime/fanout.js';
3
+ import { resolveResource } from '../names.js';
4
+ export async function shapeCollectionRead(db, resource, records, url, options = {}) {
5
+ const query = parseShapeQuery(db, resource, url, options);
6
+ const paginated = options.allowPagination ? paginateRecords(records, query) : records;
7
+ const expanded = query.expand.length > 0
8
+ ? await expandRecords(db, paginated, query)
9
+ : paginated;
10
+ if (query.select.length === 0) {
11
+ return expanded;
12
+ }
13
+ const computed = query.select
14
+ .filter(([head, child]) => !child && resource.fields?.[head]?.computed)
15
+ .map(([head]) => head);
16
+ const resolved = computed.length > 0
17
+ ? await resolveSelectedComputedFields(db, resource, expanded, computed)
18
+ : expanded;
19
+ return resolved.map((record) => projectRecord(record, query));
20
+ }
21
+ function parseShapeQuery(db, resource, url, options) {
22
+ const relationMap = new Map((resource.relations ?? []).map((relation) => [relation.name, relation]));
23
+ const expand = parseListParam(url.searchParams.get('expand'));
24
+ const select = parseListParam(url.searchParams.get('select')).map((path) => path.split('.'));
25
+ for (const relationName of expand) {
26
+ const relation = relationMap.get(relationName);
27
+ if (!relation) {
28
+ throw dbError('REST_EXPAND_UNKNOWN_RELATION', `Unknown expanded relation "${relationName}" on ${resource.name}.`, {
29
+ status: 400,
30
+ hint: `Use one of: ${listChoices([...relationMap.keys()])}.`,
31
+ details: {
32
+ resource: resource.name,
33
+ relation: relationName,
34
+ availableRelations: [...relationMap.keys()],
35
+ },
36
+ });
37
+ }
38
+ if (relation.cardinality !== 'one') {
39
+ throw dbError('REST_EXPAND_UNSUPPORTED_CARDINALITY', `Relation "${relationName}" on ${resource.name} cannot be expanded by REST yet because it is ${relation.cardinality}.`, {
40
+ status: 400,
41
+ hint: 'MVP REST expansion supports explicit to-one relations only.',
42
+ details: relation,
43
+ });
44
+ }
45
+ }
46
+ validateSelectedFields(db, resource, select, new Set(expand), relationMap);
47
+ const offset = options.allowPagination
48
+ ? parseOffset(url.searchParams.get('offset'))
49
+ : 0;
50
+ const limit = options.allowPagination
51
+ ? parseLimit(url.searchParams.get('limit'))
52
+ : undefined;
53
+ return {
54
+ select,
55
+ expand,
56
+ offset,
57
+ limit,
58
+ relationMap,
59
+ };
60
+ }
61
+ function validateSelectedFields(db, resource, select, expanded, relationMap) {
62
+ for (const parts of select) {
63
+ const [head, child, extra] = parts;
64
+ if (!head || extra !== undefined) {
65
+ throw dbError('REST_SELECT_UNSUPPORTED_DEPTH', `Selected path "${parts.join('.')}" is too deep.`, {
66
+ status: 400,
67
+ hint: 'Use top-level fields or one nested relation field such as "author.name".',
68
+ details: {
69
+ resource: resource.name,
70
+ path: parts.join('.'),
71
+ },
72
+ });
73
+ }
74
+ const relation = relationMap.get(head);
75
+ if (!child) {
76
+ if (head in (resource.fields ?? {}) || relation) {
77
+ if (relation && !expanded.has(head)) {
78
+ throw selectRequiresExpand(resource, head);
79
+ }
80
+ continue;
81
+ }
82
+ throw unknownSelectField(resource, head, availableShapeFields(resource, relationMap));
83
+ }
84
+ if (!relation) {
85
+ throw unknownSelectField(resource, head, availableShapeFields(resource, relationMap));
86
+ }
87
+ if (!expanded.has(head)) {
88
+ throw selectRequiresExpand(resource, head);
89
+ }
90
+ const target = resolveResource(db.resources, relation.targetResource).resource;
91
+ const targetFields = target?.fields ?? {};
92
+ if (!(child in targetFields)) {
93
+ throw unknownSelectField(target ?? resource, child, Object.keys(targetFields), `${head}.${child}`);
94
+ }
95
+ }
96
+ }
97
+ function unknownSelectField(resource, field, availableFields, path = field) {
98
+ return dbError('REST_SELECT_UNKNOWN_FIELD', `Unknown selected field "${field}" on ${resource.name}.`, {
99
+ status: 400,
100
+ hint: `Use one of: ${listChoices(availableFields)}.`,
101
+ details: {
102
+ resource: resource.name,
103
+ field,
104
+ path,
105
+ availableFields: [...availableFields],
106
+ },
107
+ });
108
+ }
109
+ function selectRequiresExpand(resource, relationName) {
110
+ return dbError('REST_SELECT_REQUIRES_EXPAND', `Selected relation "${relationName}" on ${resource.name} requires explicit expand.`, {
111
+ status: 400,
112
+ hint: `Add expand=${relationName} to include nested relation fields.`,
113
+ details: {
114
+ resource: resource.name,
115
+ relation: relationName,
116
+ },
117
+ });
118
+ }
119
+ function availableShapeFields(resource, relationMap) {
120
+ return [...new Set([...Object.keys(resource.fields ?? {}), ...relationMap.keys()])];
121
+ }
122
+ function paginateRecords(records, query) {
123
+ const end = query.limit === undefined ? undefined : query.offset + query.limit;
124
+ return records.slice(query.offset, end);
125
+ }
126
+ async function expandRecords(db, records, query) {
127
+ const nextRecords = records.map((record) => ({ ...record }));
128
+ for (const relationName of query.expand) {
129
+ const relation = query.relationMap.get(relationName);
130
+ const targets = await db.collection(relation.targetResource).all();
131
+ const targetById = new Map();
132
+ for (const target of targets) {
133
+ const targetValue = target?.[relation.targetField];
134
+ if (targetValue === undefined || targetValue === null) {
135
+ continue;
136
+ }
137
+ const key = String(targetValue);
138
+ if (!targetById.has(key)) {
139
+ targetById.set(key, target);
140
+ }
141
+ }
142
+ for (const record of nextRecords) {
143
+ const value = record?.[relation.sourceField];
144
+ if (value === undefined || value === null || value === '') {
145
+ record[relation.name] = null;
146
+ continue;
147
+ }
148
+ record[relation.name] = targetById.get(String(value)) ?? null;
149
+ }
150
+ }
151
+ return nextRecords;
152
+ }
153
+ function projectRecord(record, query) {
154
+ const projected = {};
155
+ for (const [head, child] of query.select) {
156
+ if (!child) {
157
+ if (record?.[head] !== undefined) {
158
+ projected[head] = record[head];
159
+ }
160
+ continue;
161
+ }
162
+ if (record?.[head] === null) {
163
+ projected[head] = null;
164
+ continue;
165
+ }
166
+ const nested = record?.[head];
167
+ if (!isRecord(nested)) {
168
+ continue;
169
+ }
170
+ if (!isRecord(projected[head])) {
171
+ projected[head] = {};
172
+ }
173
+ const projectedNested = projected[head];
174
+ if (nested[child] !== undefined) {
175
+ projectedNested[child] = nested[child];
176
+ }
177
+ }
178
+ return projected;
179
+ }
180
+ function parseListParam(value) {
181
+ return String(value ?? '')
182
+ .split(',')
183
+ .map((item) => item.trim())
184
+ .filter((item) => item !== '');
185
+ }
186
+ function parseOffset(value) {
187
+ if (value === null || value === undefined || value === '') {
188
+ return 0;
189
+ }
190
+ if (!/^\d+$/.test(value)) {
191
+ throw dbError('REST_INVALID_OFFSET', `REST offset must be a non-negative integer: ${value}`, {
192
+ status: 400,
193
+ hint: 'Use offset=0, offset=20, or omit offset.',
194
+ details: {
195
+ offset: value,
196
+ },
197
+ });
198
+ }
199
+ return Number(value);
200
+ }
201
+ function parseLimit(value) {
202
+ if (value === null || value === undefined || value === '') {
203
+ return undefined;
204
+ }
205
+ if (!/^\d+$/.test(value) || Number(value) <= 0) {
206
+ throw dbError('REST_INVALID_LIMIT', `REST limit must be a positive integer: ${value}`, {
207
+ status: 400,
208
+ hint: 'Use limit=20, limit=100, or omit limit.',
209
+ details: {
210
+ limit: value,
211
+ },
212
+ });
213
+ }
214
+ return Number(value);
215
+ }
216
+ function isRecord(value) {
217
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
218
+ }
@@ -0,0 +1 @@
1
+ export { collection, document, field, files } from './features/schema/builders.js';
@@ -0,0 +1 @@
1
+ export { collection, document, field, files } from './features/schema/builders.js';
@@ -0,0 +1 @@
1
+ export { generateSchemaManifest, renderSchemaManifest } from './features/schema/manifest.js';
@@ -0,0 +1 @@
1
+ export { generateSchemaManifest, renderSchemaManifest } from './features/schema/manifest.js';
@@ -0,0 +1,193 @@
1
+ export type FieldDefinition =
2
+ | ({ type: 'string' } & FieldOptions<string>)
3
+ | ({ type: 'datetime' } & FieldOptions<string>)
4
+ | ({ type: 'number' } & FieldOptions<number>)
5
+ | ({ type: 'boolean' } & FieldOptions<boolean>)
6
+ | ({ type: 'enum'; values: readonly (string | number | boolean)[] } & FieldOptions<string | number | boolean>)
7
+ | ({ type: 'object'; fields?: Record<string, FieldDefinition>; additionalProperties?: boolean } & FieldOptions<Record<string, unknown>>)
8
+ | ({ type: 'array'; items?: FieldDefinition } & FieldOptions<unknown[]>)
9
+ | ({ type: 'unknown' } & FieldOptions<unknown>);
10
+
11
+ export type FieldOptions<DefaultValue> = {
12
+ required?: boolean;
13
+ nullable?: boolean;
14
+ description?: string;
15
+ default?: DefaultValue;
16
+ computed?: boolean;
17
+ readOnly?: boolean;
18
+ relation?: RelationDefinition;
19
+ unique?: boolean;
20
+ min?: number;
21
+ max?: number;
22
+ minLength?: number;
23
+ maxLength?: number;
24
+ pattern?: string;
25
+ };
26
+
27
+ export type FieldMetaOptions = FieldOptions<unknown> & {
28
+ type?: FieldDefinition['type'];
29
+ values?: readonly (string | number | boolean)[];
30
+ fields?: Record<string, FieldDefinition>;
31
+ items?: FieldDefinition;
32
+ additionalProperties?: boolean;
33
+ [key: string]: unknown;
34
+ };
35
+
36
+ export type RelationDefinition = {
37
+ /** Output name used by REST expand, such as "author" for authorId. */
38
+ name?: string;
39
+ /** Target collection resource name. */
40
+ to: string;
41
+ /** Target collection field. Defaults to "id". */
42
+ toField?: string;
43
+ /** MVP supports explicit to-one expansion. */
44
+ cardinality?: 'one' | 'many';
45
+ };
46
+
47
+ export type ObjectFieldOptions = FieldOptions<Record<string, unknown>> & {
48
+ additionalProperties?: boolean;
49
+ };
50
+
51
+ export type ResourceDefinition = {
52
+ description?: string;
53
+ idField?: string;
54
+ source?: FilesSourceDefinition | string | readonly string[];
55
+ fields: Record<string, FieldDefinition>;
56
+ seed?: unknown;
57
+ };
58
+
59
+ export type StandardSchemaIssue = {
60
+ message?: string;
61
+ path?: readonly unknown[];
62
+ [key: string]: unknown;
63
+ };
64
+
65
+ export type StandardSchemaResult<Value = unknown> = {
66
+ value?: Value;
67
+ issues?: readonly StandardSchemaIssue[];
68
+ };
69
+
70
+ export type StandardSchemaV1<Input = unknown, Output = unknown> = {
71
+ '~standard': {
72
+ version: 1;
73
+ vendor?: string;
74
+ validate(value: Input): StandardSchemaResult<Output> | Promise<StandardSchemaResult<Output>>;
75
+ jsonSchema?: {
76
+ output?: (options?: Record<string, unknown>) => unknown;
77
+ };
78
+ [key: string]: unknown;
79
+ };
80
+ };
81
+
82
+ export type StandardSchemaResourceOptions = Omit<ResourceDefinition, 'fields'> & {
83
+ fields?: Record<string, FieldDefinition>;
84
+ };
85
+
86
+ export type StandardSchemaMixedResourceDefinition<Input = unknown, Output = unknown> =
87
+ StandardSchemaResourceOptions & {
88
+ /** Preferred object-first validator hook. */
89
+ validator: StandardSchemaV1<Input, Output>;
90
+ /** Compatibility alias for older examples. Prefer validator. */
91
+ standardSchema?: StandardSchemaV1<Input, Output>;
92
+ };
93
+
94
+ export type StandardSchemaLegacyMixedResourceDefinition<Input = unknown, Output = unknown> =
95
+ StandardSchemaResourceOptions & {
96
+ /** Compatibility alias. Prefer validator. */
97
+ standardSchema: StandardSchemaV1<Input, Output>;
98
+ };
99
+
100
+ export type FilesSourceDefinition = {
101
+ kind: 'files';
102
+ patterns: readonly string[];
103
+ read?: 'frontmatter' | 'json' | 'jsonc' | 'text' | string;
104
+ };
105
+
106
+ export type ComputedResolverThis = {
107
+ get(key: string): unknown;
108
+ has(key: string): boolean;
109
+ value: unknown;
110
+ record: unknown;
111
+ records: unknown[] | undefined;
112
+ args: unknown;
113
+ db: unknown;
114
+ resource: ResourceDefinition & { kind?: 'collection' | 'document' };
115
+ field: FieldDefinition | undefined;
116
+ fieldName: string | undefined;
117
+ config: unknown;
118
+ services: Record<string, unknown>;
119
+ cache: Map<string, unknown>;
120
+ _internal: {
121
+ get(key: string): unknown;
122
+ has(key: string): boolean;
123
+ value: unknown;
124
+ record: unknown;
125
+ records: unknown[] | undefined;
126
+ args: unknown;
127
+ db: unknown;
128
+ resource: ResourceDefinition & { kind?: 'collection' | 'document' };
129
+ field: FieldDefinition | undefined;
130
+ fieldName: string | undefined;
131
+ config: unknown;
132
+ services: Record<string, unknown>;
133
+ cache: Map<string, unknown>;
134
+ };
135
+ };
136
+
137
+ export type ComputedFieldResolver<RecordValue = Record<string, unknown>, Value = unknown> = {
138
+ resolve?: (this: ComputedResolverThis, context: {
139
+ record: RecordValue;
140
+ db: unknown;
141
+ resource: ResourceDefinition & { kind?: 'collection' | 'document' };
142
+ cache: Map<string, unknown>;
143
+ [key: string]: unknown;
144
+ }) => Value | Promise<Value>;
145
+ resolveMany?: (this: ComputedResolverThis, context: {
146
+ records: RecordValue[];
147
+ db: unknown;
148
+ resource: ResourceDefinition & { kind?: 'collection' | 'document' };
149
+ cache: Map<string, unknown>;
150
+ [key: string]: unknown;
151
+ }) => Map<string | number, Value> | Value[] | Record<string, Value> | Promise<Map<string | number, Value> | Value[] | Record<string, Value>>;
152
+ };
153
+
154
+ export function collection(definition: ResourceDefinition): ResourceDefinition & { kind: 'collection' };
155
+ export function collection<Input = unknown, Output = unknown>(
156
+ definition: StandardSchemaMixedResourceDefinition<Input, Output>,
157
+ ): StandardSchemaMixedResourceDefinition<Input, Output> & { kind: 'collection' };
158
+ export function collection<Input = unknown, Output = unknown>(
159
+ definition: StandardSchemaLegacyMixedResourceDefinition<Input, Output>,
160
+ ): StandardSchemaLegacyMixedResourceDefinition<Input, Output> & { kind: 'collection' };
161
+ export function collection<Input = unknown, Output = unknown>(
162
+ definition: StandardSchemaV1<Input, Output>,
163
+ options?: StandardSchemaResourceOptions,
164
+ ): StandardSchemaResourceOptions & { kind: 'collection'; validator: StandardSchemaV1<Input, Output> };
165
+ export function document(definition: ResourceDefinition): ResourceDefinition & { kind: 'document' };
166
+ export function document<Input = unknown, Output = unknown>(
167
+ definition: StandardSchemaMixedResourceDefinition<Input, Output>,
168
+ ): StandardSchemaMixedResourceDefinition<Input, Output> & { kind: 'document' };
169
+ export function document<Input = unknown, Output = unknown>(
170
+ definition: StandardSchemaLegacyMixedResourceDefinition<Input, Output>,
171
+ ): StandardSchemaLegacyMixedResourceDefinition<Input, Output> & { kind: 'document' };
172
+ export function document<Input = unknown, Output = unknown>(
173
+ definition: StandardSchemaV1<Input, Output>,
174
+ options?: StandardSchemaResourceOptions,
175
+ ): StandardSchemaResourceOptions & { kind: 'document'; validator: StandardSchemaV1<Input, Output> };
176
+ export function files(patterns: string | readonly string[], options?: { read?: FilesSourceDefinition['read'] }): FilesSourceDefinition;
177
+
178
+ export const field: {
179
+ string(options?: FieldOptions<string>): FieldDefinition;
180
+ datetime(options?: FieldOptions<string>): FieldDefinition;
181
+ number(options?: FieldOptions<number>): FieldDefinition;
182
+ boolean(options?: FieldOptions<boolean>): FieldDefinition;
183
+ enum<const Values extends readonly (string | number | boolean)[]>(
184
+ values: Values,
185
+ options?: FieldOptions<Values[number]>,
186
+ ): FieldDefinition;
187
+ object(fields?: Record<string, FieldDefinition>, options?: ObjectFieldOptions): FieldDefinition;
188
+ array(items?: FieldDefinition, options?: FieldOptions<unknown[]>): FieldDefinition;
189
+ json(options?: FieldOptions<unknown>): FieldDefinition;
190
+ meta(options?: FieldMetaOptions): FieldDefinition;
191
+ nullable(definition: FieldDefinition, options?: Omit<FieldOptions<unknown>, 'nullable'>): FieldDefinition;
192
+ computed(definition: FieldDefinition, resolver?: ComputedFieldResolver['resolve'] | ComputedFieldResolver): FieldDefinition;
193
+ };
package/dist/schema.js ADDED
@@ -0,0 +1,6 @@
1
+ export { loadProjectSchema } from './features/schema/project.js';
2
+ export { createDbSchema, createSchemaValidator, loadDbSchema } from './features/schema/api.js';
3
+ export { makeGeneratedSchema } from './features/schema/generated.js';
4
+ export { inferFieldFromSamples, inferFieldFromValue, inferFieldsFromData, normalizeField } from './features/schema/fields.js';
5
+ export { normalizeSchemaLoadMode, resolveSchemaLocator } from './features/schema/locator.js';
6
+ export { assertRecordMatchesResource, uniqueDuplicateDiagnostic, validateRecordAgainstResource, validateUniqueCollectionFields, validateValueAgainstField } from './features/schema/validation.js';
@@ -0,0 +1,116 @@
1
+ import { type IncomingMessage, type Server, type ServerResponse } from 'node:http';
2
+ import { watch } from 'node:fs';
3
+ type ServerTraceConfig = boolean | {
4
+ enabled?: boolean;
5
+ slowMs?: number;
6
+ console?: boolean;
7
+ events?: boolean;
8
+ header?: string;
9
+ } | null;
10
+ type ServerConfig = {
11
+ cwd?: string;
12
+ sourceDir?: string;
13
+ stateDir?: string;
14
+ server?: {
15
+ host?: string;
16
+ port?: number | string;
17
+ apiBase?: string;
18
+ dataPath?: string | false | null;
19
+ maxBodyBytes?: number;
20
+ trace?: ServerTraceConfig;
21
+ expose?: Record<string, unknown>;
22
+ [key: string]: unknown;
23
+ };
24
+ graphql?: {
25
+ enabled?: boolean;
26
+ path?: string;
27
+ [key: string]: unknown;
28
+ };
29
+ rest?: {
30
+ enabled?: boolean;
31
+ [key: string]: unknown;
32
+ };
33
+ operations?: {
34
+ sourceDir?: string;
35
+ [key: string]: unknown;
36
+ };
37
+ [key: string]: unknown;
38
+ };
39
+ type ServerResource = {
40
+ name: string;
41
+ kind?: string;
42
+ routePath?: string;
43
+ [key: string]: unknown;
44
+ };
45
+ type ServerDb = {
46
+ config: ServerConfig;
47
+ resources: Map<string, ServerResource>;
48
+ diagnostics?: unknown[];
49
+ schemaVersion?: number;
50
+ close?: () => unknown | Promise<unknown>;
51
+ };
52
+ type StartServerOptions = Record<string, unknown> & {
53
+ host?: string;
54
+ port?: string | number;
55
+ };
56
+ type RequestRoutesOptions = Record<string, unknown> & {
57
+ apiBase?: string;
58
+ rootRoutes?: boolean;
59
+ restBasePath?: string | null;
60
+ dataPath?: string | false | null;
61
+ graphqlPath?: string;
62
+ manifestPath?: string;
63
+ manifestJsonPath?: string;
64
+ manifestHtmlPath?: string;
65
+ manifestMarkdownPath?: string;
66
+ };
67
+ type RequestHandlerOptions = RequestRoutesOptions & {
68
+ events?: ViewerEventHub;
69
+ trace?: ServerTraceConfig;
70
+ };
71
+ type DbRequestHandler = (request: IncomingMessage, response: ServerResponse, next?: () => unknown) => Promise<boolean>;
72
+ type ViewerEventPayload = Record<string, unknown> & {
73
+ type: string;
74
+ version?: number;
75
+ diagnostics?: unknown[];
76
+ };
77
+ type ViewerEventHub = {
78
+ subscribe(request: IncomingMessage, response: ServerResponse, db: ServerDb): void;
79
+ publish(payload: ViewerEventPayload): void;
80
+ close(): void;
81
+ };
82
+ type WatchSourceOptions = {
83
+ watch?: typeof watch;
84
+ warn?: (message: string) => unknown;
85
+ };
86
+ type SourceWatcher = {
87
+ readonly enabled: boolean;
88
+ close(): void;
89
+ };
90
+ export declare function startDbServer(options?: StartServerOptions): Promise<{
91
+ server: Server;
92
+ db: ServerDb;
93
+ url: string;
94
+ }>;
95
+ export declare function createDbRequestHandler(db: ServerDb, options?: RequestHandlerOptions): DbRequestHandler;
96
+ export declare function reloadDb(db: ServerDb): Promise<{
97
+ [key: string]: unknown;
98
+ resources: {
99
+ [key: string]: unknown;
100
+ name: string;
101
+ schemaPath?: string | null;
102
+ dataPath?: string | null;
103
+ }[];
104
+ diagnostics: {
105
+ [key: string]: unknown;
106
+ code?: string;
107
+ severity?: string;
108
+ message: string;
109
+ }[];
110
+ schema?: Record<string, unknown>;
111
+ } & {
112
+ logs: string[];
113
+ }>;
114
+ export declare function watchSourceDir(db: ServerDb, events: ViewerEventHub, options?: WatchSourceOptions): Promise<SourceWatcher>;
115
+ export declare function createViewerEventHub(): ViewerEventHub;
116
+ export {};