@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
package/src/index.ts ADDED
@@ -0,0 +1,1283 @@
1
+ import Reactor from './Reactor.js';
2
+ import {
3
+ tx,
4
+ txInit,
5
+ lookup,
6
+ getOps,
7
+ type TxChunk,
8
+ type TransactionChunk,
9
+ } from './instatx.js';
10
+ import weakHash from './utils/weakHash.js';
11
+ import id from './utils/id.ts';
12
+ import IndexedDBStorage from './IndexedDBStorage.ts';
13
+ import { coerceToDate } from './utils/dates.js';
14
+ import WindowNetworkListener from './WindowNetworkListener.js';
15
+ import { i } from './schema.js';
16
+ import { createDevtool } from './devtool.js';
17
+ import version from './version.ts';
18
+ import { validateQuery, QueryValidationError } from './queryValidation.ts';
19
+ import {
20
+ validateTransactions,
21
+ TransactionValidationError,
22
+ } from './transactionValidation.ts';
23
+ import { setInstantWarningsEnabled } from './warningToggle.ts';
24
+
25
+ import {
26
+ StoreInterface,
27
+ type StoreInterfaceClass,
28
+ type StoreInterfaceStoreName,
29
+ } from './utils/PersistedObject.ts';
30
+ import { createInstantRouteHandler } from './createRouteHandler.ts';
31
+ import type {
32
+ InstantRouteHandlerPayloadByType,
33
+ InstantRouteHandlerType,
34
+ InstantRouteHandlerBody,
35
+ } from './routeHandlerProtocol.ts';
36
+ import { parseSchemaFromJSON } from './parseSchemaFromJSON.ts';
37
+ import type { Logger } from './utils/log.ts';
38
+
39
+ import type {
40
+ PresenceOpts,
41
+ PresenceResponse,
42
+ PresenceSlice,
43
+ RoomSchemaShape,
44
+ } from './presence.ts';
45
+ import type {
46
+ DevtoolConfig,
47
+ IDatabase,
48
+ IInstantDatabase,
49
+ StrictDevtoolConfig,
50
+ } from './coreTypes.ts';
51
+ import type {
52
+ Query,
53
+ QueryResponse,
54
+ InstaQLResponse,
55
+ PageInfoResponse,
56
+ Exactly,
57
+ InstantObject,
58
+ InstaQLParams,
59
+ InstaQLOptions,
60
+ InstaQLQueryParams,
61
+ InstaQLEntity,
62
+ InstaQLEntitySubquery,
63
+ InstaQLResult,
64
+ InstaQLFields,
65
+ ValidQuery,
66
+ Cursor,
67
+ Order,
68
+ InstaQLQueryEntityResult,
69
+ } from './queryTypes.ts';
70
+ import type { PresencePeer } from './presenceTypes.ts';
71
+ import type {
72
+ AuthState,
73
+ User,
74
+ AuthResult,
75
+ ConnectionStatus,
76
+ AppStatusState,
77
+ } from './clientTypes.ts';
78
+ import type {
79
+ InstantQuery,
80
+ InstantQueryResult,
81
+ InstantSchema,
82
+ InstantEntity,
83
+ InstantSchemaDatabase,
84
+ } from './helperTypes.ts';
85
+ import type {
86
+ InstantDBAttr,
87
+ InstantDBAttrOnDelete,
88
+ InstantDBCheckedDataType,
89
+ InstantDBIdent,
90
+ InstantDBInferredType,
91
+ } from './attrTypes.ts';
92
+ import type {
93
+ AttrsDefs,
94
+ CardinalityKind,
95
+ DataAttrDef,
96
+ EntitiesDef,
97
+ EntitiesWithLinks,
98
+ EntityDef,
99
+ RoomsDef,
100
+ InstantSchemaDef,
101
+ InstantGraph,
102
+ LinkAttrDef,
103
+ LinkDef,
104
+ LinksDef,
105
+ PresenceOf,
106
+ ResolveAttrs,
107
+ RoomsOf,
108
+ TopicsOf,
109
+ TopicOf,
110
+ ValueTypes,
111
+ InstantUnknownSchema,
112
+ InstantUnknownSchemaDef,
113
+ BackwardsCompatibleSchema,
114
+ UpdateParams,
115
+ LinkParams,
116
+ CreateParams,
117
+ RuleParams,
118
+ } from './schemaTypes.ts';
119
+ import type { InstantRules } from './rulesTypes.ts';
120
+ import type { UploadFileResponse, DeleteFileResponse } from './StorageAPI.ts';
121
+ import { FrameworkClient, type FrameworkConfig } from './framework.ts';
122
+
123
+ import type {
124
+ CheckMagicCodeParams,
125
+ CheckMagicCodeResponse,
126
+ ExchangeCodeForTokenParams,
127
+ SendMagicCodeParams,
128
+ SendMagicCodeResponse,
129
+ SignInAsGuestParams,
130
+ SignInWithIdTokenParams,
131
+ VerifyMagicCodeParams,
132
+ VerifyResponse,
133
+ } from './authAPI.ts';
134
+
135
+ import { InstantAPIError, type InstantIssue } from './utils/fetch.js';
136
+ import { InstantError } from './InstantError.ts';
137
+ import {
138
+ MissingApiURIError,
139
+ requireApiURI,
140
+ websocketURIFromApiURI,
141
+ } from './endpoints.ts';
142
+ import {
143
+ SSEConnection,
144
+ EventSourceType,
145
+ type EventSourceConstructor,
146
+ } from './Connection.ts';
147
+ import { CallbackEventType as SyncTableCallbackEventType } from './SyncTable.ts';
148
+ import type {
149
+ SyncTableCallback,
150
+ CallbackEvent as SyncTableCallbackEvent,
151
+ InitialSyncBatch as SyncTableInitialSyncBatch,
152
+ InitialSyncComplete as SyncTableInitialSyncComplete,
153
+ SyncTransaction as SyncTableSyncTransaction,
154
+ LoadFromStorage as SyncTableLoadFromStorage,
155
+ SetupError as SyncTableSetupError,
156
+ } from './SyncTable.ts';
157
+ import {
158
+ InstantStream,
159
+ InstantWritableStream,
160
+ InstantReadableStream,
161
+ ReadableStreamCtor,
162
+ WritableStreamCtor,
163
+ } from './Stream.ts';
164
+ import {
165
+ type InfiniteQueryCallbackResponse,
166
+ type InfiniteQuerySubscription,
167
+ subscribeInfiniteQuery,
168
+ getInfiniteQueryInitialSnapshot,
169
+ } from './infiniteQuery.ts';
170
+
171
+ const defaultOpenDevtool = true;
172
+
173
+ // types
174
+
175
+ type ExactlyOne<T> = {
176
+ [K in keyof T]: Pick<T, K> & Partial<Record<Exclude<keyof T, K>, never>>;
177
+ }[keyof T];
178
+
179
+ export type Config = {
180
+ appId: string;
181
+ websocketURI?: string;
182
+ apiURI?: string;
183
+ devtool?: boolean | DevtoolConfig;
184
+ verbose?: boolean;
185
+ logger?: Logger;
186
+ queryCacheLimit?: number;
187
+ useDateObjects: boolean;
188
+ disableValidation?: boolean;
189
+ };
190
+
191
+ export type InstantConfig<
192
+ S extends InstantSchemaDef<any, any, any>,
193
+ UseDates extends boolean = false,
194
+ > = {
195
+ appId: string;
196
+ schema?: S;
197
+ /**
198
+ * Websocket URL of your SidekickDB server. Derived from `apiURI` when not
199
+ * set (`https://host` becomes `wss://host/runtime/session`).
200
+ */
201
+ websocketURI?: string;
202
+ firstPartyPath?: string;
203
+ /**
204
+ * URL of your SidekickDB server, e.g. `https://sidekick.example.com`.
205
+ * Required: there is no default server, and `init()` throws without it.
206
+ */
207
+ apiURI?: string;
208
+ devtool?: boolean | DevtoolConfig;
209
+ verbose?: boolean;
210
+ logger?: Logger;
211
+ queryCacheLimit?: number;
212
+ useDateObjects: UseDates;
213
+ disableValidation?: boolean;
214
+ Store?: StoreInterfaceClass;
215
+ WritableStream?: WritableStreamCtor;
216
+ ReadableStream?: ReadableStreamCtor;
217
+ };
218
+
219
+ export type ConfigWithSchema<S extends InstantGraph<any, any>> = Config & {
220
+ schema: S;
221
+ };
222
+
223
+ export type TransactionResult = {
224
+ status: 'synced' | 'enqueued';
225
+ clientId: string;
226
+ };
227
+
228
+ export type PublishTopic<TopicsByKey> = <Key extends keyof TopicsByKey>(
229
+ topic: Key,
230
+ data: TopicsByKey[Key],
231
+ ) => void;
232
+
233
+ export type SubscribeTopic<PresenceShape, TopicsByKey> = <
234
+ Key extends keyof TopicsByKey,
235
+ >(
236
+ topic: Key,
237
+ onEvent: (event: TopicsByKey[Key], peer: PresenceShape) => void,
238
+ ) => () => void;
239
+
240
+ export type GetPresence<PresenceShape> = <Keys extends keyof PresenceShape>(
241
+ opts: PresenceOpts<PresenceShape, Keys>,
242
+ ) => PresenceResponse<PresenceShape, Keys> | null;
243
+
244
+ export type SubscribePresence<PresenceShape> = <
245
+ Keys extends keyof PresenceShape,
246
+ >(
247
+ opts: PresenceOpts<PresenceShape, Keys>,
248
+ onChange: (slice: PresenceResponse<PresenceShape, Keys>) => void,
249
+ ) => () => void;
250
+
251
+ export type RoomHandle<PresenceShape, TopicsByKey> = {
252
+ leaveRoom: () => void;
253
+ publishTopic: PublishTopic<TopicsByKey>;
254
+ subscribeTopic: SubscribeTopic<PresenceShape, TopicsByKey>;
255
+ publishPresence: (data: Partial<PresenceShape>) => void;
256
+ getPresence: GetPresence<PresenceShape>;
257
+ subscribePresence: SubscribePresence<PresenceShape>;
258
+ };
259
+
260
+ type AuthToken = string;
261
+
262
+ type SubscriptionState<Q, Schema, WithCardinalityInference extends boolean> =
263
+ | {
264
+ error: { message: string; traceId?: string };
265
+ data: undefined;
266
+ pageInfo: undefined;
267
+ }
268
+ | {
269
+ error: undefined;
270
+ data: QueryResponse<Q, Schema, WithCardinalityInference>;
271
+ pageInfo: PageInfoResponse<Q>;
272
+ };
273
+
274
+ type InstaQLSubscriptionState<Schema, Q, UseDates extends boolean> =
275
+ | {
276
+ error: { message: string; traceId?: string };
277
+ data: undefined;
278
+ pageInfo: undefined;
279
+ }
280
+ | {
281
+ error: undefined;
282
+ data: InstaQLResponse<Schema, Q, UseDates>;
283
+ pageInfo: PageInfoResponse<Q>;
284
+ };
285
+
286
+ type LifecycleSubscriptionState<
287
+ Q,
288
+ Schema,
289
+ WithCardinalityInference extends boolean,
290
+ > = SubscriptionState<Q, Schema, WithCardinalityInference> & {
291
+ isLoading: boolean;
292
+ };
293
+
294
+ type InstaQLLifecycleState<Schema, Q, UseDates extends boolean = false> =
295
+ | (InstaQLSubscriptionState<Schema, Q, UseDates> & {
296
+ isLoading: boolean;
297
+ })
298
+ | {
299
+ isLoading: true;
300
+ data: undefined;
301
+ pageInfo: undefined;
302
+ error: undefined;
303
+ };
304
+
305
+ type UnsubscribeFn = () => void;
306
+
307
+ // hmr
308
+ function initSchemaHashStore(): WeakMap<any, string> {
309
+ globalThis.__instantDbSchemaHashStore =
310
+ globalThis.__instantDbSchemaHashStore ?? new WeakMap<any, string>();
311
+ return globalThis.__instantDbSchemaHashStore;
312
+ }
313
+
314
+ function initGlobalInstantCoreStore(): Record<string, any> {
315
+ globalThis.__instantDbStore = globalThis.__instantDbStore ?? {};
316
+ return globalThis.__instantDbStore;
317
+ }
318
+
319
+ function reactorKey(config: InstantConfig<any, boolean>): string {
320
+ // @ts-expect-error
321
+ const adminToken = config.__adminToken;
322
+ // Sometimes you _want_ to simulate two separate db instances in the same
323
+ // page. This can happen when doing real-time demos. To make this work we
324
+ // have a hidden `__extraDedupeKey` that you can use to differentiate db
325
+ // configs that are otherwise exactly the same.
326
+ // @ts-expect-error
327
+ const extraDedupeKey = config.__extraDedupeKey;
328
+ return (
329
+ config.appId +
330
+ '_' +
331
+ (config.websocketURI || 'default_ws_uri') +
332
+ '_' +
333
+ (config.apiURI || 'default_api_uri') +
334
+ '_' +
335
+ (adminToken || 'client_only') +
336
+ '_' +
337
+ config.useDateObjects +
338
+ '_' +
339
+ (extraDedupeKey || '')
340
+ );
341
+ }
342
+
343
+ const globalInstantCoreStore = initGlobalInstantCoreStore();
344
+ const schemaHashStore = initSchemaHashStore();
345
+
346
+ type SignoutOpts = {
347
+ invalidateToken?: boolean;
348
+ };
349
+
350
+ /**
351
+ * Functions to log users in and out.
352
+ *
353
+ * @see https://sidekickdb.co/docs/auth
354
+ */
355
+ class Auth {
356
+ constructor(private db: Reactor) {}
357
+
358
+ /**
359
+ * Sends a magic code to the user's email address.
360
+ *
361
+ * Once you send the magic code, see {@link auth.signInWithMagicCode} to let the
362
+ * user verify.
363
+ *
364
+ * @see https://sidekickdb.co/docs/auth
365
+ * @example
366
+ * db.auth.sendMagicCode({email: "example@gmail.com"})
367
+ * .catch((err) => console.error(err.body?.message))
368
+ */
369
+ sendMagicCode = (
370
+ params: SendMagicCodeParams,
371
+ ): Promise<SendMagicCodeResponse> => {
372
+ return this.db.sendMagicCode(params);
373
+ };
374
+
375
+ /**
376
+ * Verify a magic code that was sent to the user's email address.
377
+ *
378
+ * @see https://sidekickdb.co/docs/auth
379
+ *
380
+ * @example
381
+ * db.auth.signInWithMagicCode({email: "example@gmail.com", code: "123456"})
382
+ * .catch((err) => console.error(err.body?.message))
383
+ */
384
+ signInWithMagicCode = (
385
+ params: CheckMagicCodeParams,
386
+ ): Promise<CheckMagicCodeResponse> => {
387
+ return this.db.signInWithMagicCode(params);
388
+ };
389
+
390
+ /**
391
+ * Sign in a user with a refresh token
392
+ *
393
+ * @see https://sidekickdb.co/docs/backend#frontend-auth-sign-in-with-token
394
+ *
395
+ * @example
396
+ * // Get the token from your backend
397
+ * const token = await fetch('/signin', ...);
398
+ * //Sign in
399
+ * db.auth.signInWithToken(token);
400
+ */
401
+ signInWithToken = (token: AuthToken): Promise<VerifyResponse> => {
402
+ return this.db.signInWithCustomToken(token);
403
+ };
404
+
405
+ /**
406
+ * Sign in as guest, creating a new user without email
407
+ *
408
+ * @see https://sidekickdb.co/docs/auth
409
+ *
410
+ * @example
411
+ * db.auth.signInAsGuest();
412
+ */
413
+ signInAsGuest = (params?: SignInAsGuestParams): Promise<VerifyResponse> => {
414
+ return this.db.signInAsGuest(params);
415
+ };
416
+
417
+ /**
418
+ * Create an authorization url to sign in with an external provider.
419
+ *
420
+ * @see https://sidekickdb.co/docs/auth
421
+ *
422
+ * @example
423
+ * // Get the authorization url from your backend
424
+ * const url = db.auth.createAuthorizationUrl({
425
+ * clientName: "google",
426
+ * redirectURL: window.location.href,
427
+ * });
428
+ *
429
+ * // Put it in a sign in link
430
+ * <a href={url}>Log in with Google</a>
431
+ */
432
+ createAuthorizationURL = (params: {
433
+ clientName: string;
434
+ redirectURL: string;
435
+ extraFields?: Record<string, any>;
436
+ }): string => {
437
+ return this.db.createAuthorizationURL(params);
438
+ };
439
+
440
+ /**
441
+ * Sign in with the id_token from an external provider like Google
442
+ *
443
+ * @see https://sidekickdb.co/docs/auth
444
+ * @example
445
+ * db.auth
446
+ * .signInWithIdToken({
447
+ * // Token from external service
448
+ * idToken: id_token,
449
+ * // The name you gave the client when you registered it with Instant
450
+ * clientName: "google",
451
+ * // The nonce, if any, that you used when you initiated the auth flow
452
+ * // with the external service.
453
+ * nonce: your_nonce
454
+ * })
455
+ * .catch((err) => console.error(err.body?.message));
456
+ *
457
+ */
458
+ signInWithIdToken = (
459
+ params: SignInWithIdTokenParams,
460
+ ): Promise<CheckMagicCodeResponse> => {
461
+ return this.db.signInWithIdToken(params);
462
+ };
463
+
464
+ /**
465
+ * Sign in with the id_token from an external provider like Google
466
+ *
467
+ * @see https://sidekickdb.co/docs/auth
468
+ * @example
469
+ * db.auth
470
+ * .exchangeOAuthCode({
471
+ * // code received in redirect from OAuth callback
472
+ * code: code
473
+ * // The PKCE code_verifier, if any, that you used when you
474
+ * // initiated the auth flow
475
+ * codeVerifier: your_code_verifier
476
+ * })
477
+ * .catch((err) => console.error(err.body?.message));
478
+ *
479
+ */
480
+ exchangeOAuthCode = (
481
+ params: ExchangeCodeForTokenParams,
482
+ ): Promise<CheckMagicCodeResponse> => {
483
+ return this.db.exchangeCodeForToken(params);
484
+ };
485
+
486
+ /**
487
+ * OpenID Discovery path for use with tools like
488
+ * expo-auth-session that use auto-discovery of
489
+ * OAuth parameters.
490
+ *
491
+ * @see https://sidekickdb.co/docs/auth
492
+ * @example
493
+ * const discovery = useAutoDiscovery(
494
+ * db.auth.issuerURI()
495
+ * );
496
+ */
497
+ issuerURI = (): string => {
498
+ return this.db.issuerURI();
499
+ };
500
+
501
+ /**
502
+ * Sign out the current user
503
+ */
504
+ signOut = (opts: SignoutOpts = { invalidateToken: true }): Promise<void> => {
505
+ return this.db.signOut(opts);
506
+ };
507
+ }
508
+
509
+ type FileOpts = {
510
+ contentType?: string;
511
+ contentDisposition?: string;
512
+ fileSize?: number; // Required for streaming uploads
513
+ };
514
+
515
+ /**
516
+ * Functions to manage file storage.
517
+ */
518
+ class Storage {
519
+ constructor(private db: Reactor) {}
520
+
521
+ /**
522
+ * Uploads file at the provided path.
523
+ *
524
+ * @see https://sidekickdb.co/docs/storage
525
+ * @example
526
+ * const [file] = e.target.files; // result of file input
527
+ * const data = await db.storage.uploadFile('photos/demo.png', file);
528
+ */
529
+ uploadFile = (
530
+ path: string,
531
+ file: File | Blob,
532
+ opts: FileOpts = {},
533
+ ): Promise<UploadFileResponse> => {
534
+ return this.db.uploadFile(path, file, opts);
535
+ };
536
+
537
+ /**
538
+ * Deletes a file by path name.
539
+ *
540
+ * @deprecated Use `db.transact` to delete files instead:
541
+ * @example
542
+ * // Delete by id
543
+ * db.transact(db.tx.$files[fileId].delete());
544
+ *
545
+ * // Delete by path
546
+ * db.transact(db.tx.$files[lookup('path', 'photos/demo.png')].delete());
547
+ *
548
+ * // Delete multiple files
549
+ * db.transact(fileIds.map(id => db.tx.$files[id].delete()));
550
+ *
551
+ * @see https://sidekickdb.co/docs/storage
552
+ */
553
+ delete = (pathname: string) => {
554
+ return this.db.deleteFile(pathname);
555
+ };
556
+
557
+ // Deprecated Storage API (Jan 2025)
558
+ // ---------------------------------
559
+
560
+ /**
561
+ * @deprecated. Use `db.storage.uploadFile` instead
562
+ * remove in the future.
563
+ */
564
+ upload = (pathname: string, file: File) => {
565
+ return this.db.upload(pathname, file);
566
+ };
567
+
568
+ /**
569
+ * @deprecated Use `db.storage.uploadFile` instead
570
+ */
571
+ put = this.upload;
572
+
573
+ /**
574
+ * @deprecated. getDownloadUrl will be removed in the future.
575
+ * Use `useQuery` instead to query and fetch for valid urls
576
+ *
577
+ * db.useQuery({
578
+ * $files: {
579
+ * $: {
580
+ * where: {
581
+ * path: "moop.png"
582
+ * }
583
+ * }
584
+ * }
585
+ * })
586
+ */
587
+ getDownloadUrl = (pathname: string) => {
588
+ return this.db.getDownloadUrl(pathname);
589
+ };
590
+ }
591
+
592
+ type CreateReadStreamOpts = {
593
+ clientId?: string | null | undefined;
594
+ streamId?: string | null | undefined;
595
+ byteOffset?: number | null | undefined;
596
+ ruleParams?: RuleParams | null | undefined;
597
+ };
598
+
599
+ type CreateWriteStreamOpts = {
600
+ clientId: string;
601
+ /**
602
+ * A function that takes a promise and ensures that the current program stays alive
603
+ * until the promise is resolved.
604
+ *
605
+ * Useful in serverless environments to ensure the writer fully flushes before the
606
+ * environment is shut down.
607
+ *
608
+ * @example
609
+ * import { after } from 'next/server'
610
+ * db.streams.createWriteStream({clientId, waitUntil: after})
611
+ */
612
+ waitUntil?: (promise: Promise<any>) => void | null | undefined;
613
+ ruleParams?: RuleParams | null | undefined;
614
+ };
615
+
616
+ /**
617
+ * Functions to manage streams.
618
+ */
619
+ class Streams {
620
+ constructor(private db: Reactor) {}
621
+
622
+ /**
623
+ * Creates a new ReadableStream for the given clientId.
624
+ *
625
+ * @example
626
+ * const stream = db.streams.createReadStream({clientId: clientId})
627
+ * for await (const chunk of stream) {
628
+ * console.log(chunk);
629
+ * }
630
+ */
631
+ createReadStream = (
632
+ opts: CreateReadStreamOpts,
633
+ ): InstantReadableStream<string> => {
634
+ return this.db.createReadStream(opts);
635
+ };
636
+
637
+ /**
638
+ * Creates a new WritableStream for the given clientId.
639
+ *
640
+ * @example
641
+ * const writeStream = db.streams.createWriteStream({clientId: clientId})
642
+ * const writer = writeStream.getWriter();
643
+ * writer.write('Hello world');
644
+ * writer.close();
645
+ */
646
+ createWriteStream = (
647
+ opts: CreateWriteStreamOpts,
648
+ ): InstantWritableStream<string> => {
649
+ return this.db.createWriteStream(opts);
650
+ };
651
+ }
652
+
653
+ // util
654
+
655
+ function coerceQuery(o: any) {
656
+ // stringify and parse to remove undefined values
657
+ return JSON.parse(JSON.stringify(o));
658
+ }
659
+
660
+ class InstantCoreDatabase<
661
+ Schema extends InstantSchemaDef<any, any, any>,
662
+ UseDates extends boolean = false,
663
+ > implements IInstantDatabase<Schema>
664
+ {
665
+ public _reactor: Reactor<RoomsOf<Schema>>;
666
+ public auth: Auth;
667
+ public storage: Storage;
668
+ public streams: Streams;
669
+
670
+ public tx = txInit<Schema>();
671
+
672
+ constructor(reactor: Reactor<RoomsOf<Schema>>) {
673
+ this._reactor = reactor;
674
+ this.auth = new Auth(this._reactor);
675
+ this.storage = new Storage(this._reactor);
676
+ this.streams = new Streams(this._reactor);
677
+ }
678
+
679
+ /**
680
+ * Use this to write data! You can create, update, delete, and link objects
681
+ *
682
+ * @see https://sidekickdb.co/docs/instaml
683
+ *
684
+ * @example
685
+ * // Create a new object in the `goals` namespace
686
+ * const goalId = id();
687
+ * db.transact(db.tx.goals[goalId].update({title: "Get fit"}))
688
+ *
689
+ * // Update the title
690
+ * db.transact(db.tx.goals[goalId].update({title: "Get super fit"}))
691
+ *
692
+ * // Delete it
693
+ * db.transact(db.tx.goals[goalId].delete())
694
+ *
695
+ * // Or create an association:
696
+ * todoId = id();
697
+ * db.transact([
698
+ * db.tx.todos[todoId].update({ title: 'Go on a run' }),
699
+ * db.tx.goals[goalId].link({todos: todoId}),
700
+ * ])
701
+ */
702
+ transact(
703
+ chunks: TransactionChunk<any, any> | TransactionChunk<any, any>[],
704
+ ): Promise<TransactionResult> {
705
+ return this._reactor.pushTx(chunks);
706
+ }
707
+
708
+ getLocalId(name: string): Promise<string> {
709
+ return this._reactor.getLocalId(name);
710
+ }
711
+
712
+ /**
713
+ * Use this to query your data!
714
+ *
715
+ * @see https://sidekickdb.co/docs/instaql
716
+ *
717
+ * @example
718
+ * // listen to all goals
719
+ * db.subscribeQuery({ goals: {} }, (resp) => {
720
+ * console.log(resp.data.goals)
721
+ * })
722
+ *
723
+ * // goals where the title is "Get Fit"
724
+ * db.subscribeQuery(
725
+ * { goals: { $: { where: { title: "Get Fit" } } } },
726
+ * (resp) => {
727
+ * console.log(resp.data.goals)
728
+ * }
729
+ * )
730
+ *
731
+ * // all goals, _alongside_ their todos
732
+ * db.subscribeQuery({ goals: { todos: {} } }, (resp) => {
733
+ * console.log(resp.data.goals)
734
+ * });
735
+ */
736
+ subscribeQuery<
737
+ Q extends ValidQuery<Q, Schema>,
738
+ UseDatesLocal extends boolean = UseDates,
739
+ >(
740
+ query: Q,
741
+ cb: (resp: InstaQLSubscriptionState<Schema, Q, UseDatesLocal>) => void,
742
+ opts?: InstaQLOptions,
743
+ ) {
744
+ return this._reactor.subscribeQuery(query, cb, opts);
745
+ }
746
+
747
+ /**
748
+ * Subscribe to a query and incrementally load more items
749
+ *
750
+ * Only one top level namespace in the query is allowed.
751
+ * @example
752
+ * const { unsubscribe, loadNextPage } = db.subscribeInfiniteQuery({
753
+ * posts: {
754
+ * $: {
755
+ * limit: 20, // Load 20 posts at a time
756
+ * order: {
757
+ * createdAt: 'desc',
758
+ * },
759
+ * },
760
+ * },
761
+ * (resp) => {
762
+ * console.log(resp.data.posts);
763
+ * }
764
+ * });
765
+ */
766
+ subscribeInfiniteQuery<Q extends ValidQuery<Q, Schema>>(
767
+ query: Q,
768
+ cb: (resp: InfiniteQueryCallbackResponse<Schema, Q, UseDates>) => void,
769
+ opts?: InstaQLOptions,
770
+ ): InfiniteQuerySubscription {
771
+ return subscribeInfiniteQuery<Schema, Q, UseDates>(
772
+ this as any,
773
+ query,
774
+ cb,
775
+ opts,
776
+ );
777
+ }
778
+
779
+ /**
780
+ * Listen for the logged in state. This is useful
781
+ * for deciding when to show a login screen.
782
+ *
783
+ * @see https://sidekickdb.co/docs/auth
784
+ * @example
785
+ * const unsub = db.subscribeAuth((auth) => {
786
+ * if (auth.user) {
787
+ * console.log('logged in as', auth.user.email)
788
+ * } else {
789
+ * console.log('logged out')
790
+ * }
791
+ * })
792
+ */
793
+ subscribeAuth(cb: (auth: AuthResult) => void): UnsubscribeFn {
794
+ return this._reactor.subscribeAuth(cb);
795
+ }
796
+
797
+ /**
798
+ * One time query for the logged in state. This is useful
799
+ * for scenarios where you want to know the current auth
800
+ * state without subscribing to changes.
801
+ *
802
+ * @see https://sidekickdb.co/docs/auth
803
+ * @example
804
+ * const user = await db.getAuth();
805
+ * console.log('logged in as', user.email)
806
+ */
807
+ getAuth(): Promise<User | null> {
808
+ return this._reactor.getAuth();
809
+ }
810
+
811
+ /**
812
+ * Listen for connection status changes to Instant. This is useful
813
+ * for building things like connectivity indicators
814
+ *
815
+ * @see https://sidekickdb.co/docs/patterns#connection-status
816
+ * @example
817
+ * const unsub = db.subscribeConnectionStatus((status) => {
818
+ * const connectionState =
819
+ * status === 'connecting' || status === 'opened'
820
+ * ? 'authenticating'
821
+ * : status === 'authenticated'
822
+ * ? 'connected'
823
+ * : status === 'closed'
824
+ * ? 'closed'
825
+ * : status === 'errored'
826
+ * ? 'errored'
827
+ * : 'unexpected state';
828
+ *
829
+ * console.log('Connection status:', connectionState);
830
+ * });
831
+ */
832
+ subscribeConnectionStatus(
833
+ cb: (status: ConnectionStatus) => void,
834
+ ): UnsubscribeFn {
835
+ return this._reactor.subscribeConnectionStatus(cb);
836
+ }
837
+
838
+ /**
839
+ * Observe the app's maintenance-mode state. While read-only, reads and
840
+ * live queries keep working but writes are rejected. The callback fires
841
+ * immediately with the current state and again on every change.
842
+ *
843
+ * @example
844
+ * const unsub = db.subscribeAppStatus(({ isLoading, isReadOnly }) => {
845
+ * if (isLoading) return;
846
+ * if (isReadOnly) showMaintenanceBanner();
847
+ * });
848
+ */
849
+ subscribeAppStatus(cb: (state: AppStatusState) => void): UnsubscribeFn {
850
+ return this._reactor.subscribeAppStatus(cb);
851
+ }
852
+
853
+ /**
854
+ * Join a room to publish and subscribe to topics and presence.
855
+ *
856
+ * @see https://sidekickdb.co/docs/presence-and-topics
857
+ * @example
858
+ * // init
859
+ * const db = init();
860
+ * const room = db.joinRoom(roomType, roomId);
861
+ * // usage
862
+ * const unsubscribeTopic = room.subscribeTopic("foo", console.log);
863
+ * const unsubscribePresence = room.subscribePresence({}, console.log);
864
+ * room.publishTopic("hello", { message: "hello world!" });
865
+ * room.publishPresence({ name: "joe" });
866
+ * // later
867
+ * unsubscribePresence();
868
+ * unsubscribeTopic();
869
+ * room.leaveRoom();
870
+ */
871
+ joinRoom<RoomType extends string & keyof RoomsOf<Schema>>(
872
+ roomType: RoomType = '_defaultRoomType' as RoomType,
873
+ roomId: string = '_defaultRoomId',
874
+ opts?: {
875
+ initialPresence?: Partial<PresenceOf<Schema, RoomType>>;
876
+ },
877
+ ): RoomHandle<PresenceOf<Schema, RoomType>, TopicsOf<Schema, RoomType>> {
878
+ const leaveRoom = this._reactor.joinRoom(
879
+ roomType,
880
+ roomId,
881
+ opts?.initialPresence,
882
+ );
883
+
884
+ return {
885
+ leaveRoom,
886
+ subscribeTopic: (topic, onEvent) =>
887
+ this._reactor.subscribeTopic(roomType, roomId, topic, onEvent),
888
+ subscribePresence: (opts, onChange) =>
889
+ this._reactor.subscribePresence(roomType, roomId, opts, onChange),
890
+ publishTopic: (topic, data) =>
891
+ this._reactor.publishTopic({ roomType, roomId, topic, data }),
892
+ publishPresence: (data) =>
893
+ this._reactor.publishPresence(roomType, roomId, data),
894
+ getPresence: (opts) => this._reactor.getPresence(roomType, roomId, opts),
895
+ };
896
+ }
897
+
898
+ shutdown() {
899
+ delete globalInstantCoreStore[reactorKey(this._reactor.config)];
900
+ this._reactor.shutdown();
901
+ }
902
+
903
+ /**
904
+ * Use this for one-off queries.
905
+ * Returns local data if available, otherwise fetches from the server.
906
+ * Because we want to avoid stale data, this method will throw an error
907
+ * if the user is offline or there is no active connection to the server.
908
+ *
909
+ * @see https://sidekickdb.co/docs/instaql
910
+ *
911
+ * @example
912
+ *
913
+ * const resp = await db.queryOnce({ goals: {} });
914
+ * console.log(resp.data.goals)
915
+ */
916
+ queryOnce<Q extends ValidQuery<Q, Schema>>(
917
+ query: Q,
918
+ opts?: InstaQLOptions,
919
+ ): Promise<{
920
+ data: InstaQLResponse<Schema, Q, UseDates>;
921
+ pageInfo: PageInfoResponse<Q>;
922
+ }> {
923
+ return this._reactor.queryOnce(query, opts);
924
+ }
925
+
926
+ /**
927
+ * @deprecated This is an experimental function that is not yet ready for production use.
928
+ * Use this function to sync an entire namespace.
929
+ * It has many limitations that will be removed in the future:
930
+ * 1. Must be used with an admin token
931
+ * 2. Does not support permissions
932
+ * 3. Does not support where clauses
933
+ * 4. Does not support links
934
+ * It also does not support multiple top-level namespaces. For example,
935
+ * {posts: {}, users: {}} is invalid. Only `posts` or `users` is allowed, but not both.
936
+ */
937
+ _syncTableExperimental<Q extends ValidQuery<Q, Schema>>(
938
+ query: ExactlyOne<Q>,
939
+ cb: SyncTableCallback<Schema, Q, UseDates>,
940
+ ): (
941
+ opts?: { keepSubscription: boolean | null | undefined } | null | undefined,
942
+ ) => void {
943
+ return this._reactor.subscribeTable(query, cb);
944
+ }
945
+ }
946
+
947
+ function schemaHash(schema?: InstantSchemaDef<any, any, any>): string {
948
+ if (!schema) {
949
+ return '0';
950
+ }
951
+
952
+ const fromStore = schemaHashStore.get(schema);
953
+ if (fromStore) {
954
+ return fromStore;
955
+ }
956
+ const hash = weakHash(schema);
957
+ schemaHashStore.set(schema, hash);
958
+ return hash;
959
+ }
960
+
961
+ function schemaChanged(
962
+ existingClient: InstantCoreDatabase<any, boolean>,
963
+ newSchema?: InstantSchemaDef<any, any, any>,
964
+ ): boolean {
965
+ return (
966
+ schemaHash(existingClient._reactor.config.schema) !== schemaHash(newSchema)
967
+ );
968
+ }
969
+
970
+ /**
971
+ *
972
+ * The first step: init your application!
973
+ *
974
+ * Find your `appId` in your SidekickDB dashboard.
975
+ *
976
+ * @example
977
+ * import { init } from "@sidekickdb/core"
978
+ *
979
+ * const db = init({
980
+ * appId: "my-app-id",
981
+ * apiURI: "https://sidekick.example.com",
982
+ * })
983
+ *
984
+ * // You can also provide a schema for type safety and editor autocomplete!
985
+ *
986
+ * import { init } from "@sidekickdb/core"
987
+ * import schema from ""../instant.schema.ts";
988
+ *
989
+ * const db = init({
990
+ * appId: "my-app-id",
991
+ * apiURI: "https://sidekick.example.com",
992
+ * schema,
993
+ * })
994
+ *
995
+ * // To learn more: https://sidekickdb.co/docs/modeling-data
996
+ */
997
+ function init<
998
+ Schema extends InstantSchemaDef<any, any, any> = InstantUnknownSchema,
999
+ UseDates extends boolean = false,
1000
+ >(
1001
+ // Allows config with missing `useDateObjects`, but keeps `UseDates`
1002
+ // as a non-nullable in the InstantConfig type.
1003
+ config: Omit<InstantConfig<Schema, UseDates>, 'useDateObjects'> & {
1004
+ useDateObjects?: UseDates;
1005
+ },
1006
+ Store?: any,
1007
+ NetworkListener?: any,
1008
+ versions?: { [key: string]: string },
1009
+ EventSourceImpl?: any,
1010
+ ): InstantCoreDatabase<Schema, UseDates> {
1011
+ const apiURI = requireApiURI(config.apiURI, { target: 'init()' });
1012
+ const configStrict = {
1013
+ ...config,
1014
+ appId: config.appId?.trim(),
1015
+ apiURI,
1016
+ websocketURI: config.websocketURI || websocketURIFromApiURI(apiURI),
1017
+ useDateObjects: (config.useDateObjects ?? false) as UseDates,
1018
+ };
1019
+ const existingClient = globalInstantCoreStore[
1020
+ reactorKey(configStrict)
1021
+ ] as InstantCoreDatabase<any, UseDates>;
1022
+
1023
+ if (existingClient) {
1024
+ if (schemaChanged(existingClient, configStrict.schema)) {
1025
+ existingClient._reactor.updateSchema(configStrict.schema);
1026
+ }
1027
+ return existingClient;
1028
+ }
1029
+
1030
+ const reactor = new Reactor<RoomsOf<Schema>>(
1031
+ {
1032
+ ...configStrict,
1033
+ cardinalityInference: configStrict.schema ? true : false,
1034
+ },
1035
+ Store || IndexedDBStorage,
1036
+ NetworkListener || WindowNetworkListener,
1037
+ { ...(versions || {}), '@instantdb/core': version },
1038
+ EventSourceImpl,
1039
+ );
1040
+
1041
+ const client = new InstantCoreDatabase<any, UseDates>(reactor);
1042
+ globalInstantCoreStore[reactorKey(configStrict)] = client;
1043
+
1044
+ handleDevtool(configStrict.appId, configStrict.devtool);
1045
+
1046
+ return client;
1047
+ }
1048
+
1049
+ function handleDevtool(
1050
+ appId: string,
1051
+ devtool: boolean | DevtoolConfig | null | undefined,
1052
+ ) {
1053
+ if (
1054
+ typeof window === 'undefined' ||
1055
+ typeof window.location === 'undefined' ||
1056
+ typeof document === 'undefined'
1057
+ ) {
1058
+ return;
1059
+ }
1060
+
1061
+ if (typeof devtool === 'boolean' && !devtool) {
1062
+ return;
1063
+ }
1064
+
1065
+ const config: StrictDevtoolConfig = {
1066
+ position: 'bottom-right' as const,
1067
+ allowedHosts: ['localhost'],
1068
+ ...(typeof devtool === 'object' ? devtool : {}),
1069
+ };
1070
+
1071
+ if (!config.allowedHosts.includes(window.location.hostname)) {
1072
+ return;
1073
+ }
1074
+
1075
+ createDevtool(appId, config);
1076
+ }
1077
+
1078
+ /**
1079
+ * @deprecated
1080
+ * `init_experimental` is deprecated. You can replace it with `init`.
1081
+ *
1082
+ * @example
1083
+ *
1084
+ * // Before
1085
+ * import { init_experimental } from "@sidekickdb/core"
1086
+ * const db = init_experimental({ ... });
1087
+ *
1088
+ * // After
1089
+ * import { init } from "@sidekickdb/core"
1090
+ * const db = init({ ... });
1091
+ */
1092
+ const init_experimental = init;
1093
+
1094
+ export {
1095
+ // bada bing bada boom
1096
+ init,
1097
+ init_experimental,
1098
+ id,
1099
+ tx,
1100
+ txInit,
1101
+ lookup,
1102
+ validateQuery,
1103
+ QueryValidationError,
1104
+ validateTransactions,
1105
+ parseSchemaFromJSON,
1106
+ TransactionValidationError,
1107
+ FrameworkClient,
1108
+ Reactor,
1109
+
1110
+ // error
1111
+ InstantAPIError,
1112
+
1113
+ // warnings
1114
+ setInstantWarningsEnabled,
1115
+
1116
+ // cli
1117
+ i,
1118
+
1119
+ // util
1120
+ getOps,
1121
+ coerceQuery,
1122
+ weakHash,
1123
+ coerceToDate,
1124
+ IndexedDBStorage,
1125
+ WindowNetworkListener,
1126
+ InstantCoreDatabase,
1127
+ Auth,
1128
+ Storage,
1129
+ Streams,
1130
+ version,
1131
+ InstantError,
1132
+ MissingApiURIError,
1133
+ requireApiURI,
1134
+ websocketURIFromApiURI,
1135
+
1136
+ // infinite query
1137
+ getInfiniteQueryInitialSnapshot,
1138
+
1139
+ // sync table enums
1140
+ SyncTableCallbackEventType,
1141
+
1142
+ // og types
1143
+ type IDatabase,
1144
+ type RoomSchemaShape,
1145
+ type Query,
1146
+ type QueryResponse,
1147
+ type InstaQLResponse,
1148
+ type PageInfoResponse,
1149
+ type InstantObject,
1150
+ type Exactly,
1151
+ type TransactionChunk,
1152
+ type AuthState,
1153
+ type ConnectionStatus,
1154
+ type AppStatusState,
1155
+ type User,
1156
+ type AuthToken,
1157
+ type TxChunk,
1158
+ type SubscriptionState,
1159
+ type InstaQLSubscriptionState,
1160
+ type LifecycleSubscriptionState,
1161
+ type InstaQLLifecycleState,
1162
+
1163
+ // presence types
1164
+ type PresenceOpts,
1165
+ type PresenceSlice,
1166
+ type PresenceResponse,
1167
+ type PresencePeer,
1168
+
1169
+ // new query types
1170
+ type InstaQLParams,
1171
+ type ValidQuery,
1172
+ type Cursor,
1173
+ type InstaQLOptions,
1174
+ type InstaQLQueryParams,
1175
+ type InstantQuery,
1176
+ type InstantQueryResult,
1177
+ type InstantSchema,
1178
+ type InstantEntity,
1179
+ type InstantSchemaDatabase,
1180
+ type InstaQLFields,
1181
+ type Order,
1182
+ type InstaQLQueryEntityResult,
1183
+ type InfiniteQueryCallbackResponse,
1184
+ type InfiniteQuerySubscription,
1185
+
1186
+ // schema types
1187
+ type AttrsDefs,
1188
+ type CardinalityKind,
1189
+ type DataAttrDef,
1190
+ type EntitiesDef,
1191
+ type InstantUnknownSchemaDef,
1192
+ type EntitiesWithLinks,
1193
+ type EntityDef,
1194
+ type RoomsDef,
1195
+ type InstantGraph,
1196
+ type LinkAttrDef,
1197
+ type LinkDef,
1198
+ type LinksDef,
1199
+ type ResolveAttrs,
1200
+ type ValueTypes,
1201
+ type RoomsOf,
1202
+ type PresenceOf,
1203
+ type TopicsOf,
1204
+ type TopicOf,
1205
+ type InstaQLEntity,
1206
+ type InstaQLResult,
1207
+ type InstaQLEntitySubquery,
1208
+ type InstantSchemaDef,
1209
+ type InstantUnknownSchema,
1210
+ type IInstantDatabase,
1211
+ type BackwardsCompatibleSchema,
1212
+ type InstantRules,
1213
+ type UpdateParams,
1214
+ type LinkParams,
1215
+ type CreateParams,
1216
+ type RuleParams,
1217
+
1218
+ // attr types
1219
+ type InstantDBAttr,
1220
+ type InstantDBAttrOnDelete,
1221
+ type InstantDBCheckedDataType,
1222
+ type InstantDBIdent,
1223
+ type InstantDBInferredType,
1224
+
1225
+ // auth types
1226
+ type CheckMagicCodeParams,
1227
+ type CheckMagicCodeResponse,
1228
+ type ExchangeCodeForTokenParams,
1229
+ type SendMagicCodeParams,
1230
+ type SendMagicCodeResponse,
1231
+ type SignInAsGuestParams,
1232
+ type SignInWithIdTokenParams,
1233
+ type VerifyMagicCodeParams,
1234
+ type VerifyResponse,
1235
+
1236
+ // storage types
1237
+ type FileOpts,
1238
+ type UploadFileResponse,
1239
+ type DeleteFileResponse,
1240
+
1241
+ // SSE
1242
+ SSEConnection,
1243
+ type EventSourceType,
1244
+ type FrameworkConfig,
1245
+ type EventSourceConstructor,
1246
+
1247
+ // streams
1248
+ InstantStream,
1249
+ type WritableStreamCtor,
1250
+ type ReadableStreamCtor,
1251
+ type CreateReadStreamOpts,
1252
+ type CreateWriteStreamOpts,
1253
+ type InstantWritableStream,
1254
+ type InstantReadableStream,
1255
+
1256
+ // sync table types
1257
+ type SyncTableCallback,
1258
+ type SyncTableCallbackEvent,
1259
+ type SyncTableInitialSyncBatch,
1260
+ type SyncTableInitialSyncComplete,
1261
+ type SyncTableSyncTransaction,
1262
+ type SyncTableLoadFromStorage,
1263
+ type SyncTableSetupError,
1264
+
1265
+ // error types
1266
+ type InstantIssue,
1267
+
1268
+ // store (e.g. indexeddb) interface
1269
+ StoreInterface,
1270
+ type StoreInterfaceClass,
1271
+ type StoreInterfaceStoreName,
1272
+ createInstantRouteHandler,
1273
+ type InstantRouteHandlerPayloadByType,
1274
+ type InstantRouteHandlerType,
1275
+ type InstantRouteHandlerBody,
1276
+ type Logger,
1277
+ };
1278
+
1279
+ /** @deprecated Use StoreInterface instead */
1280
+ export const StorageInterface = StoreInterface;
1281
+
1282
+ /** @deprecated Use StoreInterfaceStoreName instead */
1283
+ export type StorageInterfaceStoreName = StoreInterfaceStoreName;