framework-do-dede 6.0.3 → 6.0.5
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 +19 -5
- package/dist/protocols/repository.d.ts +6 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -501,12 +501,26 @@ Interfaces tipadas para padrao de repositorio:
|
|
|
501
501
|
- `RepositoryNotExistsBy<T>`
|
|
502
502
|
- `RepositoryPagination<T>`
|
|
503
503
|
|
|
504
|
-
`RepositoryRestore` e `RepositoryRestoreBy`
|
|
504
|
+
`RepositoryRestore` e `RepositoryRestoreBy` podem retornar a entidade diretamente (padrão) ou `Optional<T>` quando quiser usar `orElse*`:
|
|
505
505
|
|
|
506
506
|
```ts
|
|
507
507
|
import { Optional } from './src';
|
|
508
508
|
|
|
509
509
|
class UserRepository {
|
|
510
|
+
async restore(id: string): Promise<User> {
|
|
511
|
+
const result = await this.orm
|
|
512
|
+
.select()
|
|
513
|
+
.from(userTable)
|
|
514
|
+
.where(eq(userTable.id, id));
|
|
515
|
+
|
|
516
|
+
const row = result[0] ?? null;
|
|
517
|
+
const model = row ? new UserModel().fromModel(row) : null;
|
|
518
|
+
if (!model) throw new Error('Usuario nao encontrado');
|
|
519
|
+
return model.toEntity();
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
class UserRepositoryOptional implements RepositoryRestore<User, User, true> {
|
|
510
524
|
async restore(id: string): Promise<Optional<User>> {
|
|
511
525
|
const result = await this.orm
|
|
512
526
|
.select()
|
|
@@ -520,12 +534,12 @@ class UserRepository {
|
|
|
520
534
|
}
|
|
521
535
|
}
|
|
522
536
|
|
|
523
|
-
const user = await repo.restore('1')
|
|
524
|
-
const userByEmail = await repo.restoreByEmail('a@b.com')
|
|
525
|
-
const userOrThrow = await
|
|
537
|
+
const user = await repo.restore('1');
|
|
538
|
+
const userByEmail = await repo.restoreByEmail('a@b.com');
|
|
539
|
+
const userOrThrow = await repoOptional.restoreByEmail('a@b.com').orElseThrow(
|
|
526
540
|
() => new Unauthorized('Usuario ou senha invalida')
|
|
527
541
|
);
|
|
528
|
-
const userOrThrow2 = await
|
|
542
|
+
const userOrThrow2 = await repoOptional.restoreByEmail('a@b.com').orElseThrow(
|
|
529
543
|
new Unauthorized('Usuario ou senha invalida')
|
|
530
544
|
);
|
|
531
545
|
```
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Entity, Optional } from "../domain";
|
|
2
|
+
type MaybeOptional<TResult, TOptional extends boolean> = TOptional extends true ? Optional<TResult> : TResult;
|
|
2
3
|
export interface RepositoryCreate<T extends Entity> {
|
|
3
4
|
create(input: T): Promise<void>;
|
|
4
5
|
}
|
|
@@ -8,14 +9,14 @@ export interface RepositoryUpdate<T extends Entity> {
|
|
|
8
9
|
export interface RepositoryRemove {
|
|
9
10
|
remove(id: string | number): Promise<void>;
|
|
10
11
|
}
|
|
11
|
-
export interface RepositoryRestore<T extends Entity> {
|
|
12
|
-
restore(id: string | number): Promise<
|
|
12
|
+
export interface RepositoryRestore<T extends Entity, TResult = T, TOptional extends boolean = false> {
|
|
13
|
+
restore(id: string | number): Promise<MaybeOptional<TResult, TOptional>>;
|
|
13
14
|
}
|
|
14
15
|
export type RepositoryRemoveBy<T> = {
|
|
15
16
|
[K in keyof T & string as `removeBy${Capitalize<K>}`]: (value: T[K]) => Promise<void>;
|
|
16
17
|
};
|
|
17
|
-
export type RepositoryRestoreBy<TKeys
|
|
18
|
-
[K in keyof TKeys & string as `restoreBy${Capitalize<K>}`]: (value: TKeys[K]) => Promise<
|
|
18
|
+
export type RepositoryRestoreBy<TKeys> = {
|
|
19
|
+
[K in keyof TKeys & string as `restoreBy${Capitalize<K>}`]: (value: TKeys[K]) => Promise<any>;
|
|
19
20
|
};
|
|
20
21
|
export type RepositoryExistsBy<T> = {
|
|
21
22
|
[K in keyof T & string as `existsBy${Capitalize<K>}`]: (value: T[K]) => Promise<boolean>;
|
|
@@ -32,3 +33,4 @@ export interface RepositoryPagination<T> {
|
|
|
32
33
|
};
|
|
33
34
|
}): Promise<T>;
|
|
34
35
|
}
|
|
36
|
+
export {};
|