@dxos/echo 0.8.4-staging.ac66bdf99f → 0.9.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 (437) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +7 -7
  3. package/dist/lib/neutral/Annotation.mjs +37 -6
  4. package/dist/lib/neutral/Database.mjs +6 -17
  5. package/dist/lib/neutral/Entity.mjs +31 -20
  6. package/dist/lib/neutral/Err.mjs +3 -3
  7. package/dist/lib/neutral/Feed.mjs +23 -19
  8. package/dist/lib/neutral/Filter.mjs +13 -15
  9. package/dist/lib/neutral/Format.mjs +23 -3
  10. package/dist/lib/neutral/JsonSchema.mjs +7 -8
  11. package/dist/lib/neutral/Key.mjs +9 -5
  12. package/dist/lib/neutral/Migration.mjs +11 -10
  13. package/dist/lib/neutral/Obj.mjs +29 -20
  14. package/dist/lib/neutral/Order.mjs +7 -3
  15. package/dist/lib/neutral/Query.mjs +17 -17
  16. package/dist/lib/neutral/QueryResult.mjs +1 -1
  17. package/dist/lib/neutral/Ref.mjs +10 -9
  18. package/dist/lib/neutral/Registry.mjs +14 -0
  19. package/dist/lib/neutral/Relation.mjs +28 -25
  20. package/dist/lib/neutral/Scope.mjs +12 -0
  21. package/dist/lib/neutral/Tag.mjs +17 -14
  22. package/dist/lib/neutral/Type.mjs +54 -26
  23. package/dist/lib/neutral/{chunk-OMUPQMLR.mjs → chunk-35INCYOE.mjs} +1 -1
  24. package/dist/lib/neutral/chunk-35INCYOE.mjs.map +7 -0
  25. package/dist/lib/neutral/chunk-3PBP4V4O.mjs +101 -0
  26. package/dist/lib/neutral/chunk-3PBP4V4O.mjs.map +7 -0
  27. package/dist/lib/neutral/chunk-4ZUHOTCG.mjs +184 -0
  28. package/dist/lib/neutral/chunk-4ZUHOTCG.mjs.map +7 -0
  29. package/dist/lib/neutral/chunk-5SMDBFVB.mjs +108 -0
  30. package/dist/lib/neutral/chunk-5SMDBFVB.mjs.map +7 -0
  31. package/dist/lib/neutral/{chunk-OS35K56T.mjs → chunk-5SUJPHAE.mjs} +3 -3
  32. package/dist/lib/neutral/{chunk-OS35K56T.mjs.map → chunk-5SUJPHAE.mjs.map} +2 -2
  33. package/dist/lib/neutral/{chunk-GZQTCRJB.mjs → chunk-6M2Z6WBH.mjs} +22 -2
  34. package/dist/lib/neutral/chunk-6M2Z6WBH.mjs.map +7 -0
  35. package/dist/lib/neutral/{chunk-V36VO5SS.mjs → chunk-6YDI3J37.mjs} +32 -40
  36. package/dist/lib/neutral/chunk-6YDI3J37.mjs.map +7 -0
  37. package/dist/lib/neutral/{chunk-MOR5ERFM.mjs → chunk-7FPIAJIV.mjs} +701 -1256
  38. package/dist/lib/neutral/chunk-7FPIAJIV.mjs.map +7 -0
  39. package/dist/lib/neutral/{chunk-ANHVGJI4.mjs → chunk-7LOUAPYZ.mjs} +9 -5
  40. package/dist/lib/neutral/chunk-7LOUAPYZ.mjs.map +7 -0
  41. package/dist/lib/neutral/{chunk-JUXPFOEI.mjs → chunk-7PI7C4EF.mjs} +48 -88
  42. package/dist/lib/neutral/chunk-7PI7C4EF.mjs.map +7 -0
  43. package/dist/lib/neutral/{chunk-UBEZSGXY.mjs → chunk-BBFJWWAV.mjs} +6 -6
  44. package/dist/lib/neutral/chunk-BBFJWWAV.mjs.map +7 -0
  45. package/dist/lib/neutral/{chunk-UI6MWK5W.mjs → chunk-EVK6XBXO.mjs} +16 -2
  46. package/dist/lib/neutral/chunk-EVK6XBXO.mjs.map +7 -0
  47. package/dist/lib/neutral/{chunk-7RO7CPBZ.mjs → chunk-IGK6FN65.mjs} +2 -2
  48. package/dist/lib/neutral/{chunk-HBUZJNZO.mjs → chunk-LWXVKPPW.mjs} +94 -99
  49. package/dist/lib/neutral/chunk-LWXVKPPW.mjs.map +7 -0
  50. package/dist/lib/neutral/{chunk-BVOFLCVF.mjs → chunk-MZ7K3MLL.mjs} +9 -6
  51. package/dist/lib/neutral/chunk-MZ7K3MLL.mjs.map +7 -0
  52. package/dist/lib/neutral/{chunk-TBKX6JQO.mjs → chunk-O6BH7EPN.mjs} +30 -3
  53. package/dist/lib/neutral/chunk-O6BH7EPN.mjs.map +7 -0
  54. package/dist/lib/neutral/{chunk-EAMSSLZC.mjs → chunk-QQIYS74I.mjs} +83 -46
  55. package/dist/lib/neutral/chunk-QQIYS74I.mjs.map +7 -0
  56. package/dist/lib/neutral/chunk-R5W6DXR4.mjs +678 -0
  57. package/dist/lib/neutral/chunk-R5W6DXR4.mjs.map +7 -0
  58. package/dist/lib/neutral/{chunk-WAK4DMFV.mjs → chunk-RIVWNMSF.mjs} +12 -7
  59. package/dist/lib/neutral/chunk-RIVWNMSF.mjs.map +7 -0
  60. package/dist/lib/neutral/{chunk-T6W2LEZU.mjs → chunk-SBVFRTST.mjs} +73 -38
  61. package/dist/lib/neutral/chunk-SBVFRTST.mjs.map +7 -0
  62. package/dist/lib/neutral/chunk-T6E37YIP.mjs +251 -0
  63. package/dist/lib/neutral/chunk-T6E37YIP.mjs.map +7 -0
  64. package/dist/lib/neutral/{chunk-DQYLD2RB.mjs → chunk-TFEWTY5A.mjs} +155 -129
  65. package/dist/lib/neutral/chunk-TFEWTY5A.mjs.map +7 -0
  66. package/dist/lib/neutral/{chunk-B4BASU6W.mjs → chunk-TYGKCRMK.mjs} +85 -76
  67. package/dist/lib/neutral/chunk-TYGKCRMK.mjs.map +7 -0
  68. package/dist/lib/neutral/{chunk-4OIBYSXE.mjs → chunk-UUP46KUQ.mjs} +78 -32
  69. package/dist/lib/neutral/chunk-UUP46KUQ.mjs.map +7 -0
  70. package/dist/lib/neutral/chunk-WISOH2XH.mjs +36 -0
  71. package/dist/lib/neutral/chunk-WISOH2XH.mjs.map +7 -0
  72. package/dist/lib/neutral/{chunk-G3IQMKF7.mjs → chunk-WTQJHC75.mjs} +111 -112
  73. package/dist/lib/neutral/chunk-WTQJHC75.mjs.map +7 -0
  74. package/dist/lib/neutral/chunk-WU3GIANS.mjs +31 -0
  75. package/dist/lib/neutral/chunk-WU3GIANS.mjs.map +7 -0
  76. package/dist/lib/neutral/{chunk-TU3GW67D.mjs → chunk-ZGNNFYHS.mjs} +40 -40
  77. package/dist/lib/neutral/chunk-ZGNNFYHS.mjs.map +7 -0
  78. package/dist/lib/neutral/index.mjs +47 -41
  79. package/dist/lib/neutral/internal/index.mjs +137 -72
  80. package/dist/lib/neutral/meta.json +1 -1
  81. package/dist/lib/neutral/testing/index.mjs +261 -178
  82. package/dist/lib/neutral/testing/index.mjs.map +4 -4
  83. package/dist/types/src/Annotation.d.ts +108 -4
  84. package/dist/types/src/Annotation.d.ts.map +1 -1
  85. package/dist/types/src/Annotation.test.d.ts +2 -0
  86. package/dist/types/src/Annotation.test.d.ts.map +1 -0
  87. package/dist/types/src/Collection.d.ts +2 -3
  88. package/dist/types/src/Collection.d.ts.map +1 -1
  89. package/dist/types/src/Database.d.ts +56 -49
  90. package/dist/types/src/Database.d.ts.map +1 -1
  91. package/dist/types/src/Dataset.d.ts +16 -6
  92. package/dist/types/src/Dataset.d.ts.map +1 -1
  93. package/dist/types/src/Entity.d.ts +101 -28
  94. package/dist/types/src/Entity.d.ts.map +1 -1
  95. package/dist/types/src/Err.d.ts +27 -27
  96. package/dist/types/src/Err.d.ts.map +1 -1
  97. package/dist/types/src/Feed.d.ts +66 -19
  98. package/dist/types/src/Feed.d.ts.map +1 -1
  99. package/dist/types/src/Filter.d.ts +38 -12
  100. package/dist/types/src/Filter.d.ts.map +1 -1
  101. package/dist/types/src/Format.d.ts +0 -2
  102. package/dist/types/src/Format.d.ts.map +1 -1
  103. package/dist/types/src/Hypergraph.d.ts +14 -9
  104. package/dist/types/src/Hypergraph.d.ts.map +1 -1
  105. package/dist/types/src/Json.d.ts +33 -0
  106. package/dist/types/src/Json.d.ts.map +1 -0
  107. package/dist/types/src/Json.test.d.ts +2 -0
  108. package/dist/types/src/Json.test.d.ts.map +1 -0
  109. package/dist/types/src/JsonSchema.d.ts +2 -2
  110. package/dist/types/src/JsonSchema.d.ts.map +1 -1
  111. package/dist/types/src/Key.d.ts +1 -1
  112. package/dist/types/src/Key.d.ts.map +1 -1
  113. package/dist/types/src/Migration.d.ts +26 -11
  114. package/dist/types/src/Migration.d.ts.map +1 -1
  115. package/dist/types/src/Obj.d.ts +104 -61
  116. package/dist/types/src/Obj.d.ts.map +1 -1
  117. package/dist/types/src/Order.d.ts +10 -0
  118. package/dist/types/src/Order.d.ts.map +1 -1
  119. package/dist/types/src/Query.d.ts +34 -12
  120. package/dist/types/src/Query.d.ts.map +1 -1
  121. package/dist/types/src/QueryResult.d.ts +21 -0
  122. package/dist/types/src/QueryResult.d.ts.map +1 -1
  123. package/dist/types/src/Ref.d.ts +15 -7
  124. package/dist/types/src/Ref.d.ts.map +1 -1
  125. package/dist/types/src/Registry.d.ts +131 -0
  126. package/dist/types/src/Registry.d.ts.map +1 -0
  127. package/dist/types/src/Relation.d.ts +73 -41
  128. package/dist/types/src/Relation.d.ts.map +1 -1
  129. package/dist/types/src/Scope.d.ts +35 -0
  130. package/dist/types/src/Scope.d.ts.map +1 -0
  131. package/dist/types/src/Tag.d.ts +21 -5
  132. package/dist/types/src/Tag.d.ts.map +1 -1
  133. package/dist/types/src/Type.d.ts +362 -95
  134. package/dist/types/src/Type.d.ts.map +1 -1
  135. package/dist/types/src/View.d.ts +9 -12
  136. package/dist/types/src/View.d.ts.map +1 -1
  137. package/dist/types/src/exemplars.test.d.ts +2 -0
  138. package/dist/types/src/exemplars.test.d.ts.map +1 -0
  139. package/dist/types/src/index.d.ts +4 -3
  140. package/dist/types/src/index.d.ts.map +1 -1
  141. package/dist/types/src/internal/Annotation/annotations.d.ts +79 -38
  142. package/dist/types/src/internal/Annotation/annotations.d.ts.map +1 -1
  143. package/dist/types/src/internal/Annotation/dictionary.d.ts +24 -0
  144. package/dist/types/src/internal/Annotation/dictionary.d.ts.map +1 -0
  145. package/dist/types/src/internal/Annotation/entity-dictionary.d.ts +14 -0
  146. package/dist/types/src/internal/Annotation/entity-dictionary.d.ts.map +1 -0
  147. package/dist/types/src/internal/Annotation/index.d.ts +4 -2
  148. package/dist/types/src/internal/Annotation/index.d.ts.map +1 -1
  149. package/dist/types/src/internal/Annotation/sorting.d.ts.map +1 -1
  150. package/dist/types/src/internal/Annotation/util.d.ts +14 -5
  151. package/dist/types/src/internal/Annotation/util.d.ts.map +1 -1
  152. package/dist/types/src/internal/Entity/api.d.ts +17 -3
  153. package/dist/types/src/internal/Entity/api.d.ts.map +1 -1
  154. package/dist/types/src/internal/Entity/entity.d.ts +72 -8
  155. package/dist/types/src/internal/Entity/entity.d.ts.map +1 -1
  156. package/dist/types/src/internal/Entity/guard.d.ts +10 -0
  157. package/dist/types/src/internal/Entity/guard.d.ts.map +1 -0
  158. package/dist/types/src/internal/Entity/index.d.ts +2 -0
  159. package/dist/types/src/internal/Entity/index.d.ts.map +1 -1
  160. package/dist/types/src/internal/Entity/model.d.ts +21 -17
  161. package/dist/types/src/internal/Entity/model.d.ts.map +1 -1
  162. package/dist/types/src/internal/Entity/object.d.ts +3 -3
  163. package/dist/types/src/internal/Entity/object.d.ts.map +1 -1
  164. package/dist/types/src/internal/Entity/relation.d.ts +30 -7
  165. package/dist/types/src/internal/Entity/relation.d.ts.map +1 -1
  166. package/dist/types/src/internal/Entity/type-kind.d.ts +24 -0
  167. package/dist/types/src/internal/Entity/type-kind.d.ts.map +1 -0
  168. package/dist/types/src/internal/Entity/type-uri.d.ts +24 -0
  169. package/dist/types/src/internal/Entity/type-uri.d.ts.map +1 -0
  170. package/dist/types/src/internal/Entity/version.d.ts.map +1 -1
  171. package/dist/types/src/internal/Format/date.d.ts.map +1 -1
  172. package/dist/types/src/internal/Format/format.d.ts +3 -2
  173. package/dist/types/src/internal/Format/format.d.ts.map +1 -1
  174. package/dist/types/src/internal/Format/index.d.ts +2 -2
  175. package/dist/types/src/internal/Format/index.d.ts.map +1 -1
  176. package/dist/types/src/internal/Format/number.d.ts.map +1 -1
  177. package/dist/types/src/internal/Format/object.d.ts +3 -1
  178. package/dist/types/src/internal/Format/object.d.ts.map +1 -1
  179. package/dist/types/src/internal/Format/types.d.ts.map +1 -1
  180. package/dist/types/src/internal/JsonSchema/json-schema-normalize.d.ts.map +1 -1
  181. package/dist/types/src/internal/JsonSchema/json-schema-type.d.ts +34 -34
  182. package/dist/types/src/internal/JsonSchema/json-schema-type.d.ts.map +1 -1
  183. package/dist/types/src/internal/JsonSchema/json-schema.d.ts +3 -2
  184. package/dist/types/src/internal/JsonSchema/json-schema.d.ts.map +1 -1
  185. package/dist/types/src/internal/Obj/atoms.d.ts +38 -0
  186. package/dist/types/src/internal/Obj/atoms.d.ts.map +1 -0
  187. package/dist/types/src/internal/Obj/clone.d.ts.map +1 -1
  188. package/dist/types/src/internal/Obj/common.d.ts.map +1 -1
  189. package/dist/types/src/internal/Obj/create-object.d.ts +12 -12
  190. package/dist/types/src/internal/Obj/create-object.d.ts.map +1 -1
  191. package/dist/types/src/internal/Obj/deleted.d.ts.map +1 -1
  192. package/dist/types/src/internal/Obj/index.d.ts +1 -1
  193. package/dist/types/src/internal/Obj/index.d.ts.map +1 -1
  194. package/dist/types/src/internal/Obj/json-serializer.d.ts +8 -8
  195. package/dist/types/src/internal/Obj/json-serializer.d.ts.map +1 -1
  196. package/dist/types/src/internal/Obj/set-value.d.ts +1 -1
  197. package/dist/types/src/internal/Obj/set-value.d.ts.map +1 -1
  198. package/dist/types/src/internal/Obj/snapshot.d.ts.map +1 -1
  199. package/dist/types/src/internal/Obj/typed-object.d.ts +1 -1
  200. package/dist/types/src/internal/Query/index.d.ts +2 -0
  201. package/dist/types/src/internal/Query/index.d.ts.map +1 -0
  202. package/dist/types/src/internal/{Query.d.ts → Query/pretty.d.ts} +1 -1
  203. package/dist/types/src/internal/Query/pretty.d.ts.map +1 -0
  204. package/dist/types/src/internal/Ref/atoms.d.ts +10 -0
  205. package/dist/types/src/internal/Ref/atoms.d.ts.map +1 -0
  206. package/dist/types/src/internal/Ref/ref-array.d.ts +2 -2
  207. package/dist/types/src/internal/Ref/ref.d.ts +50 -19
  208. package/dist/types/src/internal/Ref/ref.d.ts.map +1 -1
  209. package/dist/types/src/internal/Ref/utils.d.ts +8 -0
  210. package/dist/types/src/internal/Ref/utils.d.ts.map +1 -0
  211. package/dist/types/src/internal/Type/compose.d.ts.map +1 -1
  212. package/dist/types/src/internal/Type/index.d.ts +1 -2
  213. package/dist/types/src/internal/Type/index.d.ts.map +1 -1
  214. package/dist/types/src/internal/Type/manipulation.d.ts +0 -1
  215. package/dist/types/src/internal/Type/manipulation.d.ts.map +1 -1
  216. package/dist/types/src/internal/Type/type-schema.d.ts +52 -0
  217. package/dist/types/src/internal/Type/type-schema.d.ts.map +1 -0
  218. package/dist/types/src/internal/common/api/meta.d.ts +14 -11
  219. package/dist/types/src/internal/common/api/meta.d.ts.map +1 -1
  220. package/dist/types/src/internal/common/proxy/change-context.d.ts +1 -1
  221. package/dist/types/src/internal/common/proxy/change-context.d.ts.map +1 -1
  222. package/dist/types/src/internal/common/proxy/define-hidden-property.d.ts.map +1 -1
  223. package/dist/types/src/internal/common/proxy/errors.d.ts +1 -1
  224. package/dist/types/src/internal/common/proxy/errors.d.ts.map +1 -1
  225. package/dist/types/src/internal/common/proxy/event-batch.d.ts.map +1 -1
  226. package/dist/types/src/internal/common/proxy/json-serializer.d.ts.map +1 -1
  227. package/dist/types/src/internal/common/proxy/make-object.d.ts +11 -5
  228. package/dist/types/src/internal/common/proxy/make-object.d.ts.map +1 -1
  229. package/dist/types/src/internal/common/proxy/ownership.d.ts.map +1 -1
  230. package/dist/types/src/internal/common/proxy/proxy-utils.d.ts.map +1 -1
  231. package/dist/types/src/internal/common/proxy/reactive-array.d.ts +1 -1
  232. package/dist/types/src/internal/common/proxy/reactive.d.ts +1 -1
  233. package/dist/types/src/internal/common/proxy/reactive.d.ts.map +1 -1
  234. package/dist/types/src/internal/common/proxy/reactive.test.d.ts +2 -0
  235. package/dist/types/src/internal/common/proxy/reactive.test.d.ts.map +1 -0
  236. package/dist/types/src/internal/common/proxy/schema-validator.d.ts.map +1 -1
  237. package/dist/types/src/internal/common/proxy/typed-handler.d.ts +18 -2
  238. package/dist/types/src/internal/common/proxy/typed-handler.d.ts.map +1 -1
  239. package/dist/types/src/internal/common/types/base.d.ts +4 -4
  240. package/dist/types/src/internal/common/types/base.d.ts.map +1 -1
  241. package/dist/types/src/internal/common/types/entity.d.ts +62 -5
  242. package/dist/types/src/internal/common/types/entity.d.ts.map +1 -1
  243. package/dist/types/src/internal/common/types/index.d.ts +1 -1
  244. package/dist/types/src/internal/common/types/index.d.ts.map +1 -1
  245. package/dist/types/src/internal/common/types/meta.d.ts +33 -12
  246. package/dist/types/src/internal/common/types/meta.d.ts.map +1 -1
  247. package/dist/types/src/internal/common/types/model-symbols.d.ts +15 -4
  248. package/dist/types/src/internal/common/types/model-symbols.d.ts.map +1 -1
  249. package/dist/types/src/internal/common/types/typename.d.ts +7 -0
  250. package/dist/types/src/internal/common/types/typename.d.ts.map +1 -1
  251. package/dist/types/src/internal/common/types/version.d.ts +1 -1
  252. package/dist/types/src/internal/common/types/well-known-types.d.ts +11 -0
  253. package/dist/types/src/internal/common/types/well-known-types.d.ts.map +1 -0
  254. package/dist/types/src/internal/index.d.ts +2 -2
  255. package/dist/types/src/internal/index.d.ts.map +1 -1
  256. package/dist/types/src/testing/index.d.ts +1 -0
  257. package/dist/types/src/testing/index.d.ts.map +1 -1
  258. package/dist/types/src/testing/registry.d.ts +9 -0
  259. package/dist/types/src/testing/registry.d.ts.map +1 -0
  260. package/dist/types/src/testing/test-data.d.ts +8 -8
  261. package/dist/types/src/testing/test-data.d.ts.map +1 -1
  262. package/dist/types/src/testing/test-schema.d.ts +83 -89
  263. package/dist/types/src/testing/test-schema.d.ts.map +1 -1
  264. package/dist/types/src/testing/util.d.ts +5 -3
  265. package/dist/types/src/testing/util.d.ts.map +1 -1
  266. package/dist/types/tsconfig.tsbuildinfo +1 -1
  267. package/package.json +26 -24
  268. package/src/Annotation.test.ts +439 -0
  269. package/src/Annotation.ts +158 -4
  270. package/src/Collection.ts +5 -9
  271. package/src/Database.ts +93 -100
  272. package/src/Dataset.ts +10 -2
  273. package/src/Entity.test.ts +116 -6
  274. package/src/Entity.ts +134 -32
  275. package/src/Err.ts +4 -4
  276. package/src/Feed.ts +92 -44
  277. package/src/Filter.ts +70 -40
  278. package/src/Format.ts +0 -4
  279. package/src/Hypergraph.ts +14 -9
  280. package/src/Json.test.ts +175 -0
  281. package/src/Json.ts +103 -0
  282. package/src/Key.ts +1 -1
  283. package/src/Migration.ts +39 -19
  284. package/src/Obj.test.ts +122 -20
  285. package/src/Obj.ts +168 -91
  286. package/src/Order.ts +22 -0
  287. package/src/Query.test.ts +183 -154
  288. package/src/Query.ts +172 -85
  289. package/src/QueryResult.ts +26 -0
  290. package/src/Ref.ts +22 -4
  291. package/src/Registry.ts +155 -0
  292. package/src/Relation.test.ts +10 -10
  293. package/src/Relation.ts +116 -69
  294. package/src/Scope.ts +50 -0
  295. package/src/Tag.md +88 -0
  296. package/src/Tag.ts +49 -6
  297. package/src/Type.test.ts +223 -18
  298. package/src/Type.ts +609 -131
  299. package/src/View.ts +14 -23
  300. package/src/exemplars.test.ts +21 -0
  301. package/src/index.ts +4 -4
  302. package/src/internal/Annotation/annotations.test.ts +31 -11
  303. package/src/internal/Annotation/annotations.ts +143 -111
  304. package/src/internal/Annotation/dictionary.ts +47 -0
  305. package/src/internal/Annotation/entity-dictionary.ts +74 -0
  306. package/src/internal/Annotation/index.ts +4 -2
  307. package/src/internal/Annotation/util.ts +17 -8
  308. package/src/internal/Entity/api.ts +54 -7
  309. package/src/internal/Entity/entity.ts +196 -47
  310. package/src/internal/Entity/guard.ts +26 -0
  311. package/src/internal/Entity/index.ts +2 -0
  312. package/src/internal/Entity/model.ts +38 -28
  313. package/src/internal/Entity/object.ts +21 -5
  314. package/src/internal/Entity/relation.ts +68 -34
  315. package/src/internal/Entity/type-kind.ts +75 -0
  316. package/src/internal/Entity/type-uri.ts +92 -0
  317. package/src/internal/Entity/util.ts +9 -9
  318. package/src/internal/Format/date.ts +0 -4
  319. package/src/internal/Format/format.test.ts +21 -0
  320. package/src/internal/Format/index.ts +2 -3
  321. package/src/internal/Format/object.ts +21 -4
  322. package/src/internal/Format/types.ts +1 -1
  323. package/src/internal/JsonSchema/annotations.ts +1 -1
  324. package/src/internal/JsonSchema/json-schema-type.ts +4 -4
  325. package/src/internal/JsonSchema/json-schema.test.ts +71 -145
  326. package/src/internal/JsonSchema/json-schema.ts +49 -35
  327. package/src/internal/Obj/atoms.ts +244 -0
  328. package/src/internal/Obj/clone.ts +9 -4
  329. package/src/internal/Obj/create-object.test.ts +12 -10
  330. package/src/internal/Obj/create-object.ts +68 -22
  331. package/src/internal/Obj/index.ts +1 -1
  332. package/src/internal/Obj/inspect.ts +5 -3
  333. package/src/internal/Obj/json-serializer.test.ts +101 -22
  334. package/src/internal/Obj/json-serializer.ts +89 -33
  335. package/src/internal/Obj/set-value.test.ts +22 -45
  336. package/src/internal/Obj/set-value.ts +12 -19
  337. package/src/internal/Obj/snapshot.ts +13 -4
  338. package/src/internal/Obj/typed-object.test.ts +9 -11
  339. package/src/internal/Obj/typed-object.ts +1 -1
  340. package/src/internal/Query/index.ts +5 -0
  341. package/src/internal/{Query.ts → Query/pretty.ts} +40 -12
  342. package/src/internal/Ref/atoms.ts +20 -0
  343. package/src/internal/Ref/ref-array.ts +3 -3
  344. package/src/internal/Ref/ref.test.ts +18 -27
  345. package/src/internal/Ref/ref.ts +137 -59
  346. package/src/internal/Ref/utils.ts +45 -0
  347. package/src/internal/Type/compose.test.ts +3 -1
  348. package/src/internal/Type/index.ts +1 -2
  349. package/src/internal/Type/manipulation.ts +0 -25
  350. package/src/internal/Type/type-schema.ts +60 -0
  351. package/src/internal/common/README.md +2 -2
  352. package/src/internal/common/api/meta.ts +19 -17
  353. package/src/internal/common/proxy/change-context.ts +1 -1
  354. package/src/internal/common/proxy/change.test.ts +91 -83
  355. package/src/internal/common/proxy/errors.ts +2 -2
  356. package/src/internal/common/proxy/handler.test.ts +1 -1
  357. package/src/internal/common/proxy/json-serializer.ts +27 -16
  358. package/src/internal/common/proxy/make-object.ts +44 -20
  359. package/src/internal/common/proxy/ownership.ts +2 -2
  360. package/src/internal/common/proxy/reactive-array.ts +1 -1
  361. package/src/internal/common/proxy/reactive.test.ts +54 -0
  362. package/src/internal/common/proxy/reactive.ts +11 -2
  363. package/src/internal/common/proxy/schema.test.ts +48 -86
  364. package/src/internal/common/proxy/typed-handler.test.ts +12 -11
  365. package/src/internal/common/proxy/typed-handler.ts +78 -16
  366. package/src/internal/common/proxy/typed-object.test.ts +16 -28
  367. package/src/internal/common/types/base.ts +4 -4
  368. package/src/internal/common/types/entity.ts +80 -1
  369. package/src/internal/common/types/index.ts +6 -1
  370. package/src/internal/common/types/meta.ts +62 -20
  371. package/src/internal/common/types/model-symbols.ts +24 -4
  372. package/src/internal/common/types/typename.ts +39 -3
  373. package/src/internal/common/types/well-known-types.ts +15 -0
  374. package/src/internal/index.ts +6 -4
  375. package/src/testing/api.test.ts +15 -9
  376. package/src/testing/index.ts +1 -0
  377. package/src/testing/registry.ts +44 -0
  378. package/src/testing/test-data.ts +159 -99
  379. package/src/testing/test-schema.ts +22 -58
  380. package/src/testing/util.ts +14 -11
  381. package/dist/lib/neutral/Extension.mjs +0 -18
  382. package/dist/lib/neutral/SchemaRegistry.mjs +0 -2
  383. package/dist/lib/neutral/chunk-2KHZ36F5.mjs +0 -361
  384. package/dist/lib/neutral/chunk-2KHZ36F5.mjs.map +0 -7
  385. package/dist/lib/neutral/chunk-4OIBYSXE.mjs.map +0 -7
  386. package/dist/lib/neutral/chunk-4P3IXBLT.mjs +0 -45
  387. package/dist/lib/neutral/chunk-4P3IXBLT.mjs.map +0 -7
  388. package/dist/lib/neutral/chunk-ANHVGJI4.mjs.map +0 -7
  389. package/dist/lib/neutral/chunk-B4BASU6W.mjs.map +0 -7
  390. package/dist/lib/neutral/chunk-BNCCGLJN.mjs +0 -7
  391. package/dist/lib/neutral/chunk-BNCCGLJN.mjs.map +0 -7
  392. package/dist/lib/neutral/chunk-BVOFLCVF.mjs.map +0 -7
  393. package/dist/lib/neutral/chunk-DQYLD2RB.mjs.map +0 -7
  394. package/dist/lib/neutral/chunk-EAMSSLZC.mjs.map +0 -7
  395. package/dist/lib/neutral/chunk-G3IQMKF7.mjs.map +0 -7
  396. package/dist/lib/neutral/chunk-GZQTCRJB.mjs.map +0 -7
  397. package/dist/lib/neutral/chunk-HBUZJNZO.mjs.map +0 -7
  398. package/dist/lib/neutral/chunk-JUXPFOEI.mjs.map +0 -7
  399. package/dist/lib/neutral/chunk-MOR5ERFM.mjs.map +0 -7
  400. package/dist/lib/neutral/chunk-OMUPQMLR.mjs.map +0 -7
  401. package/dist/lib/neutral/chunk-PHU22NLC.mjs +0 -136
  402. package/dist/lib/neutral/chunk-PHU22NLC.mjs.map +0 -7
  403. package/dist/lib/neutral/chunk-ROG4RXXL.mjs +0 -97
  404. package/dist/lib/neutral/chunk-ROG4RXXL.mjs.map +0 -7
  405. package/dist/lib/neutral/chunk-T6W2LEZU.mjs.map +0 -7
  406. package/dist/lib/neutral/chunk-TBKX6JQO.mjs.map +0 -7
  407. package/dist/lib/neutral/chunk-TU3GW67D.mjs.map +0 -7
  408. package/dist/lib/neutral/chunk-UBEZSGXY.mjs.map +0 -7
  409. package/dist/lib/neutral/chunk-UI6MWK5W.mjs.map +0 -7
  410. package/dist/lib/neutral/chunk-V36VO5SS.mjs.map +0 -7
  411. package/dist/lib/neutral/chunk-WAK4DMFV.mjs.map +0 -7
  412. package/dist/lib/neutral/chunk-YAHXAYOW.mjs +0 -56
  413. package/dist/lib/neutral/chunk-YAHXAYOW.mjs.map +0 -7
  414. package/dist/lib/neutral/chunk-YS6Q3XAD.mjs +0 -50
  415. package/dist/lib/neutral/chunk-YS6Q3XAD.mjs.map +0 -7
  416. package/dist/types/src/Extension.d.ts +0 -80
  417. package/dist/types/src/Extension.d.ts.map +0 -1
  418. package/dist/types/src/Extension.test.d.ts +0 -2
  419. package/dist/types/src/Extension.test.d.ts.map +0 -1
  420. package/dist/types/src/SchemaRegistry.d.ts +0 -84
  421. package/dist/types/src/SchemaRegistry.d.ts.map +0 -1
  422. package/dist/types/src/internal/Obj/ids.d.ts +0 -6
  423. package/dist/types/src/internal/Obj/ids.d.ts.map +0 -1
  424. package/dist/types/src/internal/Query.d.ts.map +0 -1
  425. package/dist/types/src/internal/Type/echo-schema.d.ts +0 -181
  426. package/dist/types/src/internal/Type/echo-schema.d.ts.map +0 -1
  427. package/dist/types/src/internal/Type/persistent-schema.d.ts +0 -20
  428. package/dist/types/src/internal/Type/persistent-schema.d.ts.map +0 -1
  429. package/src/Extension.test.ts +0 -235
  430. package/src/Extension.ts +0 -122
  431. package/src/SchemaRegistry.ts +0 -106
  432. package/src/internal/Obj/ids.ts +0 -12
  433. package/src/internal/Type/echo-schema.ts +0 -423
  434. package/src/internal/Type/persistent-schema.ts +0 -33
  435. /package/dist/lib/neutral/{Extension.mjs.map → Registry.mjs.map} +0 -0
  436. /package/dist/lib/neutral/{SchemaRegistry.mjs.map → Scope.mjs.map} +0 -0
  437. /package/dist/lib/neutral/{chunk-7RO7CPBZ.mjs.map → chunk-IGK6FN65.mjs.map} +0 -0
package/src/Type.ts CHANGED
@@ -4,70 +4,91 @@
4
4
 
5
5
  // @import-as-namespace
6
6
 
7
- import type * as Schema from 'effect/Schema';
7
+ import * as Schema from 'effect/Schema';
8
+ import type * as Types from 'effect/Types';
8
9
 
10
+ import { raise } from '@dxos/debug';
9
11
  import { type EncodedReference } from '@dxos/echo-protocol';
10
- import { invariant } from '@dxos/invariant';
11
- import { type DXN } from '@dxos/keys';
12
- import { type ToMutable } from '@dxos/util';
12
+ import { assertArgument, invariant } from '@dxos/invariant';
13
+ import { DXN, EID, type EntityId, type URI } from '@dxos/keys';
13
14
 
14
- import type * as Entity from './Entity';
15
+ import type * as Database from './Database';
16
+ import type * as EntityModule from './Entity';
15
17
  import * as internal from './internal';
16
18
  import * as typeInternal from './internal/Type';
17
- import type * as ObjModule from './Obj';
18
19
  import type * as RelationModule from './Relation';
19
20
 
20
- /**
21
- * Dynamic type that can be constructed, mutated, and persisted in the ECHO database.
22
- */
23
- export const RuntimeType = typeInternal.EchoSchema;
24
-
25
- /**
26
- * Dynamic type that can be constructed, mutated, and persisted in the ECHO database.
27
- */
28
- export type RuntimeType = typeInternal.EchoSchema;
29
-
30
21
  //
31
22
  // Internal types (not exported)
32
23
  //
33
24
 
34
25
  /**
35
- * Type that marks a schema as an ECHO schema.
36
- * The value indicates the entity kind (Object or Relation).
26
+ * Structural base shared by the three sibling type-entity interfaces
27
+ * ({@link Obj}, {@link Relation}, {@link Type}). NOT exported — callers
28
+ * should constrain on {@link AnyEntity} when they want "any of the three"
29
+ * and on the specific kind interface otherwise.
37
30
  */
38
- type EchoSchemaKind<K extends internal.EntityKind = internal.EntityKind> = {
39
- readonly [internal.SchemaKindId]: K;
40
- };
31
+ interface BaseTypeEntity<A> {
32
+ /**
33
+ * Entity-kind brand of the type-entity value itself — always `EntityKind.Type`.
34
+ * The kind of instance the type *describes* lives on `[SchemaKindId]`
35
+ * (Object / Relation / Type). Lets `Obj.isObject` / `Relation.isRelation`
36
+ * reject type entities by a single `[KindId]` check.
37
+ */
38
+ readonly [internal.KindId]: internal.EntityKind.Type;
39
+
40
+ /**
41
+ * Object id. Like all ECHO entities, type entities always carry an id —
42
+ * stamped at construction for in-memory (static) declarations and assigned by
43
+ * the database once persisted. The id does NOT determine the entity's URI:
44
+ * static types resolve to their typename DXN, persisted types to `echo:/<id>`
45
+ * (see `getTypeURIFromSpecifier`).
46
+ */
47
+ readonly id: EntityId;
48
+
49
+ readonly name?: string;
50
+ // NOTE: `typename` / `version` are intentionally NOT fields on any type-entity
51
+ // interface. Both static and persisted entities carry them in `EntityMeta`
52
+ // (`key` / `version`); read them via `Type.getTypename(self)` /
53
+ // `Type.getVersion(self)` — never as a direct property.
54
+ readonly jsonSchema: internal.JsonSchemaType;
55
+ readonly [InstancePhantomId]?: A;
56
+ }
41
57
 
42
58
  //
43
- // Obj - Runtime schema for any ECHO object
59
+ // Obj `Type.Type` value for an ECHO object schema.
44
60
  //
45
61
 
46
62
  /**
47
- * TypeScript type for an ECHO object schema.
48
- * `T` is the instance type produced by the schema.
49
- * `Fields` is the optional struct fields type for introspection.
63
+ * TypeScript type for an ECHO object type — a `Type.Type<A>` entity.
64
+ *
65
+ * `T` is the instance type produced by `Obj.make(Foo, props)`. `Fields` is
66
+ * retained as a structural hint (the runtime value still carries `.fields`),
67
+ * but consumers should derive instance/encoded types via `Type.InstanceType`.
68
+ *
69
+ * **Not a `Schema.Schema`.** `Foo.ast` / `Schema.Schema.Type<typeof Foo>` /
70
+ * `Schema.extend(Foo)` no longer typecheck — extract the Effect Schema via
71
+ * `Type.getSchema(Foo)` first, or derive instance types via
72
+ * `Type.InstanceType<typeof Foo>`.
50
73
  *
51
74
  * @example
52
75
  * ```ts
53
- * const PersonSchema: Type.Obj<Person> = Schema.Struct({
76
+ * const Person = Schema.Struct({
54
77
  * name: Schema.String,
55
- * }).pipe(Type.object({ typename: 'Person', version: '0.1.0' }));
78
+ * }).pipe(Type.makeObject(DXN.make('com.example.type.person', '0.1.0')));
56
79
  *
57
- * // Access fields for introspection:
58
- * Object.keys(PersonSchema.fields); // ['name']
80
+ * type Person = Type.InstanceType<typeof Person>;
59
81
  * ```
60
82
  */
61
- export interface Obj<T, Fields extends Schema.Struct.Fields = Schema.Struct.Fields>
62
- extends
63
- internal.TypeMeta,
64
- EchoSchemaKind<internal.EntityKind.Object>,
65
- Schema.AnnotableClass<
66
- Obj<T, Fields>,
67
- Entity.OfKind<typeof Entity.Kind.Object> & T,
68
- Schema.Simplify<ObjModule.BaseObjJson & ToMutable<T>>,
69
- never
70
- > {
83
+ export interface Obj<T, Fields extends Schema.Struct.Fields = Schema.Struct.Fields> extends BaseTypeEntity<
84
+ T & EntityModule.OfKind<typeof EntityModule.Kind.Object>
85
+ > {
86
+ /** Schema-kind brand (object). */
87
+ readonly [internal.SchemaKindId]: internal.EntityKind.Object;
88
+
89
+ /** Source Effect Schema — used internally by `Type.getSchema(self)`. */
90
+ readonly [internal.StaticTypeSchemaSlot]: Schema.Schema.AnyNoContext;
91
+
71
92
  /**
72
93
  * The fields defined in the original struct schema.
73
94
  * Allows accessing field definitions for introspection.
@@ -76,61 +97,157 @@ export interface Obj<T, Fields extends Schema.Struct.Fields = Schema.Struct.Fiel
76
97
  }
77
98
 
78
99
  /**
79
- * Structural base type for any ECHO object schema.
80
- * Accepts both static schemas (created with Type.object()) and EchoSchema.
81
- * NOTE: Does not include the brand symbol to avoid TS4053 declaration portability issues.
82
- * Use Type.isObjectSchema() for runtime type guards.
83
- */
84
- type ObjectSchemaBase = Schema.Schema.AnyNoContext & {
85
- readonly typename: string;
86
- readonly version: string;
87
- };
88
-
89
- /**
90
- * Type that represents any ECHO object schema.
91
- * Accepts both static schemas (Type.object()) and mutable schemas (EchoSchema).
100
+ * Type that represents any ECHO object type — a `Type.Type` entity branded
101
+ * with the object entity kind, i.e. what `Type.makeObject(dxn)` produces.
92
102
  */
93
- export type AnyObj = ObjectSchemaBase;
103
+ export type AnyObj = Obj<unknown>;
94
104
 
95
105
  /**
96
- * Factory function to create an ECHO object schema.
97
- * Adds object metadata annotations to an Effect schema.
106
+ * Factory function to create an ECHO object type.
107
+ *
108
+ * Returns a `Type.Type` entity — a live, in-memory `TypeSchema` instance,
109
+ * NOT a `Schema.Schema`. Use `Type.InstanceType<typeof Foo>` for the instance
110
+ * type and `Type.getSchema(Foo)` to obtain the underlying Effect Schema.
111
+ *
112
+ * The entity's id defaults to `EntityId.deterministic(typename, version)` so
113
+ * constructing a type never reaches `crypto.getRandomValues()` — required for
114
+ * Cloudflare workerd, which forbids RNG calls in global (module-evaluation)
115
+ * scope. Pass `{ id }` to override (e.g. with `EntityId.random()` from a
116
+ * request handler).
98
117
  *
99
118
  * @example
100
119
  * ```ts
101
120
  * const Person = Schema.Struct({
102
121
  * name: Schema.String,
103
- * }).pipe(Type.object({ typename: 'com.example.type.person', version: '0.1.0' }));
122
+ * }).pipe(Type.makeObject(DXN.make('com.example.type.person', '0.1.0')));
104
123
  * ```
105
124
  */
106
- export const object: {
107
- (opts: internal.TypeMeta): <Self extends Schema.Schema.Any>(self: Self) => Obj<Schema.Schema.Type<Self>>;
125
+ export const makeObject: {
126
+ (
127
+ dxn: DXN.DXN,
128
+ options?: { id?: EntityId },
129
+ ): <Self extends Schema.Schema.Any>(self: Self) => Obj<Schema.Schema.Type<Self>>;
108
130
  } = internal.EchoObjectSchema as any;
109
131
 
110
132
  //
111
- // PersistentType (Schema stored in database)
133
+ // Type the ECHO entity that holds a schema and metadata.
134
+ // Persisted via `db.addType()`; subscribed to via `Filter.type(Type.Type)`.
112
135
  //
113
136
 
114
- export const PersistentType: Obj<typeInternal.PersistentSchema> = typeInternal.PersistentSchema as any;
137
+ /**
138
+ * ECHO meta-schema entity — stores `{ name?, typename, version, jsonSchema }`.
139
+ * Type-kind sibling of `Type.makeObject(...)` / `Type.makeRelation(...)` outputs.
140
+ * Stored types live under this entity; filter via `Filter.type(Type.Type)`.
141
+ */
142
+ export const Type: Type<typeInternal.TypeSchema> = typeInternal.TypeSchema as any;
143
+
144
+ /**
145
+ * Default version stamped on draft (unnamed) types created via
146
+ * {@link makeObjectFromJsonSchema} / {@link makeRelationFromJsonSchema} when
147
+ * the caller does not supply one. Pure dynamic drafts surface as `'0.0.0'`
148
+ * until they are persisted, at which point automerge-heads suffix the version.
149
+ */
150
+ const DRAFT_VERSION = '0.0.0';
115
151
 
116
- export interface PersistentType extends Schema.Schema.Type<typeof PersistentType> {}
152
+ /**
153
+ * Common props shared by the type-kind factories. Typename and version are
154
+ * optional — drafts omit typename and default version to {@link DRAFT_VERSION}.
155
+ */
156
+ type MakeTypeProps = {
157
+ jsonSchema: internal.JsonSchemaType;
158
+ typename?: string;
159
+ version?: string;
160
+ name?: string;
161
+ id?: EntityId;
162
+ };
117
163
 
118
164
  /**
119
- * TypeScript type for an ECHO relation schema.
120
- * `T` is the instance type produced by the schema (excluding source/target).
121
- * `Source` and `Target` are the endpoint types.
122
- * `Fields` is the optional struct fields type for introspection.
165
+ * Construct a new object-kind type entity from raw metadata — for cases where
166
+ * an Effect Schema isn't available (e.g. JSON-Schema arriving over the network
167
+ * or from a UI editor). Parallel to {@link makeObject} but takes pre-built
168
+ * `jsonSchema` instead of piping through an Effect schema.
169
+ *
170
+ * The returned entity is in-memory; persist it with `db.addType(entity)`.
123
171
  */
124
- export interface Relation<T, Source, Target, Fields extends Schema.Struct.Fields = Schema.Struct.Fields>
125
- extends
126
- internal.TypeMeta,
127
- EchoSchemaKind<internal.EntityKind.Relation>,
128
- Schema.AnnotableClass<
129
- Relation<T, Source, Target, Fields>,
130
- Entity.OfKind<typeof Entity.Kind.Relation> & RelationModule.Endpoints<Source, Target> & T,
131
- Schema.Simplify<RelationModule.BaseRelationJson & ToMutable<T>>,
132
- never
133
- > {
172
+ export const makeObjectFromJsonSchema = (props: MakeTypeProps): Type<typeInternal.TypeSchema> => {
173
+ const { typename, version, ...data } = props;
174
+ // `typename` / `version` are routed through `EntityMeta` (`key` / `version`)
175
+ // — the canonical registry-provenance pair — not data fields. Drafts default
176
+ // to `'0.0.0'`; the version is omitted from meta entirely when the caller
177
+ // doesn't supply one so the proxy projection can apply its own default.
178
+ return internal.makeObject(
179
+ internal.getStaticTypeSchema(typeInternal.TypeSchema) as any,
180
+ data as any,
181
+ {
182
+ keys: [],
183
+ key: typename,
184
+ version: version ?? DRAFT_VERSION,
185
+ },
186
+ typeInternal.TypeSchema,
187
+ ) as unknown as Type<typeInternal.TypeSchema>;
188
+ };
189
+
190
+ /**
191
+ * Construct a new relation-kind type entity from raw metadata. Parallel to
192
+ * {@link makeRelation} but takes pre-built `jsonSchema` instead of piping
193
+ * through an Effect schema. `source` / `target` accept either a static
194
+ * `Type.Obj` entity or the well-known `Obj.Unknown` schema.
195
+ *
196
+ * The returned entity is in-memory; persist it with `db.addType(entity)`.
197
+ */
198
+ export const makeRelationFromJsonSchema = (
199
+ props: MakeTypeProps & {
200
+ source: AnyObj | internal.UnknownTypeSchema<any, typeof EntityModule.Kind.Object>;
201
+ target: AnyObj | internal.UnknownTypeSchema<any, typeof EntityModule.Kind.Object>;
202
+ },
203
+ ): Type<typeInternal.TypeSchema> => {
204
+ const { source, target, jsonSchema, typename, version, ...rest } = props;
205
+ // Embed source/target DXNs + relation entity-kind into the jsonSchema so the
206
+ // entity round-trips correctly through `toEffectSchema` / queries / refs.
207
+ const sourceURI = internal.getTypeURIFromSpecifier(source);
208
+ const targetURI = internal.getTypeURIFromSpecifier(target);
209
+ const enrichedJsonSchema: internal.JsonSchemaType = {
210
+ ...jsonSchema,
211
+ entityKind: internal.EntityKind.Relation,
212
+ relationSource: { $ref: sourceURI },
213
+ relationTarget: { $ref: targetURI },
214
+ };
215
+ // `typename` / `version` route through `EntityMeta` (see
216
+ // {@link makeObjectFromJsonSchema}); drafts default version to `'0.0.0'`.
217
+ return internal.makeObject(
218
+ internal.getStaticTypeSchema(typeInternal.TypeSchema) as any,
219
+ { ...rest, jsonSchema: enrichedJsonSchema } as any,
220
+ {
221
+ keys: [],
222
+ key: typename,
223
+ version: version ?? DRAFT_VERSION,
224
+ },
225
+ typeInternal.TypeSchema,
226
+ ) as unknown as Type<typeInternal.TypeSchema>;
227
+ };
228
+
229
+ /**
230
+ * TypeScript type for an ECHO relation type — a `Type.Type<A>` entity.
231
+ *
232
+ * `T` is the instance-property type produced by `Relation.make(...)` (excluding
233
+ * source/target endpoints). `Source` and `Target` are the endpoint types.
234
+ *
235
+ * **Not a `Schema.Schema`.** See {@link Obj}'s note.
236
+ */
237
+ export interface Relation<
238
+ T,
239
+ Source,
240
+ Target,
241
+ Fields extends Schema.Struct.Fields = Schema.Struct.Fields,
242
+ > extends BaseTypeEntity<
243
+ RelationModule.Endpoints<Source, Target> & T & EntityModule.OfKind<typeof EntityModule.Kind.Relation>
244
+ > {
245
+ /** Schema-kind brand (relation). */
246
+ readonly [internal.SchemaKindId]: internal.EntityKind.Relation;
247
+
248
+ /** Source Effect Schema — used internally by `Type.getSchema(self)`. */
249
+ readonly [internal.StaticTypeSchemaSlot]: Schema.Schema.AnyNoContext;
250
+
134
251
  /**
135
252
  * The fields defined in the original struct schema.
136
253
  * Allows accessing field definitions for introspection.
@@ -139,21 +256,10 @@ export interface Relation<T, Source, Target, Fields extends Schema.Struct.Fields
139
256
  }
140
257
 
141
258
  /**
142
- * Structural base type for any ECHO relation schema.
143
- * Accepts static schemas (created with Type.relation()).
144
- * NOTE: Does not include the brand symbol to avoid TS4053 declaration portability issues.
145
- * Use Type.isRelationSchema() for runtime type guards.
259
+ * Type that represents any ECHO relation type — a `Type.Type` entity branded
260
+ * with the relation entity kind, i.e. what `Type.makeRelation(...)` produces.
146
261
  */
147
- type RelationSchemaBase = Schema.Schema.AnyNoContext & {
148
- readonly typename: string;
149
- readonly version: string;
150
- };
151
-
152
- /**
153
- * Type that represents any ECHO relation schema.
154
- * Accepts static schemas (Type.relation()).
155
- */
156
- export type AnyRelation = RelationSchemaBase;
262
+ export type AnyRelation = Relation<unknown, unknown, unknown>;
157
263
 
158
264
  /**
159
265
  * Factory function to create an ECHO relation schema.
@@ -163,42 +269,93 @@ export type AnyRelation = RelationSchemaBase;
163
269
  * ```ts
164
270
  * const WorksFor = Schema.Struct({
165
271
  * role: Schema.String,
166
- * }).pipe(Type.relation({
167
- * typename: 'com.example.type.works-for',
168
- * version: '0.1.0',
272
+ * }).pipe(Type.makeRelation({
273
+ * dxn: DXN.make('com.example.type.worksFor', '0.1.0'),
169
274
  * source: Person,
170
275
  * target: Company,
171
276
  * }));
172
277
  * ```
173
278
  */
174
- export const relation: {
175
- <SourceSchema extends Schema.Schema.AnyNoContext, TargetSchema extends Schema.Schema.AnyNoContext>(
176
- opts: internal.EchoRelationSchemaOptions<SourceSchema, TargetSchema>,
177
- ): <Self extends Schema.Schema.Any>(
279
+ export const makeRelation: {
280
+ <SourceInstance, TargetInstance>(opts: {
281
+ dxn: DXN.DXN;
282
+ source: Obj<SourceInstance, any> | internal.UnknownTypeSchema<SourceInstance, typeof EntityModule.Kind.Object>;
283
+ target: Obj<TargetInstance, any> | internal.UnknownTypeSchema<TargetInstance, typeof EntityModule.Kind.Object>;
284
+ /**
285
+ * Override the entity id. Defaults to `EntityId.deterministic(typename, version)`;
286
+ * see `Type.makeObject` for the workerd motivation.
287
+ */
288
+ id?: EntityId;
289
+ }): <Self extends Schema.Schema.Any>(
178
290
  self: Self,
179
- ) => Relation<Schema.Schema.Type<Self>, Schema.Schema.Type<SourceSchema>, Schema.Schema.Type<TargetSchema>>;
291
+ ) => Relation<
292
+ Schema.Schema.Type<Self>,
293
+ SourceInstance & EntityModule.OfKind<typeof EntityModule.Kind.Object>,
294
+ TargetInstance & EntityModule.OfKind<typeof EntityModule.Kind.Object>
295
+ >;
180
296
  } = internal.EchoRelationSchema as any;
181
297
 
182
298
  /**
183
- * Type alias for any ECHO entity schema (object or relation).
184
- * Use this in type annotations for schema parameters.
299
+ * Type that represents any ECHO type-kind entity a `Type.Type` meta-schema
300
+ * value (static `Type.Type` or a persisted draft from `db.addType(...)`).
301
+ * Mirrors {@link AnyObj} / {@link AnyRelation} for the third sibling kind.
302
+ */
303
+ export type AnyType = Type<unknown>;
304
+
305
+ /**
306
+ * Any ECHO type-entity — one of the three sibling kinds: object-kind, relation-kind,
307
+ * or type-kind (the meta-schema). APIs that want "any ECHO type" use this union;
308
+ * the underlying Effect Schema is retrieved via `Type.getSchema`.
185
309
  */
186
- export type AnyEntity = AnyObj | AnyRelation;
310
+ export type AnyEntity = AnyObj | AnyRelation | AnyType;
187
311
 
188
312
  /**
189
- * Type guard to check if a schema is an object schema.
190
- * NOTE: This checks SCHEMAS, not instances. Use Obj.isObject for instances.
313
+ * Type guard: narrows a `Type.AnyEntity` to an object-kind entity. Checks
314
+ * ENTITIES, not instances use `Obj.isObject` for instances. Raw
315
+ * `Schema.Schema` values (including the branded `Obj.Unknown` companion)
316
+ * are intentionally not accepted; inspect their `TypeAnnotation` directly.
191
317
  */
192
- export const isObjectSchema = (schema: AnyEntity): schema is AnyObj => {
193
- return (schema as any)[internal.SchemaKindId] === internal.EntityKind.Object;
318
+ export const isObject = (entity: AnyEntity): entity is AnyObj => {
319
+ return internal.getSchemaKind(entity) === internal.EntityKind.Object;
194
320
  };
195
321
 
196
322
  /**
197
- * Type guard to check if a schema is a relation schema.
198
- * NOTE: This checks SCHEMAS, not instances. Use Relation.isRelation for instances.
323
+ * Type guard: narrows a `Type.AnyEntity` to a relation-kind entity. Checks
324
+ * ENTITIES, not instances use `Relation.isRelation` for instances.
199
325
  */
200
- export const isRelationSchema = (schema: AnyEntity): schema is AnyRelation => {
201
- return (schema as any)[internal.SchemaKindId] === internal.EntityKind.Relation;
326
+ export const isRelation = (entity: AnyEntity): entity is AnyRelation => {
327
+ return internal.getSchemaKind(entity) === internal.EntityKind.Relation;
328
+ };
329
+
330
+ /**
331
+ * Type guard: narrows a `Type.AnyEntity` to the type-kind meta-schema
332
+ * (e.g. `Type.Type`). Mirrors {@link isObject} / {@link isRelation}.
333
+ */
334
+ export const isTypeKind = (entity: AnyEntity): entity is Type => {
335
+ return internal.getSchemaKind(entity) === internal.EntityKind.Type;
336
+ };
337
+
338
+ /**
339
+ * Narrow a `Type.AnyEntity` (e.g. one returned from `schemaRegistry.query(...)`)
340
+ * to `AnyObj`, throwing if it describes a relation or the type-kind
341
+ * meta-schema. Use at call sites that need to pass the value to `Obj.make`,
342
+ * `Filter.type`, or other object-only APIs.
343
+ */
344
+ export const assertObject = (entity: AnyEntity): AnyObj => {
345
+ assertArgument(isObject(entity), 'entity', 'Expected an object-kind Type entity.');
346
+ return entity;
347
+ };
348
+
349
+ /** Narrow a `Type.AnyEntity` to `AnyRelation`, throwing otherwise. */
350
+ export const expectRelation = (entity: AnyEntity): AnyRelation => {
351
+ assertArgument(isRelation(entity), 'entity', 'Expected a relation-kind Type entity.');
352
+ return entity;
353
+ };
354
+
355
+ /** Narrow a `Type.AnyEntity` to the `Type.Type` meta-schema, throwing otherwise. */
356
+ export const expectTypeKind = (entity: AnyEntity): Type => {
357
+ assertArgument(isTypeKind(entity), 'entity', 'Expected a type-kind Type entity.');
358
+ return entity;
202
359
  };
203
360
 
204
361
  /**
@@ -212,48 +369,369 @@ export type AnyRef = Schema.Schema<internal.Ref<any>, EncodedReference>;
212
369
  //
213
370
 
214
371
  /**
215
- * Gets the full DXN of the schema.
216
- * Will include the version if it's a `type` DXN.
217
- * @example "dxn:com.example.type.person:0.1.0"
218
- * @example "dxn:echo:SSSSSSSSSS:XXXXXXXXXXXXX"
372
+ * Returns the URI identifying a type entity. Always defined.
373
+ *
374
+ * - Static `Type.Obj` / `Type.Relation` → typename DXN (e.g. `dxn:com.example.type.person:0.1.0`).
375
+ * - Persisted `Type.Type` instance (has `id`) → local `EID` (`echo:/<objectId>`).
376
+ * - In-memory `Type.Type` draft (has `id`, no typename) → local `EID`.
377
+ *
378
+ * Only accepts `Type.AnyEntity` entities. Raw `Schema.Schema` values and the
379
+ * branded `Obj.Unknown` / `Relation.Unknown` schemas are intentionally not
380
+ * supported — use `internal.getSchemaURI` or the schema's typename annotation
381
+ * directly when working at the schema level.
219
382
  */
220
- export const getDXN = (schema: AnyEntity): DXN | undefined => {
221
- return internal.getSchemaDXN(schema);
383
+ export const getURI = (input: AnyEntity): URI.URI => {
384
+ // For Type entities, route through `getTypeURIFromSpecifier` (id → EID,
385
+ // typename/version → DXN). For Obj/Relation entities, unwrap to the source
386
+ // Effect Schema first and read its annotations.
387
+ if (isType(input)) {
388
+ return internal.getTypeURIFromSpecifier(input);
389
+ }
390
+ return internal.getSchemaURI(getSchema(input)) ?? raise(new TypeError('Type entity has no URI'));
222
391
  };
223
392
 
224
393
  /**
225
- * @param schema - Schema to get the typename from.
226
- * @returns The typename of the schema. Example: `com.example.type.person`.
394
+ * @returns The typename. Example: `com.example.type.person`.
395
+ *
396
+ * Persisted `Type.Type` entities carry typename in `EntityMeta.key` (the
397
+ * canonical registry-provenance field); unnamed drafts fall back to the
398
+ * entity's object id so the helper always returns a string. Any `dxn:` or
399
+ * `echo:/` prefix is stripped — typename is a bare identifier, not a URI.
227
400
  */
228
- export const getTypename = (schema: AnyEntity): string => {
229
- const typename = internal.getSchemaTypename(schema);
230
- invariant(typeof typename === 'string' && !typename.startsWith('dxn:'), 'Invalid typename');
401
+ // TODO(wittjosiah): For in-database types this should return the object id once the registry
402
+ // has more robust options for shadowing types (so callers can disambiguate db-stored copies).
403
+ export const getTypename = (input: AnyEntity): string => {
404
+ // Both in-memory and in-database entities carry typename in `EntityMeta.key`
405
+ // — the canonical registry-provenance field. In-memory entities attach meta
406
+ // eagerly (see `makeEchoTypeSchema`), so a single meta-backed read covers
407
+ // both forms.
408
+ const meta = internal.getMetaChecked(input);
409
+ let typename: string | undefined = meta.key as string | undefined;
410
+ // `meta.key` is a denormalized copy of the typename. The authoritative source
411
+ // for a type entity is its `jsonSchema.typename` (`getSchema` rebuilds the
412
+ // Effect Schema from `jsonSchema`). A type loaded from a snapshot can arrive
413
+ // with `meta.key` absent (the meta round-trip dropped the denormalized copy)
414
+ // while `jsonSchema.typename` is always present — consult it before the
415
+ // last-resort id fallback so callers never receive a bare object id where a
416
+ // typename is expected (e.g. `Filter.typename`, which rejects non-typenames).
417
+ if (typename == null) {
418
+ typename = input.jsonSchema?.typename;
419
+ }
420
+ // Unnamed drafts (no meta.key, no jsonSchema typename) fall back to the id.
421
+ typename ??= input.id as string;
422
+ // Typename is a bare identifier — strip URI prefixes if a caller seeded
423
+ // meta.key with one accidentally (or if a static entity carries a DXN-
424
+ // style typename).
425
+ typename = stripTypenamePrefix(typename);
426
+ invariant(typeof typename === 'string' && typename.length > 0, 'Invalid typename');
231
427
  return typename;
232
428
  };
233
429
 
234
430
  /**
235
- * Gets the version of the schema.
431
+ * Gets the version.
236
432
  * @example 0.1.0
433
+ * @example 0.1.0-<heads> (in-database, versioned by automerge heads)
434
+ *
435
+ * The registry-provenance semver lives in `EntityMeta.version`; unversioned
436
+ * drafts default to {@link DRAFT_VERSION} (`'0.0.0'`). In-database entities are
437
+ * additionally versioned by their automerge heads, which are exposed as the
438
+ * semver pre-release tag (`<semver>-<heads>`). In-memory declarations have no
439
+ * heads and surface the bare semver. Read the registry semver alone via
440
+ * `Type.getMeta(input).version`.
237
441
  */
238
- export const getVersion = (schema: AnyEntity): string => {
239
- const version = internal.getSchemaVersion(schema);
240
- invariant(typeof version === 'string' && version.match(/^\d+\.\d+\.\d+$/), 'Invalid version');
241
- return version;
442
+ export const getVersion = (input: AnyEntity): string => {
443
+ const meta = internal.getMetaChecked(input);
444
+ // As with `getTypename`: `meta.version` is a denormalized copy; the
445
+ // authoritative semver lives in `jsonSchema.version`. Prefer meta, fall back
446
+ // to jsonSchema (always present on persisted types, survives serialization),
447
+ // then to `DRAFT_VERSION` for unversioned drafts.
448
+ const semver = (meta.version as string | undefined) ?? input.jsonSchema?.version ?? DRAFT_VERSION;
449
+ invariant(typeof semver === 'string' && semver.match(/^\d+\.\d+\.\d+$/), 'Invalid version');
450
+ // In-database entities are versioned by their automerge heads; expose them as
451
+ // the semver pre-release tag. In-memory drafts carry no heads → bare semver.
452
+ const heads = internal.version(input).automergeHeads;
453
+ if (heads != null && heads.length > 0) {
454
+ return `${semver}-${[...heads].sort().join('.')}`;
455
+ }
456
+ return semver;
242
457
  };
243
458
 
244
459
  /**
245
- * @returns True if the schema is mutable.
460
+ * Strip URI prefixes (`dxn:`, `echo:/`, `echo://`) from a typename string.
461
+ * Typename is a bare identifier — callers reading from meta or from a
462
+ * caller-supplied seed value shouldn't propagate URI prefixes downstream.
246
463
  */
247
- export const isMutable = typeInternal.isMutable;
464
+ const stripTypenamePrefix = (value: string): string => {
465
+ if (value.startsWith('dxn:')) {
466
+ return value.slice('dxn:'.length);
467
+ }
468
+ if (value.startsWith('echo://')) {
469
+ return value.slice('echo://'.length);
470
+ }
471
+ if (value.startsWith('echo:/')) {
472
+ return value.slice('echo:/'.length);
473
+ }
474
+ return value;
475
+ };
248
476
 
249
477
  /**
250
- * ECHO type metadata.
478
+ * Type predicate: true iff the value is any type-kind ECHO entity — a static
479
+ * `Type.Obj` / `Type.Relation` produced by `Type.makeObject` / `Type.makeRelation`, a
480
+ * static meta `Type.Type`, or a persisted `Type.Type` returned by the database.
481
+ *
482
+ * All three branches stamp `[KindId] = Type`, so this is a single brand check.
483
+ * Use {@link isObject} / {@link isRelation} / {@link isTypeKind}
484
+ * when you need to discriminate further; use {@link getDatabase} when you mean
485
+ * "is this a db-attached type" (vs. an in-memory declaration).
486
+ */
487
+ export const isType = (value: unknown): value is AnyEntity =>
488
+ internal.getEntityKindBrand(value) === internal.EntityKind.Type;
489
+
490
+ /**
491
+ * Get the database the type entity belongs to, or `undefined` if it is an
492
+ * in-memory declaration (`Type.makeObject` / `Type.makeRelation` result) not
493
+ * yet attached to a database. Mirrors `Obj.getDatabase` / `Relation.getDatabase`.
494
+ *
495
+ * Database attachment is the canonical discriminator between in-memory and
496
+ * in-database type entities — both are live reactive `TypeSchema` instances and
497
+ * are otherwise indistinguishable.
498
+ */
499
+ export const getDatabase = (input: AnyEntity): Database.Database | undefined => internal.getDatabase(input);
500
+
501
+ /**
502
+ * Mutable meta type returned by `Type.getMeta` inside a `Type.update` callback.
503
+ * Mirrors `Obj.Meta` / `Relation.Meta` — `Type.Type` is an Entity like its
504
+ * siblings, so its meta is the same `EntityMeta` record:
505
+ * `{ keys, tags?, key?, version? }`.
506
+ *
507
+ * `key` / `version` here are the canonical registry-provenance pair
508
+ * (typename + semver) on persisted Type.Type entities; they are absent on
509
+ * unnamed drafts. Use {@link getTypename} / {@link getVersion} when you want
510
+ * a non-`undefined` value with id / {@link DRAFT_VERSION} fallbacks.
511
+ */
512
+ export type Meta = internal.Meta;
513
+
514
+ /**
515
+ * Deeply read-only version of {@link Meta}.
516
+ * Prevents mutation at all nesting levels (e.g., `meta.keys.push()` is a TS error).
517
+ */
518
+ export type ReadonlyMeta = internal.ReadonlyMeta;
519
+
520
+ /**
521
+ * Returns the entity's `EntityMeta`. Same semantics as `Obj.getMeta` /
522
+ * `Relation.getMeta` — `Type.Type` is an Entity and carries the canonical
523
+ * `EntityMeta` directly. Returns mutable meta when passed a mutable type
524
+ * (inside a `Type.update` callback), read-only meta otherwise.
525
+ *
526
+ * For persisted Type entities, `meta.key` holds the typename and
527
+ * `meta.version` holds the semver. Use {@link getTypename} / {@link getVersion}
528
+ * if you want the helpers' id / {@link DRAFT_VERSION} fallbacks for drafts.
529
+ *
530
+ * Both persisted and in-memory type entities (`Type.makeObject` /
531
+ * `Type.makeRelation` results) carry their `EntityMeta` via `[MetaId]`, so the
532
+ * lookup is uniform.
533
+ */
534
+ export function getMeta(entity: internal.Mutable<AnyEntity>): Meta;
535
+ export function getMeta(entity: Mutable): Meta;
536
+ export function getMeta(entity: AnyEntity): ReadonlyMeta;
537
+ export function getMeta(entity: AnyEntity | internal.Mutable<AnyEntity> | Mutable): ReadonlyMeta | Meta {
538
+ // The `Mutable` overload accepts the narrowed view passed to `Type.update`
539
+ // callbacks; at runtime that draft IS the underlying persisted Type entity,
540
+ // so the same `MetaId` lookup works.
541
+ assertArgument(isType(entity), 'entity', 'Expected a Type entity.');
542
+ // Both persisted and in-memory type entities carry runtime `EntityMeta` via
543
+ // `[MetaId]`, so the lookup is uniform.
544
+ return internal.getMetaChecked(entity);
545
+ }
546
+
547
+ /**
548
+ * Get the display label of a type entity.
549
+ * Reads the field(s) nominated by the type's {@link LabelAnnotation} (e.g. `name` on persisted schemas).
550
+ * Returns `undefined` if no label field is populated.
551
+ */
552
+ export const getLabel = (entity: AnyEntity, options?: internal.GetLabelOptions): string | undefined =>
553
+ internal.getLabel(entity, options);
554
+
555
+ /**
556
+ * String key used to phantom-carry the instance type produced by a `Type.Type`.
557
+ * Used by `Type.InstanceType<typeof Foo>` to recover the schema instance type
558
+ * since `Type.makeObject(dxn)` does not return a `Schema.Schema`.
559
+ *
560
+ * Re-exported from the internal types layer so both `Type.ts` and internal
561
+ * helpers (`makeObject`, `createObject`) reference the same phantom key.
562
+ */
563
+ export const InstancePhantomId = internal.InstancePhantomId;
564
+ export type InstancePhantomId = internal.InstancePhantomId;
565
+
566
+ /**
567
+ * Sibling of {@link Obj} / {@link Relation} for the third ECHO entity kind:
568
+ * **type-kind** entities (meta-schemas). The singleton {@link Type} const is
569
+ * the canonical example — it describes stored type definitions themselves.
570
+ *
571
+ * Not a `Schema.Schema`. Use `Type.getSchema(value)` to obtain the underlying
572
+ * Effect Schema and `Type.update(value, draft => ...)` to mutate.
573
+ *
574
+ * `A` is the instance-type phantom — what `Obj.make(value, ...)` would produce.
575
+ * Merged with the `Type` const value via TypeScript declaration merging.
576
+ */
577
+ export interface Type<A = unknown> extends BaseTypeEntity<A & EntityModule.OfKind<typeof EntityModule.Kind.Type>> {
578
+ /** Schema-kind brand (type — the meta-schema kind). */
579
+ readonly [internal.SchemaKindId]: internal.EntityKind.Type;
580
+
581
+ /** Source Effect Schema — used internally by `Type.getSchema(self)`. */
582
+ readonly [internal.StaticTypeSchemaSlot]: Schema.Schema.AnyNoContext;
583
+ }
584
+
585
+ /**
586
+ * Instance type produced by a Type entity.
587
+ *
588
+ * Accepts ONLY {@link AnyEntity} inputs — `Type.Obj`, `Type.Relation`, or
589
+ * `Type.Type`. Raw Effect `Schema.Schema` values are rejected: for those, use
590
+ * `Schema.Schema.Type<typeof Foo>` directly. This separation keeps the type
591
+ * system honest about which values represent ECHO entities versus plain
592
+ * Effect schemas.
593
+ *
594
+ * Dispatches on the entity kind:
595
+ * - `Relation<Props, S, T>` → `Endpoints<S,T> & Props & OfKind<Relation>`
596
+ * - `Obj<A>` → `A & OfKind<Object>`
597
+ * - `Type<A>` → `A & OfKind<Type>`
598
+ */
599
+ export type InstanceType<T extends AnyEntity> =
600
+ T extends Relation<infer Props, infer Source, infer Target, any>
601
+ ? RelationModule.Endpoints<Source, Target> & Props & EntityModule.OfKind<typeof EntityModule.Kind.Relation>
602
+ : T extends Obj<infer A, any>
603
+ ? A & EntityModule.OfKind<typeof EntityModule.Kind.Object>
604
+ : T extends Type<infer A>
605
+ ? A & EntityModule.OfKind<typeof EntityModule.Kind.Type>
606
+ : never;
607
+
608
+ /**
609
+ * Returns the Effect Schema for a type entity.
610
+ *
611
+ * - For static `Type.Obj` / `Type.Relation` entities the source Effect Schema is
612
+ * read from a hidden slot — these overloads preserve the instance type.
613
+ * - For `Type.Type` entities (the meta-schema kind) the schema is rebuilt from
614
+ * `type.jsonSchema`; the instance type isn't statically knowable so the wide
615
+ * `AnyEntity` overload widens to `Schema.Schema.AnyNoContext`.
616
+ *
617
+ * Always call this when you need to interact with the Effect Schema API
618
+ * (e.g. before passing to Effect.Schema functions). For ECHO-side APIs
619
+ * (`Obj.make`, `Filter.type`, `Ref`) pass the type entity directly.
620
+ *
621
+ * Only accepts `Type.AnyEntity` — raw `Schema.Schema` values can be used
622
+ * directly without unwrapping.
623
+ */
624
+ export function getSchema<T extends AnyObj>(type: T): Schema.Schema<InstanceType<T>>;
625
+ export function getSchema<T extends AnyRelation>(type: T): Schema.Schema<InstanceType<T>>;
626
+ export function getSchema(type: AnyEntity): Schema.Schema.AnyNoContext;
627
+ export function getSchema(type: AnyEntity): Schema.Schema.AnyNoContext {
628
+ // Static `Type.Type` entities carry the source Effect Schema on a hidden
629
+ // slot so we can return it without round-tripping through JsonSchema.
630
+ const staticSchema = internal.getStaticTypeSchema(type);
631
+ if (staticSchema != null) {
632
+ return staticSchema;
633
+ }
634
+ assertArgument(isType(type), 'type', 'Expected a Type entity.');
635
+ // Persisted `Type.Type` entity — build the Effect Schema from its stored
636
+ // jsonSchema and re-attach the TypeIdentifierAnnotation so the rebuilt
637
+ // schema's URI (via getSchemaURI) matches the entity's local EID.
638
+ const rebuilt = internal.toEffectSchema(type.jsonSchema);
639
+ if (typeof type.id === 'string') {
640
+ return rebuilt.annotations({
641
+ [internal.TypeIdentifierAnnotationId]: EID.make({ entityId: type.id }),
642
+ });
643
+ }
644
+ return rebuilt;
645
+ }
646
+
647
+ /**
648
+ * Mutable view of a `Type.Type` — the shape passed to the `Type.update` callback.
649
+ * Outside `Type.update`, `Type.Type` fields are read-only (both at the type level
650
+ * and at runtime — direct assignment throws). Use this to constrain mutation to
651
+ * the change context, analogous to `Obj.update(obj, (draft) => ...)`.
652
+ *
653
+ * NOTE: `typename` and `version` are intentionally absent — they live in
654
+ * `EntityMeta` (`key` / `version` — the canonical registry-provenance pair).
655
+ * Read them via {@link getTypename} / {@link getVersion} / {@link getMeta};
656
+ * `typename` is treated as immutable on persisted entities.
657
+ *
658
+ * Unlike `Obj.update` — whose mutable view is inferred as `Mutable<A>` over the
659
+ * whole instance type because every data field is editable — a `Type.Type`
660
+ * exposes only `name` and `jsonSchema` for mutation. The rest of its shape
661
+ * (`id`, the `[KindId]` / `[SchemaKindId]` brands, and `typename` / `version`
662
+ * in meta) is immutable, so this view is declared explicitly rather than
663
+ * derived from `InstanceType<Type.Type>`.
251
664
  */
252
- export type Meta = internal.TypeAnnotation;
665
+ export interface Mutable {
666
+ name?: string;
667
+ // Deep-mutable within the change context — `Type.update`'s purpose is to allow
668
+ // mutation, so the draft exposes `jsonSchema` as writable (the readonly
669
+ // `JsonSchemaType` would force callers to cast).
670
+ jsonSchema: Types.DeepMutable<internal.JsonSchemaType>;
671
+ }
672
+
673
+ /**
674
+ * Perform mutations on a `Type.Type` within a change context.
675
+ *
676
+ * The callback receives a {@link Mutable} view of the type — direct mutation of
677
+ * a `Type.Type` outside `Type.update` throws at runtime, mirroring `Obj.update`.
678
+ * Delegates to the same automerge-transaction primitive `Obj.update(obj, cb)` uses.
679
+ */
680
+ export const update = (type: AnyEntity, callback: (mutable: Mutable) => void): void => {
681
+ // `Type.Type` is an ECHO object; the change machinery is the same as `Obj.update`.
682
+ internal.change(type, callback as internal.ChangeCallback<AnyEntity>);
683
+ };
684
+
685
+ //
686
+ // Field-level helpers for mutating persisted types.
687
+ // These are thin wrappers over `Type.update` plus the JsonSchema manipulation
688
+ // utilities. Callers pass a persisted `Type.Type` (e.g. one returned by
689
+ // `db.addType(schemaEntity)`) and the helper drives the change context.
690
+ //
691
+
692
+ /**
693
+ * Add fields to a persisted type's schema.
694
+ * @throws if the type is not persisted.
695
+ */
696
+ export const addFields = (type: AnyEntity, fields: Schema.Struct.Fields): void => {
697
+ const extended = typeInternal.addFieldsToSchema(getSchema(type), fields);
698
+ update(type, (draft) => {
699
+ draft.jsonSchema = internal.toJsonSchema(extended);
700
+ });
701
+ };
702
+
703
+ /**
704
+ * Replace existing fields on a persisted type's schema.
705
+ * @throws if the type is not persisted.
706
+ */
707
+ export const updateFields = (type: AnyEntity, fields: Schema.Struct.Fields): void => {
708
+ const updated = typeInternal.updateFieldsInSchema(getSchema(type), fields);
709
+ update(type, (draft) => {
710
+ draft.jsonSchema = internal.toJsonSchema(updated);
711
+ });
712
+ };
713
+
714
+ /**
715
+ * Rename a field on a persisted type's schema.
716
+ * @throws if the type is not persisted.
717
+ */
718
+ export const updateFieldPropertyName = (
719
+ type: Type,
720
+ { before, after }: { before: PropertyKey; after: PropertyKey },
721
+ ): void => {
722
+ const renamed = typeInternal.updateFieldNameInSchema(getSchema(type), { before, after });
723
+ update(type, (draft) => {
724
+ draft.jsonSchema = internal.toJsonSchema(renamed);
725
+ });
726
+ };
253
727
 
254
728
  /**
255
- * Gets the meta data of the schema.
729
+ * Remove fields from a persisted type's schema.
730
+ * @throws if the type is not persisted.
256
731
  */
257
- export const getMeta = (schema: AnyEntity): Meta | undefined => {
258
- return internal.getTypeAnnotation(schema);
732
+ export const removeFields = (type: AnyEntity, fieldNames: string[]): void => {
733
+ const removed = typeInternal.removeFieldsFromSchema(getSchema(type), fieldNames);
734
+ update(type, (draft) => {
735
+ draft.jsonSchema = internal.toJsonSchema(removed);
736
+ });
259
737
  };