nest-knexjs 0.0.3 → 0.0.7

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 (45) hide show
  1. package/.github/workflows/codeql-analysis.yml +31 -0
  2. package/LICENSE +1 -1
  3. package/README.md +178 -8
  4. package/package.json +29 -29
  5. package/tests/{app → app-knex/app}/app.module.ts +0 -0
  6. package/tests/{app → app-knex/app}/main.ts +0 -0
  7. package/tests/app-knex/app/users/dto/create-user.dto.ts +4 -0
  8. package/tests/app-knex/app/users/dto/update-user.dto.ts +4 -0
  9. package/tests/app-knex/app/users/entities/user.entity.ts +5 -0
  10. package/tests/{app → app-knex/app}/users/users.controller.ts +0 -0
  11. package/tests/{app → app-knex/app}/users/users.module.ts +0 -0
  12. package/tests/{app → app-knex/app}/users/users.service.spec.ts +0 -0
  13. package/tests/{app → app-knex/app}/users/users.service.ts +13 -13
  14. package/tests/{docker-compose.yml → app-knex/docker-compose.yml} +1 -1
  15. package/tests/{knexfile.js → app-knex/knexfile.js} +0 -0
  16. package/tests/{migrations → app-knex/migrations}/20210223224919_api.js +2 -2
  17. package/tests/{seeds → app-knex/seeds}/users.js +6 -6
  18. package/tests/app-multi-database/app/app.module.ts +61 -0
  19. package/tests/app-multi-database/app/main.ts +9 -0
  20. package/tests/app-multi-database/app/post/dto/create-post.dto.ts +4 -0
  21. package/tests/app-multi-database/app/post/dto/update-post.dto.ts +4 -0
  22. package/tests/app-multi-database/app/post/entities/post.entity.ts +5 -0
  23. package/tests/app-multi-database/app/post/post.controller.ts +42 -0
  24. package/tests/app-multi-database/app/post/post.module.ts +10 -0
  25. package/tests/app-multi-database/app/post/post.service.spec.ts +18 -0
  26. package/tests/app-multi-database/app/post/post.service.ts +64 -0
  27. package/tests/app-multi-database/app/users/dto/create-user.dto.ts +4 -0
  28. package/tests/app-multi-database/app/users/dto/update-user.dto.ts +4 -0
  29. package/tests/app-multi-database/app/users/entities/user.entity.ts +5 -0
  30. package/tests/app-multi-database/app/users/users.controller.ts +42 -0
  31. package/tests/app-multi-database/app/users/users.module.ts +9 -0
  32. package/tests/app-multi-database/app/users/users.service.spec.ts +18 -0
  33. package/tests/app-multi-database/app/users/users.service.ts +71 -0
  34. package/tests/app-multi-database/docker-compose.yml +21 -0
  35. package/tests/app-multi-database/knexfile.js +34 -0
  36. package/tests/app-multi-database/migrations/posts/20210902093735_posts.js +11 -0
  37. package/tests/app-multi-database/migrations/users/20210808150144_users.js +11 -0
  38. package/tests/app-multi-database/seeds/posts.js +24 -0
  39. package/tests/app-multi-database/seeds/users.js +23 -0
  40. package/tests/posts.e2e-spec.ts +130 -0
  41. package/tests/user.e2e-spec.ts +119 -0
  42. package/tests/users.e2e-spec.ts +79 -35
  43. package/tests/app/users/dto/create-user.dto.ts +0 -4
  44. package/tests/app/users/dto/update-user.dto.ts +0 -4
  45. package/tests/app/users/entities/user.entity.ts +0 -5
@@ -0,0 +1,64 @@
1
+ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
2
+ import { CreatePostDto } from './dto/create-post.dto';
3
+ import { UpdatePostDto } from './dto/update-post.dto';
4
+ import { NotFoundException } from '@nestjs/common';
5
+ import { Knex } from 'knex';
6
+ import { InjectConnection } from '../../../../lib';
7
+
8
+ @Injectable()
9
+ export class PostService {
10
+ constructor(
11
+ @InjectConnection('db2Connection')
12
+ private knexConnection: Knex,
13
+ ) {}
14
+
15
+ async create(createPostDto: CreatePostDto) {
16
+ try {
17
+ const post = await this.knexConnection.table('posts').insert({
18
+ title: createPostDto.title,
19
+ description: createPostDto.description,
20
+ });
21
+
22
+ return post;
23
+ } catch (err) {
24
+ throw new HttpException(err, HttpStatus.BAD_REQUEST);
25
+ }
26
+ }
27
+
28
+ async findAll() {
29
+ const posts = await this.knexConnection.table('posts');
30
+ return { posts };
31
+ }
32
+
33
+ async findOne(id: number) {
34
+ if (!id) {
35
+ throw new NotFoundException('Post ID does not exist');
36
+ }
37
+ const posts = await this.knexConnection.table('posts').where('id', id);
38
+ return { posts };
39
+ }
40
+
41
+ async update(id: number, updatePostDto: UpdatePostDto) {
42
+ try {
43
+ const posts = await this.knexConnection
44
+ .table('posts')
45
+ .where('id', id)
46
+ .update({
47
+ title: updatePostDto.title,
48
+ description: updatePostDto.description,
49
+ });
50
+
51
+ return { posts };
52
+ } catch (err) {
53
+ throw new HttpException(err, HttpStatus.BAD_REQUEST);
54
+ }
55
+ }
56
+
57
+ async remove(id: number) {
58
+ if (!id) {
59
+ throw new NotFoundException('Post ID does not exist');
60
+ }
61
+ const post = await this.knexConnection.table('posts').where('id', id).del();
62
+ return { post };
63
+ }
64
+ }
@@ -0,0 +1,4 @@
1
+ export class CreateUserDto {
2
+ firstName: string;
3
+ lastName: string;
4
+ }
@@ -0,0 +1,4 @@
1
+ export class UpdateUserDto {
2
+ firstName: string;
3
+ lastName: string;
4
+ }
@@ -0,0 +1,5 @@
1
+ export class User {
2
+ id: number;
3
+ firstName: string;
4
+ lastName: string;
5
+ }
@@ -0,0 +1,42 @@
1
+ import {
2
+ Controller,
3
+ Get,
4
+ Post,
5
+ Body,
6
+ Put,
7
+ Param,
8
+ Delete,
9
+ } from '@nestjs/common';
10
+ import { UsersService } from './users.service';
11
+ import { CreateUserDto } from './dto/create-user.dto';
12
+ import { UpdateUserDto } from './dto/update-user.dto';
13
+
14
+ @Controller('users')
15
+ export class UsersController {
16
+ constructor(private readonly usersService: UsersService) {}
17
+
18
+ @Post()
19
+ create(@Body() createUserDto: CreateUserDto) {
20
+ return this.usersService.create(createUserDto);
21
+ }
22
+
23
+ @Get()
24
+ findAll() {
25
+ return this.usersService.findAll();
26
+ }
27
+
28
+ @Get(':id')
29
+ findOne(@Param('id') id: string) {
30
+ return this.usersService.findOne(+id);
31
+ }
32
+
33
+ @Put(':id')
34
+ update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
35
+ return this.usersService.update(+id, updateUserDto);
36
+ }
37
+
38
+ @Delete(':id')
39
+ remove(@Param('id') id: string) {
40
+ return this.usersService.remove(+id);
41
+ }
42
+ }
@@ -0,0 +1,9 @@
1
+ import { Module } from '@nestjs/common';
2
+ import { UsersService } from './users.service';
3
+ import { UsersController } from './users.controller';
4
+
5
+ @Module({
6
+ controllers: [UsersController],
7
+ providers: [UsersService]
8
+ })
9
+ export class UsersModule {}
@@ -0,0 +1,18 @@
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
+ });
@@ -0,0 +1,71 @@
1
+ import {
2
+ HttpException,
3
+ HttpStatus,
4
+ Injectable,
5
+ NotFoundException,
6
+ } from '@nestjs/common';
7
+ import { Knex } from 'knex';
8
+ import { InjectConnection } from '../../../../lib';
9
+ import { CreateUserDto } from './dto/create-user.dto';
10
+ import { UpdateUserDto } from './dto/update-user.dto';
11
+
12
+ @Injectable()
13
+ export class UsersService {
14
+ constructor(
15
+ @InjectConnection('db1Connection')
16
+ private knexConnection: Knex,
17
+ ) {}
18
+
19
+ async findAll() {
20
+ const users = await this.knexConnection.table('users');
21
+ return { users };
22
+ }
23
+
24
+ async create(createUserDto: CreateUserDto) {
25
+ try {
26
+ const users = await this.knexConnection.table('users').insert({
27
+ firstName: createUserDto.firstName,
28
+ lastName: createUserDto.lastName,
29
+ });
30
+
31
+ return { users };
32
+ } catch (err) {
33
+ throw new HttpException(err, HttpStatus.BAD_REQUEST);
34
+ }
35
+ }
36
+
37
+ async findOne(id: number) {
38
+ if (!id) {
39
+ throw new NotFoundException('User ID does not exist');
40
+ }
41
+ const users = await this.knexConnection.table('users').where('id', id);
42
+ return { users };
43
+ }
44
+
45
+ async update(id: number, updateUserDto: UpdateUserDto) {
46
+ try {
47
+ const users = await this.knexConnection
48
+ .table('users')
49
+ .where('id', id)
50
+ .update({
51
+ firstName: updateUserDto.firstName,
52
+ lastName: updateUserDto.lastName,
53
+ });
54
+
55
+ return { users };
56
+ } catch (err) {
57
+ throw new HttpException(err, HttpStatus.BAD_REQUEST);
58
+ }
59
+ }
60
+
61
+ async remove(id: number) {
62
+ if (!id) {
63
+ throw new NotFoundException('User ID does not exist');
64
+ }
65
+ const users = await this.knexConnection
66
+ .table('users')
67
+ .where('id', id)
68
+ .del();
69
+ return { users };
70
+ }
71
+ }
@@ -0,0 +1,21 @@
1
+ version: "3"
2
+
3
+ services:
4
+ db1:
5
+ image: mysql:5
6
+ restart: always
7
+ environment:
8
+ MYSQL_ROOT_USER: root
9
+ MYSQL_ROOT_PASSWORD: root
10
+ MYSQL_DATABASE: test1
11
+ ports:
12
+ - "3306:3306"
13
+ db2:
14
+ image: mysql:5
15
+ restart: always
16
+ environment:
17
+ MYSQL_ROOT_USER: root
18
+ MYSQL_ROOT_PASSWORD: root
19
+ MYSQL_DATABASE: test2
20
+ ports:
21
+ - "3307:3306"
@@ -0,0 +1,34 @@
1
+ module.exports = {
2
+ development: {
3
+ client: 'mysql',
4
+ version: '5.7',
5
+ connection: {
6
+ name: 'db1Connection',
7
+ host: '127.0.0.1',
8
+ user: 'root',
9
+ port: 3306,
10
+ password: 'root',
11
+ database: 'test1',
12
+ },
13
+ migrations: {
14
+ directory: './migrations/users',
15
+ tableName: '20210808150144_users',
16
+ },
17
+ },
18
+ developmentOne: {
19
+ client: 'mysql',
20
+ version: '5.7',
21
+ connection: {
22
+ name: 'db2Connection',
23
+ host: '127.0.0.1',
24
+ user: 'root',
25
+ port: 3307,
26
+ password: 'root',
27
+ database: 'test2',
28
+ },
29
+ migrations: {
30
+ directory: './migrations/posts',
31
+ tableName: '20210902093735_posts',
32
+ },
33
+ },
34
+ };
@@ -0,0 +1,11 @@
1
+ exports.up = function (knex) {
2
+ return knex.schema.createTable('posts', function (table) {
3
+ table.increments('id').primary();
4
+ table.string('title', 255).notNullable();
5
+ table.string('description', 255).notNullable();
6
+ });
7
+ };
8
+
9
+ exports.down = function (knex) {
10
+ return knex.schema.dropTable('posts');
11
+ };
@@ -0,0 +1,11 @@
1
+ exports.up = function (knex) {
2
+ return knex.schema.createTable('users', function (table) {
3
+ table.increments('id').primary();
4
+ table.string('firstName', 255).notNullable();
5
+ table.string('lastName', 255).notNullable();
6
+ });
7
+ };
8
+
9
+ exports.down = function (knex) {
10
+ return knex.schema.dropTable('users');
11
+ };
@@ -0,0 +1,24 @@
1
+ exports.seed = function (knex) {
2
+ return knex('posts')
3
+ .del()
4
+ .then(function () {
5
+ return knex('posts').insert([
6
+ {
7
+ id: 1,
8
+ title: 'title #1',
9
+ description: 'description #1',
10
+ },
11
+ {
12
+ id: 2,
13
+ title: 'title #2',
14
+ description: 'description #2',
15
+ },
16
+ {
17
+ id: 3,
18
+ title: 'title #3',
19
+ description: 'description #3',
20
+ },
21
+ ]);
22
+ });
23
+ };
24
+
@@ -0,0 +1,23 @@
1
+ exports.seed = function (knex) {
2
+ return knex('users')
3
+ .del()
4
+ .then(function () {
5
+ return knex('users').insert([
6
+ {
7
+ id: 1,
8
+ firstName: 'firstName #1',
9
+ lastName: 'lastName #1',
10
+ },
11
+ {
12
+ id: 2,
13
+ firstName: 'firstName #2',
14
+ lastName: 'lastName #2',
15
+ },
16
+ {
17
+ id: 3,
18
+ firstName: 'firstName #3',
19
+ lastName: 'lastName #3',
20
+ },
21
+ ]);
22
+ });
23
+ };
@@ -0,0 +1,130 @@
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
+ });
@@ -0,0 +1,119 @@
1
+ import { INestApplication, HttpStatus } from '@nestjs/common';
2
+ import { TestingModule, Test } from '@nestjs/testing';
3
+ import { UsersModule } from './app-knex/app/users/users.module';
4
+ import { KnexModule } from '../lib';
5
+ import * as request from 'supertest';
6
+
7
+ describe('[Feature] User - /user', () => {
8
+ let app: INestApplication;
9
+
10
+ beforeAll(async () => {
11
+ const moduleFixture: TestingModule = await Test.createTestingModule({
12
+ imports: [
13
+ UsersModule,
14
+ KnexModule.forRoot({
15
+ config: {
16
+ client: 'mysql',
17
+ version: '5.7',
18
+ useNullAsDefault: true,
19
+ connection: {
20
+ host: '127.0.0.1',
21
+ user: 'root',
22
+ port: 3308,
23
+ password: 'root',
24
+ database: 'test',
25
+ },
26
+ },
27
+ }),
28
+ ],
29
+ }).compile();
30
+
31
+ app = moduleFixture.createNestApplication();
32
+ await app.init();
33
+ });
34
+
35
+ it('Create [POST /]', () => {
36
+ return request(app.getHttpServer())
37
+ .post('/user')
38
+ .expect(HttpStatus.CREATED)
39
+ .set('Accept', 'application/json')
40
+ .send({
41
+ firstName: 'firstName',
42
+ lastName: 'lastName',
43
+ })
44
+ .then((res) => {
45
+ res.body;
46
+ });
47
+ });
48
+
49
+ it('Get all [GET /]', () => {
50
+ return request(app.getHttpServer())
51
+ .get('/user')
52
+ .expect(HttpStatus.OK)
53
+ .set('Accept', 'application/json')
54
+ .then(({ body }) => {
55
+ expect(body['users']).toEqual([
56
+ {
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
+ firstName: 'firstName',
74
+ lastName: 'lastName',
75
+ },
76
+ ]);
77
+ });
78
+ });
79
+
80
+ it('Get one [GET /:id]', () => {
81
+ return request(app.getHttpServer())
82
+ .get('/user/2')
83
+ .expect(HttpStatus.OK)
84
+ .set('Accept', 'application/json')
85
+ .then(({ body }) => {
86
+ expect(body['users']).toEqual([
87
+ {
88
+ id: 2,
89
+ firstName: 'firstName #1',
90
+ lastName: 'lastName #1',
91
+ },
92
+ ]);
93
+ });
94
+ });
95
+
96
+ it('Update one [PUT /:id]', () => {
97
+ return request(app.getHttpServer())
98
+ .put('/user/2')
99
+ .expect(HttpStatus.OK)
100
+ .send({
101
+ firstName: 'firstName update',
102
+ lastName: 'lastName update',
103
+ })
104
+ .then((res) => {
105
+ res.body;
106
+ });
107
+ });
108
+
109
+ it('Delete one [DELETE /:id]', () => {
110
+ return request(app.getHttpServer())
111
+ .delete('/user/1')
112
+ .set('Accept', 'application/json')
113
+ .expect(HttpStatus.OK);
114
+ });
115
+
116
+ afterAll(async () => {
117
+ await app.close();
118
+ });
119
+ });