@nathapp/nestjs-data 2.1.5 → 3.0.2
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/.turbo/turbo-build.log +4 -0
- package/CHANGELOG.md +17 -0
- package/CLAUDE.md +42 -0
- package/dist/constant/index.d.ts +1 -0
- package/dist/constant/index.js +18 -0
- package/dist/constant/index.js.map +1 -0
- package/dist/constant/tokens.d.ts +1 -0
- package/dist/constant/tokens.js +5 -0
- package/dist/constant/tokens.js.map +1 -0
- package/dist/index.d.ts +2 -6
- package/dist/index.js +2 -6
- package/dist/index.js.map +1 -1
- package/dist/interface/index.d.ts +2 -0
- package/dist/interface/index.js +19 -0
- package/dist/interface/index.js.map +1 -0
- package/dist/interface/repository.interface.d.ts +18 -0
- package/dist/interface/repository.interface.js +3 -0
- package/dist/interface/repository.interface.js.map +1 -0
- package/dist/interface/transaction-manager.interface.d.ts +5 -0
- package/dist/interface/transaction-manager.interface.js +3 -0
- package/dist/interface/transaction-manager.interface.js.map +1 -0
- package/lib/constant/index.ts +1 -0
- package/lib/constant/tokens.ts +1 -0
- package/lib/index.ts +2 -0
- package/lib/interface/index.ts +2 -0
- package/lib/interface/repository.interface.ts +87 -0
- package/lib/interface/transaction-manager.interface.ts +40 -0
- package/package.json +7 -21
- package/test/jest-test.json +24 -0
- package/test/repository.interface.spec.ts +112 -0
- package/test/tokens.spec.ts +11 -0
- package/tsconfig.json +9 -0
- package/LICENSE +0 -21
- package/README.md +0 -4
- package/dist/decorator/date-column.decorator.d.ts +0 -7
- package/dist/decorator/date-column.decorator.js +0 -51
- package/dist/decorator/date-column.decorator.js.map +0 -1
- package/dist/decorator/index.d.ts +0 -4
- package/dist/decorator/index.js +0 -15
- package/dist/decorator/index.js.map +0 -1
- package/dist/decorator/readonly-column.decorator.d.ts +0 -1
- package/dist/decorator/readonly-column.decorator.js +0 -14
- package/dist/decorator/readonly-column.decorator.js.map +0 -1
- package/dist/decorator/typeorm-ex.decorator.d.ts +0 -2
- package/dist/decorator/typeorm-ex.decorator.js +0 -10
- package/dist/decorator/typeorm-ex.decorator.js.map +0 -1
- package/dist/entities/base-entity.d.ts +0 -3
- package/dist/entities/base-entity.js +0 -8
- package/dist/entities/base-entity.js.map +0 -1
- package/dist/entities/index.d.ts +0 -2
- package/dist/entities/index.js +0 -6
- package/dist/entities/index.js.map +0 -1
- package/dist/pagination/index.d.ts +0 -2
- package/dist/pagination/index.js +0 -9
- package/dist/pagination/index.js.map +0 -1
- package/dist/pagination/page.d.ts +0 -35
- package/dist/pagination/page.js +0 -123
- package/dist/pagination/page.js.map +0 -1
- package/dist/repository/base.repository.d.ts +0 -18
- package/dist/repository/base.repository.js +0 -140
- package/dist/repository/base.repository.js.map +0 -1
- package/dist/repository/index.d.ts +0 -2
- package/dist/repository/index.js +0 -7
- package/dist/repository/index.js.map +0 -1
- package/dist/testing/index.d.ts +0 -2
- package/dist/testing/index.js +0 -11
- package/dist/testing/index.js.map +0 -1
- package/dist/testing/testing-utils.d.ts +0 -56
- package/dist/testing/testing-utils.js +0 -44
- package/dist/testing/testing-utils.js.map +0 -1
- package/dist/typeorm/base-typeorm-options-factory.d.ts +0 -18
- package/dist/typeorm/base-typeorm-options-factory.js +0 -42
- package/dist/typeorm/base-typeorm-options-factory.js.map +0 -1
- package/dist/typeorm/constants.d.ts +0 -16
- package/dist/typeorm/constants.js +0 -20
- package/dist/typeorm/constants.js.map +0 -1
- package/dist/typeorm/index.d.ts +0 -5
- package/dist/typeorm/index.js +0 -16
- package/dist/typeorm/index.js.map +0 -1
- package/dist/typeorm/typeorm-ex.module.d.ts +0 -13
- package/dist/typeorm/typeorm-ex.module.js +0 -75
- package/dist/typeorm/typeorm-ex.module.js.map +0 -1
- package/dist/typeorm/typeorm-utils.d.ts +0 -26
- package/dist/typeorm/typeorm-utils.js +0 -16
- package/dist/typeorm/typeorm-utils.js.map +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -6
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @nathapp/nestjs-data
|
|
2
|
+
|
|
3
|
+
## 3.0.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- a448f5b: add irepository and abstract repository
|
|
8
|
+
|
|
9
|
+
## 3.0.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Add ITransactionManager interface and ORM-specific implementations
|
|
14
|
+
|
|
15
|
+
- **nestjs-data**: New package with `ITransactionManager` interface and `TRANSACTION_MANAGER` DI token for ORM-agnostic transaction management via AsyncLocalStorage
|
|
16
|
+
- **nestjs-prisma**: Add `PrismaTransactionManager` implementing `ITransactionManager` with Prisma interactive transactions
|
|
17
|
+
- **nestjs-typeorm**: Add `TypeOrmTransactionManager` implementing `ITransactionManager` with TypeORM QueryRunner transactions
|
package/CLAUDE.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# @nathapp/nestjs-data
|
|
2
|
+
|
|
3
|
+
ORM-agnostic data access abstractions. Pure-type package — no NestJS or ORM imports.
|
|
4
|
+
|
|
5
|
+
## Exports
|
|
6
|
+
|
|
7
|
+
- `TRANSACTION_MANAGER` (Symbol) — DI token for `ITransactionManager`
|
|
8
|
+
- `ITransactionManager` — orm-agnostic transaction control via AsyncLocalStorage
|
|
9
|
+
- `IRepository<TDomain, TId>` — portable repository contract (CRUD + pagination)
|
|
10
|
+
- `IPageResult<T>` — structural shape of paginated results (compatible with `Page<T>` from `@nathapp/nestjs-common`)
|
|
11
|
+
|
|
12
|
+
## Design
|
|
13
|
+
|
|
14
|
+
Services depend on `IRepository<TDomain>` via DI tokens (e.g. `USER_REPOSITORY`),
|
|
15
|
+
never on the concrete ORM repo class. Concrete implementations live in
|
|
16
|
+
`@nathapp/nestjs-typeorm` (`AbstractTypeOrmRepository`) and `@nathapp/nestjs-prisma`
|
|
17
|
+
(`AbstractPrismaRepository`). Both implementations:
|
|
18
|
+
|
|
19
|
+
- Accept the `TRANSACTION_MANAGER` provider and resolve the active client per operation.
|
|
20
|
+
- Require subclasses to implement three mappers (`toDomain`, `toPersistenceCreate`, `toPersistenceUpdate`).
|
|
21
|
+
- Return `Page<T>` from `@nathapp/nestjs-common` for `findAll` (compatible with `IPageResult<T>`).
|
|
22
|
+
|
|
23
|
+
For ORM-specific power (Prisma `include`, TypeORM `QueryBuilder`, raw SQL,
|
|
24
|
+
stored procedures), inject the concrete repo class directly — those call sites
|
|
25
|
+
intentionally lose portability. The portable surface is the `IRepository`
|
|
26
|
+
contract; everything else is an escape hatch.
|
|
27
|
+
|
|
28
|
+
## Build
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
cd packages/nestjs-data && npm run build
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Peer Dependencies
|
|
35
|
+
|
|
36
|
+
- `@nestjs/common` — for `Inject` typing
|
|
37
|
+
- `@nathapp/nestjs-common` — for `IPageOption` (used by `IRepository.findAll`)
|
|
38
|
+
|
|
39
|
+
## Limitations
|
|
40
|
+
|
|
41
|
+
- Concrete base classes assume the primary key column/property is named `"id"`.
|
|
42
|
+
Schemas with different PK names must implement `IRepository` directly.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './tokens';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./tokens"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/constant/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const TRANSACTION_MANAGER: unique symbol;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokens.js","sourceRoot":"","sources":["../../lib/constant/tokens.ts"],"names":[],"mappings":";;;AAAa,QAAA,mBAAmB,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -14,10 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
18
|
-
__exportStar(require("./
|
|
19
|
-
__exportStar(require("./repository"), exports);
|
|
20
|
-
__exportStar(require("./pagination"), exports);
|
|
21
|
-
__exportStar(require("./testing"), exports);
|
|
22
|
-
__exportStar(require("./typeorm"), exports);
|
|
17
|
+
__exportStar(require("./constant"), exports);
|
|
18
|
+
__exportStar(require("./interface"), exports);
|
|
23
19
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,8CAA4B"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./transaction-manager.interface"), exports);
|
|
18
|
+
__exportStar(require("./repository.interface"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/interface/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kEAAgD;AAChD,yDAAuC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { IPageOption } from '@nathapp/nestjs-common';
|
|
2
|
+
export interface IPageResult<T> {
|
|
3
|
+
total: number;
|
|
4
|
+
current: number;
|
|
5
|
+
size: number;
|
|
6
|
+
hasNext: boolean;
|
|
7
|
+
hasPrev: boolean;
|
|
8
|
+
records: T[];
|
|
9
|
+
}
|
|
10
|
+
export interface IRepository<TDomain, TId = string> {
|
|
11
|
+
findById(id: TId): Promise<TDomain | null>;
|
|
12
|
+
findOne(criteria: Partial<TDomain>): Promise<TDomain | null>;
|
|
13
|
+
findAll(page: IPageOption): Promise<IPageResult<TDomain>>;
|
|
14
|
+
count(criteria?: Partial<TDomain>): Promise<number>;
|
|
15
|
+
create(input: TDomain): Promise<TDomain>;
|
|
16
|
+
update(id: TId, patch: Partial<TDomain>): Promise<TDomain>;
|
|
17
|
+
delete(id: TId): Promise<void>;
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repository.interface.js","sourceRoot":"","sources":["../../lib/interface/repository.interface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction-manager.interface.js","sourceRoot":"","sources":["../../lib/interface/transaction-manager.interface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './tokens';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const TRANSACTION_MANAGER = Symbol('TRANSACTION_MANAGER');
|
package/lib/index.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { IPageOption } from '@nathapp/nestjs-common';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ORM-agnostic page result shape returned by `IRepository.findAll`.
|
|
5
|
+
*
|
|
6
|
+
* Mirrors the shape of `Page<T>` from `@nathapp/nestjs-common` but is declared
|
|
7
|
+
* structurally here so `nestjs-data` does not need a runtime dependency on
|
|
8
|
+
* `nestjs-common`. Concrete ORM base classes return the real `Page<T>` instance.
|
|
9
|
+
*/
|
|
10
|
+
export interface IPageResult<T> {
|
|
11
|
+
total: number;
|
|
12
|
+
current: number;
|
|
13
|
+
size: number;
|
|
14
|
+
hasNext: boolean;
|
|
15
|
+
hasPrev: boolean;
|
|
16
|
+
records: T[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Portable repository contract.
|
|
21
|
+
*
|
|
22
|
+
* Services depend on `IRepository<TDomain>` via a DI token (e.g. `USER_REPOSITORY`)
|
|
23
|
+
* so business logic stays decoupled from the underlying ORM. Concrete
|
|
24
|
+
* implementations live in `@nathapp/nestjs-typeorm` (`AbstractTypeOrmRepository`)
|
|
25
|
+
* and `@nathapp/nestjs-prisma` (`AbstractPrismaRepository`).
|
|
26
|
+
*
|
|
27
|
+
* For ORM-specific power (Prisma `include`, TypeORM `QueryBuilder`, raw SQL,
|
|
28
|
+
* stored procedures), inject the concrete repo class directly — those call sites
|
|
29
|
+
* intentionally lose portability.
|
|
30
|
+
*
|
|
31
|
+
* @typeParam TDomain - Domain model type (a plain interface or class owned by
|
|
32
|
+
* the service layer). Never an ORM-generated type.
|
|
33
|
+
* @typeParam TId - Primary key type. Defaults to string.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* Concrete base classes (`AbstractTypeOrmRepository`, `AbstractPrismaRepository`)
|
|
37
|
+
* structurally assume the primary key column/property is named `"id"`. If your
|
|
38
|
+
* schema uses a different name (e.g. `userId`, `uuid`), you must bypass the
|
|
39
|
+
* abstract base and implement `IRepository` directly, or use an ORM-specific
|
|
40
|
+
* escape hatch.
|
|
41
|
+
*/
|
|
42
|
+
export interface IRepository<TDomain, TId = string> {
|
|
43
|
+
/**
|
|
44
|
+
* Find a single entity by primary key.
|
|
45
|
+
* Returns `null` if not found (does NOT throw).
|
|
46
|
+
*/
|
|
47
|
+
findById(id: TId): Promise<TDomain | null>;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Find a single entity matching all fields in `criteria` (equality only).
|
|
51
|
+
* For complex queries, use the concrete ORM repo's escape hatches.
|
|
52
|
+
*/
|
|
53
|
+
findOne(criteria: Partial<TDomain>): Promise<TDomain | null>;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Paginated list of entities. `page.current` is 1-indexed, `page.size` is items
|
|
57
|
+
* per page. Returns total count and pagination metadata in `IPageResult`.
|
|
58
|
+
*
|
|
59
|
+
* @remarks Filtering criteria are NOT supported here — use the concrete ORM
|
|
60
|
+
* repo's escape hatches for filtered list queries.
|
|
61
|
+
*/
|
|
62
|
+
findAll(page: IPageOption): Promise<IPageResult<TDomain>>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Count entities. Without criteria, returns total. With criteria, counts
|
|
66
|
+
* matching entities (equality only).
|
|
67
|
+
*/
|
|
68
|
+
count(criteria?: Partial<TDomain>): Promise<number>;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Create a new entity. Returns the persisted entity (with any DB-generated
|
|
72
|
+
* fields populated, e.g. autoincrement id, createdAt).
|
|
73
|
+
*/
|
|
74
|
+
create(input: TDomain): Promise<TDomain>;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Apply a partial patch to an existing entity. Returns the updated entity.
|
|
78
|
+
* Throws if the entity does not exist.
|
|
79
|
+
*/
|
|
80
|
+
update(id: TId, patch: Partial<TDomain>): Promise<TDomain>;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Delete an entity by primary key. Idempotent — no-op if entity does not exist
|
|
84
|
+
* (concrete impls may differ; document any deviation).
|
|
85
|
+
*/
|
|
86
|
+
delete(id: TId): Promise<void>;
|
|
87
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ORM-agnostic transaction manager using AsyncLocalStorage.
|
|
3
|
+
*
|
|
4
|
+
* Implementations:
|
|
5
|
+
* - PrismaTransactionManager (@nathapp/nestjs-prisma)
|
|
6
|
+
* - TypeOrmTransactionManager (@nathapp/nestjs-typeorm)
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* ```typescript
|
|
10
|
+
* @Inject(TRANSACTION_MANAGER)
|
|
11
|
+
* private readonly txManager: ITransactionManager;
|
|
12
|
+
*
|
|
13
|
+
* async register(input: CreateAccountInput) {
|
|
14
|
+
* return this.txManager.run(() => {
|
|
15
|
+
* const account = await this.accountService.create(input);
|
|
16
|
+
* await this.membershipService.assignRoles(account.id, roles);
|
|
17
|
+
* return account;
|
|
18
|
+
* });
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export interface ITransactionManager {
|
|
23
|
+
/**
|
|
24
|
+
* Execute fn within a transaction.
|
|
25
|
+
* Auto-commits on success, rolls back on error.
|
|
26
|
+
* Supports nesting (inner calls reuse the outer transaction).
|
|
27
|
+
*/
|
|
28
|
+
run<T>(fn: () => Promise<T>): Promise<T>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Get the active transaction client, or the root client if not in a transaction.
|
|
32
|
+
* Repositories call this instead of using their injected client directly.
|
|
33
|
+
*/
|
|
34
|
+
getClient<C = unknown>(): C;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Check if currently inside a transaction.
|
|
38
|
+
*/
|
|
39
|
+
isInTransaction(): boolean;
|
|
40
|
+
}
|
package/package.json
CHANGED
|
@@ -1,43 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nathapp/nestjs-data",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "3.0.2",
|
|
4
|
+
"description": "ORM-agnostic data access abstractions for NestJS",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"clean": "rimraf dist/ .turbo/",
|
|
7
7
|
"build": "rimraf dist && tsc -p tsconfig.json",
|
|
8
|
-
"precommit": "lint-staged",
|
|
9
8
|
"test": "jest --config ./test/jest-test.json",
|
|
10
|
-
"test:cov": "jest --config ./test/jest-test.json --coverage"
|
|
11
|
-
"test:watch": "jest --config ./test/jest-test.json --watch",
|
|
12
|
-
"lint": "eslint -c .eslintrc.js"
|
|
9
|
+
"test:cov": "jest --config ./test/jest-test.json --coverage"
|
|
13
10
|
},
|
|
14
11
|
"author": "William Khoo",
|
|
15
12
|
"license": "ISC",
|
|
16
13
|
"types": "dist/index.d.ts",
|
|
17
14
|
"main": "dist/index.js",
|
|
18
|
-
"directories": {
|
|
19
|
-
"lib": "lib"
|
|
20
|
-
},
|
|
21
15
|
"dependencies": {
|
|
22
|
-
"@nathapp/nestjs-common": "^
|
|
23
|
-
"dayjs": "^1.11.13"
|
|
16
|
+
"@nathapp/nestjs-common": "^3.0.0"
|
|
24
17
|
},
|
|
25
18
|
"devDependencies": {
|
|
26
19
|
"@nathapp/eslint-config": "1.0.1",
|
|
27
20
|
"@nathapp/typescript-config": "1.0.1",
|
|
21
|
+
"@golevelup/ts-jest": "^0.6.1",
|
|
28
22
|
"eslint": "^8.57.0",
|
|
29
23
|
"typescript": "^5"
|
|
30
24
|
},
|
|
31
25
|
"peerDependencies": {
|
|
32
|
-
"@
|
|
33
|
-
"@nestjs
|
|
34
|
-
"pg-mem": "*",
|
|
35
|
-
"typeorm": "^0.3.27",
|
|
36
|
-
"typeorm-naming-strategies": "^4.1.0"
|
|
37
|
-
},
|
|
38
|
-
"peerDependenciesMeta": {
|
|
39
|
-
"pg-mem": {
|
|
40
|
-
"optional": true
|
|
41
|
-
}
|
|
26
|
+
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
|
27
|
+
"@nathapp/nestjs-common": "^3.0.0"
|
|
42
28
|
}
|
|
43
29
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"moduleFileExtensions": [
|
|
3
|
+
"js",
|
|
4
|
+
"json",
|
|
5
|
+
"ts",
|
|
6
|
+
"d.ts"
|
|
7
|
+
],
|
|
8
|
+
"rootDir": "../",
|
|
9
|
+
"testEnvironment": "node",
|
|
10
|
+
"testRegex": ".spec.ts$",
|
|
11
|
+
"transform": {
|
|
12
|
+
"^.+\\.(t)s$": [
|
|
13
|
+
"ts-jest",
|
|
14
|
+
{
|
|
15
|
+
"diagnostics": true,
|
|
16
|
+
"isolatedModules": true
|
|
17
|
+
}
|
|
18
|
+
]
|
|
19
|
+
},
|
|
20
|
+
"collectCoverageFrom": [
|
|
21
|
+
"lib/**/*.ts",
|
|
22
|
+
"!**/node_modules/**"
|
|
23
|
+
]
|
|
24
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { IRepository } from '../lib/interface/repository.interface';
|
|
2
|
+
|
|
3
|
+
interface User {
|
|
4
|
+
id: string;
|
|
5
|
+
email: string;
|
|
6
|
+
name: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
class InMemoryUserRepository implements IRepository<User, string> {
|
|
10
|
+
private store = new Map<string, User>();
|
|
11
|
+
|
|
12
|
+
async findById(id: string): Promise<User | null> {
|
|
13
|
+
return this.store.get(id) ?? null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async findOne(criteria: Partial<User>): Promise<User | null> {
|
|
17
|
+
for (const user of this.store.values()) {
|
|
18
|
+
if (Object.entries(criteria).every(([k, v]) => (user as any)[k] === v)) {
|
|
19
|
+
return user;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async findAll(page: { current: number; size: number }) {
|
|
26
|
+
const records = Array.from(this.store.values());
|
|
27
|
+
return {
|
|
28
|
+
total: records.length,
|
|
29
|
+
current: page.current,
|
|
30
|
+
size: page.size,
|
|
31
|
+
hasNext: false,
|
|
32
|
+
hasPrev: false,
|
|
33
|
+
records,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async count(criteria?: Partial<User>): Promise<number> {
|
|
38
|
+
if (!criteria) return this.store.size;
|
|
39
|
+
let n = 0;
|
|
40
|
+
for (const user of this.store.values()) {
|
|
41
|
+
if (Object.entries(criteria).every(([k, v]) => (user as any)[k] === v)) n++;
|
|
42
|
+
}
|
|
43
|
+
return n;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async create(input: User): Promise<User> {
|
|
47
|
+
this.store.set(input.id, input);
|
|
48
|
+
return input;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async update(id: string, patch: Partial<User>): Promise<User> {
|
|
52
|
+
const existing = this.store.get(id);
|
|
53
|
+
if (!existing) throw new Error('not found');
|
|
54
|
+
const updated = { ...existing, ...patch };
|
|
55
|
+
this.store.set(id, updated);
|
|
56
|
+
return updated;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async delete(id: string): Promise<void> {
|
|
60
|
+
this.store.delete(id);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
describe('IRepository contract', () => {
|
|
65
|
+
let repo: IRepository<User, string>;
|
|
66
|
+
|
|
67
|
+
beforeEach(() => {
|
|
68
|
+
repo = new InMemoryUserRepository();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('create + findById round-trips a domain entity', async () => {
|
|
72
|
+
const user: User = { id: '1', email: 'a@b.c', name: 'Alice' };
|
|
73
|
+
await repo.create(user);
|
|
74
|
+
expect(await repo.findById('1')).toEqual(user);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('findById returns null for missing entity', async () => {
|
|
78
|
+
expect(await repo.findById('missing')).toBeNull();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('findOne matches by partial criteria', async () => {
|
|
82
|
+
await repo.create({ id: '1', email: 'a@b.c', name: 'Alice' });
|
|
83
|
+
await repo.create({ id: '2', email: 'b@b.c', name: 'Bob' });
|
|
84
|
+
expect(await repo.findOne({ name: 'Bob' })).toMatchObject({ id: '2' });
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('findAll returns Page-shaped result', async () => {
|
|
88
|
+
await repo.create({ id: '1', email: 'a@b.c', name: 'Alice' });
|
|
89
|
+
const page = await repo.findAll({ current: 1, size: 10 });
|
|
90
|
+
expect(page.total).toBe(1);
|
|
91
|
+
expect(page.records).toHaveLength(1);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('count without criteria returns total', async () => {
|
|
95
|
+
await repo.create({ id: '1', email: 'a@b.c', name: 'Alice' });
|
|
96
|
+
await repo.create({ id: '2', email: 'b@b.c', name: 'Bob' });
|
|
97
|
+
expect(await repo.count()).toBe(2);
|
|
98
|
+
expect(await repo.count({ name: 'Alice' })).toBe(1);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('update merges patch into existing entity', async () => {
|
|
102
|
+
await repo.create({ id: '1', email: 'a@b.c', name: 'Alice' });
|
|
103
|
+
const updated = await repo.update('1', { name: 'Alicia' });
|
|
104
|
+
expect(updated).toMatchObject({ id: '1', name: 'Alicia', email: 'a@b.c' });
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('delete removes the entity', async () => {
|
|
108
|
+
await repo.create({ id: '1', email: 'a@b.c', name: 'Alice' });
|
|
109
|
+
await repo.delete('1');
|
|
110
|
+
expect(await repo.findById('1')).toBeNull();
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { TRANSACTION_MANAGER } from '../lib/constant/tokens';
|
|
2
|
+
|
|
3
|
+
describe('TRANSACTION_MANAGER token', () => {
|
|
4
|
+
it('should be a Symbol', () => {
|
|
5
|
+
expect(typeof TRANSACTION_MANAGER).toBe('symbol');
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it('should have the correct description', () => {
|
|
9
|
+
expect(TRANSACTION_MANAGER.toString()).toBe('Symbol(TRANSACTION_MANAGER)');
|
|
10
|
+
});
|
|
11
|
+
});
|
package/tsconfig.json
ADDED
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2018 KnowYourself
|
|
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
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { ColumnOptions } from "typeorm";
|
|
2
|
-
import { TransformOptions } from "class-transformer";
|
|
3
|
-
export declare const toDateString: (value: Date, format?: string) => string | Date;
|
|
4
|
-
export declare const NullableDateString: (options?: TransformOptions) => <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
5
|
-
export declare const CreateDate: (format?: string, options?: ColumnOptions) => <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
6
|
-
export declare const UpdateDate: (format?: string, options?: ColumnOptions) => <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
7
|
-
export declare const DateColumn: (format?: string, options?: ColumnOptions) => <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DateColumn = exports.UpdateDate = exports.CreateDate = exports.NullableDateString = exports.toDateString = void 0;
|
|
4
|
-
const dayjs = require("dayjs");
|
|
5
|
-
const typeorm_1 = require("typeorm");
|
|
6
|
-
const common_1 = require("@nestjs/common");
|
|
7
|
-
const class_transformer_1 = require("class-transformer");
|
|
8
|
-
const DEFAULT_DATE_FORMAT = "YYYY-MM-DD HH:mm:ss";
|
|
9
|
-
const toDateString = (value, format) => {
|
|
10
|
-
return value ? dayjs(value).format(format || DEFAULT_DATE_FORMAT) : value;
|
|
11
|
-
};
|
|
12
|
-
exports.toDateString = toDateString;
|
|
13
|
-
const NullableDateString = (options = { toPlainOnly: true }) => {
|
|
14
|
-
return applyDateDecorators((0, class_transformer_1.Transform)(({ value }) => (value ? (0, exports.toDateString)(value) : null), options));
|
|
15
|
-
};
|
|
16
|
-
exports.NullableDateString = NullableDateString;
|
|
17
|
-
const applyDateDecorators = (decorator) => {
|
|
18
|
-
return (0, common_1.applyDecorators)((0, class_transformer_1.Transform)(({ value, type }) => (0, exports.toDateString)(value)), decorator);
|
|
19
|
-
};
|
|
20
|
-
const CreateDate = (format, options) => {
|
|
21
|
-
return applyDateDecorators((0, typeorm_1.CreateDateColumn)({
|
|
22
|
-
transformer: {
|
|
23
|
-
to: (value) => (0, exports.toDateString)(value, format),
|
|
24
|
-
from: (value) => new Date(value),
|
|
25
|
-
},
|
|
26
|
-
...options,
|
|
27
|
-
}));
|
|
28
|
-
};
|
|
29
|
-
exports.CreateDate = CreateDate;
|
|
30
|
-
const UpdateDate = (format, options) => {
|
|
31
|
-
return applyDateDecorators((0, typeorm_1.UpdateDateColumn)({
|
|
32
|
-
transformer: {
|
|
33
|
-
to: (value) => (0, exports.toDateString)(value, format),
|
|
34
|
-
from: (value) => new Date(value),
|
|
35
|
-
},
|
|
36
|
-
...options,
|
|
37
|
-
}));
|
|
38
|
-
};
|
|
39
|
-
exports.UpdateDate = UpdateDate;
|
|
40
|
-
const DateColumn = (format, options) => {
|
|
41
|
-
return applyDateDecorators((0, typeorm_1.Column)({
|
|
42
|
-
transformer: {
|
|
43
|
-
to: (value) => (0, exports.toDateString)(value, format),
|
|
44
|
-
from: (value) => new Date(value),
|
|
45
|
-
},
|
|
46
|
-
...options,
|
|
47
|
-
type: "datetime",
|
|
48
|
-
}));
|
|
49
|
-
};
|
|
50
|
-
exports.DateColumn = DateColumn;
|
|
51
|
-
//# sourceMappingURL=date-column.decorator.js.map
|