@dxos/echo 0.8.4-main.406dc2a → 0.8.4-main.4a85c3132b

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 (554) hide show
  1. package/README.md +3 -4
  2. package/dist/lib/neutral/Annotation.mjs +37 -0
  3. package/dist/lib/neutral/Database.mjs +45 -0
  4. package/dist/lib/neutral/Entity.mjs +56 -0
  5. package/dist/lib/neutral/Err.mjs +12 -0
  6. package/dist/lib/neutral/Feed.mjs +43 -0
  7. package/dist/lib/neutral/Filter.mjs +61 -0
  8. package/dist/lib/neutral/Format.mjs +66 -0
  9. package/dist/lib/neutral/Format.mjs.map +7 -0
  10. package/dist/lib/neutral/JsonSchema.mjs +18 -0
  11. package/dist/lib/neutral/JsonSchema.mjs.map +7 -0
  12. package/dist/lib/neutral/Key.mjs +12 -0
  13. package/dist/lib/neutral/Key.mjs.map +7 -0
  14. package/dist/lib/neutral/Obj.mjs +107 -0
  15. package/dist/lib/neutral/Obj.mjs.map +7 -0
  16. package/dist/lib/neutral/Order.mjs +12 -0
  17. package/dist/lib/neutral/Order.mjs.map +7 -0
  18. package/dist/lib/neutral/Query.mjs +32 -0
  19. package/dist/lib/neutral/Query.mjs.map +7 -0
  20. package/dist/lib/neutral/QueryResult.mjs +2 -0
  21. package/dist/lib/neutral/QueryResult.mjs.map +7 -0
  22. package/dist/lib/neutral/Ref.mjs +24 -0
  23. package/dist/lib/neutral/Ref.mjs.map +7 -0
  24. package/dist/lib/neutral/Relation.mjs +90 -0
  25. package/dist/lib/neutral/Relation.mjs.map +7 -0
  26. package/dist/lib/neutral/SchemaRegistry.mjs +2 -0
  27. package/dist/lib/neutral/SchemaRegistry.mjs.map +7 -0
  28. package/dist/lib/neutral/Tag.mjs +25 -0
  29. package/dist/lib/neutral/Tag.mjs.map +7 -0
  30. package/dist/lib/neutral/Type.mjs +34 -0
  31. package/dist/lib/neutral/Type.mjs.map +7 -0
  32. package/dist/lib/neutral/chunk-4KG6IGL4.mjs +67 -0
  33. package/dist/lib/neutral/chunk-4KG6IGL4.mjs.map +7 -0
  34. package/dist/lib/neutral/chunk-5ELDDYWE.mjs +130 -0
  35. package/dist/lib/neutral/chunk-5ELDDYWE.mjs.map +7 -0
  36. package/dist/lib/neutral/chunk-ALOGSVBP.mjs +299 -0
  37. package/dist/lib/neutral/chunk-ALOGSVBP.mjs.map +7 -0
  38. package/dist/lib/neutral/chunk-ANHVGJI4.mjs +21 -0
  39. package/dist/lib/neutral/chunk-ANHVGJI4.mjs.map +7 -0
  40. package/dist/lib/neutral/chunk-AQP4QKYP.mjs +283 -0
  41. package/dist/lib/neutral/chunk-AQP4QKYP.mjs.map +7 -0
  42. package/dist/lib/neutral/chunk-BRJSLACP.mjs +152 -0
  43. package/dist/lib/neutral/chunk-BRJSLACP.mjs.map +7 -0
  44. package/dist/lib/neutral/chunk-DZQSL6RW.mjs +73 -0
  45. package/dist/lib/neutral/chunk-DZQSL6RW.mjs.map +7 -0
  46. package/dist/lib/neutral/chunk-F7KMHDPJ.mjs +327 -0
  47. package/dist/lib/neutral/chunk-F7KMHDPJ.mjs.map +7 -0
  48. package/dist/lib/neutral/chunk-GAWKQ5DZ.mjs +56 -0
  49. package/dist/lib/neutral/chunk-GAWKQ5DZ.mjs.map +7 -0
  50. package/dist/lib/neutral/chunk-GFNCSK7F.mjs +101 -0
  51. package/dist/lib/neutral/chunk-GFNCSK7F.mjs.map +7 -0
  52. package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
  53. package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
  54. package/dist/lib/neutral/chunk-KGV3QIRX.mjs +313 -0
  55. package/dist/lib/neutral/chunk-KGV3QIRX.mjs.map +7 -0
  56. package/dist/lib/neutral/chunk-M2KVTHZM.mjs +402 -0
  57. package/dist/lib/neutral/chunk-M2KVTHZM.mjs.map +7 -0
  58. package/dist/lib/neutral/chunk-MXQJZCPY.mjs +51 -0
  59. package/dist/lib/neutral/chunk-MXQJZCPY.mjs.map +7 -0
  60. package/dist/lib/neutral/chunk-O3TZFQCT.mjs +27 -0
  61. package/dist/lib/neutral/chunk-O3TZFQCT.mjs.map +7 -0
  62. package/dist/lib/neutral/chunk-OMUPQMLR.mjs +7 -0
  63. package/dist/lib/neutral/chunk-OMUPQMLR.mjs.map +7 -0
  64. package/dist/lib/neutral/chunk-OVUBTQLT.mjs +45 -0
  65. package/dist/lib/neutral/chunk-OVUBTQLT.mjs.map +7 -0
  66. package/dist/lib/neutral/chunk-PP4DNUZU.mjs +3919 -0
  67. package/dist/lib/neutral/chunk-PP4DNUZU.mjs.map +7 -0
  68. package/dist/lib/neutral/chunk-QXF3LGN2.mjs +43 -0
  69. package/dist/lib/neutral/chunk-QXF3LGN2.mjs.map +7 -0
  70. package/dist/lib/neutral/chunk-T4MPQJ7X.mjs +97 -0
  71. package/dist/lib/neutral/chunk-T4MPQJ7X.mjs.map +7 -0
  72. package/dist/lib/neutral/chunk-T6JEFNP6.mjs +7 -0
  73. package/dist/lib/neutral/chunk-T6JEFNP6.mjs.map +7 -0
  74. package/dist/lib/neutral/chunk-W7OP5HGZ.mjs +121 -0
  75. package/dist/lib/neutral/chunk-W7OP5HGZ.mjs.map +7 -0
  76. package/dist/lib/neutral/chunk-ZTUBYOGB.mjs +42 -0
  77. package/dist/lib/neutral/chunk-ZTUBYOGB.mjs.map +7 -0
  78. package/dist/lib/neutral/chunk-ZWKJ4LZJ.mjs +298 -0
  79. package/dist/lib/neutral/chunk-ZWKJ4LZJ.mjs.map +7 -0
  80. package/dist/lib/neutral/index.mjs +94 -0
  81. package/dist/lib/neutral/index.mjs.map +7 -0
  82. package/dist/lib/{node-esm → neutral}/internal/index.mjs +253 -121
  83. package/dist/lib/neutral/internal/index.mjs.map +7 -0
  84. package/dist/lib/neutral/meta.json +1 -0
  85. package/dist/lib/neutral/testing/index.mjs +369 -0
  86. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  87. package/dist/types/src/Annotation.d.ts +24 -0
  88. package/dist/types/src/Annotation.d.ts.map +1 -0
  89. package/dist/types/src/Collection.d.ts +16 -0
  90. package/dist/types/src/Collection.d.ts.map +1 -0
  91. package/dist/types/src/Database.d.ts +189 -0
  92. package/dist/types/src/Database.d.ts.map +1 -0
  93. package/dist/types/src/Dataset.d.ts +18 -0
  94. package/dist/types/src/Dataset.d.ts.map +1 -0
  95. package/dist/types/src/Entity.d.ts +168 -0
  96. package/dist/types/src/Entity.d.ts.map +1 -0
  97. package/dist/types/src/Entity.test.d.ts +2 -0
  98. package/dist/types/src/Entity.test.d.ts.map +1 -0
  99. package/dist/types/src/Err.d.ts +107 -0
  100. package/dist/types/src/Err.d.ts.map +1 -0
  101. package/dist/types/src/Feed.d.ts +165 -0
  102. package/dist/types/src/Feed.d.ts.map +1 -0
  103. package/dist/types/src/Filter.d.ts +120 -0
  104. package/dist/types/src/Filter.d.ts.map +1 -0
  105. package/dist/types/src/Format.d.ts +4 -0
  106. package/dist/types/src/Format.d.ts.map +1 -0
  107. package/dist/types/src/Hypergraph.d.ts +60 -0
  108. package/dist/types/src/Hypergraph.d.ts.map +1 -0
  109. package/dist/types/src/JsonSchema.d.ts +16 -0
  110. package/dist/types/src/JsonSchema.d.ts.map +1 -0
  111. package/dist/types/src/Key.d.ts +1 -0
  112. package/dist/types/src/Key.d.ts.map +1 -1
  113. package/dist/types/src/Obj.d.ts +417 -101
  114. package/dist/types/src/Obj.d.ts.map +1 -1
  115. package/dist/types/src/Obj.test.d.ts +2 -0
  116. package/dist/types/src/Obj.test.d.ts.map +1 -0
  117. package/dist/types/src/Order.d.ts +16 -0
  118. package/dist/types/src/Order.d.ts.map +1 -0
  119. package/dist/types/src/Query.d.ts +202 -0
  120. package/dist/types/src/Query.d.ts.map +1 -0
  121. package/dist/types/src/Query.test.d.ts +2 -0
  122. package/dist/types/src/Query.test.d.ts.map +1 -0
  123. package/dist/types/src/QueryResult.d.ts +80 -0
  124. package/dist/types/src/QueryResult.d.ts.map +1 -0
  125. package/dist/types/src/Ref.d.ts +61 -11
  126. package/dist/types/src/Ref.d.ts.map +1 -1
  127. package/dist/types/src/Relation.d.ts +287 -21
  128. package/dist/types/src/Relation.d.ts.map +1 -1
  129. package/dist/types/src/Relation.test.d.ts +2 -0
  130. package/dist/types/src/Relation.test.d.ts.map +1 -0
  131. package/dist/types/src/SchemaRegistry.d.ts +84 -0
  132. package/dist/types/src/SchemaRegistry.d.ts.map +1 -0
  133. package/dist/types/src/Tag.d.ts +18 -0
  134. package/dist/types/src/Tag.d.ts.map +1 -0
  135. package/dist/types/src/Type.d.ts +120 -97
  136. package/dist/types/src/Type.d.ts.map +1 -1
  137. package/dist/types/src/Type.test.d.ts +2 -0
  138. package/dist/types/src/Type.test.d.ts.map +1 -0
  139. package/dist/types/src/View.d.ts +68 -0
  140. package/dist/types/src/View.d.ts.map +1 -0
  141. package/dist/types/src/hierarchy.test.d.ts +2 -0
  142. package/dist/types/src/hierarchy.test.d.ts.map +1 -0
  143. package/dist/types/src/index.d.ts +19 -5
  144. package/dist/types/src/index.d.ts.map +1 -1
  145. package/dist/types/src/internal/annotations/annotations.d.ts +209 -0
  146. package/dist/types/src/internal/annotations/annotations.d.ts.map +1 -0
  147. package/dist/types/src/internal/annotations/annotations.test.d.ts.map +1 -0
  148. package/dist/types/src/internal/annotations/index.d.ts +3 -0
  149. package/dist/types/src/internal/annotations/index.d.ts.map +1 -0
  150. package/dist/types/src/internal/annotations/util.d.ts +39 -0
  151. package/dist/types/src/internal/annotations/util.d.ts.map +1 -0
  152. package/dist/types/src/internal/api/annotations.d.ts +23 -0
  153. package/dist/types/src/internal/api/annotations.d.ts.map +1 -0
  154. package/dist/types/src/internal/api/entity.d.ts +13 -0
  155. package/dist/types/src/internal/api/entity.d.ts.map +1 -0
  156. package/dist/types/src/internal/api/index.d.ts +15 -0
  157. package/dist/types/src/internal/api/index.d.ts.map +1 -0
  158. package/dist/types/src/internal/api/meta.d.ts +42 -0
  159. package/dist/types/src/internal/api/meta.d.ts.map +1 -0
  160. package/dist/types/src/internal/api/sorting.d.ts +24 -0
  161. package/dist/types/src/internal/api/sorting.d.ts.map +1 -0
  162. package/dist/types/src/internal/api/version.d.ts +42 -0
  163. package/dist/types/src/internal/api/version.d.ts.map +1 -0
  164. package/dist/types/src/internal/entities/entity.d.ts +20 -0
  165. package/dist/types/src/internal/entities/entity.d.ts.map +1 -0
  166. package/dist/types/src/internal/entities/index.d.ts +6 -0
  167. package/dist/types/src/internal/entities/index.d.ts.map +1 -0
  168. package/dist/types/src/internal/entities/model.d.ts +79 -0
  169. package/dist/types/src/internal/entities/model.d.ts.map +1 -0
  170. package/dist/types/src/internal/entities/object.d.ts +18 -0
  171. package/dist/types/src/internal/entities/object.d.ts.map +1 -0
  172. package/dist/types/src/internal/entities/relation.d.ts +62 -0
  173. package/dist/types/src/internal/entities/relation.d.ts.map +1 -0
  174. package/dist/types/src/internal/entities/util.d.ts +2 -0
  175. package/dist/types/src/internal/entities/util.d.ts.map +1 -0
  176. package/dist/types/src/internal/formats/format.d.ts +4 -2
  177. package/dist/types/src/internal/formats/format.d.ts.map +1 -1
  178. package/dist/types/src/internal/formats/select.d.ts +6 -4
  179. package/dist/types/src/internal/formats/select.d.ts.map +1 -1
  180. package/dist/types/src/internal/formats/string.d.ts +4 -0
  181. package/dist/types/src/internal/formats/string.d.ts.map +1 -1
  182. package/dist/types/src/internal/formats/types.d.ts +13 -9
  183. package/dist/types/src/internal/formats/types.d.ts.map +1 -1
  184. package/dist/types/src/internal/index.d.ts +7 -10
  185. package/dist/types/src/internal/index.d.ts.map +1 -1
  186. package/dist/types/src/internal/json-schema/annotations.d.ts.map +1 -0
  187. package/dist/types/src/internal/json-schema/effect-schema.test.d.ts.map +1 -0
  188. package/dist/types/src/internal/json-schema/index.d.ts +2 -0
  189. package/dist/types/src/internal/json-schema/index.d.ts.map +1 -1
  190. package/dist/types/src/internal/json-schema/json-schema-normalize.d.ts.map +1 -1
  191. package/dist/types/src/internal/json-schema/json-schema-type.d.ts +130 -29
  192. package/dist/types/src/internal/json-schema/json-schema-type.d.ts.map +1 -1
  193. package/dist/types/src/internal/{json → json-schema}/json-schema.d.ts +4 -2
  194. package/dist/types/src/internal/json-schema/json-schema.d.ts.map +1 -0
  195. package/dist/types/src/internal/json-schema/json-schema.test.d.ts.map +1 -0
  196. package/dist/types/src/internal/object/clone.d.ts +8 -0
  197. package/dist/types/src/internal/object/clone.d.ts.map +1 -0
  198. package/dist/types/src/internal/object/common.d.ts.map +1 -1
  199. package/dist/types/src/internal/object/{create.d.ts → create-object.d.ts} +10 -11
  200. package/dist/types/src/internal/object/create-object.d.ts.map +1 -0
  201. package/dist/types/src/internal/object/create-object.test.d.ts +2 -0
  202. package/dist/types/src/internal/object/create-object.test.d.ts.map +1 -0
  203. package/dist/types/src/internal/object/deleted.d.ts +2 -2
  204. package/dist/types/src/internal/object/deleted.d.ts.map +1 -1
  205. package/dist/types/src/internal/object/ids.d.ts.map +1 -1
  206. package/dist/types/src/internal/object/index.d.ts +6 -11
  207. package/dist/types/src/internal/object/index.d.ts.map +1 -1
  208. package/dist/types/src/internal/object/json-serializer.d.ts +14 -9
  209. package/dist/types/src/internal/object/json-serializer.d.ts.map +1 -1
  210. package/dist/types/src/internal/object/schema-validator.d.ts +1 -14
  211. package/dist/types/src/internal/object/schema-validator.d.ts.map +1 -1
  212. package/dist/types/src/internal/object/set-value.d.ts +7 -0
  213. package/dist/types/src/internal/object/set-value.d.ts.map +1 -0
  214. package/dist/types/src/internal/object/set-value.test.d.ts +2 -0
  215. package/dist/types/src/internal/object/set-value.test.d.ts.map +1 -0
  216. package/dist/types/src/internal/object/snapshot.d.ts +6 -0
  217. package/dist/types/src/internal/object/snapshot.d.ts.map +1 -0
  218. package/dist/types/src/internal/object/typed-object.d.ts +8 -14
  219. package/dist/types/src/internal/object/typed-object.d.ts.map +1 -1
  220. package/dist/types/src/internal/proxy/change-context.d.ts +55 -0
  221. package/dist/types/src/internal/proxy/change-context.d.ts.map +1 -0
  222. package/dist/types/src/internal/proxy/change.test.d.ts +2 -0
  223. package/dist/types/src/internal/proxy/change.test.d.ts.map +1 -0
  224. package/dist/types/src/internal/proxy/define-hidden-property.d.ts +5 -0
  225. package/dist/types/src/internal/proxy/define-hidden-property.d.ts.map +1 -0
  226. package/dist/types/src/internal/proxy/errors.d.ts +19 -0
  227. package/dist/types/src/internal/proxy/errors.d.ts.map +1 -0
  228. package/dist/types/src/internal/proxy/event-batch.d.ts +10 -0
  229. package/dist/types/src/internal/proxy/event-batch.d.ts.map +1 -0
  230. package/dist/types/src/internal/proxy/index.d.ts +14 -0
  231. package/dist/types/src/internal/proxy/index.d.ts.map +1 -0
  232. package/dist/types/src/internal/proxy/json-serializer.d.ts +6 -0
  233. package/dist/types/src/internal/proxy/json-serializer.d.ts.map +1 -0
  234. package/dist/types/src/internal/proxy/make-object.d.ts +14 -0
  235. package/dist/types/src/internal/proxy/make-object.d.ts.map +1 -0
  236. package/dist/types/src/internal/proxy/ownership.d.ts +57 -0
  237. package/dist/types/src/internal/proxy/ownership.d.ts.map +1 -0
  238. package/dist/types/src/internal/proxy/proxy-types.d.ts +18 -0
  239. package/dist/types/src/internal/proxy/proxy-types.d.ts.map +1 -0
  240. package/dist/types/src/internal/proxy/proxy-utils.d.ts +47 -0
  241. package/dist/types/src/internal/proxy/proxy-utils.d.ts.map +1 -0
  242. package/dist/types/src/internal/proxy/reactive-array.d.ts +8 -0
  243. package/dist/types/src/internal/proxy/reactive-array.d.ts.map +1 -0
  244. package/dist/types/src/internal/proxy/reactive.d.ts +39 -0
  245. package/dist/types/src/internal/proxy/reactive.d.ts.map +1 -0
  246. package/dist/types/src/internal/proxy/schema-validator.d.ts +15 -0
  247. package/dist/types/src/internal/proxy/schema-validator.d.ts.map +1 -0
  248. package/dist/types/src/internal/proxy/symbols.d.ts +3 -0
  249. package/dist/types/src/internal/proxy/symbols.d.ts.map +1 -0
  250. package/dist/types/src/internal/proxy/typed-handler.d.ts +16 -12
  251. package/dist/types/src/internal/proxy/typed-handler.d.ts.map +1 -1
  252. package/dist/types/src/internal/ref/ref-array.d.ts +4 -4
  253. package/dist/types/src/internal/ref/ref-array.d.ts.map +1 -1
  254. package/dist/types/src/internal/ref/ref.d.ts +43 -18
  255. package/dist/types/src/internal/ref/ref.d.ts.map +1 -1
  256. package/dist/types/src/internal/schema/compose.d.ts +7 -0
  257. package/dist/types/src/internal/schema/compose.d.ts.map +1 -0
  258. package/dist/types/src/internal/schema/compose.test.d.ts.map +1 -0
  259. package/dist/types/src/internal/schema/echo-schema.d.ts +25 -12
  260. package/dist/types/src/internal/schema/echo-schema.d.ts.map +1 -1
  261. package/dist/types/src/internal/schema/index.d.ts +1 -4
  262. package/dist/types/src/internal/schema/index.d.ts.map +1 -1
  263. package/dist/types/src/internal/schema/persistent-schema.d.ts +20 -0
  264. package/dist/types/src/internal/schema/persistent-schema.d.ts.map +1 -0
  265. package/dist/types/src/internal/types/base.d.ts +27 -0
  266. package/dist/types/src/internal/types/base.d.ts.map +1 -0
  267. package/dist/types/src/internal/types/entity.d.ts +37 -0
  268. package/dist/types/src/internal/types/entity.d.ts.map +1 -0
  269. package/dist/types/src/internal/types/index.d.ts +5 -2
  270. package/dist/types/src/internal/types/index.d.ts.map +1 -1
  271. package/dist/types/src/internal/{object → types}/meta.d.ts +16 -14
  272. package/dist/types/src/internal/types/meta.d.ts.map +1 -0
  273. package/dist/types/src/internal/types/typename.d.ts +21 -0
  274. package/dist/types/src/internal/types/typename.d.ts.map +1 -0
  275. package/dist/types/src/internal/types/version.d.ts +15 -0
  276. package/dist/types/src/internal/types/version.d.ts.map +1 -0
  277. package/dist/types/src/{test → testing}/api.test.d.ts.map +1 -1
  278. package/dist/types/src/testing/index.d.ts +3 -3
  279. package/dist/types/src/testing/index.d.ts.map +1 -1
  280. package/dist/types/src/testing/test-data.d.ts +18 -0
  281. package/dist/types/src/testing/test-data.d.ts.map +1 -0
  282. package/dist/types/src/testing/test-schema.d.ts +304 -0
  283. package/dist/types/src/testing/test-schema.d.ts.map +1 -0
  284. package/dist/types/src/testing/util.d.ts +21 -0
  285. package/dist/types/src/testing/util.d.ts.map +1 -0
  286. package/dist/types/tsconfig.tsbuildinfo +1 -1
  287. package/package.json +106 -65
  288. package/src/Annotation.ts +46 -0
  289. package/src/Collection.ts +30 -0
  290. package/src/Database.ts +315 -0
  291. package/src/Dataset.ts +24 -0
  292. package/src/Entity.test.ts +22 -0
  293. package/src/Entity.ts +234 -0
  294. package/src/Err.ts +38 -0
  295. package/src/Feed.ts +254 -0
  296. package/src/Filter.ts +389 -0
  297. package/src/Format.ts +9 -0
  298. package/src/Hypergraph.ts +74 -0
  299. package/src/JsonSchema.ts +24 -0
  300. package/src/Key.ts +3 -0
  301. package/src/Obj.test.ts +447 -0
  302. package/src/Obj.ts +580 -267
  303. package/src/Order.ts +44 -0
  304. package/src/Query.test.ts +632 -0
  305. package/src/Query.ts +520 -0
  306. package/src/QueryResult.ts +106 -0
  307. package/src/Ref.ts +77 -9
  308. package/src/Relation.test.ts +88 -0
  309. package/src/Relation.ts +440 -58
  310. package/src/SchemaRegistry.ts +106 -0
  311. package/src/{query/tag.ts → Tag.ts} +10 -8
  312. package/src/Type.test.ts +52 -0
  313. package/src/Type.ts +162 -151
  314. package/src/View.ts +100 -0
  315. package/src/hierarchy.test.ts +33 -0
  316. package/src/index.ts +20 -6
  317. package/src/internal/README.md +102 -0
  318. package/src/internal/{ast → annotations}/annotations.test.ts +18 -20
  319. package/src/internal/annotations/annotations.ts +526 -0
  320. package/src/internal/annotations/index.ts +6 -0
  321. package/src/internal/annotations/util.ts +85 -0
  322. package/src/internal/api/annotations.ts +60 -0
  323. package/src/internal/api/entity.ts +29 -0
  324. package/src/internal/api/index.ts +19 -0
  325. package/src/internal/api/meta.ts +88 -0
  326. package/src/internal/api/sorting.ts +53 -0
  327. package/src/internal/api/version.ts +96 -0
  328. package/src/internal/entities/entity.ts +128 -0
  329. package/src/internal/entities/index.ts +9 -0
  330. package/src/internal/entities/model.ts +138 -0
  331. package/src/internal/entities/object.ts +58 -0
  332. package/src/internal/entities/relation.ts +171 -0
  333. package/src/internal/entities/util.ts +33 -0
  334. package/src/internal/formats/date.test.ts +1 -1
  335. package/src/internal/formats/date.ts +5 -5
  336. package/src/internal/formats/format.test.ts +6 -6
  337. package/src/internal/formats/format.ts +8 -6
  338. package/src/internal/formats/number.ts +5 -5
  339. package/src/internal/formats/object.ts +4 -4
  340. package/src/internal/formats/select.ts +6 -4
  341. package/src/internal/formats/string.ts +14 -9
  342. package/src/internal/formats/types.ts +53 -42
  343. package/src/internal/index.ts +30 -13
  344. package/src/internal/{json → json-schema}/annotations.ts +1 -1
  345. package/src/internal/json-schema/index.ts +2 -0
  346. package/src/internal/json-schema/json-schema-normalize.ts +4 -2
  347. package/src/internal/json-schema/json-schema-type.ts +35 -35
  348. package/src/internal/{json → json-schema}/json-schema.test.ts +73 -69
  349. package/src/internal/{json → json-schema}/json-schema.ts +30 -15
  350. package/src/internal/object/clone.ts +48 -0
  351. package/src/internal/object/common.ts +3 -4
  352. package/src/internal/object/{create.test.ts → create-object.test.ts} +31 -33
  353. package/src/internal/object/{create.ts → create-object.ts} +36 -37
  354. package/src/internal/object/deleted.ts +6 -6
  355. package/src/internal/object/ids.ts +1 -1
  356. package/src/internal/object/index.ts +6 -11
  357. package/src/internal/object/inspect.ts +5 -7
  358. package/src/internal/object/json-serializer.test.ts +59 -37
  359. package/src/internal/object/json-serializer.ts +87 -108
  360. package/src/internal/object/schema-validator.test.ts +3 -7
  361. package/src/internal/object/schema-validator.ts +2 -237
  362. package/src/internal/object/set-value.test.ts +281 -0
  363. package/src/internal/object/set-value.ts +165 -0
  364. package/src/internal/object/snapshot.ts +96 -0
  365. package/src/internal/object/typed-object.test.ts +11 -11
  366. package/src/internal/object/typed-object.ts +8 -66
  367. package/src/internal/proxy/change-context.ts +138 -0
  368. package/src/internal/proxy/change.test.ts +519 -0
  369. package/src/internal/proxy/define-hidden-property.ts +14 -0
  370. package/src/internal/proxy/errors.ts +42 -0
  371. package/src/internal/proxy/event-batch.ts +44 -0
  372. package/src/internal/proxy/handler.test.ts +51 -91
  373. package/src/internal/proxy/index.ts +17 -0
  374. package/src/internal/proxy/json-serializer.ts +87 -0
  375. package/src/internal/proxy/make-object.ts +106 -0
  376. package/src/internal/proxy/ownership.ts +253 -0
  377. package/src/internal/proxy/proxy-types.ts +23 -0
  378. package/src/internal/proxy/proxy-utils.ts +150 -0
  379. package/src/internal/proxy/reactive-array.ts +71 -0
  380. package/src/internal/proxy/reactive.ts +69 -0
  381. package/src/internal/proxy/schema-validator.ts +244 -0
  382. package/src/internal/proxy/schema.test.ts +27 -18
  383. package/src/internal/proxy/symbols.ts +7 -0
  384. package/src/internal/proxy/typed-handler.test.ts +260 -48
  385. package/src/internal/proxy/typed-handler.ts +283 -61
  386. package/src/internal/proxy/typed-object.test.ts +54 -38
  387. package/src/internal/ref/ref-array.ts +4 -4
  388. package/src/internal/ref/ref.test.ts +10 -9
  389. package/src/internal/ref/ref.ts +107 -56
  390. package/src/internal/{projection → schema}/compose.test.ts +8 -9
  391. package/src/internal/{projection → schema}/compose.ts +13 -8
  392. package/src/internal/schema/echo-schema.ts +74 -33
  393. package/src/internal/schema/index.ts +1 -4
  394. package/src/internal/schema/manipulation.ts +1 -1
  395. package/src/internal/schema/persistent-schema.ts +33 -0
  396. package/src/internal/types/base.ts +43 -0
  397. package/src/internal/types/entity.ts +54 -0
  398. package/src/internal/types/index.ts +5 -2
  399. package/src/internal/types/meta.ts +68 -0
  400. package/src/internal/types/typename.ts +55 -0
  401. package/src/internal/types/version.ts +19 -0
  402. package/src/testing/api.test.ts +126 -0
  403. package/src/testing/index.ts +3 -3
  404. package/src/testing/test-data.ts +130 -0
  405. package/src/testing/test-schema.ts +240 -0
  406. package/src/testing/util.ts +85 -0
  407. package/dist/lib/browser/chunk-HKFCK2GL.mjs +0 -175
  408. package/dist/lib/browser/chunk-HKFCK2GL.mjs.map +0 -7
  409. package/dist/lib/browser/chunk-MAAYELT7.mjs +0 -830
  410. package/dist/lib/browser/chunk-MAAYELT7.mjs.map +0 -7
  411. package/dist/lib/browser/chunk-MB6MMNFP.mjs +0 -3857
  412. package/dist/lib/browser/chunk-MB6MMNFP.mjs.map +0 -7
  413. package/dist/lib/browser/index.mjs +0 -35
  414. package/dist/lib/browser/internal/index.mjs +0 -332
  415. package/dist/lib/browser/meta.json +0 -1
  416. package/dist/lib/browser/query/index.mjs +0 -15
  417. package/dist/lib/browser/testing/index.mjs +0 -298
  418. package/dist/lib/browser/testing/index.mjs.map +0 -7
  419. package/dist/lib/node-esm/chunk-5NWDGIBT.mjs +0 -830
  420. package/dist/lib/node-esm/chunk-5NWDGIBT.mjs.map +0 -7
  421. package/dist/lib/node-esm/chunk-AUAH4E2J.mjs +0 -175
  422. package/dist/lib/node-esm/chunk-AUAH4E2J.mjs.map +0 -7
  423. package/dist/lib/node-esm/chunk-BQRA4VLX.mjs +0 -3857
  424. package/dist/lib/node-esm/chunk-BQRA4VLX.mjs.map +0 -7
  425. package/dist/lib/node-esm/index.mjs +0 -35
  426. package/dist/lib/node-esm/meta.json +0 -1
  427. package/dist/lib/node-esm/query/index.mjs +0 -15
  428. package/dist/lib/node-esm/testing/index.mjs +0 -298
  429. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  430. package/dist/types/src/errors.d.ts +0 -72
  431. package/dist/types/src/errors.d.ts.map +0 -1
  432. package/dist/types/src/internal/ast/annotation-helper.d.ts +0 -8
  433. package/dist/types/src/internal/ast/annotation-helper.d.ts.map +0 -1
  434. package/dist/types/src/internal/ast/annotations.d.ts +0 -125
  435. package/dist/types/src/internal/ast/annotations.d.ts.map +0 -1
  436. package/dist/types/src/internal/ast/annotations.test.d.ts.map +0 -1
  437. package/dist/types/src/internal/ast/entity-kind.d.ts +0 -10
  438. package/dist/types/src/internal/ast/entity-kind.d.ts.map +0 -1
  439. package/dist/types/src/internal/ast/index.d.ts +0 -5
  440. package/dist/types/src/internal/ast/index.d.ts.map +0 -1
  441. package/dist/types/src/internal/ast/types.d.ts +0 -6
  442. package/dist/types/src/internal/ast/types.d.ts.map +0 -1
  443. package/dist/types/src/internal/json/annotations.d.ts.map +0 -1
  444. package/dist/types/src/internal/json/effect-schema.test.d.ts.map +0 -1
  445. package/dist/types/src/internal/json/index.d.ts +0 -2
  446. package/dist/types/src/internal/json/index.d.ts.map +0 -1
  447. package/dist/types/src/internal/json/json-schema.d.ts.map +0 -1
  448. package/dist/types/src/internal/json/json-schema.test.d.ts.map +0 -1
  449. package/dist/types/src/internal/object/accessors.d.ts +0 -37
  450. package/dist/types/src/internal/object/accessors.d.ts.map +0 -1
  451. package/dist/types/src/internal/object/create.d.ts.map +0 -1
  452. package/dist/types/src/internal/object/create.test.d.ts +0 -2
  453. package/dist/types/src/internal/object/create.test.d.ts.map +0 -1
  454. package/dist/types/src/internal/object/entity.d.ts +0 -33
  455. package/dist/types/src/internal/object/entity.d.ts.map +0 -1
  456. package/dist/types/src/internal/object/expando.d.ts +0 -14
  457. package/dist/types/src/internal/object/expando.d.ts.map +0 -1
  458. package/dist/types/src/internal/object/meta.d.ts.map +0 -1
  459. package/dist/types/src/internal/object/model.d.ts +0 -117
  460. package/dist/types/src/internal/object/model.d.ts.map +0 -1
  461. package/dist/types/src/internal/object/relation.d.ts +0 -17
  462. package/dist/types/src/internal/object/relation.d.ts.map +0 -1
  463. package/dist/types/src/internal/object/typename.d.ts +0 -15
  464. package/dist/types/src/internal/object/typename.d.ts.map +0 -1
  465. package/dist/types/src/internal/object/version.d.ts +0 -14
  466. package/dist/types/src/internal/object/version.d.ts.map +0 -1
  467. package/dist/types/src/internal/projection/compose.d.ts +0 -6
  468. package/dist/types/src/internal/projection/compose.d.ts.map +0 -1
  469. package/dist/types/src/internal/projection/compose.test.d.ts.map +0 -1
  470. package/dist/types/src/internal/projection/index.d.ts +0 -2
  471. package/dist/types/src/internal/projection/index.d.ts.map +0 -1
  472. package/dist/types/src/internal/proxy/reactive-object.d.ts +0 -15
  473. package/dist/types/src/internal/proxy/reactive-object.d.ts.map +0 -1
  474. package/dist/types/src/internal/query/index.d.ts +0 -2
  475. package/dist/types/src/internal/query/index.d.ts.map +0 -1
  476. package/dist/types/src/internal/query/query.d.ts +0 -17
  477. package/dist/types/src/internal/query/query.d.ts.map +0 -1
  478. package/dist/types/src/internal/schema/runtime-schema-registry.d.ts +0 -18
  479. package/dist/types/src/internal/schema/runtime-schema-registry.d.ts.map +0 -1
  480. package/dist/types/src/internal/schema/snapshot.d.ts +0 -6
  481. package/dist/types/src/internal/schema/snapshot.d.ts.map +0 -1
  482. package/dist/types/src/internal/schema/stored-schema.d.ts +0 -13
  483. package/dist/types/src/internal/schema/stored-schema.d.ts.map +0 -1
  484. package/dist/types/src/internal/testing/index.d.ts +0 -3
  485. package/dist/types/src/internal/testing/index.d.ts.map +0 -1
  486. package/dist/types/src/internal/testing/types.d.ts +0 -455
  487. package/dist/types/src/internal/testing/types.d.ts.map +0 -1
  488. package/dist/types/src/internal/testing/utils.d.ts +0 -10
  489. package/dist/types/src/internal/testing/utils.d.ts.map +0 -1
  490. package/dist/types/src/internal/types/types.d.ts +0 -79
  491. package/dist/types/src/internal/types/types.d.ts.map +0 -1
  492. package/dist/types/src/internal/types/types.test.d.ts +0 -2
  493. package/dist/types/src/internal/types/types.test.d.ts.map +0 -1
  494. package/dist/types/src/internal/types/util.d.ts +0 -5
  495. package/dist/types/src/internal/types/util.d.ts.map +0 -1
  496. package/dist/types/src/query/index.d.ts +0 -3
  497. package/dist/types/src/query/index.d.ts.map +0 -1
  498. package/dist/types/src/query/query.d.ts +0 -248
  499. package/dist/types/src/query/query.d.ts.map +0 -1
  500. package/dist/types/src/query/query.test.d.ts +0 -2
  501. package/dist/types/src/query/query.test.d.ts.map +0 -1
  502. package/dist/types/src/query/tag.d.ts +0 -17
  503. package/dist/types/src/query/tag.d.ts.map +0 -1
  504. package/dist/types/src/testing/echo-schema.d.ts +0 -7
  505. package/dist/types/src/testing/echo-schema.d.ts.map +0 -1
  506. package/dist/types/src/testing/types.d.ts +0 -113
  507. package/dist/types/src/testing/types.d.ts.map +0 -1
  508. package/src/errors.ts +0 -18
  509. package/src/internal/ast/annotation-helper.ts +0 -22
  510. package/src/internal/ast/annotations.ts +0 -219
  511. package/src/internal/ast/entity-kind.ts +0 -15
  512. package/src/internal/ast/index.ts +0 -8
  513. package/src/internal/ast/types.ts +0 -17
  514. package/src/internal/json/index.ts +0 -5
  515. package/src/internal/object/accessors.ts +0 -153
  516. package/src/internal/object/entity.ts +0 -248
  517. package/src/internal/object/expando.ts +0 -21
  518. package/src/internal/object/meta.ts +0 -61
  519. package/src/internal/object/model.ts +0 -170
  520. package/src/internal/object/relation.ts +0 -24
  521. package/src/internal/object/typename.ts +0 -61
  522. package/src/internal/object/version.ts +0 -22
  523. package/src/internal/projection/index.ts +0 -5
  524. package/src/internal/proxy/reactive-object.ts +0 -108
  525. package/src/internal/query/index.ts +0 -5
  526. package/src/internal/query/query.ts +0 -23
  527. package/src/internal/schema/runtime-schema-registry.ts +0 -78
  528. package/src/internal/schema/snapshot.ts +0 -25
  529. package/src/internal/schema/stored-schema.ts +0 -26
  530. package/src/internal/testing/index.ts +0 -6
  531. package/src/internal/testing/types.ts +0 -211
  532. package/src/internal/testing/utils.ts +0 -54
  533. package/src/internal/types/types.test.ts +0 -48
  534. package/src/internal/types/types.ts +0 -176
  535. package/src/internal/types/util.ts +0 -9
  536. package/src/query/index.ts +0 -6
  537. package/src/query/query.test.ts +0 -401
  538. package/src/query/query.ts +0 -789
  539. package/src/test/api.test.ts +0 -180
  540. package/src/testing/echo-schema.ts +0 -39
  541. package/src/testing/types.ts +0 -91
  542. /package/dist/lib/{browser/index.mjs.map → neutral/Annotation.mjs.map} +0 -0
  543. /package/dist/lib/{browser/internal/index.mjs.map → neutral/Database.mjs.map} +0 -0
  544. /package/dist/lib/{browser/query/index.mjs.map → neutral/Entity.mjs.map} +0 -0
  545. /package/dist/lib/{node-esm/index.mjs.map → neutral/Err.mjs.map} +0 -0
  546. /package/dist/lib/{node-esm/internal/index.mjs.map → neutral/Feed.mjs.map} +0 -0
  547. /package/dist/lib/{node-esm/query/index.mjs.map → neutral/Filter.mjs.map} +0 -0
  548. /package/dist/types/src/internal/{ast → annotations}/annotations.test.d.ts +0 -0
  549. /package/dist/types/src/internal/{json → json-schema}/annotations.d.ts +0 -0
  550. /package/dist/types/src/internal/{json → json-schema}/effect-schema.test.d.ts +0 -0
  551. /package/dist/types/src/internal/{json → json-schema}/json-schema.test.d.ts +0 -0
  552. /package/dist/types/src/internal/{projection → schema}/compose.test.d.ts +0 -0
  553. /package/dist/types/src/{test → testing}/api.test.d.ts +0 -0
  554. /package/src/internal/{json → json-schema}/effect-schema.test.ts +0 -0
package/src/Obj.ts CHANGED
@@ -2,67 +2,141 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
+ import * as Effect from 'effect/Effect';
5
6
  import * as Function from 'effect/Function';
7
+ import * as Option from 'effect/Option';
6
8
  import * as Schema from 'effect/Schema';
7
9
 
8
- import { assertArgument, invariant } from '@dxos/invariant';
9
- import { type DXN } from '@dxos/keys';
10
- import * as LiveObject from '@dxos/live-object';
11
- import { assumeType, deepMapValues } from '@dxos/util';
10
+ import type { ForeignKey } from '@dxos/echo-protocol';
11
+ import { createJsonPath } from '@dxos/effect';
12
+ import { assertArgument } from '@dxos/invariant';
13
+ import { type DXN, ObjectId } from '@dxos/keys';
14
+ import { assumeType } from '@dxos/util';
12
15
 
13
- import { live } from './internal';
14
- import * as EchoSchema from './internal';
16
+ import type * as Database from './Database';
17
+ import * as Entity from './Entity';
18
+ import * as Err from './Err';
19
+ import * as internal from './internal';
15
20
  import type * as Ref from './Ref';
16
- import type * as Relation from './Relation';
17
- import * as Type from './Type';
21
+ import type * as Type from './Type';
18
22
 
19
23
  /**
20
- * NOTE: Don't export: Obj.Any and Obj.Obj form the public API.
24
+ * Base type for all ECHO objects.
21
25
  */
22
- interface BaseObj extends EchoSchema.HasId, Type.OfKind<EchoSchema.EntityKind.Object> {}
26
+ interface BaseObj extends internal.AnyEntity, Entity.OfKind<typeof Entity.Kind.Object> {}
23
27
 
24
28
  /**
25
29
  * Object type with specific properties.
26
30
  */
27
- export type Obj<Props> = BaseObj & Props;
31
+ export type OfShape<Props> = BaseObj & Props;
28
32
 
29
33
  /**
30
- * Base type for all ECHO objects.
31
- * This type does not define any properties.
34
+ * Object with no known properties beyond id and kind.
35
+ * Use this when the object's schema/properties are not known.
36
+ * For objects with arbitrary properties, use `Obj.AnyProps`.
37
+ *
38
+ * NOTE: This is a TypeScript type only, not a schema.
39
+ * To validate that a value is an ECHO object, use `Schema.is(Type.Obj)`.
32
40
  */
33
- export interface Any extends BaseObj {}
41
+ export interface Unknown extends BaseObj {}
42
+
43
+ /**
44
+ * Runtime Effect schema for any ECHO object.
45
+ * Use for validation, parsing, or as a reference target for collections.
46
+ *
47
+ * NOTE: `Schema.is(Type.Obj)` does STRUCTURAL validation only (checks for `id` field).
48
+ * Use `Obj.isObject()` for proper ECHO instance type guards that check the KindId brand.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * // Structural type guard (accepts any object with id field)
53
+ * if (Schema.is(Type.Obj)(unknownValue)) { ... }
54
+ *
55
+ * // ECHO instance type guard (checks KindId brand)
56
+ * if (Obj.isObject(unknownValue)) { ... }
57
+ *
58
+ * // Reference to any object type
59
+ * const Collection = Schema.Struct({
60
+ * objects: Schema.Array(Ref.Ref(Obj.Unknown)),
61
+ * }).pipe(Type.object({ typename: 'Collection', version: '0.1.0' }));
62
+ * ```
63
+ */
64
+ // TODO(wittjosiah): Investigate if Schema.filter can validate KindId on ECHO instances.
65
+ // Effect Schema normalizes proxy objects to plain objects before calling filter predicates.
66
+ // Possible approaches: custom Schema.declare, AST manipulation, or upstream contribution.
67
+ export const Unknown: Type.Obj<Unknown> = Schema.Struct({
68
+ id: Schema.String,
69
+ }).pipe(
70
+ Schema.extend(Schema.Record({ key: Schema.String, value: Schema.Unknown })),
71
+ // TODO(dmaretskyi): Clean this up.
72
+ // NOTE: The EchoObjectSchema annotation is required for Ref.Ref(Obj.Unknown) to work.
73
+ // The typename/version only satisfy ECHO schema machinery for reference targets.
74
+ internal.EchoObjectSchema({ typename: internal.ANY_OBJECT_TYPENAME, version: internal.ANY_OBJECT_VERSION }),
75
+ (schema) =>
76
+ Object.assign(schema, {
77
+ [internal.SchemaKindId]: (schema as any)[internal.SchemaKindId],
78
+ }) as unknown as Type.Obj<Unknown>,
79
+ );
80
+
81
+ /**
34
82
 
35
83
  /**
36
84
  * Object with arbitrary properties.
37
85
  *
38
- * NOTE: Due to how typescript works, this type is not assignable to a specific schema type.
86
+ * NOTE: Due to how TypeScript works, this type is not assignable to a specific schema type.
39
87
  * In that case, use `Obj.instanceOf` to check if an object is of a specific type.
88
+ *
89
+ * Prefer using `Obj.Unknown` when you don't need to access arbitrary properties.
40
90
  */
41
- export interface AnyProps extends BaseObj {
42
- [key: string]: any;
91
+ export interface Any extends BaseObj, internal.AnyProperties {}
92
+
93
+ /**
94
+ * Base type for snapshot objects (has SnapshotKindId instead of KindId).
95
+ */
96
+ interface BaseSnapshot extends internal.AnyEntity {
97
+ readonly [Entity.SnapshotKindId]: typeof Entity.Kind.Object;
98
+ readonly id: ObjectId;
43
99
  }
44
100
 
45
- export const Any = Schema.Struct({}).pipe(
46
- Type.Obj({
47
- typename: 'dxos.org/types/Any',
48
- version: '0.1.0',
49
- }),
50
- );
101
+ /**
102
+ * Immutable snapshot of an ECHO object.
103
+ * Branded with SnapshotKindId (not KindId).
104
+ * Property values are frozen at the time the snapshot was created.
105
+ * Returned by getSnapshot() and hooks like useObject().
106
+ *
107
+ * Snapshots are structurally identical to reactive objects but have a different brand,
108
+ * making them distinguishable at the TypeScript level. Neither is assignable to the other.
109
+ */
110
+ export type Snapshot<T extends Unknown = Unknown> = Omit<T, Entity.KindId> & BaseSnapshot;
51
111
 
52
- type Props<T = any> = { id?: EchoSchema.ObjectId } & Type.Properties<T>;
112
+ /**
113
+ * JSON-encoded properties for objects.
114
+ */
115
+ export interface BaseObjJson {
116
+ id: string;
117
+ }
53
118
 
54
- export type MakeProps<T extends Type.Obj.Any> = NoInfer<Props<Schema.Schema.Type<T>>> & {
55
- [Meta]?: Partial<EchoSchema.ObjectMeta>;
119
+ const defaultMeta: internal.ObjectMeta = {
120
+ keys: [],
56
121
  };
57
122
 
58
- export const Meta: unique symbol = EchoSchema.MetaId as any;
123
+ type MakePropsInternal<T extends Unknown> = {
124
+ id?: ObjectId;
125
+ [Meta]?: Partial<internal.ObjectMeta>;
126
+ } & Entity.Properties<T>;
59
127
 
60
- const DEFAULT_META: EchoSchema.ObjectMeta = {
61
- keys: [],
62
- };
128
+ // TODO(burdon): Should we allow the caller to set the id?
129
+ /**
130
+ * Props type for object creation with a given schema.
131
+ */
132
+ export type MakeProps<S extends Schema.Schema.AnyNoContext> = {
133
+ id?: ObjectId;
134
+ [Meta]?: Partial<internal.ObjectMeta>;
135
+ [Parent]?: Unknown;
136
+ } & MakePropsInternal<Schema.Schema.Type<S>>;
63
137
 
64
138
  /**
65
- * Creates new object.
139
+ * Creates a new echo object of the given schema.
66
140
  * @param schema - Object schema.
67
141
  * @param props - Object properties.
68
142
  * @param meta - Object metadata (deprecated) -- pass with Obj.Meta.
@@ -73,41 +147,265 @@ const DEFAULT_META: EchoSchema.ObjectMeta = {
73
147
  * ```ts
74
148
  * const obj = Obj.make(Person, { [Obj.Meta]: { keys: [...] }, name: 'John' });
75
149
  * ```
150
+ *
151
+ * Note: Only accepts object schemas, not relation schemas. Use `Relation.make` for relations.
76
152
  */
77
- export const make = <S extends Type.Obj.Any>(
153
+ export const make = <S extends Type.AnyObj>(
78
154
  schema: S,
79
- props: MakeProps<S>,
80
- meta?: Partial<EchoSchema.ObjectMeta>,
81
- ): LiveObject.Live<Schema.Schema.Type<S>> => {
155
+ props: NoInfer<MakeProps<S>>,
156
+ ): OfShape<Schema.Schema.Type<S>> => {
82
157
  assertArgument(
83
- EchoSchema.getTypeAnnotation(schema)?.kind === EchoSchema.EntityKind.Object,
158
+ internal.getTypeAnnotation(schema)?.kind === Entity.Kind.Object,
84
159
  'schema',
85
160
  'Expected an object schema',
86
161
  );
87
162
 
88
- if (props[EchoSchema.MetaId] != null) {
89
- // Set default fields on meta on creation.
90
- meta = { ...structuredClone(DEFAULT_META), ...props[EchoSchema.MetaId] };
91
- delete props[EchoSchema.MetaId];
163
+ let meta: internal.ObjectMeta | undefined = undefined;
164
+
165
+ // Set default fields on meta on creation.
166
+ if (props[internal.MetaId] != null) {
167
+ meta = { ...structuredClone(defaultMeta), ...props[internal.MetaId] };
168
+ delete props[internal.MetaId];
92
169
  }
93
170
 
94
- const filterUndefined = Object.fromEntries(Object.entries(props).filter(([_, v]) => v !== undefined));
171
+ // Filter undefined values (Object.entries only returns string-keyed properties).
172
+ const filterUndefined: any = Object.fromEntries(Object.entries(props).filter(([_, v]) => v !== undefined));
173
+
174
+ // Copy symbol properties (like ParentId) that Object.entries doesn't include.
175
+ for (const sym of Object.getOwnPropertySymbols(props)) {
176
+ const value = (props as any)[sym];
177
+ if (value !== undefined) {
178
+ filterUndefined[sym] = value;
179
+ }
180
+ }
95
181
 
96
- return live<Schema.Schema.Type<S>>(schema, filterUndefined as any, { keys: [], ...meta });
182
+ return internal.makeObject<Schema.Schema.Type<S>>(schema, filterUndefined as any, {
183
+ ...defaultMeta,
184
+ ...meta,
185
+ });
186
+ };
187
+
188
+ /**
189
+ * Determine if object is an ECHO object.
190
+ */
191
+ export const isObject = (obj: unknown): obj is Unknown => {
192
+ assumeType<internal.InternalObjectProps>(obj);
193
+ return typeof obj === 'object' && obj !== null && obj[Entity.KindId] === Entity.Kind.Object;
97
194
  };
98
195
 
99
- export const isObject = (obj: unknown): obj is Any => {
100
- assumeType<EchoSchema.InternalObjectProps>(obj);
101
- return typeof obj === 'object' && obj !== null && obj[EchoSchema.EntityKindId] === EchoSchema.EntityKind.Object;
196
+ export const isSnapshot = (obj: unknown): obj is Snapshot => {
197
+ assumeType<internal.InternalObjectProps>(obj);
198
+ return typeof obj === 'object' && obj !== null && (obj as any)[Entity.SnapshotKindId] === Entity.Kind.Object;
199
+ };
200
+
201
+ /**
202
+ * Subscribe to object updates.
203
+ * The callback is called synchronously when the object is modified.
204
+ * Only accepts reactive objects (not snapshots).
205
+ * @returns Unsubscribe function.
206
+ */
207
+ export const subscribe = (obj: Unknown, callback: () => void): (() => void) => {
208
+ return internal.subscribe(obj, callback);
102
209
  };
103
210
 
211
+ //
212
+ // Snapshot
213
+ //
214
+
215
+ /**
216
+ * Returns an immutable snapshot of an object.
217
+ * The snapshot is branded with SnapshotKindId instead of KindId,
218
+ * making it distinguishable from the reactive object at the type level.
219
+ */
220
+ export const getSnapshot: <T extends Unknown>(obj: T) => Snapshot<T> = internal.getSnapshot as any;
221
+
222
+ /**
223
+ * Returns the reactive version of an object from the database, given its snapshot.
224
+ * Inverse of `Obj.getSnapshot`.
225
+ *
226
+ * Uses `Obj.getDatabase` internally to get the database from the snapshot,
227
+ * then resolves the reactive object by ID.
228
+ *
229
+ * @param snapshot - A snapshot of the object (from `Obj.getSnapshot`).
230
+ * @returns Effect that succeeds with the reactive object, or fails with `GetReactiveError`.
231
+ * @example
232
+ * ```ts
233
+ * const snapshot = Obj.getSnapshot(obj);
234
+ * const reactive = Obj.getReactive(snapshot).pipe(
235
+ * Effect.runSync
236
+ * );
237
+ * ```
238
+ */
239
+ export const getReactive = <T extends Unknown>(snapshot: Snapshot<T>): Effect.Effect<T, Err.GetReactiveError> =>
240
+ Effect.gen(function* () {
241
+ const db = internal.getDatabase(snapshot);
242
+ if (!db) {
243
+ return yield* Effect.fail(new Err.GetReactiveError({ reason: 'no-database', snapshotId: snapshot.id }));
244
+ }
245
+ const obj = db.getObjectById(snapshot.id);
246
+ if (!obj) {
247
+ return yield* Effect.fail(new Err.GetReactiveError({ reason: 'object-not-found', snapshotId: snapshot.id }));
248
+ }
249
+ return obj as T;
250
+ });
251
+
252
+ /**
253
+ * Like `Obj.getReactive` but returns `Option.none()` instead of failing when the object
254
+ * cannot be resolved (no database, object not found).
255
+ *
256
+ * @param snapshot - A snapshot of the object (from `Obj.getSnapshot`).
257
+ * @returns Effect that succeeds with `Option.some(reactive)` or `Option.none()`.
258
+ */
259
+ export const getReactiveOption = <T extends Unknown>(snapshot: Snapshot<T>): Effect.Effect<Option.Option<T>, never> =>
260
+ getReactive(snapshot).pipe(
261
+ Effect.map(Option.some),
262
+ Effect.catchAll(() => Effect.succeed(Option.none())),
263
+ );
264
+
265
+ /**
266
+ * Synchronous version of `Obj.getReactive`. Returns the reactive object or throws
267
+ * `GetReactiveError` when the object cannot be resolved (no database, object not found).
268
+ *
269
+ * @param snapshot - A snapshot of the object (from `Obj.getSnapshot`).
270
+ * @returns The reactive object.
271
+ * @throws {Err.GetReactiveError} When the object cannot be resolved.
272
+ */
273
+ export const getReactiveOrThrow = <T extends Unknown>(snapshot: Snapshot<T>): T =>
274
+ Effect.runSync(getReactive(snapshot));
275
+
276
+ export type CloneOptions = {
277
+ /**
278
+ * Retain the original object's ID.
279
+ * @default false
280
+ */
281
+ retainId?: boolean;
282
+
283
+ /**
284
+ * Recursively clone referenced objects.
285
+ * @default false
286
+ */
287
+ deep?: boolean;
288
+ };
289
+
290
+ /**
291
+ * Clones an object or relation.
292
+ * This does not clone referenced objects, only the properties in the object.
293
+ * @returns A new object with the same schema and properties.
294
+ */
295
+ export const clone: <T extends Unknown>(obj: T, opts?: CloneOptions) => T = internal.clone;
296
+
297
+ //
298
+ // Change
299
+ //
300
+
301
+ /**
302
+ * Makes all properties mutable recursively.
303
+ * Used to provide a mutable view of an object within `Obj.change`.
304
+ */
305
+ export type Mutable<T> = internal.Mutable<T>;
306
+
307
+ /**
308
+ * Perform mutations on an echo object within a controlled context.
309
+ *
310
+ * All mutations within the callback are batched and trigger a single notification
311
+ * when the callback completes. Direct mutations outside of `Obj.change` will throw
312
+ * an error for echo objects.
313
+ *
314
+ * This function also works with nested objects within echo objects (e.g., Template structs)
315
+ * that are reactive at runtime.
316
+ *
317
+ * @param obj - The echo object to mutate. Use `Relation.change` for relations.
318
+ * @param callback - The callback that performs mutations on the object.
319
+ *
320
+ * @example
321
+ * ```ts
322
+ * const person = Obj.make(Person, { name: 'John', age: 25 });
323
+ *
324
+ * // Mutate within Obj.change
325
+ * Obj.change(person, (p) => {
326
+ * p.name = 'Jane';
327
+ * p.age = 30;
328
+ * });
329
+ * // ONE notification fires here
330
+ *
331
+ * // Direct mutation throws
332
+ * person.name = 'Bob'; // Error: Cannot modify outside Obj.change()
333
+ * ```
334
+ *
335
+ * Note: Only accepts objects. Use `Relation.change` for relations.
336
+ */
337
+ export const change = <T extends Unknown>(obj: T, callback: internal.ChangeCallback<T>): void => {
338
+ internal.change(obj, callback);
339
+ };
340
+
341
+ /**
342
+ * Get a deeply nested property from an object.
343
+ *
344
+ * Similar to lodash.get and getDeep from @dxos/util.
345
+ * This is the complementary function to setValue.
346
+ * Accepts both reactive objects and snapshots.
347
+ *
348
+ * @param obj - The ECHO object to get the property from.
349
+ * @param path - Path to the property (array of keys).
350
+ * @returns The value at the path, or undefined if not found.
351
+ *
352
+ * @example
353
+ * ```ts
354
+ * const person = Obj.make(Person, {
355
+ * name: 'John',
356
+ * addresses: [{ street: '123 Main St' }]
357
+ * });
358
+ *
359
+ * Obj.getValue(person, ['addresses', 0, 'street']); // '123 Main St'
360
+ * Obj.getValue(person, ['addresses', 1, 'street']); // undefined
361
+ * ```
362
+ */
363
+ export const getValue = (obj: Unknown | Snapshot, path: readonly (string | number)[]): any => {
364
+ return internal.getValue(obj, createJsonPath(path));
365
+ };
366
+
367
+ /**
368
+ * Set a deeply nested property on an object, using the object's schema to determine
369
+ * whether to initialize nested data as an empty object or array.
370
+ *
371
+ * Similar to lodash.set and setDeep from @dxos/util, but schema-aware.
372
+ * Must be called within an `Obj.change` callback.
373
+ *
374
+ * NOTE: TypeScript's structural typing allows readonly objects to be passed to `Mutable<T>`
375
+ * parameters, so there is no compile-time error. Enforcement is runtime-only.
376
+ *
377
+ * @param obj - The mutable ECHO object to set the property on.
378
+ * @param path - Path to the property (array of keys).
379
+ * @param value - Value to set.
380
+ * @returns The value that was set.
381
+ *
382
+ * @example
383
+ * ```ts
384
+ * const person = Obj.make(Person, { name: 'John' });
385
+ * // Person schema has: addresses: Schema.Array(Address)
386
+ * Obj.change(person, (p) => {
387
+ * Obj.setValue(p, ['addresses', 0, 'street'], '123 Main St');
388
+ * });
389
+ * // Creates: person.addresses = [{ street: '123 Main St' }]
390
+ * ```
391
+ */
392
+ // TODO(wittjosiah): Compute possible path values + type value based on generic object type.
393
+ export const setValue: (obj: Mutable<Unknown>, path: readonly (string | number)[], value: any) => void =
394
+ internal.setValue as any;
395
+
396
+ //
397
+ // Type
398
+ //
399
+
400
+ // TODO(burdon): To discuss: prefer over ObjectId or Key.ObjectId or Type.ID?
401
+ export const ID = ObjectId;
402
+ export type ID = ObjectId;
403
+
104
404
  /**
105
405
  * Test if object or relation is an instance of a schema.
106
406
  * @example
107
407
  * ```ts
108
408
  * const john = Obj.make(Person, { name: 'John' });
109
- * const johnIsPerson = Obj.instanceOf(Person)(john);
110
- *
111
409
  * const isPerson = Obj.instanceOf(Person);
112
410
  * if (isPerson(john)) {
113
411
  * // john is Person
@@ -115,298 +413,313 @@ export const isObject = (obj: unknown): obj is Any => {
115
413
  * ```
116
414
  */
117
415
  export const instanceOf: {
118
- <S extends Type.Relation.Any | Type.Obj.Any>(schema: S): (value: unknown) => value is Schema.Schema.Type<S>;
119
- <S extends Type.Relation.Any | Type.Obj.Any>(schema: S, value: unknown): value is Schema.Schema.Type<S>;
120
- } = ((
121
- ...args: [schema: Type.Relation.Any | Type.Obj.Any, value: unknown] | [schema: Type.Relation.Any | Type.Obj.Any]
122
- ) => {
416
+ <S extends Type.AnyEntity>(schema: S): (value: unknown) => value is Schema.Schema.Type<S>;
417
+ <S extends Type.AnyEntity>(schema: S, value: unknown): value is Schema.Schema.Type<S>;
418
+ } = ((...args: [schema: Type.AnyEntity, value: unknown] | [schema: Type.AnyEntity]) => {
123
419
  if (args.length === 1) {
124
- return (obj: unknown) => EchoSchema.isInstanceOf(args[0], obj);
420
+ return (entity: unknown) => internal.isInstanceOf(args[0], entity);
125
421
  }
126
422
 
127
- return EchoSchema.isInstanceOf(args[0], args[1]);
423
+ return internal.isInstanceOf(args[0], args[1]);
128
424
  }) as any;
129
425
 
130
- export const getSchema = EchoSchema.getSchema;
426
+ /**
427
+ * Test if a snapshot is an instance of a schema.
428
+ * Mirrors `instanceOf` but only accepts values branded with SnapshotKindId.
429
+ * Use when the value is known to be a snapshot (e.g. from `getSnapshot` or `useObject`).
430
+ *
431
+ * @example
432
+ * ```ts
433
+ * const snapshot = Obj.getSnapshot(person);
434
+ * if (Obj.snapshotOf(Person, snapshot)) {
435
+ * // snapshot is Obj.Snapshot<Person>
436
+ * }
437
+ * ```
438
+ */
439
+ export const snapshotOf: {
440
+ <S extends Type.AnyEntity>(schema: S): (value: unknown) => value is Snapshot<Schema.Schema.Type<S>>;
441
+ <S extends Type.AnyEntity>(schema: S, value: unknown): value is Snapshot<Schema.Schema.Type<S>>;
442
+ } = ((...args: [schema: Type.AnyEntity, value: unknown] | [schema: Type.AnyEntity]) => {
443
+ const check = (entity: unknown) =>
444
+ entity != null &&
445
+ typeof entity === 'object' &&
446
+ Entity.SnapshotKindId in entity &&
447
+ internal.isInstanceOf(args[0], entity);
448
+
449
+ if (args.length === 1) {
450
+ return (entity: unknown) => check(entity);
451
+ }
452
+
453
+ return check(args[1]);
454
+ }) as any;
131
455
 
132
456
  // TODO(dmaretskyi): Allow returning undefined.
133
- export const getDXN = (obj: Any | Relation.Any): DXN => {
134
- assertArgument(!Schema.isSchema(obj), 'obj', 'Object should not be a schema.');
135
- const dxn = EchoSchema.getObjectDXN(obj);
136
- invariant(dxn != null, 'Invalid object.');
137
- return dxn;
457
+ /**
458
+ * Get the DXN of the object.
459
+ * Accepts both reactive objects and snapshots.
460
+ */
461
+ export const getDXN = (entity: Unknown | Snapshot): DXN => {
462
+ assertArgument(!Schema.isSchema(entity), 'obj', 'Object should not be a schema.');
463
+ return internal.getDXN(entity);
138
464
  };
139
465
 
140
466
  /**
141
467
  * @returns The DXN of the object's type.
142
- * @example dxn:example.com/type/Contact:1.0.0
468
+ * @example dxn:example.com/type/Person:1.0.0
469
+ */
470
+ // TODO(wittjosiah): Narrow types.
471
+ export const getTypeDXN: (obj: unknown | undefined) => DXN | undefined = internal.getTypeDXN as any;
472
+
473
+ /**
474
+ * Get the schema of the object.
475
+ * Returns the branded ECHO schema used to create the object.
143
476
  */
144
- // TODO(burdon): Expando does not have a type.
145
- export const getTypeDXN = EchoSchema.getType;
477
+ // TODO(wittjosiah): Narrow types.
478
+ export const getSchema: (obj: unknown | undefined) => Type.AnyEntity | undefined = internal.getSchema as any;
146
479
 
147
480
  /**
148
481
  * @returns The typename of the object's type.
149
- * @example `example.com/type/Contact`
482
+ * Accepts both reactive objects and snapshots.
483
+ * @example `example.com/type/Person`
150
484
  */
151
- export const getTypename = (obj: Any | Relation.Any): string | undefined => {
152
- const schema = getSchema(obj);
153
- if (schema == null) {
154
- // Try to extract typename from DXN.
155
- return EchoSchema.getType(obj)?.asTypeDXN()?.type;
156
- }
485
+ export const getTypename = (entity: Unknown | Snapshot): string | undefined => internal.getTypename(entity);
157
486
 
158
- return EchoSchema.getSchemaTypename(schema);
159
- };
487
+ //
488
+ // Database
489
+ //
490
+
491
+ /**
492
+ * Get the database the object belongs to.
493
+ * Accepts both reactive objects and snapshots.
494
+ */
495
+ export const getDatabase = (entity: Entity.Unknown | Entity.Snapshot): Database.Database | undefined =>
496
+ internal.getDatabase(entity);
497
+
498
+ //
499
+ // Meta
500
+ //
501
+
502
+ /**
503
+ * Property that accesses metadata for an entity.
504
+ *
505
+ * Alias for `Entity.Meta`.
506
+ */
507
+ export const Meta = internal.MetaId;
508
+
509
+ /**
510
+ * Deeply read-only version of ObjectMeta.
511
+ * Prevents mutation at all nesting levels (e.g., `meta.keys.push()` is a TypeScript error).
512
+ */
513
+ export type ReadonlyMeta = internal.ReadonlyMeta;
514
+
515
+ /**
516
+ * Mutable meta type returned by `Obj.getMeta` inside an `Obj.change` callback.
517
+ */
518
+ export type Meta = internal.Meta;
160
519
 
520
+ // TODO(burdon): Narrow type.
161
521
  // TODO(dmaretskyi): Allow returning undefined.
162
- export const getMeta = (obj: Any | Relation.Any): EchoSchema.ObjectMeta => {
163
- const meta = EchoSchema.getMeta(obj);
164
- invariant(meta != null, 'Invalid object.');
165
- return meta;
166
- };
522
+ /**
523
+ * Get the metadata for an object.
524
+ * Returns mutable meta when passed a mutable object (inside `Obj.change` callback).
525
+ * Returns read-only meta when passed a regular object or snapshot.
526
+ *
527
+ * @example
528
+ * ```ts
529
+ * // Read-only access outside change callback
530
+ * const meta = Obj.getMeta(person); // ReadonlyMeta
531
+ *
532
+ * // Mutable access inside change callback
533
+ * Obj.change(person, (p) => {
534
+ * const meta = Obj.getMeta(p); // ObjectMeta (mutable)
535
+ * meta.tags ??= [];
536
+ * meta.tags.push('important');
537
+ * });
538
+ * ```
539
+ */
540
+ // TODO(wittjosiah): When passed a Snapshot, should return a snapshot of meta, not the live meta proxy.
541
+ export function getMeta(entity: Mutable<Unknown>): Meta;
542
+ export function getMeta(entity: Unknown | Snapshot): ReadonlyMeta;
543
+ export function getMeta(entity: Unknown | Snapshot | Mutable<Unknown>): Meta | ReadonlyMeta {
544
+ return internal.getMetaChecked(entity);
545
+ }
167
546
 
168
547
  /**
169
548
  * @returns Foreign keys for the object from the specified source.
549
+ * Accepts both reactive objects and snapshots.
170
550
  */
171
551
  export const getKeys: {
172
- (obj: Any | Relation.Any, source: string): EchoSchema.ForeignKey[];
173
- (source: string): (obj: Any | Relation.Any) => EchoSchema.ForeignKey[];
174
- } = Function.dual(2, (obj: Any | Relation.Any, source?: string): EchoSchema.ForeignKey[] => {
175
- const meta = EchoSchema.getMeta(obj);
176
- invariant(meta != null, 'Invalid object.');
177
- return meta.keys.filter((key) => key.source === source);
178
- });
552
+ (entity: Unknown | Snapshot, source: string): ForeignKey[];
553
+ (source: string): (entity: Unknown | Snapshot) => ForeignKey[];
554
+ } = Function.dual(2, (entity: Unknown | Snapshot, source?: string): ForeignKey[] => internal.getKeys(entity, source!));
179
555
 
180
556
  /**
181
557
  * Delete all keys from the object for the specified source.
182
- * @param obj
183
- * @param source
184
- */
185
- export const deleteKeys = (obj: Any | Relation.Any, source: string) => {
186
- const meta = EchoSchema.getMeta(obj);
187
- for (let i = 0; i < meta.keys.length; i++) {
188
- if (meta.keys[i].source === source) {
189
- meta.keys.splice(i, 1);
190
- i--;
191
- }
192
- }
193
- };
558
+ * Must be called within an `Obj.change` callback.
559
+ *
560
+ * NOTE: TypeScript's structural typing allows readonly objects to be passed to `Mutable<T>`
561
+ * parameters, so there is no compile-time error. Enforcement is runtime-only.
562
+ */
563
+ export const deleteKeys = (entity: Mutable<Unknown>, source: string): void => internal.deleteKeys(entity, source);
564
+
565
+ /**
566
+ * Add a tag to the object.
567
+ * Must be called within an `Obj.change` callback.
568
+ *
569
+ * NOTE: TypeScript's structural typing allows readonly objects to be passed to `Mutable<T>`
570
+ * parameters, so there is no compile-time error. Enforcement is runtime-only.
571
+ */
572
+ export const addTag = (entity: Mutable<Unknown>, tag: string): void => internal.addTag(entity, tag);
194
573
 
574
+ /**
575
+ * Remove a tag from the object.
576
+ * Must be called within an `Obj.change` callback.
577
+ *
578
+ * NOTE: TypeScript's structural typing allows readonly objects to be passed to `Mutable<T>`
579
+ * parameters, so there is no compile-time error. Enforcement is runtime-only.
580
+ */
581
+ export const removeTag = (entity: Mutable<Unknown>, tag: string): void => internal.removeTag(entity, tag);
582
+
583
+ /**
584
+ * Check if the object is deleted.
585
+ * Accepts both reactive objects and snapshots.
586
+ */
195
587
  // TODO(dmaretskyi): Default to `false`.
196
- export const isDeleted = (obj: Any | Relation.Any): boolean => {
197
- const deleted = EchoSchema.isDeleted(obj);
198
- invariant(typeof deleted === 'boolean', 'Invalid object.');
199
- return deleted;
200
- };
588
+ export const isDeleted = (entity: Unknown | Snapshot): boolean => internal.isDeleted(entity);
201
589
 
202
- export const getLabel = (obj: Any | Relation.Any): string | undefined => {
203
- const schema = getSchema(obj);
204
- if (schema != null) {
205
- return EchoSchema.getLabel(schema, obj);
206
- }
207
- };
590
+ //
591
+ // Annotations
592
+ //
208
593
 
209
- export const setLabel = (obj: Any | Relation.Any, label: string) => {
210
- const schema = getSchema(obj);
211
- if (schema != null) {
212
- EchoSchema.setLabel(schema, obj, label);
213
- }
214
- };
594
+ /**
595
+ * Get the label of the object.
596
+ * Accepts both reactive objects and snapshots.
597
+ */
598
+ export const getLabel = (entity: Unknown | Snapshot): string | undefined => internal.getLabel(entity);
215
599
 
216
- export const getDescription = (obj: Any | Relation.Any): string | undefined => {
217
- const schema = getSchema(obj);
218
- if (schema != null) {
219
- return EchoSchema.getDescription(schema, obj);
220
- }
221
- };
600
+ /**
601
+ * Set the label of the object.
602
+ * Must be called within an `Obj.change` callback.
603
+ *
604
+ * NOTE: TypeScript's structural typing allows readonly objects to be passed to `Mutable<T>`
605
+ * parameters, so there is no compile-time error. Enforcement is runtime-only.
606
+ */
607
+ export const setLabel = (entity: Mutable<Unknown>, label: string): void => internal.setLabel(entity, label);
222
608
 
223
- export const setDescription = (obj: Any | Relation.Any, description: string) => {
224
- const schema = getSchema(obj);
225
- if (schema != null) {
226
- EchoSchema.setDescription(schema, obj, description);
227
- }
228
- };
609
+ /**
610
+ * Get the description of the object.
611
+ * Accepts both reactive objects and snapshots.
612
+ */
613
+ export const getDescription = (entity: Unknown | Snapshot): string | undefined => internal.getDescription(entity);
229
614
 
230
- const compare = (a?: string, b?: string) => {
231
- if (a == null) {
232
- return b == null ? 0 : 1;
233
- }
615
+ /**
616
+ * Set the description of the object.
617
+ * Must be called within an `Obj.change` callback.
618
+ *
619
+ * NOTE: TypeScript's structural typing allows readonly objects to be passed to `Mutable<T>`
620
+ * parameters, so there is no compile-time error. Enforcement is runtime-only.
621
+ */
622
+ export const setDescription = (entity: Mutable<Unknown>, description: string): void =>
623
+ internal.setDescription(entity, description);
234
624
 
235
- if (b == null) {
236
- return -1;
237
- }
625
+ /**
626
+ * Symbol to set parent when creating objects with `Obj.make`.
627
+ * @example
628
+ * ```ts
629
+ * Obj.make(TestSchema.Person, {
630
+ * [Obj.Parent]: parentObject,
631
+ * name: 'John',
632
+ * })
633
+ * ```
634
+ */
635
+ export const Parent: unique symbol = internal.ParentId as any;
238
636
 
239
- return a.localeCompare(b);
637
+ /**
638
+ * Get the parent of an object.
639
+ * The parent is always loaded together with the object.
640
+ * Only objects are allowed to have a parent
641
+ * @returns The parent object, or undefined if the object has no parent.
642
+ */
643
+ export const getParent = (entity: Unknown | Snapshot): Unknown | undefined => {
644
+ assertArgument(isObject(entity), 'Expected an object');
645
+ assumeType<internal.InternalObjectProps>(entity);
646
+ return entity[internal.ParentId] as Unknown | undefined;
240
647
  };
241
648
 
242
- export type Comparator = (a: Any, b: Any) => number;
243
-
244
- export const sortByLabel: Comparator = (a: Any, b: Any) => compare(getLabel(a), getLabel(b));
245
- export const sortByTypename: Comparator = (a: Any, b: Any) => compare(getTypename(a), getTypename(b));
246
- export const sort = (...comparators: Comparator[]): Comparator => {
247
- return (a: Any, b: Any) => {
248
- for (const comparator of comparators) {
249
- const result = comparator(a, b);
250
- if (result !== 0) {
251
- return result;
252
- }
253
- }
254
-
255
- return 0;
256
- };
649
+ /**
650
+ * Sets the parent of an object.
651
+ * If a parent (or any transitive parent) is deleted, the object will be deleted.
652
+ * Only objects are allowed to have a parent.
653
+ */
654
+ export const setParent = (entity: Unknown, parent: Any | undefined) => {
655
+ assertArgument(isObject(entity), 'Expected an object');
656
+ assertArgument(parent === undefined || isObject(parent), 'Expected an object');
657
+ assumeType<internal.InternalObjectProps>(entity);
658
+ assumeType<internal.InternalObjectProps | undefined>(parent);
659
+ entity[internal.ParentId] = parent;
257
660
  };
258
661
 
662
+ //
663
+ // JSON
664
+ //
665
+
259
666
  /**
260
667
  * JSON representation of an object.
261
668
  */
262
- export type JSON = EchoSchema.ObjectJSON;
669
+ export type JSON = internal.ObjectJSON;
263
670
 
264
671
  /**
265
672
  * Converts object to its JSON representation.
673
+ * Accepts both reactive objects and snapshots.
266
674
  *
267
675
  * The same algorithm is used when calling the standard `JSON.stringify(obj)` function.
268
676
  */
269
- // TODO(burdon): Base util type for Obj/Relation?
270
- export const toJSON = (obj: Any | Relation.Any): JSON => EchoSchema.objectToJSON(obj);
677
+ export const toJSON = (entity: Unknown | Snapshot): JSON => internal.objectToJSON(entity);
271
678
 
272
679
  /**
273
680
  * Creates an object from its json representation, performing schema validation.
274
681
  * References and schemas will be resolvable if the `refResolver` is provided.
275
682
  *
276
- * The function need to be async to support resolving the schema as well as the relation endpoints.
683
+ * The function must be async to support resolving the schema as well as the relation endpoints.
277
684
  *
278
685
  * @param options.refResolver - Resolver for references. Produces hydrated references that can be resolved.
279
686
  * @param options.dxn - Override object DXN. Changes the result of `Obj.getDXN`.
280
687
  */
281
- export const fromJSON: (json: unknown, options?: { refResolver?: Ref.Resolver; dxn?: DXN }) => Promise<Any> =
282
- EchoSchema.objectFromJSON as any;
688
+ export const fromJSON: (json: unknown, options?: { refResolver?: Ref.Resolver; dxn?: DXN }) => Promise<Unknown> =
689
+ internal.objectFromJSON as any;
283
690
 
284
691
  /**
285
- * Returns an immutable snapshot of an object.
692
+ * Comparator function type for sorting objects.
693
+ * Accepts both reactive objects and snapshots.
286
694
  */
287
- export const getSnapshot: <T extends Any>(obj: Obj<T>) => T = LiveObject.getSnapshot;
695
+ export type Comparator = internal.Comparator<Unknown | Snapshot>;
288
696
 
289
- export type CloneOptions = {
290
- /**
291
- * Retain the original object's ID.
292
- * @default false
293
- */
294
- retainId?: boolean;
295
- };
697
+ export const sortByLabel: Comparator = internal.sortByLabel as Comparator;
698
+ export const sortByTypename: Comparator = internal.sortByTypename as Comparator;
699
+ export const sort = (...comparators: Comparator[]): Comparator => internal.sort(...comparators) as Comparator;
296
700
 
297
- /**
298
- * Clones an object or relation.
299
- * This does not clone referenced objects, only the properties in the object.
300
- * @returns A new object with the same schema and properties.
301
- */
302
- export const clone = <T extends Any | Relation.Any>(obj: T, opts?: CloneOptions): T => {
303
- const { id, ...data } = obj;
304
- const schema = getSchema(obj);
305
- invariant(schema != null, 'Object should have a schema');
306
- const props: any = deepMapValues(data, (value, recurse) => {
307
- if (EchoSchema.Ref.isRef(value)) {
308
- return value;
309
- }
310
- return recurse(value);
311
- });
312
- if (opts?.retainId) {
313
- props.id = id;
314
- }
315
- const meta = getMeta(obj);
316
- props[EchoSchema.MetaId] = deepMapValues(meta, (value, recurse) => {
317
- if (EchoSchema.Ref.isRef(value)) {
318
- return value;
319
- }
320
- return recurse(value);
321
- });
322
- return make(schema, props);
323
- };
701
+ //
702
+ // Version
703
+ //
324
704
 
325
- export const VersionTypeId = EchoSchema.VersionTypeId;
326
- export type VersionType = typeof VersionTypeId;
705
+ export const VersionTypeId = internal.VersionTypeId;
706
+ export type VersionCompareResult = internal.VersionCompareResult;
327
707
 
328
708
  /**
329
709
  * Represent object version.
330
710
  * May be backed by Automerge.
331
711
  * Objects with no history are not versioned.
332
712
  */
333
- export interface Version {
334
- [VersionTypeId]: {};
335
-
336
- /**
337
- * Whether the object is versioned.
338
- */
339
- versioned: boolean;
340
-
341
- /**
342
- * Automerge heads.
343
- */
344
- automergeHeads?: string[];
345
- }
713
+ export type Version = internal.EntityVersion;
346
714
 
347
- const unversioned: Version = {
348
- [VersionTypeId]: {},
349
- versioned: false,
350
- };
351
-
352
- /**
353
- * Checks that `obj` is a version object.
354
- */
355
- export const isVersion = (obj: unknown): obj is Version => {
356
- return obj != null && typeof obj === 'object' && VersionTypeId in obj;
357
- };
715
+ export const isVersion = internal.isVersion;
716
+ export const versionValid = internal.versionValid;
717
+ export const compareVersions = internal.compareVersions;
718
+ export const encodeVersion = internal.encodeVersion;
719
+ export const decodeVersion = internal.decodeVersion;
358
720
 
359
721
  /**
360
722
  * Returns the version of the object.
723
+ * Accepts both reactive objects and snapshots.
361
724
  */
362
- export const version = (obj: Any | Relation.Any): Version => {
363
- const version = (obj as any)[EchoSchema.ObjectVersionId];
364
- if (version === undefined) {
365
- return unversioned;
366
- }
367
- return version;
368
- };
369
-
370
- /**
371
- * Checks that `version` is a valid version object.
372
- */
373
- export const versionValid = (version: Version): boolean => {
374
- assertArgument(isVersion(version), 'version', 'Invalid version object');
375
- return !!version.versioned;
376
- };
377
-
378
- export type VersionCompareResult = 'unversioned' | 'equal' | 'different';
379
-
380
- /**
381
- * Compares two versions.
382
- * @param version1
383
- * @param version2
384
- * @returns 'unversioned' if either object is unversioned, 'equal' if the versions are equal, 'different' if the versions are different.
385
- */
386
- export const compareVersions = (version1: Version, version2: Version): VersionCompareResult => {
387
- assertArgument(isVersion(version1), 'version1', 'Invalid version object');
388
- assertArgument(isVersion(version2), 'version2', 'Invalid version object');
389
-
390
- if (!versionValid(version1) || !versionValid(version2)) {
391
- return 'unversioned';
392
- }
393
-
394
- if (version1.automergeHeads?.length !== version2.automergeHeads?.length) {
395
- return 'different';
396
- }
397
- if (version1.automergeHeads?.some((head) => !version2.automergeHeads?.includes(head))) {
398
- return 'different';
399
- }
400
-
401
- return 'equal';
402
- };
403
-
404
- export const encodeVersion = (version: Version): string => {
405
- return JSON.stringify(version);
406
- };
407
-
408
- export const decodeVersion = (version: string): Version => {
409
- const parsed = JSON.parse(version);
410
- parsed[VersionTypeId] = {};
411
- return parsed;
412
- };
725
+ export const version = (entity: Unknown | Snapshot): Version => internal.version(entity);