@dxos/echo 0.8.4-main.c4373fc → 0.8.4-main.c85a9c8dae

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