@nestlize/repository 0.1.13 → 0.1.14
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 +2 -2
- package/index.d.ts +42 -0
- package/index.js +21 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -74,13 +74,13 @@ All methods return Promises.
|
|
|
74
74
|
| `findAll(query?, options?)` | `query?: WhereOptions`, `options?: Omit<FindOptions, 'where'>` | Find all matching records |
|
|
75
75
|
| `findAllPaginated(options?)` | `limit?: number`, `offset?: number`, `page?: number`, `query?: WhereOptions`, `options?: Omit<FindAndCountOptions, 'where' \| 'offset' \| 'limit'>` | Find paginated records and total count |
|
|
76
76
|
| `updateByPk(primaryKey, dto, options?)` | `primaryKey: string \| number`, `dto: Partial<Attributes<TModel>>`, `options?: SaveOptions` | Update record by primary key |
|
|
77
|
+
| `delete(query, options?)` | `query?: WhereOptions`, `options?: Omit<DestroyOptions<Attributes<TModel>>, 'where'>` | Update record by primary key |
|
|
78
|
+
| `restore(query, options?)` | `query?: WhereOptions`, `options?: Omit<RestoreOptions<Attributes<TModel>>, 'where'>` | Update record by primary key |
|
|
77
79
|
| `deleteByPk(primaryKey, options?)` | `primaryKey: string \| number`, `options?: InstanceDestroyOptions` | Delete (soft/hard) record by primary key |
|
|
78
80
|
| `restoreByPk(primaryKey, options?)` | `primaryKey: string \| number`, `options?: InstanceRestoreOptions` | Restore previously soft-deleted record |
|
|
79
81
|
| `transaction(runInTransaction)` | `(transaction: Transaction) => Promise<R>` | Execute callback within a Sequelize transaction |
|
|
80
82
|
| `calculateOffset(limit: number, page: number)` | `limit: number, page: number` | Calculate offset for page pagination |
|
|
81
83
|
|
|
82
|
-
---
|
|
83
|
-
|
|
84
84
|
## 🚀 Quick Start
|
|
85
85
|
|
|
86
86
|
### 1. Define a model
|
package/index.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ import {
|
|
|
11
11
|
FindOptions,
|
|
12
12
|
BulkCreateOptions,
|
|
13
13
|
FindAndCountOptions,
|
|
14
|
+
DestroyOptions,
|
|
15
|
+
RestoreOptions,
|
|
14
16
|
} from 'sequelize'
|
|
15
17
|
import { Model, ModelCtor } from 'sequelize-typescript'
|
|
16
18
|
|
|
@@ -163,6 +165,30 @@ export interface IRepository<TModel extends Model> {
|
|
|
163
165
|
options?: SaveOptions<Attributes<TModel>>,
|
|
164
166
|
): Promise<TModel | null>
|
|
165
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Delete all records matching query
|
|
170
|
+
*
|
|
171
|
+
* @param query A Sequelize where clause.
|
|
172
|
+
* @param options Optional Sequelize destroy options.
|
|
173
|
+
* @returns A Promise represented amount of deleted records.
|
|
174
|
+
*/
|
|
175
|
+
delete(
|
|
176
|
+
query?: WhereOptions<Attributes<TModel>>,
|
|
177
|
+
options?: Omit<DestroyOptions<Attributes<TModel>>, 'where'>,
|
|
178
|
+
): Promise<number>
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Restore all deleted records.
|
|
182
|
+
*
|
|
183
|
+
* @param query A Sequelize where clause.
|
|
184
|
+
* @param options Optional Sequelize restore options.
|
|
185
|
+
* @returns Promise<void>
|
|
186
|
+
*/
|
|
187
|
+
restore(
|
|
188
|
+
query?: WhereOptions<Attributes<TModel>>,
|
|
189
|
+
options?: Omit<RestoreOptions<Attributes<TModel>>, 'where'>,
|
|
190
|
+
): Promise<void>
|
|
191
|
+
|
|
166
192
|
/**
|
|
167
193
|
* Deletes (soft or hard) a record by its primary key.
|
|
168
194
|
*
|
|
@@ -322,6 +348,22 @@ export declare class AbstractRepository<
|
|
|
322
348
|
options?: SaveOptions<Attributes<TModel>>,
|
|
323
349
|
): Promise<TModel | null>
|
|
324
350
|
|
|
351
|
+
/**
|
|
352
|
+
* @inheritdoc
|
|
353
|
+
*/
|
|
354
|
+
delete(
|
|
355
|
+
query?: WhereOptions<Attributes<TModel>>,
|
|
356
|
+
options?: Omit<DestroyOptions<Attributes<TModel>>, 'where'>,
|
|
357
|
+
): Promise<number>
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* @inheritdoc
|
|
361
|
+
*/
|
|
362
|
+
restore(
|
|
363
|
+
query?: WhereOptions<Attributes<TModel>>,
|
|
364
|
+
options?: Omit<RestoreOptions<Attributes<TModel>>, 'where'>,
|
|
365
|
+
): Promise<void>
|
|
366
|
+
|
|
325
367
|
/**
|
|
326
368
|
* @inheritdoc
|
|
327
369
|
*/
|
package/index.js
CHANGED
|
@@ -127,6 +127,27 @@ class AbstractRepository {
|
|
|
127
127
|
throw new common_1.InternalServerErrorException();
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
|
+
async delete(query, options) {
|
|
131
|
+
try {
|
|
132
|
+
return this.model.destroy({
|
|
133
|
+
where: query,
|
|
134
|
+
...options,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
this.logger.error(`delete: ${error}`);
|
|
139
|
+
throw new common_1.InternalServerErrorException();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async restore(query, options) {
|
|
143
|
+
try {
|
|
144
|
+
return this.model.restore({ where: query, ...options });
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
this.logger.error(`restore: ${error}`);
|
|
148
|
+
throw new common_1.InternalServerErrorException();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
130
151
|
async deleteByPk(primaryKey, options) {
|
|
131
152
|
try {
|
|
132
153
|
const entity = await this.findByPk(primaryKey, {
|