@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.
- package/.tshy/build.json +8 -0
- package/.tshy/commonjs.json +16 -0
- package/.tshy/esm.json +15 -0
- package/LICENSE.md +202 -0
- package/NOTICE +27 -0
- package/README.md +25 -2
- package/__example__/standalone.esm.html +23 -0
- package/__example__/standalone.umd.html +21 -0
- package/__tests__/src/Reactor.test.ts +480 -0
- package/__tests__/src/auth-extra-fields.e2e.test.ts +160 -0
- package/__tests__/src/cookieSync.e2e.test.ts +165 -0
- package/__tests__/src/data/movies/attrs.json +145 -0
- package/__tests__/src/data/movies/triples.json +1507 -0
- package/__tests__/src/data/zeneca/attrs.json +251 -0
- package/__tests__/src/data/zeneca/triples.json +15854 -0
- package/__tests__/src/dataForQueryMemo.test.ts +136 -0
- package/__tests__/src/datalog.test.ts +175 -0
- package/__tests__/src/devtool.test.ts +48 -0
- package/__tests__/src/endpoints.test.ts +228 -0
- package/__tests__/src/infiniteQuery.e2e.test.ts +454 -0
- package/__tests__/src/instaml.test.ts +1593 -0
- package/__tests__/src/instaql.bench.ts +34 -0
- package/__tests__/src/instaql.test.ts +1388 -0
- package/__tests__/src/instaqlInference.test.ts +243 -0
- package/__tests__/src/notifyScope.test.ts +1145 -0
- package/__tests__/src/queryValidation.test.ts +1033 -0
- package/__tests__/src/schema.test.ts +641 -0
- package/__tests__/src/serializeSchema.test.ts +123 -0
- package/__tests__/src/simple.e2e.test.ts +10 -0
- package/__tests__/src/storageUpload.test.ts +70 -0
- package/__tests__/src/store.test.ts +718 -0
- package/__tests__/src/transactionValidation.test.ts +450 -0
- package/__tests__/src/typeUtils.ts +39 -0
- package/__tests__/src/utils/PersistedObject.test.ts +529 -0
- package/__tests__/src/utils/dates.test.ts +82 -0
- package/__tests__/src/utils/e2e.ts +95 -0
- package/__tests__/src/utils/msw.ts +3 -0
- package/__tests__/src/utils/object.test.ts +78 -0
- package/__tests__/src/utils/weakHash.test.ts +68 -0
- package/__tests__/src/utils/weakHashLegacy.test.ts +22 -0
- package/__tests__/src/vite.env.d.ts +1 -0
- package/dist/commonjs/Connection.d.ts +93 -0
- package/dist/commonjs/Connection.d.ts.map +1 -0
- package/dist/commonjs/Connection.js +194 -0
- package/dist/commonjs/Connection.js.map +1 -0
- package/dist/commonjs/InMemoryStorage.d.ts +11 -0
- package/dist/commonjs/InMemoryStorage.d.ts.map +1 -0
- package/dist/commonjs/InMemoryStorage.js +29 -0
- package/dist/commonjs/InMemoryStorage.js.map +1 -0
- package/dist/commonjs/IndexedDBStorage.d.ts +18 -0
- package/dist/commonjs/IndexedDBStorage.d.ts.map +1 -0
- package/dist/commonjs/IndexedDBStorage.js +341 -0
- package/dist/commonjs/IndexedDBStorage.js.map +1 -0
- package/dist/commonjs/InstantError.d.ts +7 -0
- package/dist/commonjs/InstantError.d.ts.map +1 -0
- package/dist/commonjs/InstantError.js +28 -0
- package/dist/commonjs/InstantError.js.map +1 -0
- package/dist/commonjs/Reactor.d.ts +445 -0
- package/dist/commonjs/Reactor.d.ts.map +1 -0
- package/dist/commonjs/Reactor.js +2471 -0
- package/dist/commonjs/Reactor.js.map +1 -0
- package/dist/commonjs/StorageAPI.d.ts +40 -0
- package/dist/commonjs/StorageAPI.d.ts.map +1 -0
- package/dist/commonjs/StorageAPI.js +82 -0
- package/dist/commonjs/StorageAPI.js.map +1 -0
- package/dist/commonjs/Stream.d.ts +121 -0
- package/dist/commonjs/Stream.d.ts.map +1 -0
- package/dist/commonjs/Stream.js +819 -0
- package/dist/commonjs/Stream.js.map +1 -0
- package/dist/commonjs/SyncTable.d.ts +145 -0
- package/dist/commonjs/SyncTable.d.ts.map +1 -0
- package/dist/commonjs/SyncTable.js +564 -0
- package/dist/commonjs/SyncTable.js.map +1 -0
- package/dist/commonjs/WindowNetworkListener.d.ts +5 -0
- package/dist/commonjs/WindowNetworkListener.d.ts.map +1 -0
- package/dist/commonjs/WindowNetworkListener.js +23 -0
- package/dist/commonjs/WindowNetworkListener.js.map +1 -0
- package/dist/commonjs/__types__/fieldsTypeTest.d.ts +2 -0
- package/dist/commonjs/__types__/fieldsTypeTest.d.ts.map +1 -0
- package/dist/commonjs/__types__/fieldsTypeTest.js +68 -0
- package/dist/commonjs/__types__/fieldsTypeTest.js.map +1 -0
- package/dist/commonjs/__types__/typeUtils.d.ts +20 -0
- package/dist/commonjs/__types__/typeUtils.d.ts.map +1 -0
- package/dist/commonjs/__types__/typeUtils.js +5 -0
- package/dist/commonjs/__types__/typeUtils.js.map +1 -0
- package/dist/commonjs/__types__/useDatesTypeTest.d.ts +2 -0
- package/dist/commonjs/__types__/useDatesTypeTest.d.ts.map +1 -0
- package/dist/commonjs/__types__/useDatesTypeTest.js +55 -0
- package/dist/commonjs/__types__/useDatesTypeTest.js.map +1 -0
- package/dist/commonjs/attrTypes.d.ts +26 -0
- package/dist/commonjs/attrTypes.d.ts.map +1 -0
- package/dist/commonjs/attrTypes.js +3 -0
- package/dist/commonjs/attrTypes.js.map +1 -0
- package/dist/commonjs/authAPI.d.ts +65 -0
- package/dist/commonjs/authAPI.d.ts.map +1 -0
- package/dist/commonjs/authAPI.js +114 -0
- package/dist/commonjs/authAPI.js.map +1 -0
- package/dist/commonjs/clientTypes.d.ts +41 -0
- package/dist/commonjs/clientTypes.d.ts.map +1 -0
- package/dist/commonjs/clientTypes.js +3 -0
- package/dist/commonjs/clientTypes.js.map +1 -0
- package/dist/commonjs/coreTypes.d.ts +34 -0
- package/dist/commonjs/coreTypes.d.ts.map +1 -0
- package/dist/commonjs/coreTypes.js +3 -0
- package/dist/commonjs/coreTypes.js.map +1 -0
- package/dist/commonjs/createRouteHandler.d.ts +8 -0
- package/dist/commonjs/createRouteHandler.d.ts.map +1 -0
- package/dist/commonjs/createRouteHandler.js +56 -0
- package/dist/commonjs/createRouteHandler.js.map +1 -0
- package/dist/commonjs/datalog.d.ts +8 -0
- package/dist/commonjs/datalog.d.ts.map +1 -0
- package/dist/commonjs/datalog.js +106 -0
- package/dist/commonjs/datalog.js.map +1 -0
- package/dist/commonjs/devtool.d.ts +4 -0
- package/dist/commonjs/devtool.d.ts.map +1 -0
- package/dist/commonjs/devtool.js +191 -0
- package/dist/commonjs/devtool.js.map +1 -0
- package/dist/commonjs/endpoints.d.ts +35 -0
- package/dist/commonjs/endpoints.d.ts.map +1 -0
- package/dist/commonjs/endpoints.js +80 -0
- package/dist/commonjs/endpoints.js.map +1 -0
- package/dist/commonjs/framework.d.ts +81 -0
- package/dist/commonjs/framework.d.ts.map +1 -0
- package/dist/commonjs/framework.js +284 -0
- package/dist/commonjs/framework.js.map +1 -0
- package/dist/commonjs/helperTypes.d.ts +76 -0
- package/dist/commonjs/helperTypes.d.ts.map +1 -0
- package/dist/commonjs/helperTypes.js +3 -0
- package/dist/commonjs/helperTypes.js.map +1 -0
- package/dist/commonjs/index.d.ts +614 -0
- package/dist/commonjs/index.d.ts.map +1 -0
- package/dist/commonjs/index.js +677 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/infiniteQuery.d.ts +26 -0
- package/dist/commonjs/infiniteQuery.d.ts.map +1 -0
- package/dist/commonjs/infiniteQuery.js +438 -0
- package/dist/commonjs/infiniteQuery.js.map +1 -0
- package/dist/commonjs/instaml.d.ts +18 -0
- package/dist/commonjs/instaml.d.ts.map +1 -0
- package/dist/commonjs/instaml.js +584 -0
- package/dist/commonjs/instaml.js.map +1 -0
- package/dist/commonjs/instaql.d.ts +13 -0
- package/dist/commonjs/instaql.d.ts.map +1 -0
- package/dist/commonjs/instaql.js +670 -0
- package/dist/commonjs/instaql.js.map +1 -0
- package/dist/commonjs/instatx.d.ts +128 -0
- package/dist/commonjs/instatx.d.ts.map +1 -0
- package/dist/commonjs/instatx.js +102 -0
- package/dist/commonjs/instatx.js.map +1 -0
- package/dist/commonjs/model/instaqlResult.d.ts +8 -0
- package/dist/commonjs/model/instaqlResult.d.ts.map +1 -0
- package/dist/commonjs/model/instaqlResult.js +28 -0
- package/dist/commonjs/model/instaqlResult.js.map +1 -0
- package/dist/commonjs/notifyScope.d.ts +32 -0
- package/dist/commonjs/notifyScope.d.ts.map +1 -0
- package/dist/commonjs/notifyScope.js +306 -0
- package/dist/commonjs/notifyScope.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/parseSchemaFromJSON.d.ts +3 -0
- package/dist/commonjs/parseSchemaFromJSON.d.ts.map +1 -0
- package/dist/commonjs/parseSchemaFromJSON.js +147 -0
- package/dist/commonjs/parseSchemaFromJSON.js.map +1 -0
- package/dist/commonjs/presence.d.ts +51 -0
- package/dist/commonjs/presence.d.ts.map +1 -0
- package/dist/commonjs/presence.js +55 -0
- package/dist/commonjs/presence.js.map +1 -0
- package/dist/commonjs/presenceTypes.d.ts +10 -0
- package/dist/commonjs/presenceTypes.d.ts.map +1 -0
- package/dist/commonjs/presenceTypes.js +5 -0
- package/dist/commonjs/presenceTypes.js.map +1 -0
- package/dist/commonjs/queryTypes.d.ts +272 -0
- package/dist/commonjs/queryTypes.d.ts.map +1 -0
- package/dist/commonjs/queryTypes.js +5 -0
- package/dist/commonjs/queryTypes.js.map +1 -0
- package/dist/commonjs/queryValidation.d.ts +6 -0
- package/dist/commonjs/queryValidation.d.ts.map +1 -0
- package/dist/commonjs/queryValidation.js +293 -0
- package/dist/commonjs/queryValidation.js.map +1 -0
- package/dist/commonjs/reactorTypes.d.ts +30 -0
- package/dist/commonjs/reactorTypes.d.ts.map +1 -0
- package/dist/commonjs/reactorTypes.js +3 -0
- package/dist/commonjs/reactorTypes.js.map +1 -0
- package/dist/commonjs/routeHandlerProtocol.d.ts +20 -0
- package/dist/commonjs/routeHandlerProtocol.d.ts.map +1 -0
- package/dist/commonjs/routeHandlerProtocol.js +3 -0
- package/dist/commonjs/routeHandlerProtocol.js.map +1 -0
- package/dist/commonjs/rulesTypes.d.ts +86 -0
- package/dist/commonjs/rulesTypes.d.ts.map +1 -0
- package/dist/commonjs/rulesTypes.js +3 -0
- package/dist/commonjs/rulesTypes.js.map +1 -0
- package/dist/commonjs/sanityCheckQueries.d.ts +19 -0
- package/dist/commonjs/sanityCheckQueries.d.ts.map +1 -0
- package/dist/commonjs/sanityCheckQueries.js +156 -0
- package/dist/commonjs/sanityCheckQueries.js.map +1 -0
- package/dist/commonjs/schema.d.ts +73 -0
- package/dist/commonjs/schema.d.ts.map +1 -0
- package/dist/commonjs/schema.js +132 -0
- package/dist/commonjs/schema.js.map +1 -0
- package/dist/commonjs/schemaTypes.d.ts +245 -0
- package/dist/commonjs/schemaTypes.d.ts.map +1 -0
- package/dist/commonjs/schemaTypes.js +116 -0
- package/dist/commonjs/schemaTypes.js.map +1 -0
- package/dist/commonjs/store.d.ts +90 -0
- package/dist/commonjs/store.d.ts.map +1 -0
- package/dist/commonjs/store.js +745 -0
- package/dist/commonjs/store.js.map +1 -0
- package/dist/commonjs/transactionValidation.d.ts +8 -0
- package/dist/commonjs/transactionValidation.d.ts.map +1 -0
- package/dist/commonjs/transactionValidation.js +137 -0
- package/dist/commonjs/transactionValidation.js.map +1 -0
- package/dist/commonjs/utils/Deferred.d.ts +9 -0
- package/dist/commonjs/utils/Deferred.d.ts.map +1 -0
- package/dist/commonjs/utils/Deferred.js +22 -0
- package/dist/commonjs/utils/Deferred.js.map +1 -0
- package/dist/commonjs/utils/PersistedObject.d.ts +81 -0
- package/dist/commonjs/utils/PersistedObject.d.ts.map +1 -0
- package/dist/commonjs/utils/PersistedObject.js +469 -0
- package/dist/commonjs/utils/PersistedObject.js.map +1 -0
- package/dist/commonjs/utils/dates.d.ts +2 -0
- package/dist/commonjs/utils/dates.d.ts.map +1 -0
- package/dist/commonjs/utils/dates.js +215 -0
- package/dist/commonjs/utils/dates.js.map +1 -0
- package/dist/commonjs/utils/error.d.ts +3 -0
- package/dist/commonjs/utils/error.d.ts.map +1 -0
- package/dist/commonjs/utils/error.js +13 -0
- package/dist/commonjs/utils/error.js.map +1 -0
- package/dist/commonjs/utils/fetch.d.ts +126 -0
- package/dist/commonjs/utils/fetch.d.ts.map +1 -0
- package/dist/commonjs/utils/fetch.js +37 -0
- package/dist/commonjs/utils/fetch.js.map +1 -0
- package/dist/commonjs/utils/flags.d.ts +5 -0
- package/dist/commonjs/utils/flags.d.ts.map +1 -0
- package/dist/commonjs/utils/flags.js +16 -0
- package/dist/commonjs/utils/flags.js.map +1 -0
- package/dist/commonjs/utils/id.d.ts +4 -0
- package/dist/commonjs/utils/id.d.ts.map +1 -0
- package/dist/commonjs/utils/id.js +29 -0
- package/dist/commonjs/utils/id.js.map +1 -0
- package/dist/commonjs/utils/linkIndex.d.ts +8 -0
- package/dist/commonjs/utils/linkIndex.d.ts.map +1 -0
- package/dist/commonjs/utils/linkIndex.js +21 -0
- package/dist/commonjs/utils/linkIndex.js.map +1 -0
- package/dist/commonjs/utils/log.d.ts +7 -0
- package/dist/commonjs/utils/log.d.ts.map +1 -0
- package/dist/commonjs/utils/log.js +17 -0
- package/dist/commonjs/utils/log.js.map +1 -0
- package/dist/commonjs/utils/object.d.ts +17 -0
- package/dist/commonjs/utils/object.d.ts.map +1 -0
- package/dist/commonjs/utils/object.js +157 -0
- package/dist/commonjs/utils/object.js.map +1 -0
- package/dist/commonjs/utils/pgtime.d.ts +198 -0
- package/dist/commonjs/utils/pgtime.d.ts.map +1 -0
- package/dist/commonjs/utils/pgtime.js +201 -0
- package/dist/commonjs/utils/pgtime.js.map +1 -0
- package/dist/commonjs/utils/pick.d.ts +2 -0
- package/dist/commonjs/utils/pick.d.ts.map +1 -0
- package/dist/commonjs/utils/pick.js +13 -0
- package/dist/commonjs/utils/pick.js.map +1 -0
- package/dist/commonjs/utils/strings.d.ts +2 -0
- package/dist/commonjs/utils/strings.d.ts.map +1 -0
- package/dist/commonjs/utils/strings.js +19 -0
- package/dist/commonjs/utils/strings.js.map +1 -0
- package/dist/commonjs/utils/weakHash.d.ts +16 -0
- package/dist/commonjs/utils/weakHash.d.ts.map +1 -0
- package/dist/commonjs/utils/weakHash.js +75 -0
- package/dist/commonjs/utils/weakHash.js.map +1 -0
- package/dist/commonjs/utils/weakHashLegacy.d.ts +8 -0
- package/dist/commonjs/utils/weakHashLegacy.d.ts.map +1 -0
- package/dist/commonjs/utils/weakHashLegacy.js +76 -0
- package/dist/commonjs/utils/weakHashLegacy.js.map +1 -0
- package/dist/commonjs/version.d.ts +3 -0
- package/dist/commonjs/version.d.ts.map +1 -0
- package/dist/commonjs/version.js +5 -0
- package/dist/commonjs/version.js.map +1 -0
- package/dist/commonjs/warningToggle.d.ts +3 -0
- package/dist/commonjs/warningToggle.d.ts.map +1 -0
- package/dist/commonjs/warningToggle.js +9 -0
- package/dist/commonjs/warningToggle.js.map +1 -0
- package/dist/esm/Connection.d.ts +93 -0
- package/dist/esm/Connection.d.ts.map +1 -0
- package/dist/esm/Connection.js +189 -0
- package/dist/esm/Connection.js.map +1 -0
- package/dist/esm/InMemoryStorage.d.ts +11 -0
- package/dist/esm/InMemoryStorage.d.ts.map +1 -0
- package/dist/esm/InMemoryStorage.js +26 -0
- package/dist/esm/InMemoryStorage.js.map +1 -0
- package/dist/esm/IndexedDBStorage.d.ts +18 -0
- package/dist/esm/IndexedDBStorage.d.ts.map +1 -0
- package/dist/esm/IndexedDBStorage.js +338 -0
- package/dist/esm/IndexedDBStorage.js.map +1 -0
- package/dist/esm/InstantError.d.ts +7 -0
- package/dist/esm/InstantError.d.ts.map +1 -0
- package/dist/esm/InstantError.js +24 -0
- package/dist/esm/InstantError.js.map +1 -0
- package/dist/esm/Reactor.d.ts +445 -0
- package/dist/esm/Reactor.d.ts.map +1 -0
- package/dist/esm/Reactor.js +2431 -0
- package/dist/esm/Reactor.js.map +1 -0
- package/dist/esm/StorageAPI.d.ts +40 -0
- package/dist/esm/StorageAPI.d.ts.map +1 -0
- package/dist/esm/StorageAPI.js +75 -0
- package/dist/esm/StorageAPI.js.map +1 -0
- package/dist/esm/Stream.d.ts +121 -0
- package/dist/esm/Stream.d.ts.map +1 -0
- package/dist/esm/Stream.js +812 -0
- package/dist/esm/Stream.js.map +1 -0
- package/dist/esm/SyncTable.d.ts +145 -0
- package/dist/esm/SyncTable.d.ts.map +1 -0
- package/dist/esm/SyncTable.js +524 -0
- package/dist/esm/SyncTable.js.map +1 -0
- package/dist/esm/WindowNetworkListener.d.ts +5 -0
- package/dist/esm/WindowNetworkListener.d.ts.map +1 -0
- package/dist/esm/WindowNetworkListener.js +20 -0
- package/dist/esm/WindowNetworkListener.js.map +1 -0
- package/dist/esm/__types__/fieldsTypeTest.d.ts +2 -0
- package/dist/esm/__types__/fieldsTypeTest.d.ts.map +1 -0
- package/dist/esm/__types__/fieldsTypeTest.js +66 -0
- package/dist/esm/__types__/fieldsTypeTest.js.map +1 -0
- package/dist/esm/__types__/typeUtils.d.ts +20 -0
- package/dist/esm/__types__/typeUtils.d.ts.map +1 -0
- package/dist/esm/__types__/typeUtils.js +4 -0
- package/dist/esm/__types__/typeUtils.js.map +1 -0
- package/dist/esm/__types__/useDatesTypeTest.d.ts +2 -0
- package/dist/esm/__types__/useDatesTypeTest.d.ts.map +1 -0
- package/dist/esm/__types__/useDatesTypeTest.js +53 -0
- package/dist/esm/__types__/useDatesTypeTest.js.map +1 -0
- package/dist/esm/attrTypes.d.ts +26 -0
- package/dist/esm/attrTypes.d.ts.map +1 -0
- package/dist/esm/attrTypes.js +2 -0
- package/dist/esm/attrTypes.js.map +1 -0
- package/dist/esm/authAPI.d.ts +65 -0
- package/dist/esm/authAPI.d.ts.map +1 -0
- package/dist/esm/authAPI.js +104 -0
- package/dist/esm/authAPI.js.map +1 -0
- package/dist/esm/clientTypes.d.ts +41 -0
- package/dist/esm/clientTypes.d.ts.map +1 -0
- package/dist/esm/clientTypes.js +2 -0
- package/dist/esm/clientTypes.js.map +1 -0
- package/dist/esm/coreTypes.d.ts +34 -0
- package/dist/esm/coreTypes.d.ts.map +1 -0
- package/dist/esm/coreTypes.js +2 -0
- package/dist/esm/coreTypes.js.map +1 -0
- package/dist/esm/createRouteHandler.d.ts +8 -0
- package/dist/esm/createRouteHandler.d.ts.map +1 -0
- package/dist/esm/createRouteHandler.js +52 -0
- package/dist/esm/createRouteHandler.js.map +1 -0
- package/dist/esm/datalog.d.ts +8 -0
- package/dist/esm/datalog.d.ts.map +1 -0
- package/dist/esm/datalog.js +100 -0
- package/dist/esm/datalog.js.map +1 -0
- package/dist/esm/devtool.d.ts +4 -0
- package/dist/esm/devtool.d.ts.map +1 -0
- package/dist/esm/devtool.js +154 -0
- package/dist/esm/devtool.js.map +1 -0
- package/dist/esm/endpoints.d.ts +35 -0
- package/dist/esm/endpoints.d.ts.map +1 -0
- package/dist/esm/endpoints.js +73 -0
- package/dist/esm/endpoints.js.map +1 -0
- package/dist/esm/framework.d.ts +81 -0
- package/dist/esm/framework.d.ts.map +1 -0
- package/dist/esm/framework.js +244 -0
- package/dist/esm/framework.js.map +1 -0
- package/dist/esm/helperTypes.d.ts +76 -0
- package/dist/esm/helperTypes.d.ts.map +1 -0
- package/dist/esm/helperTypes.js +2 -0
- package/dist/esm/helperTypes.js.map +1 -0
- package/dist/esm/index.d.ts +614 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +655 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/infiniteQuery.d.ts +26 -0
- package/dist/esm/infiniteQuery.d.ts.map +1 -0
- package/dist/esm/infiniteQuery.js +433 -0
- package/dist/esm/infiniteQuery.js.map +1 -0
- package/dist/esm/instaml.d.ts +18 -0
- package/dist/esm/instaml.d.ts.map +1 -0
- package/dist/esm/instaml.js +577 -0
- package/dist/esm/instaml.js.map +1 -0
- package/dist/esm/instaql.d.ts +13 -0
- package/dist/esm/instaql.d.ts.map +1 -0
- package/dist/esm/instaql.js +633 -0
- package/dist/esm/instaql.js.map +1 -0
- package/dist/esm/instatx.d.ts +128 -0
- package/dist/esm/instatx.d.ts.map +1 -0
- package/dist/esm/instatx.js +94 -0
- package/dist/esm/instatx.js.map +1 -0
- package/dist/esm/model/instaqlResult.d.ts +8 -0
- package/dist/esm/model/instaqlResult.d.ts.map +1 -0
- package/dist/esm/model/instaqlResult.js +25 -0
- package/dist/esm/model/instaqlResult.js.map +1 -0
- package/dist/esm/notifyScope.d.ts +32 -0
- package/dist/esm/notifyScope.d.ts.map +1 -0
- package/dist/esm/notifyScope.js +300 -0
- package/dist/esm/notifyScope.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/parseSchemaFromJSON.d.ts +3 -0
- package/dist/esm/parseSchemaFromJSON.d.ts.map +1 -0
- package/dist/esm/parseSchemaFromJSON.js +143 -0
- package/dist/esm/parseSchemaFromJSON.js.map +1 -0
- package/dist/esm/presence.d.ts +51 -0
- package/dist/esm/presence.d.ts.map +1 -0
- package/dist/esm/presence.js +51 -0
- package/dist/esm/presence.js.map +1 -0
- package/dist/esm/presenceTypes.d.ts +10 -0
- package/dist/esm/presenceTypes.d.ts.map +1 -0
- package/dist/esm/presenceTypes.js +4 -0
- package/dist/esm/presenceTypes.js.map +1 -0
- package/dist/esm/queryTypes.d.ts +272 -0
- package/dist/esm/queryTypes.d.ts.map +1 -0
- package/dist/esm/queryTypes.js +4 -0
- package/dist/esm/queryTypes.js.map +1 -0
- package/dist/esm/queryValidation.d.ts +6 -0
- package/dist/esm/queryValidation.d.ts.map +1 -0
- package/dist/esm/queryValidation.js +288 -0
- package/dist/esm/queryValidation.js.map +1 -0
- package/dist/esm/reactorTypes.d.ts +30 -0
- package/dist/esm/reactorTypes.d.ts.map +1 -0
- package/dist/esm/reactorTypes.js +2 -0
- package/dist/esm/reactorTypes.js.map +1 -0
- package/dist/esm/routeHandlerProtocol.d.ts +20 -0
- package/dist/esm/routeHandlerProtocol.d.ts.map +1 -0
- package/dist/esm/routeHandlerProtocol.js +2 -0
- package/dist/esm/routeHandlerProtocol.js.map +1 -0
- package/dist/esm/rulesTypes.d.ts +86 -0
- package/dist/esm/rulesTypes.d.ts.map +1 -0
- package/dist/esm/rulesTypes.js +2 -0
- package/dist/esm/rulesTypes.js.map +1 -0
- package/dist/esm/sanityCheckQueries.d.ts +19 -0
- package/dist/esm/sanityCheckQueries.d.ts.map +1 -0
- package/dist/esm/sanityCheckQueries.js +153 -0
- package/dist/esm/sanityCheckQueries.js.map +1 -0
- package/dist/esm/schema.d.ts +73 -0
- package/dist/esm/schema.d.ts.map +1 -0
- package/dist/esm/schema.js +129 -0
- package/dist/esm/schema.js.map +1 -0
- package/dist/esm/schemaTypes.d.ts +245 -0
- package/dist/esm/schemaTypes.d.ts.map +1 -0
- package/dist/esm/schemaTypes.js +107 -0
- package/dist/esm/schemaTypes.js.map +1 -0
- package/dist/esm/store.d.ts +90 -0
- package/dist/esm/store.d.ts.map +1 -0
- package/dist/esm/store.js +723 -0
- package/dist/esm/store.js.map +1 -0
- package/dist/esm/transactionValidation.d.ts +8 -0
- package/dist/esm/transactionValidation.d.ts.map +1 -0
- package/dist/esm/transactionValidation.js +131 -0
- package/dist/esm/transactionValidation.js.map +1 -0
- package/dist/esm/utils/Deferred.d.ts +9 -0
- package/dist/esm/utils/Deferred.d.ts.map +1 -0
- package/dist/esm/utils/Deferred.js +18 -0
- package/dist/esm/utils/Deferred.js.map +1 -0
- package/dist/esm/utils/PersistedObject.d.ts +81 -0
- package/dist/esm/utils/PersistedObject.d.ts.map +1 -0
- package/dist/esm/utils/PersistedObject.js +464 -0
- package/dist/esm/utils/PersistedObject.js.map +1 -0
- package/dist/esm/utils/dates.d.ts +2 -0
- package/dist/esm/utils/dates.d.ts.map +1 -0
- package/dist/esm/utils/dates.js +212 -0
- package/dist/esm/utils/dates.js.map +1 -0
- package/dist/esm/utils/error.d.ts +3 -0
- package/dist/esm/utils/error.d.ts.map +1 -0
- package/dist/esm/utils/error.js +9 -0
- package/dist/esm/utils/error.js.map +1 -0
- package/dist/esm/utils/fetch.d.ts +126 -0
- package/dist/esm/utils/fetch.d.ts.map +1 -0
- package/dist/esm/utils/fetch.js +32 -0
- package/dist/esm/utils/fetch.js.map +1 -0
- package/dist/esm/utils/flags.d.ts +5 -0
- package/dist/esm/utils/flags.d.ts.map +1 -0
- package/dist/esm/utils/flags.js +11 -0
- package/dist/esm/utils/flags.js.map +1 -0
- package/dist/esm/utils/id.d.ts +4 -0
- package/dist/esm/utils/id.d.ts.map +1 -0
- package/dist/esm/utils/id.js +26 -0
- package/dist/esm/utils/id.js.map +1 -0
- package/dist/esm/utils/linkIndex.d.ts +8 -0
- package/dist/esm/utils/linkIndex.d.ts.map +1 -0
- package/dist/esm/utils/linkIndex.js +18 -0
- package/dist/esm/utils/linkIndex.js.map +1 -0
- package/dist/esm/utils/log.d.ts +7 -0
- package/dist/esm/utils/log.d.ts.map +1 -0
- package/dist/esm/utils/log.js +14 -0
- package/dist/esm/utils/log.js.map +1 -0
- package/dist/esm/utils/object.d.ts +17 -0
- package/dist/esm/utils/object.d.ts.map +1 -0
- package/dist/esm/utils/object.js +144 -0
- package/dist/esm/utils/object.js.map +1 -0
- package/dist/esm/utils/pgtime.d.ts +198 -0
- package/dist/esm/utils/pgtime.d.ts.map +1 -0
- package/dist/esm/utils/pgtime.js +198 -0
- package/dist/esm/utils/pgtime.js.map +1 -0
- package/dist/esm/utils/pick.d.ts +2 -0
- package/dist/esm/utils/pick.d.ts.map +1 -0
- package/dist/esm/utils/pick.js +10 -0
- package/dist/esm/utils/pick.js.map +1 -0
- package/dist/esm/utils/strings.d.ts +2 -0
- package/dist/esm/utils/strings.d.ts.map +1 -0
- package/dist/esm/utils/strings.js +16 -0
- package/dist/esm/utils/strings.js.map +1 -0
- package/dist/esm/utils/weakHash.d.ts +16 -0
- package/dist/esm/utils/weakHash.d.ts.map +1 -0
- package/dist/esm/utils/weakHash.js +72 -0
- package/dist/esm/utils/weakHash.js.map +1 -0
- package/dist/esm/utils/weakHashLegacy.d.ts +8 -0
- package/dist/esm/utils/weakHashLegacy.d.ts.map +1 -0
- package/dist/esm/utils/weakHashLegacy.js +73 -0
- package/dist/esm/utils/weakHashLegacy.js.map +1 -0
- package/dist/esm/version.d.ts +3 -0
- package/dist/esm/version.d.ts.map +1 -0
- package/dist/esm/version.js +3 -0
- package/dist/esm/version.js.map +1 -0
- package/dist/esm/warningToggle.d.ts +3 -0
- package/dist/esm/warningToggle.d.ts.map +1 -0
- package/dist/esm/warningToggle.js +5 -0
- package/dist/esm/warningToggle.js.map +1 -0
- package/dist/standalone/index.js +8740 -0
- package/dist/standalone/index.umd.cjs +7 -0
- package/package.json +81 -3
- package/src/Connection.ts +277 -0
- package/src/InMemoryStorage.ts +34 -0
- package/src/IndexedDBStorage.ts +387 -0
- package/src/InstantError.ts +28 -0
- package/src/Reactor.js +2921 -0
- package/src/StorageAPI.ts +156 -0
- package/src/Stream.ts +1164 -0
- package/src/SyncTable.ts +919 -0
- package/src/WindowNetworkListener.js +19 -0
- package/src/__types__/fieldsTypeTest.ts +113 -0
- package/src/__types__/typeUtils.ts +39 -0
- package/src/__types__/useDatesTypeTest.ts +69 -0
- package/src/attrTypes.ts +29 -0
- package/src/authAPI.ts +205 -0
- package/src/clientTypes.ts +28 -0
- package/src/coreTypes.ts +58 -0
- package/src/createRouteHandler.ts +66 -0
- package/src/datalog.js +116 -0
- package/src/devtool.ts +186 -0
- package/src/endpoints.ts +101 -0
- package/src/framework.ts +395 -0
- package/src/helperTypes.ts +115 -0
- package/src/index.ts +1283 -0
- package/src/infiniteQuery.ts +602 -0
- package/src/instaml.ts +714 -0
- package/src/instaql.ts +952 -0
- package/src/instatx.ts +265 -0
- package/src/model/instaqlResult.js +25 -0
- package/src/notifyScope.ts +343 -0
- package/src/parseSchemaFromJSON.ts +176 -0
- package/src/presence.ts +123 -0
- package/src/presenceTypes.ts +19 -0
- package/src/queryTypes.ts +721 -0
- package/src/queryValidation.ts +535 -0
- package/src/reactorTypes.ts +33 -0
- package/src/routeHandlerProtocol.ts +24 -0
- package/src/rulesTypes.ts +122 -0
- package/src/sanityCheckQueries.ts +189 -0
- package/src/schema.ts +194 -0
- package/src/schemaTypes.ts +642 -0
- package/src/store.ts +949 -0
- package/src/transactionValidation.ts +204 -0
- package/src/utils/Deferred.ts +20 -0
- package/src/utils/PersistedObject.ts +589 -0
- package/src/utils/dates.ts +258 -0
- package/src/utils/error.ts +9 -0
- package/src/utils/fetch.ts +129 -0
- package/src/utils/flags.ts +14 -0
- package/src/utils/id.ts +28 -0
- package/src/utils/linkIndex.ts +33 -0
- package/src/utils/log.ts +23 -0
- package/src/utils/object.js +176 -0
- package/src/utils/pgtime.ts +199 -0
- package/src/utils/pick.js +9 -0
- package/src/utils/strings.ts +19 -0
- package/src/utils/weakHash.ts +77 -0
- package/src/utils/weakHashLegacy.ts +75 -0
- package/src/version.ts +3 -0
- package/src/warningToggle.ts +5 -0
- package/tsconfig.cjs.dev.json +15 -0
- package/tsconfig.dev.json +14 -0
- package/tsconfig.json +13 -0
- package/tsconfig.test.json +5 -0
- package/vite.config.ts +19 -0
- package/vitest.config.ts +75 -0
package/src/instaql.ts
ADDED
|
@@ -0,0 +1,952 @@
|
|
|
1
|
+
import { query as datalogQuery } from './datalog.js';
|
|
2
|
+
import { uuidCompare } from './utils/id.ts';
|
|
3
|
+
import { stringCompare } from './utils/strings.ts';
|
|
4
|
+
import * as s from './store.ts';
|
|
5
|
+
import { InstantDBAttr } from './attrTypes.ts';
|
|
6
|
+
import { Cursor } from './queryTypes.ts';
|
|
7
|
+
import { warningEnabled } from './warningToggle.ts';
|
|
8
|
+
|
|
9
|
+
type Pat = [string | any, string, string | any, string | number];
|
|
10
|
+
type Pats = Array<Pat>;
|
|
11
|
+
type OrPat = {
|
|
12
|
+
or: {
|
|
13
|
+
patterns: FullPats;
|
|
14
|
+
joinSym: string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
type AndPat = {
|
|
18
|
+
and: {
|
|
19
|
+
patterns: FullPats;
|
|
20
|
+
joinSym: string;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
type FullPat = Pat | OrPat | AndPat;
|
|
24
|
+
type FullPats = Array<FullPat>;
|
|
25
|
+
|
|
26
|
+
// Pattern variables
|
|
27
|
+
// -----------------
|
|
28
|
+
|
|
29
|
+
type MakeVar = (x: string, level: number) => string;
|
|
30
|
+
|
|
31
|
+
let _seed = 0;
|
|
32
|
+
|
|
33
|
+
function wildcard(friendlyName: string) {
|
|
34
|
+
return makeVarImpl(`_${friendlyName}`, _seed++);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function makeVarImpl(x: string, level: number): string {
|
|
38
|
+
return `?${x}-${level}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Where
|
|
42
|
+
// -----------------
|
|
43
|
+
|
|
44
|
+
class AttrNotFoundError extends Error {
|
|
45
|
+
constructor(message) {
|
|
46
|
+
super(message);
|
|
47
|
+
this.name = 'AttrNotFoundError';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function idAttr(attrsStore: s.AttrsStore, ns: string): InstantDBAttr {
|
|
52
|
+
const attr = s.getPrimaryKeyAttr(attrsStore, ns);
|
|
53
|
+
|
|
54
|
+
if (!attr) {
|
|
55
|
+
throw new AttrNotFoundError(`Could not find id attr for ${ns}`);
|
|
56
|
+
}
|
|
57
|
+
return attr;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function defaultWhere(
|
|
61
|
+
makeVar: MakeVar,
|
|
62
|
+
attrsStore: s.AttrsStore,
|
|
63
|
+
etype: string,
|
|
64
|
+
level: number,
|
|
65
|
+
): Pats {
|
|
66
|
+
return [eidWhere(makeVar, attrsStore, etype, level)];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function eidWhere(
|
|
70
|
+
makeVar: MakeVar,
|
|
71
|
+
attrsStore: s.AttrsStore,
|
|
72
|
+
etype: string,
|
|
73
|
+
level: number,
|
|
74
|
+
): Pat {
|
|
75
|
+
return [
|
|
76
|
+
makeVar(etype, level),
|
|
77
|
+
idAttr(attrsStore, etype).id,
|
|
78
|
+
makeVar(etype, level),
|
|
79
|
+
makeVar('time', level),
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function replaceInAttrPat(attrPat: Pat, needle: string, v: any): Pat {
|
|
84
|
+
return attrPat.map((x) => (x === needle ? v : x)) as Pat;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function refAttrPat(
|
|
88
|
+
makeVar: MakeVar,
|
|
89
|
+
attrsStore: s.AttrsStore,
|
|
90
|
+
etype: string,
|
|
91
|
+
level: number,
|
|
92
|
+
label: string,
|
|
93
|
+
): [string, number, Pat, InstantDBAttr, boolean] {
|
|
94
|
+
const fwdAttr = s.getAttrByFwdIdentName(attrsStore, etype, label);
|
|
95
|
+
const revAttr = s.getAttrByReverseIdentName(attrsStore, etype, label);
|
|
96
|
+
const attr = fwdAttr || revAttr;
|
|
97
|
+
|
|
98
|
+
if (!attr) {
|
|
99
|
+
throw new AttrNotFoundError(`Could not find attr for ${[etype, label]}`);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (attr['value-type'] !== 'ref') {
|
|
103
|
+
throw new Error(`Attr ${attr.id} is not a ref`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const [_f, fwdEtype] = attr['forward-identity'];
|
|
107
|
+
const [_r, revEtype] = attr['reverse-identity']!;
|
|
108
|
+
const nextLevel = level + 1;
|
|
109
|
+
const attrPat: Pat = fwdAttr
|
|
110
|
+
? [
|
|
111
|
+
makeVar(fwdEtype, level),
|
|
112
|
+
attr.id,
|
|
113
|
+
makeVar(revEtype, nextLevel),
|
|
114
|
+
wildcard('time'),
|
|
115
|
+
]
|
|
116
|
+
: [
|
|
117
|
+
makeVar(fwdEtype, nextLevel),
|
|
118
|
+
attr.id,
|
|
119
|
+
makeVar(revEtype, level),
|
|
120
|
+
wildcard('time'),
|
|
121
|
+
];
|
|
122
|
+
|
|
123
|
+
const nextEtype = fwdAttr ? revEtype : fwdEtype;
|
|
124
|
+
|
|
125
|
+
const isForward = Boolean(fwdAttr);
|
|
126
|
+
|
|
127
|
+
return [nextEtype, nextLevel, attrPat, attr, isForward];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function makeLikeMatcher(caseSensitive: boolean, pattern: string) {
|
|
131
|
+
if (typeof pattern !== 'string') {
|
|
132
|
+
return function likeMatcher(_value) {
|
|
133
|
+
return false;
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const escapedPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
138
|
+
const regexPattern = escapedPattern.replace(/%/g, '.*').replace(/_/g, '.');
|
|
139
|
+
|
|
140
|
+
// The `s` (dotall) flag makes `%` (-> .*) and `_` (-> .) match newline
|
|
141
|
+
// characters, matching Postgres LIKE/ILIKE on the server.
|
|
142
|
+
const regex = new RegExp(`^${regexPattern}$`, caseSensitive ? 's' : 'is');
|
|
143
|
+
|
|
144
|
+
return function likeMatcher(value) {
|
|
145
|
+
if (typeof value !== 'string') {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
return regex.test(value);
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function parseValue(attr: InstantDBAttr, v: any) {
|
|
153
|
+
if (
|
|
154
|
+
typeof v !== 'object' ||
|
|
155
|
+
v.hasOwnProperty('$in') ||
|
|
156
|
+
v.hasOwnProperty('in')
|
|
157
|
+
) {
|
|
158
|
+
return v;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const isDate = attr['checked-data-type'] === 'date';
|
|
162
|
+
|
|
163
|
+
if (v.hasOwnProperty('$gt')) {
|
|
164
|
+
return {
|
|
165
|
+
$comparator: true,
|
|
166
|
+
$op: isDate
|
|
167
|
+
? function gtDate(triple) {
|
|
168
|
+
return new Date(triple[2]) > new Date(v.$gt);
|
|
169
|
+
}
|
|
170
|
+
: function gt(triple) {
|
|
171
|
+
return triple[2] > v.$gt;
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
if (v.hasOwnProperty('$gte')) {
|
|
176
|
+
return {
|
|
177
|
+
$comparator: true,
|
|
178
|
+
$op: isDate
|
|
179
|
+
? function gteDate(triple) {
|
|
180
|
+
return new Date(triple[2]) >= new Date(v.$gte);
|
|
181
|
+
}
|
|
182
|
+
: function gte(triple) {
|
|
183
|
+
return triple[2] >= v.$gte;
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (v.hasOwnProperty('$lt')) {
|
|
189
|
+
return {
|
|
190
|
+
$comparator: true,
|
|
191
|
+
$op: isDate
|
|
192
|
+
? function ltDate(triple) {
|
|
193
|
+
return new Date(triple[2]) < new Date(v.$lt);
|
|
194
|
+
}
|
|
195
|
+
: function lt(triple) {
|
|
196
|
+
return triple[2] < v.$lt;
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
if (v.hasOwnProperty('$lte')) {
|
|
201
|
+
return {
|
|
202
|
+
$comparator: true,
|
|
203
|
+
$op: isDate
|
|
204
|
+
? function lteDate(triple) {
|
|
205
|
+
return new Date(triple[2]) <= new Date(v.$lte);
|
|
206
|
+
}
|
|
207
|
+
: function lte(triple) {
|
|
208
|
+
return triple[2] <= v.$lte;
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (v.hasOwnProperty('$like')) {
|
|
214
|
+
const matcher = makeLikeMatcher(true, v.$like);
|
|
215
|
+
return {
|
|
216
|
+
$comparator: true,
|
|
217
|
+
$op: function like(triple) {
|
|
218
|
+
return matcher(triple[2]);
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (v.hasOwnProperty('$ilike')) {
|
|
224
|
+
const matcher = makeLikeMatcher(false, v.$ilike);
|
|
225
|
+
return {
|
|
226
|
+
$comparator: true,
|
|
227
|
+
$op: function ilike(triple) {
|
|
228
|
+
return matcher(triple[2]);
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return v;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function valueAttrPat(
|
|
237
|
+
makeVar: MakeVar,
|
|
238
|
+
attrsStore: s.AttrsStore,
|
|
239
|
+
valueEtype: string,
|
|
240
|
+
valueLevel: number,
|
|
241
|
+
valueLabel: string,
|
|
242
|
+
v: any,
|
|
243
|
+
): Pat {
|
|
244
|
+
const fwdAttr = s.getAttrByFwdIdentName(attrsStore, valueEtype, valueLabel);
|
|
245
|
+
const revAttr = s.getAttrByReverseIdentName(
|
|
246
|
+
attrsStore,
|
|
247
|
+
valueEtype,
|
|
248
|
+
valueLabel,
|
|
249
|
+
);
|
|
250
|
+
const attr = fwdAttr || revAttr;
|
|
251
|
+
|
|
252
|
+
if (!attr) {
|
|
253
|
+
throw new AttrNotFoundError(
|
|
254
|
+
`No attr for etype = ${valueEtype} label = ${valueLabel}`,
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (v?.hasOwnProperty('$isNull')) {
|
|
259
|
+
const idAttr = s.getAttrByFwdIdentName(attrsStore, valueEtype, 'id');
|
|
260
|
+
if (!idAttr) {
|
|
261
|
+
throw new AttrNotFoundError(
|
|
262
|
+
`No attr for etype = ${valueEtype} label = id`,
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return [
|
|
267
|
+
makeVar(valueEtype, valueLevel),
|
|
268
|
+
idAttr.id,
|
|
269
|
+
{ $isNull: { attrId: attr.id, isNull: v.$isNull, reverse: !fwdAttr } },
|
|
270
|
+
wildcard('time'),
|
|
271
|
+
];
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (fwdAttr) {
|
|
275
|
+
return [
|
|
276
|
+
makeVar(valueEtype, valueLevel),
|
|
277
|
+
attr.id,
|
|
278
|
+
parseValue(attr, v),
|
|
279
|
+
wildcard('time'),
|
|
280
|
+
];
|
|
281
|
+
}
|
|
282
|
+
return [v, attr.id, makeVar(valueEtype, valueLevel), wildcard('time')];
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function refAttrPats(
|
|
286
|
+
makeVar: MakeVar,
|
|
287
|
+
attrsStore: s.AttrsStore,
|
|
288
|
+
etype: string,
|
|
289
|
+
level: number,
|
|
290
|
+
refsPath: string[],
|
|
291
|
+
): [string, number, Pats] {
|
|
292
|
+
const [lastEtype, lastLevel, attrPats] = refsPath.reduce(
|
|
293
|
+
(acc, label) => {
|
|
294
|
+
const [etype, level, attrPats] = acc;
|
|
295
|
+
const [nextEtype, nextLevel, attrPat] = refAttrPat(
|
|
296
|
+
makeVar,
|
|
297
|
+
attrsStore,
|
|
298
|
+
etype,
|
|
299
|
+
level,
|
|
300
|
+
label,
|
|
301
|
+
);
|
|
302
|
+
return [nextEtype, nextLevel, [...attrPats, attrPat]];
|
|
303
|
+
},
|
|
304
|
+
[etype, level, []],
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
return [lastEtype, lastLevel, attrPats];
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function whereCondAttrPats(
|
|
311
|
+
makeVar: MakeVar,
|
|
312
|
+
attrsStore: s.AttrsStore,
|
|
313
|
+
etype: string,
|
|
314
|
+
level: number,
|
|
315
|
+
path: string[],
|
|
316
|
+
v: any,
|
|
317
|
+
): Pats {
|
|
318
|
+
const refsPath = path.slice(0, path.length - 1);
|
|
319
|
+
const valueLabel = path[path.length - 1];
|
|
320
|
+
const [lastEtype, lastLevel, refPats] = refAttrPats(
|
|
321
|
+
makeVar,
|
|
322
|
+
attrsStore,
|
|
323
|
+
etype,
|
|
324
|
+
level,
|
|
325
|
+
refsPath,
|
|
326
|
+
);
|
|
327
|
+
const valuePat = valueAttrPat(
|
|
328
|
+
makeVar,
|
|
329
|
+
attrsStore,
|
|
330
|
+
lastEtype,
|
|
331
|
+
lastLevel,
|
|
332
|
+
valueLabel,
|
|
333
|
+
v,
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
return refPats.concat([valuePat]);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function withJoin(where, join) {
|
|
340
|
+
return join ? [join].concat(where) : where;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function isOrClauses([k, v]): boolean {
|
|
344
|
+
return k === 'or' && Array.isArray(v);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function isAndClauses([k, v]): boolean {
|
|
348
|
+
return k === 'and' && Array.isArray(v);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// Creates a makeVar that will namespace symbols for or clauses
|
|
352
|
+
// to prevent conflicts, except for the base etype
|
|
353
|
+
function genMakeVar(baseMakeVar: MakeVar, joinSym: string, orIdx: number) {
|
|
354
|
+
return (x, lvl) => {
|
|
355
|
+
const base = baseMakeVar(x, lvl);
|
|
356
|
+
if (joinSym == base) {
|
|
357
|
+
return base;
|
|
358
|
+
}
|
|
359
|
+
return `${base}-${orIdx}`;
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function parseWhereClauses(
|
|
364
|
+
makeVar: MakeVar,
|
|
365
|
+
clauseType: 'or' | 'and' /* 'or' | 'and' */,
|
|
366
|
+
attrsStore: s.AttrsStore,
|
|
367
|
+
etype: string,
|
|
368
|
+
level: number,
|
|
369
|
+
whereValue: any,
|
|
370
|
+
): FullPat {
|
|
371
|
+
const joinSym = makeVar(etype, level);
|
|
372
|
+
const patterns: FullPats = whereValue.map((w, i) => {
|
|
373
|
+
const makeNamespacedVar = genMakeVar(makeVar, joinSym, i);
|
|
374
|
+
return parseWhere(makeNamespacedVar, attrsStore, etype, level, w);
|
|
375
|
+
});
|
|
376
|
+
return { [clauseType]: { patterns, joinSym } } as AndPat | OrPat;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// Given a path, returns a list of paths leading up to this path:
|
|
380
|
+
// growPath([1, 2, 3]) -> [[1], [1, 2], [1, 2, 3]]
|
|
381
|
+
function growPath<T>(path: T[]) {
|
|
382
|
+
const ret: Array<T[]> = [];
|
|
383
|
+
for (let i = 1; i <= path.length; i++) {
|
|
384
|
+
ret.push(path.slice(0, i));
|
|
385
|
+
}
|
|
386
|
+
return ret;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// Returns array of pattern arrays that should be grouped in OR
|
|
390
|
+
// to capture any intermediate nulls
|
|
391
|
+
function whereCondAttrPatsForNullIsTrue(
|
|
392
|
+
makeVar: MakeVar,
|
|
393
|
+
attrsStore: s.AttrsStore,
|
|
394
|
+
etype: string,
|
|
395
|
+
level: number,
|
|
396
|
+
path: string[],
|
|
397
|
+
): Pats[] {
|
|
398
|
+
return growPath(path).map((path) =>
|
|
399
|
+
whereCondAttrPats(makeVar, attrsStore, etype, level, path, {
|
|
400
|
+
$isNull: true,
|
|
401
|
+
}),
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function parseWhere(
|
|
406
|
+
makeVar: MakeVar,
|
|
407
|
+
attrsStore: s.AttrsStore,
|
|
408
|
+
etype: string,
|
|
409
|
+
level: number,
|
|
410
|
+
where: Record<string, any>,
|
|
411
|
+
): FullPats {
|
|
412
|
+
return Object.entries(where).flatMap(([k, v]) => {
|
|
413
|
+
if (isOrClauses([k, v])) {
|
|
414
|
+
return parseWhereClauses(makeVar, 'or', attrsStore, etype, level, v);
|
|
415
|
+
}
|
|
416
|
+
if (isAndClauses([k, v])) {
|
|
417
|
+
return parseWhereClauses(makeVar, 'and', attrsStore, etype, level, v);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// Temporary hack until we have support for a uuid index on `id`
|
|
421
|
+
if (k === '$entityIdStartsWith') {
|
|
422
|
+
return [];
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const path = k.split('.');
|
|
426
|
+
|
|
427
|
+
// Normalize $ne to $not
|
|
428
|
+
if (v?.hasOwnProperty('$ne')) {
|
|
429
|
+
v = { ...v, $not: v.$ne };
|
|
430
|
+
delete v.$ne;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
if (v?.hasOwnProperty('$not')) {
|
|
434
|
+
// `$not` won't pick up entities that are missing the attr, so we
|
|
435
|
+
// add in a `$isNull` to catch those too.
|
|
436
|
+
const notPats = whereCondAttrPats(
|
|
437
|
+
makeVar,
|
|
438
|
+
attrsStore,
|
|
439
|
+
etype,
|
|
440
|
+
level,
|
|
441
|
+
path,
|
|
442
|
+
v,
|
|
443
|
+
);
|
|
444
|
+
const nilPats = whereCondAttrPatsForNullIsTrue(
|
|
445
|
+
makeVar,
|
|
446
|
+
attrsStore,
|
|
447
|
+
etype,
|
|
448
|
+
level,
|
|
449
|
+
path,
|
|
450
|
+
);
|
|
451
|
+
return [
|
|
452
|
+
{
|
|
453
|
+
or: {
|
|
454
|
+
patterns: [notPats, ...nilPats],
|
|
455
|
+
joinSym: makeVar(etype, level),
|
|
456
|
+
},
|
|
457
|
+
},
|
|
458
|
+
];
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (v?.hasOwnProperty('$isNull') && v.$isNull === true && path.length > 1) {
|
|
462
|
+
// Make sure we're capturing all of the intermediate paths that might be null
|
|
463
|
+
// by checking for null at each step along the path
|
|
464
|
+
return [
|
|
465
|
+
{
|
|
466
|
+
or: {
|
|
467
|
+
patterns: whereCondAttrPatsForNullIsTrue(
|
|
468
|
+
makeVar,
|
|
469
|
+
attrsStore,
|
|
470
|
+
etype,
|
|
471
|
+
level,
|
|
472
|
+
path,
|
|
473
|
+
),
|
|
474
|
+
joinSym: makeVar(etype, level),
|
|
475
|
+
},
|
|
476
|
+
},
|
|
477
|
+
];
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
return whereCondAttrPats(makeVar, attrsStore, etype, level, path, v);
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function makeWhere(
|
|
485
|
+
attrsStore: s.AttrsStore,
|
|
486
|
+
etype: string,
|
|
487
|
+
level: number,
|
|
488
|
+
where: Record<string, any> | null,
|
|
489
|
+
): FullPats {
|
|
490
|
+
const makeVar = makeVarImpl;
|
|
491
|
+
if (!where) {
|
|
492
|
+
return defaultWhere(makeVar, attrsStore, etype, level);
|
|
493
|
+
}
|
|
494
|
+
const parsedWhere = parseWhere(makeVar, attrsStore, etype, level, where);
|
|
495
|
+
return parsedWhere.concat(defaultWhere(makeVar, attrsStore, etype, level));
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// Find
|
|
499
|
+
// -----------------
|
|
500
|
+
|
|
501
|
+
function makeFind(makeVar: MakeVar, etype: string, level: number) {
|
|
502
|
+
return [makeVar(etype, level), makeVar('time', level)];
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// extendObjects
|
|
506
|
+
// -----------------
|
|
507
|
+
|
|
508
|
+
function makeJoin(
|
|
509
|
+
makeVar: MakeVar,
|
|
510
|
+
attrsStore: s.AttrsStore,
|
|
511
|
+
etype: string,
|
|
512
|
+
level: number,
|
|
513
|
+
label: string,
|
|
514
|
+
eid: string,
|
|
515
|
+
) {
|
|
516
|
+
const [nextEtype, nextLevel, pat, attr, isForward] = refAttrPat(
|
|
517
|
+
makeVar,
|
|
518
|
+
attrsStore,
|
|
519
|
+
etype,
|
|
520
|
+
level,
|
|
521
|
+
label,
|
|
522
|
+
);
|
|
523
|
+
const actualized = replaceInAttrPat(pat, makeVar(etype, level), eid);
|
|
524
|
+
return [nextEtype, nextLevel, actualized, attr, isForward];
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
function extendObjects(
|
|
528
|
+
makeVar: MakeVar,
|
|
529
|
+
store: s.Store,
|
|
530
|
+
attrsStore: s.AttrsStore,
|
|
531
|
+
{ etype, level, form },
|
|
532
|
+
objects,
|
|
533
|
+
) {
|
|
534
|
+
const childQueries = Object.keys(form).filter((c) => c !== '$');
|
|
535
|
+
if (!childQueries.length) {
|
|
536
|
+
return Object.values(objects);
|
|
537
|
+
}
|
|
538
|
+
return Object.entries(objects).map(function extendChildren([eid, parent]) {
|
|
539
|
+
const childResults = childQueries.map(function getChildResult(label) {
|
|
540
|
+
const isSingular = Boolean(
|
|
541
|
+
store.cardinalityInference &&
|
|
542
|
+
attrsStore.linkIndex?.[etype]?.[label]?.isSingular,
|
|
543
|
+
);
|
|
544
|
+
|
|
545
|
+
try {
|
|
546
|
+
const [nextEtype, nextLevel, join] = makeJoin(
|
|
547
|
+
makeVar,
|
|
548
|
+
attrsStore,
|
|
549
|
+
etype,
|
|
550
|
+
level,
|
|
551
|
+
label,
|
|
552
|
+
eid,
|
|
553
|
+
);
|
|
554
|
+
|
|
555
|
+
const childrenArray = queryOne(store, attrsStore, {
|
|
556
|
+
etype: nextEtype,
|
|
557
|
+
level: nextLevel,
|
|
558
|
+
form: form[label],
|
|
559
|
+
join,
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
const childOrChildren = isSingular ? childrenArray[0] : childrenArray;
|
|
563
|
+
|
|
564
|
+
return { [label]: childOrChildren };
|
|
565
|
+
} catch (e) {
|
|
566
|
+
if (e instanceof AttrNotFoundError) {
|
|
567
|
+
return { [label]: isSingular ? undefined : [] };
|
|
568
|
+
}
|
|
569
|
+
throw e;
|
|
570
|
+
}
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
return childResults.reduce(function reduceChildren(parent, child) {
|
|
574
|
+
return { ...parent, ...child };
|
|
575
|
+
}, parent);
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// resolveObjects
|
|
580
|
+
// -----------------
|
|
581
|
+
|
|
582
|
+
function shouldIgnoreAttr(attrs, id) {
|
|
583
|
+
const attr = attrs[id];
|
|
584
|
+
return attr['value-type'] === 'ref' && attr['forward-identity'][2] !== 'id';
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// Compares values where we already know that the two values are distinct
|
|
588
|
+
// and not null.
|
|
589
|
+
// Takes into account the data type.
|
|
590
|
+
function compareDisparateValues(a, b, dataType) {
|
|
591
|
+
if (dataType === 'string') {
|
|
592
|
+
return stringCompare(a, b);
|
|
593
|
+
}
|
|
594
|
+
if (a > b) {
|
|
595
|
+
return 1;
|
|
596
|
+
}
|
|
597
|
+
return -1;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
export function compareOrder(id_a: string, v_a, id_b: string, v_b, dataType) {
|
|
601
|
+
if (v_a === v_b || (v_a == null && v_b == null)) {
|
|
602
|
+
return uuidCompare(id_a, id_b);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
if (v_b == null) {
|
|
606
|
+
return 1;
|
|
607
|
+
}
|
|
608
|
+
if (v_a == null) {
|
|
609
|
+
return -1;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
return compareDisparateValues(v_a, v_b, dataType);
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
function compareOrderTriples(
|
|
616
|
+
[id_a, v_a]: [string, any],
|
|
617
|
+
[id_b, v_b]: [string, any],
|
|
618
|
+
dataType,
|
|
619
|
+
) {
|
|
620
|
+
return compareOrder(id_a, v_a, id_b, v_b, dataType);
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
function comparableDate(x) {
|
|
624
|
+
if (x == null) {
|
|
625
|
+
return x;
|
|
626
|
+
}
|
|
627
|
+
return new Date(x).getTime();
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
function compareToCursor(cursor, orderAttr, idVec) {
|
|
631
|
+
const [c_e, _c_a, c_v, c_t] = cursor;
|
|
632
|
+
|
|
633
|
+
if (orderAttr['forward-identity']?.[2] === 'id') {
|
|
634
|
+
return compareOrderTriples(idVec, [c_e, c_t], null);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
const [e, v] = idVec;
|
|
638
|
+
const dataType = orderAttr['checked-data-type'];
|
|
639
|
+
const v_new = dataType === 'date' ? comparableDate(v) : v;
|
|
640
|
+
const c_v_new = dataType === 'date' ? comparableDate(c_v) : c_v;
|
|
641
|
+
|
|
642
|
+
return compareOrderTriples([e, v_new], [c_e, c_v_new], dataType);
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
function isBefore(startCursor, orderAttr, direction, idVec) {
|
|
646
|
+
const cmp = compareToCursor(startCursor, orderAttr, idVec);
|
|
647
|
+
return direction === 'desc' ? cmp > 0 : cmp < 0;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
function isAfter(endCursor, orderAttr, direction, idVec) {
|
|
651
|
+
const cmp = compareToCursor(endCursor, orderAttr, idVec);
|
|
652
|
+
return direction === 'desc' ? cmp < 0 : cmp > 0;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
function orderAttrFromCursor(attrsStore: s.AttrsStore, cursor) {
|
|
656
|
+
const cursorAttrId = cursor[1];
|
|
657
|
+
return attrsStore.getAttr(cursorAttrId);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function orderAttrFromOrder(attrsStore: s.AttrsStore, etype, order) {
|
|
661
|
+
const label = Object.keys(order)[0];
|
|
662
|
+
return s.getAttrByFwdIdentName(attrsStore, etype, label);
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
function getOrderAttr(attrsStore: s.AttrsStore, etype, cursor, order) {
|
|
666
|
+
if (cursor) {
|
|
667
|
+
return orderAttrFromCursor(attrsStore, cursor);
|
|
668
|
+
}
|
|
669
|
+
if (order) {
|
|
670
|
+
return orderAttrFromOrder(attrsStore, etype, order);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
function objectAttrs(
|
|
675
|
+
attrsStore: s.AttrsStore,
|
|
676
|
+
etype,
|
|
677
|
+
dq,
|
|
678
|
+
): Map<string, InstantDBAttr> | undefined {
|
|
679
|
+
if (!Array.isArray(dq.fields)) {
|
|
680
|
+
return s.getBlobAttrs(attrsStore, etype);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
const attrs = new Map();
|
|
684
|
+
|
|
685
|
+
for (const field of dq.fields) {
|
|
686
|
+
const attr = s.getAttrByFwdIdentName(attrsStore, etype, field);
|
|
687
|
+
const label = attr?.['forward-identity']?.[2];
|
|
688
|
+
if (label && s.isBlob(attr)) {
|
|
689
|
+
attrs.set(label, attr);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
// Ensure we add the id field to avoid empty objects
|
|
693
|
+
if (!attrs.has('id')) {
|
|
694
|
+
const attr = s.getAttrByFwdIdentName(attrsStore, etype, 'id');
|
|
695
|
+
const label = attr?.['forward-identity']?.[2];
|
|
696
|
+
if (label) {
|
|
697
|
+
attrs.set(label, attr);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
return attrs;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
function runDataloadAndReturnObjects(
|
|
705
|
+
store: s.Store,
|
|
706
|
+
attrsStore: s.AttrsStore,
|
|
707
|
+
{ etype, pageInfo, dq, form },
|
|
708
|
+
) {
|
|
709
|
+
const order = form?.$?.order;
|
|
710
|
+
const isLeadingQuery = isLeading(form);
|
|
711
|
+
const direction = determineDirection(form);
|
|
712
|
+
|
|
713
|
+
let idVecs = datalogQuery(store, dq);
|
|
714
|
+
|
|
715
|
+
const startCursor = pageInfo?.['start-cursor'];
|
|
716
|
+
const endCursor = pageInfo?.['end-cursor'];
|
|
717
|
+
const orderAttr = getOrderAttr(attrsStore, etype, startCursor, order);
|
|
718
|
+
|
|
719
|
+
if (orderAttr && orderAttr?.['forward-identity']?.[2] !== 'id') {
|
|
720
|
+
const isDate = orderAttr['checked-data-type'] === 'date';
|
|
721
|
+
const a = orderAttr.id;
|
|
722
|
+
idVecs = idVecs.map(([id]) => {
|
|
723
|
+
// order attr is required to be cardinality one, so there will
|
|
724
|
+
// be at most one value here
|
|
725
|
+
let v = store.eav.get(id)?.get(a)?.values()?.next()?.value?.[2];
|
|
726
|
+
if (isDate) {
|
|
727
|
+
v = comparableDate(v);
|
|
728
|
+
}
|
|
729
|
+
return [id, v];
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
idVecs.sort(
|
|
734
|
+
direction === 'asc'
|
|
735
|
+
? function compareIdVecs(a, b) {
|
|
736
|
+
return compareOrderTriples(a, b, orderAttr?.['checked-data-type']);
|
|
737
|
+
}
|
|
738
|
+
: function compareIdVecs(a, b) {
|
|
739
|
+
return compareOrderTriples(b, a, orderAttr?.['checked-data-type']);
|
|
740
|
+
},
|
|
741
|
+
);
|
|
742
|
+
|
|
743
|
+
let objects = {};
|
|
744
|
+
const attrs = objectAttrs(attrsStore, etype, dq);
|
|
745
|
+
|
|
746
|
+
for (const idVec of idVecs) {
|
|
747
|
+
const [id] = idVec;
|
|
748
|
+
if (objects[id]) {
|
|
749
|
+
continue;
|
|
750
|
+
}
|
|
751
|
+
if (
|
|
752
|
+
!isLeadingQuery &&
|
|
753
|
+
startCursor &&
|
|
754
|
+
orderAttr &&
|
|
755
|
+
isBefore(startCursor, orderAttr, direction, idVec)
|
|
756
|
+
) {
|
|
757
|
+
continue;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
if (
|
|
761
|
+
!isLeadingQuery &&
|
|
762
|
+
endCursor &&
|
|
763
|
+
orderAttr &&
|
|
764
|
+
isAfter(endCursor, orderAttr, direction, idVec)
|
|
765
|
+
) {
|
|
766
|
+
continue;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
const obj = s.getAsObject(store, attrs, id);
|
|
770
|
+
if (obj) {
|
|
771
|
+
objects[id] = obj;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
return objects;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
function determineDirection(form) {
|
|
778
|
+
const orderOpts = form.$?.order;
|
|
779
|
+
if (!orderOpts) {
|
|
780
|
+
return 'asc';
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
return orderOpts[Object.keys(orderOpts)[0]] || 'asc';
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* A "leading" query has no `offset`, `before`, or `after`
|
|
788
|
+
*
|
|
789
|
+
* It is at the 'beginning' of the order
|
|
790
|
+
*/
|
|
791
|
+
function isLeading(form) {
|
|
792
|
+
const offset = form.$?.offset;
|
|
793
|
+
const before = form.$?.before;
|
|
794
|
+
const after = form.$?.after;
|
|
795
|
+
return !offset && !before && !after;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* Given a query like:
|
|
800
|
+
*
|
|
801
|
+
* {
|
|
802
|
+
* users: {
|
|
803
|
+
* $: { where: { name: "Joe" } },
|
|
804
|
+
* },
|
|
805
|
+
* };
|
|
806
|
+
*
|
|
807
|
+
* `resolveObjects`, turns where clause: `{ name: "Joe" }`
|
|
808
|
+
* into a datalog query. We then run the datalog query,
|
|
809
|
+
* and reduce all the triples into objects.
|
|
810
|
+
*/
|
|
811
|
+
function resolveObjects(
|
|
812
|
+
store: s.Store,
|
|
813
|
+
attrsStore: s.AttrsStore,
|
|
814
|
+
{ etype, level, form, join, pageInfo },
|
|
815
|
+
) {
|
|
816
|
+
// Wait for server to tell us where we start if we don't start from the beginning
|
|
817
|
+
if (!isLeading(form) && (!pageInfo || !pageInfo['start-cursor'])) {
|
|
818
|
+
return [];
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
const where = withJoin(
|
|
822
|
+
makeWhere(attrsStore, etype, level, form.$?.where),
|
|
823
|
+
join,
|
|
824
|
+
);
|
|
825
|
+
const find = makeFind(makeVarImpl, etype, level);
|
|
826
|
+
const fields = form.$?.fields;
|
|
827
|
+
|
|
828
|
+
const objs = runDataloadAndReturnObjects(store, attrsStore, {
|
|
829
|
+
etype,
|
|
830
|
+
pageInfo,
|
|
831
|
+
form,
|
|
832
|
+
dq: { where, find, fields },
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
const limit = form.$?.limit || form.$?.first || form.$?.last;
|
|
836
|
+
if (limit != null) {
|
|
837
|
+
if (level > 0 && warningEnabled) {
|
|
838
|
+
console.warn(
|
|
839
|
+
'WARNING: Limits in child queries are only run client-side. Data returned from the server will not have a limit.',
|
|
840
|
+
);
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
const entries = Object.entries(objs);
|
|
844
|
+
if (entries.length <= limit) {
|
|
845
|
+
return objs;
|
|
846
|
+
}
|
|
847
|
+
return Object.fromEntries(entries.slice(0, limit));
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
return objs;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/**
|
|
854
|
+
* It's possible that we query
|
|
855
|
+
* for an attribute that doesn't exist yet.
|
|
856
|
+
*
|
|
857
|
+
* { users: { $: { where: { nonExistentProperty: "foo" } } } }
|
|
858
|
+
*
|
|
859
|
+
* This swallows the missing attr error and returns
|
|
860
|
+
* an empty result instead
|
|
861
|
+
*/
|
|
862
|
+
function guardedResolveObjects(store: s.Store, attrsStore: s.AttrsStore, opts) {
|
|
863
|
+
try {
|
|
864
|
+
return resolveObjects(store, attrsStore, opts);
|
|
865
|
+
} catch (e) {
|
|
866
|
+
if (e instanceof AttrNotFoundError) {
|
|
867
|
+
return {};
|
|
868
|
+
}
|
|
869
|
+
throw e;
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
/**
|
|
873
|
+
* Given a query like:
|
|
874
|
+
*
|
|
875
|
+
* {
|
|
876
|
+
* users: {
|
|
877
|
+
* $: { where: { name: "Joe" } },
|
|
878
|
+
* posts: {},
|
|
879
|
+
* },
|
|
880
|
+
* };
|
|
881
|
+
*
|
|
882
|
+
* `guardResolveObjects` will return the relevant `users` objects
|
|
883
|
+
* `extendObjects` will then extend each `user` object with relevant `posts`.
|
|
884
|
+
*/
|
|
885
|
+
function queryOne(store: s.Store, attrsStore: s.AttrsStore, opts) {
|
|
886
|
+
const objects = guardedResolveObjects(store, attrsStore, opts);
|
|
887
|
+
return extendObjects(makeVarImpl, store, attrsStore, opts, objects);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
function formatPageInfo(
|
|
891
|
+
pageInfo: Record<
|
|
892
|
+
string,
|
|
893
|
+
{
|
|
894
|
+
'start-cursor'?: Cursor | null;
|
|
895
|
+
'end-cursor'?: Cursor | null;
|
|
896
|
+
'has-next-page?'?: boolean | null;
|
|
897
|
+
'has-previous-page?'?: boolean | null;
|
|
898
|
+
}
|
|
899
|
+
>,
|
|
900
|
+
) {
|
|
901
|
+
const res = {};
|
|
902
|
+
for (const [k, v] of Object.entries(pageInfo)) {
|
|
903
|
+
res[k] = {
|
|
904
|
+
startCursor: v['start-cursor'],
|
|
905
|
+
endCursor: v['end-cursor'],
|
|
906
|
+
hasNextPage: v['has-next-page?'],
|
|
907
|
+
hasPreviousPage: v['has-previous-page?'],
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
return res;
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
export default function query(
|
|
914
|
+
{
|
|
915
|
+
store,
|
|
916
|
+
attrsStore,
|
|
917
|
+
pageInfo,
|
|
918
|
+
aggregate,
|
|
919
|
+
}: {
|
|
920
|
+
store: s.Store;
|
|
921
|
+
attrsStore: s.AttrsStore;
|
|
922
|
+
pageInfo?: any;
|
|
923
|
+
aggregate?: any;
|
|
924
|
+
},
|
|
925
|
+
q,
|
|
926
|
+
) {
|
|
927
|
+
const data = Object.keys(q).reduce(function reduceResult(res, k) {
|
|
928
|
+
if (aggregate?.[k] || '$$ruleParams' === k) {
|
|
929
|
+
// Aggregate doesn't return any join rows and has no children,
|
|
930
|
+
// so don't bother querying further
|
|
931
|
+
return res;
|
|
932
|
+
}
|
|
933
|
+
res[k] = queryOne(store, attrsStore, {
|
|
934
|
+
etype: k,
|
|
935
|
+
form: q[k],
|
|
936
|
+
level: 0,
|
|
937
|
+
pageInfo: pageInfo?.[k],
|
|
938
|
+
});
|
|
939
|
+
return res;
|
|
940
|
+
}, {});
|
|
941
|
+
|
|
942
|
+
const result: { data: any; pageInfo?: any; aggregate?: any } = { data };
|
|
943
|
+
if (pageInfo) {
|
|
944
|
+
result.pageInfo = formatPageInfo(pageInfo);
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
if (aggregate) {
|
|
948
|
+
result.aggregate = aggregate;
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
return result;
|
|
952
|
+
}
|