nest-knexjs 0.0.13 → 0.0.15

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.
Files changed (51) hide show
  1. package/.github/workflows/nodejs-environment.yml +29 -0
  2. package/package.json +30 -27
  3. package/tests/docker-compose.yml +13 -0
  4. package/tests/{user.e2e-spec.ts → e2e/knex-app-user.spec.ts} +23 -39
  5. package/tests/e2e/knex-async-class.spec.ts +35 -0
  6. package/tests/e2e/knex-async-existing.spec.ts +35 -0
  7. package/tests/e2e/knex-async.spec.ts +35 -0
  8. package/tests/e2e/knex.spec.ts +35 -0
  9. package/tests/jest-e2e.json +2 -2
  10. package/tests/src/app-async.module.ts +8 -0
  11. package/tests/src/app.module.ts +28 -0
  12. package/tests/{app-knex → src/apps/app-knex}/app/app.module.ts +2 -2
  13. package/tests/{app-knex → src/apps/app-knex}/app/users/users.service.ts +1 -1
  14. package/tests/{app-knex → src/apps/app-knex}/docker-compose.yml +1 -1
  15. package/tests/{app-multi-database → src/apps/app-multi-database}/app/users/users.service.ts +1 -1
  16. package/tests/src/async-class-options.module.ts +54 -0
  17. package/tests/src/async-existing-options.module.ts +62 -0
  18. package/tests/src/database.module.ts +58 -0
  19. package/tests/app-knex/app/users/users.service.spec.ts +0 -18
  20. package/tests/app-multi-database/app/post/post.service.spec.ts +0 -18
  21. package/tests/app-multi-database/app/users/users.service.spec.ts +0 -18
  22. package/tests/posts.e2e-spec.ts +0 -130
  23. package/tests/users.e2e-spec.ts +0 -128
  24. /package/tests/{app-knex → src/apps/app-knex}/app/main.ts +0 -0
  25. /package/tests/{app-knex → src/apps/app-knex}/app/users/dto/create-user.dto.ts +0 -0
  26. /package/tests/{app-knex → src/apps/app-knex}/app/users/dto/update-user.dto.ts +0 -0
  27. /package/tests/{app-knex → src/apps/app-knex}/app/users/entities/user.entity.ts +0 -0
  28. /package/tests/{app-knex → src/apps/app-knex}/app/users/users.controller.ts +0 -0
  29. /package/tests/{app-knex → src/apps/app-knex}/app/users/users.module.ts +0 -0
  30. /package/tests/{app-knex → src/apps/app-knex}/knexfile.js +0 -0
  31. /package/tests/{app-knex → src/apps/app-knex}/migrations/20210223224919_api.js +0 -0
  32. /package/tests/{app-knex → src/apps/app-knex}/seeds/users.js +0 -0
  33. /package/tests/{app-multi-database → src/apps/app-multi-database}/app/app.module.ts +0 -0
  34. /package/tests/{app-multi-database → src/apps/app-multi-database}/app/main.ts +0 -0
  35. /package/tests/{app-multi-database → src/apps/app-multi-database}/app/post/dto/create-post.dto.ts +0 -0
  36. /package/tests/{app-multi-database → src/apps/app-multi-database}/app/post/dto/update-post.dto.ts +0 -0
  37. /package/tests/{app-multi-database → src/apps/app-multi-database}/app/post/entities/post.entity.ts +0 -0
  38. /package/tests/{app-multi-database → src/apps/app-multi-database}/app/post/post.controller.ts +0 -0
  39. /package/tests/{app-multi-database → src/apps/app-multi-database}/app/post/post.module.ts +0 -0
  40. /package/tests/{app-multi-database → src/apps/app-multi-database}/app/post/post.service.ts +0 -0
  41. /package/tests/{app-multi-database → src/apps/app-multi-database}/app/users/dto/create-user.dto.ts +0 -0
  42. /package/tests/{app-multi-database → src/apps/app-multi-database}/app/users/dto/update-user.dto.ts +0 -0
  43. /package/tests/{app-multi-database → src/apps/app-multi-database}/app/users/entities/user.entity.ts +0 -0
  44. /package/tests/{app-multi-database → src/apps/app-multi-database}/app/users/users.controller.ts +0 -0
  45. /package/tests/{app-multi-database → src/apps/app-multi-database}/app/users/users.module.ts +0 -0
  46. /package/tests/{app-multi-database → src/apps/app-multi-database}/docker-compose.yml +0 -0
  47. /package/tests/{app-multi-database → src/apps/app-multi-database}/knexfile.js +0 -0
  48. /package/tests/{app-multi-database → src/apps/app-multi-database}/migrations/posts/20210902093735_posts.js +0 -0
  49. /package/tests/{app-multi-database → src/apps/app-multi-database}/migrations/users/20210808150144_users.js +0 -0
  50. /package/tests/{app-multi-database → src/apps/app-multi-database}/seeds/posts.js +0 -0
  51. /package/tests/{app-multi-database → src/apps/app-multi-database}/seeds/users.js +0 -0
@@ -23,3 +23,32 @@ jobs:
23
23
  node-version: ${{ matrix.node-version }}
24
24
  - run: npm ci
25
25
  - run: npm run build --if-present
26
+
27
+ integration-test:
28
+
29
+ runs-on: ubuntu-latest
30
+
31
+ strategy:
32
+ matrix:
33
+ node-version: [14.x, 16.x, 18.x]
34
+ needs: [build]
35
+
36
+ steps:
37
+ - uses: actions/checkout@v3
38
+ - name: Use Node.js ${{ matrix.node-version }}
39
+ uses: actions/setup-node@v3
40
+ with:
41
+ node-version: ${{ matrix.node-version }}
42
+ - name: Start Docker-Compose
43
+ run: cd tests && docker-compose up -d
44
+ - name: Install npm
45
+ uses: actions/setup-node@v3
46
+ with:
47
+ node-version: ${{ matrix.node-version }}
48
+ - run: npm install
49
+ - name: Run migrations for test
50
+ run: cd tests/src/apps/app-knex && npx knex migrate:up
51
+ - name: Run tests e2e
52
+ run: npm run test:e2e
53
+ - name: Stop Docker-Compose
54
+ run: cd tests && docker-compose down
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nest-knexjs",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "Knexjs module for Nest framework (node.js) 😻",
5
5
  "author": "Tony133",
6
6
  "license": "MIT",
@@ -12,48 +12,48 @@
12
12
  "publish:npm": "npm publish --access public",
13
13
  "prepublish:next": "npm run build",
14
14
  "publish:next": "npm publish --access public --tag next",
15
- "test:e2e": "jest --config ./tests/jest-e2e.json --runInBand",
15
+ "test:e2e": "jest --config ./tests/jest-e2e.json --detectOpenHandles --forceExit",
16
16
  "test:e2e:dev": "jest --config ./tests/jest-e2e.json --runInBand --watch",
17
17
  "prerelease": "npm run build",
18
18
  "release": "release-it",
19
19
  "prepare": "husky install"
20
20
  },
21
21
  "devDependencies": {
22
- "@commitlint/cli": "17.4.0",
23
- "@commitlint/config-angular": "17.4.0",
24
- "@nestjs/common": "9.2.1",
25
- "@nestjs/core": "9.2.1",
26
- "@nestjs/platform-express": "9.2.1",
27
- "@nestjs/testing": "9.2.1",
28
- "@types/express": "4.17.15",
29
- "@types/node": "18.11.18",
30
- "@types/jest": "29.2.5",
22
+ "@commitlint/cli": "17.5.1",
23
+ "@commitlint/config-angular": "17.4.4",
24
+ "@nestjs/common": "9.3.12",
25
+ "@nestjs/core": "9.3.12",
26
+ "@nestjs/platform-express": "9.3.12",
27
+ "@nestjs/testing": "9.3.12",
28
+ "@types/express": "4.17.17",
29
+ "@types/jest": "29.5.0",
30
+ "@types/node": "18.15.11",
31
31
  "@types/supertest": "2.0.12",
32
- "@typescript-eslint/eslint-plugin": "5.48.0",
33
- "@typescript-eslint/parser": "5.48.0",
34
- "eslint": "8.31.0",
35
- "eslint-config-prettier": "8.6.0",
36
- "eslint-plugin-import": "2.26.0",
37
- "knex": "2.4.0",
38
- "mysql": "2.18.1",
32
+ "@typescript-eslint/eslint-plugin": "5.57.0",
33
+ "@typescript-eslint/parser": "5.57.0",
34
+ "eslint": "8.37.0",
35
+ "eslint-config-prettier": "8.8.0",
36
+ "eslint-plugin-import": "2.27.5",
39
37
  "husky": "8.0.3",
40
- "prettier": "2.8.1",
41
- "jest": "29.3.1",
38
+ "jest": "29.5.0",
39
+ "knex": "2.4.2",
40
+ "mysql": "2.18.1",
41
+ "prettier": "2.8.7",
42
42
  "reflect-metadata": "0.1.13",
43
- "release-it": "15.6.0",
43
+ "release-it": "15.9.3",
44
+ "rxjs": "7.8.0",
44
45
  "supertest": "6.3.3",
46
+ "ts-jest": "29.0.5",
45
47
  "ts-node": "10.9.1",
46
- "rxjs": "7.8.0",
47
- "ts-jest": "29.0.3",
48
- "tsconfig-paths": "4.1.2",
49
- "typescript": "4.9.4"
48
+ "tsconfig-paths": "4.2.0",
49
+ "typescript": "5.0.3"
50
50
  },
51
51
  "peerDependencies": {
52
52
  "@nestjs/common": "^7.0.0 || ^8.0.0 || ^9.0.0",
53
53
  "@nestjs/core": "^7.0.0 || ^8.0.0 || ^9.0.0",
54
+ "knex": "^0.21.17 || ^0.95.0 || ^1.0.0 || ^2.0.0",
54
55
  "reflect-metadata": "^0.1.13",
55
- "rxjs": "^6.6.3 || ^7.2.0",
56
- "knex": "^0.21.17 || ^0.95.0 || ^1.0.0 || ^2.0.0"
56
+ "rxjs": "^6.6.3 || ^7.2.0"
57
57
  },
58
58
  "lint-staged": {
59
59
  "**/*.{ts,json}": []
@@ -61,5 +61,8 @@
61
61
  "repository": {
62
62
  "type": "git",
63
63
  "url": "https://github.com/tony133/nestjs-knexjs"
64
+ },
65
+ "dependencies": {
66
+ "uuid": "9.0.0"
64
67
  }
65
68
  }
@@ -0,0 +1,13 @@
1
+ version: "3"
2
+
3
+ services:
4
+ mysql:
5
+ image: mysql:5
6
+ environment:
7
+ MYSQL_ROOT_USER: root
8
+ MYSQL_ROOT_PASSWORD: root
9
+ MYSQL_DATABASE: test
10
+ ports:
11
+ - "3306:3306"
12
+ restart: always
13
+
@@ -1,10 +1,10 @@
1
1
  import { INestApplication, HttpStatus } from '@nestjs/common';
2
2
  import { TestingModule, Test } from '@nestjs/testing';
3
- import { UsersModule } from './app-knex/app/users/users.module';
4
- import { KnexModule } from '../lib';
3
+ import { UsersModule } from '../src/apps/app-knex/app/users/users.module';
4
+ import { KnexModule } from '../../lib';
5
5
  import * as request from 'supertest';
6
6
 
7
- describe('[Feature] User - /user', () => {
7
+ describe('[Feature] Users - /users', () => {
8
8
  let app: INestApplication;
9
9
 
10
10
  beforeAll(async () => {
@@ -19,7 +19,7 @@ describe('[Feature] User - /user', () => {
19
19
  connection: {
20
20
  host: '127.0.0.1',
21
21
  user: 'root',
22
- port: 3308,
22
+ port: 3306,
23
23
  password: 'root',
24
24
  database: 'test',
25
25
  },
@@ -32,44 +32,29 @@ describe('[Feature] User - /user', () => {
32
32
  await app.init();
33
33
  });
34
34
 
35
- it('Create [POST /]', () => {
35
+ it('should create a new user [POST /users]', () => {
36
36
  return request(app.getHttpServer())
37
- .post('/user')
37
+ .post('/users')
38
38
  .expect(HttpStatus.CREATED)
39
39
  .set('Accept', 'application/json')
40
40
  .send({
41
41
  firstName: 'firstName',
42
42
  lastName: 'lastName',
43
43
  })
44
- .then((res) => {
45
- res.body;
44
+ .then(({ body }) => {
45
+ expect(body).toEqual({ users: [1] });
46
46
  });
47
47
  });
48
48
 
49
- it('Get all [GET /]', () => {
49
+ it('should get all users [GET /users]', () => {
50
50
  return request(app.getHttpServer())
51
- .get('/user')
51
+ .get('/users')
52
52
  .expect(HttpStatus.OK)
53
53
  .set('Accept', 'application/json')
54
54
  .then(({ body }) => {
55
55
  expect(body['users']).toEqual([
56
56
  {
57
57
  id: 1,
58
- firstName: 'firstName #1',
59
- lastName: 'lastName #1',
60
- },
61
- {
62
- id: 2,
63
- firstName: 'firstName #1',
64
- lastName: 'lastName #1',
65
- },
66
- {
67
- id: 3,
68
- firstName: 'firstName #1',
69
- lastName: 'lastName #1',
70
- },
71
- {
72
- id: 4,
73
58
  firstName: 'firstName',
74
59
  lastName: 'lastName',
75
60
  },
@@ -77,39 +62,38 @@ describe('[Feature] User - /user', () => {
77
62
  });
78
63
  });
79
64
 
80
- it('Get one [GET /:id]', () => {
65
+ it('should get a user by id [GET /users/:id]', () => {
81
66
  return request(app.getHttpServer())
82
- .get('/user/2')
67
+ .get('/users/1')
83
68
  .expect(HttpStatus.OK)
84
69
  .set('Accept', 'application/json')
85
70
  .then(({ body }) => {
86
71
  expect(body['users']).toEqual([
87
72
  {
88
- id: 2,
89
- firstName: 'firstName #1',
90
- lastName: 'lastName #1',
73
+ id: 1,
74
+ firstName: 'firstName',
75
+ lastName: 'lastName',
91
76
  },
92
77
  ]);
93
78
  });
94
79
  });
95
80
 
96
- it('Update one [PUT /:id]', () => {
81
+ it('should update a user by id [PUT /users/:id]', () => {
97
82
  return request(app.getHttpServer())
98
- .put('/user/2')
83
+ .put('/users/1')
99
84
  .expect(HttpStatus.OK)
100
85
  .send({
101
- firstName: 'firstName update',
102
- lastName: 'lastName update',
86
+ firstName: 'firstName',
87
+ lastName: 'lastName',
103
88
  })
104
- .then((res) => {
105
- res.body;
89
+ .then(({ body }) => {
90
+ expect(body).toEqual({ users: 1 });
106
91
  });
107
92
  });
108
93
 
109
- it('Delete one [DELETE /:id]', () => {
94
+ it('should delete a user by id [DELETE /users/:id]', () => {
110
95
  return request(app.getHttpServer())
111
- .delete('/user/1')
112
- .set('Accept', 'application/json')
96
+ .delete('/users/1')
113
97
  .expect(HttpStatus.OK);
114
98
  });
115
99
 
@@ -0,0 +1,35 @@
1
+ import { HttpStatus, INestApplication } from '@nestjs/common';
2
+ import { Test } from '@nestjs/testing';
3
+ import { AsyncOptionsClassModule } from '../src/async-class-options.module';
4
+ import * as request from 'supertest';
5
+
6
+ describe('Knex (async configuration)', () => {
7
+ let app: INestApplication;
8
+
9
+ beforeAll(async () => {
10
+ const module = await Test.createTestingModule({
11
+ imports: [AsyncOptionsClassModule],
12
+ }).compile();
13
+
14
+ app = module.createNestApplication();
15
+ await app.init();
16
+ });
17
+
18
+ it(`should return created entity`, () => {
19
+ return request(app.getHttpServer())
20
+ .post('/users')
21
+ .expect(HttpStatus.CREATED)
22
+ .set('Accept', 'application/json')
23
+ .send({
24
+ firstName: 'firstName',
25
+ lastName: 'lastName',
26
+ })
27
+ .expect(({ body }) => {
28
+ expect(body).toEqual({ users: [body.users[0]] });
29
+ });
30
+ });
31
+
32
+ afterAll(async () => {
33
+ await app.close();
34
+ });
35
+ });
@@ -0,0 +1,35 @@
1
+ import { HttpStatus, INestApplication } from '@nestjs/common';
2
+ import { Test } from '@nestjs/testing';
3
+ import { AsyncOptionsExistingModule } from '../src/async-existing-options.module';
4
+ import * as request from 'supertest';
5
+
6
+ describe('Knex (async configuration)', () => {
7
+ let app: INestApplication;
8
+
9
+ beforeAll(async () => {
10
+ const module = await Test.createTestingModule({
11
+ imports: [AsyncOptionsExistingModule],
12
+ }).compile();
13
+
14
+ app = module.createNestApplication();
15
+ await app.init();
16
+ });
17
+
18
+ it(`should return created entity`, () => {
19
+ return request(app.getHttpServer())
20
+ .post('/users')
21
+ .expect(HttpStatus.CREATED)
22
+ .set('Accept', 'application/json')
23
+ .send({
24
+ firstName: 'firstName',
25
+ lastName: 'lastName',
26
+ })
27
+ .expect(({ body }) => {
28
+ expect(body).toEqual({ users: [body.users[0]] });
29
+ });
30
+ });
31
+
32
+ afterAll(async () => {
33
+ await app.close();
34
+ });
35
+ });
@@ -0,0 +1,35 @@
1
+ import { HttpStatus, INestApplication } from '@nestjs/common';
2
+ import { Test } from '@nestjs/testing';
3
+ import { AsyncApplicationModule } from '../src/app-async.module';
4
+ import * as request from 'supertest';
5
+
6
+ describe('Knex (async configuration)', () => {
7
+ let app: INestApplication;
8
+
9
+ beforeAll(async () => {
10
+ const module = await Test.createTestingModule({
11
+ imports: [AsyncApplicationModule],
12
+ }).compile();
13
+
14
+ app = module.createNestApplication();
15
+ await app.init();
16
+ });
17
+
18
+ it(`should return created entity`, () => {
19
+ return request(app.getHttpServer())
20
+ .post('/users')
21
+ .expect(HttpStatus.CREATED)
22
+ .set('Accept', 'application/json')
23
+ .send({
24
+ firstName: 'firstName',
25
+ lastName: 'lastName',
26
+ })
27
+ .expect(({ body }) => {
28
+ expect(body).toEqual({ users: [body.users[0]] });
29
+ });
30
+ });
31
+
32
+ afterAll(async () => {
33
+ await app.close();
34
+ });
35
+ });
@@ -0,0 +1,35 @@
1
+ import { INestApplication } from '@nestjs/common';
2
+ import { Test } from '@nestjs/testing';
3
+ import { ApplicationModule } from '../src/app.module';
4
+ import * as request from 'supertest';
5
+
6
+ describe('Knex', () => {
7
+ let app: INestApplication;
8
+
9
+ beforeAll(async () => {
10
+ const module = await Test.createTestingModule({
11
+ imports: [ApplicationModule],
12
+ }).compile();
13
+
14
+ app = module.createNestApplication();
15
+ await app.init();
16
+ });
17
+
18
+ it(`should return created entity`, () => {
19
+ return request(app.getHttpServer())
20
+ .post('/users')
21
+ .expect(201)
22
+ .set('Accept', 'application/json')
23
+ .send({
24
+ firstName: 'firstName',
25
+ lastName: 'lastName',
26
+ })
27
+ .expect(({ body }) => {
28
+ expect(body).toEqual({ users: [body.users[0]] });
29
+ });
30
+ });
31
+
32
+ afterAll(async () => {
33
+ await app.close();
34
+ });
35
+ });
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "moduleFileExtensions": ["js", "json", "ts"],
3
- "rootDir": "..",
3
+ "rootDir": ".",
4
4
  "testEnvironment": "node",
5
- "testRegex": ".e2e-spec.ts$",
5
+ "testRegex": ".spec.ts$",
6
6
  "transform": {
7
7
  "^.+\\.(t|j)s$": "ts-jest"
8
8
  }
@@ -0,0 +1,8 @@
1
+ import { Module } from '@nestjs/common';
2
+ import { AppModule } from './apps/app-knex/app/app.module';
3
+ import { DatabaseModule } from './database.module';
4
+
5
+ @Module({
6
+ imports: [DatabaseModule.forRoot(), AppModule],
7
+ })
8
+ export class AsyncApplicationModule {}
@@ -0,0 +1,28 @@
1
+ import { Module } from '@nestjs/common';
2
+ import { KnexModule } from '../../lib';
3
+ import { UsersModule } from './apps/app-knex/app/users/users.module';
4
+
5
+ @Module({
6
+ imports: [
7
+ KnexModule.forRoot({
8
+ config: {
9
+ client: 'mysql',
10
+ version: '5.7',
11
+ useNullAsDefault: true,
12
+ connection: {
13
+ host: '127.0.0.1',
14
+ user: 'root',
15
+ port: 3306,
16
+ password: 'root',
17
+ database: 'test',
18
+ },
19
+ pool: {
20
+ min: 2,
21
+ max: 10,
22
+ },
23
+ },
24
+ }),
25
+ UsersModule,
26
+ ],
27
+ })
28
+ export class ApplicationModule {}
@@ -1,5 +1,5 @@
1
1
  import { Module } from '@nestjs/common';
2
- import { KnexModule } from '../../../lib';
2
+ import { KnexModule } from '../../../../../lib';
3
3
  import { UsersModule } from './users/users.module';
4
4
 
5
5
  @Module({
@@ -11,7 +11,7 @@ import { UsersModule } from './users/users.module';
11
11
  useNullAsDefault: true,
12
12
  connection: {
13
13
  host: '127.0.0.1',
14
- port: 3308,
14
+ port: 3306,
15
15
  user: 'root',
16
16
  password: 'root',
17
17
  database: 'test',
@@ -5,7 +5,7 @@ import {
5
5
  NotFoundException,
6
6
  } from '@nestjs/common';
7
7
  import { Knex } from 'knex';
8
- import { InjectModel } from '../../../../lib';
8
+ import { InjectModel } from '../../../../../../lib';
9
9
  import { CreateUserDto } from './dto/create-user.dto';
10
10
  import { UpdateUserDto } from './dto/update-user.dto';
11
11
 
@@ -9,4 +9,4 @@ services:
9
9
  MYSQL_ROOT_PASSWORD: root
10
10
  MYSQL_DATABASE: test
11
11
  ports:
12
- - "3308:3306"
12
+ - "3306:3306"
@@ -5,7 +5,7 @@ import {
5
5
  NotFoundException,
6
6
  } from '@nestjs/common';
7
7
  import { Knex } from 'knex';
8
- import { InjectConnection } from '../../../../lib';
8
+ import { InjectConnection } from '../../../../../../lib';
9
9
  import { CreateUserDto } from './dto/create-user.dto';
10
10
  import { UpdateUserDto } from './dto/update-user.dto';
11
11
 
@@ -0,0 +1,54 @@
1
+ import { Module } from '@nestjs/common';
2
+ import { KnexOptionsFactory, KnexModuleOptions, KnexModule } from '../../lib';
3
+ import { UsersModule } from './apps/app-knex/app/users/users.module';
4
+
5
+ class ConfigService implements KnexOptionsFactory {
6
+ createKnexOptions(): KnexModuleOptions {
7
+ return {
8
+ config: {
9
+ client: 'mysql',
10
+ version: '5.7',
11
+ useNullAsDefault: true,
12
+ connection: {
13
+ host: '127.0.0.1',
14
+ user: 'root',
15
+ port: 3306,
16
+ password: 'root',
17
+ database: 'test',
18
+ },
19
+ pool: {
20
+ min: 2,
21
+ max: 10,
22
+ },
23
+ },
24
+ };
25
+ }
26
+ }
27
+
28
+ @Module({
29
+ imports: [
30
+ KnexModule.forRootAsync({
31
+ useClass: ConfigService,
32
+ }),
33
+ KnexModule.forRoot({
34
+ config: {
35
+ client: 'mysql',
36
+ version: '5.7',
37
+ useNullAsDefault: true,
38
+ connection: {
39
+ host: '127.0.0.1',
40
+ user: 'root',
41
+ port: 3306,
42
+ password: 'root',
43
+ database: 'test',
44
+ },
45
+ pool: {
46
+ min: 2,
47
+ max: 10,
48
+ },
49
+ },
50
+ }),
51
+ UsersModule,
52
+ ],
53
+ })
54
+ export class AsyncOptionsClassModule {}
@@ -0,0 +1,62 @@
1
+ import { Module } from '@nestjs/common';
2
+ import { UsersModule } from '../src/apps/app-knex/app/users/users.module';
3
+ import { KnexModule } from '../../lib/knex.module';
4
+ import { KnexModuleOptions, KnexOptionsFactory } from '../../lib';
5
+
6
+ class ConfigService implements KnexOptionsFactory {
7
+ createKnexOptions(): KnexModuleOptions {
8
+ return {
9
+ config: {
10
+ client: 'mysql',
11
+ version: '5.7',
12
+ useNullAsDefault: true,
13
+ connection: {
14
+ host: '127.0.0.1',
15
+ user: 'root',
16
+ port: 3306,
17
+ password: 'root',
18
+ database: 'test',
19
+ },
20
+ pool: {
21
+ min: 2,
22
+ max: 10,
23
+ },
24
+ },
25
+ };
26
+ }
27
+ }
28
+
29
+ @Module({
30
+ providers: [ConfigService],
31
+ exports: [ConfigService],
32
+ })
33
+ class ConfigModule {}
34
+
35
+ @Module({
36
+ imports: [
37
+ KnexModule.forRootAsync({
38
+ imports: [ConfigModule],
39
+ useExisting: ConfigService,
40
+ }),
41
+ KnexModule.forRoot({
42
+ config: {
43
+ client: 'mysql',
44
+ version: '5.7',
45
+ useNullAsDefault: true,
46
+ connection: {
47
+ host: '127.0.0.1',
48
+ user: 'root',
49
+ port: 3306,
50
+ password: 'root',
51
+ database: 'test',
52
+ },
53
+ pool: {
54
+ min: 2,
55
+ max: 10,
56
+ },
57
+ },
58
+ }),
59
+ UsersModule,
60
+ ],
61
+ })
62
+ export class AsyncOptionsExistingModule {}
@@ -0,0 +1,58 @@
1
+ import { DynamicModule, Module } from '@nestjs/common';
2
+ import { KnexModule } from '../../lib';
3
+
4
+ @Module({})
5
+ export class DatabaseModule {
6
+ static async forRoot(): Promise<DynamicModule> {
7
+ await new Promise((resolve) => setTimeout(resolve, 1000));
8
+ return {
9
+ module: DatabaseModule,
10
+ imports: [
11
+ KnexModule.forRoot(
12
+ {
13
+ name: 'db1Connection',
14
+ config: {
15
+ client: 'mysql',
16
+ version: '5.7',
17
+ useNullAsDefault: true,
18
+ connection: {
19
+ host: '127.0.0.1',
20
+ user: 'root',
21
+ port: 3306,
22
+ password: 'root',
23
+ database: 'test',
24
+ },
25
+ pool: {
26
+ min: 2,
27
+ max: 10,
28
+ },
29
+ },
30
+ },
31
+ 'db1Connection',
32
+ ),
33
+ KnexModule.forRoot(
34
+ {
35
+ name: 'db2Connection',
36
+ config: {
37
+ client: 'mysql',
38
+ version: '5.7',
39
+ useNullAsDefault: true,
40
+ connection: {
41
+ host: '127.0.0.1',
42
+ user: 'root',
43
+ port: 3306,
44
+ password: 'root',
45
+ database: 'test',
46
+ },
47
+ pool: {
48
+ min: 2,
49
+ max: 10,
50
+ },
51
+ },
52
+ },
53
+ 'db2Connection',
54
+ ),
55
+ ],
56
+ };
57
+ }
58
+ }
@@ -1,18 +0,0 @@
1
- import { Test, TestingModule } from '@nestjs/testing';
2
- import { UsersService } from './users.service';
3
-
4
- describe('UsersService', () => {
5
- let service: UsersService;
6
-
7
- beforeEach(async () => {
8
- const module: TestingModule = await Test.createTestingModule({
9
- providers: [UsersService],
10
- }).compile();
11
-
12
- service = module.get<UsersService>(UsersService);
13
- });
14
-
15
- it('should be defined', () => {
16
- expect(service).toBeDefined();
17
- });
18
- });
@@ -1,18 +0,0 @@
1
- import { Test, TestingModule } from '@nestjs/testing';
2
- import { PostService } from './post.service';
3
-
4
- describe('PostService', () => {
5
- let service: PostService;
6
-
7
- beforeEach(async () => {
8
- const module: TestingModule = await Test.createTestingModule({
9
- providers: [PostService],
10
- }).compile();
11
-
12
- service = module.get<PostService>(PostService);
13
- });
14
-
15
- it('should be defined', () => {
16
- expect(service).toBeDefined();
17
- });
18
- });
@@ -1,18 +0,0 @@
1
- import { Test, TestingModule } from '@nestjs/testing';
2
- import { UsersService } from './users.service';
3
-
4
- describe('UsersService', () => {
5
- let service: UsersService;
6
-
7
- beforeEach(async () => {
8
- const module: TestingModule = await Test.createTestingModule({
9
- providers: [UsersService],
10
- }).compile();
11
-
12
- service = module.get<UsersService>(UsersService);
13
- });
14
-
15
- it('should be defined', () => {
16
- expect(service).toBeDefined();
17
- });
18
- });
@@ -1,130 +0,0 @@
1
- import { INestApplication, HttpStatus } from '@nestjs/common';
2
- import { TestingModule, Test } from '@nestjs/testing';
3
- import { UsersModule } from './app-multi-database/app/users/users.module';
4
- import { KnexModule } from '../lib';
5
- import * as request from 'supertest';
6
- import { PostModule } from './app-multi-database/app/post/post.module';
7
-
8
- describe('[Feature] Posts - /posts', () => {
9
- let app: INestApplication;
10
-
11
- beforeAll(async () => {
12
- const moduleFixture: TestingModule = await Test.createTestingModule({
13
- imports: [
14
- KnexModule.forRootAsync(
15
- {
16
- useFactory: () => ({
17
- config: {
18
- client: 'mysql',
19
- version: '5.7',
20
- useNullAsDefault: true,
21
- connection: {
22
- host: '127.0.0.1',
23
- user: 'root',
24
- port: 3307,
25
- password: 'root',
26
- database: 'test2',
27
- },
28
- pool: {
29
- min: 2,
30
- max: 10,
31
- },
32
- },
33
- }),
34
- },
35
- 'db2Connection',
36
- ),
37
- UsersModule,
38
- PostModule,
39
- ],
40
- }).compile();
41
-
42
- app = moduleFixture.createNestApplication();
43
- await app.init();
44
- });
45
-
46
- it('Create [POST /]', () => {
47
- return request(app.getHttpServer())
48
- .post('/posts')
49
- .expect(HttpStatus.CREATED)
50
- .set('Accept', 'application/json')
51
- .send({
52
- title: 'title #1',
53
- description: 'description #1',
54
- })
55
- .then((res) => {
56
- res.body;
57
- });
58
- });
59
-
60
- it('Get all [GET /]', () => {
61
- return request(app.getHttpServer())
62
- .get('/posts')
63
- .expect(HttpStatus.OK)
64
- .set('Accept', 'application/json')
65
- .then(({ body }) => {
66
- expect(body['users']).toEqual([
67
- {
68
- id: 1,
69
- title: 'title #1',
70
- description: 'description #1',
71
- },
72
- {
73
- id: 2,
74
- title: 'title #1',
75
- description: 'description #1',
76
- },
77
- {
78
- id: 3,
79
- title: 'title #1',
80
- description: 'description #1',
81
- },
82
- {
83
- id: 4,
84
- title: 'title #1',
85
- description: 'description #1',
86
- },
87
- ]);
88
- });
89
- });
90
-
91
- it('Get one [GET /:id]', () => {
92
- return request(app.getHttpServer())
93
- .get('/posts/2')
94
- .expect(HttpStatus.OK)
95
- .set('Accept', 'application/json')
96
- .then(({ body }) => {
97
- expect(body['users']).toEqual([
98
- {
99
- id: 2,
100
- title: 'title #1',
101
- description: 'description #1',
102
- },
103
- ]);
104
- });
105
- });
106
-
107
- it('Update one [PUT /:id]', () => {
108
- return request(app.getHttpServer())
109
- .put('/posts/2')
110
- .expect(HttpStatus.OK)
111
- .send({
112
- title: 'title update',
113
- description: 'description update',
114
- })
115
- .then((res) => {
116
- res.body;
117
- });
118
- });
119
-
120
- it('Delete one [DELETE /:id]', () => {
121
- return request(app.getHttpServer())
122
- .delete('/posts/1')
123
- .set('Accept', 'application/json')
124
- .expect(HttpStatus.OK);
125
- });
126
-
127
- afterAll(async () => {
128
- await app.close();
129
- });
130
- });
@@ -1,128 +0,0 @@
1
- import { INestApplication, HttpStatus } from '@nestjs/common';
2
- import { TestingModule, Test } from '@nestjs/testing';
3
- import { UsersModule } from './app-multi-database/app/users/users.module';
4
- import { KnexModule } from '../lib';
5
- import * as request from 'supertest';
6
-
7
- describe('[Feature] Users - /users', () => {
8
- let app: INestApplication;
9
-
10
- beforeAll(async () => {
11
- const moduleFixture: TestingModule = await Test.createTestingModule({
12
- imports: [
13
- UsersModule,
14
- KnexModule.forRootAsync(
15
- {
16
- useFactory: () => ({
17
- config: {
18
- client: 'mysql',
19
- version: '5.7',
20
- useNullAsDefault: true,
21
- connection: {
22
- host: '127.0.0.1',
23
- user: 'root',
24
- port: 3306,
25
- password: 'root',
26
- database: 'test1',
27
- },
28
- pool: {
29
- min: 2,
30
- max: 10,
31
- },
32
- },
33
- }),
34
- },
35
- 'db1Connection',
36
- ),
37
- ],
38
- }).compile();
39
-
40
- app = moduleFixture.createNestApplication();
41
- await app.init();
42
- });
43
-
44
- it('Create [POST /]', () => {
45
- return request(app.getHttpServer())
46
- .post('/users')
47
- .expect(HttpStatus.CREATED)
48
- .set('Accept', 'application/json')
49
- .send({
50
- firstName: 'firstName',
51
- lastName: 'lastName',
52
- })
53
- .then((res) => {
54
- res.body;
55
- });
56
- });
57
-
58
- it('Get all [GET /]', () => {
59
- return request(app.getHttpServer())
60
- .get('/users')
61
- .expect(HttpStatus.OK)
62
- .set('Accept', 'application/json')
63
- .then(({ body }) => {
64
- expect(body['users']).toEqual([
65
- {
66
- id: 1,
67
- firstName: 'firstName #1',
68
- lastName: 'lastName #1',
69
- },
70
- {
71
- id: 2,
72
- firstName: 'firstName #1',
73
- lastName: 'lastName #1',
74
- },
75
- {
76
- id: 3,
77
- firstName: 'firstName #1',
78
- lastName: 'lastName #1',
79
- },
80
- {
81
- id: 4,
82
- firstName: 'firstName',
83
- lastName: 'lastName',
84
- },
85
- ]);
86
- });
87
- });
88
-
89
- it('Get one [GET /:id]', () => {
90
- return request(app.getHttpServer())
91
- .get('/users/2')
92
- .expect(HttpStatus.OK)
93
- .set('Accept', 'application/json')
94
- .then(({ body }) => {
95
- expect(body['users']).toEqual([
96
- {
97
- id: 2,
98
- firstName: 'firstName #1',
99
- lastName: 'lastName #1',
100
- },
101
- ]);
102
- });
103
- });
104
-
105
- it('Update one [PUT /:id]', () => {
106
- return request(app.getHttpServer())
107
- .put('/users/2')
108
- .expect(HttpStatus.OK)
109
- .send({
110
- firstName: 'firstName update',
111
- lastName: 'lastName update',
112
- })
113
- .then((res) => {
114
- res.body;
115
- });
116
- });
117
-
118
- it('Delete one [DELETE /:id]', () => {
119
- return request(app.getHttpServer())
120
- .delete('/users/1')
121
- .set('Accept', 'application/json')
122
- .expect(HttpStatus.OK);
123
- });
124
-
125
- afterAll(async () => {
126
- await app.close();
127
- });
128
- });