@paralect/hive 0.1.50-alpha.2 → 0.1.50
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/.claude/settings.local.json +13 -0
- package/cli/helpers/execCommand.js +10 -10
- package/cli/helpers/isProjectInit.js +3 -3
- package/cli/helpers/mergeDirs.js +1 -1
- package/cli/hive.js +105 -105
- package/{test-app → framework}/.cursor/commands/add-service.md +6 -6
- package/{test-app → framework}/.cursor/skills/hive-overview/SKILL.md +1 -1
- package/{test-app → framework}/.cursor/skills/hive-service/SKILL.md +2 -2
- package/framework/bin/start.sh +2 -0
- package/{.hive/bootstrap-hive.js → framework/bootstrap-hive.ts} +4 -4
- package/{.hive → framework}/package-lock.json +212 -156
- package/framework/package.json +42 -0
- package/{.hive/src/app.js → framework/src/app.ts} +1 -1
- package/{.hive/src/bullMqBus.js → framework/src/bullMqBus.ts} +1 -1
- package/{.hive/src/bullMqWrapper.js → framework/src/bullMqWrapper.ts} +1 -1
- package/{.hive/src/app-config/index.js → framework/src/config/index.ts} +7 -7
- package/{.hive/src/db.js → framework/src/db.ts} +21 -6
- package/framework/src/endpoint.ts +132 -0
- package/framework/src/helpers/generateDbTypes.ts +47 -0
- package/{.hive/src/helpers/getMiddlewares.js → framework/src/helpers/getMiddlewares.ts} +1 -1
- package/{.hive/src/helpers/getResourceEndpoints.js → framework/src/helpers/getResourceEndpoints.ts} +8 -8
- package/{.hive/src/helpers/getResources.js → framework/src/helpers/getResources.ts} +1 -1
- package/{.hive/src/helpers/getSchemas.js → framework/src/helpers/getSchemas.ts} +13 -13
- package/framework/src/index.ts +3 -0
- package/{.hive/src/ioEmitter.js → framework/src/ioEmitter.ts} +1 -1
- package/{.hive/src/lib/node-mongo/src/mongo-query-service.js → framework/src/lib/node-mongo/src/mongo-query-service.ts} +18 -7
- package/{.hive/src/lib/node-mongo/src/mongo-service.js → framework/src/lib/node-mongo/src/mongo-service.ts} +17 -9
- package/{starter/src/logger.js → framework/src/logger.ts} +4 -3
- package/{.hive/src/migrations/migrations-log/migration-log.schema.js → framework/src/migrations/migrations-log/migration-log.schema.ts} +1 -1
- package/{.hive/src/migrations/migrations-log/migration-log.service.js → framework/src/migrations/migrations-log/migration-log.service.ts} +1 -1
- package/{.hive/src/migrations/migrations.schema.js → framework/src/migrations/migrations.schema.ts} +1 -1
- package/{.hive/src/resources/_dev/endpoints/triggerSchedulerHandler.js → framework/src/resources/_dev/endpoints/triggerSchedulerHandler.ts} +2 -1
- package/framework/src/resources/health/endpoints/get.ts +14 -0
- package/{.hive/src/resources/schemaMappings/schemaMappings.schema.js → framework/src/resources/schemaMappings/schemaMappings.schema.ts} +1 -1
- package/{.hive/src/routes/index.js → framework/src/routes/index.ts} +23 -27
- package/framework/src/schema.ts +3 -0
- package/{.hive/src/services/emailService.js → framework/src/services/emailService.ts} +1 -1
- package/{.hive/src/socketIo.js → framework/src/socketIo.ts} +1 -1
- package/{.hive → framework}/tsconfig.json +17 -13
- package/package.json +43 -51
- package/starter/hive/autoMap/schemaMappings.json +1 -0
- package/starter/hive/config/.env.example +11 -0
- package/starter/hive/config/app.ts +19 -0
- package/{.hive/src/services/setCookie.js → starter/hive/helpers/setCookie.ts} +1 -2
- package/starter/hive/middlewares/_global.ts +9 -0
- package/{.hive/src/middlewares/attachUser.js → starter/hive/middlewares/attachUser.ts} +2 -2
- package/{.hive/src/middlewares/shouldExist.js → starter/hive/middlewares/shouldExist.ts} +1 -1
- package/{.hive/src/middlewares/shouldNotExist.js → starter/hive/middlewares/shouldNotExist.ts} +1 -1
- package/starter/hive/resources/files/endpoints/upload.ts +25 -0
- package/starter/hive/resources/files/files.schema.ts +20 -0
- package/{.hive/src/resources/tokens/methods/storeToken.js → starter/hive/resources/tokens/methods/storeToken.ts} +3 -3
- package/{.hive/src/resources/tokens/tokens.schema.js → starter/hive/resources/tokens/tokens.schema.ts} +1 -2
- package/starter/hive/resources/users/endpoints/getCurrentUser.ts +12 -0
- package/starter/hive/resources/users/endpoints/getUserProfile.ts +17 -0
- package/{.hive/src/resources/users/users.schema.js → starter/hive/resources/users/users.schema.ts} +1 -2
- package/starter/hive/services/cloudStorage.ts +102 -0
- package/starter/package.json +9 -97
- package/starter/tsconfig.json +16 -20
- package/.cursor/commands/deslop.md +0 -12
- package/.hive/.cursor/commands/add-service.md +0 -188
- package/.hive/.cursor/skills/hive-overview/SKILL.md +0 -50
- package/.hive/.cursor/skills/hive-service/SKILL.md +0 -90
- package/.hive/bin/start.sh +0 -2
- package/.hive/package.json +0 -98
- package/.hive/src/logger.js +0 -43
- package/.hive/src/middlewares/global/tryToAttachUser.js +0 -33
- package/.hive/src/resources/health/endpoints/get.js +0 -19
- package/.hive/src/resources/tokens/methods/setToken.js +0 -8
- package/.hive/src/resources/users/endpoints/getCurrentUser.js +0 -14
- package/.hive/src/resources/users/endpoints/getUserProfile.js +0 -19
- package/.hive/src/resources/users/methods/ensureUserCreated.js +0 -68
- package/.hive/src/security.util.js +0 -38
- package/.hive/src/services/globalTest.js +0 -0
- package/cli/helpers/docker.js +0 -59
- package/cli/helpers/envCheck.js +0 -127
- package/cli/helpers/findPort.js +0 -32
- package/starter/.babelrc +0 -3
- package/starter/.cursor/commands/add-endpoint.md +0 -262
- package/starter/.cursor/commands/add-handler.md +0 -137
- package/starter/.cursor/commands/add-middleware.md +0 -95
- package/starter/.cursor/commands/add-resource.md +0 -71
- package/starter/.cursor/commands/add-scheduler.md +0 -138
- package/starter/.cursor/commands/add-service.md +0 -188
- package/starter/.cursor/skills/hive-auth/SKILL.md +0 -134
- package/starter/.cursor/skills/hive-database/SKILL.md +0 -103
- package/starter/.cursor/skills/hive-endpoint/SKILL.md +0 -103
- package/starter/.cursor/skills/hive-handler/SKILL.md +0 -88
- package/starter/.cursor/skills/hive-mapping/SKILL.md +0 -85
- package/starter/.cursor/skills/hive-middleware/SKILL.md +0 -104
- package/starter/.cursor/skills/hive-overview/SKILL.md +0 -50
- package/starter/.cursor/skills/hive-scheduler/SKILL.md +0 -94
- package/starter/.cursor/skills/hive-schema/SKILL.md +0 -73
- package/starter/.cursor/skills/hive-service/SKILL.md +0 -90
- package/starter/.dockerignore +0 -1
- package/starter/Dockerfile +0 -22
- package/starter/Dockerfile.dev +0 -33
- package/starter/Dockerfile.prod +0 -29
- package/starter/README.md +0 -11
- package/starter/bin/deploy.sh +0 -5
- package/starter/bin/start.sh +0 -2
- package/starter/bootstrap-hive.js +0 -118
- package/starter/deploy/api/Chart.yaml +0 -6
- package/starter/deploy/api/staging.yaml +0 -3
- package/starter/deploy/api/templates/deployment.yaml +0 -44
- package/starter/deploy/api/templates/ingress.yaml +0 -26
- package/starter/deploy/api/templates/service.yaml +0 -14
- package/starter/deploy/script/Dockerfile +0 -39
- package/starter/deploy/script/package-lock.json +0 -1499
- package/starter/deploy/script/package.json +0 -12
- package/starter/deploy/script/src/config.js +0 -48
- package/starter/deploy/script/src/index.js +0 -108
- package/starter/deploy/script/src/util.js +0 -19
- package/starter/initial-data.json +0 -176
- package/starter/loader.mjs +0 -40
- package/starter/package-lock.json +0 -13492
- package/starter/register.mjs +0 -6
- package/starter/ship_logo.png +0 -0
- package/starter/src/app-config/app.js +0 -3
- package/starter/src/app-config/assertEnv.js +0 -15
- package/starter/src/app-config/index.js +0 -65
- package/starter/src/app.js +0 -65
- package/starter/src/assets/emails/components/header.mjml +0 -13
- package/starter/src/assets/emails/dist/.gitkeep +0 -0
- package/starter/src/assets/emails/signup-welcome.mjml +0 -34
- package/starter/src/assets/emails/styles/index.mjml +0 -77
- package/starter/src/autoMap/addHandlers.js +0 -142
- package/starter/src/autoMap/getDependentFields.js +0 -37
- package/starter/src/autoMap/mapSchema.js +0 -99
- package/starter/src/autoMap/schemaMappings.js +0 -13
- package/starter/src/autoMap/schemaMappings.json +0 -3
- package/starter/src/bullMqBus.js +0 -21
- package/starter/src/bullMqWrapper.js +0 -23
- package/starter/src/db.js +0 -53
- package/starter/src/emails/MyEmailComponent.jsx +0 -14
- package/starter/src/emails/compiled/MyEmailComponent.js +0 -18
- package/starter/src/emails/compiled/compiled/MyEmailComponent.js +0 -18
- package/starter/src/helpers/db/ifUpdated.js +0 -22
- package/starter/src/helpers/esm.js +0 -56
- package/starter/src/helpers/getMiddlewares.js +0 -41
- package/starter/src/helpers/getResourceEndpoints.js +0 -31
- package/starter/src/helpers/getResources.js +0 -35
- package/starter/src/helpers/getSchemas.js +0 -53
- package/starter/src/helpers/importHandlers.js +0 -20
- package/starter/src/helpers/isZodArray.js +0 -13
- package/starter/src/helpers/prettierFormat.js +0 -8
- package/starter/src/helpers/schema/db.schema.js +0 -9
- package/starter/src/helpers/schema/pagination.schema.js +0 -14
- package/starter/src/ioEmitter.js +0 -9
- package/starter/src/jsconfig.json +0 -5
- package/starter/src/lib/node-mongo/.github/workflows/npm-publish.yml +0 -32
- package/starter/src/lib/node-mongo/API.md +0 -654
- package/starter/src/lib/node-mongo/CHANGELOG.md +0 -98
- package/starter/src/lib/node-mongo/README.md +0 -97
- package/starter/src/lib/node-mongo/package-lock.json +0 -3682
- package/starter/src/lib/node-mongo/package.json +0 -74
- package/starter/src/lib/node-mongo/src/index.js +0 -64
- package/starter/src/lib/node-mongo/src/mongo-query-service.js +0 -78
- package/starter/src/lib/node-mongo/src/mongo-service-error.js +0 -15
- package/starter/src/lib/node-mongo/src/mongo-service.js +0 -303
- package/starter/src/middlewares/attachUser.js +0 -41
- package/starter/src/middlewares/global/extractUserTokens.js +0 -15
- package/starter/src/middlewares/global/tryToAttachUser.js +0 -33
- package/starter/src/middlewares/isAuthorized.js +0 -18
- package/starter/src/middlewares/shouldExist.js +0 -37
- package/starter/src/middlewares/shouldNotExist.js +0 -19
- package/starter/src/middlewares/uploadFile.js +0 -5
- package/starter/src/middlewares/validate.js +0 -32
- package/starter/src/migrations/migration.js +0 -8
- package/starter/src/migrations/migration.service.js +0 -76
- package/starter/src/migrations/migrations/1.js +0 -22
- package/starter/src/migrations/migrations-log/migration-log.schema.js +0 -13
- package/starter/src/migrations/migrations-log/migration-log.service.js +0 -50
- package/starter/src/migrations/migrations.schema.js +0 -6
- package/starter/src/migrations/migrator.js +0 -75
- package/starter/src/migrator.js +0 -3
- package/starter/src/resources/_dev/endpoints/triggerSchedulerHandler.js +0 -32
- package/starter/src/resources/health/endpoints/get.js +0 -19
- package/starter/src/resources/schemaMappings/schemaMappings.schema.js +0 -6
- package/starter/src/resources/tokens/methods/generateSecureToken.js +0 -9
- package/starter/src/resources/tokens/methods/setToken.js +0 -8
- package/starter/src/resources/tokens/methods/storeToken.js +0 -35
- package/starter/src/resources/tokens/tokens.schema.js +0 -11
- package/starter/src/resources/users/endpoints/getCurrentUser.js +0 -14
- package/starter/src/resources/users/endpoints/getUserProfile.js +0 -19
- package/starter/src/resources/users/handlers/test.js +0 -1
- package/starter/src/resources/users/methods/ensureUserCreated.js +0 -68
- package/starter/src/resources/users/users.schema.js +0 -16
- package/starter/src/routes/index.js +0 -172
- package/starter/src/routes/middlewares/attachCustomErrors.js +0 -28
- package/starter/src/routes/middlewares/routeErrorHandler.js +0 -27
- package/starter/src/scheduler.js +0 -35
- package/starter/src/security.util.js +0 -38
- package/starter/src/services/emailService.js +0 -15
- package/starter/src/services/globalTest.js +0 -0
- package/starter/src/services/setCookie.js +0 -21
- package/starter/src/socketIo.js +0 -99
- package/test-app/.cursor/commands/add-endpoint.md +0 -262
- package/test-app/.cursor/commands/add-handler.md +0 -137
- package/test-app/.cursor/commands/add-middleware.md +0 -95
- package/test-app/.cursor/commands/add-resource.md +0 -71
- package/test-app/.cursor/commands/add-scheduler.md +0 -138
- package/test-app/.cursor/skills/hive-auth/SKILL.md +0 -134
- package/test-app/.cursor/skills/hive-database/SKILL.md +0 -103
- package/test-app/.cursor/skills/hive-endpoint/SKILL.md +0 -103
- package/test-app/.cursor/skills/hive-handler/SKILL.md +0 -88
- package/test-app/.cursor/skills/hive-mapping/SKILL.md +0 -85
- package/test-app/.cursor/skills/hive-middleware/SKILL.md +0 -104
- package/test-app/.cursor/skills/hive-scheduler/SKILL.md +0 -94
- package/test-app/.cursor/skills/hive-schema/SKILL.md +0 -73
- package/test-app/package-lock.json +0 -462
- package/test-app/package.json +0 -21
- /package/{.hive → framework}/.babelrc +0 -0
- /package/{.hive → framework}/.cursor/commands/add-endpoint.md +0 -0
- /package/{.hive → framework}/.cursor/commands/add-handler.md +0 -0
- /package/{.hive → framework}/.cursor/commands/add-middleware.md +0 -0
- /package/{.hive → framework}/.cursor/commands/add-resource.md +0 -0
- /package/{.hive → framework}/.cursor/commands/add-scheduler.md +0 -0
- /package/{.hive → framework}/.cursor/skills/hive-auth/SKILL.md +0 -0
- /package/{.hive → framework}/.cursor/skills/hive-database/SKILL.md +0 -0
- /package/{.hive → framework}/.cursor/skills/hive-endpoint/SKILL.md +0 -0
- /package/{.hive → framework}/.cursor/skills/hive-handler/SKILL.md +0 -0
- /package/{.hive → framework}/.cursor/skills/hive-mapping/SKILL.md +0 -0
- /package/{.hive → framework}/.cursor/skills/hive-middleware/SKILL.md +0 -0
- /package/{.hive → framework}/.cursor/skills/hive-scheduler/SKILL.md +0 -0
- /package/{.hive → framework}/.cursor/skills/hive-schema/SKILL.md +0 -0
- /package/{.hive → framework}/.dockerignore +0 -0
- /package/{.hive → framework}/Dockerfile +0 -0
- /package/{.hive → framework}/Dockerfile.dev +0 -0
- /package/{.hive → framework}/Dockerfile.prod +0 -0
- /package/{.hive → framework}/README.md +0 -0
- /package/{.hive → framework}/bin/deploy.sh +0 -0
- /package/{.hive → framework}/deploy/api/Chart.yaml +0 -0
- /package/{.hive → framework}/deploy/api/staging.yaml +0 -0
- /package/{.hive → framework}/deploy/api/templates/deployment.yaml +0 -0
- /package/{.hive → framework}/deploy/api/templates/ingress.yaml +0 -0
- /package/{.hive → framework}/deploy/api/templates/service.yaml +0 -0
- /package/{.hive → framework}/deploy/script/Dockerfile +0 -0
- /package/{.hive → framework}/deploy/script/package-lock.json +0 -0
- /package/{.hive → framework}/deploy/script/package.json +0 -0
- /package/{.hive/deploy/script/src/config.js → framework/deploy/script/src/config.ts} +0 -0
- /package/{.hive/deploy/script/src/index.js → framework/deploy/script/src/index.ts} +0 -0
- /package/{.hive/deploy/script/src/util.js → framework/deploy/script/src/util.ts} +0 -0
- /package/{.hive → framework}/initial-data.json +0 -0
- /package/{.hive → framework}/ship_logo.png +0 -0
- /package/{.hive → framework}/src/assets/emails/components/header.mjml +0 -0
- /package/{.hive → framework}/src/assets/emails/dist/.gitkeep +0 -0
- /package/{.hive → framework}/src/assets/emails/signup-welcome.mjml +0 -0
- /package/{.hive → framework}/src/assets/emails/styles/index.mjml +0 -0
- /package/{.hive/src/autoMap/addHandlers.js → framework/src/autoMap/addHandlers.ts} +0 -0
- /package/{.hive/src/autoMap/getDependentFields.js → framework/src/autoMap/getDependentFields.ts} +0 -0
- /package/{.hive/src/autoMap/mapSchema.js → framework/src/autoMap/mapSchema.ts} +0 -0
- /package/{.hive → framework}/src/autoMap/schemaMappings.json +0 -0
- /package/{.hive/src/autoMap/schemaMappings.js → framework/src/autoMap/schemaMappings.ts} +0 -0
- /package/{.hive/src/helpers/schema/db.schema.js → framework/src/common/schema/db.schema.ts} +0 -0
- /package/{.hive/src/helpers/schema/pagination.schema.js → framework/src/common/schema/pagination.schema.ts} +0 -0
- /package/{.hive/src/app-config/app.js → framework/src/config/app.ts} +0 -0
- /package/{.hive/src/app-config/assertEnv.js → framework/src/config/assertEnv.ts} +0 -0
- /package/{.hive/src/emails/MyEmailComponent.jsx → framework/src/emails/MyEmailComponent.tsx} +0 -0
- /package/{.hive/src/emails/compiled/MyEmailComponent.js → framework/src/emails/compiled/MyEmailComponent.ts} +0 -0
- /package/{.hive/src/emails/compiled/compiled/MyEmailComponent.js → framework/src/emails/compiled/compiled/MyEmailComponent.ts} +0 -0
- /package/{.hive/src/helpers/db/ifUpdated.js → framework/src/helpers/db/ifUpdated.ts} +0 -0
- /package/{.hive/src/helpers/importHandlers.js → framework/src/helpers/importHandlers.ts} +0 -0
- /package/{.hive/src/helpers/isZodArray.js → framework/src/helpers/isZodArray.ts} +0 -0
- /package/{.hive/src/helpers/prettierFormat.js → framework/src/helpers/prettierFormat.ts} +0 -0
- /package/{.hive → framework}/src/jsconfig.json +0 -0
- /package/{.hive → framework}/src/lib/node-mongo/.github/workflows/npm-publish.yml +0 -0
- /package/{.hive → framework}/src/lib/node-mongo/API.md +0 -0
- /package/{.hive → framework}/src/lib/node-mongo/CHANGELOG.md +0 -0
- /package/{.hive → framework}/src/lib/node-mongo/README.md +0 -0
- /package/{.hive → framework}/src/lib/node-mongo/package-lock.json +0 -0
- /package/{.hive → framework}/src/lib/node-mongo/package.json +0 -0
- /package/{.hive/src/lib/node-mongo/src/index.js → framework/src/lib/node-mongo/src/index.ts} +0 -0
- /package/{.hive/src/lib/node-mongo/src/mongo-service-error.js → framework/src/lib/node-mongo/src/mongo-service-error.ts} +0 -0
- /package/{.hive/src/middlewares/allowNoAuth.js → framework/src/middlewares/allowNoAuth.ts} +0 -0
- /package/{.hive/src/middlewares/global/extractUserTokens.js → framework/src/middlewares/global/extractUserTokens.ts} +0 -0
- /package/{.hive/src/middlewares/validate.js → framework/src/middlewares/validate.ts} +0 -0
- /package/{.hive/src/migrations/migration.service.js → framework/src/migrations/migration.service.ts} +0 -0
- /package/{.hive/src/migrations/migration.js → framework/src/migrations/migration.ts} +0 -0
- /package/{.hive/src/migrations/migrations/1.js → framework/src/migrations/migrations/1.ts} +0 -0
- /package/{.hive/src/migrations/migrator.js → framework/src/migrations/migrator.ts} +0 -0
- /package/{.hive/src/migrator.js → framework/src/migrator.ts} +0 -0
- /package/{.hive/src/routes/middlewares/attachCustomErrors.js → framework/src/routes/middlewares/attachCustomErrors.ts} +0 -0
- /package/{.hive/src/routes/middlewares/routeErrorHandler.js → framework/src/routes/middlewares/routeErrorHandler.ts} +0 -0
- /package/{.hive/src/scheduler/handlers/sendDailyReport.example.js → framework/src/scheduler/handlers/sendDailyReport.example.ts} +0 -0
- /package/{.hive/src/scheduler.js → framework/src/scheduler.ts} +0 -0
- /package/starter/{src/middlewares/allowNoAuth.js → hive/middlewares/allowNoAuth.ts} +0 -0
- /package/{.hive/src/middlewares/isAuthorized.js → starter/hive/middlewares/isAuthorized.ts} +0 -0
- /package/{.hive/src/middlewares/uploadFile.js → starter/hive/middlewares/uploadFile.ts} +0 -0
- /package/{.hive/src/resources/tokens/methods/generateSecureToken.js → starter/hive/resources/tokens/methods/generateSecureToken.ts} +0 -0
- /package/{.hive/src/resources/users/handlers/test.js → starter/hive/resources/users/handlers/test.ts} +0 -0
- /package/starter/{src/scheduler/handlers/sendDailyReport.example.js → hive/scheduler/handlers/dailyExample.ts} +0 -0
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"_args": [
|
|
3
|
-
[
|
|
4
|
-
"@paralect/node-mongo@2.1.1",
|
|
5
|
-
"/Users/igorkrasnik/Documents/work/trojanflix-api"
|
|
6
|
-
]
|
|
7
|
-
],
|
|
8
|
-
"_from": "@paralect/node-mongo@2.1.1",
|
|
9
|
-
"_id": "@paralect/node-mongo@2.1.1",
|
|
10
|
-
"_inBundle": false,
|
|
11
|
-
"_integrity": "sha512-IneYD4qTa2P0St3m3Zlw/nRXejTsrsepkmEy7Qw0KYjEQeoekr6gxC6edOPouupkQA0qBOUl4zQSdkaEIUn36w==",
|
|
12
|
-
"_location": "/@paralect/node-mongo",
|
|
13
|
-
"_phantomChildren": {},
|
|
14
|
-
"_requested": {
|
|
15
|
-
"type": "version",
|
|
16
|
-
"registry": true,
|
|
17
|
-
"raw": "@paralect/node-mongo@2.1.1",
|
|
18
|
-
"name": "@paralect/node-mongo",
|
|
19
|
-
"escapedName": "@paralect%2fnode-mongo",
|
|
20
|
-
"scope": "@paralect",
|
|
21
|
-
"rawSpec": "2.1.1",
|
|
22
|
-
"saveSpec": null,
|
|
23
|
-
"fetchSpec": "2.1.1"
|
|
24
|
-
},
|
|
25
|
-
"_requiredBy": [
|
|
26
|
-
"/"
|
|
27
|
-
],
|
|
28
|
-
"_resolved": "https://registry.npmjs.org/@paralect/node-mongo/-/node-mongo-2.1.1.tgz",
|
|
29
|
-
"_spec": "2.1.1",
|
|
30
|
-
"_where": "/Users/igorkrasnik/Documents/work/trojanflix-api",
|
|
31
|
-
"author": {
|
|
32
|
-
"name": "Paralect"
|
|
33
|
-
},
|
|
34
|
-
"bugs": {
|
|
35
|
-
"url": "https://github.com/paralect/node-mongo/issues"
|
|
36
|
-
},
|
|
37
|
-
"dependencies": {
|
|
38
|
-
"lodash": "4.17.20",
|
|
39
|
-
"monk": "7.3.2"
|
|
40
|
-
},
|
|
41
|
-
"description": "MongoDB wrapper for Node.JS 12",
|
|
42
|
-
"devDependencies": {
|
|
43
|
-
"chai": "^4.2.0",
|
|
44
|
-
"chai-spies": "^1.0.0",
|
|
45
|
-
"eslint": "^7.10.0",
|
|
46
|
-
"eslint-config-airbnb-base": "^14.2.0",
|
|
47
|
-
"eslint-plugin-import": "^2.22.1",
|
|
48
|
-
"mocha": "^8.1.3",
|
|
49
|
-
"npm-run-all": "^4.1.5"
|
|
50
|
-
},
|
|
51
|
-
"engines": {
|
|
52
|
-
"node": ">=12.0.0"
|
|
53
|
-
},
|
|
54
|
-
"homepage": "https://github.com/paralect/node-mongo#readme",
|
|
55
|
-
"keywords": [
|
|
56
|
-
"mongo",
|
|
57
|
-
"monk",
|
|
58
|
-
"paralect"
|
|
59
|
-
],
|
|
60
|
-
"license": "MIT",
|
|
61
|
-
"main": "src/index.js",
|
|
62
|
-
"name": "@paralect/node-mongo",
|
|
63
|
-
"private": false,
|
|
64
|
-
"repository": {
|
|
65
|
-
"type": "git",
|
|
66
|
-
"url": "git+https://github.com/paralect/node-mongo.git"
|
|
67
|
-
},
|
|
68
|
-
"scripts": {
|
|
69
|
-
"test": "run-s test:*",
|
|
70
|
-
"test:eslint": "eslint ./",
|
|
71
|
-
"test:mocha": "NODE_ENV=test mocha --exit --recursive -c -R spec src/index.test.js"
|
|
72
|
-
},
|
|
73
|
-
"version": "2.1.1"
|
|
74
|
-
}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import monk from 'monk';
|
|
2
|
-
import _ from 'lodash';
|
|
3
|
-
import MongoService from './mongo-service';
|
|
4
|
-
import MongoQueryService from './mongo-query-service';
|
|
5
|
-
|
|
6
|
-
const logger = global.logger || console;
|
|
7
|
-
|
|
8
|
-
export const connect = (connectionString, settings) => {
|
|
9
|
-
const connectionSettings = _.defaults({}, settings, {
|
|
10
|
-
connectTimeoutMS: 20000,
|
|
11
|
-
});
|
|
12
|
-
const db = monk(connectionString, connectionSettings);
|
|
13
|
-
|
|
14
|
-
db.on("error-opening", (err) => {
|
|
15
|
-
logger.error(err, "Failed to connect to the mongodb on start");
|
|
16
|
-
throw err;
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
db.on("open", () => {
|
|
20
|
-
logger.info(`Connected to mongodb`);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
db.on("close", (err) => {
|
|
24
|
-
if (err) {
|
|
25
|
-
logger.error(err, `Lost connection with mongodb: ${connectionString}`);
|
|
26
|
-
} else {
|
|
27
|
-
logger.warn(`Closed connection with mongodb: ${connectionString}`);
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
db.on("connected", (err) => {
|
|
32
|
-
if (err) {
|
|
33
|
-
logger.error(err);
|
|
34
|
-
} else {
|
|
35
|
-
logger.info(`Connected to mongodb`);
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
db.createService = (collectionName, options = {}) => {
|
|
40
|
-
const collection = db.get(collectionName, { castIds: false });
|
|
41
|
-
|
|
42
|
-
return new MongoService(collection, options);
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
db.setServiceMethod = (name, method) => {
|
|
46
|
-
MongoService.prototype[name] = function customMethod(...args) {
|
|
47
|
-
return method.apply(this, [this, ...args]);
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
db.createQueryService = (collectionName, options = {}) => {
|
|
52
|
-
const collection = db.get(collectionName, { castIds: false });
|
|
53
|
-
|
|
54
|
-
return new MongoQueryService(collection, options);
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
db.setQueryServiceMethod = (name, method) => {
|
|
58
|
-
MongoQueryService.prototype[name] = function customMethod(...args) {
|
|
59
|
-
return method.apply(this, [this, ...args]);
|
|
60
|
-
};
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
return db;
|
|
64
|
-
};
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
import MongoServiceError from './mongo-service-error';
|
|
3
|
-
|
|
4
|
-
class MongoQueryService {
|
|
5
|
-
constructor(collection, options = {}) {
|
|
6
|
-
this._collection = collection;
|
|
7
|
-
this._options = options;
|
|
8
|
-
|
|
9
|
-
this.name = collection.name;
|
|
10
|
-
|
|
11
|
-
this.aggregate = collection.aggregate;
|
|
12
|
-
this.count = collection.count;
|
|
13
|
-
this.distinct = collection.distinct;
|
|
14
|
-
this.geoHaystackSearch = collection.geoHaystackSearch;
|
|
15
|
-
this.indexes = collection.indexes;
|
|
16
|
-
this.mapReduce = collection.mapReduce;
|
|
17
|
-
this.stats = collection.stats;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
async find(query = {}, opt = { perPage: 100, page: 0 }) {
|
|
21
|
-
const options = _.cloneDeep(opt);
|
|
22
|
-
const { page, perPage } = options;
|
|
23
|
-
const hasPaging = page > 0;
|
|
24
|
-
if (hasPaging) {
|
|
25
|
-
options.skip = (page - 1) * perPage;
|
|
26
|
-
options.limit = perPage;
|
|
27
|
-
}
|
|
28
|
-
delete options.perPage;
|
|
29
|
-
delete options.page;
|
|
30
|
-
|
|
31
|
-
const results = await this._collection.find(query, options);
|
|
32
|
-
if (!hasPaging) {
|
|
33
|
-
return {
|
|
34
|
-
results,
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const countOptions = {};
|
|
39
|
-
if (options.session) countOptions.session = options.session;
|
|
40
|
-
const count = await this._collection.count(query, countOptions);
|
|
41
|
-
const pagesCount = Math.ceil(count / perPage) || 1;
|
|
42
|
-
|
|
43
|
-
return {
|
|
44
|
-
pagesCount,
|
|
45
|
-
results: results.map(doc => {
|
|
46
|
-
return _.omit(doc, opt.isIncludeSecureFields ? [] : this._options.secureFields || []);
|
|
47
|
-
}),
|
|
48
|
-
count,
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async findOne(query = {}, options = {}) {
|
|
53
|
-
const { results } = await this.find(query, { limit: 2, ...options });
|
|
54
|
-
|
|
55
|
-
// if (results.length > 1) {
|
|
56
|
-
// throw new MongoServiceError(
|
|
57
|
-
// MongoServiceError.MORE_THAN_ONE,
|
|
58
|
-
// `findOne: More than one document return for query ${JSON.stringify(
|
|
59
|
-
// query
|
|
60
|
-
// )}`
|
|
61
|
-
// );
|
|
62
|
-
// }
|
|
63
|
-
let result = results[0] || null;
|
|
64
|
-
|
|
65
|
-
if (result && !options.isIncludeSecureFields) {
|
|
66
|
-
result = _.omit(result, this._options.secureFields || []);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return result;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async exists(query, options = {}) {
|
|
73
|
-
const count = await this.count(query, { limit: 1, ...options });
|
|
74
|
-
return count > 0;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export default MongoQueryService;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
class MongoServiceError extends Error {
|
|
2
|
-
constructor(code, message, error) {
|
|
3
|
-
super(message);
|
|
4
|
-
this.name = "MongoServiceError";
|
|
5
|
-
this.code = code;
|
|
6
|
-
this.error = error;
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
MongoServiceError.NOT_FOUND = "NOT_FOUND";
|
|
11
|
-
MongoServiceError.MORE_THAN_ONE = "MORE_THAN_ONE";
|
|
12
|
-
MongoServiceError.INVALID_SCHEMA = "INVALID_SCHEMA";
|
|
13
|
-
MongoServiceError.INVALID_ARGUMENT = "INVALID_ARGUMENT";
|
|
14
|
-
|
|
15
|
-
export default MongoServiceError;
|
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
import monk from 'monk';
|
|
2
|
-
import { EventEmitter } from 'events';
|
|
3
|
-
import _ from 'lodash';
|
|
4
|
-
import MongoQueryService from './mongo-query-service';
|
|
5
|
-
import MongoServiceError from './mongo-service-error';
|
|
6
|
-
|
|
7
|
-
const defaultOptions = {
|
|
8
|
-
addCreatedOnField: true,
|
|
9
|
-
addUpdatedOnField: true,
|
|
10
|
-
useStringId: true,
|
|
11
|
-
validate: undefined,
|
|
12
|
-
emitter: undefined,
|
|
13
|
-
secureFields: ['verySecureField'],
|
|
14
|
-
|
|
15
|
-
onBeforeCreated: ({ docs }) => docs,
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
class MongoService extends MongoQueryService {
|
|
19
|
-
constructor(collection, options = {}) {
|
|
20
|
-
super(collection, options);
|
|
21
|
-
_.defaults(this._options, defaultOptions);
|
|
22
|
-
|
|
23
|
-
this._bus = this._options.emitter || new EventEmitter();
|
|
24
|
-
|
|
25
|
-
this.generateId = () => monk.id().toHexString();
|
|
26
|
-
|
|
27
|
-
this.atomic = {
|
|
28
|
-
bulkWrite: collection.bulkWrite,
|
|
29
|
-
createIndex: collection.createIndex,
|
|
30
|
-
drop: collection.drop,
|
|
31
|
-
dropIndex: collection.dropIndex,
|
|
32
|
-
dropIndexes: collection.dropIndexes,
|
|
33
|
-
findOneAndDelete: collection.findOneAndDelete,
|
|
34
|
-
findOneAndUpdate: collection.findOneAndUpdate,
|
|
35
|
-
insert: collection.insert,
|
|
36
|
-
remove: collection.remove,
|
|
37
|
-
update: collection.update,
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
collection.manager
|
|
41
|
-
.executeWhenOpened()
|
|
42
|
-
.then(async () => {
|
|
43
|
-
await collection.manager._db.command({ create: collection.name });
|
|
44
|
-
})
|
|
45
|
-
.catch((error) => {
|
|
46
|
-
// a collection already exists
|
|
47
|
-
if (error.code !== 48) {
|
|
48
|
-
throw error;
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
static _deepCompare(data, initialData, properties) {
|
|
54
|
-
let changed = false;
|
|
55
|
-
|
|
56
|
-
if (Array.isArray(properties)) {
|
|
57
|
-
changed =
|
|
58
|
-
_.find(properties, (prop) => {
|
|
59
|
-
const value = _.get(data, prop);
|
|
60
|
-
const initialValue = _.get(initialData, prop);
|
|
61
|
-
|
|
62
|
-
return !_.isEqual(value, initialValue);
|
|
63
|
-
}) !== undefined;
|
|
64
|
-
} else {
|
|
65
|
-
Object.keys(properties).forEach((prop) => {
|
|
66
|
-
if (changed) return;
|
|
67
|
-
|
|
68
|
-
const value = _.get(data, prop);
|
|
69
|
-
const initialValue = _.get(initialData, prop);
|
|
70
|
-
|
|
71
|
-
if (
|
|
72
|
-
_.isEqual(value, properties[prop]) &&
|
|
73
|
-
!_.isEqual(initialValue, properties[prop])
|
|
74
|
-
) {
|
|
75
|
-
changed = true;
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return changed;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async _validate(entity) {
|
|
84
|
-
if (this._options.validate) {
|
|
85
|
-
const { value, error } = await this._options.validate(entity);
|
|
86
|
-
|
|
87
|
-
if (error) {
|
|
88
|
-
console.log("Schema Error", error);
|
|
89
|
-
|
|
90
|
-
throw new MongoServiceError(
|
|
91
|
-
MongoServiceError.INVALID_SCHEMA,
|
|
92
|
-
`Document schema is invalid: ${JSON.stringify(error, null, 2)}`,
|
|
93
|
-
error
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return value;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return entity;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
emit(eventName, event) {
|
|
104
|
-
return this._bus.emit(`${this._collection.name}:${eventName}`, event);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
once(eventName, handler) {
|
|
108
|
-
return this._bus.once(`${this._collection.name}:${eventName}`, handler);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
on(eventName, handler) {
|
|
112
|
-
return this._bus.on(`${this._collection.name}:${eventName}`, handler);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
onUpdated(fieldNames = [], handler = (e) => { }) {
|
|
116
|
-
return this.on(`${this._collection.name}:${eventName}`, (event) => {
|
|
117
|
-
const { doc, prevDoc } = event;
|
|
118
|
-
|
|
119
|
-
let isFieldChanged = false;
|
|
120
|
-
|
|
121
|
-
if (_.isArray(fieldNames)) {
|
|
122
|
-
_.forEach(fieldNames, (fieldName) => {
|
|
123
|
-
if (!_.isEqual(doc[fieldName], prevDoc[fieldName])) {
|
|
124
|
-
isFieldChanged = true;
|
|
125
|
-
return false; // break loop
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return true;
|
|
129
|
-
});
|
|
130
|
-
} else if (_.isObject(fieldNames)) {
|
|
131
|
-
const fieldName = Object.keys(fieldNames)[0];
|
|
132
|
-
|
|
133
|
-
if (_.isEqual(_.get(doc, fieldName), fieldNames[fieldName]) && !_.isEqual(_.get(doc, fieldName), _.get(prevDoc, fieldName))) {
|
|
134
|
-
isFieldChanged = true;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (isFieldChanged) handler(event);
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async create(objs, options = {}) {
|
|
143
|
-
const entities = _.isArray(objs) ? objs : [objs];
|
|
144
|
-
|
|
145
|
-
let created = await Promise.all(
|
|
146
|
-
entities.map(async (doc) => {
|
|
147
|
-
const entity = _.cloneDeep(doc);
|
|
148
|
-
|
|
149
|
-
if (this._options.useStringId && !entity._id)
|
|
150
|
-
entity._id = this.generateId();
|
|
151
|
-
if (this._options.addCreatedOnField && !entity.createdOn) {
|
|
152
|
-
entity.createdOn = new Date().toISOString();
|
|
153
|
-
}
|
|
154
|
-
const validated = await this._validate(entity);
|
|
155
|
-
|
|
156
|
-
return validated;
|
|
157
|
-
})
|
|
158
|
-
);
|
|
159
|
-
|
|
160
|
-
created = await this._options.onBeforeCreated({ docs: created });
|
|
161
|
-
|
|
162
|
-
await this._collection.insert(created, options);
|
|
163
|
-
|
|
164
|
-
created.forEach((doc) => {
|
|
165
|
-
this._bus.emit(`${this._collection.name}:created`, {
|
|
166
|
-
doc,
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
created = created.map(doc => {
|
|
171
|
-
return _.omit(doc, options.isIncludeSecureFields ? [] : this._options.secureFields);
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
return created.length > 1 ? created : created[0];
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
async updateOne(query, updateFn, options = {}) {
|
|
178
|
-
if (!_.isFunction(updateFn)) {
|
|
179
|
-
throw new MongoServiceError(
|
|
180
|
-
MongoServiceError.INVALID_ARGUMENT,
|
|
181
|
-
`updateOne: second argument is invalid. Expected a function but got ${typeof updateFn}`
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const findOptions = { isIncludeSecureFields: true };
|
|
186
|
-
if (options.session) findOptions.session = options.session;
|
|
187
|
-
const doc = await this.findOne(query, findOptions);
|
|
188
|
-
if (!doc) {
|
|
189
|
-
throw new MongoServiceError(
|
|
190
|
-
MongoServiceError.NOT_FOUND,
|
|
191
|
-
`updateOne: document not found. Query: ${JSON.stringify(query)}`
|
|
192
|
-
);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
let entity = _.cloneDeep(doc);
|
|
196
|
-
|
|
197
|
-
if (this._options.addUpdatedOnField)
|
|
198
|
-
entity.updatedOn = new Date().toISOString();
|
|
199
|
-
entity = await updateFn(entity);
|
|
200
|
-
let updated = await this._validate(entity);
|
|
201
|
-
|
|
202
|
-
await this._collection.update(
|
|
203
|
-
{ ...query, _id: doc._id },
|
|
204
|
-
{ $set: updated },
|
|
205
|
-
options
|
|
206
|
-
);
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
this._bus.emit(`${this._collection.name}:updated`, {
|
|
210
|
-
doc: updated,
|
|
211
|
-
prevDoc: doc,
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
updated = _.omit(updated, options.isIncludeSecureFields ? [] : this._options.secureFields);
|
|
215
|
-
|
|
216
|
-
return updated;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
async updateMany(query, updateFn, options = {}) {
|
|
220
|
-
if (!_.isFunction(updateFn)) {
|
|
221
|
-
throw new MongoServiceError(
|
|
222
|
-
MongoServiceError.INVALID_ARGUMENT,
|
|
223
|
-
`updateMany: second argument is invalid. Expected a function but got ${typeof updateFn}`
|
|
224
|
-
);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const findOptions = { isIncludeSecureFields: true };
|
|
228
|
-
if (options.session) findOptions.session = options.session;
|
|
229
|
-
const { results: docs } = await this.find(query, findOptions);
|
|
230
|
-
if (docs.length === 0) return [];
|
|
231
|
-
|
|
232
|
-
let updated = await Promise.all(
|
|
233
|
-
docs.map(async (doc) => {
|
|
234
|
-
let entity = _.cloneDeep(doc);
|
|
235
|
-
|
|
236
|
-
if (this._options.addUpdatedOnField)
|
|
237
|
-
entity.updatedOn = new Date().toISOString();
|
|
238
|
-
entity = await updateFn(entity);
|
|
239
|
-
const validated = await this._validate(entity);
|
|
240
|
-
|
|
241
|
-
return validated;
|
|
242
|
-
})
|
|
243
|
-
);
|
|
244
|
-
|
|
245
|
-
await Promise.all(
|
|
246
|
-
updated.map((doc) =>
|
|
247
|
-
this._collection.update(
|
|
248
|
-
{ ...query, _id: doc._id },
|
|
249
|
-
{ $set: doc },
|
|
250
|
-
options
|
|
251
|
-
)
|
|
252
|
-
)
|
|
253
|
-
);
|
|
254
|
-
|
|
255
|
-
updated.forEach((doc, index) => {
|
|
256
|
-
this._bus.emit(`${this._collection.name}:updated`, {
|
|
257
|
-
doc,
|
|
258
|
-
prevDoc: docs[index],
|
|
259
|
-
});
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
updated = updated.map(doc => _.omit(doc, options.isIncludeSecureFields ? [] : this._options.secureFields));
|
|
263
|
-
|
|
264
|
-
return updated;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
async remove(query, options = {}) {
|
|
268
|
-
const findOptions = {};
|
|
269
|
-
if (options.session) findOptions.session = options.session;
|
|
270
|
-
const removed = await this.find(query, findOptions);
|
|
271
|
-
await this._collection.remove(query, options);
|
|
272
|
-
|
|
273
|
-
removed.results.forEach((doc) => {
|
|
274
|
-
this._bus.emit(`${this._collection.name}:removed`, {
|
|
275
|
-
doc,
|
|
276
|
-
});
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
return removed;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
async performTransaction(transactionFn, options = {}) {
|
|
283
|
-
if (!_.isFunction(transactionFn)) {
|
|
284
|
-
throw new MongoServiceError(
|
|
285
|
-
MongoServiceError.INVALID_ARGUMENT,
|
|
286
|
-
`performTransaction: first argument is invalid. Expected a function but got ${typeof transactionFn}`
|
|
287
|
-
);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
await this._collection.manager.executeWhenOpened();
|
|
291
|
-
|
|
292
|
-
const session = this._collection.manager._client.startSession(options);
|
|
293
|
-
|
|
294
|
-
try {
|
|
295
|
-
await session.withTransaction(transactionFn);
|
|
296
|
-
} catch (error) {
|
|
297
|
-
session.endSession();
|
|
298
|
-
throw error;
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
export default MongoService;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import db from '#hive/db';
|
|
2
|
-
import config from '#hive/app-config';
|
|
3
|
-
|
|
4
|
-
const userService = db.services.users;
|
|
5
|
-
const tokenService = db.services.tokens;
|
|
6
|
-
|
|
7
|
-
const storeTokenToState = async (ctx) => {
|
|
8
|
-
let accessToken = ctx.cookies.get('access_token');
|
|
9
|
-
|
|
10
|
-
const { authorization } = ctx.headers;
|
|
11
|
-
|
|
12
|
-
if (!accessToken && authorization) {
|
|
13
|
-
accessToken = authorization.replace('Bearer', '').trim();
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (!accessToken && config._hive.authHeaderName && ctx.headers[config._hive.authHeaderName]) {
|
|
17
|
-
accessToken = ctx.headers[config._hive.authHeaderName];
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
ctx.state.accessToken = accessToken;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export default async (ctx, next) => {
|
|
24
|
-
storeTokenToState(ctx);
|
|
25
|
-
|
|
26
|
-
let token;
|
|
27
|
-
|
|
28
|
-
if (ctx.state.accessToken) {
|
|
29
|
-
token = await tokenService.findOne({ token: ctx.state.accessToken });
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (token) {
|
|
33
|
-
ctx.state.user = await userService.findOne({ _id: token.user._id });
|
|
34
|
-
|
|
35
|
-
if (token.metadata) {
|
|
36
|
-
ctx.state.user.authMetadata = token.metadata;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
await next();
|
|
41
|
-
};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
const storeTokenToState = async (ctx, next) => {
|
|
2
|
-
let accessToken = ctx.cookies.get("ship_access_token");
|
|
3
|
-
|
|
4
|
-
const { authorization } = ctx.headers;
|
|
5
|
-
|
|
6
|
-
if (!accessToken && authorization) {
|
|
7
|
-
accessToken = authorization.replace("Bearer", "").trim();
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
ctx.state.accessToken = accessToken;
|
|
11
|
-
|
|
12
|
-
await next();
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export default storeTokenToState;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import db from '#hive/db';
|
|
2
|
-
const userService = db.services.users;
|
|
3
|
-
const tokenService = db.services.tokens;
|
|
4
|
-
|
|
5
|
-
const storeTokenToState = async (ctx) => {
|
|
6
|
-
let accessToken = ctx.cookies.get("access_token");
|
|
7
|
-
|
|
8
|
-
const { authorization } = ctx.headers;
|
|
9
|
-
|
|
10
|
-
if (!accessToken && authorization) {
|
|
11
|
-
accessToken = authorization.replace("Bearer", "").trim();
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
ctx.state.accessToken = accessToken;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const tryToAttachUser = async (ctx, next) => {
|
|
18
|
-
storeTokenToState(ctx);
|
|
19
|
-
|
|
20
|
-
// let token;
|
|
21
|
-
|
|
22
|
-
// if (ctx.state.accessToken) {
|
|
23
|
-
// token = await tokenService.findOne({ token: ctx.state.accessToken });
|
|
24
|
-
// }
|
|
25
|
-
|
|
26
|
-
// if (token) {
|
|
27
|
-
// ctx.state.user = await userService.findOne({ _id: token.user._id });
|
|
28
|
-
// }
|
|
29
|
-
|
|
30
|
-
return next();
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export default tryToAttachUser;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import attachUser from './attachUser';
|
|
2
|
-
|
|
3
|
-
export default async (ctx, next) => {
|
|
4
|
-
if (ctx.state.isSkipAuth) {
|
|
5
|
-
ctx.state.user = null;
|
|
6
|
-
return next();
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
await attachUser(ctx, async () => {
|
|
10
|
-
if (ctx.state.user) {
|
|
11
|
-
return next();
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
ctx.status = 401;
|
|
15
|
-
ctx.body = {};
|
|
16
|
-
return null;
|
|
17
|
-
});
|
|
18
|
-
};
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import db from '#hive/db';
|
|
2
|
-
|
|
3
|
-
function singularize(word) {
|
|
4
|
-
const endings = {
|
|
5
|
-
ves: 'fe',
|
|
6
|
-
ies: 'y',
|
|
7
|
-
i: 'us',
|
|
8
|
-
zes: 'ze',
|
|
9
|
-
ses: 's',
|
|
10
|
-
es: 'e',
|
|
11
|
-
s: '',
|
|
12
|
-
};
|
|
13
|
-
return word.replace(
|
|
14
|
-
new RegExp(`(${Object.keys(endings).join('|')})$`),
|
|
15
|
-
(r) => endings[r]
|
|
16
|
-
);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export default (
|
|
20
|
-
resourceName,
|
|
21
|
-
{
|
|
22
|
-
criteria = (ctx) => ({ _id: ctx.params[`${singularize(resourceName)}Id`] }),
|
|
23
|
-
ctxName = resourceName,
|
|
24
|
-
} = {}
|
|
25
|
-
) => {
|
|
26
|
-
return async (ctx, next) => {
|
|
27
|
-
const doc = await db.services[resourceName].findOne(criteria(ctx));
|
|
28
|
-
ctx.state[ctxName] = doc;
|
|
29
|
-
ctx.state[singularize(resourceName)] = doc;
|
|
30
|
-
|
|
31
|
-
if (!doc) {
|
|
32
|
-
ctx.throw(404, { message: `${resourceName} not found` });
|
|
33
|
-
} else {
|
|
34
|
-
await next();
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
import db from '#hive/db';
|
|
3
|
-
|
|
4
|
-
export default (
|
|
5
|
-
resourceName,
|
|
6
|
-
{ criteria = (ctx) => ({ name: ctx.validatedData.name }) } = {}
|
|
7
|
-
) => {
|
|
8
|
-
return async (ctx, next) => {
|
|
9
|
-
const doc = await db.services[resourceName].findOne(criteria(ctx));
|
|
10
|
-
|
|
11
|
-
if (doc) {
|
|
12
|
-
ctx.throw(400, {
|
|
13
|
-
message: `${_.capitalize(resourceName)} already exists`,
|
|
14
|
-
});
|
|
15
|
-
} else {
|
|
16
|
-
await next();
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
};
|