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,31 @@
1
+ name: "CodeQL"
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+ schedule:
9
+ - cron: '0 0 * * *'
10
+
11
+ jobs:
12
+ analyze:
13
+ name: Analyze
14
+ runs-on: ubuntu-latest
15
+
16
+ steps:
17
+ - name: Checkout repository
18
+ uses: actions/checkout@v2
19
+ with:
20
+ fetch-depth: 0
21
+
22
+ - name: Initialize CodeQL
23
+ uses: github/codeql-action/init@v1
24
+ with:
25
+ languages: javascript
26
+
27
+ - name: Autobuild
28
+ uses: github/codeql-action/autobuild@v1
29
+
30
+ - name: Perform CodeQL Analysis
31
+ uses: github/codeql-action/analyze@v1
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021 Tony133
3
+ Copyright (c) 2021-2022 Tony133
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -51,6 +51,7 @@ or
51
51
  - [Usage](#usage)
52
52
  - [KnexModule](#knexmodule)
53
53
  - [ExampleOfUse](#example-of-use)
54
+ - [MultiConnectionsDatabae](#multi-connections-database)
54
55
  - [CreateMigrations](#create-migrations)
55
56
  - [RunMigrations](#run-migrations)
56
57
  - [CreateSeeds](#create-seeds)
@@ -108,12 +109,12 @@ UsersService:
108
109
 
109
110
  ```typescript
110
111
  import { Injectable } from '@nestjs/common';
111
- import Knex from 'knex';
112
- import { InjectModel } from 'nest-knexjs';
112
+ import { Knex } from 'knex';
113
+ import { InjectConnection } from 'nest-knexjs';
113
114
 
114
115
  @Injectable()
115
116
  export class UsersService {
116
- constructor(@InjectModel() private readonly knex: Knex) {}
117
+ constructor(@InjectConnection() private readonly knex: Knex) {}
117
118
 
118
119
  async findAll() {
119
120
  const users = await this.knex.table('users');
@@ -139,6 +140,175 @@ export class UsersController {
139
140
  }
140
141
  ```
141
142
 
143
+ ## Multi Connections Database
144
+
145
+ ```typescript
146
+ @Module({
147
+ imports: [
148
+ KnexModule.forRootAsync(
149
+ {
150
+ useFactory: () => ({
151
+ config: {
152
+ client: 'mysql',
153
+ version: '5.7',
154
+ useNullAsDefault: true,
155
+ connection: {
156
+ host: '127.0.0.1',
157
+ user: 'root',
158
+ port: 3306,
159
+ password: 'root',
160
+ database: 'test1',
161
+ },
162
+ pool: {
163
+ min: 2,
164
+ max: 10,
165
+ },
166
+ },
167
+ }),
168
+ },
169
+ 'db1Connection',
170
+ ),
171
+ KnexModule.forRootAsync(
172
+ {
173
+ useFactory: () => ({
174
+ config: {
175
+ client: 'mysql',
176
+ version: '5.7',
177
+ useNullAsDefault: true,
178
+ connection: {
179
+ host: '127.0.0.1',
180
+ user: 'root',
181
+ port: 3307,
182
+ password: 'root',
183
+ database: 'test2',
184
+ },
185
+ pool: {
186
+ min: 2,
187
+ max: 10,
188
+ },
189
+ },
190
+ }),
191
+ },
192
+ 'db2Connection',
193
+ ),
194
+ ],
195
+ controllers: [],
196
+ providers: [],
197
+ })
198
+ export class AppModule {}
199
+ ```
200
+
201
+ Usage example with Multi Connection
202
+
203
+ PostService:
204
+
205
+ ```typescript
206
+ @Injectable()
207
+ export class PostService {
208
+ constructor(
209
+ @InjectConnection('db2Connection')
210
+ private knexConnection: Knex,
211
+ ) {}
212
+
213
+ async create(createPostDto: CreatePostDto) {
214
+ try {
215
+ const post = await this.knexConnection.table('posts').insert({
216
+ title: createPostDto.title,
217
+ description: createPostDto.description,
218
+ });
219
+
220
+ return post;
221
+ } catch (err) {
222
+ throw new HttpException(err, HttpStatus.BAD_REQUEST);
223
+ }
224
+ }
225
+
226
+ async findAll() {
227
+ console.log(this.knexConnection);
228
+ const posts = await this.knexConnection.table('posts');
229
+ return { posts };
230
+ }
231
+ }
232
+ ```
233
+
234
+ UsersService:
235
+
236
+ ```typescript
237
+ @Injectable()
238
+ export class UsersService {
239
+ constructor(
240
+ @InjectConnection('db1Connection')
241
+ private knexConnection: Knex,
242
+ ) {}
243
+
244
+ async findAll() {
245
+ const users = await this.knexConnection.table('users');
246
+ return { users };
247
+ }
248
+
249
+ async create(createUserDto: CreateUserDto) {
250
+ try {
251
+ const users = await this.knexConnection.table('users').insert({
252
+ firstName: createUserDto.firstName,
253
+ lastName: createUserDto.lastName,
254
+ });
255
+
256
+ return { users };
257
+ } catch (err) {
258
+ throw new HttpException(err, HttpStatus.BAD_REQUEST);
259
+ }
260
+ }
261
+ }
262
+ ```
263
+
264
+
265
+ In `knexfile.js` update with your database configuration settings:
266
+
267
+ ```javascript
268
+ module.exports = {
269
+ development: {
270
+ client: 'mysql',
271
+ version: '5.7',
272
+ connection: {
273
+ name: 'db1Connection',
274
+ host: '127.0.0.1',
275
+ user: 'root',
276
+ port: 3306,
277
+ password: 'root',
278
+ database: 'test1',
279
+ },
280
+ migrations: {
281
+ directory: './migrations/users',
282
+ tableName: '[:name_file_migrations_users]',
283
+ },
284
+ },
285
+ developmentTwo: {
286
+ client: 'mysql',
287
+ version: '5.7',
288
+ connection: {
289
+ name: 'db2Connection',
290
+ host: '127.0.0.1',
291
+ user: 'root',
292
+ port: 3307,
293
+ password: 'root',
294
+ database: 'test2',
295
+ },
296
+ migrations: {
297
+ directory: './migrations/posts',
298
+ tableName: '[:name_file_migrations_posts]',
299
+ },
300
+ },
301
+ };
302
+ ```
303
+
304
+ Run Migrations:
305
+
306
+ ```bash
307
+ $ npx knex migrate:latest --env development
308
+
309
+ $ npx knex migrate:latest --env developmentTwo
310
+ ```
311
+
142
312
  ## Create Migrations
143
313
 
144
314
  ```bash
@@ -153,8 +323,8 @@ exports.up = function(knex) {
153
323
  return knex.schema
154
324
  .createTable('users', function(table) {
155
325
  table.increments('id');
156
- table.string('first_name', 255).notNullable();
157
- table.string('last_name', 255).notNullable();
326
+ table.string('firstName', 255).notNullable();
327
+ table.string('lastName', 255).notNullable();
158
328
  })
159
329
  .createTable('products', function(table) {
160
330
  table.increments('id');
@@ -206,9 +376,9 @@ exports.seed = function (knex) {
206
376
  .del()
207
377
  .then(function () {
208
378
  return knex('users').insert([
209
- { id: 1, first_name: 'firstname1', last_name: 'lastname1' },
210
- { id: 2, first_name: 'firstname2', last_name: 'lastname2' },
211
- { id: 3, first_name: 'firstname3', last_name: 'lastname3' },
379
+ { id: 1, firstName: 'firstName#1', lastName: 'lastName#1' },
380
+ { id: 2, firstName: 'firstName#2', lastName: 'lastName#2' },
381
+ { id: 3, firstName: 'firstName#3', lastName: 'lastName#3' },
212
382
  ]);
213
383
  });
214
384
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nest-knexjs",
3
- "version": "0.0.3",
3
+ "version": "0.0.7",
4
4
  "description": "Knexjs module for Nest framework (node.js) 😻",
5
5
  "author": "Tony133",
6
6
  "license": "MIT",
@@ -18,41 +18,41 @@
18
18
  "release": "release-it"
19
19
  },
20
20
  "devDependencies": {
21
- "@commitlint/cli": "12.1.1",
22
- "@commitlint/config-angular": "12.1.1",
23
- "@nestjs/common": "7.6.15",
24
- "@nestjs/core": "7.6.15",
25
- "@nestjs/platform-express": "7.6.15",
26
- "@nestjs/testing": "7.6.15",
27
- "@types/express": "4.17.8",
28
- "@types/jest": "26.0.23",
29
- "@types/node": "14.14.44",
21
+ "@commitlint/cli": "16.0.2",
22
+ "@commitlint/config-angular": "16.0.0",
23
+ "@nestjs/common": "8.2.5",
24
+ "@nestjs/core": "8.2.5",
25
+ "@nestjs/platform-express": "8.2.5",
26
+ "@nestjs/testing": "8.2.5",
27
+ "@types/express": "4.17.13",
28
+ "@types/node": "16.11.19",
29
+ "@types/jest": "27.4.0",
30
30
  "@types/supertest": "2.0.11",
31
- "@typescript-eslint/eslint-plugin": "4.22.1",
32
- "@typescript-eslint/parser": "4.22.1",
33
- "eslint": "7.26.0",
31
+ "@typescript-eslint/eslint-plugin": "5.9.1",
32
+ "@typescript-eslint/parser": "5.9.1",
33
+ "eslint": "8.7.0",
34
34
  "eslint-config-prettier": "8.3.0",
35
- "eslint-plugin-import": "2.22.1",
36
- "knex": "0.95.4",
35
+ "eslint-plugin-import": "2.25.4",
36
+ "knex": "1.0.1",
37
37
  "mysql": "2.18.1",
38
- "husky": "6.0.0",
39
- "jest": "26.6.3",
40
- "prettier": "2.2.1",
38
+ "husky": "7.0.4",
39
+ "prettier": "2.5.1",
40
+ "jest": "27.4.7",
41
41
  "reflect-metadata": "0.1.13",
42
- "release-it": "14.6.1",
43
- "supertest": "6.1.3",
44
- "rxjs": "7.0.0",
45
- "ts-node": "9.1.1",
46
- "ts-jest": "26.5.6",
47
- "tsconfig-paths": "3.9.0",
48
- "typescript": "4.2.4"
42
+ "release-it": "14.12.3",
43
+ "supertest": "6.2.1",
44
+ "ts-node": "10.4.0",
45
+ "rxjs": "7.5.2",
46
+ "ts-jest": "27.1.3",
47
+ "tsconfig-paths": "3.12.0",
48
+ "typescript": "4.5.4"
49
49
  },
50
50
  "peerDependencies": {
51
- "@nestjs/common": "^7.0.0",
52
- "@nestjs/core": "^7.0.0",
51
+ "@nestjs/common": "^7.0.0 || ^8.0.0",
52
+ "@nestjs/core": "^7.0.0 || ^8.0.0",
53
53
  "reflect-metadata": "^0.1.13",
54
- "rxjs": "^6.6.3 || ^7.0.0",
55
- "knex": "^0.21.17 || ^0.95.0"
54
+ "rxjs": "^6.6.3 || ^7.2.0",
55
+ "knex": "^0.21.17 || ^0.95.0 || ^1.0.0"
56
56
  },
57
57
  "lint-staged": {
58
58
  "*.ts": [
File without changes
File without changes
@@ -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
+ }
@@ -4,8 +4,8 @@ import {
4
4
  Injectable,
5
5
  NotFoundException,
6
6
  } from '@nestjs/common';
7
- import * as Knex from 'knex';
8
- import { InjectModel } from '../../../lib';
7
+ import { Knex } from 'knex';
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
 
@@ -14,15 +14,15 @@ export class UsersService {
14
14
  constructor(@InjectModel() private readonly knex: Knex) {}
15
15
 
16
16
  async findAll() {
17
- const users = await this.knex.table('user_test');
17
+ const users = await this.knex.table('user');
18
18
  return { users };
19
19
  }
20
20
 
21
21
  async create(createUserDto: CreateUserDto) {
22
22
  try {
23
- const users = await this.knex.table('user_test').insert({
24
- first_name: createUserDto.first_name,
25
- last_name: createUserDto.last_name,
23
+ const users = await this.knex.table('user').insert({
24
+ firstName: createUserDto.firstName,
25
+ lastName: createUserDto.lastName,
26
26
  });
27
27
 
28
28
  return { users };
@@ -33,17 +33,17 @@ export class UsersService {
33
33
 
34
34
  async findOne(id: number) {
35
35
  if (!id) {
36
- throw new NotFoundException('Customer ID does not exist');
36
+ throw new NotFoundException('User ID does not exist');
37
37
  }
38
- const users = await this.knex.table('user_test').where('id', id);
38
+ const users = await this.knex.table('user').where('id', id);
39
39
  return { users };
40
40
  }
41
41
 
42
42
  async update(id: number, updateUserDto: UpdateUserDto) {
43
43
  try {
44
- const users = await this.knex.table('user_test').where('id', id).update({
45
- first_name: updateUserDto.first_name,
46
- last_name: updateUserDto.last_name,
44
+ const users = await this.knex.table('user').where('id', id).update({
45
+ firstName: updateUserDto.firstName,
46
+ lastName: updateUserDto.lastName,
47
47
  });
48
48
 
49
49
  return { users };
@@ -54,9 +54,9 @@ export class UsersService {
54
54
 
55
55
  async remove(id: number) {
56
56
  if (!id) {
57
- throw new NotFoundException('Customer ID does not exist');
57
+ throw new NotFoundException('User ID does not exist');
58
58
  }
59
- const users = await this.knex.table('user_test').where('id', id).del();
59
+ const users = await this.knex.table('user').where('id', id).del();
60
60
  return { users };
61
61
  }
62
62
  }
@@ -9,4 +9,4 @@ services:
9
9
  MYSQL_ROOT_PASSWORD: root
10
10
  MYSQL_DATABASE: test
11
11
  ports:
12
- - "3306:3306"
12
+ - "3308:3306"
File without changes
@@ -1,8 +1,8 @@
1
1
  exports.up = function (knex) {
2
2
  return knex.schema.createTable('user', function (table) {
3
3
  table.increments('id');
4
- table.string('first_name', 255).notNullable();
5
- table.string('last_name', 255).notNullable();
4
+ table.string('firstName', 255).notNullable();
5
+ table.string('lastName', 255).notNullable();
6
6
  });
7
7
  };
8
8
 
@@ -5,18 +5,18 @@ exports.seed = function (knex) {
5
5
  return knex('user').insert([
6
6
  {
7
7
  id: 1,
8
- first_name: 'firstName #1',
9
- last_name: 'lastName #1',
8
+ firstName: 'firstName #1',
9
+ lastName: 'lastName #1',
10
10
  },
11
11
  {
12
12
  id: 2,
13
- first_name: 'firstName #1',
14
- last_name: 'lastName #1',
13
+ firstName: 'firstName #2',
14
+ lastName: 'lastName #2',
15
15
  },
16
16
  {
17
17
  id: 3,
18
- first_name: 'firstName #1',
19
- last_name: 'lastName #1',
18
+ firstName: 'firstName #3',
19
+ lastName: 'lastName #3',
20
20
  },
21
21
  ]);
22
22
  });
@@ -0,0 +1,61 @@
1
+ import { Module } from '@nestjs/common';
2
+ import { KnexModule } from '../../lib';
3
+ import { PostModule } from './post/post.module';
4
+ import { UsersModule } from './users/users.module';
5
+
6
+ @Module({
7
+ imports: [
8
+ KnexModule.forRootAsync(
9
+ {
10
+ useFactory: () => ({
11
+ config: {
12
+ client: 'mysql',
13
+ version: '5.7',
14
+ useNullAsDefault: true,
15
+ connection: {
16
+ host: '127.0.0.1',
17
+ user: 'root',
18
+ port: 3306,
19
+ password: 'root',
20
+ database: 'test1',
21
+ },
22
+ pool: {
23
+ min: 2,
24
+ max: 10,
25
+ },
26
+ },
27
+ }),
28
+ },
29
+ 'db1Connection',
30
+ ),
31
+ KnexModule.forRootAsync(
32
+ {
33
+ useFactory: () => ({
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: 3307,
42
+ password: 'root',
43
+ database: 'test2',
44
+ },
45
+ pool: {
46
+ min: 2,
47
+ max: 10,
48
+ },
49
+ },
50
+ }),
51
+ },
52
+ 'db2Connection',
53
+ ),
54
+
55
+ UsersModule,
56
+ PostModule,
57
+ ],
58
+ controllers: [],
59
+ providers: [],
60
+ })
61
+ export class AppModule {}
@@ -0,0 +1,9 @@
1
+ import { NestFactory } from '@nestjs/core';
2
+ import { AppModule } from './app.module';
3
+
4
+ async function bootstrap() {
5
+ const app = await NestFactory.create(AppModule);
6
+
7
+ await app.listen(3000);
8
+ }
9
+ bootstrap();
@@ -0,0 +1,4 @@
1
+ export class CreatePostDto {
2
+ readonly title: string;
3
+ readonly description: string;
4
+ }
@@ -0,0 +1,4 @@
1
+ export class UpdatePostDto {
2
+ readonly title: string;
3
+ readonly description: string;
4
+ }
@@ -0,0 +1,5 @@
1
+ export class Post {
2
+ id: number;
3
+ title: string;
4
+ description: string;
5
+ }
@@ -0,0 +1,42 @@
1
+ import {
2
+ Controller,
3
+ Get,
4
+ Post,
5
+ Body,
6
+ Patch,
7
+ Param,
8
+ Delete,
9
+ } from '@nestjs/common';
10
+ import { PostService } from './post.service';
11
+ import { CreatePostDto } from './dto/create-post.dto';
12
+ import { UpdatePostDto } from './dto/update-post.dto';
13
+
14
+ @Controller('posts')
15
+ export class PostController {
16
+ constructor(private readonly postService: PostService) {}
17
+
18
+ @Post()
19
+ create(@Body() createPostDto: CreatePostDto) {
20
+ return this.postService.create(createPostDto);
21
+ }
22
+
23
+ @Get()
24
+ findAll() {
25
+ return this.postService.findAll();
26
+ }
27
+
28
+ @Get(':id')
29
+ findOne(@Param('id') id: string) {
30
+ return this.postService.findOne(+id);
31
+ }
32
+
33
+ @Patch(':id')
34
+ update(@Param('id') id: string, @Body() updatePostDto: UpdatePostDto) {
35
+ return this.postService.update(+id, updatePostDto);
36
+ }
37
+
38
+ @Delete(':id')
39
+ remove(@Param('id') id: string) {
40
+ return this.postService.remove(+id);
41
+ }
42
+ }
@@ -0,0 +1,10 @@
1
+ import { Module } from '@nestjs/common';
2
+ import { PostService } from './post.service';
3
+ import { PostController } from './post.controller';
4
+
5
+ @Module({
6
+ imports: [],
7
+ controllers: [PostController],
8
+ providers: [PostService],
9
+ })
10
+ export class PostModule {}
@@ -0,0 +1,18 @@
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
+ });