nest-knexjs 0.0.31 → 0.0.32
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/README.md +1 -394
- package/_README.md +406 -0
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -1,396 +1,3 @@
|
|
|
1
1
|
# nest-knexjs
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
## Description
|
|
6
|
-
|
|
7
|
-
Knexjs module for Nest framework (node.js)
|
|
8
|
-
|
|
9
|
-
## Installation
|
|
10
|
-
|
|
11
|
-
First install the module via `yarn` or `npm` or `pnpm` and do not forget to install the driver package as well:
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
npm i --save nest-knexjs knex mysql # for mysql/mariadb
|
|
15
|
-
npm i --save nest-knexjs knex pg # for postgresql
|
|
16
|
-
npm i --save nest-knexjs knex sqlite # for sqlite
|
|
17
|
-
npm i --save nest-knexjs knex oracledb # for oracledb
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
or
|
|
21
|
-
|
|
22
|
-
```bash
|
|
23
|
-
yarn add nest-knexjs knex mysql # for mysql/mariadb
|
|
24
|
-
yarn add nest-knexjs knex pg # for postgresql
|
|
25
|
-
yarn add nest-knexjs knex sqlite # for sqlite
|
|
26
|
-
yarn add nest-knexjs knex oracledb # for oracledb
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
or
|
|
30
|
-
|
|
31
|
-
```bash
|
|
32
|
-
pnpm add nest-knexjs knex mysql # for mysql/mariadb
|
|
33
|
-
pnpm add nest-knexjs knex pg # for postgresql
|
|
34
|
-
pnpm add nest-knexjs knex sqlite # for sqlite
|
|
35
|
-
pnpm add nest-knexjs knex oracledb # for oracledb
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
## Table of Contents
|
|
39
|
-
|
|
40
|
-
- [nest-knexjs](#nest-knexjs)
|
|
41
|
-
- [Description](#description)
|
|
42
|
-
- [Installation](#installation)
|
|
43
|
-
- [Table of Contents](#table-of-contents)
|
|
44
|
-
- [Usage](#usage)
|
|
45
|
-
- [KnexModule](#knexmodule)
|
|
46
|
-
- [Example of use](#example-of-use)
|
|
47
|
-
- [Multi Connections Database](#multi-connections-database)
|
|
48
|
-
- [Create Migrations](#create-migrations)
|
|
49
|
-
- [Run Migrations](#run-migrations)
|
|
50
|
-
- [Create Seeds](#create-seeds)
|
|
51
|
-
- [Run Seeds](#run-seeds)
|
|
52
|
-
- [Contribute](#contribute)
|
|
53
|
-
- [License](#license)
|
|
54
|
-
|
|
55
|
-
## Usage
|
|
56
|
-
|
|
57
|
-
### KnexModule
|
|
58
|
-
|
|
59
|
-
KnexModule is the primary entry point for this package and can be used synchronously
|
|
60
|
-
|
|
61
|
-
```typescript
|
|
62
|
-
@Module({
|
|
63
|
-
imports: [
|
|
64
|
-
KnexModule.forRoot({
|
|
65
|
-
config: {
|
|
66
|
-
client: 'mysql',
|
|
67
|
-
version: '5.7',
|
|
68
|
-
connection: {
|
|
69
|
-
host: '127.0.0.1',
|
|
70
|
-
user: 'root',
|
|
71
|
-
password: 'root',
|
|
72
|
-
database: 'test',
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
}),
|
|
76
|
-
],
|
|
77
|
-
})
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
or asynchronously
|
|
81
|
-
|
|
82
|
-
```typescript
|
|
83
|
-
@Module({
|
|
84
|
-
imports: [
|
|
85
|
-
KnexModule.forRootAsync({
|
|
86
|
-
useFactory: () => ({
|
|
87
|
-
config: {
|
|
88
|
-
client: 'mysql',
|
|
89
|
-
version: '5.7',
|
|
90
|
-
connection: {
|
|
91
|
-
host: '127.0.0.1',
|
|
92
|
-
user: 'root',
|
|
93
|
-
password: 'root',
|
|
94
|
-
database: 'test',
|
|
95
|
-
},
|
|
96
|
-
},
|
|
97
|
-
}),
|
|
98
|
-
}),
|
|
99
|
-
],
|
|
100
|
-
})
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
## Example of use
|
|
104
|
-
|
|
105
|
-
UsersService:
|
|
106
|
-
|
|
107
|
-
```typescript
|
|
108
|
-
import { Injectable } from '@nestjs/common';
|
|
109
|
-
import { Knex } from 'knex';
|
|
110
|
-
import { InjectConnection } from 'nest-knexjs';
|
|
111
|
-
|
|
112
|
-
@Injectable()
|
|
113
|
-
export class UsersService {
|
|
114
|
-
constructor(@InjectConnection() private readonly knex: Knex) {}
|
|
115
|
-
|
|
116
|
-
async findAll() {
|
|
117
|
-
const users = await this.knex.table('users');
|
|
118
|
-
return { users };
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
UsersController:
|
|
124
|
-
|
|
125
|
-
```typescript
|
|
126
|
-
import { Controller, Get } from '@nestjs/common';
|
|
127
|
-
import { UsersService } from './users.service';
|
|
128
|
-
|
|
129
|
-
@Controller()
|
|
130
|
-
export class UsersController {
|
|
131
|
-
constructor(private readonly usersService: UsersService) {}
|
|
132
|
-
|
|
133
|
-
@Get()
|
|
134
|
-
async getAllUsers() {
|
|
135
|
-
return await this.usersService.findAll();
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
## Multi Connections Database
|
|
141
|
-
|
|
142
|
-
```typescript
|
|
143
|
-
@Module({
|
|
144
|
-
imports: [
|
|
145
|
-
KnexModule.forRootAsync(
|
|
146
|
-
{
|
|
147
|
-
useFactory: () => ({
|
|
148
|
-
config: {
|
|
149
|
-
client: 'mysql',
|
|
150
|
-
version: '5.7',
|
|
151
|
-
useNullAsDefault: true,
|
|
152
|
-
connection: {
|
|
153
|
-
host: '127.0.0.1',
|
|
154
|
-
user: 'root',
|
|
155
|
-
port: 3306,
|
|
156
|
-
password: 'root',
|
|
157
|
-
database: 'test1',
|
|
158
|
-
},
|
|
159
|
-
pool: {
|
|
160
|
-
min: 2,
|
|
161
|
-
max: 10,
|
|
162
|
-
},
|
|
163
|
-
},
|
|
164
|
-
}),
|
|
165
|
-
},
|
|
166
|
-
'db1Connection',
|
|
167
|
-
),
|
|
168
|
-
KnexModule.forRootAsync(
|
|
169
|
-
{
|
|
170
|
-
useFactory: () => ({
|
|
171
|
-
config: {
|
|
172
|
-
client: 'mysql',
|
|
173
|
-
version: '5.7',
|
|
174
|
-
useNullAsDefault: true,
|
|
175
|
-
connection: {
|
|
176
|
-
host: '127.0.0.1',
|
|
177
|
-
user: 'root',
|
|
178
|
-
port: 3307,
|
|
179
|
-
password: 'root',
|
|
180
|
-
database: 'test2',
|
|
181
|
-
},
|
|
182
|
-
pool: {
|
|
183
|
-
min: 2,
|
|
184
|
-
max: 10,
|
|
185
|
-
},
|
|
186
|
-
},
|
|
187
|
-
}),
|
|
188
|
-
},
|
|
189
|
-
'db2Connection',
|
|
190
|
-
),
|
|
191
|
-
],
|
|
192
|
-
controllers: [],
|
|
193
|
-
providers: [],
|
|
194
|
-
})
|
|
195
|
-
export class AppModule {}
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
Usage example with Multi Connection
|
|
199
|
-
|
|
200
|
-
PostService:
|
|
201
|
-
|
|
202
|
-
```typescript
|
|
203
|
-
@Injectable()
|
|
204
|
-
export class PostService {
|
|
205
|
-
constructor(
|
|
206
|
-
@InjectConnection('db2Connection')
|
|
207
|
-
private knexConnection: Knex,
|
|
208
|
-
) {}
|
|
209
|
-
|
|
210
|
-
async create(createPostDto: CreatePostDto) {
|
|
211
|
-
try {
|
|
212
|
-
const post = await this.knexConnection.table('posts').insert({
|
|
213
|
-
title: createPostDto.title,
|
|
214
|
-
description: createPostDto.description,
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
return post;
|
|
218
|
-
} catch (err) {
|
|
219
|
-
throw new HttpException(err, HttpStatus.BAD_REQUEST);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
async findAll() {
|
|
224
|
-
const posts = await this.knexConnection.table('posts');
|
|
225
|
-
return { posts };
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
UsersService:
|
|
231
|
-
|
|
232
|
-
```typescript
|
|
233
|
-
@Injectable()
|
|
234
|
-
export class UsersService {
|
|
235
|
-
constructor(
|
|
236
|
-
@InjectConnection('db1Connection')
|
|
237
|
-
private knexConnection: Knex,
|
|
238
|
-
) {}
|
|
239
|
-
|
|
240
|
-
async findAll() {
|
|
241
|
-
const users = await this.knexConnection.table('users');
|
|
242
|
-
return { users };
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
async create(createUserDto: CreateUserDto) {
|
|
246
|
-
try {
|
|
247
|
-
const users = await this.knexConnection.table('users').insert({
|
|
248
|
-
firstName: createUserDto.firstName,
|
|
249
|
-
lastName: createUserDto.lastName,
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
return { users };
|
|
253
|
-
} catch (err) {
|
|
254
|
-
throw new HttpException(err, HttpStatus.BAD_REQUEST);
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
In `knexfile.js` update with your database configuration settings:
|
|
261
|
-
|
|
262
|
-
```javascript
|
|
263
|
-
module.exports = {
|
|
264
|
-
development: {
|
|
265
|
-
client: 'mysql',
|
|
266
|
-
version: '5.7',
|
|
267
|
-
connection: {
|
|
268
|
-
name: 'db1Connection',
|
|
269
|
-
host: '127.0.0.1',
|
|
270
|
-
user: 'root',
|
|
271
|
-
port: 3306,
|
|
272
|
-
password: 'root',
|
|
273
|
-
database: 'test1',
|
|
274
|
-
},
|
|
275
|
-
migrations: {
|
|
276
|
-
directory: './migrations/users',
|
|
277
|
-
tableName: '[:name_file_migrations_users]',
|
|
278
|
-
},
|
|
279
|
-
},
|
|
280
|
-
developmentTwo: {
|
|
281
|
-
client: 'mysql',
|
|
282
|
-
version: '5.7',
|
|
283
|
-
connection: {
|
|
284
|
-
name: 'db2Connection',
|
|
285
|
-
host: '127.0.0.1',
|
|
286
|
-
user: 'root',
|
|
287
|
-
port: 3307,
|
|
288
|
-
password: 'root',
|
|
289
|
-
database: 'test2',
|
|
290
|
-
},
|
|
291
|
-
migrations: {
|
|
292
|
-
directory: './migrations/posts',
|
|
293
|
-
tableName: '[:name_file_migrations_posts]',
|
|
294
|
-
},
|
|
295
|
-
},
|
|
296
|
-
};
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
Run Migrations:
|
|
300
|
-
|
|
301
|
-
```bash
|
|
302
|
-
npx knex migrate:latest --env development
|
|
303
|
-
|
|
304
|
-
npx knex migrate:latest --env developmentTwo
|
|
305
|
-
```
|
|
306
|
-
|
|
307
|
-
## Create Migrations
|
|
308
|
-
|
|
309
|
-
```bash
|
|
310
|
-
npx knex init
|
|
311
|
-
npx knex migrate:make [name_migrations]
|
|
312
|
-
```
|
|
313
|
-
|
|
314
|
-
Example file migrations:
|
|
315
|
-
|
|
316
|
-
```typescript
|
|
317
|
-
exports.up = function(knex) {
|
|
318
|
-
return knex.schema
|
|
319
|
-
.createTable('users', function(table) {
|
|
320
|
-
table.increments('id');
|
|
321
|
-
table.string('firstName', 255).notNullable();
|
|
322
|
-
table.string('lastName', 255).notNullable();
|
|
323
|
-
})
|
|
324
|
-
.createTable('products', function(table) {
|
|
325
|
-
table.increments('id');
|
|
326
|
-
table.string('name', 255).notNullable();
|
|
327
|
-
table.decimal('price').notNullable();
|
|
328
|
-
});
|
|
329
|
-
};
|
|
330
|
-
|
|
331
|
-
exports.down = function (knex) {
|
|
332
|
-
return knex.schema.dropTable('products').dropTable('users');
|
|
333
|
-
};
|
|
334
|
-
```
|
|
335
|
-
|
|
336
|
-
## Run Migrations
|
|
337
|
-
|
|
338
|
-
In `knexfile.js` update with your config settings:
|
|
339
|
-
|
|
340
|
-
```javascript
|
|
341
|
-
module.exports = {
|
|
342
|
-
development: {
|
|
343
|
-
client: 'mysql',
|
|
344
|
-
connection: {
|
|
345
|
-
host: '127.0.0.1',
|
|
346
|
-
user: 'root',
|
|
347
|
-
password: 'root',
|
|
348
|
-
database: 'test',
|
|
349
|
-
},
|
|
350
|
-
},
|
|
351
|
-
}
|
|
352
|
-
```
|
|
353
|
-
|
|
354
|
-
then we run the following command from the terminal
|
|
355
|
-
|
|
356
|
-
```bash
|
|
357
|
-
npx knex migrate:latest
|
|
358
|
-
```
|
|
359
|
-
|
|
360
|
-
## Create Seeds
|
|
361
|
-
|
|
362
|
-
```bash
|
|
363
|
-
npx knex seed:make [name_seed]
|
|
364
|
-
```
|
|
365
|
-
|
|
366
|
-
Example file seeds
|
|
367
|
-
|
|
368
|
-
```javascript
|
|
369
|
-
exports.seed = function (knex) {
|
|
370
|
-
return knex('users')
|
|
371
|
-
.del()
|
|
372
|
-
.then(function () {
|
|
373
|
-
return knex('users').insert([
|
|
374
|
-
{ id: 1, firstName: 'firstName#1', lastName: 'lastName#1' },
|
|
375
|
-
{ id: 2, firstName: 'firstName#2', lastName: 'lastName#2' },
|
|
376
|
-
{ id: 3, firstName: 'firstName#3', lastName: 'lastName#3' },
|
|
377
|
-
]);
|
|
378
|
-
});
|
|
379
|
-
};
|
|
380
|
-
```
|
|
381
|
-
|
|
382
|
-
## Run Seeds
|
|
383
|
-
|
|
384
|
-
```
|
|
385
|
-
npx knex seed:run
|
|
386
|
-
```
|
|
387
|
-
|
|
388
|
-
For more information on `Knex.js` see [here](http://knexjs.org/)
|
|
389
|
-
|
|
390
|
-
## Contribute
|
|
391
|
-
|
|
392
|
-
Feel free to help this library, I'm quite busy with also another Nestjs packages, but the community will appreciate the effort of improving this library. Make sure you follow the guidelines
|
|
393
|
-
|
|
394
|
-
## License
|
|
395
|
-
|
|
396
|
-
[MIT licensed](LICENSE)
|
|
3
|
+
⚠️ DEPRECATED - This package is no longer maintained. Please use the official knex package instead.
|
package/_README.md
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
# nest-knexjs
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
⚠️ **DEPRECATED** ⚠️ - This package is no longer maintained
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Description
|
|
10
|
+
|
|
11
|
+
Knexjs module for Nest framework (node.js)
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
First install the module via `yarn` or `npm` or `pnpm` and do not forget to install the driver package as well:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm i --save nest-knexjs knex mysql # for mysql/mariadb
|
|
19
|
+
npm i --save nest-knexjs knex pg # for postgresql
|
|
20
|
+
npm i --save nest-knexjs knex sqlite # for sqlite
|
|
21
|
+
npm i --save nest-knexjs knex oracledb # for oracledb
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
or
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
yarn add nest-knexjs knex mysql # for mysql/mariadb
|
|
28
|
+
yarn add nest-knexjs knex pg # for postgresql
|
|
29
|
+
yarn add nest-knexjs knex sqlite # for sqlite
|
|
30
|
+
yarn add nest-knexjs knex oracledb # for oracledb
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
or
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pnpm add nest-knexjs knex mysql # for mysql/mariadb
|
|
37
|
+
pnpm add nest-knexjs knex pg # for postgresql
|
|
38
|
+
pnpm add nest-knexjs knex sqlite # for sqlite
|
|
39
|
+
pnpm add nest-knexjs knex oracledb # for oracledb
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Table of Contents
|
|
43
|
+
|
|
44
|
+
- [nest-knexjs](#nest-knexjs)
|
|
45
|
+
- [Description](#description)
|
|
46
|
+
- [Installation](#installation)
|
|
47
|
+
- [Table of Contents](#table-of-contents)
|
|
48
|
+
- [Usage](#usage)
|
|
49
|
+
- [KnexModule](#knexmodule)
|
|
50
|
+
- [Example of use](#example-of-use)
|
|
51
|
+
- [Multi Connections Database](#multi-connections-database)
|
|
52
|
+
- [Create Migrations](#create-migrations)
|
|
53
|
+
- [Run Migrations](#run-migrations)
|
|
54
|
+
- [Create Seeds](#create-seeds)
|
|
55
|
+
- [Run Seeds](#run-seeds)
|
|
56
|
+
- [Contribute](#contribute)
|
|
57
|
+
- [Stay in touch](#stay-in-touch)
|
|
58
|
+
- [License](#license)
|
|
59
|
+
|
|
60
|
+
## Usage
|
|
61
|
+
|
|
62
|
+
### KnexModule
|
|
63
|
+
|
|
64
|
+
KnexModule is the primary entry point for this package and can be used synchronously
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
@Module({
|
|
68
|
+
imports: [
|
|
69
|
+
KnexModule.forRoot({
|
|
70
|
+
config: {
|
|
71
|
+
client: 'mysql',
|
|
72
|
+
version: '5.7',
|
|
73
|
+
connection: {
|
|
74
|
+
host: '127.0.0.1',
|
|
75
|
+
user: 'root',
|
|
76
|
+
password: 'root',
|
|
77
|
+
database: 'test',
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
}),
|
|
81
|
+
],
|
|
82
|
+
})
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
or asynchronously
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
@Module({
|
|
89
|
+
imports: [
|
|
90
|
+
KnexModule.forRootAsync({
|
|
91
|
+
useFactory: () => ({
|
|
92
|
+
config: {
|
|
93
|
+
client: 'mysql',
|
|
94
|
+
version: '5.7',
|
|
95
|
+
connection: {
|
|
96
|
+
host: '127.0.0.1',
|
|
97
|
+
user: 'root',
|
|
98
|
+
password: 'root',
|
|
99
|
+
database: 'test',
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
}),
|
|
103
|
+
}),
|
|
104
|
+
],
|
|
105
|
+
})
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Example of use
|
|
109
|
+
|
|
110
|
+
UsersService:
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
import { Injectable } from '@nestjs/common';
|
|
114
|
+
import { Knex } from 'knex';
|
|
115
|
+
import { InjectConnection } from 'nest-knexjs';
|
|
116
|
+
|
|
117
|
+
@Injectable()
|
|
118
|
+
export class UsersService {
|
|
119
|
+
constructor(@InjectConnection() private readonly knex: Knex) {}
|
|
120
|
+
|
|
121
|
+
async findAll() {
|
|
122
|
+
const users = await this.knex.table('users');
|
|
123
|
+
return { users };
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
UsersController:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { Controller, Get } from '@nestjs/common';
|
|
132
|
+
import { UsersService } from './users.service';
|
|
133
|
+
|
|
134
|
+
@Controller()
|
|
135
|
+
export class UsersController {
|
|
136
|
+
constructor(private readonly usersService: UsersService) {}
|
|
137
|
+
|
|
138
|
+
@Get()
|
|
139
|
+
async getAllUsers() {
|
|
140
|
+
return await this.usersService.findAll();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Multi Connections Database
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
@Module({
|
|
149
|
+
imports: [
|
|
150
|
+
KnexModule.forRootAsync(
|
|
151
|
+
{
|
|
152
|
+
useFactory: () => ({
|
|
153
|
+
config: {
|
|
154
|
+
client: 'mysql',
|
|
155
|
+
version: '5.7',
|
|
156
|
+
useNullAsDefault: true,
|
|
157
|
+
connection: {
|
|
158
|
+
host: '127.0.0.1',
|
|
159
|
+
user: 'root',
|
|
160
|
+
port: 3306,
|
|
161
|
+
password: 'root',
|
|
162
|
+
database: 'test1',
|
|
163
|
+
},
|
|
164
|
+
pool: {
|
|
165
|
+
min: 2,
|
|
166
|
+
max: 10,
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
}),
|
|
170
|
+
},
|
|
171
|
+
'db1Connection',
|
|
172
|
+
),
|
|
173
|
+
KnexModule.forRootAsync(
|
|
174
|
+
{
|
|
175
|
+
useFactory: () => ({
|
|
176
|
+
config: {
|
|
177
|
+
client: 'mysql',
|
|
178
|
+
version: '5.7',
|
|
179
|
+
useNullAsDefault: true,
|
|
180
|
+
connection: {
|
|
181
|
+
host: '127.0.0.1',
|
|
182
|
+
user: 'root',
|
|
183
|
+
port: 3307,
|
|
184
|
+
password: 'root',
|
|
185
|
+
database: 'test2',
|
|
186
|
+
},
|
|
187
|
+
pool: {
|
|
188
|
+
min: 2,
|
|
189
|
+
max: 10,
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
}),
|
|
193
|
+
},
|
|
194
|
+
'db2Connection',
|
|
195
|
+
),
|
|
196
|
+
],
|
|
197
|
+
controllers: [],
|
|
198
|
+
providers: [],
|
|
199
|
+
})
|
|
200
|
+
export class AppModule {}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Usage example with Multi Connection
|
|
204
|
+
|
|
205
|
+
PostService:
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
@Injectable()
|
|
209
|
+
export class PostService {
|
|
210
|
+
constructor(
|
|
211
|
+
@InjectConnection('db2Connection')
|
|
212
|
+
private knexConnection: Knex,
|
|
213
|
+
) {}
|
|
214
|
+
|
|
215
|
+
async create(createPostDto: CreatePostDto) {
|
|
216
|
+
try {
|
|
217
|
+
const post = await this.knexConnection.table('posts').insert({
|
|
218
|
+
title: createPostDto.title,
|
|
219
|
+
description: createPostDto.description,
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
return post;
|
|
223
|
+
} catch (err) {
|
|
224
|
+
throw new HttpException(err, HttpStatus.BAD_REQUEST);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async findAll() {
|
|
229
|
+
const posts = await this.knexConnection.table('posts');
|
|
230
|
+
return { posts };
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
UsersService:
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
@Injectable()
|
|
239
|
+
export class UsersService {
|
|
240
|
+
constructor(
|
|
241
|
+
@InjectConnection('db1Connection')
|
|
242
|
+
private knexConnection: Knex,
|
|
243
|
+
) {}
|
|
244
|
+
|
|
245
|
+
async findAll() {
|
|
246
|
+
const users = await this.knexConnection.table('users');
|
|
247
|
+
return { users };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async create(createUserDto: CreateUserDto) {
|
|
251
|
+
try {
|
|
252
|
+
const users = await this.knexConnection.table('users').insert({
|
|
253
|
+
firstName: createUserDto.firstName,
|
|
254
|
+
lastName: createUserDto.lastName,
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
return { users };
|
|
258
|
+
} catch (err) {
|
|
259
|
+
throw new HttpException(err, HttpStatus.BAD_REQUEST);
|
|
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
|
+
|
|
312
|
+
## Create Migrations
|
|
313
|
+
|
|
314
|
+
```bash
|
|
315
|
+
npx knex init
|
|
316
|
+
npx knex migrate:make [name_migrations]
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
Example file migrations:
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
exports.up = function(knex) {
|
|
323
|
+
return knex.schema
|
|
324
|
+
.createTable('users', function(table) {
|
|
325
|
+
table.increments('id');
|
|
326
|
+
table.string('firstName', 255).notNullable();
|
|
327
|
+
table.string('lastName', 255).notNullable();
|
|
328
|
+
})
|
|
329
|
+
.createTable('products', function(table) {
|
|
330
|
+
table.increments('id');
|
|
331
|
+
table.string('name', 255).notNullable();
|
|
332
|
+
table.decimal('price').notNullable();
|
|
333
|
+
});
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
exports.down = function (knex) {
|
|
337
|
+
return knex.schema.dropTable('products').dropTable('users');
|
|
338
|
+
};
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
## Run Migrations
|
|
342
|
+
|
|
343
|
+
In `knexfile.js` update with your config settings:
|
|
344
|
+
|
|
345
|
+
```javascript
|
|
346
|
+
module.exports = {
|
|
347
|
+
development: {
|
|
348
|
+
client: 'mysql',
|
|
349
|
+
connection: {
|
|
350
|
+
host: '127.0.0.1',
|
|
351
|
+
user: 'root',
|
|
352
|
+
password: 'root',
|
|
353
|
+
database: 'test',
|
|
354
|
+
},
|
|
355
|
+
},
|
|
356
|
+
}
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
then we run the following command from the terminal
|
|
360
|
+
|
|
361
|
+
```bash
|
|
362
|
+
npx knex migrate:latest
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
## Create Seeds
|
|
366
|
+
|
|
367
|
+
```bash
|
|
368
|
+
npx knex seed:make [name_seed]
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
Example file seeds
|
|
372
|
+
|
|
373
|
+
```javascript
|
|
374
|
+
exports.seed = function (knex) {
|
|
375
|
+
return knex('users')
|
|
376
|
+
.del()
|
|
377
|
+
.then(function () {
|
|
378
|
+
return knex('users').insert([
|
|
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' },
|
|
382
|
+
]);
|
|
383
|
+
});
|
|
384
|
+
};
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
## Run Seeds
|
|
388
|
+
|
|
389
|
+
```
|
|
390
|
+
npx knex seed:run
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
For more information on `Knex.js` see [here](http://knexjs.org/)
|
|
394
|
+
|
|
395
|
+
## Contribute
|
|
396
|
+
|
|
397
|
+
Feel free to help this library, I'm quite busy with also another Nestjs packages, but the community will appreciate the effort of improving this library. Make sure you follow the guidelines
|
|
398
|
+
|
|
399
|
+
## Stay in touch
|
|
400
|
+
|
|
401
|
+
- Author - [Tony133](https://github.com/Tony133)
|
|
402
|
+
- Framework - [https://nestjs.com](https://nestjs.com/)
|
|
403
|
+
|
|
404
|
+
## License
|
|
405
|
+
|
|
406
|
+
[MIT licensed](LICENSE)
|
package/package.json
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nest-knexjs",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
5
|
-
"license": "MIT",
|
|
3
|
+
"version": "0.0.32",
|
|
4
|
+
"description": "⚠️ DEPRECATED - This package is no longer maintained. Please use the official knex package instead.",
|
|
6
5
|
"scripts": {
|
|
7
6
|
"build": "rm -rf dist && tsc -p tsconfig.json",
|
|
8
7
|
"format": "prettier --write \"**/*.ts\"",
|