@primate/core 0.4.5 → 0.5.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/lib/private/App.d.ts +531 -3
- package/lib/private/App.js +8 -3
- package/lib/private/Binder.d.ts +2 -2
- package/lib/private/Flags.d.ts +3 -3
- package/lib/private/Module.d.ts +5 -5
- package/lib/private/app/Facade.d.ts +566 -0
- package/lib/private/app/Facade.js +33 -0
- package/lib/private/asset/Asset.d.ts +1 -1
- package/lib/private/backend/Module.d.ts +1 -1
- package/lib/private/backend/Module.js +2 -5
- package/lib/private/backend/TAG.d.ts +1 -1
- package/lib/private/backend/TAG.js +1 -1
- package/lib/private/build/App.d.ts +2 -2
- package/lib/private/build/App.js +0 -2
- package/lib/private/build/client/index.js +12 -7
- package/lib/private/build/hook.js +4 -3
- package/lib/private/build/index.js +14 -13
- package/lib/private/build/presets.d.ts +10 -0
- package/lib/private/build/presets.js +39 -0
- package/lib/private/build/server/index.js +5 -7
- package/lib/private/build/server/plugin/assets.js +9 -7
- package/lib/private/build/server/plugin/config.js +11 -4
- package/lib/private/build/server/plugin/db-default.d.ts +4 -0
- package/lib/private/build/server/plugin/db-default.js +45 -0
- package/lib/private/build/server/plugin/frontend.js +4 -2
- package/lib/private/build/server/plugin/live-reload.d.ts +4 -0
- package/lib/private/build/server/plugin/{hot-reload.js → live-reload.js} +4 -4
- package/lib/private/build/server/plugin/native-addons.js +6 -4
- package/lib/private/build/server/plugin/node-imports.js +2 -2
- package/lib/private/build/server/plugin/route.js +2 -2
- package/lib/private/build/server/plugin/store-wrap.js +2 -2
- package/lib/private/build/server/plugin/store.js +6 -6
- package/lib/private/build/server/plugin/stores.js +4 -5
- package/lib/private/build/server/plugin/view.js +4 -4
- package/lib/private/build/server/plugin/views.js +4 -4
- package/lib/private/build/server/plugin/virtual-pages.js +8 -8
- package/lib/private/build/server/plugin/virtual-routes.js +8 -13
- package/lib/private/build/server/plugin/wasm.js +2 -3
- package/lib/private/bye.js +2 -3
- package/lib/private/client/Data.d.ts +1 -1
- package/lib/private/client/ValidateInit.d.ts +1 -3
- package/lib/private/client/ValidationError.d.ts +1 -1
- package/lib/private/client/app.js +2 -1
- package/lib/private/client/create-form.d.ts +31 -0
- package/lib/private/client/create-form.js +124 -0
- package/lib/private/client/extract-issues.d.ts +4 -0
- package/lib/private/client/extract-issues.js +20 -0
- package/lib/private/client/spa/index.d.ts +1 -1
- package/lib/private/client/spa/index.js +4 -5
- package/lib/private/client/{toValidated.d.ts → to-validated.d.ts} +2 -4
- package/lib/private/client/{toValidated.js → to-validated.js} +1 -1
- package/lib/private/client/validate-field.d.ts +3 -0
- package/lib/private/client/validate-field.js +41 -0
- package/lib/private/config/index.d.ts +2 -21
- package/lib/private/config/index.js +3 -1
- package/lib/private/config/schema.d.ts +13 -9
- package/lib/private/config/schema.js +10 -5
- package/lib/private/cookie.d.ts +5 -5
- package/lib/private/cookie.js +10 -14
- package/lib/private/db/As.d.ts +10 -0
- package/lib/private/db/DB.d.ts +33 -0
- package/lib/private/db/DB.js +2 -0
- package/lib/private/db/DataDict.d.ts +5 -0
- package/lib/private/db/DataKey.d.ts +4 -0
- package/lib/private/db/DataValue.d.ts +17 -0
- package/lib/private/db/MemoryDB.d.ts +35 -0
- package/lib/private/db/MemoryDB.js +317 -0
- package/lib/private/db/PK.d.ts +3 -0
- package/lib/private/db/PK.js +2 -0
- package/lib/private/{database → db}/Query.d.ts +2 -2
- package/lib/private/{database → db}/QueryBuilder.d.ts +2 -3
- package/lib/private/db/ReadArgs.d.ts +9 -0
- package/lib/private/db/ReadArgs.js +2 -0
- package/lib/private/db/ReadRelationsArgs.d.ts +6 -0
- package/lib/private/db/ReadRelationsArgs.js +2 -0
- package/lib/private/{database → db}/Schema.d.ts +1 -2
- package/lib/private/db/Sort.d.ts +4 -0
- package/lib/private/{database → db}/TypeMap.d.ts +2 -3
- package/lib/private/{database → db}/Types.d.ts +1 -1
- package/lib/private/db/With.d.ts +16 -0
- package/lib/private/db/With.js +2 -0
- package/lib/private/db/common.d.ts +18 -0
- package/lib/private/db/common.js +36 -0
- package/lib/private/db/error.d.ts +81 -0
- package/lib/private/db/error.js +199 -0
- package/lib/private/db/sql.d.ts +28 -0
- package/lib/private/db/sql.js +177 -0
- package/lib/private/db/storage.d.ts +3 -0
- package/lib/private/db/storage.js +3 -0
- package/lib/private/db/symbol/wrap.js +2 -0
- package/lib/private/db/symbol.js +2 -0
- package/lib/private/db/test.d.ts +4 -0
- package/lib/private/db/test.js +1384 -0
- package/lib/private/frontend/Module.d.ts +6 -6
- package/lib/private/frontend/Module.js +28 -23
- package/lib/private/frontend/Render.d.ts +1 -2
- package/lib/private/frontend/ServerData.d.ts +1 -1
- package/lib/private/frontend/ServerView.d.ts +1 -2
- package/lib/private/frontend/View.d.ts +1 -1
- package/lib/private/frontend/ViewOptions.d.ts +1 -1
- package/lib/private/frontend/ViewResponse.d.ts +1 -1
- package/lib/private/hash.js +0 -1
- package/lib/private/i18n/Catalog.d.ts +5 -2
- package/lib/private/i18n/Formatter.js +20 -8
- package/lib/private/i18n/Module.d.ts +3 -3
- package/lib/private/i18n/Module.js +28 -17
- package/lib/private/i18n/format.d.ts +4 -0
- package/lib/private/i18n/format.js +95 -0
- package/lib/private/i18n/index/client.d.ts +9 -0
- package/lib/private/i18n/index/client.js +152 -0
- package/lib/private/i18n/index/server.d.ts +9 -0
- package/lib/private/i18n/index/server.js +57 -0
- package/lib/private/i18n/index/types.d.ts +33 -0
- package/lib/private/i18n/index/types.js +2 -0
- package/lib/private/i18n/locale.d.ts +9 -3
- package/lib/private/i18n/ordinals.d.ts +1 -1
- package/lib/private/i18n/resolve.d.ts +7 -0
- package/lib/private/i18n/resolve.js +30 -0
- package/lib/private/i18n/schema.d.ts +4 -4
- package/lib/private/i18n/schema.js +6 -10
- package/lib/private/i18n/storage.d.ts +3 -0
- package/lib/private/i18n/storage.js +5 -0
- package/lib/private/i18n/validate.d.ts +2 -0
- package/lib/private/i18n/validate.js +21 -0
- package/lib/private/log.d.ts +1 -0
- package/lib/private/log.js +10 -10
- package/lib/private/module/Hook.d.ts +1 -1
- package/lib/private/module/Next.d.ts +1 -1
- package/lib/private/orm/ForeignKey.d.ts +11 -0
- package/lib/private/orm/ForeignKey.js +22 -0
- package/lib/private/orm/PrimaryKey.d.ts +15 -0
- package/lib/private/orm/PrimaryKey.js +35 -0
- package/lib/private/orm/Set.d.ts +11 -0
- package/lib/private/orm/Set.js +2 -0
- package/lib/private/orm/Store.d.ts +171 -0
- package/lib/private/orm/Store.js +518 -0
- package/lib/private/orm/foreign.d.ts +4 -0
- package/lib/private/orm/foreign.js +5 -0
- package/lib/private/orm/key.d.ts +8 -0
- package/lib/private/orm/key.js +4 -0
- package/lib/private/orm/parse.d.ts +12 -0
- package/lib/private/orm/parse.js +29 -0
- package/lib/private/orm/primary.d.ts +5 -0
- package/lib/private/orm/primary.js +5 -0
- package/lib/private/orm/relation.d.ts +43 -0
- package/lib/private/orm/relation.js +26 -0
- package/lib/private/orm/types.d.ts +18 -0
- package/lib/private/orm/types.js +2 -0
- package/lib/private/orm/wrap.d.ts +5 -0
- package/lib/private/orm/wrap.js +5 -0
- package/lib/private/paths.d.ts +2 -2
- package/lib/private/request/RequestBag.d.ts +1 -1
- package/lib/private/request/RequestBag.js +11 -7
- package/lib/private/request/RequestBody.d.ts +2 -4
- package/lib/private/request/RequestBody.js +8 -11
- package/lib/private/request/RequestContext.d.ts +12 -0
- package/lib/private/request/RequestContext.js +31 -0
- package/lib/private/request/RequestFacade.d.ts +16 -6
- package/lib/private/request/parse.d.ts +2 -2
- package/lib/private/request/parse.js +54 -16
- package/lib/private/request/route.js +37 -21
- package/lib/private/request/router.d.ts +1 -1
- package/lib/private/request/router.js +2 -2
- package/lib/private/request/sContext.d.ts +3 -0
- package/lib/private/request/sContext.js +2 -0
- package/lib/private/response/ResponseFunction.d.ts +1 -2
- package/lib/private/response/ResponseLike.d.ts +1 -1
- package/lib/private/response/binary.d.ts +1 -1
- package/lib/private/response/binary.js +4 -3
- package/lib/private/response/json.d.ts +1 -1
- package/lib/private/response/json.js +2 -2
- package/lib/private/response/redirect.js +1 -1
- package/lib/private/response/respond.js +7 -10
- package/lib/private/response/sse.d.ts +1 -1
- package/lib/private/response/sse.js +2 -2
- package/lib/private/response/text.d.ts +1 -1
- package/lib/private/response/text.js +3 -3
- package/lib/private/response/view.d.ts +2 -2
- package/lib/private/response/view.js +3 -3
- package/lib/private/response.d.ts +1 -1
- package/lib/private/route/Handler.d.ts +1 -1
- package/lib/private/route/hook.d.ts +6 -0
- package/lib/private/route/hook.js +5 -0
- package/lib/private/route/router.d.ts +9 -5
- package/lib/private/route/router.js +34 -17
- package/lib/private/route/wrap.js +2 -2
- package/lib/private/serve/App.d.ts +5 -18
- package/lib/private/serve/App.js +91 -82
- package/lib/private/serve/Init.d.ts +3 -3
- package/lib/private/serve/hook.js +1 -7
- package/lib/private/serve/index.js +6 -2
- package/lib/private/serve/module/Dev.d.ts +1 -1
- package/lib/private/serve/module/Dev.js +9 -5
- package/lib/private/serve/module/Handle.js +2 -2
- package/lib/private/session/Data.d.ts +1 -2
- package/lib/private/session/SessionHandle.d.ts +1 -1
- package/lib/private/session/SessionHandle.js +10 -9
- package/lib/private/session/SessionModule.js +7 -7
- package/lib/private/session/index.d.ts +3 -4
- package/lib/private/session/schema.d.ts +6 -6
- package/lib/private/session/schema.js +5 -4
- package/lib/private/session/storage.d.ts +1 -2
- package/lib/private/session/storage.js +2 -2
- package/lib/private/tags.js +2 -2
- package/lib/private/target/Target.d.ts +1 -1
- package/lib/public/AppFacade.d.ts +2 -0
- package/lib/public/AppFacade.js +2 -0
- package/lib/public/build/presets.d.ts +2 -0
- package/lib/public/build/presets.js +2 -0
- package/lib/public/build/transform.d.ts +2 -0
- package/lib/public/build/transform.js +2 -0
- package/lib/public/client.d.ts +14 -0
- package/lib/public/client.js +10 -0
- package/lib/public/db/MemoryDB.d.ts +2 -0
- package/lib/public/db/MemoryDB.js +2 -0
- package/lib/public/db/error.d.ts +2 -0
- package/lib/public/db/error.js +2 -0
- package/lib/public/db/sql.d.ts +2 -0
- package/lib/public/db/sql.js +2 -0
- package/lib/public/db/test.d.ts +2 -0
- package/lib/public/db/test.js +2 -0
- package/lib/public/db.d.ts +12 -0
- package/lib/public/db.js +2 -0
- package/lib/public/orm/Store.d.ts +2 -0
- package/lib/public/orm/Store.js +2 -0
- package/lib/public/orm/key.d.ts +2 -0
- package/lib/public/orm/key.js +2 -0
- package/lib/public/orm/relation.d.ts +2 -0
- package/lib/public/orm/relation.js +2 -0
- package/lib/public/orm/wrap.d.ts +2 -0
- package/lib/public/orm/wrap.js +2 -0
- package/lib/public/request.d.ts +4 -0
- package/lib/public/request.js +2 -0
- package/lib/public/response.d.ts +22 -0
- package/lib/public/response.js +19 -0
- package/lib/public/route/hook.d.ts +2 -0
- package/lib/public/route/hook.js +2 -0
- package/package.json +28 -23
- package/lib/private/build/client/reload.d.ts +0 -7
- package/lib/private/build/client/reload.js +0 -6
- package/lib/private/build/server/plugin/database-default.d.ts +0 -4
- package/lib/private/build/server/plugin/database-default.js +0 -48
- package/lib/private/build/server/plugin/hot-reload.d.ts +0 -4
- package/lib/private/client/validate.d.ts +0 -3
- package/lib/private/client/validate.js +0 -54
- package/lib/private/database/As.d.ts +0 -7
- package/lib/private/database/Binds.d.ts +0 -4
- package/lib/private/database/Binds.js +0 -2
- package/lib/private/database/Changes.d.ts +0 -11
- package/lib/private/database/Changes.js +0 -2
- package/lib/private/database/ColumnTypes.d.ts +0 -11
- package/lib/private/database/ColumnTypes.js +0 -2
- package/lib/private/database/DataDict.d.ts +0 -5
- package/lib/private/database/DataKey.d.ts +0 -4
- package/lib/private/database/DataValue.d.ts +0 -5
- package/lib/private/database/Database.d.ts +0 -56
- package/lib/private/database/Database.js +0 -153
- package/lib/private/database/InMemoryDatabase.d.ts +0 -37
- package/lib/private/database/InMemoryDatabase.js +0 -181
- package/lib/private/database/Sort.d.ts +0 -4
- package/lib/private/database/Store.d.ts +0 -172
- package/lib/private/database/Store.js +0 -261
- package/lib/private/database/storage.d.ts +0 -4
- package/lib/private/database/storage.js +0 -3
- package/lib/private/database/symbol/wrap.js +0 -2
- package/lib/private/database/symbol.js +0 -2
- package/lib/private/database/test.d.ts +0 -4
- package/lib/private/database/test.js +0 -678
- package/lib/private/database/wrap.d.ts +0 -5
- package/lib/private/database/wrap.js +0 -5
- package/lib/private/i18n/index.d.ts +0 -28
- package/lib/private/i18n/index.js +0 -236
- package/lib/private/route/guard.d.ts +0 -4
- package/lib/private/route/guard.js +0 -22
- package/lib/private/wasm/API.d.ts +0 -7
- package/lib/private/wasm/API.js +0 -2
- package/lib/private/wasm/BufferViewSource.d.ts +0 -4
- package/lib/private/wasm/BufferViewSource.js +0 -2
- package/lib/private/wasm/Exports.d.ts +0 -23
- package/lib/private/wasm/Exports.js +0 -2
- package/lib/private/wasm/I32.d.ts +0 -5
- package/lib/private/wasm/I32.js +0 -2
- package/lib/private/wasm/I32_SIZE.d.ts +0 -3
- package/lib/private/wasm/I32_SIZE.js +0 -2
- package/lib/private/wasm/Instantiation.d.ts +0 -12
- package/lib/private/wasm/Instantiation.js +0 -2
- package/lib/private/wasm/Tagged.d.ts +0 -7
- package/lib/private/wasm/Tagged.js +0 -2
- package/lib/private/wasm/buffersize.d.ts +0 -2
- package/lib/private/wasm/buffersize.js +0 -5
- package/lib/private/wasm/decode-bytes.d.ts +0 -3
- package/lib/private/wasm/decode-bytes.js +0 -5
- package/lib/private/wasm/decode-json.d.ts +0 -7
- package/lib/private/wasm/decode-json.js +0 -11
- package/lib/private/wasm/decode-option.d.ts +0 -5
- package/lib/private/wasm/decode-option.js +0 -10
- package/lib/private/wasm/decode-response.d.ts +0 -19
- package/lib/private/wasm/decode-response.js +0 -90
- package/lib/private/wasm/decode-string.d.ts +0 -3
- package/lib/private/wasm/decode-string.js +0 -5
- package/lib/private/wasm/decode-websocket-close.d.ts +0 -5
- package/lib/private/wasm/decode-websocket-close.js +0 -6
- package/lib/private/wasm/decode-websocket-send.d.ts +0 -6
- package/lib/private/wasm/decode-websocket-send.js +0 -19
- package/lib/private/wasm/encode-buffer.d.ts +0 -3
- package/lib/private/wasm/encode-buffer.js +0 -6
- package/lib/private/wasm/encode-request.d.ts +0 -9
- package/lib/private/wasm/encode-request.js +0 -195
- package/lib/private/wasm/encode-session.d.ts +0 -3
- package/lib/private/wasm/encode-session.js +0 -25
- package/lib/private/wasm/encode-string-map.d.ts +0 -5
- package/lib/private/wasm/encode-string-map.js +0 -14
- package/lib/private/wasm/encode-string.d.ts +0 -13
- package/lib/private/wasm/encode-string.js +0 -17
- package/lib/private/wasm/encode-url.d.ts +0 -11
- package/lib/private/wasm/encode-url.js +0 -14
- package/lib/private/wasm/encode-websocket-close.d.ts +0 -2
- package/lib/private/wasm/encode-websocket-close.js +0 -7
- package/lib/private/wasm/encode-websocket-message.d.ts +0 -4
- package/lib/private/wasm/encode-websocket-message.js +0 -30
- package/lib/private/wasm/encode-websocket-open.d.ts +0 -2
- package/lib/private/wasm/encode-websocket-open.js +0 -7
- package/lib/private/wasm/filesize.d.ts +0 -2
- package/lib/private/wasm/filesize.js +0 -8
- package/lib/private/wasm/instantiate.d.ts +0 -37
- package/lib/private/wasm/instantiate.js +0 -408
- package/lib/private/wasm/open-websocket.d.ts +0 -4
- package/lib/private/wasm/open-websocket.js +0 -26
- package/lib/private/wasm/stringsize.d.ts +0 -3
- package/lib/private/wasm/stringsize.js +0 -5
- package/lib/private/wasm/urlsize.d.ts +0 -2
- package/lib/private/wasm/urlsize.js +0 -5
- package/lib/public/Database.d.ts +0 -2
- package/lib/public/Database.js +0 -2
- package/lib/public/client/ValidateInit.d.ts +0 -2
- package/lib/public/client/ValidateInit.js +0 -2
- package/lib/public/client/ValidateUpdater.d.ts +0 -2
- package/lib/public/client/ValidateUpdater.js +0 -2
- package/lib/public/client/ValidationError.d.ts +0 -2
- package/lib/public/client/ValidationError.js +0 -2
- package/lib/public/client/toValidated.d.ts +0 -2
- package/lib/public/client/toValidated.js +0 -2
- package/lib/public/client/validate.d.ts +0 -2
- package/lib/public/client/validate.js +0 -2
- package/lib/public/database/As.d.ts +0 -2
- package/lib/public/database/As.js +0 -2
- package/lib/public/database/DataDict.d.ts +0 -2
- package/lib/public/database/DataDict.js +0 -2
- package/lib/public/database/InMemoryDatabase.d.ts +0 -2
- package/lib/public/database/InMemoryDatabase.js +0 -2
- package/lib/public/database/Sort.d.ts +0 -2
- package/lib/public/database/Sort.js +0 -2
- package/lib/public/database/Store.d.ts +0 -2
- package/lib/public/database/Store.js +0 -2
- package/lib/public/database/TypeMap.d.ts +0 -2
- package/lib/public/database/TypeMap.js +0 -2
- package/lib/public/database/Types.d.ts +0 -2
- package/lib/public/database/Types.js +0 -2
- package/lib/public/database/test.d.ts +0 -2
- package/lib/public/database/test.js +0 -2
- package/lib/public/database/wrap.d.ts +0 -2
- package/lib/public/database/wrap.js +0 -2
- package/lib/public/request/RequestBody.d.ts +0 -2
- package/lib/public/request/RequestBody.js +0 -2
- package/lib/public/request/RequestFacade.d.ts +0 -2
- package/lib/public/request/RequestFacade.js +0 -2
- package/lib/public/request/Verb.d.ts +0 -2
- package/lib/public/request/Verb.js +0 -2
- package/lib/public/response/ResponseFunction.d.ts +0 -2
- package/lib/public/response/ResponseFunction.js +0 -2
- package/lib/public/response/ResponseLike.d.ts +0 -2
- package/lib/public/response/ResponseLike.js +0 -2
- package/lib/public/response/binary.d.ts +0 -2
- package/lib/public/response/binary.js +0 -2
- package/lib/public/response/error.d.ts +0 -2
- package/lib/public/response/error.js +0 -2
- package/lib/public/response/json.d.ts +0 -2
- package/lib/public/response/json.js +0 -2
- package/lib/public/response/redirect.d.ts +0 -2
- package/lib/public/response/redirect.js +0 -2
- package/lib/public/response/sse.d.ts +0 -2
- package/lib/public/response/sse.js +0 -2
- package/lib/public/response/text.d.ts +0 -2
- package/lib/public/response/text.js +0 -2
- package/lib/public/response/view.d.ts +0 -2
- package/lib/public/response/view.js +0 -2
- package/lib/public/response/ws.d.ts +0 -2
- package/lib/public/response/ws.js +0 -2
- package/lib/public/wasm/decode-json.d.ts +0 -5
- package/lib/public/wasm/decode-json.js +0 -3
- package/lib/public/wasm/decode-response.d.ts +0 -3
- package/lib/public/wasm/decode-response.js +0 -3
- package/lib/public/wasm/encode-request.d.ts +0 -3
- package/lib/public/wasm/encode-request.js +0 -3
- package/lib/public/wasm/encode-session.d.ts +0 -3
- package/lib/public/wasm/encode-session.js +0 -3
- package/lib/public/wasm/instantiate.d.ts +0 -4
- package/lib/public/wasm/instantiate.js +0 -3
- /package/lib/private/{database → db}/As.js +0 -0
- /package/lib/private/{database → db}/DataDict.js +0 -0
- /package/lib/private/{database → db}/DataKey.js +0 -0
- /package/lib/private/{database → db}/DataValue.js +0 -0
- /package/lib/private/{database → db}/Query.js +0 -0
- /package/lib/private/{database → db}/QueryBuilder.js +0 -0
- /package/lib/private/{database → db}/Schema.js +0 -0
- /package/lib/private/{database → db}/Sort.js +0 -0
- /package/lib/private/{database → db}/TypeMap.js +0 -0
- /package/lib/private/{database → db}/Types.js +0 -0
- /package/lib/private/{database → db}/primary.d.ts +0 -0
- /package/lib/private/{database → db}/primary.js +0 -0
- /package/lib/private/{database → db}/symbol/wrap.d.ts +0 -0
- /package/lib/private/{database → db}/symbol.d.ts +0 -0
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
import E from "#db/error";
|
|
2
|
+
import wrap from "#db/symbol/wrap";
|
|
3
|
+
import parse from "#orm/parse";
|
|
4
|
+
import assert from "@rcompat/assert";
|
|
5
|
+
import is from "@rcompat/is";
|
|
6
|
+
import StoreType from "pema/StoreType";
|
|
7
|
+
const NUMBER_KEYS = [
|
|
8
|
+
"u8", "u16", "u32",
|
|
9
|
+
"i8", "i16", "i32",
|
|
10
|
+
"f32", "f64"
|
|
11
|
+
];
|
|
12
|
+
const BIGINT_KEYS = ["u64", "u128", "i64", "i128"];
|
|
13
|
+
const is_number_key = (d) => NUMBER_KEYS.includes(d);
|
|
14
|
+
const is_bigint_key = (d) => BIGINT_KEYS.includes(d);
|
|
15
|
+
const VALID_IDENTIFIER = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
|
|
16
|
+
function guard_options(options, allowed) {
|
|
17
|
+
if (options === undefined)
|
|
18
|
+
return;
|
|
19
|
+
const allowed_set = new Set(allowed);
|
|
20
|
+
for (const k of Object.keys(options)) {
|
|
21
|
+
if (!allowed_set.has(k))
|
|
22
|
+
throw E.option_unknown(k);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const INT_LIMITS = {
|
|
26
|
+
u8: [0, 255],
|
|
27
|
+
u16: [0, 65535],
|
|
28
|
+
u32: [0, 4294967295],
|
|
29
|
+
i8: [-128, 127],
|
|
30
|
+
i16: [-32768, 32767],
|
|
31
|
+
i32: [-2147483648, 2147483647],
|
|
32
|
+
};
|
|
33
|
+
const BIGINT_LIMITS = {
|
|
34
|
+
u64: [0n, (1n << 64n) - 1n],
|
|
35
|
+
i64: [-(1n << 63n), (1n << 63n) - 1n],
|
|
36
|
+
u128: [0n, (1n << 128n) - 1n],
|
|
37
|
+
i128: [-(1n << 127n), (1n << 127n) - 1n],
|
|
38
|
+
};
|
|
39
|
+
function assert_number_value(key, datatype, value) {
|
|
40
|
+
if (!is.finite(value))
|
|
41
|
+
throw E.where_invalid_value(key, value);
|
|
42
|
+
if (datatype in INT_LIMITS) {
|
|
43
|
+
if (!is.safeint(value))
|
|
44
|
+
throw E.where_invalid_value(key, value);
|
|
45
|
+
const [min, max] = INT_LIMITS[datatype];
|
|
46
|
+
if (value < min || value > max)
|
|
47
|
+
throw E.where_invalid_value(key, value);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function assert_bigint_value(key, datatype, value) {
|
|
51
|
+
const [min, max] = BIGINT_LIMITS[datatype];
|
|
52
|
+
if (value < min || value > max)
|
|
53
|
+
throw E.where_invalid_value(key, value);
|
|
54
|
+
}
|
|
55
|
+
const registry = new Map();
|
|
56
|
+
const STRING_OPS = ["$like", "$ilike"];
|
|
57
|
+
const NUMBER_OPS = ["$gt", "$gte", "$lt", "$lte", "$ne"];
|
|
58
|
+
const BIGINT_OPS = ["$gt", "$gte", "$lt", "$lte", "$ne"];
|
|
59
|
+
const DATE_OPS = ["$before", "$after", "$ne"];
|
|
60
|
+
/**
|
|
61
|
+
* Database-backed store.
|
|
62
|
+
*
|
|
63
|
+
* A `Store` exposes a typed, validated interface over a relational or
|
|
64
|
+
* document database table/collection. It pairs a Pema schema with a uniform
|
|
65
|
+
* CRUD/query API.
|
|
66
|
+
*/
|
|
67
|
+
export default class Store {
|
|
68
|
+
#schema;
|
|
69
|
+
#type;
|
|
70
|
+
#types;
|
|
71
|
+
#nullables;
|
|
72
|
+
#db;
|
|
73
|
+
#name;
|
|
74
|
+
#pk;
|
|
75
|
+
#generate_pk;
|
|
76
|
+
#fks;
|
|
77
|
+
#relations;
|
|
78
|
+
constructor(input, config = {}) {
|
|
79
|
+
const { pk, generate_pk, fks, schema } = parse(input);
|
|
80
|
+
this.#schema = schema;
|
|
81
|
+
this.#type = new StoreType(schema, pk);
|
|
82
|
+
this.#types = Object.fromEntries(Object.entries(schema).map(([key, value]) => [key, value.datatype]));
|
|
83
|
+
this.#name = config.name;
|
|
84
|
+
this.#db = config.db;
|
|
85
|
+
this.#pk = pk;
|
|
86
|
+
this.#generate_pk = generate_pk;
|
|
87
|
+
this.#fks = fks;
|
|
88
|
+
this.#relations = config.relations ?? {};
|
|
89
|
+
this.#nullables = new Set(Object.entries(this.#type.properties)
|
|
90
|
+
.filter(([, v]) => v.nullable)
|
|
91
|
+
.map(([k]) => k));
|
|
92
|
+
registry.set(input, this);
|
|
93
|
+
}
|
|
94
|
+
static new(input, config = {}) {
|
|
95
|
+
return new Store(input, config);
|
|
96
|
+
}
|
|
97
|
+
get #as() {
|
|
98
|
+
return {
|
|
99
|
+
table: this.name,
|
|
100
|
+
pk: this.#pk,
|
|
101
|
+
generate_pk: this.#generate_pk,
|
|
102
|
+
types: this.#types,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
get collection() {
|
|
106
|
+
const db = this.db;
|
|
107
|
+
const name = this.name;
|
|
108
|
+
const schema = this.#schema;
|
|
109
|
+
const as = this.#as;
|
|
110
|
+
return {
|
|
111
|
+
create: () => db.schema.create(as, schema),
|
|
112
|
+
delete: () => db.schema.delete(name),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
get infer() {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
get schema() {
|
|
119
|
+
return this.#schema;
|
|
120
|
+
}
|
|
121
|
+
get type() {
|
|
122
|
+
return this.#type;
|
|
123
|
+
}
|
|
124
|
+
[wrap](name, db) {
|
|
125
|
+
this.#db ??= db;
|
|
126
|
+
this.#name ??= name;
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
get db() {
|
|
130
|
+
if (this.#db === undefined)
|
|
131
|
+
throw E.db_missing();
|
|
132
|
+
return this.#db;
|
|
133
|
+
}
|
|
134
|
+
get types() {
|
|
135
|
+
return this.#types;
|
|
136
|
+
}
|
|
137
|
+
get name() {
|
|
138
|
+
const name = this.#name;
|
|
139
|
+
if (name === undefined)
|
|
140
|
+
throw E.store_name_required();
|
|
141
|
+
if (!VALID_IDENTIFIER.test(name))
|
|
142
|
+
throw E.identifier_invalid(name);
|
|
143
|
+
return name;
|
|
144
|
+
}
|
|
145
|
+
#parse_query(query, types) {
|
|
146
|
+
const { where, select, sort, limit } = query;
|
|
147
|
+
if (where !== undefined)
|
|
148
|
+
this.#parse_where(where, types);
|
|
149
|
+
if (select !== undefined)
|
|
150
|
+
this.#parse_select(select, types);
|
|
151
|
+
if (sort !== undefined)
|
|
152
|
+
this.#parse_sort(sort, types);
|
|
153
|
+
if (limit !== undefined && !is.uint(limit))
|
|
154
|
+
throw E.limit_invalid();
|
|
155
|
+
}
|
|
156
|
+
#with(options) {
|
|
157
|
+
if (options === undefined)
|
|
158
|
+
return undefined;
|
|
159
|
+
const plan = {};
|
|
160
|
+
for (const [name, query] of Object.entries(options)) {
|
|
161
|
+
if (query === undefined)
|
|
162
|
+
continue;
|
|
163
|
+
const relation = this.#relations[name];
|
|
164
|
+
if (relation === undefined)
|
|
165
|
+
throw E.relation_unknown(name);
|
|
166
|
+
const store = registry.get(relation.schema);
|
|
167
|
+
if (store === undefined)
|
|
168
|
+
throw E.unregistered_schema();
|
|
169
|
+
const { pk: target_pk } = parse(relation.schema);
|
|
170
|
+
const target_types = store.types;
|
|
171
|
+
const base = {
|
|
172
|
+
as: {
|
|
173
|
+
table: store.name,
|
|
174
|
+
pk: target_pk,
|
|
175
|
+
types: target_types,
|
|
176
|
+
},
|
|
177
|
+
kind: relation.type,
|
|
178
|
+
fk: relation.fk,
|
|
179
|
+
reverse: "reverse" in relation && relation.reverse === true,
|
|
180
|
+
};
|
|
181
|
+
if (query === true) {
|
|
182
|
+
plan[name] = { ...base, where: {} };
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
this.#parse_query(query, target_types);
|
|
186
|
+
plan[name] = {
|
|
187
|
+
...base,
|
|
188
|
+
where: query.where ?? {},
|
|
189
|
+
fields: query.select ? [...query.select] : undefined,
|
|
190
|
+
sort: query.sort,
|
|
191
|
+
limit: query.limit,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
return plan;
|
|
195
|
+
}
|
|
196
|
+
#parse_where(where, types) {
|
|
197
|
+
if (!is.dict(where))
|
|
198
|
+
throw E.where_invalid();
|
|
199
|
+
for (const [k, value] of Object.entries(where)) {
|
|
200
|
+
if (!VALID_IDENTIFIER.test(k))
|
|
201
|
+
throw E.identifier_invalid(k);
|
|
202
|
+
if (!(k in types))
|
|
203
|
+
throw E.field_unknown(k, "where");
|
|
204
|
+
if (value === undefined)
|
|
205
|
+
throw E.field_undefined(k, "where");
|
|
206
|
+
const datatype = types[k];
|
|
207
|
+
// null criteria (IS NULL semantics)
|
|
208
|
+
if (value === null)
|
|
209
|
+
continue;
|
|
210
|
+
// arrays are always invalid
|
|
211
|
+
if (is.array(value))
|
|
212
|
+
throw E.where_invalid_value(k, value);
|
|
213
|
+
if (is.dict(value)) {
|
|
214
|
+
const ops = Object.entries(value);
|
|
215
|
+
if (ops.length === 0)
|
|
216
|
+
throw E.operator_empty(k);
|
|
217
|
+
for (const [op, op_val] of ops) {
|
|
218
|
+
if (op_val === undefined)
|
|
219
|
+
throw E.field_undefined(k, "where");
|
|
220
|
+
if (datatype === "string" || datatype === "time") {
|
|
221
|
+
if (!STRING_OPS.includes(op))
|
|
222
|
+
throw E.operator_unknown(k, op);
|
|
223
|
+
if (!is.string(op_val))
|
|
224
|
+
throw E.wrong_type("string", k, op_val, op);
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
if (is_number_key(datatype)) {
|
|
228
|
+
if (!NUMBER_OPS.includes(op))
|
|
229
|
+
throw E.operator_unknown(k, op);
|
|
230
|
+
if (!is.number(op_val))
|
|
231
|
+
throw E.wrong_type("number", k, op_val, op);
|
|
232
|
+
assert_number_value(k, datatype, op_val);
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
if (is_bigint_key(datatype)) {
|
|
236
|
+
if (!BIGINT_OPS.includes(op))
|
|
237
|
+
throw E.operator_unknown(k, op);
|
|
238
|
+
if (!is.bigint(op_val))
|
|
239
|
+
throw E.wrong_type("bigint", k, op_val, op);
|
|
240
|
+
assert_bigint_value(k, datatype, op_val);
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
if (datatype === "datetime") {
|
|
244
|
+
if (!DATE_OPS.includes(op))
|
|
245
|
+
throw E.operator_unknown(k, op);
|
|
246
|
+
if (!is.date(op_val))
|
|
247
|
+
throw E.wrong_type("date", k, op_val, op);
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
// url/boolean/blob: no operator objects
|
|
251
|
+
throw E.operator_unknown(k, op);
|
|
252
|
+
}
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
switch (datatype) {
|
|
256
|
+
case "string":
|
|
257
|
+
case "time":
|
|
258
|
+
if (!is.string(value))
|
|
259
|
+
throw E.wrong_type("string", k, value);
|
|
260
|
+
continue;
|
|
261
|
+
case "u8":
|
|
262
|
+
case "u16":
|
|
263
|
+
case "u32":
|
|
264
|
+
case "i8":
|
|
265
|
+
case "i16":
|
|
266
|
+
case "i32":
|
|
267
|
+
case "f32":
|
|
268
|
+
case "f64":
|
|
269
|
+
if (!is.number(value))
|
|
270
|
+
throw E.wrong_type("number", k, value);
|
|
271
|
+
assert_number_value(k, datatype, value);
|
|
272
|
+
continue;
|
|
273
|
+
case "u64":
|
|
274
|
+
case "u128":
|
|
275
|
+
case "i64":
|
|
276
|
+
case "i128":
|
|
277
|
+
if (!is.bigint(value))
|
|
278
|
+
throw E.wrong_type("bigint", k, value);
|
|
279
|
+
assert_bigint_value(k, datatype, value);
|
|
280
|
+
continue;
|
|
281
|
+
case "datetime":
|
|
282
|
+
if (!is.date(value))
|
|
283
|
+
throw E.wrong_type("date", k, value);
|
|
284
|
+
continue;
|
|
285
|
+
case "boolean":
|
|
286
|
+
if (!is.boolean(value))
|
|
287
|
+
throw E.wrong_type("boolean", k, value);
|
|
288
|
+
continue;
|
|
289
|
+
case "url":
|
|
290
|
+
if (!is.url(value))
|
|
291
|
+
throw E.wrong_type("url", k, value);
|
|
292
|
+
continue;
|
|
293
|
+
case "blob":
|
|
294
|
+
if (!is.blob(value))
|
|
295
|
+
throw E.wrong_type("blob", k, value);
|
|
296
|
+
continue;
|
|
297
|
+
default:
|
|
298
|
+
throw E.where_invalid_value(k, value);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
#parse_select(select, types) {
|
|
303
|
+
if (!is.array(select))
|
|
304
|
+
throw E.select_invalid();
|
|
305
|
+
if (select.length === 0)
|
|
306
|
+
throw E.select_empty();
|
|
307
|
+
const seen = new Set();
|
|
308
|
+
for (const [i, v] of select.entries()) {
|
|
309
|
+
if (!is.string(v))
|
|
310
|
+
throw E.select_invalid_value(i, v);
|
|
311
|
+
if (!VALID_IDENTIFIER.test(v))
|
|
312
|
+
throw E.identifier_invalid(v);
|
|
313
|
+
if (!(v in types))
|
|
314
|
+
throw E.field_unknown(v, "select");
|
|
315
|
+
// duplicate isn't harmful, but usually a thought error
|
|
316
|
+
if (seen.has(v))
|
|
317
|
+
throw E.field_duplicate(v, "select");
|
|
318
|
+
seen.add(v);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
#parse_sort(sort, types) {
|
|
322
|
+
if (!is.dict(sort))
|
|
323
|
+
throw E.sort_invalid();
|
|
324
|
+
const keys = Object.keys(sort);
|
|
325
|
+
if (keys.length === 0)
|
|
326
|
+
throw E.sort_empty();
|
|
327
|
+
for (const [k, direction] of Object.entries(sort)) {
|
|
328
|
+
if (!is.string(direction))
|
|
329
|
+
throw E.sort_invalid_value(k, direction);
|
|
330
|
+
if (!(k in types))
|
|
331
|
+
throw E.field_unknown(k, "sort");
|
|
332
|
+
const l = direction.toLowerCase();
|
|
333
|
+
if (l !== "asc" && l !== "desc")
|
|
334
|
+
throw E.sort_invalid_value(k, direction);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
#parse_insert(record) {
|
|
338
|
+
for (const [k, v] of Object.entries(record)) {
|
|
339
|
+
if (!(k in this.#types))
|
|
340
|
+
throw E.field_unknown(k, "insert");
|
|
341
|
+
if (v === undefined)
|
|
342
|
+
throw E.field_undefined(k, "insert");
|
|
343
|
+
if (v === null)
|
|
344
|
+
throw E.null_not_allowed(k);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Count records
|
|
349
|
+
*/
|
|
350
|
+
async count(options) {
|
|
351
|
+
this.#parse_query(options ?? {}, this.#types);
|
|
352
|
+
if (is.defined(options) && "with" in options)
|
|
353
|
+
throw E.count_with_invalid();
|
|
354
|
+
return await this.db.read(this.#as, {
|
|
355
|
+
count: true,
|
|
356
|
+
where: (options?.where ?? {}),
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Check whether a record with the given primary key exists.
|
|
361
|
+
*/
|
|
362
|
+
async has(pkv) {
|
|
363
|
+
if (this.#pk === null)
|
|
364
|
+
throw E.pk_undefined(this.name);
|
|
365
|
+
return (await this.count({ where: { [this.#pk]: pkv } })) === 1;
|
|
366
|
+
}
|
|
367
|
+
async get(pkv, options) {
|
|
368
|
+
const pk = this.#pk;
|
|
369
|
+
if (pk === null)
|
|
370
|
+
throw E.pk_undefined(this.name);
|
|
371
|
+
guard_options(options, ["select", "with"]);
|
|
372
|
+
this.#parse_query(options ?? {}, this.#types);
|
|
373
|
+
const $with = this.#with(options?.with);
|
|
374
|
+
const records = await this.db.read(this.#as, {
|
|
375
|
+
where: { [pk]: pkv },
|
|
376
|
+
fields: options?.select ? [...options.select] : undefined,
|
|
377
|
+
with: $with,
|
|
378
|
+
});
|
|
379
|
+
assert.true(records.length <= 1);
|
|
380
|
+
if (records.length === 0) {
|
|
381
|
+
const err = E.record_not_found(pk, pkv);
|
|
382
|
+
err.not_found = true;
|
|
383
|
+
throw err;
|
|
384
|
+
}
|
|
385
|
+
const raw = records[0];
|
|
386
|
+
// if projected, keep it as-is (no parse)
|
|
387
|
+
if (options?.select)
|
|
388
|
+
return raw;
|
|
389
|
+
// parse *only* base fields (exclude relation keys)
|
|
390
|
+
const base_only = Object.fromEntries(Object.entries(raw)
|
|
391
|
+
.filter(([k]) => k in this.#types));
|
|
392
|
+
const parsed = this.#type.parse(base_only);
|
|
393
|
+
if ($with === undefined)
|
|
394
|
+
return parsed;
|
|
395
|
+
return {
|
|
396
|
+
...parsed,
|
|
397
|
+
...Object.fromEntries(Object.keys($with).map(k => [k, raw[k]])),
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
async try(pkv, options) {
|
|
401
|
+
try {
|
|
402
|
+
return await this.get(pkv, options);
|
|
403
|
+
}
|
|
404
|
+
catch (error) {
|
|
405
|
+
if (error?.not_found === true)
|
|
406
|
+
return undefined;
|
|
407
|
+
throw error;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Insert a single record.
|
|
412
|
+
*/
|
|
413
|
+
async insert(record) {
|
|
414
|
+
assert.dict(record);
|
|
415
|
+
this.#parse_insert(record);
|
|
416
|
+
const entries = Object.entries(record);
|
|
417
|
+
const to_parse = Object.fromEntries(entries.filter(([k, v]) => !(v === null && this.#nullables.has(k))));
|
|
418
|
+
return this.db.create(this.#as, this.#type.parse(to_parse));
|
|
419
|
+
}
|
|
420
|
+
async update(arg0, options) {
|
|
421
|
+
const by_pk = options !== undefined;
|
|
422
|
+
const set = by_pk
|
|
423
|
+
? options.set
|
|
424
|
+
: arg0.set;
|
|
425
|
+
if (!is.dict(set) || is.empty(set))
|
|
426
|
+
throw E.set_empty();
|
|
427
|
+
const pk = this.#pk;
|
|
428
|
+
if (pk !== null && pk in set)
|
|
429
|
+
throw E.pk_immutable(pk);
|
|
430
|
+
for (const [k, v] of Object.entries(set)) {
|
|
431
|
+
if (!(k in this.#types))
|
|
432
|
+
throw E.field_unknown(k, "set");
|
|
433
|
+
if (v === null && !this.#nullables.has(k))
|
|
434
|
+
throw E.null_not_allowed(k);
|
|
435
|
+
}
|
|
436
|
+
const entries = Object.entries(set);
|
|
437
|
+
const to_parse = Object.fromEntries(entries
|
|
438
|
+
.filter(([key, value]) => value !== null || !this.#nullables.has(key)));
|
|
439
|
+
const nulls = Object.fromEntries(entries
|
|
440
|
+
.filter(([key, value]) => value === null && this.#nullables.has(key)));
|
|
441
|
+
const parsed = {
|
|
442
|
+
...this.#type.partial().parse(to_parse),
|
|
443
|
+
...nulls,
|
|
444
|
+
};
|
|
445
|
+
if (by_pk) {
|
|
446
|
+
return this.#update_1(arg0, parsed);
|
|
447
|
+
}
|
|
448
|
+
const where = arg0.where;
|
|
449
|
+
if (where !== undefined)
|
|
450
|
+
this.#parse_where(where, this.#types);
|
|
451
|
+
return this.#update_n((where ?? {}), parsed);
|
|
452
|
+
}
|
|
453
|
+
async #update_1(pkv, set) {
|
|
454
|
+
const pk = this.#pk;
|
|
455
|
+
if (pk === null)
|
|
456
|
+
throw E.pk_undefined(this.name);
|
|
457
|
+
const n = await this.db.update(this.#as, {
|
|
458
|
+
set: set,
|
|
459
|
+
where: { [pk]: pkv },
|
|
460
|
+
});
|
|
461
|
+
assert.true(n === 1, `${n} records updated instead of 1`);
|
|
462
|
+
}
|
|
463
|
+
async #update_n(where, set) {
|
|
464
|
+
return await this.db.update(this.#as, {
|
|
465
|
+
set: set,
|
|
466
|
+
where: where,
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
async delete(pkv_or_options) {
|
|
470
|
+
if (is.dict(pkv_or_options)) {
|
|
471
|
+
const where = pkv_or_options.where;
|
|
472
|
+
if (!is.dict(where) || is.empty(where))
|
|
473
|
+
throw E.where_required();
|
|
474
|
+
this.#parse_where(where, this.#types);
|
|
475
|
+
return this.#delete_n(where);
|
|
476
|
+
}
|
|
477
|
+
return this.#delete_1(pkv_or_options);
|
|
478
|
+
}
|
|
479
|
+
async #delete_1(pkv) {
|
|
480
|
+
const pk = this.#pk;
|
|
481
|
+
if (pk === null)
|
|
482
|
+
throw E.pk_undefined(this.name);
|
|
483
|
+
const n = await this.db.delete(this.#as, { where: { [pk]: pkv } });
|
|
484
|
+
assert.true(n === 1, `${n} records deleted instead of 1`);
|
|
485
|
+
}
|
|
486
|
+
async #delete_n(where) {
|
|
487
|
+
return await this.db.delete(this.#as, { where: where });
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Find matching records
|
|
491
|
+
*
|
|
492
|
+
*/
|
|
493
|
+
async find(options) {
|
|
494
|
+
guard_options(options, ["where", "select", "sort", "limit", "with"]);
|
|
495
|
+
this.#parse_query(options ?? {}, this.#types);
|
|
496
|
+
const result = await this.db.read(this.#as, {
|
|
497
|
+
where: options?.where ?? {},
|
|
498
|
+
fields: options?.select ? [...options.select] : undefined,
|
|
499
|
+
limit: options?.limit,
|
|
500
|
+
sort: options?.sort,
|
|
501
|
+
with: this.#with(options?.with),
|
|
502
|
+
});
|
|
503
|
+
return result;
|
|
504
|
+
}
|
|
505
|
+
toJSON() {
|
|
506
|
+
return this.#type.toJSON();
|
|
507
|
+
}
|
|
508
|
+
extend(extensor) {
|
|
509
|
+
const extensions = extensor(this);
|
|
510
|
+
const keys = Object.keys(extensions);
|
|
511
|
+
for (const k of keys)
|
|
512
|
+
if (k in this)
|
|
513
|
+
throw E.key_duplicate(k);
|
|
514
|
+
Object.assign(this, extensions);
|
|
515
|
+
return this;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
//# sourceMappingURL=Store.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import ForeignKey from "#orm/ForeignKey";
|
|
2
|
+
import type { StoreInput } from "#orm/types";
|
|
3
|
+
import type { Dict } from "@rcompat/type";
|
|
4
|
+
import type { Storable } from "pema";
|
|
5
|
+
import type PK from "#db/PK";
|
|
6
|
+
export default function parse(input: StoreInput): {
|
|
7
|
+
pk: PK;
|
|
8
|
+
generate_pk: boolean;
|
|
9
|
+
fks: Map<string, ForeignKey<Storable<keyof import("pema").DataType>>>;
|
|
10
|
+
schema: Dict<Storable<keyof import("pema").DataType>>;
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=parse.d.ts.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import fail from "#fail";
|
|
2
|
+
import ForeignKey from "#orm/ForeignKey";
|
|
3
|
+
import PrimaryKey from "#orm/PrimaryKey";
|
|
4
|
+
const is_pk = (x) => x instanceof PrimaryKey;
|
|
5
|
+
const is_fk = (x) => x instanceof ForeignKey;
|
|
6
|
+
export default function parse(input) {
|
|
7
|
+
let pk = null;
|
|
8
|
+
let generate_pk = true;
|
|
9
|
+
const fks = new Map();
|
|
10
|
+
const schema = {};
|
|
11
|
+
for (const [key, value] of Object.entries(input)) {
|
|
12
|
+
if (is_pk(value)) {
|
|
13
|
+
if (pk !== null)
|
|
14
|
+
throw fail("multiple primary keys: {0}, {1}", pk, key);
|
|
15
|
+
pk = key;
|
|
16
|
+
generate_pk = value.generate;
|
|
17
|
+
schema[key] = value.type;
|
|
18
|
+
}
|
|
19
|
+
else if (is_fk(value)) {
|
|
20
|
+
fks.set(key, value);
|
|
21
|
+
schema[key] = value.type;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
schema[key] = value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return { pk, generate_pk, fks, schema };
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=parse.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Options } from "#orm/PrimaryKey";
|
|
2
|
+
import PrimaryKey from "#orm/PrimaryKey";
|
|
3
|
+
import type { DataKey, Storable } from "pema";
|
|
4
|
+
export default function primary<T extends Storable<DataKey>>(type: T, options?: Options): PrimaryKey<T>;
|
|
5
|
+
//# sourceMappingURL=primary.d.ts.map
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type Store from "#orm/Store";
|
|
2
|
+
import type { StoreInput } from "#orm/types";
|
|
3
|
+
/**
|
|
4
|
+
* extracts schema type from either a schema or a Store.
|
|
5
|
+
*/
|
|
6
|
+
type SchemaOf<T> = T extends Store<infer S, any> ? S : T;
|
|
7
|
+
/**
|
|
8
|
+
* One relation (singular) - returns single record or null
|
|
9
|
+
*
|
|
10
|
+
* Normal: FK is on the OTHER table, pointing to this table's PK.
|
|
11
|
+
* Author has one Profile -> Profile has author_id
|
|
12
|
+
*
|
|
13
|
+
* Reverse: FK is on THIS table, pointing to other table's PK.
|
|
14
|
+
* Profile belongs to Author -> Profile has author_id
|
|
15
|
+
*/
|
|
16
|
+
export type OneRelation<T extends StoreInput, FK extends string> = {
|
|
17
|
+
type: "one";
|
|
18
|
+
schema: T;
|
|
19
|
+
fk: FK;
|
|
20
|
+
reverse: boolean;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Many relation (plural) - returns array of records
|
|
24
|
+
*
|
|
25
|
+
* FK is always on the OTHER table, pointing to this table's PK.
|
|
26
|
+
* Example: Author has many Articles → Article has author_id
|
|
27
|
+
*/
|
|
28
|
+
export type ManyRelation<T extends StoreInput, FK extends string> = {
|
|
29
|
+
type: "many";
|
|
30
|
+
schema: T;
|
|
31
|
+
fk: FK;
|
|
32
|
+
};
|
|
33
|
+
export type Relation = OneRelation<any, string> | ManyRelation<any, string>;
|
|
34
|
+
declare function one<T extends StoreInput | Store<StoreInput, any>, FK extends string>(input: T, fk: FK, options?: {
|
|
35
|
+
reverse?: boolean;
|
|
36
|
+
}): OneRelation<SchemaOf<T>, FK>;
|
|
37
|
+
declare function many<T extends StoreInput | Store<StoreInput, any>, FK extends string>(input: T, fk: FK): ManyRelation<SchemaOf<T>, FK>;
|
|
38
|
+
declare const _default: {
|
|
39
|
+
one: typeof one;
|
|
40
|
+
many: typeof many;
|
|
41
|
+
};
|
|
42
|
+
export default _default;
|
|
43
|
+
//# sourceMappingURL=relation.d.ts.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import is from "@rcompat/is";
|
|
2
|
+
function is_store(value) {
|
|
3
|
+
return is.dict(value) && "schema" in value && is.function(value.get);
|
|
4
|
+
}
|
|
5
|
+
function extract_schema(schema_or_store) {
|
|
6
|
+
return (is_store(schema_or_store)
|
|
7
|
+
? schema_or_store.schema
|
|
8
|
+
: schema_or_store);
|
|
9
|
+
}
|
|
10
|
+
function one(input, fk, options) {
|
|
11
|
+
return {
|
|
12
|
+
type: "one",
|
|
13
|
+
schema: extract_schema(input),
|
|
14
|
+
fk,
|
|
15
|
+
reverse: options?.reverse ?? false,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function many(input, fk) {
|
|
19
|
+
return {
|
|
20
|
+
type: "many",
|
|
21
|
+
schema: extract_schema(input),
|
|
22
|
+
fk,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export default { one, many };
|
|
26
|
+
//# sourceMappingURL=relation.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type ForeignKey from "#orm/ForeignKey";
|
|
2
|
+
import type PrimaryKey from "#orm/PrimaryKey";
|
|
3
|
+
import type { ManyRelation, OneRelation } from "#orm/relation";
|
|
4
|
+
import type { Dict } from "@rcompat/type";
|
|
5
|
+
import type { DataKey, InferStore, Storable } from "pema";
|
|
6
|
+
type Relation = OneRelation<any, string> | ManyRelation<any, string>;
|
|
7
|
+
type StoreField = Storable<DataKey> | PrimaryKey<Storable<DataKey>> | ForeignKey<Storable<DataKey>>;
|
|
8
|
+
type StoreInput = Dict<StoreField>;
|
|
9
|
+
type PrimaryKeyField<T extends StoreInput> = {
|
|
10
|
+
[K in keyof T]: T[K] extends PrimaryKey<any> ? K : never;
|
|
11
|
+
}[keyof T] & keyof InferRecord<T>;
|
|
12
|
+
type ExtractSchema<T extends StoreInput> = {
|
|
13
|
+
[K in keyof T]: T[K] extends PrimaryKey<infer P> ? P : T[K] extends ForeignKey<infer P> ? P : T[K] extends Storable<DataKey> ? T[K] : never;
|
|
14
|
+
};
|
|
15
|
+
type InferRecord<T extends StoreInput> = InferStore<ExtractSchema<T>>;
|
|
16
|
+
type Insertable<T extends StoreInput> = Omit<InferRecord<T>, PrimaryKeyField<T>> & Partial<Pick<InferRecord<T>, PrimaryKeyField<T>>>;
|
|
17
|
+
export type { ExtractSchema, InferRecord, Insertable, PrimaryKeyField, Relation, StoreField, StoreInput, };
|
|
18
|
+
//# sourceMappingURL=types.d.ts.map
|