user2818430
user2818430

Reputation: 6029

NestJs multitenant database per client - error

OK! 2'nd day going with this problem ... I will appreciate any input on this:

I am using this solution available here: https://github.com/databoxtech/nestjs-multi_tenant-multiple-database

I downloaded the solution, updated the packages to latest version as bellow: package.json

{
  "name": "school-graph",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^7.6.5",
    "@nestjs/core": "^7.6.5",
    "@nestjs/graphql": "^7.9.8",
    "@nestjs/jwt": "^7.2.0",
    "@nestjs/mongoose": "^7.2.2",
    "@nestjs/passport": "^7.1.5",
    "@nestjs/platform-express": "^7.6.5",
    "graphql": "^15.4.0",
    "graphql-tools": "^7.0.2",
    "mongoose": "^5.11.13",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.6.3"
  },
  "devDependencies": {
    "@nestjs/cli": "^7.5.4",
    "@nestjs/schematics": "^7.2.7",
    "@nestjs/testing": "^7.6.5",
    "@types/passport-jwt": "^3.0.3",
    "@types/passport-local": "^1.0.33",
    "@types/supertest": "^2.0.10",
    "@typescript-eslint/eslint-plugin": "^4.14.1",
    "@typescript-eslint/parser": "^4.14.1",
    "eslint": "^7.18.0",
    "eslint-config-prettier": "^7.2.0",
    "eslint-plugin-import": "^2.22.1",
    "jest": "^26.6.3",
    "prettier": "^2.2.1",
    "supertest": "^6.1.2",
    "ts-jest": "^26.4.4",
    "ts-loader": "^8.0.14",
    "ts-node": "^9.1.1",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^4.1.3"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

after running npm run start I get the following error:

src/database/students/students.module.ts:9:58 - error TS2322: Type 'Schema<Student, Model<Student>>' is not assignable to type 'Schema<Document<any>, Model<Document<any>>>'.
  Type 'Model<Student>' is not assignable to type 'Model<Document<any>>'.
    The types returned by 'createCollection(...)' are incompatible between these types.
      Type 'Promise<Collection<Student>>' is not assignable to type 'Promise<Collection<Document<any>>>'.
        Type 'Collection<Student>' is not assignable to type 'Collection<Document<any>>'.
          Types of property 'bulkWrite' are incompatible.
            Type '{ (operations: BulkWriteOperation<Student>[], callback: MongoCallback<BulkWriteOpResultObject>): void; (operations: BulkWriteOperation<...>[], options?: CollectionBulkWriteOptions): Promise<...>; (operations: BulkWriteOperation<...>[], options: CollectionBulkWriteOptions, callback: MongoCallback<...>): void; }' is not assignable to type '{ (operations: BulkWriteOperation<Document<any>>[], callback: MongoCallback<BulkWriteOpResultObject>): void; (operations: BulkWriteOperation<...>[], options?: CollectionBulkWriteOptions): Promise<...>; (operations: BulkWriteOperation<...>[], options: CollectionBulkWriteOptions, callback: MongoCallback<...>): void; }'.
              Types of parameters 'operations' and 'operations' are incompatible.
                Type 'BulkWriteOperation<Document<any>>[]' is not assignable to type 'BulkWriteOperation<Student>[]'.
                  Type 'BulkWriteOperation<Document<any>>' is not assignable to type 'BulkWriteOperation<Student>'.
                    Type 'BulkWriteInsertOneOperation<Document<any>>' is not assignable to type 'BulkWriteOperation<Student>'.
                      Type 'BulkWriteInsertOneOperation<Document<any>>' is not assignable to type 'BulkWriteInsertOneOperation<Student>'.
                        The types of 'insertOne.document' are incompatible between these types.
                          Type 'Pick<Document<any>, "update" | "get" | "delete" | "set" | "invalidate" | "populate" | "__v" | "$ignore" | "$isDefault" | "$isDeleted" | "$isEmpty" | "$isValid" | "$locals" | ... 39 more ... | "validateSync"> & { ...; }' is not assignable to type 'Pick<Student, "update" | "get" | "delete" | "set" | "invalidate" | "populate" | "__v" | "$ignore" | "$isDefault" | "$isDeleted" | "$isEmpty" | "$isValid" | "$locals" | ... 41 more ... | "birthday"> & { ...; }'.
                            Type 'Pick<Document<any>, "update" | "get" | "delete" | "set" | "invalidate" | "populate" | "__v" | "$ignore" | "$isDefault" | "$isDeleted" | "$isEmpty" | "$isValid" | "$locals" | ... 39 more ... | "validateSync"> & { ...; }' is missing the following properties from type 'Pick<Student, "update" | "get" | "delete" | "set" | "invalidate" | "populate" | "__v" | "$ignore" | "$isDefault" | "$isDeleted" | "$isEmpty" | "$isValid" | "$locals" | ... 41 more ... | "birthday">': name, birthday

9   imports: [TenancyModule.forFeature([{ name: 'Student', schema: StudentSchema }])],
                                                           ~~~~~~

  src/tenancy/interfaces/model-definition.interface.ts:5:5
    5     schema: Schema;
          ~~~~~~
    The expected type comes from property 'schema' which is declared here on type 'ModelDefinition'

Found 1 error(s).

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `nest start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:

the complete log looks like this:

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Users\\USERNAME\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'start'
1 verbose cli ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle [email protected]~prestart: [email protected]
6 info lifecycle [email protected]~start: [email protected]
7 verbose lifecycle [email protected]~start: unsafe-perm in lifecycle true
8 verbose lifecycle [email protected]~start: PATH: C:\Users\USERNAME\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\Users\USERNAME\Downloads\nestjs\node_modules\.bin;C:\Python39\Scripts\;C:\Python39\;C:\Program Files (x86)\VMware\VMware Workstation\bin\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\nodejs\;C:\ProgramData\chocolatey\bin;C:\Program Files\Git\cmd;C:\Users\USERNAME\AppData\Local\Microsoft\WindowsApps;;C:\Users\USERNAME\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\USERNAME\AppData\Roaming\npm
9 verbose lifecycle [email protected]~start: CWD: C:\Users\USERNAME\Downloads\nestjs
10 silly lifecycle [email protected]~start: Args: [ '/d /s /c', 'nest start' ]
11 silly lifecycle [email protected]~start: Returned: code: 1  signal: null
12 info lifecycle [email protected]~start: Failed to exec start script
13 verbose stack Error: [email protected] start: `nest start`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Users\USERNAME\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:315:20)
13 verbose stack     at ChildProcess.<anonymous> (C:\Users\USERNAME\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:315:20)
13 verbose stack     at maybeClose (internal/child_process.js:1048:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
14 verbose pkgid [email protected]
15 verbose cwd C:\Users\USERNAME\Downloads\nestjs
16 verbose Windows_NT 10.0.19042
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\USERNAME\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "run" "start"
18 verbose node v14.15.4
19 verbose npm  v6.14.11
20 error code ELIFECYCLE
21 error errno 1
22 error [email protected] start: `nest start`
22 error Exit status 1
23 error Failed at the [email protected] start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

Any idea what is the issue ... ?

Upvotes: 1

Views: 840

Answers (1)

Codebling
Codebling

Reputation: 11382

@nestjs/[email protected] doesn't seem to be working with mongoose version >=5.11.11.

Solution

Downgrade to 5.11.10 and it works.

npm install [email protected]
npm uninstall @types/mongoose

(The uninstall line is for those arriving from Google, you seem to have already removed the @types/mongoose package, which is only intended for Mongoose 5.10 and below)

The Cause

As of Mongoose 5.11, types are included in Mongoose. There are few type differences as a result.

There have been a TON of bugs filed about this since the types were moved over. And even more here and here.

Alternative Solutions

Downgrading Mongoose packages

If you run into more problems, you could also downgrade your mongoose-related packages

npm i @nestjs/[email protected] [email protected] @types/[email protected]

Disable Type Checks for Dependencies

If you really you could skip type checking of your node_modules by adding this line in your tsconfig.json

     "skipLibCheck": true,

Standard warnings apply.

General warning

As a general note, GitHub code with a single commit and 0 stars is not generally considered production-ready. Especially when it is based on a repository with 9 stars, code that it copies and modifies instead of repackaging (making merging fixes more difficult).

That being said, there might be some good work in here.

Upvotes: 3

Related Questions