amqp-extension 0.0.1
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/.editorconfig +13 -0
- package/LICENSE +21 -0
- package/README.MD +611 -0
- package/dist/connection.d.ts +2 -0
- package/dist/connection.js +62 -0
- package/dist/connection.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/package.json +32 -0
package/.editorconfig
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Peter Placzek
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.MD
ADDED
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
[](https://badge.fury.io/js/amqp-extension)
|
|
2
|
+
[](https://codecov.io/gh/Tada5hi/amqp-extension)
|
|
3
|
+
[](https://github.com/Tada5hi/amqp-extension)
|
|
4
|
+
[](https://snyk.io/test/github/Tada5hi/amqp-extension?targetFile=package.json)
|
|
5
|
+
|
|
6
|
+
# AMQP Extension 🚀
|
|
7
|
+
|
|
8
|
+
This is a library on top of the famous `amqplib` and specifies a specific mesasge format for queue message across multiple standalone services.
|
|
9
|
+
|
|
10
|
+
**Table of Contents**
|
|
11
|
+
|
|
12
|
+
- [Installation](#installation)
|
|
13
|
+
- [Usage](#usage)
|
|
14
|
+
- Functions(#functions)
|
|
15
|
+
- [Connection](#functions---connection)
|
|
16
|
+
- [createDatabase](#createConnection)
|
|
17
|
+
- [dropDatabase](#useConnection)
|
|
18
|
+
- [Query](#functions---query)
|
|
19
|
+
- [applyFields](#applyfields)
|
|
20
|
+
- [applyFilters](#applyfilters)
|
|
21
|
+
- [applyIncludes](#applyincludes)
|
|
22
|
+
- [applyPagination](#applypagination)
|
|
23
|
+
- [applySort](#applysort)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install amqp-extension --save
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Limitations
|
|
35
|
+
|
|
36
|
+
At the moment you can only `create` and `drop` a database for one of the following typeorm drivers:
|
|
37
|
+
* CockroachDB
|
|
38
|
+
* MSSQL
|
|
39
|
+
* MySQL
|
|
40
|
+
* Oracle
|
|
41
|
+
* Postgres
|
|
42
|
+
* SQLite
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
### CLI
|
|
47
|
+
|
|
48
|
+
You can use the following commands in the terminal:
|
|
49
|
+
- `amqp-extension db:create` to create the database
|
|
50
|
+
- `amqp-extension db:drop` to drop the database
|
|
51
|
+
|
|
52
|
+
Alternatively, you can set the full command path in the package.json file and run it f.e. with ts-node.
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
"scripts": {
|
|
56
|
+
...
|
|
57
|
+
"db:create": "ts-node ./node_modules/amqp-extension/dist/cli/index.js db:create",
|
|
58
|
+
"db:drop": "ts-node ./node_modules/amqp-extension/dist/cli/index.js db:drop",
|
|
59
|
+
...
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
#### CLI Options
|
|
64
|
+
|
|
65
|
+
| Option | Default | Description |
|
|
66
|
+
| ---------------------- | --------------- | --------------------------------------------------------------------------- |
|
|
67
|
+
| `--connection` or `-c` | `default` | Name of the typeorm connection. Required if there are multiple connections. |
|
|
68
|
+
| `--config` or `-f` | `ormconfig.js` | Name to the typeorm config file. |
|
|
69
|
+
| `--root` or `-r` | `process.cwd()` | Path to the typeorm config file. |
|
|
70
|
+
| `--synchronize` or `-s`| `yes` | Synchronize the database schema after database creation. This option is only available for the create command. Options: `yes` or `no`. |
|
|
71
|
+
| `--initialDatabase` | `undefined` | Specify the initial database to connect to. This option is only available for the create command and is only relevant for the `postgres` driver, where you always have to connect to a database. If you don't provide a database, the database name will be equal to the connection user name.|
|
|
72
|
+
|
|
73
|
+
### Runtime
|
|
74
|
+
|
|
75
|
+
#### Database
|
|
76
|
+
As an alternative to the CLI variant, you can `create` or `drop` the specified/default database through your own code base.
|
|
77
|
+
Therefore, you can create the `ConnectionOptions` for the Connection manually, or let it be created automatically:
|
|
78
|
+
|
|
79
|
+
**General**
|
|
80
|
+
```typescript
|
|
81
|
+
import {ConnectionOptions, getConnectionOptions} from 'typeorm';
|
|
82
|
+
import {createDatabase, dropDatabase} from "amqp-extension";
|
|
83
|
+
|
|
84
|
+
(async () => {
|
|
85
|
+
const connectionOptions: ConnectionOptions = await getConnectionOptions();
|
|
86
|
+
|
|
87
|
+
// Create database with connection specification
|
|
88
|
+
await createDatabase(undefined, connectionOptions);
|
|
89
|
+
|
|
90
|
+
// or without manually specification
|
|
91
|
+
await createDatabase({ifNotExist: true});
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
// Drop Database with connection specification
|
|
95
|
+
await dropDatabase(undefined, connectionOptions);
|
|
96
|
+
|
|
97
|
+
// or without manually specification
|
|
98
|
+
await dropDatabase({ifExist: true});
|
|
99
|
+
})();
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
To get a better overview and understanding about the [createDatabase](#createdatabase) and the [dropDatabase](#dropdatabase) function go to the [database functions](#functions---database) section and read more about it.
|
|
103
|
+
|
|
104
|
+
#### Query
|
|
105
|
+
For explanation proposes of the query utils,
|
|
106
|
+
two simple entities with a simple relation between them are declared to demonstrate their usage:
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import {
|
|
110
|
+
Entity,
|
|
111
|
+
PrimaryGeneratedColumn,
|
|
112
|
+
Column,
|
|
113
|
+
OneToOne,
|
|
114
|
+
JoinColumn
|
|
115
|
+
} from "typeorm";
|
|
116
|
+
|
|
117
|
+
@Entity()
|
|
118
|
+
export class User {
|
|
119
|
+
@PrimaryGeneratedColumn({unsigned: true})
|
|
120
|
+
id: number;
|
|
121
|
+
|
|
122
|
+
@Column({type: 'varchar', length: 30})
|
|
123
|
+
@Index({unique: true})
|
|
124
|
+
name: string;
|
|
125
|
+
|
|
126
|
+
@Column({type: 'varchar', length: 255, default: null, nullable: true})
|
|
127
|
+
email: string;
|
|
128
|
+
|
|
129
|
+
@OneToOne(() => Profile)
|
|
130
|
+
profile: Profile;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
@Entity()
|
|
134
|
+
export class Profile {
|
|
135
|
+
@PrimaryGeneratedColumn({unsigned: true})
|
|
136
|
+
id: number;
|
|
137
|
+
|
|
138
|
+
@Column({type: 'varchar', length: 255, default: null, nullable: true})
|
|
139
|
+
avatar: string;
|
|
140
|
+
|
|
141
|
+
@Column({type: 'varchar', length: 255, default: null, nullable: true})
|
|
142
|
+
cover: string;
|
|
143
|
+
|
|
144
|
+
@OneToOne(() => User)
|
|
145
|
+
@JoinColumn()
|
|
146
|
+
user: User;
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
When you use express or another library, you can use the API utils accordingly to the
|
|
151
|
+
following code snippet:
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
import {getRepository} from "typeorm";
|
|
155
|
+
import {Request, Response} from 'express';
|
|
156
|
+
import {
|
|
157
|
+
applyFields,
|
|
158
|
+
applyFilters,
|
|
159
|
+
applyIncludes,
|
|
160
|
+
applyPagination,
|
|
161
|
+
applySort
|
|
162
|
+
} from "amqp-extension";
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Get many users.
|
|
166
|
+
*
|
|
167
|
+
* Request example
|
|
168
|
+
* - url: /users?page[limit]=10&page[offset]=0&include=profile&filter[id]=1&fields[user]=id,name
|
|
169
|
+
*
|
|
170
|
+
* Return Example:
|
|
171
|
+
* {
|
|
172
|
+
* data: [
|
|
173
|
+
* {id: 1, name: 'tada5hi', profile: {avatar: 'avatar.jpg', cover: 'cover.jpg'}}
|
|
174
|
+
* ],
|
|
175
|
+
* meta: {
|
|
176
|
+
* total: 1,
|
|
177
|
+
* limit: 20,
|
|
178
|
+
* offset: 0
|
|
179
|
+
* }
|
|
180
|
+
* }
|
|
181
|
+
* @param req
|
|
182
|
+
* @param res
|
|
183
|
+
*/
|
|
184
|
+
export async function getUsers(req: Request, res: Response) {
|
|
185
|
+
const {fields, filter, include, page, sort} = req.query;
|
|
186
|
+
|
|
187
|
+
const repository = getRepository(User);
|
|
188
|
+
const query = repository.createQueryBuilder('user');
|
|
189
|
+
|
|
190
|
+
// -----------------------------------------------------
|
|
191
|
+
|
|
192
|
+
const includes = applyIncludes(query, include, {
|
|
193
|
+
queryAlias: 'user',
|
|
194
|
+
allowed: ['profile']
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
applySort(query, sort, {
|
|
198
|
+
queryAlias: 'user',
|
|
199
|
+
allowed: ['id', 'name', 'profile.id'],
|
|
200
|
+
// profile.id can only be used as sorting key, if the relation 'profile' is included.
|
|
201
|
+
includes: includes
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
applyFields(query, fields, {
|
|
205
|
+
queryAlias: 'user',
|
|
206
|
+
allowed: ['id', 'name', 'profile.id', 'profile.avatar'],
|
|
207
|
+
// porfile fields can only be included, if the relation 'profile' is included.
|
|
208
|
+
includes: includes
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
// only allow filtering users by id & name
|
|
212
|
+
applyFilters(query, filter, {
|
|
213
|
+
queryAlias: 'user',
|
|
214
|
+
allowed: ['id', 'name', 'profile.id'],
|
|
215
|
+
// porfile.id can only be used as a filter, if the relation 'profile' is included.
|
|
216
|
+
includes: includes
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// only allow to select 20 items at maximum.
|
|
220
|
+
const pagination = applyPagination(query, page, {maxLimit: 20});
|
|
221
|
+
|
|
222
|
+
// -----------------------------------------------------
|
|
223
|
+
|
|
224
|
+
const [entities, total] = await query.getManyAndCount();
|
|
225
|
+
|
|
226
|
+
return res.json({
|
|
227
|
+
data: {
|
|
228
|
+
data: entities,
|
|
229
|
+
meta: {
|
|
230
|
+
total,
|
|
231
|
+
...pagination
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
```
|
|
237
|
+
To get a better overview and understanding about the [applyFields](#applyfields), [applyFilters](#applyfilters), [applyIncludes](#applyincludes), [applyPagination](#applypagination)
|
|
238
|
+
and [applySort](#applysort) function go to the [query functions](#functions---query) section and read more about it.
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
## Functions - Database
|
|
242
|
+
|
|
243
|
+
### createDatabase
|
|
244
|
+
|
|
245
|
+
â–¸ `function` **createDatabase**(`options?: CustomOptions`, `connectionOptions?: ConnectionOptions`): `Promise`<`unknown`>
|
|
246
|
+
|
|
247
|
+
Create database.
|
|
248
|
+
|
|
249
|
+
#### Example
|
|
250
|
+
|
|
251
|
+
**`Status`**
|
|
252
|
+
```typescript
|
|
253
|
+
// Only create database if it does not exists.
|
|
254
|
+
await createDatabase({ifNotExist: true});
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
**`Charset`**
|
|
258
|
+
|
|
259
|
+
You can also specify the `charset` and `characterSet` as property of the `CustomOptions` parameter
|
|
260
|
+
or parse it as extra parameter (ENV: TYPEORM_DRIVER_EXTRA) of the ormconfig.
|
|
261
|
+
F.e
|
|
262
|
+
- postgres
|
|
263
|
+
```typescript
|
|
264
|
+
await createDatabase({ifNotExist: true, characterSet: "UTF8"});
|
|
265
|
+
```
|
|
266
|
+
- mysql
|
|
267
|
+
```typescript
|
|
268
|
+
await createDatabase({ifNotExist: true, charset: "utf8mb4_general_ci", characterSet: "utf8mb4"});
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
**`ConnectionOptions`**
|
|
272
|
+
|
|
273
|
+
If you have defined `entites` or `subscribers` by environment variables or with another typeorm configuration option,
|
|
274
|
+
you may want to use them with ts-node as well with the build (`dist`) variant.
|
|
275
|
+
Therefore, you can build the ConnectionOptions with the build in function `buildConnectionOptions`.
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
import {craeteDatabase, dropDatabase, buildConnectionOptions} from "amqp-extension";
|
|
279
|
+
|
|
280
|
+
(async () => {
|
|
281
|
+
const connectionOptions = await buildConnectionOptions();
|
|
282
|
+
// Create database
|
|
283
|
+
await createDatabase({ifNotExist: true}, connectionOptions);
|
|
284
|
+
})();
|
|
285
|
+
```
|
|
286
|
+
If you have specified the path pattern for the entities like:
|
|
287
|
+
`src/database/entities.ts`, the function will rewrite it to `dist/database/entities.js`,
|
|
288
|
+
in case the function is not called within the ts-node runtime environment.
|
|
289
|
+
|
|
290
|
+
The same logic applies to `seeds` and `factories` path(s) of the `typeorm-seeding` library.
|
|
291
|
+
|
|
292
|
+
#### Parameters
|
|
293
|
+
|
|
294
|
+
| Name | Type | Description |
|
|
295
|
+
| :------ | :------ | :------ |
|
|
296
|
+
| `options` | `CustomOptions` | Specify custom options like charset, coalition & conditions. |
|
|
297
|
+
| `connectionOptions` | `ConnectionOptions` | Pass typeorm connection options object. |
|
|
298
|
+
|
|
299
|
+
#### Returns
|
|
300
|
+
|
|
301
|
+
`Promise`<`unknown`>
|
|
302
|
+
|
|
303
|
+
The function returns a promise with query results provided by underlying db driver.
|
|
304
|
+
|
|
305
|
+
### dropDatabase
|
|
306
|
+
|
|
307
|
+
â–¸ `function` **dropDatabase**(`options?: CustomOptions`, `connectionOptions?: ConnectionOptions`): `Promise`<`unknown`>
|
|
308
|
+
|
|
309
|
+
Drop database.
|
|
310
|
+
|
|
311
|
+
#### Example
|
|
312
|
+
**`Simple`**
|
|
313
|
+
```typescript
|
|
314
|
+
// Only drop database if it does exists.
|
|
315
|
+
await dropDatabase({ifExist: true});
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
#### Parameters
|
|
319
|
+
|
|
320
|
+
| Name | Type | Description |
|
|
321
|
+
| :------ | :------ | :------ |
|
|
322
|
+
| `options` | `CustomOptions` | Specify custom options like charset, coalition & conditions. |
|
|
323
|
+
| `connectionOptions` | `ConnectionOptions` | Pass typeorm connection options object. |
|
|
324
|
+
|
|
325
|
+
#### Returns
|
|
326
|
+
|
|
327
|
+
`Promise`<`unknown`>
|
|
328
|
+
|
|
329
|
+
The function returns a promise with query results provided by underlying db driver.
|
|
330
|
+
|
|
331
|
+
## Functions - Query
|
|
332
|
+
|
|
333
|
+
### applyFields
|
|
334
|
+
|
|
335
|
+
â–¸ `function` **applyFields**<`T`>(`query: SelectQueryBuilder<T>`, `data: unknown`, `options?: FieldsOptions`): `FieldsTransformed`
|
|
336
|
+
|
|
337
|
+
Transform fields of the main entity and optional of included relations passed in as `Record<string, string[]>` or `string[]` and apply them to the `SelectQueryBuilder`, in case they match the allowed fields.
|
|
338
|
+
|
|
339
|
+
#### Example
|
|
340
|
+
**`Simple`**
|
|
341
|
+
|
|
342
|
+
```typescript
|
|
343
|
+
// Only drop database if it does exists.
|
|
344
|
+
import {applyFields} from "amqp-extension";
|
|
345
|
+
|
|
346
|
+
const fields = applyFields(query, ['name'], {
|
|
347
|
+
allowed: ['id', 'name'],
|
|
348
|
+
queryAlias: 'user'
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
console.log(fields);
|
|
352
|
+
// [{alias: 'user', fields: ['name']}]
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
#### Type parameters
|
|
356
|
+
|
|
357
|
+
| Name | Description |
|
|
358
|
+
| :------ | :------ |
|
|
359
|
+
| `T` | Typeorm entity type
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
#### Parameters
|
|
363
|
+
|
|
364
|
+
| Name | Type | Description |
|
|
365
|
+
| :------ | :------ | :------ |
|
|
366
|
+
| `query` | `SelectQueryBuilder`<`T`> | Typeorm SelectQueryBuilder Class. |
|
|
367
|
+
| `data` | `unknown` | Fields in raw format. F.e `['name']` or `{user: ['name']}`. |
|
|
368
|
+
| `options` | `FieldsOptions` | Options for the fields to select.
|
|
369
|
+
|
|
370
|
+
#### Returns
|
|
371
|
+
|
|
372
|
+
`FieldsTransformed`
|
|
373
|
+
|
|
374
|
+
The function returns an array of objects. Each object has the properties `fields` and optional `alias` and `addFields`.
|
|
375
|
+
|
|
376
|
+
### applyFilters
|
|
377
|
+
|
|
378
|
+
â–¸ `function` **applyFilters**<`T`>(`query: SelectQueryBuilder<T>`, `data: unknown`, `options?: FiltersOptions`): `FiltersTransformed`
|
|
379
|
+
|
|
380
|
+
Transform filters of the main entity and optional of included relations passed in as `Record<string, unknown>` and apply them to the `SelectQueryBuilder`, in case they match the allowed filters.
|
|
381
|
+
|
|
382
|
+
#### Example
|
|
383
|
+
**`Simple`**
|
|
384
|
+
|
|
385
|
+
```typescript
|
|
386
|
+
// Only drop database if it does exists.
|
|
387
|
+
import {applyFilters} from "amqp-extension";
|
|
388
|
+
|
|
389
|
+
const filters = applyFilters(query, {id: 1}, {
|
|
390
|
+
allowed: ['id', 'name'],
|
|
391
|
+
queryAlias: 'user'
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
console.log(filters);
|
|
395
|
+
// [{statement: 'user.id = :filter_id', binding: {'filter_id': 1}}]
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
#### Type parameters
|
|
399
|
+
|
|
400
|
+
| Name | Description |
|
|
401
|
+
| :------ | :------ |
|
|
402
|
+
| `T` | Typeorm entity type
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
#### Parameters
|
|
406
|
+
|
|
407
|
+
| Name | Type | Description |
|
|
408
|
+
| :------ | :------ | :------ |
|
|
409
|
+
| `query` | `SelectQueryBuilder`<`T`> | Typeorm SelectQueryBuilder Class. |
|
|
410
|
+
| `data` | `unknown` | Fields in raw format. F.e `{id: 1}`. |
|
|
411
|
+
| `options` | `FiltersOptions` | Options for the fields to select.
|
|
412
|
+
|
|
413
|
+
#### Returns
|
|
414
|
+
|
|
415
|
+
`FiltersTransformed`
|
|
416
|
+
|
|
417
|
+
The function returns an array of objects. Each object has the properties `statement` and `binding`.
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
### applyIncludes
|
|
421
|
+
|
|
422
|
+
â–¸ `function` **applyIncludes**<`T`>(`query: SelectQueryBuilder<T>`, `data: unknown`, `options?: IncludesOptions`): `IncludesTransformed`
|
|
423
|
+
|
|
424
|
+
Transform relations passed in as `string`, `string[]` and apply them to the `SelectQueryBuilder`, in case they match the allowed relations.
|
|
425
|
+
|
|
426
|
+
#### Example
|
|
427
|
+
**`Simple`**
|
|
428
|
+
|
|
429
|
+
```typescript
|
|
430
|
+
// Only drop database if it does exists.
|
|
431
|
+
import {applyIncludes} from "amqp-extension";
|
|
432
|
+
|
|
433
|
+
const includes = applyIncludes(query, ['roles'], {
|
|
434
|
+
allowed: ['roles', 'photos'],
|
|
435
|
+
queryAlias: 'user'
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
console.log(includes);
|
|
439
|
+
// [{property: 'user.roles', alias: 'roles'}]
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
#### Type parameters
|
|
443
|
+
|
|
444
|
+
| Name | Description |
|
|
445
|
+
| :------ | :------ |
|
|
446
|
+
| `T` | Typeorm entity type
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
#### Parameters
|
|
450
|
+
|
|
451
|
+
| Name | Type | Description |
|
|
452
|
+
| :------ | :------ | :------ |
|
|
453
|
+
| `query` | `SelectQueryBuilder`<`T`> | Typeorm SelectQueryBuilder Class. |
|
|
454
|
+
| `data` | `unknown` | Relations in raw format. F.e `['roles']` or `roles` |
|
|
455
|
+
| `options` | `IncludesOptions` | Options for the relations to include.
|
|
456
|
+
|
|
457
|
+
#### Returns
|
|
458
|
+
|
|
459
|
+
`IncludesTransformed`
|
|
460
|
+
|
|
461
|
+
The function returns an array of objects. Each object has the properties `property` and `alias`.
|
|
462
|
+
|
|
463
|
+
### applyPagination
|
|
464
|
+
|
|
465
|
+
â–¸ `function` **applyPagination**<`T`>(`query: SelectQueryBuilder<T>`, `data: unknown`, `options?: PaginationOptions`): `PaginationTransformed`
|
|
466
|
+
|
|
467
|
+
Transform pagination data passed in as `{limit?: number, offset?: number}` and apply it to the `SelectQueryBuilder`.
|
|
468
|
+
|
|
469
|
+
#### Example
|
|
470
|
+
**`Simple`**
|
|
471
|
+
|
|
472
|
+
```typescript
|
|
473
|
+
// Only drop database if it does exists.
|
|
474
|
+
import {applyPagination} from "amqp-extension";
|
|
475
|
+
|
|
476
|
+
const pagination = applyPagination(query, {limit: 100}, {
|
|
477
|
+
maxLimit: 50
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
console.log(pagination);
|
|
481
|
+
// {limit: 50}
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
#### Type parameters
|
|
485
|
+
|
|
486
|
+
| Name | Description |
|
|
487
|
+
| :------ | :------ |
|
|
488
|
+
| `T` | Typeorm entity type
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
#### Parameters
|
|
492
|
+
|
|
493
|
+
| Name | Type | Description |
|
|
494
|
+
| :------ | :------ | :------ |
|
|
495
|
+
| `query` | `SelectQueryBuilder`<`T`> | Typeorm SelectQueryBuilder Class. |
|
|
496
|
+
| `data` | `unknown` | Pagination data in raw format. F.e `{limit: 20, offset: 10}`. |
|
|
497
|
+
| `options` | `PaginationOptions` | Options for the pagination to select.
|
|
498
|
+
|
|
499
|
+
#### Returns
|
|
500
|
+
|
|
501
|
+
`PaginationTransformed`
|
|
502
|
+
|
|
503
|
+
The function returns an object. The object might have the properties `limit` and `offset`.
|
|
504
|
+
|
|
505
|
+
### applySort
|
|
506
|
+
|
|
507
|
+
â–¸ `function` **applySort**<`T`>(`query: SelectQueryBuilder<T>`, `data: unknown`, `options?: SortOptions`): `SortTransformed`
|
|
508
|
+
|
|
509
|
+
Transform sort fields passed in as `string`, `string[]` and apply them to the `SelectQueryBuilder`, in case they match the allowed fields to sort.
|
|
510
|
+
|
|
511
|
+
#### Example
|
|
512
|
+
**`Simple`**
|
|
513
|
+
|
|
514
|
+
```typescript
|
|
515
|
+
// Only drop database if it does exists.
|
|
516
|
+
import {applySort} from "amqp-extension";
|
|
517
|
+
|
|
518
|
+
const sort = applySort(query, ['-name'], {
|
|
519
|
+
allowed: ['id', 'name'],
|
|
520
|
+
queryAlias: 'user'
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
console.log(sort);
|
|
524
|
+
// {'user.name': 'DESC'}
|
|
525
|
+
```
|
|
526
|
+
|
|
527
|
+
#### Type parameters
|
|
528
|
+
|
|
529
|
+
| Name | Description |
|
|
530
|
+
| :------ | :------ |
|
|
531
|
+
| `T` | Typeorm entity type
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
#### Parameters
|
|
535
|
+
|
|
536
|
+
| Name | Type | Description |
|
|
537
|
+
| :------ | :------ | :------ |
|
|
538
|
+
| `query` | `SelectQueryBuilder`<`T`> | Typeorm SelectQueryBuilder Class. |
|
|
539
|
+
| `data` | `unknown` | Sorting Fields in raw format. F.e `['-name']`, `-name` or `{name: 'DESC'}`. The hyphen prefix indicates descending order. |
|
|
540
|
+
| `options` | `SortOptions` | Options for the sorting strategy.
|
|
541
|
+
|
|
542
|
+
#### Returns
|
|
543
|
+
|
|
544
|
+
`SortTransformed`
|
|
545
|
+
|
|
546
|
+
The function returns an objects. Each key-value pair represents a field and the corresponding sorting direction.
|
|
547
|
+
|
|
548
|
+
## Types
|
|
549
|
+
|
|
550
|
+
### Types - Database
|
|
551
|
+
|
|
552
|
+
#### CustomOptions
|
|
553
|
+
```typescript
|
|
554
|
+
type CustomOptions = {
|
|
555
|
+
characterSet?: string,
|
|
556
|
+
charset?: string,
|
|
557
|
+
ifExist?: boolean,
|
|
558
|
+
ifNotExist?: boolean,
|
|
559
|
+
initialDatabase?: string
|
|
560
|
+
};
|
|
561
|
+
```
|
|
562
|
+
|
|
563
|
+
### Types - Query
|
|
564
|
+
|
|
565
|
+
#### FieldsOptions
|
|
566
|
+
```typescript
|
|
567
|
+
type FieldsOptions = {
|
|
568
|
+
aliasMapping?: Record<string, string>,
|
|
569
|
+
allowed?: Record<string, string[]> | string[],
|
|
570
|
+
includes?: IncludesTransformed,
|
|
571
|
+
queryAlias?: string
|
|
572
|
+
};
|
|
573
|
+
```
|
|
574
|
+
|
|
575
|
+
#### FiltersOptions
|
|
576
|
+
```typescript
|
|
577
|
+
export type FiltersOptions = {
|
|
578
|
+
aliasMapping?: Record<string, string>,
|
|
579
|
+
allowed?: string[],
|
|
580
|
+
includes?: IncludesTransformed,
|
|
581
|
+
queryAlias?: string,
|
|
582
|
+
queryBindingKeyFn?: (key: string) => string
|
|
583
|
+
};
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
#### IncludesOptions
|
|
587
|
+
```typescript
|
|
588
|
+
type IncludesOptions = {
|
|
589
|
+
aliasMapping?: Record<string, string>,
|
|
590
|
+
allowed?: string[],
|
|
591
|
+
includeParents?: boolean | string[] | string
|
|
592
|
+
queryAlias?: string,
|
|
593
|
+
};
|
|
594
|
+
```
|
|
595
|
+
|
|
596
|
+
#### PaginationOptions
|
|
597
|
+
```typescript
|
|
598
|
+
type PaginationOptions = {
|
|
599
|
+
maxLimit?: number
|
|
600
|
+
};
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
#### SortOptions
|
|
604
|
+
```typescript
|
|
605
|
+
type SortOptions = {
|
|
606
|
+
aliasMapping?: Record<string, string>,
|
|
607
|
+
allowed?: string[] | string[][],
|
|
608
|
+
includes?: IncludesTransformed,
|
|
609
|
+
queryAlias?: string
|
|
610
|
+
};
|
|
611
|
+
```
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.useMessageQueue = void 0;
|
|
43
|
+
var amqplib_1 = __importDefault(require("amqplib"));
|
|
44
|
+
var connection;
|
|
45
|
+
function useMessageQueue(connectionStr) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
47
|
+
return __generator(this, function (_a) {
|
|
48
|
+
switch (_a.label) {
|
|
49
|
+
case 0:
|
|
50
|
+
if (typeof connection !== 'undefined') {
|
|
51
|
+
return [2 /*return*/, connection];
|
|
52
|
+
}
|
|
53
|
+
return [4 /*yield*/, amqplib_1.default.connect(connectionStr)];
|
|
54
|
+
case 1:
|
|
55
|
+
connection = _a.sent();
|
|
56
|
+
return [2 /*return*/, connection];
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
exports.useMessageQueue = useMessageQueue;
|
|
62
|
+
//# sourceMappingURL=connection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.js","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAA6C;AAE7C,IAAI,UAAmC,CAAC;AAExC,SAAsB,eAAe,CAAC,aAAqB;;;;;oBACvD,IAAG,OAAO,UAAU,KAAK,WAAW,EAAE;wBAClC,sBAAO,UAAU,EAAC;qBACrB;oBAEY,qBAAM,iBAAQ,CAAC,OAAO,CAAC,aAAa,CAAC,EAAA;;oBAAlD,UAAU,GAAG,SAAqC,CAAC;oBACnD,sBAAO,UAAU,EAAC;;;;CACrB;AAPD,0CAOC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './connection';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
__exportStar(require("./connection"), exports);
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,+CAA6B"}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "amqp-extension",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "rm -rf ./dist && tsc",
|
|
8
|
+
"test": "cross-env NODE_ENV=test jest --config ./test/jest.config.js",
|
|
9
|
+
"test:coverage": "cross-env NODE_ENV=test jest --config ./test/jest.config.js --coverage"
|
|
10
|
+
},
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "Peter Placzek",
|
|
13
|
+
"email": "tadashi@animesu.net",
|
|
14
|
+
"url": "https:/github.com/tada5hi"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"amqplib": "^0.8.0"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/amqplib": "^0.8.2",
|
|
22
|
+
"@types/jest": "^27.0.2",
|
|
23
|
+
"@types/node": "^16.0.1",
|
|
24
|
+
"coveralls": "^3.1.0",
|
|
25
|
+
"cross-env": "^7.0.3",
|
|
26
|
+
"jest": "^27.0.5",
|
|
27
|
+
"ts-jest": "^27.0.3",
|
|
28
|
+
"ts-node": "^10.0.0",
|
|
29
|
+
"tslint": "^6.1.3",
|
|
30
|
+
"tslint-config-prettier": "^1.18.0"
|
|
31
|
+
}
|
|
32
|
+
}
|