@dxos/echo 0.8.4-main.a4bbb77 → 0.8.4-main.abd8ff62ef

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