@nathapp/nestjs-data 1.0.0-rc.1 → 1.0.0-rc.10
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/decorator/index.d.ts +2 -1
- package/dist/decorator/index.js +4 -1
- package/dist/decorator/typeorm-ex.decorator.d.ts +2 -0
- package/dist/decorator/typeorm-ex.decorator.js +9 -0
- package/dist/entities/base-entity.d.ts +3 -0
- package/dist/entities/base-entity.js +7 -0
- package/dist/entities/index.d.ts +2 -0
- package/dist/entities/index.js +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/pagination/index.d.ts +2 -0
- package/dist/pagination/index.js +8 -0
- package/dist/pagination/page.d.ts +23 -0
- package/dist/pagination/page.js +108 -0
- package/dist/repository/base.repository.d.ts +17 -0
- package/dist/repository/base.repository.js +137 -0
- package/dist/repository/index.d.ts +2 -0
- package/dist/repository/index.js +6 -0
- package/dist/typeorm/base-typeorm-options-factory.d.ts +15 -0
- package/dist/typeorm/base-typeorm-options-factory.js +40 -0
- package/dist/typeorm/constants.d.ts +16 -0
- package/dist/typeorm/constants.js +19 -0
- package/dist/typeorm/index.d.ts +5 -0
- package/dist/typeorm/index.js +15 -0
- package/dist/typeorm/typeorm-ex.module.d.ts +11 -0
- package/dist/typeorm/typeorm-ex.module.js +68 -0
- package/dist/typeorm/typeorm-utils.d.ts +26 -0
- package/dist/typeorm/typeorm-utils.js +15 -0
- package/package.json +15 -11
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { CreateDate, DateColumn, UpdateDate } from "./date-column.decorator";
|
|
2
2
|
import { ReadOnlyColumn } from "./readonly-column.decorator";
|
|
3
|
-
|
|
3
|
+
import { CustomRepository, TYPEORM_EX_CUSTOM_REPOSITORY } from "./typeorm-ex.decorator";
|
|
4
|
+
export { DateColumn, CreateDate, UpdateDate, ReadOnlyColumn, CustomRepository, TYPEORM_EX_CUSTOM_REPOSITORY, };
|
package/dist/decorator/index.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ReadOnlyColumn = exports.UpdateDate = exports.CreateDate = exports.DateColumn = void 0;
|
|
3
|
+
exports.TYPEORM_EX_CUSTOM_REPOSITORY = exports.CustomRepository = exports.ReadOnlyColumn = exports.UpdateDate = exports.CreateDate = exports.DateColumn = void 0;
|
|
4
4
|
const date_column_decorator_1 = require("./date-column.decorator");
|
|
5
5
|
Object.defineProperty(exports, "CreateDate", { enumerable: true, get: function () { return date_column_decorator_1.CreateDate; } });
|
|
6
6
|
Object.defineProperty(exports, "DateColumn", { enumerable: true, get: function () { return date_column_decorator_1.DateColumn; } });
|
|
7
7
|
Object.defineProperty(exports, "UpdateDate", { enumerable: true, get: function () { return date_column_decorator_1.UpdateDate; } });
|
|
8
8
|
const readonly_column_decorator_1 = require("./readonly-column.decorator");
|
|
9
9
|
Object.defineProperty(exports, "ReadOnlyColumn", { enumerable: true, get: function () { return readonly_column_decorator_1.ReadOnlyColumn; } });
|
|
10
|
+
const typeorm_ex_decorator_1 = require("./typeorm-ex.decorator");
|
|
11
|
+
Object.defineProperty(exports, "CustomRepository", { enumerable: true, get: function () { return typeorm_ex_decorator_1.CustomRepository; } });
|
|
12
|
+
Object.defineProperty(exports, "TYPEORM_EX_CUSTOM_REPOSITORY", { enumerable: true, get: function () { return typeorm_ex_decorator_1.TYPEORM_EX_CUSTOM_REPOSITORY; } });
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CustomRepository = exports.TYPEORM_EX_CUSTOM_REPOSITORY = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
exports.TYPEORM_EX_CUSTOM_REPOSITORY = "TYPEORM_EX_CUSTOM_REPOSITORY";
|
|
6
|
+
function CustomRepository(entity) {
|
|
7
|
+
return (0, common_1.SetMetadata)(exports.TYPEORM_EX_CUSTOM_REPOSITORY, entity);
|
|
8
|
+
}
|
|
9
|
+
exports.CustomRepository = CustomRepository;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseEntity = void 0;
|
|
4
|
+
const base_entity_1 = require("./base-entity");
|
|
5
|
+
Object.defineProperty(exports, "BaseEntity", { enumerable: true, get: function () { return base_entity_1.BaseEntity; } });
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -15,3 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./decorator"), exports);
|
|
18
|
+
__exportStar(require("./entities"), exports);
|
|
19
|
+
__exportStar(require("./pagination"), exports);
|
|
20
|
+
__exportStar(require("./typeorm"), exports);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Paginate = exports.Page = exports.PageOption = exports.TransformOptions = void 0;
|
|
4
|
+
const page_1 = require("./page");
|
|
5
|
+
Object.defineProperty(exports, "TransformOptions", { enumerable: true, get: function () { return page_1.TransformOptions; } });
|
|
6
|
+
Object.defineProperty(exports, "PageOption", { enumerable: true, get: function () { return page_1.PageOption; } });
|
|
7
|
+
Object.defineProperty(exports, "Page", { enumerable: true, get: function () { return page_1.Page; } });
|
|
8
|
+
Object.defineProperty(exports, "Paginate", { enumerable: true, get: function () { return page_1.Paginate; } });
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ClassConstructor } from "class-transformer";
|
|
2
|
+
import { FindManyOptions, MongoRepository, Repository, SelectQueryBuilder } from "typeorm";
|
|
3
|
+
type mapFunction<E> = (item: any, index: number, array: any[]) => E;
|
|
4
|
+
export declare class TransformOptions<T> {
|
|
5
|
+
cls?: ClassConstructor<T>;
|
|
6
|
+
}
|
|
7
|
+
export declare class PageOption {
|
|
8
|
+
current: number;
|
|
9
|
+
size: number;
|
|
10
|
+
}
|
|
11
|
+
export declare class Page<T> {
|
|
12
|
+
total: number;
|
|
13
|
+
size: number;
|
|
14
|
+
current: number;
|
|
15
|
+
records: T[];
|
|
16
|
+
transformOptions: TransformOptions<T>;
|
|
17
|
+
constructor(page: PageOption, total: number, records: T[], transformOptions?: TransformOptions<T>);
|
|
18
|
+
toInstance(transformOptions?: TransformOptions<T>): this;
|
|
19
|
+
toPlain(): this;
|
|
20
|
+
mapTo<E>(mapFunc: mapFunction<E>, cls?: ClassConstructor<E>): E[];
|
|
21
|
+
}
|
|
22
|
+
export declare function Paginate<T>(repositoryOrQueryBuilder: Repository<T> | MongoRepository<T> | SelectQueryBuilder<T>, options: PageOption, searchOptions?: FindManyOptions<T>, transformOptions?: TransformOptions<T>): Promise<Page<T>>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,108 @@
|
|
|
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.Paginate = exports.Page = exports.PageOption = exports.TransformOptions = void 0;
|
|
13
|
+
const class_transformer_1 = require("class-transformer");
|
|
14
|
+
const typeorm_1 = require("typeorm");
|
|
15
|
+
class TransformOptions {
|
|
16
|
+
}
|
|
17
|
+
exports.TransformOptions = TransformOptions;
|
|
18
|
+
class PageOption {
|
|
19
|
+
constructor() {
|
|
20
|
+
this.current = 1;
|
|
21
|
+
this.size = 25;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.PageOption = PageOption;
|
|
25
|
+
class Page {
|
|
26
|
+
constructor(page, total, records, transformOptions) {
|
|
27
|
+
this.size = Number(page.size);
|
|
28
|
+
this.current = Number(page.current);
|
|
29
|
+
this.total = Number(total);
|
|
30
|
+
this.transformOptions = transformOptions;
|
|
31
|
+
this.records = records;
|
|
32
|
+
}
|
|
33
|
+
toInstance(transformOptions) {
|
|
34
|
+
var _a, _b;
|
|
35
|
+
if (transformOptions === null || transformOptions === void 0 ? void 0 : transformOptions.cls) {
|
|
36
|
+
this.records = (0, class_transformer_1.plainToInstance)(transformOptions === null || transformOptions === void 0 ? void 0 : transformOptions.cls, this.records);
|
|
37
|
+
}
|
|
38
|
+
if ((_a = this.transformOptions) === null || _a === void 0 ? void 0 : _a.cls) {
|
|
39
|
+
this.records = (0, class_transformer_1.plainToInstance)((_b = this.transformOptions) === null || _b === void 0 ? void 0 : _b.cls, this.records);
|
|
40
|
+
}
|
|
41
|
+
delete this.transformOptions;
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
toPlain() {
|
|
45
|
+
this.records = (0, class_transformer_1.instanceToPlain)(this.records);
|
|
46
|
+
delete this.transformOptions;
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
mapTo(mapFunc, cls) {
|
|
50
|
+
const results = this.records.map(mapFunc);
|
|
51
|
+
return cls ? (0, class_transformer_1.plainToInstance)(cls, results) : results;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
__decorate([
|
|
55
|
+
(0, class_transformer_1.Exclude)(),
|
|
56
|
+
__metadata("design:type", TransformOptions)
|
|
57
|
+
], Page.prototype, "transformOptions", void 0);
|
|
58
|
+
exports.Page = Page;
|
|
59
|
+
async function Paginate(repositoryOrQueryBuilder, options, searchOptions, transformOptions) {
|
|
60
|
+
if (repositoryOrQueryBuilder instanceof typeorm_1.MongoRepository) {
|
|
61
|
+
return paginateMongoRepository(repositoryOrQueryBuilder, options, searchOptions, transformOptions);
|
|
62
|
+
}
|
|
63
|
+
else if (repositoryOrQueryBuilder instanceof typeorm_1.Repository) {
|
|
64
|
+
return paginateRepository(repositoryOrQueryBuilder, options, searchOptions, transformOptions);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
return paginateQueryBuilder(repositoryOrQueryBuilder, options, transformOptions);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.Paginate = Paginate;
|
|
71
|
+
async function paginateRepository(repository, options, searchOptions, transformOptions) {
|
|
72
|
+
const { take, skip } = resolvePageOption(options);
|
|
73
|
+
const [result, total] = await repository.findAndCount({
|
|
74
|
+
skip,
|
|
75
|
+
take,
|
|
76
|
+
...searchOptions,
|
|
77
|
+
});
|
|
78
|
+
return new Page(options, total, result, transformOptions);
|
|
79
|
+
}
|
|
80
|
+
async function paginateQueryBuilder(queryBuilder, options, transformOptions) {
|
|
81
|
+
const { take, skip } = resolvePageOption(options);
|
|
82
|
+
const total = await queryBuilder.getCount();
|
|
83
|
+
const result = await queryBuilder.skip(skip).take(take).getMany();
|
|
84
|
+
return new Page(options, total, result, transformOptions);
|
|
85
|
+
}
|
|
86
|
+
async function paginateMongoRepository(repository, options, searchOptions, transformOptions) {
|
|
87
|
+
const { take, skip } = resolvePageOption(options);
|
|
88
|
+
const [result, total] = await repository.findAndCount({
|
|
89
|
+
skip,
|
|
90
|
+
take,
|
|
91
|
+
...searchOptions,
|
|
92
|
+
});
|
|
93
|
+
return new Page(options, total, result, transformOptions);
|
|
94
|
+
}
|
|
95
|
+
function resolvePageOption(pageOption) {
|
|
96
|
+
let { current, size } = pageOption;
|
|
97
|
+
current = Number(current) || 1;
|
|
98
|
+
size = Number(size) || 10;
|
|
99
|
+
const pageIndex = current - 1;
|
|
100
|
+
const skip = size * (pageIndex < 0 ? 0 : pageIndex);
|
|
101
|
+
const take = size;
|
|
102
|
+
return {
|
|
103
|
+
current,
|
|
104
|
+
size,
|
|
105
|
+
skip,
|
|
106
|
+
take,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { DataSource } from 'typeorm';
|
|
2
|
+
export interface PrepareResult {
|
|
3
|
+
formattedSP: string;
|
|
4
|
+
paramaters?: any[];
|
|
5
|
+
}
|
|
6
|
+
export declare abstract class BaseRepository {
|
|
7
|
+
readonly dataSource: DataSource;
|
|
8
|
+
constructor(dataSource: DataSource);
|
|
9
|
+
}
|
|
10
|
+
export declare abstract class BaseSpRepository extends BaseRepository {
|
|
11
|
+
readonly executeCommand: string;
|
|
12
|
+
constructor(dataSource: DataSource, executeCommand?: string);
|
|
13
|
+
callQuery(spName: string, formatOrValue?: any, params?: any, rename?: (key: string, depth: number, object: any) => string, objectifyForSignleRecord?: boolean): Promise<any>;
|
|
14
|
+
private prepare;
|
|
15
|
+
private renameKey;
|
|
16
|
+
private changeKey;
|
|
17
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseSpRepository = exports.BaseRepository = void 0;
|
|
4
|
+
const common_1 = require("@nathapp/common");
|
|
5
|
+
class BaseRepository {
|
|
6
|
+
constructor(dataSource) {
|
|
7
|
+
this.dataSource = dataSource;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.BaseRepository = BaseRepository;
|
|
11
|
+
class BaseSpRepository extends BaseRepository {
|
|
12
|
+
constructor(dataSource, executeCommand) {
|
|
13
|
+
super(dataSource);
|
|
14
|
+
this.executeCommand = executeCommand || 'call';
|
|
15
|
+
}
|
|
16
|
+
async callQuery(spName, formatOrValue, params, rename, objectifyForSignleRecord = false) {
|
|
17
|
+
const { formattedSP, paramaters } = await this.prepare(spName, formatOrValue, params);
|
|
18
|
+
return this.dataSource.query(formattedSP, paramaters).then((results) => {
|
|
19
|
+
if (!Array.isArray(results)) {
|
|
20
|
+
if (objectifyForSignleRecord) {
|
|
21
|
+
return {};
|
|
22
|
+
}
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
const status = results.pop();
|
|
26
|
+
if (1 < results.length) {
|
|
27
|
+
const recordsets = [];
|
|
28
|
+
for (let i = 0; results.length > i; i++) {
|
|
29
|
+
const recordset = results[i];
|
|
30
|
+
if (!rename) {
|
|
31
|
+
recordsets.push(recordset);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
const result = [];
|
|
35
|
+
for (let i = 0; recordset.length > i; i++) {
|
|
36
|
+
const record = recordset[i];
|
|
37
|
+
result.push(this.renameKey(record, rename));
|
|
38
|
+
}
|
|
39
|
+
recordsets.push(result);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return recordsets;
|
|
43
|
+
}
|
|
44
|
+
const recordset = results[0];
|
|
45
|
+
if (objectifyForSignleRecord) {
|
|
46
|
+
if (1 == recordset.length) {
|
|
47
|
+
if (rename) {
|
|
48
|
+
return this.renameKey(recordset[0], rename);
|
|
49
|
+
}
|
|
50
|
+
return recordset[0];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (rename) {
|
|
54
|
+
const result = [];
|
|
55
|
+
for (let i = 0; recordset.length > i; i++) {
|
|
56
|
+
result.push(this.renameKey(recordset[i], rename));
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
return recordset;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
async prepare(spName, formatOrValue, params) {
|
|
64
|
+
let paramaters = [];
|
|
65
|
+
if (!(0, common_1.isUndefinedOrNullOrEmpty)(formatOrValue) &&
|
|
66
|
+
Array.isArray(formatOrValue)) {
|
|
67
|
+
paramaters = formatOrValue;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
if (!(0, common_1.isUndefinedOrNullOrEmpty)(params)) {
|
|
71
|
+
if (!(0, common_1.isUndefinedOrNullOrEmpty)(formatOrValue)) {
|
|
72
|
+
params = (0, common_1.checkFormat)(formatOrValue, params);
|
|
73
|
+
for (const key of Object.keys(params)) {
|
|
74
|
+
paramaters.push(params[key]);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
let qm;
|
|
80
|
+
if (0 >= paramaters.length) {
|
|
81
|
+
qm = '';
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
qm = '?';
|
|
85
|
+
if (1 < paramaters.length) {
|
|
86
|
+
qm += ',?'.repeat(paramaters.length - 1);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
formattedSP: `${this.executeCommand} ${spName}(${qm})`,
|
|
91
|
+
paramaters,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
renameKey(object, rename, depth) {
|
|
95
|
+
for (const key in object) {
|
|
96
|
+
try {
|
|
97
|
+
if (undefined == depth) {
|
|
98
|
+
depth = 0;
|
|
99
|
+
}
|
|
100
|
+
if (object[key] && 'object' === typeof object[key]) {
|
|
101
|
+
const type = object[key].constructor
|
|
102
|
+
? object[key].constructor.name
|
|
103
|
+
: '';
|
|
104
|
+
switch (type) {
|
|
105
|
+
case 'Object':
|
|
106
|
+
case 'Array':
|
|
107
|
+
this.renameKey(object[key], rename, depth + 1);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const newKey = rename(key, depth, object);
|
|
111
|
+
this.changeKey(object, key, newKey);
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
error.checkKey = key;
|
|
115
|
+
error.checkValue = object[key];
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return object;
|
|
120
|
+
}
|
|
121
|
+
changeKey(object, name, newName) {
|
|
122
|
+
if (!object || name == newName) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if ('object' === typeof object) {
|
|
126
|
+
const item = Object.getOwnPropertyDescriptor(object, name);
|
|
127
|
+
if (item) {
|
|
128
|
+
Object.defineProperty(object, newName, item);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
object[newName] = object[name];
|
|
133
|
+
}
|
|
134
|
+
delete object[name];
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.BaseSpRepository = BaseSpRepository;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseSpRepository = exports.BaseRepository = void 0;
|
|
4
|
+
const base_repository_1 = require("./base.repository");
|
|
5
|
+
Object.defineProperty(exports, "BaseRepository", { enumerable: true, get: function () { return base_repository_1.BaseRepository; } });
|
|
6
|
+
Object.defineProperty(exports, "BaseSpRepository", { enumerable: true, get: function () { return base_repository_1.BaseSpRepository; } });
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from "@nestjs/typeorm";
|
|
2
|
+
import { EntitySchema, MixedList } from "typeorm";
|
|
3
|
+
import { BaseDataSourceOptions } from "typeorm/data-source/BaseDataSourceOptions";
|
|
4
|
+
export type OptionWithoutEntities = Omit<Partial<BaseDataSourceOptions>, "entities" | "type">;
|
|
5
|
+
export declare class BaseTypeOrmModuleOptionFactory implements TypeOrmOptionsFactory {
|
|
6
|
+
private readonly connectionString;
|
|
7
|
+
private readonly entities?;
|
|
8
|
+
private readonly options?;
|
|
9
|
+
private connectionType;
|
|
10
|
+
constructor(connectionString: string, entities?: MixedList<Function | string | EntitySchema>, options?: OptionWithoutEntities);
|
|
11
|
+
createTypeOrmOptions(connectionName?: string): TypeOrmModuleOptions | Promise<TypeOrmModuleOptions>;
|
|
12
|
+
}
|
|
13
|
+
export declare class DefaultTypeOrmModuleOptionFactory extends BaseTypeOrmModuleOptionFactory {
|
|
14
|
+
constructor(entities?: MixedList<Function | string | EntitySchema>, options?: OptionWithoutEntities);
|
|
15
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DefaultTypeOrmModuleOptionFactory = exports.BaseTypeOrmModuleOptionFactory = void 0;
|
|
4
|
+
const typeorm_utils_1 = require("./typeorm-utils");
|
|
5
|
+
class BaseTypeOrmModuleOptionFactory {
|
|
6
|
+
constructor(connectionString, entities, options) {
|
|
7
|
+
this.connectionString = connectionString;
|
|
8
|
+
this.entities = entities;
|
|
9
|
+
this.options = options;
|
|
10
|
+
if (!connectionString || connectionString == "") {
|
|
11
|
+
throw new Error("connectionString is missing");
|
|
12
|
+
}
|
|
13
|
+
const isPresent = connectionString && connectionString != "";
|
|
14
|
+
this.connectionType = (isPresent ? this.connectionString.split(":").shift() : null);
|
|
15
|
+
}
|
|
16
|
+
createTypeOrmOptions(connectionName) {
|
|
17
|
+
const options = (0, typeorm_utils_1.sanatizeTypeOrmOptions)(this.connectionType, this.options);
|
|
18
|
+
switch (this.connectionType) {
|
|
19
|
+
case "mysql":
|
|
20
|
+
case "mariadb":
|
|
21
|
+
case "mongodb":
|
|
22
|
+
return {
|
|
23
|
+
name: connectionName,
|
|
24
|
+
type: this.connectionType,
|
|
25
|
+
url: this.connectionString,
|
|
26
|
+
entities: this.entities || [],
|
|
27
|
+
...options,
|
|
28
|
+
};
|
|
29
|
+
default:
|
|
30
|
+
throw new Error("Connection Not Supported");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.BaseTypeOrmModuleOptionFactory = BaseTypeOrmModuleOptionFactory;
|
|
35
|
+
class DefaultTypeOrmModuleOptionFactory extends BaseTypeOrmModuleOptionFactory {
|
|
36
|
+
constructor(entities, options) {
|
|
37
|
+
super(process.env.MYSQL, entities, options);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.DefaultTypeOrmModuleOptionFactory = DefaultTypeOrmModuleOptionFactory;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { SnakeNamingStrategy } from "typeorm-naming-strategies";
|
|
2
|
+
export declare const defaultExtraOptions: {
|
|
3
|
+
entities: any[];
|
|
4
|
+
logging: boolean;
|
|
5
|
+
synchronize: boolean;
|
|
6
|
+
autoLoadEntities: boolean;
|
|
7
|
+
namingStrategy: SnakeNamingStrategy;
|
|
8
|
+
};
|
|
9
|
+
export declare const defaultMysqlExtraOptions: {
|
|
10
|
+
supportBigNumbers: boolean;
|
|
11
|
+
bigNumberStrings: boolean;
|
|
12
|
+
};
|
|
13
|
+
export declare const defaultMongoExtraOptions: {
|
|
14
|
+
useNewUrlParser: boolean;
|
|
15
|
+
useUnifiedTopology: boolean;
|
|
16
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultMongoExtraOptions = exports.defaultMysqlExtraOptions = exports.defaultExtraOptions = void 0;
|
|
4
|
+
const typeorm_naming_strategies_1 = require("typeorm-naming-strategies");
|
|
5
|
+
exports.defaultExtraOptions = {
|
|
6
|
+
entities: [],
|
|
7
|
+
logging: false,
|
|
8
|
+
synchronize: false,
|
|
9
|
+
autoLoadEntities: true,
|
|
10
|
+
namingStrategy: new typeorm_naming_strategies_1.SnakeNamingStrategy(),
|
|
11
|
+
};
|
|
12
|
+
exports.defaultMysqlExtraOptions = {
|
|
13
|
+
supportBigNumbers: true,
|
|
14
|
+
bigNumberStrings: true,
|
|
15
|
+
};
|
|
16
|
+
exports.defaultMongoExtraOptions = {
|
|
17
|
+
useNewUrlParser: true,
|
|
18
|
+
useUnifiedTopology: true,
|
|
19
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { BaseTypeOrmModuleOptionFactory, DefaultTypeOrmModuleOptionFactory, OptionWithoutEntities } from "./base-typeorm-options-factory";
|
|
2
|
+
import { defaultExtraOptions, defaultMongoExtraOptions, defaultMysqlExtraOptions } from "./constants";
|
|
3
|
+
import { DEFAULT_DATA_SOURCE_NAME, SupportedConnectionType, TypeOrmExModule } from "./typeorm-ex.module";
|
|
4
|
+
import { sanatizeTypeOrmOptions } from "./typeorm-utils";
|
|
5
|
+
export { TypeOrmExModule, DEFAULT_DATA_SOURCE_NAME, defaultExtraOptions, defaultMysqlExtraOptions, defaultMongoExtraOptions, SupportedConnectionType, sanatizeTypeOrmOptions, BaseTypeOrmModuleOptionFactory, OptionWithoutEntities, DefaultTypeOrmModuleOptionFactory, };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DefaultTypeOrmModuleOptionFactory = exports.BaseTypeOrmModuleOptionFactory = exports.sanatizeTypeOrmOptions = exports.defaultMongoExtraOptions = exports.defaultMysqlExtraOptions = exports.defaultExtraOptions = exports.DEFAULT_DATA_SOURCE_NAME = exports.TypeOrmExModule = void 0;
|
|
4
|
+
const base_typeorm_options_factory_1 = require("./base-typeorm-options-factory");
|
|
5
|
+
Object.defineProperty(exports, "BaseTypeOrmModuleOptionFactory", { enumerable: true, get: function () { return base_typeorm_options_factory_1.BaseTypeOrmModuleOptionFactory; } });
|
|
6
|
+
Object.defineProperty(exports, "DefaultTypeOrmModuleOptionFactory", { enumerable: true, get: function () { return base_typeorm_options_factory_1.DefaultTypeOrmModuleOptionFactory; } });
|
|
7
|
+
const constants_1 = require("./constants");
|
|
8
|
+
Object.defineProperty(exports, "defaultExtraOptions", { enumerable: true, get: function () { return constants_1.defaultExtraOptions; } });
|
|
9
|
+
Object.defineProperty(exports, "defaultMongoExtraOptions", { enumerable: true, get: function () { return constants_1.defaultMongoExtraOptions; } });
|
|
10
|
+
Object.defineProperty(exports, "defaultMysqlExtraOptions", { enumerable: true, get: function () { return constants_1.defaultMysqlExtraOptions; } });
|
|
11
|
+
const typeorm_ex_module_1 = require("./typeorm-ex.module");
|
|
12
|
+
Object.defineProperty(exports, "DEFAULT_DATA_SOURCE_NAME", { enumerable: true, get: function () { return typeorm_ex_module_1.DEFAULT_DATA_SOURCE_NAME; } });
|
|
13
|
+
Object.defineProperty(exports, "TypeOrmExModule", { enumerable: true, get: function () { return typeorm_ex_module_1.TypeOrmExModule; } });
|
|
14
|
+
const typeorm_utils_1 = require("./typeorm-utils");
|
|
15
|
+
Object.defineProperty(exports, "sanatizeTypeOrmOptions", { enumerable: true, get: function () { return typeorm_utils_1.sanatizeTypeOrmOptions; } });
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DynamicModule } from "@nestjs/common";
|
|
2
|
+
import { TypeOrmModuleAsyncOptions, TypeOrmModuleOptions } from "@nestjs/typeorm";
|
|
3
|
+
import { DataSource, DataSourceOptions } from "typeorm";
|
|
4
|
+
export declare const DEFAULT_DATA_SOURCE_NAME = "default";
|
|
5
|
+
export type SupportedConnectionType = "mysql" | "mariadb" | "mongodb";
|
|
6
|
+
export declare class TypeOrmExModule {
|
|
7
|
+
static forRoot(options?: TypeOrmModuleOptions): DynamicModule;
|
|
8
|
+
static forRootAsync(options?: TypeOrmModuleAsyncOptions): DynamicModule;
|
|
9
|
+
static forCustomRepository<T extends new (...args: any[]) => any>(repositories: T[], dataSource?: DataSource | DataSourceOptions | string): DynamicModule;
|
|
10
|
+
private static sanitizeOptions;
|
|
11
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TypeOrmExModule = exports.DEFAULT_DATA_SOURCE_NAME = void 0;
|
|
4
|
+
const typeorm_1 = require("@nestjs/typeorm");
|
|
5
|
+
const decorator_1 = require("../decorator");
|
|
6
|
+
const typeorm_utils_1 = require("./typeorm-utils");
|
|
7
|
+
const repository_1 = require("../repository");
|
|
8
|
+
exports.DEFAULT_DATA_SOURCE_NAME = 'default';
|
|
9
|
+
class TypeOrmExModule {
|
|
10
|
+
static forRoot(options) {
|
|
11
|
+
options = this.sanitizeOptions(options);
|
|
12
|
+
return {
|
|
13
|
+
module: TypeOrmExModule,
|
|
14
|
+
imports: [typeorm_1.TypeOrmModule.forRoot(options)],
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
static forRootAsync(options) {
|
|
18
|
+
return {
|
|
19
|
+
module: TypeOrmExModule,
|
|
20
|
+
imports: [typeorm_1.TypeOrmModule.forRootAsync(options)],
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
static forCustomRepository(repositories, dataSource = exports.DEFAULT_DATA_SOURCE_NAME) {
|
|
24
|
+
const providers = [];
|
|
25
|
+
for (const repository of repositories) {
|
|
26
|
+
const entity = Reflect.getMetadata(decorator_1.TYPEORM_EX_CUSTOM_REPOSITORY, repository);
|
|
27
|
+
if (!entity) {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
providers.push({
|
|
31
|
+
inject: [(0, typeorm_1.getDataSourceToken)(dataSource)],
|
|
32
|
+
provide: repository,
|
|
33
|
+
useFactory: (dataSource) => {
|
|
34
|
+
if (repository instanceof repository_1.BaseRepository) {
|
|
35
|
+
return new repository(dataSource);
|
|
36
|
+
}
|
|
37
|
+
const baseRepository = dataSource.getRepository(entity);
|
|
38
|
+
const customRepository = new repository(baseRepository.target, baseRepository.manager);
|
|
39
|
+
Object.defineProperty(customRepository, 'manager', {
|
|
40
|
+
get() {
|
|
41
|
+
return baseRepository.manager;
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
return customRepository;
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
exports: providers,
|
|
50
|
+
module: TypeOrmExModule,
|
|
51
|
+
providers,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
static sanitizeOptions(options) {
|
|
55
|
+
let connectionType;
|
|
56
|
+
if (options.type) {
|
|
57
|
+
connectionType = options.type;
|
|
58
|
+
}
|
|
59
|
+
else if (options["url"]) {
|
|
60
|
+
connectionType = options["url"].split(":").shift();
|
|
61
|
+
}
|
|
62
|
+
if (!connectionType) {
|
|
63
|
+
throw new Error("Connection Not Supported");
|
|
64
|
+
}
|
|
65
|
+
return (0, typeorm_utils_1.sanatizeTypeOrmOptions)(connectionType, options);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.TypeOrmExModule = TypeOrmExModule;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { TypeOrmModuleOptions } from "@nestjs/typeorm";
|
|
2
|
+
import { SupportedConnectionType } from "./typeorm-ex.module";
|
|
3
|
+
import { OptionWithoutEntities } from "./base-typeorm-options-factory";
|
|
4
|
+
export declare const sanatizeTypeOrmOptions: (connectionType: SupportedConnectionType, options: TypeOrmModuleOptions | OptionWithoutEntities) => ({
|
|
5
|
+
entities: any[];
|
|
6
|
+
logging: boolean;
|
|
7
|
+
synchronize: boolean;
|
|
8
|
+
autoLoadEntities: boolean;
|
|
9
|
+
namingStrategy: import("typeorm-naming-strategies").SnakeNamingStrategy;
|
|
10
|
+
} & {
|
|
11
|
+
supportBigNumbers: boolean;
|
|
12
|
+
bigNumberStrings: boolean;
|
|
13
|
+
} & {
|
|
14
|
+
type: SupportedConnectionType;
|
|
15
|
+
} & OptionWithoutEntities) | ({
|
|
16
|
+
entities: any[];
|
|
17
|
+
logging: boolean;
|
|
18
|
+
synchronize: boolean;
|
|
19
|
+
autoLoadEntities: boolean;
|
|
20
|
+
namingStrategy: import("typeorm-naming-strategies").SnakeNamingStrategy;
|
|
21
|
+
} & {
|
|
22
|
+
useNewUrlParser: boolean;
|
|
23
|
+
useUnifiedTopology: boolean;
|
|
24
|
+
} & {
|
|
25
|
+
type: SupportedConnectionType;
|
|
26
|
+
} & OptionWithoutEntities);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sanatizeTypeOrmOptions = void 0;
|
|
4
|
+
const constants_1 = require("./constants");
|
|
5
|
+
const sanatizeTypeOrmOptions = (connectionType, options) => {
|
|
6
|
+
const dbType = { type: connectionType };
|
|
7
|
+
switch (connectionType) {
|
|
8
|
+
case "mysql":
|
|
9
|
+
case "mariadb":
|
|
10
|
+
return Object.assign(constants_1.defaultExtraOptions, constants_1.defaultMysqlExtraOptions, dbType, options);
|
|
11
|
+
case "mongodb":
|
|
12
|
+
return Object.assign(constants_1.defaultExtraOptions, constants_1.defaultMongoExtraOptions, dbType, options);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
exports.sanatizeTypeOrmOptions = sanatizeTypeOrmOptions;
|
package/package.json
CHANGED
|
@@ -1,29 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nathapp/nestjs-data",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.10",
|
|
4
4
|
"description": "@nathapp/nestjs-data",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "rimraf dist && tsc -p tsconfig.json",
|
|
7
7
|
"precommit": "lint-staged",
|
|
8
|
-
"test": "jest",
|
|
9
|
-
"test:
|
|
10
|
-
"test:
|
|
11
|
-
"test:e2e": "jest --config ./test/jest-e2e.json",
|
|
8
|
+
"test": "jest --config ./test/jest-test.json",
|
|
9
|
+
"test:cov": "jest --config ./test/jest-test.json --coverage",
|
|
10
|
+
"test:watch": "jest --config ./test/jest-test.json --watch",
|
|
12
11
|
"lint": "eslint -c .eslintrc.js"
|
|
13
12
|
},
|
|
14
13
|
"author": "",
|
|
15
14
|
"license": "ISC",
|
|
16
15
|
"devDependencies": {
|
|
17
|
-
"@
|
|
18
|
-
"@nestjs/
|
|
19
|
-
"@types/
|
|
16
|
+
"@golevelup/ts-jest": "^0.3.7",
|
|
17
|
+
"@nestjs/testing": "^10.0.0",
|
|
18
|
+
"@types/jest": "^29.5.2",
|
|
20
19
|
"@types/node": "^17.0.22",
|
|
21
20
|
"@typescript-eslint/eslint-plugin": "^4",
|
|
22
21
|
"@typescript-eslint/parser": "^4",
|
|
23
22
|
"eslint": "*",
|
|
24
23
|
"eslint-config-prettier": "^6",
|
|
25
24
|
"eslint-plugin-prettier": "^3",
|
|
25
|
+
"jest": "^29.5.0",
|
|
26
26
|
"prettier": "^2",
|
|
27
|
+
"supertest": "^6.3.3",
|
|
28
|
+
"ts-jest": "^29.1.0",
|
|
27
29
|
"typescript": "^4"
|
|
28
30
|
},
|
|
29
31
|
"types": "./index.d.ts",
|
|
@@ -32,14 +34,16 @@
|
|
|
32
34
|
"lib": "lib"
|
|
33
35
|
},
|
|
34
36
|
"dependencies": {
|
|
35
|
-
"@
|
|
37
|
+
"@nathapp/common": "^0.1.13",
|
|
38
|
+
"@nathapp/nestjs-common": "^1.0.1-rc.22",
|
|
36
39
|
"moment": "^2.29.4",
|
|
37
40
|
"typeorm": "^0.3.12",
|
|
38
41
|
"typeorm-naming-strategies": "^4.1.0"
|
|
39
42
|
},
|
|
40
43
|
"peerDependencies": {
|
|
41
|
-
"@nestjs/common": "
|
|
42
|
-
"@nestjs/core": "
|
|
44
|
+
"@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0",
|
|
45
|
+
"@nestjs/core": "^8.0.0 || ^9.0.0 || ^10.0.0",
|
|
46
|
+
"@nestjs/typeorm": "^8.0.0 || ^9.0.0 || ^10.0.0",
|
|
43
47
|
"class-transformer": "*",
|
|
44
48
|
"class-validator": "*",
|
|
45
49
|
"rxjs": "*"
|