@budibase/backend-core 2.5.8 → 2.5.10-alpha.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/dist/package.json +13 -10
- package/dist/src/configs/configs.d.ts +2 -1
- package/dist/src/configs/configs.js +9 -1
- package/dist/src/configs/configs.js.map +1 -1
- package/dist/src/constants/db.d.ts +1 -0
- package/dist/src/constants/db.js +1 -0
- package/dist/src/constants/db.js.map +1 -1
- package/dist/src/constants/misc.d.ts +4 -2
- package/dist/src/constants/misc.js +2 -0
- package/dist/src/constants/misc.js.map +1 -1
- package/dist/src/context/mainContext.d.ts +5 -2
- package/dist/src/context/mainContext.js +20 -3
- package/dist/src/context/mainContext.js.map +1 -1
- package/dist/src/context/types.d.ts +1 -0
- package/dist/src/db/couch/DatabaseImpl.js +1 -1
- package/dist/src/db/couch/DatabaseImpl.js.map +1 -1
- package/dist/src/db/index.d.ts +2 -1
- package/dist/src/db/index.js +15 -2
- package/dist/src/db/index.js.map +1 -1
- package/dist/src/db/lucene.d.ts +8 -14
- package/dist/src/db/lucene.js +164 -97
- package/dist/src/db/lucene.js.map +1 -1
- package/dist/src/db/searchIndexes/index.d.ts +1 -0
- package/dist/src/db/searchIndexes/index.js +18 -0
- package/dist/src/db/searchIndexes/index.js.map +1 -0
- package/dist/src/db/searchIndexes/searchIndexes.d.ts +1 -0
- package/dist/src/db/searchIndexes/searchIndexes.js +77 -0
- package/dist/src/db/searchIndexes/searchIndexes.js.map +1 -0
- package/dist/src/db/utils.d.ts +5 -126
- package/dist/src/db/utils.js +20 -232
- package/dist/src/db/utils.js.map +1 -1
- package/dist/src/db/views.d.ts +3 -1
- package/dist/src/db/views.js +61 -30
- package/dist/src/db/views.js.map +1 -1
- package/dist/src/docIds/conversions.js.map +1 -0
- package/dist/src/docIds/ids.d.ts +53 -0
- package/dist/src/docIds/ids.js +101 -0
- package/dist/src/docIds/ids.js.map +1 -0
- package/dist/src/docIds/index.d.ts +2 -0
- package/dist/src/docIds/index.js +19 -0
- package/dist/src/docIds/index.js.map +1 -0
- package/dist/src/docIds/newid.js.map +1 -0
- package/dist/src/docIds/params.d.ts +69 -0
- package/dist/src/docIds/params.js +138 -0
- package/dist/src/docIds/params.js.map +1 -0
- package/dist/src/docUpdates/index.d.ts +2 -0
- package/dist/src/docUpdates/index.js +36 -0
- package/dist/src/docUpdates/index.js.map +1 -0
- package/dist/src/environment.d.ts +4 -2
- package/dist/src/environment.js +35 -2
- package/dist/src/environment.js.map +1 -1
- package/dist/src/errors/errors.d.ts +3 -0
- package/dist/src/errors/errors.js +8 -1
- package/dist/src/errors/errors.js.map +1 -1
- package/dist/src/events/asyncEvents/index.d.ts +2 -0
- package/dist/src/events/asyncEvents/index.js +19 -0
- package/dist/src/events/asyncEvents/index.js.map +1 -0
- package/dist/src/events/asyncEvents/publisher.d.ts +2 -0
- package/dist/src/events/asyncEvents/publisher.js +27 -0
- package/dist/src/events/asyncEvents/publisher.js.map +1 -0
- package/dist/src/events/asyncEvents/queue.d.ts +11 -0
- package/dist/src/events/asyncEvents/queue.js +26 -0
- package/dist/src/events/asyncEvents/queue.js.map +1 -0
- package/dist/src/events/documentId.d.ts +2 -0
- package/dist/src/events/documentId.js +28 -0
- package/dist/src/events/documentId.js.map +1 -0
- package/dist/src/events/events.js +9 -0
- package/dist/src/events/events.js.map +1 -1
- package/dist/src/events/identification.d.ts +1 -0
- package/dist/src/events/identification.js +3 -2
- package/dist/src/events/identification.js.map +1 -1
- package/dist/src/events/index.d.ts +1 -0
- package/dist/src/events/index.js +3 -1
- package/dist/src/events/index.js.map +1 -1
- package/dist/src/events/processors/LoggingProcessor.js +3 -17
- package/dist/src/events/processors/LoggingProcessor.js.map +1 -1
- package/dist/src/events/processors/Processors.js +9 -3
- package/dist/src/events/processors/Processors.js.map +1 -1
- package/dist/src/events/processors/async/DocumentUpdateProcessor.d.ts +13 -0
- package/dist/src/events/processors/async/DocumentUpdateProcessor.js +44 -0
- package/dist/src/events/processors/async/DocumentUpdateProcessor.js.map +1 -0
- package/dist/src/events/processors/posthog/PosthogProcessor.js +1 -2
- package/dist/src/events/processors/posthog/PosthogProcessor.js.map +1 -1
- package/dist/src/events/processors/types.d.ts +1 -11
- package/dist/src/events/processors/types.js +0 -6
- package/dist/src/events/processors/types.js.map +1 -1
- package/dist/src/events/publishers/group.js +6 -0
- package/dist/src/events/publishers/group.js.map +1 -1
- package/dist/src/events/publishers/license.d.ts +9 -4
- package/dist/src/events/publishers/license.js +2 -17
- package/dist/src/events/publishers/license.js.map +1 -1
- package/dist/src/events/publishers/user.js +4 -0
- package/dist/src/events/publishers/user.js.map +1 -1
- package/dist/src/index.d.ts +6 -2
- package/dist/src/index.js +2 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/installation.d.ts +1 -0
- package/dist/src/installation.js +7 -6
- package/dist/src/installation.js.map +1 -1
- package/dist/src/{logging.d.ts → logging/alerts.d.ts} +0 -5
- package/dist/src/logging/alerts.js +24 -0
- package/dist/src/logging/alerts.js.map +1 -0
- package/dist/src/logging/correlation/correlation.d.ts +2 -0
- package/dist/src/logging/correlation/correlation.js +17 -0
- package/dist/src/logging/correlation/correlation.js.map +1 -0
- package/dist/src/logging/correlation/index.d.ts +1 -0
- package/dist/src/logging/correlation/index.js +18 -0
- package/dist/src/logging/correlation/index.js.map +1 -0
- package/dist/src/logging/correlation/middleware.d.ts +2 -0
- package/dist/src/logging/correlation/middleware.js +17 -0
- package/dist/src/logging/correlation/middleware.js.map +1 -0
- package/dist/src/logging/index.d.ts +4 -0
- package/dist/src/logging/index.js +36 -0
- package/dist/src/logging/index.js.map +1 -0
- package/dist/src/logging/pino/logger.d.ts +2 -0
- package/dist/src/logging/pino/logger.js +167 -0
- package/dist/src/logging/pino/logger.js.map +1 -0
- package/dist/src/logging/pino/middleware.d.ts +4 -0
- package/dist/src/logging/pino/middleware.js +47 -0
- package/dist/src/logging/pino/middleware.js.map +1 -0
- package/dist/src/middleware/authenticated.js +6 -3
- package/dist/src/middleware/authenticated.js.map +1 -1
- package/dist/src/middleware/errorHandling.js +5 -7
- package/dist/src/middleware/errorHandling.js.map +1 -1
- package/dist/src/middleware/index.d.ts +2 -1
- package/dist/src/middleware/index.js +5 -3
- package/dist/src/middleware/index.js.map +1 -1
- package/dist/src/middleware/passport/sso/oidc.js +2 -5
- package/dist/src/middleware/passport/sso/oidc.js.map +1 -1
- package/dist/src/middleware/tenancy.d.ts +1 -1
- package/dist/src/migrations/migrations.js +4 -4
- package/dist/src/migrations/migrations.js.map +1 -1
- package/dist/src/objectStore/objectStore.js +3 -3
- package/dist/src/objectStore/objectStore.js.map +1 -1
- package/dist/src/plugin/utils.js +53 -2
- package/dist/src/plugin/utils.js.map +1 -1
- package/dist/src/queue/constants.d.ts +2 -1
- package/dist/src/queue/constants.js +1 -0
- package/dist/src/queue/constants.js.map +1 -1
- package/dist/src/redis/redlockImpl.js +4 -1
- package/dist/src/redis/redlockImpl.js.map +1 -1
- package/dist/src/security/encryption.d.ts +1 -0
- package/dist/src/security/encryption.js +2 -1
- package/dist/src/security/encryption.js.map +1 -1
- package/dist/src/security/permissions.d.ts +35 -2
- package/dist/src/security/permissions.js +9 -6
- package/dist/src/security/permissions.js.map +1 -1
- package/dist/src/tenancy/tenancy.js +1 -5
- package/dist/src/tenancy/tenancy.js.map +1 -1
- package/dist/src/users.d.ts +11 -1
- package/dist/src/users.js +45 -1
- package/dist/src/users.js.map +1 -1
- package/dist/src/utils/hashing.d.ts +1 -1
- package/dist/src/utils/hashing.js +1 -1
- package/dist/src/utils/hashing.js.map +1 -1
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/index.js +1 -0
- package/dist/src/utils/index.js.map +1 -1
- package/dist/src/utils/stringUtils.d.ts +1 -0
- package/dist/src/utils/stringUtils.js +9 -0
- package/dist/src/utils/stringUtils.js.map +1 -0
- package/dist/src/utils/utils.d.ts +0 -1
- package/dist/src/utils/utils.js +1 -24
- package/dist/src/utils/utils.js.map +1 -1
- package/dist/tests/core/logging.js.map +1 -0
- package/dist/tests/{utilities → core/utilities}/index.d.ts +1 -2
- package/dist/tests/{utilities → core/utilities}/index.js +2 -7
- package/dist/tests/core/utilities/index.js.map +1 -0
- package/dist/tests/core/utilities/jestUtils.js.map +1 -0
- package/dist/tests/core/utilities/mocks/alerts.d.ts +3 -0
- package/dist/tests/core/utilities/mocks/alerts.js +30 -0
- package/dist/tests/core/utilities/mocks/alerts.js.map +1 -0
- package/dist/tests/core/utilities/mocks/date.js.map +1 -0
- package/dist/tests/core/utilities/mocks/events.js +144 -0
- package/dist/tests/core/utilities/mocks/events.js.map +1 -0
- package/dist/tests/core/utilities/mocks/fetch.d.ts +33 -0
- package/dist/tests/core/utilities/mocks/fetch.js +15 -0
- package/dist/tests/core/utilities/mocks/fetch.js.map +1 -0
- package/dist/tests/{utilities → core/utilities}/mocks/index.d.ts +4 -3
- package/dist/tests/{utilities → core/utilities}/mocks/index.js +7 -3
- package/dist/tests/core/utilities/mocks/index.js.map +1 -0
- package/dist/tests/{utilities → core/utilities}/mocks/licenses.d.ts +1 -0
- package/dist/tests/{utilities → core/utilities}/mocks/licenses.js +5 -1
- package/dist/tests/core/utilities/mocks/licenses.js.map +1 -0
- package/dist/tests/core/utilities/mocks/posthog.js.map +1 -0
- package/dist/tests/core/utilities/structures/Chance.d.ts +7 -0
- package/dist/tests/core/utilities/structures/Chance.js +21 -0
- package/dist/tests/core/utilities/structures/Chance.js.map +1 -0
- package/dist/tests/{utilities → core/utilities}/structures/accounts.js +3 -25
- package/dist/tests/core/utilities/structures/accounts.js.map +1 -0
- package/dist/tests/{utilities → core/utilities}/structures/apps.js +1 -1
- package/dist/tests/core/utilities/structures/apps.js.map +1 -0
- package/dist/tests/core/utilities/structures/common.js.map +1 -0
- package/dist/tests/{utilities → core/utilities}/structures/db.js +1 -1
- package/dist/tests/core/utilities/structures/db.js.map +1 -0
- package/dist/tests/core/utilities/structures/generator.d.ts +2 -0
- package/dist/tests/{utilities → core/utilities}/structures/generator.js +2 -2
- package/dist/tests/core/utilities/structures/generator.js.map +1 -0
- package/dist/tests/{utilities → core/utilities}/structures/index.d.ts +2 -0
- package/dist/tests/{utilities → core/utilities}/structures/index.js +3 -1
- package/dist/tests/core/utilities/structures/index.js.map +1 -0
- package/dist/tests/core/utilities/structures/koa.js.map +1 -0
- package/dist/tests/core/utilities/structures/licenses.d.ts +17 -0
- package/dist/tests/core/utilities/structures/licenses.js +127 -0
- package/dist/tests/core/utilities/structures/licenses.js.map +1 -0
- package/dist/tests/core/utilities/structures/plugins.js.map +1 -0
- package/dist/tests/core/utilities/structures/quotas.d.ts +2 -0
- package/dist/tests/core/utilities/structures/quotas.js +71 -0
- package/dist/tests/core/utilities/structures/quotas.js.map +1 -0
- package/dist/tests/core/utilities/structures/scim.d.ts +14 -0
- package/dist/tests/core/utilities/structures/scim.js +67 -0
- package/dist/tests/core/utilities/structures/scim.js.map +1 -0
- package/dist/tests/core/utilities/structures/shared.js.map +1 -0
- package/dist/tests/core/utilities/structures/sso.js.map +1 -0
- package/dist/tests/{utilities → core/utilities}/structures/tenants.js +1 -1
- package/dist/tests/core/utilities/structures/tenants.js.map +1 -0
- package/dist/tests/core/utilities/structures/userGroups.js.map +1 -0
- package/dist/tests/core/utilities/structures/users.js.map +1 -0
- package/dist/tests/core/utilities/testContainerUtils.js.map +1 -0
- package/dist/tests/core/utilities/utils/index.d.ts +1 -0
- package/dist/tests/core/utilities/utils/index.js +28 -0
- package/dist/tests/core/utilities/utils/index.js.map +1 -0
- package/dist/tests/core/utilities/utils/time.d.ts +1 -0
- package/dist/tests/core/utilities/utils/time.js +8 -0
- package/dist/tests/core/utilities/utils/time.js.map +1 -0
- package/dist/tests/{utilities → extra}/DBTestConfiguration.d.ts +1 -1
- package/dist/tests/{utilities → extra}/DBTestConfiguration.js +2 -2
- package/dist/tests/extra/DBTestConfiguration.js.map +1 -0
- package/dist/tests/extra/index.d.ts +2 -0
- package/dist/tests/extra/index.js +33 -0
- package/dist/tests/extra/index.js.map +1 -0
- package/dist/tests/{utilities → extra}/testEnv.d.ts +1 -1
- package/dist/tests/{utilities → extra}/testEnv.js +1 -1
- package/dist/tests/extra/testEnv.js.map +1 -0
- package/dist/tests/index.d.ts +1 -1
- package/dist/tests/index.js +1 -1
- package/dist/tests/index.js.map +1 -1
- package/dist/tests/jestEnv.js +0 -1
- package/dist/tests/jestEnv.js.map +1 -1
- package/dist/tests/jestSetup.d.ts +1 -1
- package/dist/tests/jestSetup.js +2 -2
- package/dist/tests/jestSetup.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +13 -10
- package/src/auth/tests/auth.spec.ts +2 -1
- package/src/cache/tests/writethrough.spec.ts +4 -3
- package/src/configs/configs.ts +9 -0
- package/src/configs/tests/configs.spec.ts +2 -6
- package/src/constants/db.ts +1 -0
- package/src/constants/misc.ts +2 -0
- package/src/context/mainContext.ts +22 -5
- package/src/context/tests/index.spec.ts +16 -3
- package/src/context/types.ts +1 -0
- package/src/db/couch/DatabaseImpl.ts +1 -1
- package/src/db/index.ts +2 -1
- package/src/db/lucene.ts +156 -99
- package/src/db/searchIndexes/index.ts +1 -0
- package/src/db/searchIndexes/searchIndexes.ts +62 -0
- package/src/db/tests/lucene.spec.ts +101 -1
- package/src/db/tests/utils.spec.ts +1 -1
- package/src/db/utils.ts +7 -275
- package/src/db/views.ts +64 -25
- package/src/docIds/ids.ts +102 -0
- package/src/docIds/index.ts +2 -0
- package/src/docIds/params.ts +174 -0
- package/src/docUpdates/index.ts +29 -0
- package/src/environment.ts +44 -2
- package/src/errors/errors.ts +8 -0
- package/src/events/asyncEvents/index.ts +2 -0
- package/src/events/asyncEvents/publisher.ts +12 -0
- package/src/events/asyncEvents/queue.ts +22 -0
- package/src/events/documentId.ts +56 -0
- package/src/events/events.ts +10 -1
- package/src/events/identification.ts +3 -3
- package/src/events/index.ts +2 -0
- package/src/events/processors/LoggingProcessor.ts +3 -22
- package/src/events/processors/Processors.ts +9 -3
- package/src/events/processors/async/DocumentUpdateProcessor.ts +43 -0
- package/src/events/processors/posthog/PosthogProcessor.ts +1 -2
- package/src/events/processors/posthog/tests/PosthogProcessor.spec.ts +1 -1
- package/src/events/processors/types.ts +1 -18
- package/src/events/publishers/group.ts +6 -1
- package/src/events/publishers/license.ts +12 -13
- package/src/events/publishers/user.ts +4 -0
- package/src/index.ts +1 -0
- package/src/installation.ts +4 -5
- package/src/logging/alerts.ts +26 -0
- package/src/logging/correlation/correlation.ts +13 -0
- package/src/logging/correlation/index.ts +1 -0
- package/src/logging/correlation/middleware.ts +17 -0
- package/src/logging/index.ts +6 -0
- package/src/logging/pino/logger.ts +173 -0
- package/src/logging/pino/middleware.ts +45 -0
- package/src/middleware/authenticated.ts +9 -3
- package/src/middleware/errorHandling.ts +4 -4
- package/src/middleware/index.ts +2 -1
- package/src/middleware/passport/sso/oidc.ts +2 -9
- package/src/middleware/passport/sso/tests/sso.spec.ts +2 -1
- package/src/migrations/migrations.ts +4 -8
- package/src/migrations/tests/__snapshots__/migrations.spec.ts.snap +1 -1
- package/src/migrations/tests/migrations.spec.ts +1 -1
- package/src/objectStore/buckets/tests/app.spec.ts +1 -1
- package/src/objectStore/buckets/tests/global.spec.ts +1 -1
- package/src/objectStore/buckets/tests/plugins.spec.ts +2 -1
- package/src/objectStore/objectStore.ts +3 -3
- package/src/platform/tests/tenants.spec.ts +2 -1
- package/src/plugin/tests/validation.spec.ts +83 -0
- package/src/plugin/utils.ts +63 -3
- package/src/queue/constants.ts +1 -0
- package/src/redis/redlockImpl.ts +9 -3
- package/src/security/encryption.ts +1 -1
- package/src/security/permissions.ts +5 -5
- package/src/security/tests/encryption.spec.ts +31 -0
- package/src/security/tests/permissions.spec.ts +145 -0
- package/src/tenancy/tenancy.ts +2 -2
- package/src/tenancy/tests/tenancy.spec.ts +184 -0
- package/src/users.ts +56 -7
- package/src/utils/hashing.ts +1 -1
- package/src/utils/index.ts +1 -0
- package/src/utils/stringUtils.ts +8 -0
- package/src/utils/tests/utils.spec.ts +2 -1
- package/src/utils/utils.ts +3 -23
- package/tests/{utilities → core/utilities}/index.ts +1 -4
- package/tests/core/utilities/mocks/alerts.ts +3 -0
- package/tests/core/utilities/mocks/events.ts +133 -0
- package/tests/core/utilities/mocks/fetch.ts +17 -0
- package/tests/{utilities → core/utilities}/mocks/index.ts +4 -3
- package/tests/{utilities → core/utilities}/mocks/licenses.ts +4 -0
- package/tests/core/utilities/structures/Chance.ts +20 -0
- package/tests/{utilities → core/utilities}/structures/accounts.ts +4 -3
- package/tests/{utilities → core/utilities}/structures/apps.ts +1 -1
- package/tests/{utilities → core/utilities}/structures/db.ts +1 -1
- package/tests/{utilities → core/utilities}/structures/generator.ts +1 -1
- package/tests/{utilities → core/utilities}/structures/index.ts +2 -0
- package/tests/core/utilities/structures/licenses.ts +145 -0
- package/tests/core/utilities/structures/quotas.ts +67 -0
- package/tests/core/utilities/structures/scim.ts +81 -0
- package/tests/{utilities → core/utilities}/structures/tenants.ts +1 -1
- package/tests/core/utilities/utils/index.ts +1 -0
- package/tests/core/utilities/utils/time.ts +3 -0
- package/tests/{utilities → extra}/DBTestConfiguration.ts +2 -2
- package/tests/extra/index.ts +2 -0
- package/tests/{utilities → extra}/testEnv.ts +1 -1
- package/tests/index.ts +1 -1
- package/tests/jestEnv.ts +0 -1
- package/tests/jestSetup.ts +2 -2
- package/tsconfig.build.json +3 -7
- package/dist/src/db/conversions.js.map +0 -1
- package/dist/src/logging.js +0 -56
- package/dist/src/logging.js.map +0 -1
- package/dist/src/middleware/logging.d.ts +0 -2
- package/dist/src/middleware/logging.js +0 -106
- package/dist/src/middleware/logging.js.map +0 -1
- package/dist/src/newid.js.map +0 -1
- package/dist/tests/logging.js.map +0 -1
- package/dist/tests/utilities/DBTestConfiguration.js.map +0 -1
- package/dist/tests/utilities/index.js.map +0 -1
- package/dist/tests/utilities/jestUtils.js.map +0 -1
- package/dist/tests/utilities/mocks/date.js.map +0 -1
- package/dist/tests/utilities/mocks/events.js +0 -126
- package/dist/tests/utilities/mocks/events.js.map +0 -1
- package/dist/tests/utilities/mocks/fetch.d.ts +0 -31
- package/dist/tests/utilities/mocks/fetch.js +0 -8
- package/dist/tests/utilities/mocks/fetch.js.map +0 -1
- package/dist/tests/utilities/mocks/index.js.map +0 -1
- package/dist/tests/utilities/mocks/licenses.js.map +0 -1
- package/dist/tests/utilities/mocks/posthog.js.map +0 -1
- package/dist/tests/utilities/structures/accounts.js.map +0 -1
- package/dist/tests/utilities/structures/apps.js.map +0 -1
- package/dist/tests/utilities/structures/common.js.map +0 -1
- package/dist/tests/utilities/structures/db.js.map +0 -1
- package/dist/tests/utilities/structures/generator.d.ts +0 -1
- package/dist/tests/utilities/structures/generator.js.map +0 -1
- package/dist/tests/utilities/structures/index.js.map +0 -1
- package/dist/tests/utilities/structures/koa.js.map +0 -1
- package/dist/tests/utilities/structures/licenses.d.ts +0 -5
- package/dist/tests/utilities/structures/licenses.js +0 -18
- package/dist/tests/utilities/structures/licenses.js.map +0 -1
- package/dist/tests/utilities/structures/plugins.js.map +0 -1
- package/dist/tests/utilities/structures/shared.js.map +0 -1
- package/dist/tests/utilities/structures/sso.js.map +0 -1
- package/dist/tests/utilities/structures/tenants.js.map +0 -1
- package/dist/tests/utilities/structures/userGroups.js.map +0 -1
- package/dist/tests/utilities/structures/users.js.map +0 -1
- package/dist/tests/utilities/testContainerUtils.js.map +0 -1
- package/dist/tests/utilities/testEnv.js.map +0 -1
- package/src/logging.ts +0 -60
- package/src/middleware/logging.ts +0 -90
- package/tests/utilities/mocks/events.ts +0 -122
- package/tests/utilities/mocks/fetch.ts +0 -10
- package/tests/utilities/structures/licenses.ts +0 -18
- /package/dist/src/{db → docIds}/conversions.d.ts +0 -0
- /package/dist/src/{db → docIds}/conversions.js +0 -0
- /package/dist/src/{newid.d.ts → docIds/newid.d.ts} +0 -0
- /package/dist/src/{newid.js → docIds/newid.js} +0 -0
- /package/dist/tests/{logging.d.ts → core/logging.d.ts} +0 -0
- /package/dist/tests/{logging.js → core/logging.js} +0 -0
- /package/dist/tests/{utilities → core/utilities}/jestUtils.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/jestUtils.js +0 -0
- /package/dist/tests/{utilities → core/utilities}/mocks/date.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/mocks/date.js +0 -0
- /package/dist/tests/{utilities → core/utilities}/mocks/events.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/mocks/posthog.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/mocks/posthog.js +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/accounts.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/apps.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/common.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/common.js +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/db.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/koa.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/koa.js +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/plugins.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/plugins.js +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/shared.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/shared.js +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/sso.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/sso.js +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/tenants.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/userGroups.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/userGroups.js +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/users.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/structures/users.js +0 -0
- /package/dist/tests/{utilities → core/utilities}/testContainerUtils.d.ts +0 -0
- /package/dist/tests/{utilities → core/utilities}/testContainerUtils.js +0 -0
- /package/src/{db → docIds}/conversions.ts +0 -0
- /package/src/{newid.ts → docIds/newid.ts} +0 -0
- /package/tests/{logging.ts → core/logging.ts} +0 -0
- /package/tests/{utilities → core/utilities}/jestUtils.ts +0 -0
- /package/tests/{utilities → core/utilities}/mocks/date.ts +0 -0
- /package/tests/{utilities → core/utilities}/mocks/posthog.ts +0 -0
- /package/tests/{utilities → core/utilities}/structures/common.ts +0 -0
- /package/tests/{utilities → core/utilities}/structures/koa.ts +0 -0
- /package/tests/{utilities → core/utilities}/structures/plugins.ts +0 -0
- /package/tests/{utilities → core/utilities}/structures/shared.ts +0 -0
- /package/tests/{utilities → core/utilities}/structures/sso.ts +0 -0
- /package/tests/{utilities → core/utilities}/structures/userGroups.ts +0 -0
- /package/tests/{utilities → core/utilities}/structures/users.ts +0 -0
- /package/tests/{utilities → core/utilities}/testContainerUtils.ts +0 -0
package/src/db/lucene.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import fetch from "node-fetch"
|
|
2
2
|
import { getCouchInfo } from "./couch"
|
|
3
3
|
import { SearchFilters, Row } from "@budibase/types"
|
|
4
|
+
import { createUserIndex } from "./searchIndexes/searchIndexes"
|
|
4
5
|
|
|
5
6
|
const QUERY_START_REGEX = /\d[0-9]*:/g
|
|
6
7
|
|
|
7
8
|
interface SearchResponse<T> {
|
|
8
9
|
rows: T[] | any[]
|
|
9
|
-
bookmark
|
|
10
|
+
bookmark?: string
|
|
11
|
+
totalRows: number
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
interface PaginatedSearchResponse<T> extends SearchResponse<T> {
|
|
@@ -42,23 +44,26 @@ export function removeKeyNumbering(key: any): string {
|
|
|
42
44
|
* Optionally takes a base lucene query object.
|
|
43
45
|
*/
|
|
44
46
|
export class QueryBuilder<T> {
|
|
45
|
-
dbName: string
|
|
46
|
-
index: string
|
|
47
|
-
query: SearchFilters
|
|
48
|
-
limit: number
|
|
49
|
-
sort?: string
|
|
50
|
-
bookmark?: string
|
|
51
|
-
sortOrder: string
|
|
52
|
-
sortType: string
|
|
53
|
-
includeDocs: boolean
|
|
54
|
-
version?: string
|
|
55
|
-
indexBuilder?: () => Promise<any>
|
|
56
|
-
noEscaping = false
|
|
47
|
+
#dbName: string
|
|
48
|
+
#index: string
|
|
49
|
+
#query: SearchFilters
|
|
50
|
+
#limit: number
|
|
51
|
+
#sort?: string
|
|
52
|
+
#bookmark?: string
|
|
53
|
+
#sortOrder: string
|
|
54
|
+
#sortType: string
|
|
55
|
+
#includeDocs: boolean
|
|
56
|
+
#version?: string
|
|
57
|
+
#indexBuilder?: () => Promise<any>
|
|
58
|
+
#noEscaping = false
|
|
59
|
+
#skip?: number
|
|
60
|
+
|
|
61
|
+
static readonly maxLimit = 200
|
|
57
62
|
|
|
58
63
|
constructor(dbName: string, index: string, base?: SearchFilters) {
|
|
59
|
-
this
|
|
60
|
-
this
|
|
61
|
-
this
|
|
64
|
+
this.#dbName = dbName
|
|
65
|
+
this.#index = index
|
|
66
|
+
this.#query = {
|
|
62
67
|
allOr: false,
|
|
63
68
|
string: {},
|
|
64
69
|
fuzzy: {},
|
|
@@ -73,86 +78,96 @@ export class QueryBuilder<T> {
|
|
|
73
78
|
containsAny: {},
|
|
74
79
|
...base,
|
|
75
80
|
}
|
|
76
|
-
this
|
|
77
|
-
this
|
|
78
|
-
this
|
|
79
|
-
this
|
|
81
|
+
this.#limit = 50
|
|
82
|
+
this.#sortOrder = "ascending"
|
|
83
|
+
this.#sortType = "string"
|
|
84
|
+
this.#includeDocs = true
|
|
80
85
|
}
|
|
81
86
|
|
|
82
87
|
disableEscaping() {
|
|
83
|
-
this
|
|
88
|
+
this.#noEscaping = true
|
|
84
89
|
return this
|
|
85
90
|
}
|
|
86
91
|
|
|
87
92
|
setIndexBuilder(builderFn: () => Promise<any>) {
|
|
88
|
-
this
|
|
93
|
+
this.#indexBuilder = builderFn
|
|
89
94
|
return this
|
|
90
95
|
}
|
|
91
96
|
|
|
92
97
|
setVersion(version?: string) {
|
|
93
98
|
if (version != null) {
|
|
94
|
-
this
|
|
99
|
+
this.#version = version
|
|
95
100
|
}
|
|
96
101
|
return this
|
|
97
102
|
}
|
|
98
103
|
|
|
99
104
|
setTable(tableId: string) {
|
|
100
|
-
this
|
|
105
|
+
this.#query.equal!.tableId = tableId
|
|
101
106
|
return this
|
|
102
107
|
}
|
|
103
108
|
|
|
104
109
|
setLimit(limit?: number) {
|
|
105
110
|
if (limit != null) {
|
|
106
|
-
this
|
|
111
|
+
this.#limit = limit
|
|
107
112
|
}
|
|
108
113
|
return this
|
|
109
114
|
}
|
|
110
115
|
|
|
111
116
|
setSort(sort?: string) {
|
|
112
117
|
if (sort != null) {
|
|
113
|
-
this
|
|
118
|
+
this.#sort = sort
|
|
114
119
|
}
|
|
115
120
|
return this
|
|
116
121
|
}
|
|
117
122
|
|
|
118
123
|
setSortOrder(sortOrder?: string) {
|
|
119
124
|
if (sortOrder != null) {
|
|
120
|
-
this
|
|
125
|
+
this.#sortOrder = sortOrder
|
|
121
126
|
}
|
|
122
127
|
return this
|
|
123
128
|
}
|
|
124
129
|
|
|
125
130
|
setSortType(sortType?: string) {
|
|
126
131
|
if (sortType != null) {
|
|
127
|
-
this
|
|
132
|
+
this.#sortType = sortType
|
|
128
133
|
}
|
|
129
134
|
return this
|
|
130
135
|
}
|
|
131
136
|
|
|
132
137
|
setBookmark(bookmark?: string) {
|
|
133
138
|
if (bookmark != null) {
|
|
134
|
-
this
|
|
139
|
+
this.#bookmark = bookmark
|
|
135
140
|
}
|
|
136
141
|
return this
|
|
137
142
|
}
|
|
138
143
|
|
|
144
|
+
setSkip(skip: number | undefined) {
|
|
145
|
+
this.#skip = skip
|
|
146
|
+
return this
|
|
147
|
+
}
|
|
148
|
+
|
|
139
149
|
excludeDocs() {
|
|
140
|
-
this
|
|
150
|
+
this.#includeDocs = false
|
|
151
|
+
return this
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
includeDocs() {
|
|
155
|
+
this.#includeDocs = true
|
|
141
156
|
return this
|
|
142
157
|
}
|
|
143
158
|
|
|
144
159
|
addString(key: string, partial: string) {
|
|
145
|
-
this
|
|
160
|
+
this.#query.string![key] = partial
|
|
146
161
|
return this
|
|
147
162
|
}
|
|
148
163
|
|
|
149
164
|
addFuzzy(key: string, fuzzy: string) {
|
|
150
|
-
this
|
|
165
|
+
this.#query.fuzzy![key] = fuzzy
|
|
151
166
|
return this
|
|
152
167
|
}
|
|
153
168
|
|
|
154
169
|
addRange(key: string, low: string | number, high: string | number) {
|
|
155
|
-
this
|
|
170
|
+
this.#query.range![key] = {
|
|
156
171
|
low,
|
|
157
172
|
high,
|
|
158
173
|
}
|
|
@@ -160,51 +175,51 @@ export class QueryBuilder<T> {
|
|
|
160
175
|
}
|
|
161
176
|
|
|
162
177
|
addEqual(key: string, value: any) {
|
|
163
|
-
this
|
|
178
|
+
this.#query.equal![key] = value
|
|
164
179
|
return this
|
|
165
180
|
}
|
|
166
181
|
|
|
167
182
|
addNotEqual(key: string, value: any) {
|
|
168
|
-
this
|
|
183
|
+
this.#query.notEqual![key] = value
|
|
169
184
|
return this
|
|
170
185
|
}
|
|
171
186
|
|
|
172
187
|
addEmpty(key: string, value: any) {
|
|
173
|
-
this
|
|
188
|
+
this.#query.empty![key] = value
|
|
174
189
|
return this
|
|
175
190
|
}
|
|
176
191
|
|
|
177
192
|
addNotEmpty(key: string, value: any) {
|
|
178
|
-
this
|
|
193
|
+
this.#query.notEmpty![key] = value
|
|
179
194
|
return this
|
|
180
195
|
}
|
|
181
196
|
|
|
182
197
|
addOneOf(key: string, value: any) {
|
|
183
|
-
this
|
|
198
|
+
this.#query.oneOf![key] = value
|
|
184
199
|
return this
|
|
185
200
|
}
|
|
186
201
|
|
|
187
202
|
addContains(key: string, value: any) {
|
|
188
|
-
this
|
|
203
|
+
this.#query.contains![key] = value
|
|
189
204
|
return this
|
|
190
205
|
}
|
|
191
206
|
|
|
192
207
|
addNotContains(key: string, value: any) {
|
|
193
|
-
this
|
|
208
|
+
this.#query.notContains![key] = value
|
|
194
209
|
return this
|
|
195
210
|
}
|
|
196
211
|
|
|
197
212
|
addContainsAny(key: string, value: any) {
|
|
198
|
-
this
|
|
213
|
+
this.#query.containsAny![key] = value
|
|
199
214
|
return this
|
|
200
215
|
}
|
|
201
216
|
|
|
202
217
|
setAllOr() {
|
|
203
|
-
this
|
|
218
|
+
this.#query.allOr = true
|
|
204
219
|
}
|
|
205
220
|
|
|
206
221
|
handleSpaces(input: string) {
|
|
207
|
-
if (this
|
|
222
|
+
if (this.#noEscaping) {
|
|
208
223
|
return input
|
|
209
224
|
} else {
|
|
210
225
|
return input.replace(/ /g, "_")
|
|
@@ -219,7 +234,7 @@ export class QueryBuilder<T> {
|
|
|
219
234
|
* @returns {string|*}
|
|
220
235
|
*/
|
|
221
236
|
preprocess(value: any, { escape, lowercase, wrap, type }: any = {}) {
|
|
222
|
-
const hasVersion = !!this
|
|
237
|
+
const hasVersion = !!this.#version
|
|
223
238
|
// Determine if type needs wrapped
|
|
224
239
|
const originalType = typeof value
|
|
225
240
|
// Convert to lowercase
|
|
@@ -227,8 +242,8 @@ export class QueryBuilder<T> {
|
|
|
227
242
|
value = value.toLowerCase ? value.toLowerCase() : value
|
|
228
243
|
}
|
|
229
244
|
// Escape characters
|
|
230
|
-
if (!this
|
|
231
|
-
value = `${value}`.replace(/[
|
|
245
|
+
if (!this.#noEscaping && escape && originalType === "string") {
|
|
246
|
+
value = `${value}`.replace(/[ \/#+\-&|!(){}\]^"~*?:\\]/g, "\\$&")
|
|
232
247
|
}
|
|
233
248
|
|
|
234
249
|
// Wrap in quotes
|
|
@@ -242,7 +257,7 @@ export class QueryBuilder<T> {
|
|
|
242
257
|
|
|
243
258
|
isMultiCondition() {
|
|
244
259
|
let count = 0
|
|
245
|
-
for (let filters of Object.values(this
|
|
260
|
+
for (let filters of Object.values(this.#query)) {
|
|
246
261
|
// not contains is one massive filter in allOr mode
|
|
247
262
|
if (typeof filters === "object") {
|
|
248
263
|
count += Object.keys(filters).length
|
|
@@ -272,13 +287,13 @@ export class QueryBuilder<T> {
|
|
|
272
287
|
|
|
273
288
|
buildSearchQuery() {
|
|
274
289
|
const builder = this
|
|
275
|
-
let allOr = this
|
|
290
|
+
let allOr = this.#query && this.#query.allOr
|
|
276
291
|
let query = allOr ? "" : "*:*"
|
|
277
292
|
const allPreProcessingOpts = { escape: true, lowercase: true, wrap: true }
|
|
278
293
|
let tableId
|
|
279
|
-
if (this
|
|
280
|
-
tableId = this
|
|
281
|
-
delete this
|
|
294
|
+
if (this.#query.equal!.tableId) {
|
|
295
|
+
tableId = this.#query.equal!.tableId
|
|
296
|
+
delete this.#query.equal!.tableId
|
|
282
297
|
}
|
|
283
298
|
|
|
284
299
|
const equal = (key: string, value: any) => {
|
|
@@ -305,6 +320,18 @@ export class QueryBuilder<T> {
|
|
|
305
320
|
return `${key}:(${statement})`
|
|
306
321
|
}
|
|
307
322
|
|
|
323
|
+
const fuzzy = (key: string, value: any) => {
|
|
324
|
+
if (!value) {
|
|
325
|
+
return null
|
|
326
|
+
}
|
|
327
|
+
value = builder.preprocess(value, {
|
|
328
|
+
escape: true,
|
|
329
|
+
lowercase: true,
|
|
330
|
+
type: "fuzzy",
|
|
331
|
+
})
|
|
332
|
+
return `${key}:/.*${value}.*/`
|
|
333
|
+
}
|
|
334
|
+
|
|
308
335
|
const notContains = (key: string, value: any) => {
|
|
309
336
|
const allPrefix = allOr ? "*:* AND " : ""
|
|
310
337
|
const mode = allOr ? "AND" : undefined
|
|
@@ -363,8 +390,8 @@ export class QueryBuilder<T> {
|
|
|
363
390
|
}
|
|
364
391
|
|
|
365
392
|
// Construct the actual lucene search query string from JSON structure
|
|
366
|
-
if (this
|
|
367
|
-
build(this
|
|
393
|
+
if (this.#query.string) {
|
|
394
|
+
build(this.#query.string, (key: string, value: any) => {
|
|
368
395
|
if (!value) {
|
|
369
396
|
return null
|
|
370
397
|
}
|
|
@@ -376,8 +403,8 @@ export class QueryBuilder<T> {
|
|
|
376
403
|
return `${key}:${value}*`
|
|
377
404
|
})
|
|
378
405
|
}
|
|
379
|
-
if (this
|
|
380
|
-
build(this
|
|
406
|
+
if (this.#query.range) {
|
|
407
|
+
build(this.#query.range, (key: string, value: any) => {
|
|
381
408
|
if (!value) {
|
|
382
409
|
return null
|
|
383
410
|
}
|
|
@@ -392,47 +419,37 @@ export class QueryBuilder<T> {
|
|
|
392
419
|
return `${key}:[${low} TO ${high}]`
|
|
393
420
|
})
|
|
394
421
|
}
|
|
395
|
-
if (this
|
|
396
|
-
build(this
|
|
397
|
-
if (!value) {
|
|
398
|
-
return null
|
|
399
|
-
}
|
|
400
|
-
value = builder.preprocess(value, {
|
|
401
|
-
escape: true,
|
|
402
|
-
lowercase: true,
|
|
403
|
-
type: "fuzzy",
|
|
404
|
-
})
|
|
405
|
-
return `${key}:${value}~`
|
|
406
|
-
})
|
|
422
|
+
if (this.#query.fuzzy) {
|
|
423
|
+
build(this.#query.fuzzy, fuzzy)
|
|
407
424
|
}
|
|
408
|
-
if (this
|
|
409
|
-
build(this
|
|
425
|
+
if (this.#query.equal) {
|
|
426
|
+
build(this.#query.equal, equal)
|
|
410
427
|
}
|
|
411
|
-
if (this
|
|
412
|
-
build(this
|
|
428
|
+
if (this.#query.notEqual) {
|
|
429
|
+
build(this.#query.notEqual, (key: string, value: any) => {
|
|
413
430
|
if (!value) {
|
|
414
431
|
return null
|
|
415
432
|
}
|
|
416
433
|
return `!${key}:${builder.preprocess(value, allPreProcessingOpts)}`
|
|
417
434
|
})
|
|
418
435
|
}
|
|
419
|
-
if (this
|
|
420
|
-
build(this
|
|
436
|
+
if (this.#query.empty) {
|
|
437
|
+
build(this.#query.empty, (key: string) => `(*:* -${key}:["" TO *])`)
|
|
421
438
|
}
|
|
422
|
-
if (this
|
|
423
|
-
build(this
|
|
439
|
+
if (this.#query.notEmpty) {
|
|
440
|
+
build(this.#query.notEmpty, (key: string) => `${key}:["" TO *]`)
|
|
424
441
|
}
|
|
425
|
-
if (this
|
|
426
|
-
build(this
|
|
442
|
+
if (this.#query.oneOf) {
|
|
443
|
+
build(this.#query.oneOf, oneOf)
|
|
427
444
|
}
|
|
428
|
-
if (this
|
|
429
|
-
build(this
|
|
445
|
+
if (this.#query.contains) {
|
|
446
|
+
build(this.#query.contains, contains)
|
|
430
447
|
}
|
|
431
|
-
if (this
|
|
432
|
-
build(this.compressFilters(this
|
|
448
|
+
if (this.#query.notContains) {
|
|
449
|
+
build(this.compressFilters(this.#query.notContains), notContains)
|
|
433
450
|
}
|
|
434
|
-
if (this
|
|
435
|
-
build(this
|
|
451
|
+
if (this.#query.containsAny) {
|
|
452
|
+
build(this.#query.containsAny, containsAny)
|
|
436
453
|
}
|
|
437
454
|
// make sure table ID is always added as an AND
|
|
438
455
|
if (tableId) {
|
|
@@ -446,29 +463,65 @@ export class QueryBuilder<T> {
|
|
|
446
463
|
buildSearchBody() {
|
|
447
464
|
let body: any = {
|
|
448
465
|
q: this.buildSearchQuery(),
|
|
449
|
-
limit: Math.min(this
|
|
450
|
-
include_docs: this
|
|
466
|
+
limit: Math.min(this.#limit, QueryBuilder.maxLimit),
|
|
467
|
+
include_docs: this.#includeDocs,
|
|
451
468
|
}
|
|
452
|
-
if (this
|
|
453
|
-
body.bookmark = this
|
|
469
|
+
if (this.#bookmark) {
|
|
470
|
+
body.bookmark = this.#bookmark
|
|
454
471
|
}
|
|
455
|
-
if (this
|
|
456
|
-
const order = this
|
|
457
|
-
const type = `<${this
|
|
458
|
-
body.sort = `${order}${this.handleSpaces(this
|
|
472
|
+
if (this.#sort) {
|
|
473
|
+
const order = this.#sortOrder === "descending" ? "-" : ""
|
|
474
|
+
const type = `<${this.#sortType}>`
|
|
475
|
+
body.sort = `${order}${this.handleSpaces(this.#sort)}${type}`
|
|
459
476
|
}
|
|
460
477
|
return body
|
|
461
478
|
}
|
|
462
479
|
|
|
463
480
|
async run() {
|
|
481
|
+
if (this.#skip) {
|
|
482
|
+
await this.#skipItems(this.#skip)
|
|
483
|
+
}
|
|
484
|
+
return await this.#execute()
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Lucene queries do not support pagination and use bookmarks instead.
|
|
489
|
+
* For the given builder, walk through pages using bookmarks until the desired
|
|
490
|
+
* page has been met.
|
|
491
|
+
*/
|
|
492
|
+
async #skipItems(skip: number) {
|
|
493
|
+
// Lucene does not support pagination.
|
|
494
|
+
// Handle pagination by finding the right bookmark
|
|
495
|
+
const prevIncludeDocs = this.#includeDocs
|
|
496
|
+
const prevLimit = this.#limit
|
|
497
|
+
|
|
498
|
+
this.excludeDocs()
|
|
499
|
+
let skipRemaining = skip
|
|
500
|
+
let iterationFetched = 0
|
|
501
|
+
do {
|
|
502
|
+
const toSkip = Math.min(QueryBuilder.maxLimit, skipRemaining)
|
|
503
|
+
this.setLimit(toSkip)
|
|
504
|
+
const { bookmark, rows } = await this.#execute()
|
|
505
|
+
this.setBookmark(bookmark)
|
|
506
|
+
iterationFetched = rows.length
|
|
507
|
+
skipRemaining -= rows.length
|
|
508
|
+
} while (skipRemaining > 0 && iterationFetched > 0)
|
|
509
|
+
|
|
510
|
+
this.#includeDocs = prevIncludeDocs
|
|
511
|
+
this.#limit = prevLimit
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
async #execute() {
|
|
464
515
|
const { url, cookie } = getCouchInfo()
|
|
465
|
-
const fullPath = `${url}/${this
|
|
516
|
+
const fullPath = `${url}/${this.#dbName}/_design/database/_search/${
|
|
517
|
+
this.#index
|
|
518
|
+
}`
|
|
466
519
|
const body = this.buildSearchBody()
|
|
467
520
|
try {
|
|
468
521
|
return await runQuery<T>(fullPath, body, cookie)
|
|
469
522
|
} catch (err: any) {
|
|
470
|
-
if (err.status === 404 && this
|
|
471
|
-
await this
|
|
523
|
+
if (err.status === 404 && this.#indexBuilder) {
|
|
524
|
+
await this.#indexBuilder()
|
|
472
525
|
return await runQuery<T>(fullPath, body, cookie)
|
|
473
526
|
} else {
|
|
474
527
|
throw err
|
|
@@ -502,8 +555,9 @@ async function runQuery<T>(
|
|
|
502
555
|
}
|
|
503
556
|
const json = await response.json()
|
|
504
557
|
|
|
505
|
-
let output:
|
|
558
|
+
let output: SearchResponse<T> = {
|
|
506
559
|
rows: [],
|
|
560
|
+
totalRows: 0,
|
|
507
561
|
}
|
|
508
562
|
if (json.rows != null && json.rows.length > 0) {
|
|
509
563
|
output.rows = json.rows.map((row: any) => row.doc)
|
|
@@ -511,6 +565,9 @@ async function runQuery<T>(
|
|
|
511
565
|
if (json.bookmark) {
|
|
512
566
|
output.bookmark = json.bookmark
|
|
513
567
|
}
|
|
568
|
+
if (json.total_rows) {
|
|
569
|
+
output.totalRows = json.total_rows
|
|
570
|
+
}
|
|
514
571
|
return output
|
|
515
572
|
}
|
|
516
573
|
|
|
@@ -543,8 +600,8 @@ async function recursiveSearch<T>(
|
|
|
543
600
|
if (rows.length >= params.limit) {
|
|
544
601
|
return rows
|
|
545
602
|
}
|
|
546
|
-
let pageSize =
|
|
547
|
-
if (rows.length > params.limit -
|
|
603
|
+
let pageSize = QueryBuilder.maxLimit
|
|
604
|
+
if (rows.length > params.limit - QueryBuilder.maxLimit) {
|
|
548
605
|
pageSize = params.limit - rows.length
|
|
549
606
|
}
|
|
550
607
|
const page = await new QueryBuilder<T>(dbName, index, query)
|
|
@@ -559,7 +616,7 @@ async function recursiveSearch<T>(
|
|
|
559
616
|
if (!page.rows.length) {
|
|
560
617
|
return rows
|
|
561
618
|
}
|
|
562
|
-
if (page.rows.length <
|
|
619
|
+
if (page.rows.length < QueryBuilder.maxLimit) {
|
|
563
620
|
return [...rows, ...page.rows]
|
|
564
621
|
}
|
|
565
622
|
const newParams = {
|
|
@@ -597,7 +654,7 @@ export async function paginatedSearch<T>(
|
|
|
597
654
|
if (limit == null || isNaN(limit) || limit < 0) {
|
|
598
655
|
limit = 50
|
|
599
656
|
}
|
|
600
|
-
limit = Math.min(limit,
|
|
657
|
+
limit = Math.min(limit, QueryBuilder.maxLimit)
|
|
601
658
|
const search = new QueryBuilder<T>(dbName, index, query)
|
|
602
659
|
if (params.version) {
|
|
603
660
|
search.setVersion(params.version)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./searchIndexes"
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { User, SearchIndex } from "@budibase/types"
|
|
2
|
+
import { getGlobalDB } from "../../context"
|
|
3
|
+
|
|
4
|
+
export async function createUserIndex() {
|
|
5
|
+
const db = getGlobalDB()
|
|
6
|
+
let designDoc
|
|
7
|
+
try {
|
|
8
|
+
designDoc = await db.get("_design/database")
|
|
9
|
+
} catch (err: any) {
|
|
10
|
+
if (err.status === 404) {
|
|
11
|
+
designDoc = { _id: "_design/database" }
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const fn = function (user: User) {
|
|
16
|
+
if (user._id && !user._id.startsWith("us_")) {
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
const ignoredFields = [
|
|
20
|
+
"_id",
|
|
21
|
+
"_rev",
|
|
22
|
+
"password",
|
|
23
|
+
"account",
|
|
24
|
+
"license",
|
|
25
|
+
"budibaseAccess",
|
|
26
|
+
"accountPortalAccess",
|
|
27
|
+
"csrfToken",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
function idx(input: Record<string, any>, prev?: string) {
|
|
31
|
+
for (let key of Object.keys(input)) {
|
|
32
|
+
if (ignoredFields.includes(key)) {
|
|
33
|
+
continue
|
|
34
|
+
}
|
|
35
|
+
let idxKey = prev != null ? `${prev}.${key}` : key
|
|
36
|
+
if (typeof input[key] === "string") {
|
|
37
|
+
// eslint-disable-next-line no-undef
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
index(idxKey, input[key].toLowerCase(), { facet: true })
|
|
40
|
+
} else if (typeof input[key] !== "object") {
|
|
41
|
+
// eslint-disable-next-line no-undef
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
index(idxKey, input[key], { facet: true })
|
|
44
|
+
} else {
|
|
45
|
+
idx(input[key], idxKey)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
idx(user)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
designDoc.indexes = {
|
|
53
|
+
[SearchIndex.USER]: {
|
|
54
|
+
index: fn.toString(),
|
|
55
|
+
analyzer: {
|
|
56
|
+
default: "keyword",
|
|
57
|
+
name: "perfield",
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
}
|
|
61
|
+
await db.put(designDoc)
|
|
62
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { newid } from "../../newid"
|
|
1
|
+
import { newid } from "../../docIds/newid"
|
|
2
2
|
import { getDB } from "../db"
|
|
3
3
|
import { Database } from "@budibase/types"
|
|
4
4
|
import { QueryBuilder, paginatedSearch, fullSearch } from "../lucene"
|
|
@@ -136,6 +136,106 @@ describe("lucene", () => {
|
|
|
136
136
|
const resp = await builder.run()
|
|
137
137
|
expect(resp.rows.length).toBe(2)
|
|
138
138
|
})
|
|
139
|
+
|
|
140
|
+
describe("skip", () => {
|
|
141
|
+
const skipDbName = `db-${newid()}`
|
|
142
|
+
let docs: {
|
|
143
|
+
_id: string
|
|
144
|
+
property: string
|
|
145
|
+
array: string[]
|
|
146
|
+
}[]
|
|
147
|
+
|
|
148
|
+
beforeAll(async () => {
|
|
149
|
+
const db = getDB(skipDbName)
|
|
150
|
+
|
|
151
|
+
docs = Array(QueryBuilder.maxLimit * 2.5)
|
|
152
|
+
.fill(0)
|
|
153
|
+
.map((_, i) => ({
|
|
154
|
+
_id: i.toString().padStart(3, "0"),
|
|
155
|
+
property: `value_${i.toString().padStart(3, "0")}`,
|
|
156
|
+
array: [],
|
|
157
|
+
}))
|
|
158
|
+
await db.bulkDocs(docs)
|
|
159
|
+
|
|
160
|
+
await db.put({
|
|
161
|
+
_id: "_design/database",
|
|
162
|
+
indexes: {
|
|
163
|
+
[INDEX_NAME]: {
|
|
164
|
+
index: index,
|
|
165
|
+
analyzer: "standard",
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
})
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
it("should be able to apply skip", async () => {
|
|
172
|
+
const builder = new QueryBuilder(skipDbName, INDEX_NAME)
|
|
173
|
+
const firstResponse = await builder.run()
|
|
174
|
+
builder.setSkip(40)
|
|
175
|
+
const secondResponse = await builder.run()
|
|
176
|
+
|
|
177
|
+
// Return the default limit
|
|
178
|
+
expect(firstResponse.rows.length).toBe(50)
|
|
179
|
+
expect(secondResponse.rows.length).toBe(50)
|
|
180
|
+
|
|
181
|
+
// Should have the expected overlap
|
|
182
|
+
expect(firstResponse.rows.slice(40)).toEqual(
|
|
183
|
+
secondResponse.rows.slice(0, 10)
|
|
184
|
+
)
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
it("should handle limits", async () => {
|
|
188
|
+
const builder = new QueryBuilder(skipDbName, INDEX_NAME)
|
|
189
|
+
builder.setLimit(10)
|
|
190
|
+
builder.setSkip(50)
|
|
191
|
+
builder.setSort("_id")
|
|
192
|
+
|
|
193
|
+
const resp = await builder.run()
|
|
194
|
+
expect(resp.rows.length).toBe(10)
|
|
195
|
+
expect(resp.rows).toEqual(
|
|
196
|
+
docs.slice(50, 60).map(expect.objectContaining)
|
|
197
|
+
)
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
it("should be able to skip searching through multiple responses", async () => {
|
|
201
|
+
const builder = new QueryBuilder(skipDbName, INDEX_NAME)
|
|
202
|
+
// Skipping 2 max limits plus a little bit more
|
|
203
|
+
const skip = QueryBuilder.maxLimit * 2 + 37
|
|
204
|
+
builder.setSkip(skip)
|
|
205
|
+
builder.setSort("_id")
|
|
206
|
+
const resp = await builder.run()
|
|
207
|
+
|
|
208
|
+
expect(resp.rows.length).toBe(50)
|
|
209
|
+
expect(resp.rows).toEqual(
|
|
210
|
+
docs.slice(skip, skip + resp.rows.length).map(expect.objectContaining)
|
|
211
|
+
)
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
it("should not return results if skipping all docs", async () => {
|
|
215
|
+
const builder = new QueryBuilder(skipDbName, INDEX_NAME)
|
|
216
|
+
// Skipping 2 max limits plus a little bit more
|
|
217
|
+
const skip = docs.length + 1
|
|
218
|
+
builder.setSkip(skip)
|
|
219
|
+
|
|
220
|
+
const resp = await builder.run()
|
|
221
|
+
|
|
222
|
+
expect(resp.rows.length).toBe(0)
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
it("skip should respect with filters", async () => {
|
|
226
|
+
const builder = new QueryBuilder(skipDbName, INDEX_NAME)
|
|
227
|
+
builder.setLimit(10)
|
|
228
|
+
builder.setSkip(50)
|
|
229
|
+
builder.addString("property", "value_1")
|
|
230
|
+
builder.setSort("property")
|
|
231
|
+
|
|
232
|
+
const resp = await builder.run()
|
|
233
|
+
expect(resp.rows.length).toBe(10)
|
|
234
|
+
expect(resp.rows).toEqual(
|
|
235
|
+
docs.slice(150, 160).map(expect.objectContaining)
|
|
236
|
+
)
|
|
237
|
+
})
|
|
238
|
+
})
|
|
139
239
|
})
|
|
140
240
|
|
|
141
241
|
describe("paginated search", () => {
|