@dxos/echo 0.8.4-main.406dc2a → 0.8.4-main.52d7546f51

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