nest-knexjs 0.0.6 → 0.0.9
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/.github/workflows/codeql-analysis.yml +31 -0
- package/LICENSE +1 -1
- package/README.md +171 -1
- package/dist/common/index.js +5 -1
- package/dist/common/knex.utils.d.ts +1 -1
- package/dist/common/knex.utils.js +3 -0
- package/dist/exceptions/index.js +5 -1
- package/dist/index.js +5 -1
- package/dist/interfaces/index.js +5 -1
- package/package.json +27 -27
- package/tests/app-knex/app/app.module.ts +25 -0
- package/tests/{app-multi-database → app-knex/app}/main.ts +0 -0
- package/tests/{app-multi-database → app-knex/app}/users/dto/create-user.dto.ts +0 -0
- package/tests/{app-multi-database → app-knex/app}/users/dto/update-user.dto.ts +0 -0
- package/tests/{app-multi-database → app-knex/app}/users/entities/user.entity.ts +0 -0
- package/tests/{app-multi-database → app-knex/app}/users/users.controller.ts +0 -0
- package/tests/{app-multi-database → app-knex/app}/users/users.module.ts +0 -0
- package/tests/{app-multi-database → app-knex/app}/users/users.service.spec.ts +0 -0
- package/tests/{app-multi-database → app-knex/app}/users/users.service.ts +7 -10
- package/tests/{docker-compose.yml → app-knex/docker-compose.yml} +1 -1
- package/tests/app-knex/knexfile.js +11 -0
- package/tests/app-knex/migrations/20210223224919_api.js +11 -0
- package/tests/app-knex/seeds/users.js +23 -0
- package/tests/app-multi-database/{app.module.ts → app/app.module.ts} +0 -2
- package/tests/app-multi-database/app/main.ts +9 -0
- package/tests/app-multi-database/{post → app/post}/dto/create-post.dto.ts +0 -0
- package/tests/app-multi-database/{post → app/post}/dto/update-post.dto.ts +0 -0
- package/tests/app-multi-database/{post → app/post}/entities/post.entity.ts +0 -0
- package/tests/app-multi-database/{post → app/post}/post.controller.ts +0 -0
- package/tests/app-multi-database/{post → app/post}/post.module.ts +2 -1
- package/tests/app-multi-database/{post → app/post}/post.service.spec.ts +0 -0
- package/tests/app-multi-database/{post → app/post}/post.service.ts +4 -8
- package/tests/app-multi-database/app/users/dto/create-user.dto.ts +4 -0
- package/tests/app-multi-database/app/users/dto/update-user.dto.ts +4 -0
- package/tests/app-multi-database/app/users/entities/user.entity.ts +5 -0
- package/tests/app-multi-database/app/users/users.controller.ts +42 -0
- package/tests/app-multi-database/app/users/users.module.ts +9 -0
- package/tests/app-multi-database/app/users/users.service.spec.ts +18 -0
- package/tests/app-multi-database/app/users/users.service.ts +71 -0
- package/tests/app-multi-database/docker-compose.yml +21 -0
- package/tests/{knexfile.ts → app-multi-database/knexfile.js} +4 -5
- package/tests/{migrations → app-multi-database/migrations}/posts/20210902093735_posts.js +0 -0
- package/tests/{migrations → app-multi-database/migrations}/users/20210808150144_users.js +0 -0
- package/tests/{seeds → app-multi-database/seeds}/posts.js +6 -6
- package/tests/{seeds → app-multi-database/seeds}/users.js +4 -4
- package/tests/posts.e2e-spec.ts +68 -107
- package/tests/user.e2e-spec.ts +119 -0
- package/tests/users.e2e-spec.ts +68 -108
- package/tests/app-multi-database/post/post.controller.spec.ts +0 -23
- package/tests/app-multi-database/users/users.controller.spec.ts +0 -134
|
@@ -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@v3
|
|
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
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/
|
|
2
|
+
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="200" alt="Nest Logo" /></a>
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
5
|
[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
|
|
@@ -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)
|
|
@@ -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
|
package/dist/common/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -7,7 +7,7 @@ import { Observable } from 'rxjs';
|
|
|
7
7
|
* @returns {string} The Entity injection token
|
|
8
8
|
*/
|
|
9
9
|
export declare function getModelToken(model: Function, connection?: KnexModuleOptions | string): string;
|
|
10
|
-
export declare function getConnectionToken(connection?: KnexModuleOptions |
|
|
10
|
+
export declare function getConnectionToken(connection?: KnexModuleOptions | string): string | Function;
|
|
11
11
|
/**
|
|
12
12
|
* This function returns a Connection prefix based on the connection name
|
|
13
13
|
* @param {KnexModuleOptions | string} [connection='default'] This optional parameter is either
|
|
@@ -22,6 +22,9 @@ function getModelToken(model, connection = knex_constants_1.DEFAULT_CONNECTION_N
|
|
|
22
22
|
}
|
|
23
23
|
exports.getModelToken = getModelToken;
|
|
24
24
|
function getConnectionToken(connection = knex_constants_1.DEFAULT_CONNECTION_NAME) {
|
|
25
|
+
if (typeof connection === 'string') {
|
|
26
|
+
return connection;
|
|
27
|
+
}
|
|
25
28
|
return `${connection.name || knex_constants_1.DEFAULT_CONNECTION_NAME}`;
|
|
26
29
|
}
|
|
27
30
|
exports.getConnectionToken = getConnectionToken;
|
package/dist/exceptions/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
package/dist/interfaces/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nest-knexjs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
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": "
|
|
22
|
-
"@commitlint/config-angular": "
|
|
23
|
-
"@nestjs/common": "8.
|
|
24
|
-
"@nestjs/core": "8.
|
|
25
|
-
"@nestjs/platform-express": "8.
|
|
26
|
-
"@nestjs/testing": "8.
|
|
21
|
+
"@commitlint/cli": "16.2.3",
|
|
22
|
+
"@commitlint/config-angular": "16.2.3",
|
|
23
|
+
"@nestjs/common": "8.4.4",
|
|
24
|
+
"@nestjs/core": "8.4.4",
|
|
25
|
+
"@nestjs/platform-express": "8.4.4",
|
|
26
|
+
"@nestjs/testing": "8.4.4",
|
|
27
27
|
"@types/express": "4.17.13",
|
|
28
|
-
"@types/
|
|
29
|
-
"@types/
|
|
30
|
-
"@types/supertest": "2.0.
|
|
31
|
-
"@typescript-eslint/eslint-plugin": "
|
|
32
|
-
"@typescript-eslint/parser": "
|
|
33
|
-
"eslint": "
|
|
34
|
-
"eslint-config-prettier": "8.
|
|
35
|
-
"eslint-plugin-import": "2.
|
|
36
|
-
"knex": "0.
|
|
28
|
+
"@types/node": "16.11.27",
|
|
29
|
+
"@types/jest": "27.4.1",
|
|
30
|
+
"@types/supertest": "2.0.12",
|
|
31
|
+
"@typescript-eslint/eslint-plugin": "5.19.0",
|
|
32
|
+
"@typescript-eslint/parser": "5.19.0",
|
|
33
|
+
"eslint": "8.13.0",
|
|
34
|
+
"eslint-config-prettier": "8.5.0",
|
|
35
|
+
"eslint-plugin-import": "2.26.0",
|
|
36
|
+
"knex": "1.0.7",
|
|
37
37
|
"mysql": "2.18.1",
|
|
38
|
-
"husky": "7.0.
|
|
39
|
-
"
|
|
40
|
-
"
|
|
38
|
+
"husky": "7.0.4",
|
|
39
|
+
"prettier": "2.6.2",
|
|
40
|
+
"jest": "27.5.1",
|
|
41
41
|
"reflect-metadata": "0.1.13",
|
|
42
|
-
"release-it": "14.
|
|
43
|
-
"supertest": "6.
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"ts-jest": "27.
|
|
47
|
-
"tsconfig-paths": "3.
|
|
48
|
-
"typescript": "4.
|
|
42
|
+
"release-it": "14.14.2",
|
|
43
|
+
"supertest": "6.2.2",
|
|
44
|
+
"ts-node": "10.7.0",
|
|
45
|
+
"rxjs": "7.5.5",
|
|
46
|
+
"ts-jest": "27.1.4",
|
|
47
|
+
"tsconfig-paths": "3.14.1",
|
|
48
|
+
"typescript": "4.6.3"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"@nestjs/common": "^7.0.0 || ^8.0.0",
|
|
52
52
|
"@nestjs/core": "^7.0.0 || ^8.0.0",
|
|
53
53
|
"reflect-metadata": "^0.1.13",
|
|
54
54
|
"rxjs": "^6.6.3 || ^7.2.0",
|
|
55
|
-
"knex": "^0.21.17 || ^0.95.0"
|
|
55
|
+
"knex": "^0.21.17 || ^0.95.0 || ^1.0.0"
|
|
56
56
|
},
|
|
57
57
|
"lint-staged": {
|
|
58
58
|
"*.ts": [
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Module } from '@nestjs/common';
|
|
2
|
+
import { KnexModule } from '../../lib';
|
|
3
|
+
import { UsersModule } from './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
|
+
password: 'root',
|
|
16
|
+
database: 'test',
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
}),
|
|
20
|
+
UsersModule,
|
|
21
|
+
],
|
|
22
|
+
controllers: [],
|
|
23
|
+
providers: [],
|
|
24
|
+
})
|
|
25
|
+
export class AppModule {}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -5,25 +5,22 @@ import {
|
|
|
5
5
|
NotFoundException,
|
|
6
6
|
} from '@nestjs/common';
|
|
7
7
|
import { Knex } from 'knex';
|
|
8
|
-
import {
|
|
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
|
|
|
12
12
|
@Injectable()
|
|
13
13
|
export class UsersService {
|
|
14
|
-
constructor(
|
|
15
|
-
@InjectConnection('db1Connection')
|
|
16
|
-
private knex: Knex, //= knex(knexConfig.developmentOne.connection),
|
|
17
|
-
) {}
|
|
14
|
+
constructor(@InjectModel() private readonly knex: Knex) {}
|
|
18
15
|
|
|
19
16
|
async findAll() {
|
|
20
|
-
const users = await this.knex.table('
|
|
17
|
+
const users = await this.knex.table('user');
|
|
21
18
|
return { users };
|
|
22
19
|
}
|
|
23
20
|
|
|
24
21
|
async create(createUserDto: CreateUserDto) {
|
|
25
22
|
try {
|
|
26
|
-
const users = await this.knex.table('
|
|
23
|
+
const users = await this.knex.table('user').insert({
|
|
27
24
|
firstName: createUserDto.firstName,
|
|
28
25
|
lastName: createUserDto.lastName,
|
|
29
26
|
});
|
|
@@ -38,13 +35,13 @@ export class UsersService {
|
|
|
38
35
|
if (!id) {
|
|
39
36
|
throw new NotFoundException('User ID does not exist');
|
|
40
37
|
}
|
|
41
|
-
const users = await this.knex.table('
|
|
38
|
+
const users = await this.knex.table('user').where('id', id);
|
|
42
39
|
return { users };
|
|
43
40
|
}
|
|
44
41
|
|
|
45
42
|
async update(id: number, updateUserDto: UpdateUserDto) {
|
|
46
43
|
try {
|
|
47
|
-
const users = await this.knex.table('
|
|
44
|
+
const users = await this.knex.table('user').where('id', id).update({
|
|
48
45
|
firstName: updateUserDto.firstName,
|
|
49
46
|
lastName: updateUserDto.lastName,
|
|
50
47
|
});
|
|
@@ -59,7 +56,7 @@ export class UsersService {
|
|
|
59
56
|
if (!id) {
|
|
60
57
|
throw new NotFoundException('User ID does not exist');
|
|
61
58
|
}
|
|
62
|
-
const users = await this.knex.table('
|
|
59
|
+
const users = await this.knex.table('user').where('id', id).del();
|
|
63
60
|
return { users };
|
|
64
61
|
}
|
|
65
62
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
exports.up = function (knex) {
|
|
2
|
+
return knex.schema.createTable('user', function (table) {
|
|
3
|
+
table.increments('id');
|
|
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('user');
|
|
11
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
exports.seed = function (knex) {
|
|
2
|
+
return knex('user')
|
|
3
|
+
.del()
|
|
4
|
+
.then(function () {
|
|
5
|
+
return knex('user').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
|
+
};
|
|
@@ -23,7 +23,6 @@ import { UsersModule } from './users/users.module';
|
|
|
23
23
|
min: 2,
|
|
24
24
|
max: 10,
|
|
25
25
|
},
|
|
26
|
-
searchPath: ['db1Connection'],
|
|
27
26
|
},
|
|
28
27
|
}),
|
|
29
28
|
},
|
|
@@ -47,7 +46,6 @@ import { UsersModule } from './users/users.module';
|
|
|
47
46
|
min: 2,
|
|
48
47
|
max: 10,
|
|
49
48
|
},
|
|
50
|
-
searchPath: ['db2Connection'],
|
|
51
49
|
},
|
|
52
50
|
}),
|
|
53
51
|
},
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -2,17 +2,14 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|
|
2
2
|
import { CreatePostDto } from './dto/create-post.dto';
|
|
3
3
|
import { UpdatePostDto } from './dto/update-post.dto';
|
|
4
4
|
import { NotFoundException } from '@nestjs/common';
|
|
5
|
-
// import KnexConnection from '../knex';
|
|
6
|
-
// import knex from 'knex';
|
|
7
|
-
// import knexConfig from '../../knexfile';
|
|
8
5
|
import { Knex } from 'knex';
|
|
9
|
-
import { InjectConnection } from '
|
|
6
|
+
import { InjectConnection } from '../../../../lib';
|
|
10
7
|
|
|
11
8
|
@Injectable()
|
|
12
9
|
export class PostService {
|
|
13
10
|
constructor(
|
|
14
11
|
@InjectConnection('db2Connection')
|
|
15
|
-
private knexConnection: Knex,
|
|
12
|
+
private knexConnection: Knex,
|
|
16
13
|
) {}
|
|
17
14
|
|
|
18
15
|
async create(createPostDto: CreatePostDto) {
|
|
@@ -29,14 +26,13 @@ export class PostService {
|
|
|
29
26
|
}
|
|
30
27
|
|
|
31
28
|
async findAll() {
|
|
32
|
-
console.log(this.knexConnection);
|
|
33
29
|
const posts = await this.knexConnection.table('posts');
|
|
34
30
|
return { posts };
|
|
35
31
|
}
|
|
36
32
|
|
|
37
33
|
async findOne(id: number) {
|
|
38
34
|
if (!id) {
|
|
39
|
-
throw new NotFoundException('
|
|
35
|
+
throw new NotFoundException('Post ID does not exist');
|
|
40
36
|
}
|
|
41
37
|
const posts = await this.knexConnection.table('posts').where('id', id);
|
|
42
38
|
return { posts };
|
|
@@ -60,7 +56,7 @@ export class PostService {
|
|
|
60
56
|
|
|
61
57
|
async remove(id: number) {
|
|
62
58
|
if (!id) {
|
|
63
|
-
throw new NotFoundException('
|
|
59
|
+
throw new NotFoundException('Post ID does not exist');
|
|
64
60
|
}
|
|
65
61
|
const post = await this.knexConnection.table('posts').where('id', id).del();
|
|
66
62
|
return { post };
|