Skip to content

Commit 9c30c30

Browse files
authored
Merge pull request #27 from stbestichhh/feature/#26/remove-id-generation
chore: updated findAllPaginated params, removed auto id generattion
2 parents 1623070 + ce6355e commit 9c30c30

File tree

12 files changed

+355
-432
lines changed

12 files changed

+355
-432
lines changed

.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"singleQuote": true,
33
"trailingComma": "all",
44
"tabWidth": 2,
5-
"semi": true
5+
"semi": false
66
}

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
# Unreleased
9+
### Removed:
10+
* Autogenerating id is removed
11+
12+
### Changed:
13+
* Updated `findAllPaginated` parameters
14+
815
# Patch 0.1.11
916
### Updated:
1017
* New [**README.md**](README.md) file
@@ -14,7 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1421
* New method for find with pagination `findAllPaginated`
1522

1623
### Changed:
17-
* Now default auto generated id is type of UUIDv4 instead of v7, what helped to reduce package size
24+
* Now default auto generated id is type of UUIDv4 instead of v7, what helped to reduce package size
1825

1926
# Patch 0.1.9
2027
### Fixed:
@@ -41,7 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4148
### Added:
4249
* idGenerator config option. See [README.md](README.md/#️-options)
4350

44-
### Fixed:
51+
### Fixed:
4552
* Added types for protected fields, so they can be reused in derived classes
4653

4754
## Patch 0.1.3

README.md

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ Configuration options for the abstract repository:
5656

5757
| Option | Type | Default | Description |
5858
|------------------|--------------------------|-------------------------|-------------------------------------------------------|
59-
| `autoGenerateId` | `boolean` | `false` | Whether to auto-generate a UUIDv4 for the primary key |
60-
| `idField` | `string` | `'id'` | Name of the primary key field |
61-
| `idGenerator` | `() => string \| number` | `UUIDv4` | Function for generating unique IDs |
6259
| `logger` | `Logger` | `NestJS default Logger` | Optional NestJS logger instance for internal logging |
6360

6461
---
@@ -67,19 +64,19 @@ Configuration options for the abstract repository:
6764

6865
All methods return Promises.
6966

70-
| Method | Parameters | Description | | |
71-
|------------------------------------------------------|------------------------------------------------------------------------------------------------------------|-------------------------------------------------|------------|----------------------------------------|
72-
| `create(dto, options?)` | `dto: CreationAttributes<TModel>`, `options?: CreateOptions<TModel>` | Creates a new record | | |
73-
| `insert(dto, options?)` | Same as `create` | Alias for `create` | | |
74-
| `insertMany(dtos, options?)` | `dtos: CreationAttributes<TModel>[]`, `options?: BulkCreateOptions<Attributes<TModel>>` | Creates multiple records | | |
75-
| `findByPk(primaryKey, options?)` | `primaryKey: string \| number`, `options?: Omit<FindOptions, 'where'>` | Find record by primary key | | |
76-
| `findOne(query?, options?)` | `query?: WhereOptions`, `options?: Omit<FindOptions, 'where'>` | Find single record by query | | |
77-
| `findAll(query?, options?)` | `query?: WhereOptions`, `options?: Omit<FindOptions, 'where'>` | Find all matching records | | |
78-
| `findAllPaginated(limit, offset?, query?, options?)` | `limit: number`, `offset?: number`, `query?: WhereOptions`, \`options?: Omit\<FindAndCountOptions, 'where' | 'offset' | 'limit'>\` | Find paginated records and total count |
79-
| `updateByPk(primaryKey, dto, options?)` | `primaryKey: string \| number`, `dto: Partial<Attributes<TModel>>`, `options?: SaveOptions` | Update record by primary key | | |
80-
| `deleteByPk(primaryKey, options?)` | `primaryKey: string \| number`, `options?: InstanceDestroyOptions` | Delete (soft/hard) record by primary key | | |
81-
| `restoreByPk(primaryKey, options?)` | `primaryKey: string \| number`, `options?: InstanceRestoreOptions` | Restore previously soft-deleted record | | |
82-
| `transaction(runInTransaction)` | `(transaction: Transaction) => Promise<R>` | Execute callback within a Sequelize transaction | | |
67+
| Method | Parameters | Description |
68+
|-----------------------------------------|------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------|
69+
| `create(dto, options?)` | `dto: CreationAttributes<TModel>`, `options?: CreateOptions<TModel>` | Creates a new record |
70+
| `insert(dto, options?)` | Same as `create` | Alias for `create` |
71+
| `insertMany(dtos, options?)` | `dtos: CreationAttributes<TModel>[]`, `options?: BulkCreateOptions<Attributes<TModel>>` | Creates multiple records |
72+
| `findByPk(primaryKey, options?)` | `primaryKey: string \| number`, `options?: Omit<FindOptions, 'where'>` | Find record by primary key |
73+
| `findOne(query?, options?)` | `query?: WhereOptions`, `options?: Omit<FindOptions, 'where'>` | Find single record by query |
74+
| `findAll(query?, options?)` | `query?: WhereOptions`, `options?: Omit<FindOptions, 'where'>` | Find all matching records |
75+
| `findAllPaginated(options?)` | `limit?: number`, `offset?: number`, `query?: WhereOptions`, `options?: Omit<FindAndCountOptions, 'where' \| 'offset' \| 'limit'>` | Find paginated records and total count |
76+
| `updateByPk(primaryKey, dto, options?)` | `primaryKey: string \| number`, `dto: Partial<Attributes<TModel>>`, `options?: SaveOptions` | Update record by primary key |
77+
| `deleteByPk(primaryKey, options?)` | `primaryKey: string \| number`, `options?: InstanceDestroyOptions` | Delete (soft/hard) record by primary key |
78+
| `restoreByPk(primaryKey, options?)` | `primaryKey: string \| number`, `options?: InstanceRestoreOptions` | Restore previously soft-deleted record |
79+
| `transaction(runInTransaction)` | `(transaction: Transaction) => Promise<R>` | Execute callback within a Sequelize transaction |
8380

8481
---
8582

@@ -91,6 +88,7 @@ All methods return Promises.
9188
@Table({ tableName: 'users', paranoid: true })
9289
export class User extends Model<User> {
9390
@PrimaryKey
91+
@Default(DataType.UUIDV4)
9492
@Column
9593
user_id: string;
9694

@@ -112,10 +110,7 @@ import { AbstractRepository } from 'nest-sequelize-repository';
112110
@Injectable()
113111
export class UserRepository extends AbstractRepository<User> {
114112
constructor(@InjectModel(User) userModel: typeof User) {
115-
super(userModel, {
116-
autoGenerateId: true,
117-
idField: 'user_id',
118-
});
113+
super(userModel);
119114
}
120115
}
121116
```
@@ -151,9 +146,6 @@ You can pass options when instantiating:
151146

152147
```ts
153148
{
154-
autoGenerateId: true, // optional
155-
idField: 'user_id', // optional, default field is 'id'
156-
idGenerator: myGenerateIdFunc, // optional, default is UUIDv4
157149
logger: new MyCustomLogger('MyRepo'), // optional
158150
}
159151
```

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
22
preset: 'ts-jest',
33
testEnvironment: 'node',
4-
};
4+
}

src/IRepository.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,61 @@ import {
1010
SaveOptions,
1111
Transaction,
1212
WhereOptions,
13-
} from 'sequelize';
14-
import { Model } from 'sequelize-typescript';
13+
} from 'sequelize'
14+
import { Model } from 'sequelize-typescript'
15+
16+
export interface PaginationOptions<TModel extends Model> {
17+
limit?: number
18+
offset?: number
19+
query?: WhereOptions<Attributes<TModel>>
20+
findOptions?: Omit<
21+
FindAndCountOptions<Attributes<TModel>>,
22+
'where' | 'offset' | 'limit'
23+
>
24+
}
1525

1626
export interface IRepository<TModel extends Model> {
1727
create(
1828
dto: CreationAttributes<TModel>,
1929
options?: CreateOptions<TModel>,
20-
): Promise<TModel>;
30+
): Promise<TModel>
2131
insert(
2232
dto: CreationAttributes<TModel>,
2333
options?: CreateOptions<TModel>,
24-
): Promise<TModel>;
34+
): Promise<TModel>
2535
insertMany(
2636
dtos: CreationAttributes<TModel>[],
2737
options?: BulkCreateOptions<Attributes<TModel>>,
28-
): Promise<TModel[]>;
38+
): Promise<TModel[]>
2939
findByPk(
3040
primaryKey: string | number,
3141
options?: Omit<FindOptions<Attributes<TModel>>, 'where'>,
32-
): Promise<TModel | null>;
42+
): Promise<TModel | null>
3343
findOne(
3444
query?: WhereOptions<Attributes<TModel>>,
3545
options?: Omit<FindOptions<Attributes<TModel>>, 'where'>,
36-
): Promise<TModel | null>;
46+
): Promise<TModel | null>
3747
findAll(
3848
query?: WhereOptions<Attributes<TModel>>,
3949
options?: Omit<FindOptions<Attributes<TModel>>, 'where'>,
40-
): Promise<TModel[]>;
50+
): Promise<TModel[]>
4151
findAllPaginated(
42-
limit: number,
43-
offset?: number,
44-
query?: WhereOptions<Attributes<TModel>>,
45-
options?: Omit<
46-
FindAndCountOptions<Attributes<TModel>>,
47-
'where' | 'offset' | 'limit'
48-
>,
49-
): Promise<{ rows: TModel[]; count: number }>;
52+
options: PaginationOptions<TModel>,
53+
): Promise<{ rows: TModel[]; count: number }>
5054
updateByPk(
5155
primaryKey: string | number,
5256
dto: Partial<Attributes<TModel>>,
5357
options?: SaveOptions<Attributes<TModel>>,
54-
): Promise<TModel | null>;
58+
): Promise<TModel | null>
5559
deleteByPk(
5660
primaryKey: string | number,
5761
options?: InstanceDestroyOptions,
58-
): Promise<TModel | null>;
62+
): Promise<TModel | null>
5963
restoreByPk(
6064
primaryKey: string | number,
6165
options?: InstanceRestoreOptions,
62-
): Promise<TModel | null>;
66+
): Promise<TModel | null>
6367
transaction<R>(
6468
runInTransaction: (transaction: Transaction) => Promise<R>,
65-
): Promise<R>;
69+
): Promise<R>
6670
}

src/IRepositoryOptions.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import { Logger } from '@nestjs/common';
2-
import { Attributes, Model } from 'sequelize';
1+
import { Logger } from '@nestjs/common'
32

4-
export interface IRepositoryOptions<T extends Model> {
5-
autoGenerateId?: boolean;
6-
idField?: Extract<keyof Attributes<T>, string>;
7-
idGenerator?: () => string;
8-
logger?: Logger;
3+
export interface IRepositoryOptions {
4+
logger?: Logger
95
}

0 commit comments

Comments
 (0)