@sidekickdb/core 0.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (583) hide show
  1. package/.tshy/build.json +8 -0
  2. package/.tshy/commonjs.json +16 -0
  3. package/.tshy/esm.json +15 -0
  4. package/LICENSE.md +202 -0
  5. package/NOTICE +27 -0
  6. package/README.md +25 -2
  7. package/__example__/standalone.esm.html +23 -0
  8. package/__example__/standalone.umd.html +21 -0
  9. package/__tests__/src/Reactor.test.ts +480 -0
  10. package/__tests__/src/auth-extra-fields.e2e.test.ts +160 -0
  11. package/__tests__/src/cookieSync.e2e.test.ts +165 -0
  12. package/__tests__/src/data/movies/attrs.json +145 -0
  13. package/__tests__/src/data/movies/triples.json +1507 -0
  14. package/__tests__/src/data/zeneca/attrs.json +251 -0
  15. package/__tests__/src/data/zeneca/triples.json +15854 -0
  16. package/__tests__/src/dataForQueryMemo.test.ts +136 -0
  17. package/__tests__/src/datalog.test.ts +175 -0
  18. package/__tests__/src/devtool.test.ts +48 -0
  19. package/__tests__/src/endpoints.test.ts +228 -0
  20. package/__tests__/src/infiniteQuery.e2e.test.ts +454 -0
  21. package/__tests__/src/instaml.test.ts +1593 -0
  22. package/__tests__/src/instaql.bench.ts +34 -0
  23. package/__tests__/src/instaql.test.ts +1388 -0
  24. package/__tests__/src/instaqlInference.test.ts +243 -0
  25. package/__tests__/src/notifyScope.test.ts +1145 -0
  26. package/__tests__/src/queryValidation.test.ts +1033 -0
  27. package/__tests__/src/schema.test.ts +641 -0
  28. package/__tests__/src/serializeSchema.test.ts +123 -0
  29. package/__tests__/src/simple.e2e.test.ts +10 -0
  30. package/__tests__/src/storageUpload.test.ts +70 -0
  31. package/__tests__/src/store.test.ts +718 -0
  32. package/__tests__/src/transactionValidation.test.ts +450 -0
  33. package/__tests__/src/typeUtils.ts +39 -0
  34. package/__tests__/src/utils/PersistedObject.test.ts +529 -0
  35. package/__tests__/src/utils/dates.test.ts +82 -0
  36. package/__tests__/src/utils/e2e.ts +95 -0
  37. package/__tests__/src/utils/msw.ts +3 -0
  38. package/__tests__/src/utils/object.test.ts +78 -0
  39. package/__tests__/src/utils/weakHash.test.ts +68 -0
  40. package/__tests__/src/utils/weakHashLegacy.test.ts +22 -0
  41. package/__tests__/src/vite.env.d.ts +1 -0
  42. package/dist/commonjs/Connection.d.ts +93 -0
  43. package/dist/commonjs/Connection.d.ts.map +1 -0
  44. package/dist/commonjs/Connection.js +194 -0
  45. package/dist/commonjs/Connection.js.map +1 -0
  46. package/dist/commonjs/InMemoryStorage.d.ts +11 -0
  47. package/dist/commonjs/InMemoryStorage.d.ts.map +1 -0
  48. package/dist/commonjs/InMemoryStorage.js +29 -0
  49. package/dist/commonjs/InMemoryStorage.js.map +1 -0
  50. package/dist/commonjs/IndexedDBStorage.d.ts +18 -0
  51. package/dist/commonjs/IndexedDBStorage.d.ts.map +1 -0
  52. package/dist/commonjs/IndexedDBStorage.js +341 -0
  53. package/dist/commonjs/IndexedDBStorage.js.map +1 -0
  54. package/dist/commonjs/InstantError.d.ts +7 -0
  55. package/dist/commonjs/InstantError.d.ts.map +1 -0
  56. package/dist/commonjs/InstantError.js +28 -0
  57. package/dist/commonjs/InstantError.js.map +1 -0
  58. package/dist/commonjs/Reactor.d.ts +445 -0
  59. package/dist/commonjs/Reactor.d.ts.map +1 -0
  60. package/dist/commonjs/Reactor.js +2471 -0
  61. package/dist/commonjs/Reactor.js.map +1 -0
  62. package/dist/commonjs/StorageAPI.d.ts +40 -0
  63. package/dist/commonjs/StorageAPI.d.ts.map +1 -0
  64. package/dist/commonjs/StorageAPI.js +82 -0
  65. package/dist/commonjs/StorageAPI.js.map +1 -0
  66. package/dist/commonjs/Stream.d.ts +121 -0
  67. package/dist/commonjs/Stream.d.ts.map +1 -0
  68. package/dist/commonjs/Stream.js +819 -0
  69. package/dist/commonjs/Stream.js.map +1 -0
  70. package/dist/commonjs/SyncTable.d.ts +145 -0
  71. package/dist/commonjs/SyncTable.d.ts.map +1 -0
  72. package/dist/commonjs/SyncTable.js +564 -0
  73. package/dist/commonjs/SyncTable.js.map +1 -0
  74. package/dist/commonjs/WindowNetworkListener.d.ts +5 -0
  75. package/dist/commonjs/WindowNetworkListener.d.ts.map +1 -0
  76. package/dist/commonjs/WindowNetworkListener.js +23 -0
  77. package/dist/commonjs/WindowNetworkListener.js.map +1 -0
  78. package/dist/commonjs/__types__/fieldsTypeTest.d.ts +2 -0
  79. package/dist/commonjs/__types__/fieldsTypeTest.d.ts.map +1 -0
  80. package/dist/commonjs/__types__/fieldsTypeTest.js +68 -0
  81. package/dist/commonjs/__types__/fieldsTypeTest.js.map +1 -0
  82. package/dist/commonjs/__types__/typeUtils.d.ts +20 -0
  83. package/dist/commonjs/__types__/typeUtils.d.ts.map +1 -0
  84. package/dist/commonjs/__types__/typeUtils.js +5 -0
  85. package/dist/commonjs/__types__/typeUtils.js.map +1 -0
  86. package/dist/commonjs/__types__/useDatesTypeTest.d.ts +2 -0
  87. package/dist/commonjs/__types__/useDatesTypeTest.d.ts.map +1 -0
  88. package/dist/commonjs/__types__/useDatesTypeTest.js +55 -0
  89. package/dist/commonjs/__types__/useDatesTypeTest.js.map +1 -0
  90. package/dist/commonjs/attrTypes.d.ts +26 -0
  91. package/dist/commonjs/attrTypes.d.ts.map +1 -0
  92. package/dist/commonjs/attrTypes.js +3 -0
  93. package/dist/commonjs/attrTypes.js.map +1 -0
  94. package/dist/commonjs/authAPI.d.ts +65 -0
  95. package/dist/commonjs/authAPI.d.ts.map +1 -0
  96. package/dist/commonjs/authAPI.js +114 -0
  97. package/dist/commonjs/authAPI.js.map +1 -0
  98. package/dist/commonjs/clientTypes.d.ts +41 -0
  99. package/dist/commonjs/clientTypes.d.ts.map +1 -0
  100. package/dist/commonjs/clientTypes.js +3 -0
  101. package/dist/commonjs/clientTypes.js.map +1 -0
  102. package/dist/commonjs/coreTypes.d.ts +34 -0
  103. package/dist/commonjs/coreTypes.d.ts.map +1 -0
  104. package/dist/commonjs/coreTypes.js +3 -0
  105. package/dist/commonjs/coreTypes.js.map +1 -0
  106. package/dist/commonjs/createRouteHandler.d.ts +8 -0
  107. package/dist/commonjs/createRouteHandler.d.ts.map +1 -0
  108. package/dist/commonjs/createRouteHandler.js +56 -0
  109. package/dist/commonjs/createRouteHandler.js.map +1 -0
  110. package/dist/commonjs/datalog.d.ts +8 -0
  111. package/dist/commonjs/datalog.d.ts.map +1 -0
  112. package/dist/commonjs/datalog.js +106 -0
  113. package/dist/commonjs/datalog.js.map +1 -0
  114. package/dist/commonjs/devtool.d.ts +4 -0
  115. package/dist/commonjs/devtool.d.ts.map +1 -0
  116. package/dist/commonjs/devtool.js +191 -0
  117. package/dist/commonjs/devtool.js.map +1 -0
  118. package/dist/commonjs/endpoints.d.ts +35 -0
  119. package/dist/commonjs/endpoints.d.ts.map +1 -0
  120. package/dist/commonjs/endpoints.js +80 -0
  121. package/dist/commonjs/endpoints.js.map +1 -0
  122. package/dist/commonjs/framework.d.ts +81 -0
  123. package/dist/commonjs/framework.d.ts.map +1 -0
  124. package/dist/commonjs/framework.js +284 -0
  125. package/dist/commonjs/framework.js.map +1 -0
  126. package/dist/commonjs/helperTypes.d.ts +76 -0
  127. package/dist/commonjs/helperTypes.d.ts.map +1 -0
  128. package/dist/commonjs/helperTypes.js +3 -0
  129. package/dist/commonjs/helperTypes.js.map +1 -0
  130. package/dist/commonjs/index.d.ts +614 -0
  131. package/dist/commonjs/index.d.ts.map +1 -0
  132. package/dist/commonjs/index.js +677 -0
  133. package/dist/commonjs/index.js.map +1 -0
  134. package/dist/commonjs/infiniteQuery.d.ts +26 -0
  135. package/dist/commonjs/infiniteQuery.d.ts.map +1 -0
  136. package/dist/commonjs/infiniteQuery.js +438 -0
  137. package/dist/commonjs/infiniteQuery.js.map +1 -0
  138. package/dist/commonjs/instaml.d.ts +18 -0
  139. package/dist/commonjs/instaml.d.ts.map +1 -0
  140. package/dist/commonjs/instaml.js +584 -0
  141. package/dist/commonjs/instaml.js.map +1 -0
  142. package/dist/commonjs/instaql.d.ts +13 -0
  143. package/dist/commonjs/instaql.d.ts.map +1 -0
  144. package/dist/commonjs/instaql.js +670 -0
  145. package/dist/commonjs/instaql.js.map +1 -0
  146. package/dist/commonjs/instatx.d.ts +128 -0
  147. package/dist/commonjs/instatx.d.ts.map +1 -0
  148. package/dist/commonjs/instatx.js +102 -0
  149. package/dist/commonjs/instatx.js.map +1 -0
  150. package/dist/commonjs/model/instaqlResult.d.ts +8 -0
  151. package/dist/commonjs/model/instaqlResult.d.ts.map +1 -0
  152. package/dist/commonjs/model/instaqlResult.js +28 -0
  153. package/dist/commonjs/model/instaqlResult.js.map +1 -0
  154. package/dist/commonjs/notifyScope.d.ts +32 -0
  155. package/dist/commonjs/notifyScope.d.ts.map +1 -0
  156. package/dist/commonjs/notifyScope.js +306 -0
  157. package/dist/commonjs/notifyScope.js.map +1 -0
  158. package/dist/commonjs/package.json +3 -0
  159. package/dist/commonjs/parseSchemaFromJSON.d.ts +3 -0
  160. package/dist/commonjs/parseSchemaFromJSON.d.ts.map +1 -0
  161. package/dist/commonjs/parseSchemaFromJSON.js +147 -0
  162. package/dist/commonjs/parseSchemaFromJSON.js.map +1 -0
  163. package/dist/commonjs/presence.d.ts +51 -0
  164. package/dist/commonjs/presence.d.ts.map +1 -0
  165. package/dist/commonjs/presence.js +55 -0
  166. package/dist/commonjs/presence.js.map +1 -0
  167. package/dist/commonjs/presenceTypes.d.ts +10 -0
  168. package/dist/commonjs/presenceTypes.d.ts.map +1 -0
  169. package/dist/commonjs/presenceTypes.js +5 -0
  170. package/dist/commonjs/presenceTypes.js.map +1 -0
  171. package/dist/commonjs/queryTypes.d.ts +272 -0
  172. package/dist/commonjs/queryTypes.d.ts.map +1 -0
  173. package/dist/commonjs/queryTypes.js +5 -0
  174. package/dist/commonjs/queryTypes.js.map +1 -0
  175. package/dist/commonjs/queryValidation.d.ts +6 -0
  176. package/dist/commonjs/queryValidation.d.ts.map +1 -0
  177. package/dist/commonjs/queryValidation.js +293 -0
  178. package/dist/commonjs/queryValidation.js.map +1 -0
  179. package/dist/commonjs/reactorTypes.d.ts +30 -0
  180. package/dist/commonjs/reactorTypes.d.ts.map +1 -0
  181. package/dist/commonjs/reactorTypes.js +3 -0
  182. package/dist/commonjs/reactorTypes.js.map +1 -0
  183. package/dist/commonjs/routeHandlerProtocol.d.ts +20 -0
  184. package/dist/commonjs/routeHandlerProtocol.d.ts.map +1 -0
  185. package/dist/commonjs/routeHandlerProtocol.js +3 -0
  186. package/dist/commonjs/routeHandlerProtocol.js.map +1 -0
  187. package/dist/commonjs/rulesTypes.d.ts +86 -0
  188. package/dist/commonjs/rulesTypes.d.ts.map +1 -0
  189. package/dist/commonjs/rulesTypes.js +3 -0
  190. package/dist/commonjs/rulesTypes.js.map +1 -0
  191. package/dist/commonjs/sanityCheckQueries.d.ts +19 -0
  192. package/dist/commonjs/sanityCheckQueries.d.ts.map +1 -0
  193. package/dist/commonjs/sanityCheckQueries.js +156 -0
  194. package/dist/commonjs/sanityCheckQueries.js.map +1 -0
  195. package/dist/commonjs/schema.d.ts +73 -0
  196. package/dist/commonjs/schema.d.ts.map +1 -0
  197. package/dist/commonjs/schema.js +132 -0
  198. package/dist/commonjs/schema.js.map +1 -0
  199. package/dist/commonjs/schemaTypes.d.ts +245 -0
  200. package/dist/commonjs/schemaTypes.d.ts.map +1 -0
  201. package/dist/commonjs/schemaTypes.js +116 -0
  202. package/dist/commonjs/schemaTypes.js.map +1 -0
  203. package/dist/commonjs/store.d.ts +90 -0
  204. package/dist/commonjs/store.d.ts.map +1 -0
  205. package/dist/commonjs/store.js +745 -0
  206. package/dist/commonjs/store.js.map +1 -0
  207. package/dist/commonjs/transactionValidation.d.ts +8 -0
  208. package/dist/commonjs/transactionValidation.d.ts.map +1 -0
  209. package/dist/commonjs/transactionValidation.js +137 -0
  210. package/dist/commonjs/transactionValidation.js.map +1 -0
  211. package/dist/commonjs/utils/Deferred.d.ts +9 -0
  212. package/dist/commonjs/utils/Deferred.d.ts.map +1 -0
  213. package/dist/commonjs/utils/Deferred.js +22 -0
  214. package/dist/commonjs/utils/Deferred.js.map +1 -0
  215. package/dist/commonjs/utils/PersistedObject.d.ts +81 -0
  216. package/dist/commonjs/utils/PersistedObject.d.ts.map +1 -0
  217. package/dist/commonjs/utils/PersistedObject.js +469 -0
  218. package/dist/commonjs/utils/PersistedObject.js.map +1 -0
  219. package/dist/commonjs/utils/dates.d.ts +2 -0
  220. package/dist/commonjs/utils/dates.d.ts.map +1 -0
  221. package/dist/commonjs/utils/dates.js +215 -0
  222. package/dist/commonjs/utils/dates.js.map +1 -0
  223. package/dist/commonjs/utils/error.d.ts +3 -0
  224. package/dist/commonjs/utils/error.d.ts.map +1 -0
  225. package/dist/commonjs/utils/error.js +13 -0
  226. package/dist/commonjs/utils/error.js.map +1 -0
  227. package/dist/commonjs/utils/fetch.d.ts +126 -0
  228. package/dist/commonjs/utils/fetch.d.ts.map +1 -0
  229. package/dist/commonjs/utils/fetch.js +37 -0
  230. package/dist/commonjs/utils/fetch.js.map +1 -0
  231. package/dist/commonjs/utils/flags.d.ts +5 -0
  232. package/dist/commonjs/utils/flags.d.ts.map +1 -0
  233. package/dist/commonjs/utils/flags.js +16 -0
  234. package/dist/commonjs/utils/flags.js.map +1 -0
  235. package/dist/commonjs/utils/id.d.ts +4 -0
  236. package/dist/commonjs/utils/id.d.ts.map +1 -0
  237. package/dist/commonjs/utils/id.js +29 -0
  238. package/dist/commonjs/utils/id.js.map +1 -0
  239. package/dist/commonjs/utils/linkIndex.d.ts +8 -0
  240. package/dist/commonjs/utils/linkIndex.d.ts.map +1 -0
  241. package/dist/commonjs/utils/linkIndex.js +21 -0
  242. package/dist/commonjs/utils/linkIndex.js.map +1 -0
  243. package/dist/commonjs/utils/log.d.ts +7 -0
  244. package/dist/commonjs/utils/log.d.ts.map +1 -0
  245. package/dist/commonjs/utils/log.js +17 -0
  246. package/dist/commonjs/utils/log.js.map +1 -0
  247. package/dist/commonjs/utils/object.d.ts +17 -0
  248. package/dist/commonjs/utils/object.d.ts.map +1 -0
  249. package/dist/commonjs/utils/object.js +157 -0
  250. package/dist/commonjs/utils/object.js.map +1 -0
  251. package/dist/commonjs/utils/pgtime.d.ts +198 -0
  252. package/dist/commonjs/utils/pgtime.d.ts.map +1 -0
  253. package/dist/commonjs/utils/pgtime.js +201 -0
  254. package/dist/commonjs/utils/pgtime.js.map +1 -0
  255. package/dist/commonjs/utils/pick.d.ts +2 -0
  256. package/dist/commonjs/utils/pick.d.ts.map +1 -0
  257. package/dist/commonjs/utils/pick.js +13 -0
  258. package/dist/commonjs/utils/pick.js.map +1 -0
  259. package/dist/commonjs/utils/strings.d.ts +2 -0
  260. package/dist/commonjs/utils/strings.d.ts.map +1 -0
  261. package/dist/commonjs/utils/strings.js +19 -0
  262. package/dist/commonjs/utils/strings.js.map +1 -0
  263. package/dist/commonjs/utils/weakHash.d.ts +16 -0
  264. package/dist/commonjs/utils/weakHash.d.ts.map +1 -0
  265. package/dist/commonjs/utils/weakHash.js +75 -0
  266. package/dist/commonjs/utils/weakHash.js.map +1 -0
  267. package/dist/commonjs/utils/weakHashLegacy.d.ts +8 -0
  268. package/dist/commonjs/utils/weakHashLegacy.d.ts.map +1 -0
  269. package/dist/commonjs/utils/weakHashLegacy.js +76 -0
  270. package/dist/commonjs/utils/weakHashLegacy.js.map +1 -0
  271. package/dist/commonjs/version.d.ts +3 -0
  272. package/dist/commonjs/version.d.ts.map +1 -0
  273. package/dist/commonjs/version.js +5 -0
  274. package/dist/commonjs/version.js.map +1 -0
  275. package/dist/commonjs/warningToggle.d.ts +3 -0
  276. package/dist/commonjs/warningToggle.d.ts.map +1 -0
  277. package/dist/commonjs/warningToggle.js +9 -0
  278. package/dist/commonjs/warningToggle.js.map +1 -0
  279. package/dist/esm/Connection.d.ts +93 -0
  280. package/dist/esm/Connection.d.ts.map +1 -0
  281. package/dist/esm/Connection.js +189 -0
  282. package/dist/esm/Connection.js.map +1 -0
  283. package/dist/esm/InMemoryStorage.d.ts +11 -0
  284. package/dist/esm/InMemoryStorage.d.ts.map +1 -0
  285. package/dist/esm/InMemoryStorage.js +26 -0
  286. package/dist/esm/InMemoryStorage.js.map +1 -0
  287. package/dist/esm/IndexedDBStorage.d.ts +18 -0
  288. package/dist/esm/IndexedDBStorage.d.ts.map +1 -0
  289. package/dist/esm/IndexedDBStorage.js +338 -0
  290. package/dist/esm/IndexedDBStorage.js.map +1 -0
  291. package/dist/esm/InstantError.d.ts +7 -0
  292. package/dist/esm/InstantError.d.ts.map +1 -0
  293. package/dist/esm/InstantError.js +24 -0
  294. package/dist/esm/InstantError.js.map +1 -0
  295. package/dist/esm/Reactor.d.ts +445 -0
  296. package/dist/esm/Reactor.d.ts.map +1 -0
  297. package/dist/esm/Reactor.js +2431 -0
  298. package/dist/esm/Reactor.js.map +1 -0
  299. package/dist/esm/StorageAPI.d.ts +40 -0
  300. package/dist/esm/StorageAPI.d.ts.map +1 -0
  301. package/dist/esm/StorageAPI.js +75 -0
  302. package/dist/esm/StorageAPI.js.map +1 -0
  303. package/dist/esm/Stream.d.ts +121 -0
  304. package/dist/esm/Stream.d.ts.map +1 -0
  305. package/dist/esm/Stream.js +812 -0
  306. package/dist/esm/Stream.js.map +1 -0
  307. package/dist/esm/SyncTable.d.ts +145 -0
  308. package/dist/esm/SyncTable.d.ts.map +1 -0
  309. package/dist/esm/SyncTable.js +524 -0
  310. package/dist/esm/SyncTable.js.map +1 -0
  311. package/dist/esm/WindowNetworkListener.d.ts +5 -0
  312. package/dist/esm/WindowNetworkListener.d.ts.map +1 -0
  313. package/dist/esm/WindowNetworkListener.js +20 -0
  314. package/dist/esm/WindowNetworkListener.js.map +1 -0
  315. package/dist/esm/__types__/fieldsTypeTest.d.ts +2 -0
  316. package/dist/esm/__types__/fieldsTypeTest.d.ts.map +1 -0
  317. package/dist/esm/__types__/fieldsTypeTest.js +66 -0
  318. package/dist/esm/__types__/fieldsTypeTest.js.map +1 -0
  319. package/dist/esm/__types__/typeUtils.d.ts +20 -0
  320. package/dist/esm/__types__/typeUtils.d.ts.map +1 -0
  321. package/dist/esm/__types__/typeUtils.js +4 -0
  322. package/dist/esm/__types__/typeUtils.js.map +1 -0
  323. package/dist/esm/__types__/useDatesTypeTest.d.ts +2 -0
  324. package/dist/esm/__types__/useDatesTypeTest.d.ts.map +1 -0
  325. package/dist/esm/__types__/useDatesTypeTest.js +53 -0
  326. package/dist/esm/__types__/useDatesTypeTest.js.map +1 -0
  327. package/dist/esm/attrTypes.d.ts +26 -0
  328. package/dist/esm/attrTypes.d.ts.map +1 -0
  329. package/dist/esm/attrTypes.js +2 -0
  330. package/dist/esm/attrTypes.js.map +1 -0
  331. package/dist/esm/authAPI.d.ts +65 -0
  332. package/dist/esm/authAPI.d.ts.map +1 -0
  333. package/dist/esm/authAPI.js +104 -0
  334. package/dist/esm/authAPI.js.map +1 -0
  335. package/dist/esm/clientTypes.d.ts +41 -0
  336. package/dist/esm/clientTypes.d.ts.map +1 -0
  337. package/dist/esm/clientTypes.js +2 -0
  338. package/dist/esm/clientTypes.js.map +1 -0
  339. package/dist/esm/coreTypes.d.ts +34 -0
  340. package/dist/esm/coreTypes.d.ts.map +1 -0
  341. package/dist/esm/coreTypes.js +2 -0
  342. package/dist/esm/coreTypes.js.map +1 -0
  343. package/dist/esm/createRouteHandler.d.ts +8 -0
  344. package/dist/esm/createRouteHandler.d.ts.map +1 -0
  345. package/dist/esm/createRouteHandler.js +52 -0
  346. package/dist/esm/createRouteHandler.js.map +1 -0
  347. package/dist/esm/datalog.d.ts +8 -0
  348. package/dist/esm/datalog.d.ts.map +1 -0
  349. package/dist/esm/datalog.js +100 -0
  350. package/dist/esm/datalog.js.map +1 -0
  351. package/dist/esm/devtool.d.ts +4 -0
  352. package/dist/esm/devtool.d.ts.map +1 -0
  353. package/dist/esm/devtool.js +154 -0
  354. package/dist/esm/devtool.js.map +1 -0
  355. package/dist/esm/endpoints.d.ts +35 -0
  356. package/dist/esm/endpoints.d.ts.map +1 -0
  357. package/dist/esm/endpoints.js +73 -0
  358. package/dist/esm/endpoints.js.map +1 -0
  359. package/dist/esm/framework.d.ts +81 -0
  360. package/dist/esm/framework.d.ts.map +1 -0
  361. package/dist/esm/framework.js +244 -0
  362. package/dist/esm/framework.js.map +1 -0
  363. package/dist/esm/helperTypes.d.ts +76 -0
  364. package/dist/esm/helperTypes.d.ts.map +1 -0
  365. package/dist/esm/helperTypes.js +2 -0
  366. package/dist/esm/helperTypes.js.map +1 -0
  367. package/dist/esm/index.d.ts +614 -0
  368. package/dist/esm/index.d.ts.map +1 -0
  369. package/dist/esm/index.js +655 -0
  370. package/dist/esm/index.js.map +1 -0
  371. package/dist/esm/infiniteQuery.d.ts +26 -0
  372. package/dist/esm/infiniteQuery.d.ts.map +1 -0
  373. package/dist/esm/infiniteQuery.js +433 -0
  374. package/dist/esm/infiniteQuery.js.map +1 -0
  375. package/dist/esm/instaml.d.ts +18 -0
  376. package/dist/esm/instaml.d.ts.map +1 -0
  377. package/dist/esm/instaml.js +577 -0
  378. package/dist/esm/instaml.js.map +1 -0
  379. package/dist/esm/instaql.d.ts +13 -0
  380. package/dist/esm/instaql.d.ts.map +1 -0
  381. package/dist/esm/instaql.js +633 -0
  382. package/dist/esm/instaql.js.map +1 -0
  383. package/dist/esm/instatx.d.ts +128 -0
  384. package/dist/esm/instatx.d.ts.map +1 -0
  385. package/dist/esm/instatx.js +94 -0
  386. package/dist/esm/instatx.js.map +1 -0
  387. package/dist/esm/model/instaqlResult.d.ts +8 -0
  388. package/dist/esm/model/instaqlResult.d.ts.map +1 -0
  389. package/dist/esm/model/instaqlResult.js +25 -0
  390. package/dist/esm/model/instaqlResult.js.map +1 -0
  391. package/dist/esm/notifyScope.d.ts +32 -0
  392. package/dist/esm/notifyScope.d.ts.map +1 -0
  393. package/dist/esm/notifyScope.js +300 -0
  394. package/dist/esm/notifyScope.js.map +1 -0
  395. package/dist/esm/package.json +3 -0
  396. package/dist/esm/parseSchemaFromJSON.d.ts +3 -0
  397. package/dist/esm/parseSchemaFromJSON.d.ts.map +1 -0
  398. package/dist/esm/parseSchemaFromJSON.js +143 -0
  399. package/dist/esm/parseSchemaFromJSON.js.map +1 -0
  400. package/dist/esm/presence.d.ts +51 -0
  401. package/dist/esm/presence.d.ts.map +1 -0
  402. package/dist/esm/presence.js +51 -0
  403. package/dist/esm/presence.js.map +1 -0
  404. package/dist/esm/presenceTypes.d.ts +10 -0
  405. package/dist/esm/presenceTypes.d.ts.map +1 -0
  406. package/dist/esm/presenceTypes.js +4 -0
  407. package/dist/esm/presenceTypes.js.map +1 -0
  408. package/dist/esm/queryTypes.d.ts +272 -0
  409. package/dist/esm/queryTypes.d.ts.map +1 -0
  410. package/dist/esm/queryTypes.js +4 -0
  411. package/dist/esm/queryTypes.js.map +1 -0
  412. package/dist/esm/queryValidation.d.ts +6 -0
  413. package/dist/esm/queryValidation.d.ts.map +1 -0
  414. package/dist/esm/queryValidation.js +288 -0
  415. package/dist/esm/queryValidation.js.map +1 -0
  416. package/dist/esm/reactorTypes.d.ts +30 -0
  417. package/dist/esm/reactorTypes.d.ts.map +1 -0
  418. package/dist/esm/reactorTypes.js +2 -0
  419. package/dist/esm/reactorTypes.js.map +1 -0
  420. package/dist/esm/routeHandlerProtocol.d.ts +20 -0
  421. package/dist/esm/routeHandlerProtocol.d.ts.map +1 -0
  422. package/dist/esm/routeHandlerProtocol.js +2 -0
  423. package/dist/esm/routeHandlerProtocol.js.map +1 -0
  424. package/dist/esm/rulesTypes.d.ts +86 -0
  425. package/dist/esm/rulesTypes.d.ts.map +1 -0
  426. package/dist/esm/rulesTypes.js +2 -0
  427. package/dist/esm/rulesTypes.js.map +1 -0
  428. package/dist/esm/sanityCheckQueries.d.ts +19 -0
  429. package/dist/esm/sanityCheckQueries.d.ts.map +1 -0
  430. package/dist/esm/sanityCheckQueries.js +153 -0
  431. package/dist/esm/sanityCheckQueries.js.map +1 -0
  432. package/dist/esm/schema.d.ts +73 -0
  433. package/dist/esm/schema.d.ts.map +1 -0
  434. package/dist/esm/schema.js +129 -0
  435. package/dist/esm/schema.js.map +1 -0
  436. package/dist/esm/schemaTypes.d.ts +245 -0
  437. package/dist/esm/schemaTypes.d.ts.map +1 -0
  438. package/dist/esm/schemaTypes.js +107 -0
  439. package/dist/esm/schemaTypes.js.map +1 -0
  440. package/dist/esm/store.d.ts +90 -0
  441. package/dist/esm/store.d.ts.map +1 -0
  442. package/dist/esm/store.js +723 -0
  443. package/dist/esm/store.js.map +1 -0
  444. package/dist/esm/transactionValidation.d.ts +8 -0
  445. package/dist/esm/transactionValidation.d.ts.map +1 -0
  446. package/dist/esm/transactionValidation.js +131 -0
  447. package/dist/esm/transactionValidation.js.map +1 -0
  448. package/dist/esm/utils/Deferred.d.ts +9 -0
  449. package/dist/esm/utils/Deferred.d.ts.map +1 -0
  450. package/dist/esm/utils/Deferred.js +18 -0
  451. package/dist/esm/utils/Deferred.js.map +1 -0
  452. package/dist/esm/utils/PersistedObject.d.ts +81 -0
  453. package/dist/esm/utils/PersistedObject.d.ts.map +1 -0
  454. package/dist/esm/utils/PersistedObject.js +464 -0
  455. package/dist/esm/utils/PersistedObject.js.map +1 -0
  456. package/dist/esm/utils/dates.d.ts +2 -0
  457. package/dist/esm/utils/dates.d.ts.map +1 -0
  458. package/dist/esm/utils/dates.js +212 -0
  459. package/dist/esm/utils/dates.js.map +1 -0
  460. package/dist/esm/utils/error.d.ts +3 -0
  461. package/dist/esm/utils/error.d.ts.map +1 -0
  462. package/dist/esm/utils/error.js +9 -0
  463. package/dist/esm/utils/error.js.map +1 -0
  464. package/dist/esm/utils/fetch.d.ts +126 -0
  465. package/dist/esm/utils/fetch.d.ts.map +1 -0
  466. package/dist/esm/utils/fetch.js +32 -0
  467. package/dist/esm/utils/fetch.js.map +1 -0
  468. package/dist/esm/utils/flags.d.ts +5 -0
  469. package/dist/esm/utils/flags.d.ts.map +1 -0
  470. package/dist/esm/utils/flags.js +11 -0
  471. package/dist/esm/utils/flags.js.map +1 -0
  472. package/dist/esm/utils/id.d.ts +4 -0
  473. package/dist/esm/utils/id.d.ts.map +1 -0
  474. package/dist/esm/utils/id.js +26 -0
  475. package/dist/esm/utils/id.js.map +1 -0
  476. package/dist/esm/utils/linkIndex.d.ts +8 -0
  477. package/dist/esm/utils/linkIndex.d.ts.map +1 -0
  478. package/dist/esm/utils/linkIndex.js +18 -0
  479. package/dist/esm/utils/linkIndex.js.map +1 -0
  480. package/dist/esm/utils/log.d.ts +7 -0
  481. package/dist/esm/utils/log.d.ts.map +1 -0
  482. package/dist/esm/utils/log.js +14 -0
  483. package/dist/esm/utils/log.js.map +1 -0
  484. package/dist/esm/utils/object.d.ts +17 -0
  485. package/dist/esm/utils/object.d.ts.map +1 -0
  486. package/dist/esm/utils/object.js +144 -0
  487. package/dist/esm/utils/object.js.map +1 -0
  488. package/dist/esm/utils/pgtime.d.ts +198 -0
  489. package/dist/esm/utils/pgtime.d.ts.map +1 -0
  490. package/dist/esm/utils/pgtime.js +198 -0
  491. package/dist/esm/utils/pgtime.js.map +1 -0
  492. package/dist/esm/utils/pick.d.ts +2 -0
  493. package/dist/esm/utils/pick.d.ts.map +1 -0
  494. package/dist/esm/utils/pick.js +10 -0
  495. package/dist/esm/utils/pick.js.map +1 -0
  496. package/dist/esm/utils/strings.d.ts +2 -0
  497. package/dist/esm/utils/strings.d.ts.map +1 -0
  498. package/dist/esm/utils/strings.js +16 -0
  499. package/dist/esm/utils/strings.js.map +1 -0
  500. package/dist/esm/utils/weakHash.d.ts +16 -0
  501. package/dist/esm/utils/weakHash.d.ts.map +1 -0
  502. package/dist/esm/utils/weakHash.js +72 -0
  503. package/dist/esm/utils/weakHash.js.map +1 -0
  504. package/dist/esm/utils/weakHashLegacy.d.ts +8 -0
  505. package/dist/esm/utils/weakHashLegacy.d.ts.map +1 -0
  506. package/dist/esm/utils/weakHashLegacy.js +73 -0
  507. package/dist/esm/utils/weakHashLegacy.js.map +1 -0
  508. package/dist/esm/version.d.ts +3 -0
  509. package/dist/esm/version.d.ts.map +1 -0
  510. package/dist/esm/version.js +3 -0
  511. package/dist/esm/version.js.map +1 -0
  512. package/dist/esm/warningToggle.d.ts +3 -0
  513. package/dist/esm/warningToggle.d.ts.map +1 -0
  514. package/dist/esm/warningToggle.js +5 -0
  515. package/dist/esm/warningToggle.js.map +1 -0
  516. package/dist/standalone/index.js +8740 -0
  517. package/dist/standalone/index.umd.cjs +7 -0
  518. package/package.json +81 -3
  519. package/src/Connection.ts +277 -0
  520. package/src/InMemoryStorage.ts +34 -0
  521. package/src/IndexedDBStorage.ts +387 -0
  522. package/src/InstantError.ts +28 -0
  523. package/src/Reactor.js +2921 -0
  524. package/src/StorageAPI.ts +156 -0
  525. package/src/Stream.ts +1164 -0
  526. package/src/SyncTable.ts +919 -0
  527. package/src/WindowNetworkListener.js +19 -0
  528. package/src/__types__/fieldsTypeTest.ts +113 -0
  529. package/src/__types__/typeUtils.ts +39 -0
  530. package/src/__types__/useDatesTypeTest.ts +69 -0
  531. package/src/attrTypes.ts +29 -0
  532. package/src/authAPI.ts +205 -0
  533. package/src/clientTypes.ts +28 -0
  534. package/src/coreTypes.ts +58 -0
  535. package/src/createRouteHandler.ts +66 -0
  536. package/src/datalog.js +116 -0
  537. package/src/devtool.ts +186 -0
  538. package/src/endpoints.ts +101 -0
  539. package/src/framework.ts +395 -0
  540. package/src/helperTypes.ts +115 -0
  541. package/src/index.ts +1283 -0
  542. package/src/infiniteQuery.ts +602 -0
  543. package/src/instaml.ts +714 -0
  544. package/src/instaql.ts +952 -0
  545. package/src/instatx.ts +265 -0
  546. package/src/model/instaqlResult.js +25 -0
  547. package/src/notifyScope.ts +343 -0
  548. package/src/parseSchemaFromJSON.ts +176 -0
  549. package/src/presence.ts +123 -0
  550. package/src/presenceTypes.ts +19 -0
  551. package/src/queryTypes.ts +721 -0
  552. package/src/queryValidation.ts +535 -0
  553. package/src/reactorTypes.ts +33 -0
  554. package/src/routeHandlerProtocol.ts +24 -0
  555. package/src/rulesTypes.ts +122 -0
  556. package/src/sanityCheckQueries.ts +189 -0
  557. package/src/schema.ts +194 -0
  558. package/src/schemaTypes.ts +642 -0
  559. package/src/store.ts +949 -0
  560. package/src/transactionValidation.ts +204 -0
  561. package/src/utils/Deferred.ts +20 -0
  562. package/src/utils/PersistedObject.ts +589 -0
  563. package/src/utils/dates.ts +258 -0
  564. package/src/utils/error.ts +9 -0
  565. package/src/utils/fetch.ts +129 -0
  566. package/src/utils/flags.ts +14 -0
  567. package/src/utils/id.ts +28 -0
  568. package/src/utils/linkIndex.ts +33 -0
  569. package/src/utils/log.ts +23 -0
  570. package/src/utils/object.js +176 -0
  571. package/src/utils/pgtime.ts +199 -0
  572. package/src/utils/pick.js +9 -0
  573. package/src/utils/strings.ts +19 -0
  574. package/src/utils/weakHash.ts +77 -0
  575. package/src/utils/weakHashLegacy.ts +75 -0
  576. package/src/version.ts +3 -0
  577. package/src/warningToggle.ts +5 -0
  578. package/tsconfig.cjs.dev.json +15 -0
  579. package/tsconfig.dev.json +14 -0
  580. package/tsconfig.json +13 -0
  581. package/tsconfig.test.json +5 -0
  582. package/vite.config.ts +19 -0
  583. package/vitest.config.ts +75 -0
@@ -0,0 +1,1593 @@
1
+ import { test, expect } from 'vitest';
2
+ import * as instaml from '../../src/instaml';
3
+ import * as instatx from '../../src/instatx';
4
+ import zenecaAttrs from './data/zeneca/attrs.json';
5
+ import zenecaTriples from './data/zeneca/triples.json';
6
+ import { createStore, transact, AttrsStoreClass } from '../../src/store';
7
+ import uuid from '../../src/utils/id';
8
+ import { i, InstantDBAttr } from '../../src/index';
9
+
10
+ const zenecaAttrToId = zenecaAttrs.reduce((res, x) => {
11
+ // @ts-expect-error
12
+ res[`${x['forward-identity'][1]}/${x['forward-identity'][2]}`] = x.id;
13
+ return res;
14
+ }, {});
15
+
16
+ const zenecaAttrsStore = new AttrsStoreClass(
17
+ zenecaAttrs.reduce((res, x) => {
18
+ // @ts-expect-error
19
+ res[x.id] = x;
20
+ return res;
21
+ }, {}),
22
+ null,
23
+ );
24
+
25
+ const store = createStore(
26
+ zenecaAttrsStore,
27
+ zenecaTriples as [string, string, any, number][],
28
+ );
29
+
30
+ test('simple update transform', () => {
31
+ const testId = uuid();
32
+
33
+ const ops = instatx.tx.books[testId].update({ title: 'New Title' });
34
+ const result = instaml.transform({ attrsStore: zenecaAttrsStore }, ops);
35
+
36
+ const expected = [
37
+ // @ts-expect-error
38
+ ['add-triple', testId, zenecaAttrToId['books/title'], 'New Title'],
39
+ // @ts-expect-error
40
+ ['add-triple', testId, zenecaAttrToId['books/id'], testId],
41
+ ];
42
+
43
+ expect(result).toHaveLength(expected.length);
44
+ for (const item of expected) {
45
+ expect(result).toContainEqual(item);
46
+ }
47
+ });
48
+
49
+ test('undefined is ignored in update', () => {
50
+ const testId = uuid();
51
+
52
+ const ops = instatx.tx.users[testId].update({
53
+ handle: 'bobby',
54
+ fullName: undefined,
55
+ });
56
+ const result = instaml.transform({ attrsStore: zenecaAttrsStore }, ops);
57
+
58
+ const expected = [
59
+ // @ts-expect-error
60
+ ['add-triple', testId, zenecaAttrToId['users/id'], testId],
61
+ // @ts-expect-error
62
+ ['add-triple', testId, zenecaAttrToId['users/handle'], 'bobby'],
63
+ ];
64
+
65
+ expect(result).toHaveLength(expected.length);
66
+ for (const item of expected) {
67
+ expect(result).toContainEqual(item);
68
+ }
69
+ });
70
+
71
+ test('ignores id attrs', () => {
72
+ const testId = uuid();
73
+
74
+ const ops = instatx.tx.books[testId].update({
75
+ title: 'New Title',
76
+ id: 'ploop',
77
+ });
78
+ const result = instaml.transform({ attrsStore: zenecaAttrsStore }, ops);
79
+
80
+ const expected = [
81
+ // @ts-expect-error
82
+ ['add-triple', testId, zenecaAttrToId['books/title'], 'New Title'],
83
+ // @ts-expect-error
84
+ ['add-triple', testId, zenecaAttrToId['books/id'], testId],
85
+ ];
86
+ expect(result).toHaveLength(expected.length);
87
+ for (const item of expected) {
88
+ expect(result).toContainEqual(item);
89
+ }
90
+ });
91
+
92
+ test("optimistically adds attrs if they don't exist", () => {
93
+ const testId = uuid();
94
+
95
+ const ops = instatx.tx.books[testId].update({ newAttr: 'New Title' });
96
+
97
+ const result = instaml.transform({ attrsStore: zenecaAttrsStore }, ops);
98
+
99
+ const expected = [
100
+ [
101
+ 'add-attr',
102
+ {
103
+ cardinality: 'one',
104
+ 'forward-identity': [expect.any(String), 'books', 'newAttr'],
105
+ id: expect.any(String),
106
+ 'index?': false,
107
+ isUnsynced: true,
108
+ 'unique?': false,
109
+ 'value-type': 'blob',
110
+ },
111
+ ],
112
+ ['add-triple', testId, expect.any(String), 'New Title'],
113
+ // @ts-expect-error
114
+ ['add-triple', testId, zenecaAttrToId['books/id'], testId],
115
+ ];
116
+
117
+ expect(result).toHaveLength(expected.length);
118
+ for (const item of expected) {
119
+ expect(result).toContainEqual(item);
120
+ }
121
+ });
122
+
123
+ test('lookup resolves attr ids', () => {
124
+ const ops = instatx.tx.users[
125
+ instatx.lookup('email', 'stopa@instantdb.com')
126
+ ].update({
127
+ handle: 'stopa',
128
+ });
129
+
130
+ // @ts-expect-error
131
+ const stopaLookup = [zenecaAttrToId['users/email'], 'stopa@instantdb.com'];
132
+
133
+ const result = instaml.transform({ attrsStore: zenecaAttrsStore }, ops);
134
+
135
+ const expected = [
136
+ // @ts-expect-error
137
+ ['add-triple', stopaLookup, zenecaAttrToId['users/handle'], 'stopa'],
138
+ // @ts-expect-error
139
+ ['add-triple', stopaLookup, zenecaAttrToId['users/id'], stopaLookup],
140
+ ];
141
+
142
+ expect(result).toHaveLength(expected.length);
143
+ for (const item of expected) {
144
+ expect(result).toContainEqual(item);
145
+ }
146
+ });
147
+
148
+ test('lookup creates unique attrs for custom lookups', () => {
149
+ const ops = instatx.tx.users[
150
+ instatx.lookup('newAttr', 'newAttrValue')
151
+ ].update({
152
+ handle: 'stopa',
153
+ });
154
+
155
+ const lookup = [
156
+ // The attr is going to be created, so we don't know its value yet
157
+ expect.any(String),
158
+ 'newAttrValue',
159
+ ];
160
+
161
+ const result = instaml.transform({ attrsStore: zenecaAttrsStore }, ops);
162
+ const expected = [
163
+ [
164
+ 'add-attr',
165
+ {
166
+ cardinality: 'one',
167
+ 'forward-identity': [expect.any(String), 'users', 'newAttr'],
168
+ id: expect.any(String),
169
+ 'index?': true,
170
+ isUnsynced: true,
171
+ 'unique?': true,
172
+ 'value-type': 'blob',
173
+ },
174
+ ],
175
+ // @ts-expect-error
176
+ ['add-triple', lookup, zenecaAttrToId['users/handle'], 'stopa'],
177
+ // @ts-expect-error
178
+
179
+ ['add-triple', lookup, zenecaAttrToId['users/id'], lookup],
180
+ ];
181
+
182
+ expect(result).toHaveLength(expected.length);
183
+ for (const item of expected) {
184
+ expect(result).toContainEqual(item);
185
+ }
186
+ });
187
+
188
+ test('lookup creates unique attrs for lookups in link values', () => {
189
+ const uid = uuid();
190
+ const ops = instatx.tx.users[uid]
191
+ .update({})
192
+ .link({ posts: instatx.lookup('slug', 'life-is-good') });
193
+
194
+ const result = instaml.transform(
195
+ { attrsStore: new AttrsStoreClass({}, null) },
196
+ ops,
197
+ );
198
+
199
+ expect(result).toEqual([
200
+ [
201
+ 'add-attr',
202
+ {
203
+ id: expect.any(String),
204
+ 'forward-identity': [expect.any(String), 'users', 'posts'],
205
+ 'reverse-identity': [expect.any(String), 'posts', 'users'],
206
+ 'value-type': 'ref',
207
+ cardinality: 'one',
208
+ 'unique?': true,
209
+ 'index?': true,
210
+ isUnsynced: true,
211
+ },
212
+ ],
213
+ [
214
+ 'add-attr',
215
+ {
216
+ id: expect.any(String),
217
+ 'forward-identity': [expect.any(String), 'posts', 'slug'],
218
+ 'value-type': 'blob',
219
+ cardinality: 'one',
220
+ 'unique?': true,
221
+ 'index?': true,
222
+ isUnsynced: true,
223
+ },
224
+ ],
225
+ [
226
+ 'add-attr',
227
+ {
228
+ id: expect.any(String),
229
+ 'forward-identity': [expect.any(String), 'users', 'id'],
230
+ 'value-type': 'blob',
231
+ cardinality: 'one',
232
+ 'unique?': true,
233
+ 'index?': false,
234
+ isUnsynced: true,
235
+ },
236
+ ],
237
+ ['add-triple', uid, expect.any(String), uid],
238
+ [
239
+ 'add-triple',
240
+ uid,
241
+ expect.any(String),
242
+ [expect.any(String), 'life-is-good'],
243
+ ],
244
+ ]);
245
+ });
246
+
247
+ test('lookup creates unique attrs for lookups in link values with arrays', () => {
248
+ const uid = uuid();
249
+ const ops = instatx.tx.users[uid].update({}).link({
250
+ posts: [
251
+ instatx.lookup('slug', 'life-is-good'),
252
+ instatx.lookup('slug', 'check-this-out'),
253
+ ],
254
+ });
255
+
256
+ const result = instaml.transform(
257
+ { attrsStore: new AttrsStoreClass({}, null) },
258
+ ops,
259
+ );
260
+
261
+ const expected = [
262
+ [
263
+ 'add-attr',
264
+ {
265
+ id: expect.any(String),
266
+ 'forward-identity': [expect.any(String), 'users', 'posts'],
267
+ 'reverse-identity': [expect.any(String), 'posts', 'users'],
268
+ 'value-type': 'ref',
269
+ cardinality: 'one',
270
+ 'unique?': true,
271
+ 'index?': true,
272
+ isUnsynced: true,
273
+ },
274
+ ],
275
+ [
276
+ 'add-attr',
277
+ {
278
+ id: expect.any(String),
279
+ 'forward-identity': [expect.any(String), 'posts', 'slug'],
280
+ 'value-type': 'blob',
281
+ cardinality: 'one',
282
+ 'unique?': true,
283
+ 'index?': true,
284
+ isUnsynced: true,
285
+ },
286
+ ],
287
+ [
288
+ 'add-attr',
289
+ {
290
+ id: expect.any(String),
291
+ 'forward-identity': [expect.any(String), 'users', 'id'],
292
+ 'value-type': 'blob',
293
+ cardinality: 'one',
294
+ 'unique?': true,
295
+ 'index?': false,
296
+ isUnsynced: true,
297
+ },
298
+ ],
299
+ ['add-triple', uid, expect.any(String), uid],
300
+ [
301
+ 'add-triple',
302
+ uid,
303
+ expect.any(String),
304
+ [expect.any(String), 'life-is-good'],
305
+ ],
306
+ [
307
+ 'add-triple',
308
+ uid,
309
+ expect.any(String),
310
+ [expect.any(String), 'check-this-out'],
311
+ ],
312
+ ];
313
+
314
+ expect(result).toHaveLength(expected.length);
315
+ for (const item of expected) {
316
+ expect(result).toContainEqual(item);
317
+ }
318
+ });
319
+
320
+ test('lookup creates unique attrs for lookups in link values when fwd-ident exists', () => {
321
+ const uid = uuid();
322
+ const ops = instatx.tx.users[uid]
323
+ .update({})
324
+ .link({ posts: instatx.lookup('slug', 'life-is-good') });
325
+
326
+ const attrId = uuid();
327
+ const existingRefAttr = {
328
+ id: attrId,
329
+ 'forward-identity': [uuid(), 'users', 'posts'],
330
+ 'reverse-identity': [uuid(), 'posts', 'users'],
331
+ 'value-type': 'ref',
332
+ cardinality: 'one',
333
+ 'unique?': true,
334
+ 'index?': true,
335
+ } as unknown as InstantDBAttr;
336
+
337
+ const result = instaml.transform(
338
+ { attrsStore: new AttrsStoreClass({ [attrId]: existingRefAttr }, null) },
339
+ ops,
340
+ );
341
+
342
+ expect(result).toEqual([
343
+ [
344
+ 'add-attr',
345
+ {
346
+ id: expect.any(String),
347
+ 'forward-identity': [expect.any(String), 'posts', 'slug'],
348
+ 'value-type': 'blob',
349
+ cardinality: 'one',
350
+ 'unique?': true,
351
+ 'index?': true,
352
+ isUnsynced: true,
353
+ },
354
+ ],
355
+ [
356
+ 'add-attr',
357
+ {
358
+ id: expect.any(String),
359
+ 'forward-identity': [expect.any(String), 'users', 'id'],
360
+ 'value-type': 'blob',
361
+ cardinality: 'one',
362
+ 'unique?': true,
363
+ 'index?': false,
364
+ isUnsynced: true,
365
+ },
366
+ ],
367
+ ['add-triple', uid, expect.any(String), uid],
368
+ [
369
+ 'add-triple',
370
+ uid,
371
+ expect.any(String),
372
+ [expect.any(String), 'life-is-good'],
373
+ ],
374
+ ]);
375
+ });
376
+
377
+ test('lookup creates unique attrs for lookups in link values when rev-ident exists', () => {
378
+ const uid = uuid();
379
+ const ops = instatx.tx.users[uid]
380
+ .update({})
381
+ .link({ posts: instatx.lookup('slug', 'life-is-good') });
382
+
383
+ const attrId = uuid();
384
+ const existingRefAttr = {
385
+ id: attrId,
386
+ 'forward-identity': [uuid(), 'posts', 'users'],
387
+ 'reverse-identity': [uuid(), 'users', 'posts'],
388
+ 'value-type': 'ref',
389
+ cardinality: 'one',
390
+ 'unique?': true,
391
+ 'index?': true,
392
+ } as unknown as InstantDBAttr;
393
+
394
+ const result = instaml.transform(
395
+ { attrsStore: new AttrsStoreClass({ [attrId]: existingRefAttr }, null) },
396
+ ops,
397
+ );
398
+
399
+ expect(result).toEqual([
400
+ [
401
+ 'add-attr',
402
+ {
403
+ id: expect.any(String),
404
+ 'forward-identity': [expect.any(String), 'posts', 'slug'],
405
+ 'value-type': 'blob',
406
+ cardinality: 'one',
407
+ 'unique?': true,
408
+ 'index?': true,
409
+ isUnsynced: true,
410
+ },
411
+ ],
412
+ [
413
+ 'add-attr',
414
+ {
415
+ id: expect.any(String),
416
+ 'forward-identity': [expect.any(String), 'users', 'id'],
417
+ 'value-type': 'blob',
418
+ cardinality: 'one',
419
+ 'unique?': true,
420
+ 'index?': false,
421
+ isUnsynced: true,
422
+ },
423
+ ],
424
+ ['add-triple', uid, expect.any(String), uid],
425
+ [
426
+ 'add-triple',
427
+ [expect.any(String), 'life-is-good'],
428
+ expect.any(String),
429
+ uid,
430
+ ],
431
+ ]);
432
+ });
433
+
434
+ test("lookup doesn't override attrs for lookups in link values", () => {
435
+ const uid = uuid();
436
+ const ops = instatx.tx.users[uid]
437
+ .update({})
438
+ .link({ posts: instatx.lookup('slug', 'life-is-good') });
439
+
440
+ const refAttrId = uuid();
441
+ const userIdAttrId = uuid();
442
+ const postsSlugAttrId = uuid();
443
+
444
+ const attrsStore = new AttrsStoreClass(
445
+ {
446
+ [refAttrId]: {
447
+ id: refAttrId,
448
+ 'forward-identity': [uuid(), 'users', 'posts'],
449
+ 'reverse-identity': [uuid(), 'posts', 'users'],
450
+ 'value-type': 'ref',
451
+ cardinality: 'one',
452
+ 'unique?': true,
453
+ 'index?': true,
454
+ } as unknown as InstantDBAttr,
455
+ [userIdAttrId]: {
456
+ id: userIdAttrId,
457
+ 'forward-identity': [uuid(), 'users', 'id'],
458
+ 'value-type': 'blob',
459
+ cardinality: 'one',
460
+ 'unique?': true,
461
+ 'index?': false,
462
+ } as unknown as InstantDBAttr,
463
+ [postsSlugAttrId]: {
464
+ id: postsSlugAttrId,
465
+ 'forward-identity': [uuid(), 'posts', 'slug'],
466
+ 'value-type': 'blob',
467
+ cardinality: 'one',
468
+ 'unique?': true,
469
+ 'index?': true,
470
+ } as unknown as InstantDBAttr,
471
+ },
472
+ null,
473
+ );
474
+
475
+ const result = instaml.transform({ attrsStore }, ops);
476
+
477
+ expect(result).toEqual([
478
+ ['add-triple', uid, userIdAttrId, uid],
479
+ ['add-triple', uid, refAttrId, [postsSlugAttrId, 'life-is-good']],
480
+ ]);
481
+ });
482
+
483
+ test("lookup doesn't override attrs for lookups in self links", () => {
484
+ const refAttrId = uuid();
485
+ const postIdAttrId = uuid();
486
+ const postsSlugAttrId = uuid();
487
+
488
+ const attrsStore = new AttrsStoreClass(
489
+ {
490
+ [postIdAttrId]: {
491
+ id: postIdAttrId,
492
+ 'forward-identity': [uuid(), 'posts', 'id'],
493
+ 'value-type': 'blob',
494
+ cardinality: 'one',
495
+ 'unique?': true,
496
+ 'index?': false,
497
+ } as unknown as InstantDBAttr,
498
+ [postsSlugAttrId]: {
499
+ id: postsSlugAttrId,
500
+ 'forward-identity': [uuid(), 'posts', 'slug'],
501
+ 'value-type': 'blob',
502
+ cardinality: 'one',
503
+ 'unique?': true,
504
+ 'index?': true,
505
+ } as unknown as InstantDBAttr,
506
+ [refAttrId]: {
507
+ id: refAttrId,
508
+ 'forward-identity': [uuid(), 'posts', 'parent'],
509
+ 'reverse-identity': [uuid(), 'posts', 'child'],
510
+ 'value-type': 'ref',
511
+ cardinality: 'one',
512
+ 'unique?': true,
513
+ 'index?': true,
514
+ } as unknown as InstantDBAttr,
515
+ },
516
+ null,
517
+ );
518
+
519
+ const ops1 = instatx.tx.posts[instatx.lookup('slug', 'life-is-good')]
520
+ .update({})
521
+ .link({ parent: instatx.lookup('slug', 'life-is-good') });
522
+
523
+ const result1 = instaml.transform({ attrsStore }, ops1);
524
+
525
+ expect(result1.filter((x) => x[0] !== 'add-triple')).toEqual([]);
526
+
527
+ const ops2 = instatx.tx.posts[instatx.lookup('slug', 'life-is-good')]
528
+ .update({})
529
+ .link({ child: instatx.lookup('slug', 'life-is-good') });
530
+
531
+ const result2 = instaml.transform({ attrsStore }, ops2);
532
+
533
+ expect(result2.filter((x) => x[0] !== 'add-triple')).toEqual([]);
534
+ });
535
+
536
+ test('lookup creates unique ref attrs for ref lookup', () => {
537
+ const uid = uuid();
538
+ const ops = [
539
+ instatx.tx.users[uid].update({}),
540
+ instatx.tx.user_prefs[instatx.lookup('users.id', uid)].update({}),
541
+ ];
542
+
543
+ const lookup = [
544
+ // The attr is going to be created, so we don't know its value yet
545
+ expect.any(String),
546
+ uid,
547
+ ];
548
+
549
+ const result = instaml.transform(
550
+ { attrsStore: new AttrsStoreClass({}, null) },
551
+ ops,
552
+ );
553
+ const expected = [
554
+ [
555
+ 'add-attr',
556
+ {
557
+ id: expect.any(String),
558
+ 'forward-identity': [expect.any(String), 'users', 'id'],
559
+ 'value-type': 'blob',
560
+ cardinality: 'one',
561
+ 'unique?': true,
562
+ 'index?': false,
563
+ isUnsynced: true,
564
+ },
565
+ ],
566
+ [
567
+ 'add-attr',
568
+ {
569
+ id: expect.any(String),
570
+ 'forward-identity': [expect.any(String), 'user_prefs', 'id'],
571
+ 'value-type': 'blob',
572
+ cardinality: 'one',
573
+ 'unique?': true,
574
+ 'index?': false,
575
+ isUnsynced: true,
576
+ },
577
+ ],
578
+ [
579
+ 'add-attr',
580
+ {
581
+ id: expect.any(String),
582
+ 'forward-identity': [expect.any(String), 'user_prefs', 'users'],
583
+ 'reverse-identity': [expect.any(String), 'users', 'user_prefs'],
584
+ 'value-type': 'ref',
585
+ cardinality: 'one',
586
+ 'unique?': true,
587
+ 'index?': true,
588
+ isUnsynced: true,
589
+ },
590
+ ],
591
+ ['add-triple', uid, expect.any(String), uid],
592
+ ['add-triple', lookup, expect.any(String), lookup],
593
+ ];
594
+
595
+ expect(result).toHaveLength(expected.length);
596
+ for (const item of expected) {
597
+ expect(result).toContainEqual(item);
598
+ }
599
+ });
600
+
601
+ test('lookup creates unique ref attrs for ref lookup in link value', () => {
602
+ const uid = uuid();
603
+ const ops = [
604
+ instatx.tx.users[uid]
605
+ .update({})
606
+ .link({ user_prefs: instatx.lookup('users.id', uid) }),
607
+ ];
608
+
609
+ const lookup = [
610
+ // The attr is going to be created, so we don't know its value yet
611
+ expect.any(String),
612
+ uid,
613
+ ];
614
+
615
+ const result = instaml.transform(
616
+ { attrsStore: new AttrsStoreClass({}, null) },
617
+ ops,
618
+ );
619
+
620
+ const expected = [
621
+ [
622
+ 'add-attr',
623
+ {
624
+ id: expect.any(String),
625
+ 'forward-identity': [expect.any(String), 'users', 'user_prefs'],
626
+ 'reverse-identity': [expect.any(String), 'user_prefs', 'users'],
627
+ 'value-type': 'ref',
628
+ cardinality: 'one',
629
+ 'unique?': true,
630
+ 'index?': true,
631
+ isUnsynced: true,
632
+ },
633
+ ],
634
+ [
635
+ 'add-attr',
636
+ {
637
+ id: expect.any(String),
638
+ 'forward-identity': [expect.any(String), 'users', 'id'],
639
+ 'value-type': 'blob',
640
+ cardinality: 'one',
641
+ 'unique?': true,
642
+ 'index?': false,
643
+ isUnsynced: true,
644
+ },
645
+ ],
646
+ ['add-triple', uid, expect.any(String), uid],
647
+ ['add-triple', uid, expect.any(String), lookup],
648
+ ];
649
+
650
+ expect(result).toHaveLength(expected.length);
651
+ for (const item of expected) {
652
+ expect(result).toContainEqual(item);
653
+ }
654
+ });
655
+
656
+ test('lookups create entities from links', () => {
657
+ const bookshelfId = uuid();
658
+
659
+ const ops = instatx.tx.users[instatx.lookup('handle', 'bobby_newuser')].link({
660
+ bookshelves: bookshelfId,
661
+ });
662
+
663
+ const result = instaml.transform({ attrsStore: zenecaAttrsStore }, ops);
664
+ // @ts-expect-error
665
+ const expectedLookup = [zenecaAttrToId['users/handle'], 'bobby_newuser'];
666
+ const expected = [
667
+ // @ts-expect-error
668
+ ['add-triple', expectedLookup, zenecaAttrToId['users/id'], expectedLookup],
669
+ [
670
+ 'add-triple',
671
+ expectedLookup,
672
+ // @ts-expect-error
673
+ zenecaAttrToId['users/bookshelves'],
674
+ bookshelfId,
675
+ ],
676
+ ];
677
+
678
+ expect(result).toHaveLength(expected.length);
679
+ for (const item of expected) {
680
+ expect(result).toContainEqual(item);
681
+ }
682
+ });
683
+
684
+ test('lookups create entities from unlinks', () => {
685
+ const bookshelfId = uuid();
686
+
687
+ const ops = instatx.tx.users[
688
+ instatx.lookup('handle', 'bobby_newuser')
689
+ ].unlink({
690
+ bookshelves: bookshelfId,
691
+ });
692
+
693
+ const result = instaml.transform({ attrsStore: zenecaAttrsStore }, ops);
694
+ // @ts-expect-error
695
+ const expectedLookup = [zenecaAttrToId['users/handle'], 'bobby_newuser'];
696
+ const expected = [
697
+ // @ts-expect-error
698
+ ['add-triple', expectedLookup, zenecaAttrToId['users/id'], expectedLookup],
699
+ [
700
+ 'retract-triple',
701
+ expectedLookup,
702
+ // @ts-expect-error
703
+ zenecaAttrToId['users/bookshelves'],
704
+ bookshelfId,
705
+ ],
706
+ ];
707
+
708
+ expect(result).toHaveLength(expected.length);
709
+ for (const item of expected) {
710
+ expect(result).toContainEqual(item);
711
+ }
712
+ });
713
+
714
+ test('mode: update', () => {
715
+ // create
716
+ var id = uuid();
717
+ var ops = instatx.tx.users[id].update({ handle: 'test' });
718
+ var result = instaml.transform(
719
+ { attrsStore: zenecaAttrsStore, stores: [store] },
720
+ ops,
721
+ );
722
+ for (const txStep of result) {
723
+ expect(txStep[4]).toEqual(undefined);
724
+ }
725
+
726
+ // update by id
727
+ ops = instatx.tx.users['ce942051-2d74-404a-9c7d-4aa3f2d54ae4'].update({
728
+ handle: 'joe2',
729
+ });
730
+ result = instaml.transform(
731
+ { attrsStore: zenecaAttrsStore, stores: [store] },
732
+ ops,
733
+ );
734
+ for (const txStep of result) {
735
+ expect(txStep[4]).toEqual({ mode: 'update' });
736
+ }
737
+
738
+ // update by lookup
739
+ ops = instatx.tx.users[instatx.lookup('email', 'stopa@instantdb.com')].update(
740
+ { handle: 'stopa2' },
741
+ );
742
+ result = instaml.transform(
743
+ { attrsStore: zenecaAttrsStore, stores: [store] },
744
+ ops,
745
+ );
746
+ for (const txStep of result) {
747
+ expect(txStep[4]).toEqual({ mode: 'update' });
748
+ }
749
+
750
+ // forced mode
751
+ var ops = instatx.tx.users[id].update({ handle: 'test' }, { upsert: false });
752
+ var result = instaml.transform(
753
+ { attrsStore: zenecaAttrsStore, stores: [store] },
754
+ ops,
755
+ );
756
+ for (const txStep of result) {
757
+ expect(txStep[4]).toEqual({ mode: 'update' });
758
+ }
759
+
760
+ var ops = instatx.tx.users['ce942051-2d74-404a-9c7d-4aa3f2d54ae4'].update(
761
+ { handle: 'test' },
762
+ { upsert: true },
763
+ );
764
+ var result = instaml.transform(
765
+ { attrsStore: zenecaAttrsStore, stores: [store] },
766
+ ops,
767
+ );
768
+ for (const txStep of result) {
769
+ expect(txStep[4]).toEqual(undefined);
770
+ }
771
+ });
772
+
773
+ test('it throws if you use an invalid link attr', () => {
774
+ expect(() =>
775
+ instaml.transform(
776
+ { attrsStore: new AttrsStoreClass({}, null) },
777
+ instatx.tx.users[
778
+ instatx.lookup('user_pref.email', 'test@example.com')
779
+ ].update({
780
+ a: 1,
781
+ }),
782
+ ),
783
+ ).toThrowError('user_pref.email is not a valid lookup attribute.');
784
+ });
785
+
786
+ test("it doesn't throw if you have a period in your attr", () => {
787
+ const aid = uuid();
788
+ const iid = uuid();
789
+ const pid = uuid();
790
+ const attrsStore = new AttrsStoreClass(
791
+ {
792
+ [aid]: {
793
+ id: aid,
794
+ cardinality: 'one',
795
+ 'forward-identity': [uuid(), 'users', 'attr.with.dot'],
796
+ 'index?': true,
797
+ 'unique?': true,
798
+ 'value-type': 'blob',
799
+ } as unknown as InstantDBAttr,
800
+ [iid]: {
801
+ id: iid,
802
+ cardinality: 'one',
803
+ 'forward-identity': [uuid(), 'users', 'id'],
804
+ 'index?': true,
805
+ 'unique?': false,
806
+ 'value-type': 'blob',
807
+ } as unknown as InstantDBAttr,
808
+ [pid]: {
809
+ id: pid,
810
+ cardinality: 'one',
811
+ 'forward-identity': [uuid(), 'users', 'a'],
812
+ 'index?': false,
813
+ 'unique?': false,
814
+ 'value-type': 'blob',
815
+ } as unknown as InstantDBAttr,
816
+ },
817
+ null,
818
+ );
819
+
820
+ expect(
821
+ instaml.transform(
822
+ { attrsStore },
823
+ instatx.tx.users[instatx.lookup('attr.with.dot', 'value')].update({
824
+ a: 1,
825
+ }),
826
+ ),
827
+ ).toEqual([
828
+ ['add-triple', [aid, 'value'], iid, [aid, 'value']],
829
+ ['add-triple', [aid, 'value'], pid, 1],
830
+ ]);
831
+ });
832
+
833
+ test("it doesn't create duplicate ref attrs", () => {
834
+ const aid = uuid();
835
+ const bid = uuid();
836
+ const ops = [
837
+ instatx.tx.nsA[aid].update({}).link({ nsB: bid }),
838
+ instatx.tx.nsB[bid].update({}).link({ nsA: aid }),
839
+ ];
840
+
841
+ const result = instaml.transform(
842
+ { attrsStore: new AttrsStoreClass({}, null) },
843
+ ops,
844
+ );
845
+
846
+ const expected = [
847
+ [
848
+ 'add-attr',
849
+ {
850
+ cardinality: 'one',
851
+ 'forward-identity': [expect.any(String), 'nsA', 'id'],
852
+ id: expect.any(String),
853
+ 'index?': false,
854
+ isUnsynced: true,
855
+ 'unique?': true,
856
+ 'value-type': 'blob',
857
+ },
858
+ ],
859
+ [
860
+ 'add-attr',
861
+ {
862
+ cardinality: 'one',
863
+ 'forward-identity': [expect.any(String), 'nsB', 'id'],
864
+ id: expect.any(String),
865
+ 'index?': false,
866
+ isUnsynced: true,
867
+ 'unique?': true,
868
+ 'value-type': 'blob',
869
+ },
870
+ ],
871
+ [
872
+ 'add-attr',
873
+ {
874
+ cardinality: 'many',
875
+ 'forward-identity': [expect.any(String), 'nsA', 'nsB'],
876
+ id: expect.any(String),
877
+ 'index?': false,
878
+ isUnsynced: true,
879
+ 'reverse-identity': [expect.any(String), 'nsB', 'nsA'],
880
+ 'unique?': false,
881
+ 'value-type': 'ref',
882
+ },
883
+ ],
884
+ ['add-triple', aid, expect.any(String), aid],
885
+ ['add-triple', aid, expect.any(String), bid],
886
+ ['add-triple', bid, expect.any(String), bid],
887
+ ['add-triple', aid, expect.any(String), bid],
888
+ ];
889
+
890
+ expect(result).toHaveLength(expected.length);
891
+ for (const item of expected) {
892
+ expect(result).toContainEqual(item);
893
+ }
894
+ });
895
+
896
+ test('Schema: uses info in `attrs` and `links`', () => {
897
+ const schema = i.schema({
898
+ entities: {
899
+ comments: i.entity({
900
+ slug: i.string().unique().indexed(),
901
+ }),
902
+ books: i.entity({}),
903
+ },
904
+ links: {
905
+ commentBooks: {
906
+ forward: {
907
+ on: 'comments',
908
+ has: 'one',
909
+ label: 'book',
910
+ },
911
+ reverse: {
912
+ on: 'books',
913
+ has: 'many',
914
+ label: 'comments',
915
+ },
916
+ },
917
+ },
918
+ });
919
+
920
+ const commentId = uuid();
921
+ const bookId = uuid();
922
+ const ops = instatx.tx.comments[commentId]
923
+ .update({
924
+ slug: 'test-slug',
925
+ })
926
+ .link({
927
+ book: bookId,
928
+ });
929
+
930
+ const result = instaml.transform(
931
+ {
932
+ attrsStore: zenecaAttrsStore,
933
+ schema: schema,
934
+ },
935
+ ops,
936
+ );
937
+
938
+ const expected = [
939
+ [
940
+ 'add-attr',
941
+ {
942
+ id: expect.any(String),
943
+ 'forward-identity': [expect.any(String), 'comments', 'slug'],
944
+ 'value-type': 'blob',
945
+ cardinality: 'one',
946
+ 'unique?': true,
947
+ 'index?': true,
948
+ 'checked-data-type': 'string',
949
+ isUnsynced: true,
950
+ },
951
+ ],
952
+ [
953
+ 'add-attr',
954
+ {
955
+ id: expect.any(String),
956
+ 'forward-identity': [expect.any(String), 'comments', 'id'],
957
+ 'value-type': 'blob',
958
+ cardinality: 'one',
959
+ 'unique?': true,
960
+ 'index?': false,
961
+ isUnsynced: true,
962
+ },
963
+ ],
964
+ [
965
+ 'add-attr',
966
+ {
967
+ id: expect.any(String),
968
+ 'forward-identity': [expect.any(String), 'comments', 'book'],
969
+ 'reverse-identity': [expect.any(String), 'books', 'comments'],
970
+ 'value-type': 'ref',
971
+ cardinality: 'one',
972
+ 'unique?': false,
973
+ 'index?': false,
974
+ isUnsynced: true,
975
+ },
976
+ ],
977
+ ['add-triple', commentId, expect.any(String), commentId],
978
+ ['add-triple', commentId, expect.any(String), 'test-slug'],
979
+ ['add-triple', commentId, expect.any(String), bookId],
980
+ ];
981
+ expect(result).toHaveLength(expected.length);
982
+ for (const item of expected) {
983
+ expect(result).toContainEqual(item);
984
+ }
985
+ });
986
+
987
+ test("Schema: doesn't create duplicate ref attrs", () => {
988
+ const schema = i.schema({
989
+ entities: {
990
+ comments: i.entity({}),
991
+ books: i.entity({}),
992
+ },
993
+ links: {
994
+ commentBooks: {
995
+ forward: {
996
+ on: 'comments',
997
+ has: 'one',
998
+ label: 'book',
999
+ },
1000
+ reverse: {
1001
+ on: 'books',
1002
+ has: 'many',
1003
+ label: 'comments',
1004
+ },
1005
+ },
1006
+ },
1007
+ });
1008
+
1009
+ const commentId = uuid();
1010
+ const bookId = uuid();
1011
+ const ops = [
1012
+ instatx.tx.comments[commentId].update({}).link({ book: bookId }),
1013
+ instatx.tx.books[bookId].update({}).link({ comments: commentId }),
1014
+ ];
1015
+
1016
+ const result = instaml.transform(
1017
+ { attrsStore: zenecaAttrsStore, schema },
1018
+ ops,
1019
+ );
1020
+
1021
+ const expected = [
1022
+ [
1023
+ 'add-attr',
1024
+ {
1025
+ cardinality: 'one',
1026
+ 'forward-identity': [expect.any(String), 'comments', 'id'],
1027
+ id: expect.any(String),
1028
+ 'index?': false,
1029
+ isUnsynced: true,
1030
+ 'unique?': true,
1031
+ 'value-type': 'blob',
1032
+ },
1033
+ ],
1034
+ [
1035
+ 'add-attr',
1036
+ {
1037
+ cardinality: 'one',
1038
+ 'forward-identity': [expect.any(String), 'comments', 'book'],
1039
+ id: expect.any(String),
1040
+ 'index?': false,
1041
+ isUnsynced: true,
1042
+ 'reverse-identity': [expect.any(String), 'books', 'comments'],
1043
+ 'unique?': false,
1044
+ 'value-type': 'ref',
1045
+ },
1046
+ ],
1047
+ ['add-triple', commentId, expect.any(String), commentId],
1048
+ ['add-triple', commentId, expect.any(String), bookId],
1049
+ ['add-triple', bookId, expect.any(String), bookId],
1050
+ ['add-triple', commentId, expect.any(String), bookId],
1051
+ ];
1052
+ expect(result).toHaveLength(expected.length);
1053
+ for (const item of expected) {
1054
+ expect(result).toContainEqual(item);
1055
+ }
1056
+ });
1057
+
1058
+ test('Schema: lookup creates unique attrs for custom lookups', () => {
1059
+ const schema = i.schema({
1060
+ entities: {
1061
+ users: i.entity({
1062
+ nickname: i.string().unique().indexed(),
1063
+ }),
1064
+ },
1065
+ });
1066
+
1067
+ const ops = instatx.tx.users[instatx.lookup('nickname', 'stopanator')].update(
1068
+ {
1069
+ handle: 'stopa',
1070
+ },
1071
+ );
1072
+
1073
+ const lookup = [
1074
+ // The attr is going to be created, so we don't know its value yet
1075
+ expect.any(String),
1076
+ 'stopanator',
1077
+ ];
1078
+
1079
+ const result = instaml.transform(
1080
+ { attrsStore: zenecaAttrsStore, schema },
1081
+ ops,
1082
+ );
1083
+ const expected = [
1084
+ [
1085
+ 'add-attr',
1086
+ {
1087
+ cardinality: 'one',
1088
+ 'forward-identity': [expect.any(String), 'users', 'nickname'],
1089
+ id: expect.any(String),
1090
+ 'index?': true,
1091
+ isUnsynced: true,
1092
+ 'unique?': true,
1093
+ 'checked-data-type': 'string',
1094
+ 'value-type': 'blob',
1095
+ },
1096
+ ],
1097
+ // @ts-expect-error
1098
+ ['add-triple', lookup, zenecaAttrToId['users/handle'], 'stopa'],
1099
+ // @ts-expect-error
1100
+ ['add-triple', lookup, zenecaAttrToId['users/id'], lookup],
1101
+ ];
1102
+
1103
+ expect(result).toHaveLength(expected.length);
1104
+ for (const item of expected) {
1105
+ expect(result).toContainEqual(item);
1106
+ }
1107
+ });
1108
+
1109
+ test('Schema: lookup creates unique attrs for lookups in link values', () => {
1110
+ const schema = i.schema({
1111
+ entities: {
1112
+ posts: i.entity({
1113
+ slug: i.string().unique().indexed(),
1114
+ }),
1115
+ users: i.entity({}),
1116
+ },
1117
+ links: {
1118
+ postUsers: {
1119
+ forward: {
1120
+ on: 'users',
1121
+ has: 'many',
1122
+ label: 'authoredPosts',
1123
+ },
1124
+ reverse: {
1125
+ on: 'posts',
1126
+ has: 'one',
1127
+ label: 'author',
1128
+ },
1129
+ },
1130
+ },
1131
+ });
1132
+
1133
+ const uid = uuid();
1134
+ const ops = instatx.tx.users[uid]
1135
+ .update({})
1136
+ .link({ authoredPosts: instatx.lookup('slug', 'life-is-good') });
1137
+
1138
+ const result = instaml.transform(
1139
+ { attrsStore: new AttrsStoreClass({}, null), schema },
1140
+ ops,
1141
+ );
1142
+
1143
+ expect(result).toEqual([
1144
+ [
1145
+ 'add-attr',
1146
+ {
1147
+ id: expect.any(String),
1148
+ 'forward-identity': [expect.any(String), 'users', 'authoredPosts'],
1149
+ 'reverse-identity': [expect.any(String), 'posts', 'author'],
1150
+ 'value-type': 'ref',
1151
+ // TODO: should this be one?
1152
+ cardinality: 'one',
1153
+ 'unique?': true,
1154
+ 'index?': true,
1155
+ isUnsynced: true,
1156
+ },
1157
+ ],
1158
+ [
1159
+ 'add-attr',
1160
+ {
1161
+ id: expect.any(String),
1162
+ 'forward-identity': [expect.any(String), 'posts', 'slug'],
1163
+ 'value-type': 'blob',
1164
+ cardinality: 'one',
1165
+ 'unique?': true,
1166
+ 'index?': true,
1167
+ 'checked-data-type': 'string',
1168
+ isUnsynced: true,
1169
+ },
1170
+ ],
1171
+ [
1172
+ 'add-attr',
1173
+ {
1174
+ id: expect.any(String),
1175
+ 'forward-identity': [expect.any(String), 'users', 'id'],
1176
+ 'value-type': 'blob',
1177
+ cardinality: 'one',
1178
+ 'unique?': true,
1179
+ 'index?': false,
1180
+ isUnsynced: true,
1181
+ },
1182
+ ],
1183
+ ['add-triple', uid, expect.any(String), uid],
1184
+ [
1185
+ 'add-triple',
1186
+ uid,
1187
+ expect.any(String),
1188
+ [expect.any(String), 'life-is-good'],
1189
+ ],
1190
+ ]);
1191
+ });
1192
+
1193
+ test('Schema: lookup creates unique attrs for lookups in link values with arrays', () => {
1194
+ const schema = i.schema({
1195
+ entities: {
1196
+ posts: i.entity({
1197
+ slug: i.string().unique().indexed(),
1198
+ }),
1199
+ users: i.entity({}),
1200
+ },
1201
+ links: {
1202
+ postUsers: {
1203
+ forward: {
1204
+ on: 'users',
1205
+ has: 'many',
1206
+ label: 'authoredPosts',
1207
+ },
1208
+ reverse: {
1209
+ on: 'posts',
1210
+ has: 'one',
1211
+ label: 'author',
1212
+ },
1213
+ },
1214
+ },
1215
+ });
1216
+
1217
+ const uid = uuid();
1218
+ const ops = instatx.tx.users[uid].update({}).link({
1219
+ authoredPosts: [
1220
+ instatx.lookup('slug', 'life-is-good'),
1221
+ instatx.lookup('slug', 'check-this-out'),
1222
+ ],
1223
+ });
1224
+
1225
+ const result = instaml.transform(
1226
+ { attrsStore: new AttrsStoreClass({}, null), schema },
1227
+ ops,
1228
+ );
1229
+
1230
+ const expected = [
1231
+ [
1232
+ 'add-attr',
1233
+ {
1234
+ id: expect.any(String),
1235
+ 'forward-identity': [expect.any(String), 'users', 'authoredPosts'],
1236
+ 'reverse-identity': [expect.any(String), 'posts', 'author'],
1237
+ 'value-type': 'ref',
1238
+ cardinality: 'one',
1239
+ 'unique?': true,
1240
+ 'index?': true,
1241
+ isUnsynced: true,
1242
+ },
1243
+ ],
1244
+ [
1245
+ 'add-attr',
1246
+ {
1247
+ id: expect.any(String),
1248
+ 'forward-identity': [expect.any(String), 'posts', 'slug'],
1249
+ 'value-type': 'blob',
1250
+ cardinality: 'one',
1251
+ 'unique?': true,
1252
+ 'index?': true,
1253
+ 'checked-data-type': 'string',
1254
+ isUnsynced: true,
1255
+ },
1256
+ ],
1257
+ [
1258
+ 'add-attr',
1259
+ {
1260
+ id: expect.any(String),
1261
+ 'forward-identity': [expect.any(String), 'users', 'id'],
1262
+ 'value-type': 'blob',
1263
+ cardinality: 'one',
1264
+ 'unique?': true,
1265
+ 'index?': false,
1266
+ isUnsynced: true,
1267
+ },
1268
+ ],
1269
+ ['add-triple', uid, expect.any(String), uid],
1270
+ [
1271
+ 'add-triple',
1272
+ uid,
1273
+ expect.any(String),
1274
+ [expect.any(String), 'life-is-good'],
1275
+ ],
1276
+ [
1277
+ 'add-triple',
1278
+ uid,
1279
+ expect.any(String),
1280
+ [expect.any(String), 'check-this-out'],
1281
+ ],
1282
+ ];
1283
+
1284
+ expect(result).toHaveLength(expected.length);
1285
+ for (const item of expected) {
1286
+ expect(result).toContainEqual(item);
1287
+ }
1288
+ });
1289
+
1290
+ test('Schema: lookup creates unique ref attrs for ref lookup', () => {
1291
+ const schema = i.schema({
1292
+ entities: {
1293
+ users: i.entity({}),
1294
+ user_prefs: i.entity({}),
1295
+ },
1296
+ links: {
1297
+ user_user_prefs: {
1298
+ forward: {
1299
+ on: 'user_prefs',
1300
+ has: 'one',
1301
+ label: 'user',
1302
+ },
1303
+ reverse: {
1304
+ on: 'users',
1305
+ has: 'one',
1306
+ label: 'user_pref',
1307
+ },
1308
+ },
1309
+ },
1310
+ });
1311
+
1312
+ const uid = uuid();
1313
+ const ops = [
1314
+ instatx.tx.users[uid].update({}),
1315
+ instatx.tx.user_prefs[instatx.lookup('user.id', uid)].update({}),
1316
+ ];
1317
+
1318
+ const lookup = [
1319
+ // The attr is going to be created, so we don't know its value yet
1320
+ expect.any(String),
1321
+ uid,
1322
+ ];
1323
+
1324
+ const result = instaml.transform(
1325
+ { attrsStore: new AttrsStoreClass({}, null), schema },
1326
+ ops,
1327
+ );
1328
+ const expected = [
1329
+ [
1330
+ 'add-attr',
1331
+ {
1332
+ id: expect.any(String),
1333
+ 'forward-identity': [expect.any(String), 'users', 'id'],
1334
+ 'value-type': 'blob',
1335
+ cardinality: 'one',
1336
+ 'unique?': true,
1337
+ 'index?': false,
1338
+ isUnsynced: true,
1339
+ },
1340
+ ],
1341
+ [
1342
+ 'add-attr',
1343
+ {
1344
+ id: expect.any(String),
1345
+ 'forward-identity': [expect.any(String), 'user_prefs', 'id'],
1346
+ 'value-type': 'blob',
1347
+ cardinality: 'one',
1348
+ 'unique?': true,
1349
+ 'index?': false,
1350
+ isUnsynced: true,
1351
+ },
1352
+ ],
1353
+ [
1354
+ 'add-attr',
1355
+ {
1356
+ id: expect.any(String),
1357
+ 'forward-identity': [expect.any(String), 'user_prefs', 'user'],
1358
+ 'reverse-identity': [expect.any(String), 'users', 'user_pref'],
1359
+ 'value-type': 'ref',
1360
+ cardinality: 'one',
1361
+ 'unique?': true,
1362
+ 'index?': true,
1363
+ isUnsynced: true,
1364
+ },
1365
+ ],
1366
+ ['add-triple', uid, expect.any(String), uid],
1367
+ ['add-triple', lookup, expect.any(String), lookup],
1368
+ ];
1369
+
1370
+ expect(result).toHaveLength(expected.length);
1371
+ for (const item of expected) {
1372
+ expect(result).toContainEqual(item);
1373
+ }
1374
+ });
1375
+
1376
+ test('Schema: lookup creates unique ref attrs for ref lookup in link value', () => {
1377
+ const schema = i.schema({
1378
+ entities: {
1379
+ users: i.entity({}),
1380
+ user_prefs: i.entity({}),
1381
+ },
1382
+ links: {
1383
+ user_user_prefs: {
1384
+ forward: {
1385
+ on: 'users',
1386
+ has: 'one',
1387
+ label: 'user_pref',
1388
+ },
1389
+ reverse: {
1390
+ on: 'user_prefs',
1391
+ has: 'one',
1392
+ label: 'user',
1393
+ },
1394
+ },
1395
+ },
1396
+ });
1397
+ const uid = uuid();
1398
+ const ops = [
1399
+ instatx.tx.users[uid]
1400
+ .update({})
1401
+ .link({ user_pref: instatx.lookup('user.id', uid) }),
1402
+ ];
1403
+
1404
+ const lookup = [
1405
+ // The attr is going to be created, so we don't know its value yet
1406
+ expect.any(String),
1407
+ uid,
1408
+ ];
1409
+
1410
+ const result = instaml.transform(
1411
+ { attrsStore: new AttrsStoreClass({}, null), schema },
1412
+ ops,
1413
+ );
1414
+
1415
+ const expected = [
1416
+ [
1417
+ 'add-attr',
1418
+ {
1419
+ id: expect.any(String),
1420
+ 'forward-identity': [expect.any(String), 'users', 'user_pref'],
1421
+ 'reverse-identity': [expect.any(String), 'user_prefs', 'user'],
1422
+ 'value-type': 'ref',
1423
+ cardinality: 'one',
1424
+ 'unique?': true,
1425
+ 'index?': true,
1426
+ isUnsynced: true,
1427
+ },
1428
+ ],
1429
+ [
1430
+ 'add-attr',
1431
+ {
1432
+ id: expect.any(String),
1433
+ 'forward-identity': [expect.any(String), 'users', 'id'],
1434
+ 'value-type': 'blob',
1435
+ cardinality: 'one',
1436
+ 'unique?': true,
1437
+ 'index?': false,
1438
+ isUnsynced: true,
1439
+ },
1440
+ ],
1441
+ ['add-triple', uid, expect.any(String), uid],
1442
+ ['add-triple', uid, expect.any(String), lookup],
1443
+ ];
1444
+
1445
+ expect(result).toHaveLength(expected.length);
1446
+ for (const item of expected) {
1447
+ expect(result).toContainEqual(item);
1448
+ }
1449
+ });
1450
+
1451
+ test('Schema: populates checked-data-type', () => {
1452
+ const schema = i.schema({
1453
+ entities: {
1454
+ comments: i.entity({
1455
+ s: i.string(),
1456
+ n: i.number(),
1457
+ d: i.date(),
1458
+ b: i.boolean(),
1459
+ a: i.any(),
1460
+ j: i.json(),
1461
+ }),
1462
+ },
1463
+ });
1464
+
1465
+ const commentId = uuid();
1466
+ const ops = instatx.tx.comments[commentId].update({
1467
+ s: 'str',
1468
+ n: 'num',
1469
+ d: 'date',
1470
+ b: 'bool',
1471
+ a: 'any',
1472
+ j: 'json',
1473
+ });
1474
+
1475
+ const result = instaml.transform(
1476
+ {
1477
+ attrsStore: zenecaAttrsStore,
1478
+ schema: schema,
1479
+ },
1480
+ ops,
1481
+ );
1482
+
1483
+ const expected = [
1484
+ [
1485
+ 'add-attr',
1486
+ {
1487
+ id: expect.any(String),
1488
+ 'forward-identity': [expect.any(String), 'comments', 's'],
1489
+ 'value-type': 'blob',
1490
+ cardinality: 'one',
1491
+ 'unique?': false,
1492
+ 'index?': false,
1493
+ isUnsynced: true,
1494
+ 'checked-data-type': 'string',
1495
+ },
1496
+ ],
1497
+ [
1498
+ 'add-attr',
1499
+ {
1500
+ id: expect.any(String),
1501
+ 'forward-identity': [expect.any(String), 'comments', 'n'],
1502
+ 'value-type': 'blob',
1503
+ cardinality: 'one',
1504
+ 'unique?': false,
1505
+ 'index?': false,
1506
+ isUnsynced: true,
1507
+ 'checked-data-type': 'number',
1508
+ },
1509
+ ],
1510
+ [
1511
+ 'add-attr',
1512
+ {
1513
+ id: expect.any(String),
1514
+ 'forward-identity': [expect.any(String), 'comments', 'd'],
1515
+ 'value-type': 'blob',
1516
+ cardinality: 'one',
1517
+ 'unique?': false,
1518
+ 'index?': false,
1519
+ isUnsynced: true,
1520
+ 'checked-data-type': 'date',
1521
+ },
1522
+ ],
1523
+ [
1524
+ 'add-attr',
1525
+ {
1526
+ id: expect.any(String),
1527
+ 'forward-identity': [expect.any(String), 'comments', 'b'],
1528
+ 'value-type': 'blob',
1529
+ cardinality: 'one',
1530
+ 'unique?': false,
1531
+ 'index?': false,
1532
+ isUnsynced: true,
1533
+ 'checked-data-type': 'boolean',
1534
+ },
1535
+ ],
1536
+ [
1537
+ 'add-attr',
1538
+ {
1539
+ id: expect.any(String),
1540
+ 'forward-identity': [expect.any(String), 'comments', 'a'],
1541
+ 'value-type': 'blob',
1542
+ cardinality: 'one',
1543
+ 'unique?': false,
1544
+ 'index?': false,
1545
+ isUnsynced: true,
1546
+ },
1547
+ ],
1548
+ [
1549
+ 'add-attr',
1550
+ {
1551
+ id: expect.any(String),
1552
+ 'forward-identity': [expect.any(String), 'comments', 'j'],
1553
+ 'value-type': 'blob',
1554
+ cardinality: 'one',
1555
+ 'unique?': false,
1556
+ 'index?': false,
1557
+ isUnsynced: true,
1558
+ },
1559
+ ],
1560
+ [
1561
+ 'add-attr',
1562
+ {
1563
+ id: expect.any(String),
1564
+ 'forward-identity': [expect.any(String), 'comments', 'id'],
1565
+ 'value-type': 'blob',
1566
+ cardinality: 'one',
1567
+ 'unique?': true,
1568
+ 'index?': false,
1569
+ isUnsynced: true,
1570
+ },
1571
+ ],
1572
+ ['add-triple', commentId, expect.any(String), commentId],
1573
+ ['add-triple', commentId, expect.any(String), 'str'],
1574
+ ['add-triple', commentId, expect.any(String), 'num'],
1575
+ ['add-triple', commentId, expect.any(String), 'date'],
1576
+ ['add-triple', commentId, expect.any(String), 'bool'],
1577
+ ['add-triple', commentId, expect.any(String), 'any'],
1578
+ ['add-triple', commentId, expect.any(String), 'json'],
1579
+ ];
1580
+
1581
+ expect(result).toHaveLength(expected.length);
1582
+ for (const item of expected) {
1583
+ expect(result).toContainEqual(item);
1584
+ }
1585
+ });
1586
+
1587
+ test('instatx should not be too permissive', () => {
1588
+ const ops = instatx.tx.books[uuid()].update({
1589
+ title: 'New Title',
1590
+ // @ts-expect-error: testing invalid states
1591
+ }).this_is_an_unknown_op;
1592
+ expect(ops).toBeUndefined();
1593
+ });