@new-hros/libs-sql 1.0.0
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/dist/base.entity.d.ts +11 -0
- package/dist/base.entity.js +63 -0
- package/dist/base.entity.js.map +1 -0
- package/dist/base.repository.d.ts +18 -0
- package/dist/base.repository.js +91 -0
- package/dist/base.repository.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/pagination.d.ts +12 -0
- package/dist/pagination.js +16 -0
- package/dist/pagination.js.map +1 -0
- package/dist/sql-error.mapper.d.ts +3 -0
- package/dist/sql-error.mapper.js +23 -0
- package/dist/sql-error.mapper.js.map +1 -0
- package/dist/sql-health.service.d.ts +11 -0
- package/dist/sql-health.service.js +21 -0
- package/dist/sql-health.service.js.map +1 -0
- package/dist/sql.module.d.ts +6 -0
- package/dist/sql.module.js +98 -0
- package/dist/sql.module.js.map +1 -0
- package/dist/transaction.service.d.ts +8 -0
- package/dist/transaction.service.js +56 -0
- package/dist/transaction.service.js.map +1 -0
- package/dist/unit-of-work.service.d.ts +6 -0
- package/dist/unit-of-work.service.js +29 -0
- package/dist/unit-of-work.service.js.map +1 -0
- package/package.json +39 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BaseEntity = void 0;
|
|
13
|
+
const typeorm_1 = require("typeorm");
|
|
14
|
+
class BaseEntity {
|
|
15
|
+
id;
|
|
16
|
+
tenantCode;
|
|
17
|
+
createdAt;
|
|
18
|
+
updatedAt;
|
|
19
|
+
deletedAt;
|
|
20
|
+
isDeleted;
|
|
21
|
+
version;
|
|
22
|
+
createdBy;
|
|
23
|
+
updatedBy;
|
|
24
|
+
}
|
|
25
|
+
exports.BaseEntity = BaseEntity;
|
|
26
|
+
__decorate([
|
|
27
|
+
(0, typeorm_1.PrimaryGeneratedColumn)('uuid'),
|
|
28
|
+
__metadata("design:type", String)
|
|
29
|
+
], BaseEntity.prototype, "id", void 0);
|
|
30
|
+
__decorate([
|
|
31
|
+
(0, typeorm_1.Index)(),
|
|
32
|
+
(0, typeorm_1.Column)({ name: 'tenant_code', type: 'varchar', length: 64 }),
|
|
33
|
+
__metadata("design:type", String)
|
|
34
|
+
], BaseEntity.prototype, "tenantCode", void 0);
|
|
35
|
+
__decorate([
|
|
36
|
+
(0, typeorm_1.CreateDateColumn)({ name: 'created_at', type: 'timestamptz' }),
|
|
37
|
+
__metadata("design:type", Date)
|
|
38
|
+
], BaseEntity.prototype, "createdAt", void 0);
|
|
39
|
+
__decorate([
|
|
40
|
+
(0, typeorm_1.UpdateDateColumn)({ name: 'updated_at', type: 'timestamptz' }),
|
|
41
|
+
__metadata("design:type", Date)
|
|
42
|
+
], BaseEntity.prototype, "updatedAt", void 0);
|
|
43
|
+
__decorate([
|
|
44
|
+
(0, typeorm_1.DeleteDateColumn)({ name: 'deleted_at', type: 'timestamptz', nullable: true }),
|
|
45
|
+
__metadata("design:type", Date)
|
|
46
|
+
], BaseEntity.prototype, "deletedAt", void 0);
|
|
47
|
+
__decorate([
|
|
48
|
+
(0, typeorm_1.Column)({ name: 'is_deleted', type: 'boolean', default: false }),
|
|
49
|
+
__metadata("design:type", Boolean)
|
|
50
|
+
], BaseEntity.prototype, "isDeleted", void 0);
|
|
51
|
+
__decorate([
|
|
52
|
+
(0, typeorm_1.VersionColumn)({ default: 1 }),
|
|
53
|
+
__metadata("design:type", Number)
|
|
54
|
+
], BaseEntity.prototype, "version", void 0);
|
|
55
|
+
__decorate([
|
|
56
|
+
(0, typeorm_1.Column)({ name: 'created_by', type: 'varchar', length: 128, nullable: true }),
|
|
57
|
+
__metadata("design:type", String)
|
|
58
|
+
], BaseEntity.prototype, "createdBy", void 0);
|
|
59
|
+
__decorate([
|
|
60
|
+
(0, typeorm_1.Column)({ name: 'updated_by', type: 'varchar', length: 128, nullable: true }),
|
|
61
|
+
__metadata("design:type", String)
|
|
62
|
+
], BaseEntity.prototype, "updatedBy", void 0);
|
|
63
|
+
//# sourceMappingURL=base.entity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.entity.js","sourceRoot":"","sources":["../src/base.entity.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qCAQiB;AAEjB,MAAsB,UAAU;IAE9B,EAAE,CAAU;IAIZ,UAAU,CAAU;IAGpB,SAAS,CAAQ;IAGjB,SAAS,CAAQ;IAGjB,SAAS,CAAQ;IAGjB,SAAS,CAAW;IAGpB,OAAO,CAAU;IAGjB,SAAS,CAAU;IAGnB,SAAS,CAAU;CACpB;AA5BD,gCA4BC;AA1BC;IADC,IAAA,gCAAsB,EAAC,MAAM,CAAC;;sCACnB;AAIZ;IAFC,IAAA,eAAK,GAAE;IACP,IAAA,gBAAM,EAAC,EAAE,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC;;8CACzC;AAGpB;IADC,IAAA,0BAAgB,EAAC,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC;8BAClD,IAAI;6CAAC;AAGjB;IADC,IAAA,0BAAgB,EAAC,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC;8BAClD,IAAI;6CAAC;AAGjB;IADC,IAAA,0BAAgB,EAAC,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,aAAa,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;8BAClE,IAAI;6CAAC;AAGjB;IADC,IAAA,gBAAM,EAAC,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;;6CAC5C;AAGpB;IADC,IAAA,uBAAa,EAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;;2CACb;AAGjB;IADC,IAAA,gBAAM,EAAC,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;;6CAC1D;AAGnB;IADC,IAAA,gBAAM,EAAC,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;;6CAC1D"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { FindOptionsWhere, Repository } from 'typeorm';
|
|
2
|
+
import { TransactionService } from './transaction.service';
|
|
3
|
+
import { PaginationOptions, PaginatedResult } from './pagination';
|
|
4
|
+
import { BaseEntity } from './base.entity';
|
|
5
|
+
export declare abstract class BaseRepository<Entity extends BaseEntity> {
|
|
6
|
+
protected readonly entityTarget: new () => Entity;
|
|
7
|
+
protected readonly transactionService: TransactionService;
|
|
8
|
+
constructor(entityTarget: new () => Entity, transactionService: TransactionService);
|
|
9
|
+
protected get repository(): Repository<Entity>;
|
|
10
|
+
protected get tenantCode(): string;
|
|
11
|
+
private applyTenantScope;
|
|
12
|
+
create(entityData: Partial<Entity>): Promise<Entity>;
|
|
13
|
+
findById(id: string): Promise<Entity | null>;
|
|
14
|
+
findPaginated(options: PaginationOptions, where?: FindOptionsWhere<Entity>): Promise<PaginatedResult<Entity>>;
|
|
15
|
+
update(id: string, entityData: Partial<Entity>): Promise<Entity>;
|
|
16
|
+
delete(id: string): Promise<void>;
|
|
17
|
+
restore(id: string): Promise<void>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseRepository = void 0;
|
|
4
|
+
const libs_core_1 = require("@new-hros/libs-core");
|
|
5
|
+
const pagination_1 = require("./pagination");
|
|
6
|
+
class BaseRepository {
|
|
7
|
+
entityTarget;
|
|
8
|
+
transactionService;
|
|
9
|
+
constructor(entityTarget, transactionService) {
|
|
10
|
+
this.entityTarget = entityTarget;
|
|
11
|
+
this.transactionService = transactionService;
|
|
12
|
+
}
|
|
13
|
+
get repository() {
|
|
14
|
+
return this.transactionService.getManager().getRepository(this.entityTarget);
|
|
15
|
+
}
|
|
16
|
+
get tenantCode() {
|
|
17
|
+
const code = libs_core_1.RequestContextService.getTenantCode();
|
|
18
|
+
if (!code) {
|
|
19
|
+
throw new Error('Tenant code is missing from active RequestContext');
|
|
20
|
+
}
|
|
21
|
+
return code;
|
|
22
|
+
}
|
|
23
|
+
applyTenantScope(where) {
|
|
24
|
+
const scope = { tenantCode: this.tenantCode, isDeleted: false };
|
|
25
|
+
return { ...(where || {}), ...scope };
|
|
26
|
+
}
|
|
27
|
+
async create(entityData) {
|
|
28
|
+
const user = libs_core_1.RequestContextService.getUser();
|
|
29
|
+
const entity = this.repository.create({
|
|
30
|
+
...entityData,
|
|
31
|
+
tenantCode: this.tenantCode,
|
|
32
|
+
createdBy: user?.userId || undefined,
|
|
33
|
+
updatedBy: user?.userId || undefined,
|
|
34
|
+
});
|
|
35
|
+
return this.repository.save(entity);
|
|
36
|
+
}
|
|
37
|
+
async findById(id) {
|
|
38
|
+
return this.repository.findOne({
|
|
39
|
+
where: this.applyTenantScope({ id }),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
async findPaginated(options, where) {
|
|
43
|
+
const page = Math.max(1, options.page || 1);
|
|
44
|
+
const limit = Math.max(1, options.limit || 10);
|
|
45
|
+
const skip = (page - 1) * limit;
|
|
46
|
+
const [data, total] = await this.repository.findAndCount({
|
|
47
|
+
where: this.applyTenantScope(where),
|
|
48
|
+
skip,
|
|
49
|
+
take: limit,
|
|
50
|
+
});
|
|
51
|
+
return (0, pagination_1.buildPaginatedResult)(data, total, options);
|
|
52
|
+
}
|
|
53
|
+
async update(id, entityData) {
|
|
54
|
+
const user = libs_core_1.RequestContextService.getUser();
|
|
55
|
+
const existing = await this.findById(id);
|
|
56
|
+
if (!existing) {
|
|
57
|
+
throw new Error(`Record not found with ID: ${id}`);
|
|
58
|
+
}
|
|
59
|
+
const updated = this.repository.merge(existing, {
|
|
60
|
+
...entityData,
|
|
61
|
+
updatedBy: user?.userId || undefined,
|
|
62
|
+
});
|
|
63
|
+
return this.repository.save(updated);
|
|
64
|
+
}
|
|
65
|
+
async delete(id) {
|
|
66
|
+
const user = libs_core_1.RequestContextService.getUser();
|
|
67
|
+
const existing = await this.findById(id);
|
|
68
|
+
if (!existing) {
|
|
69
|
+
throw new Error(`Record not found with ID: ${id}`);
|
|
70
|
+
}
|
|
71
|
+
existing.isDeleted = true;
|
|
72
|
+
existing.deletedAt = new Date();
|
|
73
|
+
existing.updatedBy = user?.userId || undefined;
|
|
74
|
+
await this.repository.save(existing);
|
|
75
|
+
}
|
|
76
|
+
async restore(id) {
|
|
77
|
+
const user = libs_core_1.RequestContextService.getUser();
|
|
78
|
+
const existing = await this.repository.findOne({
|
|
79
|
+
where: { id, tenantCode: this.tenantCode, isDeleted: true },
|
|
80
|
+
});
|
|
81
|
+
if (!existing) {
|
|
82
|
+
throw new Error(`Soft-deleted record not found with ID: ${id}`);
|
|
83
|
+
}
|
|
84
|
+
existing.isDeleted = false;
|
|
85
|
+
existing.deletedAt = undefined;
|
|
86
|
+
existing.updatedBy = user?.userId || undefined;
|
|
87
|
+
await this.repository.save(existing);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.BaseRepository = BaseRepository;
|
|
91
|
+
//# sourceMappingURL=base.repository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.repository.js","sourceRoot":"","sources":["../src/base.repository.ts"],"names":[],"mappings":";;;AAEA,mDAA4D;AAC5D,6CAAwF;AAGxF,MAAsB,cAAc;IAEb;IACA;IAFrB,YACqB,YAA8B,EAC9B,kBAAsC;QADtC,iBAAY,GAAZ,YAAY,CAAkB;QAC9B,uBAAkB,GAAlB,kBAAkB,CAAoB;IACxD,CAAC;IAEJ,IAAc,UAAU;QACtB,OAAO,IAAI,CAAC,kBAAkB,CAAC,UAAU,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAC/E,CAAC;IAED,IAAc,UAAU;QACtB,MAAM,IAAI,GAAG,iCAAqB,CAAC,aAAa,EAAE,CAAC;QACnD,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;QACvE,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAEO,gBAAgB,CAAC,KAAgC;QACvD,MAAM,KAAK,GAAQ,EAAE,UAAU,EAAE,IAAI,CAAC,UAAU,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;QACrE,OAAO,EAAE,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC,EAAE,GAAG,KAAK,EAA8B,CAAC;IACpE,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,UAA2B;QACtC,MAAM,IAAI,GAAG,iCAAqB,CAAC,OAAO,EAAE,CAAC;QAC7C,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC;YACpC,GAAG,UAAU;YACb,UAAU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;QAED,QAAQ,CAAC,SAAS,GAAG,KAAK,CAAC;QAC3B,QAAQ,CAAC,SAAS,GAAG,SAAS,CAAC;QAC/B,QAAQ,CAAC,SAAS,GAAG,IAAI,EAAE,MAAM,IAAI,SAAS,CAAC;QAE/C,MAAM,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACvC,CAAC;CACF;AAxGD,wCAwGC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './base.entity';
|
|
2
|
+
export * from './pagination';
|
|
3
|
+
export * from './base.repository';
|
|
4
|
+
export * from './transaction.service';
|
|
5
|
+
export * from './unit-of-work.service';
|
|
6
|
+
export * from './sql-health.service';
|
|
7
|
+
export * from './sql-error.mapper';
|
|
8
|
+
export * from './sql.module';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
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("./base.entity"), exports);
|
|
18
|
+
__exportStar(require("./pagination"), exports);
|
|
19
|
+
__exportStar(require("./base.repository"), exports);
|
|
20
|
+
__exportStar(require("./transaction.service"), exports);
|
|
21
|
+
__exportStar(require("./unit-of-work.service"), exports);
|
|
22
|
+
__exportStar(require("./sql-health.service"), exports);
|
|
23
|
+
__exportStar(require("./sql-error.mapper"), exports);
|
|
24
|
+
__exportStar(require("./sql.module"), exports);
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA8B;AAC9B,+CAA6B;AAC7B,oDAAkC;AAClC,wDAAsC;AACtC,yDAAuC;AACvC,uDAAqC;AACrC,qDAAmC;AACnC,+CAA6B"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface PaginationOptions {
|
|
2
|
+
page: number;
|
|
3
|
+
limit: number;
|
|
4
|
+
}
|
|
5
|
+
export interface PaginatedResult<T> {
|
|
6
|
+
data: T[];
|
|
7
|
+
total: number;
|
|
8
|
+
page: number;
|
|
9
|
+
limit: number;
|
|
10
|
+
totalPages: number;
|
|
11
|
+
}
|
|
12
|
+
export declare function buildPaginatedResult<T>(data: T[], total: number, options: PaginationOptions): PaginatedResult<T>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildPaginatedResult = buildPaginatedResult;
|
|
4
|
+
function buildPaginatedResult(data, total, options) {
|
|
5
|
+
const page = Math.max(1, options.page || 1);
|
|
6
|
+
const limit = Math.max(1, options.limit || 10);
|
|
7
|
+
const totalPages = Math.ceil(total / limit);
|
|
8
|
+
return {
|
|
9
|
+
data,
|
|
10
|
+
total,
|
|
11
|
+
page,
|
|
12
|
+
limit,
|
|
13
|
+
totalPages,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=pagination.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pagination.js","sourceRoot":"","sources":["../src/pagination.ts"],"names":[],"mappings":";;AAaA,oDAgBC;AAhBD,SAAgB,oBAAoB,CAClC,IAAS,EACT,KAAa,EACb,OAA0B;IAE1B,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC;IAC5C,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;IAC/C,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC;IAE5C,OAAO;QACL,IAAI;QACJ,KAAK;QACL,IAAI;QACJ,KAAK;QACL,UAAU;KACX,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqlErrorMapper = void 0;
|
|
4
|
+
const libs_core_1 = require("@new-hros/libs-core");
|
|
5
|
+
class SqlErrorMapper {
|
|
6
|
+
static map(error) {
|
|
7
|
+
if (!error)
|
|
8
|
+
return new Error('Unknown database error');
|
|
9
|
+
const code = error.code || error.driverError?.code;
|
|
10
|
+
switch (code) {
|
|
11
|
+
case '23505':
|
|
12
|
+
return new libs_core_1.ConflictException(error.detail || 'Unique constraint violation occurred');
|
|
13
|
+
case '23503':
|
|
14
|
+
return new libs_core_1.ConflictException(error.detail || 'Foreign key constraint violation occurred');
|
|
15
|
+
case '23502':
|
|
16
|
+
return new libs_core_1.ValidationException(`Field '${error.column}' cannot be null`);
|
|
17
|
+
default:
|
|
18
|
+
return new libs_core_1.BusinessException(error.message || 'Database error occurred', 'DATABASE_ERROR', 500);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.SqlErrorMapper = SqlErrorMapper;
|
|
23
|
+
//# sourceMappingURL=sql-error.mapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql-error.mapper.js","sourceRoot":"","sources":["../src/sql-error.mapper.ts"],"names":[],"mappings":";;;AAAA,mDAI6B;AAE7B,MAAa,cAAc;IACzB,MAAM,CAAC,GAAG,CAAC,KAAU;QACnB,IAAI,CAAC,KAAK;YAAE,OAAO,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;QAGvD,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,WAAW,EAAE,IAAI,CAAC;QAEnD,QAAQ,IAAI,EAAE,CAAC;YACb,KAAK,OAAO;gBACV,OAAO,IAAI,6BAAiB,CAAC,KAAK,CAAC,MAAM,IAAI,sCAAsC,CAAC,CAAC;YACvF,KAAK,OAAO;gBACV,OAAO,IAAI,6BAAiB,CAAC,KAAK,CAAC,MAAM,IAAI,2CAA2C,CAAC,CAAC;YAC5F,KAAK,OAAO;gBACV,OAAO,IAAI,+BAAmB,CAAC,UAAU,KAAK,CAAC,MAAM,kBAAkB,CAAC,CAAC;YAC3E;gBACE,OAAO,IAAI,6BAAiB,CAAC,KAAK,CAAC,OAAO,IAAI,yBAAyB,EAAE,gBAAgB,EAAE,GAAG,CAAC,CAAC;QACpG,CAAC;IACH,CAAC;CACF;AAlBD,wCAkBC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { HealthIndicator } from '@new-hros/libs-core';
|
|
2
|
+
import { EntityManager } from 'typeorm';
|
|
3
|
+
export declare class SqlHealthService implements HealthIndicator {
|
|
4
|
+
private readonly entityManager;
|
|
5
|
+
readonly name = "postgres";
|
|
6
|
+
constructor(entityManager: EntityManager);
|
|
7
|
+
checkHealth(): Promise<{
|
|
8
|
+
status: 'up' | 'down';
|
|
9
|
+
details?: Record<string, any>;
|
|
10
|
+
}>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqlHealthService = void 0;
|
|
4
|
+
class SqlHealthService {
|
|
5
|
+
entityManager;
|
|
6
|
+
name = 'postgres';
|
|
7
|
+
constructor(entityManager) {
|
|
8
|
+
this.entityManager = entityManager;
|
|
9
|
+
}
|
|
10
|
+
async checkHealth() {
|
|
11
|
+
try {
|
|
12
|
+
await this.entityManager.query('SELECT 1');
|
|
13
|
+
return { status: 'up' };
|
|
14
|
+
}
|
|
15
|
+
catch (err) {
|
|
16
|
+
return { status: 'down', details: { error: err.message } };
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.SqlHealthService = SqlHealthService;
|
|
21
|
+
//# sourceMappingURL=sql-health.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql-health.service.js","sourceRoot":"","sources":["../src/sql-health.service.ts"],"names":[],"mappings":";;;AAGA,MAAa,gBAAgB;IAGE;IAFpB,IAAI,GAAG,UAAU,CAAC;IAE3B,YAA6B,aAA4B;QAA5B,kBAAa,GAAb,aAAa,CAAe;IAAG,CAAC;IAE7D,KAAK,CAAC,WAAW;QACf,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;YAC3C,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;QAC1B,CAAC;QAAC,OAAO,GAAQ,EAAE,CAAC;YAClB,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,GAAG,CAAC,OAAO,EAAE,EAAE,CAAC;QAC7D,CAAC;IACH,CAAC;CACF;AAbD,4CAaC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { SqlModuleOptions, SqlModuleAsyncOptions } from '@new-hros/libs-core';
|
|
3
|
+
export declare class SqlModule {
|
|
4
|
+
static forRoot(options: SqlModuleOptions): DynamicModule;
|
|
5
|
+
static forRootAsync(options: SqlModuleAsyncOptions): DynamicModule;
|
|
6
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var SqlModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.SqlModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const typeorm_1 = require("@nestjs/typeorm");
|
|
13
|
+
const transaction_service_1 = require("./transaction.service");
|
|
14
|
+
const unit_of_work_service_1 = require("./unit-of-work.service");
|
|
15
|
+
const sql_health_service_1 = require("./sql-health.service");
|
|
16
|
+
const typeorm_2 = require("typeorm");
|
|
17
|
+
let SqlModule = SqlModule_1 = class SqlModule {
|
|
18
|
+
static forRoot(options) {
|
|
19
|
+
const typeOrmModule = typeorm_1.TypeOrmModule.forRoot({
|
|
20
|
+
type: 'postgres',
|
|
21
|
+
host: options.host,
|
|
22
|
+
port: options.port,
|
|
23
|
+
username: options.username,
|
|
24
|
+
password: options.password,
|
|
25
|
+
database: options.database,
|
|
26
|
+
url: options.url,
|
|
27
|
+
autoLoadEntities: options.autoLoadEntities ?? true,
|
|
28
|
+
synchronize: options.synchronize ?? false,
|
|
29
|
+
logging: false,
|
|
30
|
+
});
|
|
31
|
+
return {
|
|
32
|
+
module: SqlModule_1,
|
|
33
|
+
imports: [typeOrmModule],
|
|
34
|
+
providers: [
|
|
35
|
+
{
|
|
36
|
+
provide: transaction_service_1.TransactionService,
|
|
37
|
+
useFactory: (manager) => new transaction_service_1.TransactionService(manager),
|
|
38
|
+
inject: [typeorm_2.EntityManager],
|
|
39
|
+
},
|
|
40
|
+
unit_of_work_service_1.UnitOfWork,
|
|
41
|
+
{
|
|
42
|
+
provide: sql_health_service_1.SqlHealthService,
|
|
43
|
+
useFactory: (manager) => new sql_health_service_1.SqlHealthService(manager),
|
|
44
|
+
inject: [typeorm_2.EntityManager],
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
exports: [typeorm_1.TypeOrmModule, transaction_service_1.TransactionService, unit_of_work_service_1.UnitOfWork, sql_health_service_1.SqlHealthService],
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
static forRootAsync(options) {
|
|
51
|
+
const typeOrmModule = typeorm_1.TypeOrmModule.forRootAsync({
|
|
52
|
+
name: options.name,
|
|
53
|
+
imports: options.imports || [],
|
|
54
|
+
useExisting: options.useExisting,
|
|
55
|
+
useClass: options.useClass,
|
|
56
|
+
useFactory: async (...args) => {
|
|
57
|
+
const sqlOpts = await options.useFactory(...args);
|
|
58
|
+
return {
|
|
59
|
+
type: 'postgres',
|
|
60
|
+
host: sqlOpts.host,
|
|
61
|
+
port: sqlOpts.port,
|
|
62
|
+
username: sqlOpts.username,
|
|
63
|
+
password: sqlOpts.password,
|
|
64
|
+
database: sqlOpts.database,
|
|
65
|
+
url: sqlOpts.url,
|
|
66
|
+
autoLoadEntities: sqlOpts.autoLoadEntities ?? true,
|
|
67
|
+
synchronize: sqlOpts.synchronize ?? false,
|
|
68
|
+
logging: false,
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
inject: options.inject || [],
|
|
72
|
+
});
|
|
73
|
+
return {
|
|
74
|
+
module: SqlModule_1,
|
|
75
|
+
imports: [typeOrmModule],
|
|
76
|
+
providers: [
|
|
77
|
+
{
|
|
78
|
+
provide: transaction_service_1.TransactionService,
|
|
79
|
+
useFactory: (manager) => new transaction_service_1.TransactionService(manager),
|
|
80
|
+
inject: [typeorm_2.EntityManager],
|
|
81
|
+
},
|
|
82
|
+
unit_of_work_service_1.UnitOfWork,
|
|
83
|
+
{
|
|
84
|
+
provide: sql_health_service_1.SqlHealthService,
|
|
85
|
+
useFactory: (manager) => new sql_health_service_1.SqlHealthService(manager),
|
|
86
|
+
inject: [typeorm_2.EntityManager],
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
exports: [typeorm_1.TypeOrmModule, transaction_service_1.TransactionService, unit_of_work_service_1.UnitOfWork, sql_health_service_1.SqlHealthService],
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
exports.SqlModule = SqlModule;
|
|
94
|
+
exports.SqlModule = SqlModule = SqlModule_1 = __decorate([
|
|
95
|
+
(0, common_1.Global)(),
|
|
96
|
+
(0, common_1.Module)({})
|
|
97
|
+
], SqlModule);
|
|
98
|
+
//# sourceMappingURL=sql.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql.module.js","sourceRoot":"","sources":["../src/sql.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAA+D;AAC/D,6CAAgD;AAEhD,+DAA2D;AAC3D,iEAAoD;AACpD,6DAAwD;AACxD,qCAAwC;AAIjC,IAAM,SAAS,iBAAf,MAAM,SAAS;IACpB,MAAM,CAAC,OAAO,CAAC,OAAyB;QACtC,MAAM,aAAa,GAAG,uBAAa,CAAC,OAAO,CAAC;YAC1C,IAAI,EAAE,UAAU;YAChB,IAAI,EAAE,OAAO,CAAC,IAAI;YAClB,IAAI,EAAE,OAAO,CAAC,IAAI;YAClB,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,GAAG,EAAE,OAAO,CAA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,EAAE,OAAO,CAAC,QAAQ;oBAC1B,QAAQ,EAAE,OAAO,CAAC,QAAQ;oBAC1B,GAAG,EAAE,OAAO,CAAC,GAAG;oBAChB,gBAAgB,EAAE,OAAO,CAAC,gBAAgB,IAAI,IAAI;oBAClD,WAAW,EAAE,OAAO,CAAC,WAAW,IAAI,KAAK;oBACzC,OAAO,EAAE,KAAK;iBACf,CAAC;YACJ,CAAC;YACD,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,EAAE;SAC7B,CAAC,CAAC;QAEH,OAAO;YACL,MAAM,EAAE,WAAS;YACjB,OAAO,EAAE,CAAC,aAAa,CAAC;YACxB,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,wCAAkB;oBAC3B,UAAU,EAAE,CAAC,OAAsB,EAAE,EAAE,CAAC,IAAI,wCAAkB,CAAC,OAAO,CAAC;oBACvE,MAAM,EAAE,CAAC,uBAAa,CAAC;iBACxB;gBACD,iCAAU;gBACV;oBACE,OAAO,EAAE,qCAAgB;oBACzB,UAAU,EAAE,CAAC,OAAsB,EAAE,EAAE,CAAC,IAAI,qCAAgB,CAAC,OAAO,CAAC;oBACrE,MAAM,EAAE,CAAC,uBAAa,CAAC;iBACxB;aACF;YACD,OAAO,EAAE,CAAC,uBAAa,EAAE,wCAAkB,EAAE,iCAAU,EAAE,qCAAgB,CAAC;SAC3E,CAAC;IACJ,CAAC;CACF,CAAA;AA9EY,8BAAS;oBAAT,SAAS;IAFrB,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,SAAS,CA8ErB"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { EntityManager } from 'typeorm';
|
|
2
|
+
export declare class TransactionService {
|
|
3
|
+
private readonly defaultManager;
|
|
4
|
+
static readonly TRANSACTION_MANAGER_KEY = "_entityManager";
|
|
5
|
+
constructor(defaultManager: EntityManager);
|
|
6
|
+
getManager(): EntityManager;
|
|
7
|
+
runInTransaction<T>(work: () => Promise<T>): Promise<T>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var TransactionService_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.TransactionService = void 0;
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const typeorm_1 = require("typeorm");
|
|
16
|
+
const libs_core_1 = require("@new-hros/libs-core");
|
|
17
|
+
let TransactionService = class TransactionService {
|
|
18
|
+
static { TransactionService_1 = this; }
|
|
19
|
+
defaultManager;
|
|
20
|
+
static TRANSACTION_MANAGER_KEY = '_entityManager';
|
|
21
|
+
constructor(defaultManager) {
|
|
22
|
+
this.defaultManager = defaultManager;
|
|
23
|
+
}
|
|
24
|
+
getManager() {
|
|
25
|
+
const currentContext = libs_core_1.RequestContextService.current();
|
|
26
|
+
if (currentContext && currentContext[TransactionService_1.TRANSACTION_MANAGER_KEY]) {
|
|
27
|
+
return currentContext[TransactionService_1.TRANSACTION_MANAGER_KEY];
|
|
28
|
+
}
|
|
29
|
+
return this.defaultManager;
|
|
30
|
+
}
|
|
31
|
+
async runInTransaction(work) {
|
|
32
|
+
const currentContext = libs_core_1.RequestContextService.current();
|
|
33
|
+
if (currentContext && currentContext[TransactionService_1.TRANSACTION_MANAGER_KEY]) {
|
|
34
|
+
return work();
|
|
35
|
+
}
|
|
36
|
+
return this.defaultManager.transaction(async (transactionalEntityManager) => {
|
|
37
|
+
if (currentContext) {
|
|
38
|
+
currentContext[TransactionService_1.TRANSACTION_MANAGER_KEY] = transactionalEntityManager;
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
return await work();
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
if (currentContext) {
|
|
45
|
+
delete currentContext[TransactionService_1.TRANSACTION_MANAGER_KEY];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
exports.TransactionService = TransactionService;
|
|
52
|
+
exports.TransactionService = TransactionService = TransactionService_1 = __decorate([
|
|
53
|
+
(0, common_1.Injectable)(),
|
|
54
|
+
__metadata("design:paramtypes", [typeorm_1.EntityManager])
|
|
55
|
+
], TransactionService);
|
|
56
|
+
//# sourceMappingURL=transaction.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction.service.js","sourceRoot":"","sources":["../src/transaction.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,2CAA4C;AAC5C,qCAAwC;AACxC,mDAA4D;AAGrD,IAAM,kBAAkB,GAAxB,MAAM,kBAAkB;;IAGA;IAFtB,MAAM,CAAU,uBAAuB,GAAG,gBAAgB,CAAC;IAElE,YAA6B,cAA6B;QAA7B,mBAAc,GAAd,cAAc,CAAe;IAAG,CAAC;IAE9D,UAAU;QACR,MAAM,cAAc,GAAG,iCAAqB,CAAC,OAAO,EAAS,CAAC;QAC9D,IAAI,cAAc,IAAI,cAAc,CAAC,oBAAkB,CAAC,uBAAuB,CAAC,EAAE,CAAC;YACjF,OAAO,cAAc,CAAC,oBAAkB,CAAC,uBAAuB,CAAC,CAAC;QACpE,CAAC;QACD,OAAO,IAAI,CAAC,cAAc,CAAC;IAC7B,CAAC;IAED,KAAK,CAAC,gBAAgB,CAAI,IAAsB;QAC9C,MAAM,cAAc,GAAG,iCAAqB,CAAC,OAAO,EAAS,CAAC;QAG9D,IAAI,cAAc,IAAI,cAAc,CAAC,oBAAkB,CAAC,uBAAuB,CAAC,EAAE,CAAC;YACjF,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,OAAO,IAAI,CAAC,cAAc,CAAC,WAAW,CAAC,KAAK,EAAE,0BAA0B,EAAE,EAAE;YAC1E,IAAI,cAAc,EAAE,CAAC;gBACnB,cAAc,CAAC,oBAAkB,CAAC,uBAAuB,CAAC,GAAG,0BAA0B,CAAC;YAC1F,CAAC;YACD,IAAI,CAAC;gBACH,OAAO,MAAM,IAAI,EAAE,CAAC;YACtB,CAAC;oBAAS,CAAC;gBACT,IAAI,cAAc,EAAE,CAAC;oBACnB,OAAO,cAAc,CAAC,oBAAkB,CAAC,uBAAuB,CAAC,CAAC;gBACpE,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;;AAjCU,gDAAkB;6BAAlB,kBAAkB;IAD9B,IAAA,mBAAU,GAAE;qCAIkC,uBAAa;GAH/C,kBAAkB,CAkC9B"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.UnitOfWork = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const transaction_service_1 = require("./transaction.service");
|
|
15
|
+
let UnitOfWork = class UnitOfWork {
|
|
16
|
+
transactionService;
|
|
17
|
+
constructor(transactionService) {
|
|
18
|
+
this.transactionService = transactionService;
|
|
19
|
+
}
|
|
20
|
+
async execute(work) {
|
|
21
|
+
return this.transactionService.runInTransaction(work);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
exports.UnitOfWork = UnitOfWork;
|
|
25
|
+
exports.UnitOfWork = UnitOfWork = __decorate([
|
|
26
|
+
(0, common_1.Injectable)(),
|
|
27
|
+
__metadata("design:paramtypes", [transaction_service_1.TransactionService])
|
|
28
|
+
], UnitOfWork);
|
|
29
|
+
//# sourceMappingURL=unit-of-work.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unit-of-work.service.js","sourceRoot":"","sources":["../src/unit-of-work.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA4C;AAC5C,+DAA2D;AAGpD,IAAM,UAAU,GAAhB,MAAM,UAAU;IACQ;IAA7B,YAA6B,kBAAsC;QAAtC,uBAAkB,GAAlB,kBAAkB,CAAoB;IAAG,CAAC;IAEvE,KAAK,CAAC,OAAO,CAAI,IAAsB;QACrC,OAAO,IAAI,CAAC,kBAAkB,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;IACxD,CAAC;CACF,CAAA;AANY,gCAAU;qBAAV,UAAU;IADtB,IAAA,mBAAU,GAAE;qCAEsC,wCAAkB;GADxD,UAAU,CAMtB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@new-hros/libs-sql",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "NestJS multi-tenant platform foundation - SQL Database Layer",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc",
|
|
15
|
+
"test": "jest"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/hros-aio/apis-factory.git",
|
|
20
|
+
"directory": "libs/libs-sql"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/hros-aio/apis-factory/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/hros-aio/apis-factory/tree/master/libs/libs-sql#readme",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"author": "New HROS Team",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@nestjs/common": "^10.0.0",
|
|
30
|
+
"@nestjs/typeorm": "^10.0.0",
|
|
31
|
+
"@new-hros/libs-core": "^1.0.0",
|
|
32
|
+
"typeorm": "^0.3.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/jest": "^29.5.11",
|
|
36
|
+
"jest": "^29.7.0",
|
|
37
|
+
"ts-jest": "^29.1.1"
|
|
38
|
+
}
|
|
39
|
+
}
|