c2-mongoose 2.1.13 → 2.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -18
- package/dist/configuration/Environment.d.ts +4 -4
- package/dist/configuration/Environment.js +5 -5
- package/dist/flow/CrudFlow.d.ts +19 -18
- package/dist/flow/CrudFlow.js +170 -149
- package/dist/flow/SearchFlow.d.ts +28 -28
- package/dist/flow/SearchFlow.js +308 -308
- package/dist/index.d.ts +4 -4
- package/dist/index.js +10 -10
- package/dist/utils/Utils.d.ts +6 -6
- package/dist/utils/Utils.js +56 -56
- package/package.json +1 -1
- package/src/configuration/Environment.ts +2 -2
- package/src/flow/CrudFlow.ts +66 -54
- package/src/flow/SearchFlow.ts +237 -237
- package/src/index.ts +5 -5
- package/src/types/SearchResponse.d.ts +9 -9
- package/src/utils/Utils.ts +56 -56
- package/tsconfig.json +12 -12
- package/dist/model/Search.d.ts +0 -26
- package/dist/model/Search.js +0 -265
package/dist/utils/Utils.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export declare const isNotEmpty: (obj: any) => boolean;
|
|
2
|
-
export declare const isEmpty: (obj: any) => boolean;
|
|
3
|
-
export declare const isIterable: (input: any) => boolean;
|
|
4
|
-
export declare const isNotIterable: (input: any) => boolean;
|
|
5
|
-
export declare const compareAsc: (a: any, b: any) => 0 | 1 | -1;
|
|
6
|
-
export declare const compareDesc: (a: any, b: any) => 0 | 1 | -1;
|
|
1
|
+
export declare const isNotEmpty: (obj: any) => boolean;
|
|
2
|
+
export declare const isEmpty: (obj: any) => boolean;
|
|
3
|
+
export declare const isIterable: (input: any) => boolean;
|
|
4
|
+
export declare const isNotIterable: (input: any) => boolean;
|
|
5
|
+
export declare const compareAsc: (a: any, b: any) => 0 | 1 | -1;
|
|
6
|
+
export declare const compareDesc: (a: any, b: any) => 0 | 1 | -1;
|
package/dist/utils/Utils.js
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.compareDesc = exports.compareAsc = exports.isNotIterable = exports.isIterable = exports.isEmpty = exports.isNotEmpty = void 0;
|
|
4
|
-
var isNotEmpty = function (obj) {
|
|
5
|
-
return !(0, exports.isEmpty)(obj);
|
|
6
|
-
};
|
|
7
|
-
exports.isNotEmpty = isNotEmpty;
|
|
8
|
-
var isEmpty = function (obj) {
|
|
9
|
-
if (!obj) {
|
|
10
|
-
return true;
|
|
11
|
-
}
|
|
12
|
-
if (Array.isArray(obj)) {
|
|
13
|
-
if (obj.length == 0) {
|
|
14
|
-
return true;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
if (obj === "") {
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
return false;
|
|
21
|
-
};
|
|
22
|
-
exports.isEmpty = isEmpty;
|
|
23
|
-
var isIterable = function (input) {
|
|
24
|
-
if (input === null || input === undefined) {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
if ('string' === typeof input) {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
return typeof input[Symbol.iterator] === 'function';
|
|
31
|
-
};
|
|
32
|
-
exports.isIterable = isIterable;
|
|
33
|
-
var isNotIterable = function (input) {
|
|
34
|
-
return !(0, exports.isIterable)(input);
|
|
35
|
-
};
|
|
36
|
-
exports.isNotIterable = isNotIterable;
|
|
37
|
-
var compareAsc = function (a, b) {
|
|
38
|
-
if (a._id < b._id) {
|
|
39
|
-
return -1;
|
|
40
|
-
}
|
|
41
|
-
if (a._id > b._id) {
|
|
42
|
-
return 1;
|
|
43
|
-
}
|
|
44
|
-
return 0;
|
|
45
|
-
};
|
|
46
|
-
exports.compareAsc = compareAsc;
|
|
47
|
-
var compareDesc = function (a, b) {
|
|
48
|
-
if (a._id > b._id) {
|
|
49
|
-
return -1;
|
|
50
|
-
}
|
|
51
|
-
if (a._id < b._id) {
|
|
52
|
-
return 1;
|
|
53
|
-
}
|
|
54
|
-
return 0;
|
|
55
|
-
};
|
|
56
|
-
exports.compareDesc = compareDesc;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.compareDesc = exports.compareAsc = exports.isNotIterable = exports.isIterable = exports.isEmpty = exports.isNotEmpty = void 0;
|
|
4
|
+
var isNotEmpty = function (obj) {
|
|
5
|
+
return !(0, exports.isEmpty)(obj);
|
|
6
|
+
};
|
|
7
|
+
exports.isNotEmpty = isNotEmpty;
|
|
8
|
+
var isEmpty = function (obj) {
|
|
9
|
+
if (!obj) {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
if (Array.isArray(obj)) {
|
|
13
|
+
if (obj.length == 0) {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
if (obj === "") {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
return false;
|
|
21
|
+
};
|
|
22
|
+
exports.isEmpty = isEmpty;
|
|
23
|
+
var isIterable = function (input) {
|
|
24
|
+
if (input === null || input === undefined) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
if ('string' === typeof input) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
return typeof input[Symbol.iterator] === 'function';
|
|
31
|
+
};
|
|
32
|
+
exports.isIterable = isIterable;
|
|
33
|
+
var isNotIterable = function (input) {
|
|
34
|
+
return !(0, exports.isIterable)(input);
|
|
35
|
+
};
|
|
36
|
+
exports.isNotIterable = isNotIterable;
|
|
37
|
+
var compareAsc = function (a, b) {
|
|
38
|
+
if (a._id < b._id) {
|
|
39
|
+
return -1;
|
|
40
|
+
}
|
|
41
|
+
if (a._id > b._id) {
|
|
42
|
+
return 1;
|
|
43
|
+
}
|
|
44
|
+
return 0;
|
|
45
|
+
};
|
|
46
|
+
exports.compareAsc = compareAsc;
|
|
47
|
+
var compareDesc = function (a, b) {
|
|
48
|
+
if (a._id > b._id) {
|
|
49
|
+
return -1;
|
|
50
|
+
}
|
|
51
|
+
if (a._id < b._id) {
|
|
52
|
+
return 1;
|
|
53
|
+
}
|
|
54
|
+
return 0;
|
|
55
|
+
};
|
|
56
|
+
exports.compareDesc = compareDesc;
|
package/package.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export const dbCollation = process.env.DB_COLLACTION as string
|
|
2
|
-
|
|
1
|
+
export const dbCollation = process.env.DB_COLLACTION as string
|
|
2
|
+
|
|
3
3
|
export default process.env
|
package/src/flow/CrudFlow.ts
CHANGED
|
@@ -1,55 +1,67 @@
|
|
|
1
|
-
import mongoose, { ClientSession } from "mongoose"
|
|
2
|
-
import { SearchResponse } from "../types/SearchResponse"
|
|
3
|
-
import SearchFlow from "./SearchFlow"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
private
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
this.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
1
|
+
import mongoose, { ClientSession } from "mongoose"
|
|
2
|
+
import { SearchResponse } from "../types/SearchResponse"
|
|
3
|
+
import SearchFlow from "./SearchFlow"
|
|
4
|
+
import { isEmpty, isNotEmpty } from "../utils/Utils"
|
|
5
|
+
|
|
6
|
+
class CrudFlow<D> {
|
|
7
|
+
|
|
8
|
+
private repository: mongoose.Model<any>
|
|
9
|
+
private search: any
|
|
10
|
+
|
|
11
|
+
constructor(repository: mongoose.Model<any>, search?: SearchFlow) {
|
|
12
|
+
this.repository = repository
|
|
13
|
+
this.search = search
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public prepareSearch(search: SearchFlow) {
|
|
17
|
+
this.search = search
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public async create(data: any, session: any = undefined) {
|
|
21
|
+
return await this.repository.create([data], { session })
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public async delete(id: string, session: any = undefined) {
|
|
25
|
+
await this.repository.findByIdAndRemove(id, { session })
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public async deleteMany(conditions: {}, session: any = undefined) {
|
|
29
|
+
await this.repository.deleteMany(conditions).session(session)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public async update(id: string, data: D, session: any = undefined): Promise<D> {
|
|
33
|
+
const dataAfter = await this.repository.findByIdAndUpdate(id, { $set: data as any }, { returnDocument: 'after', session })
|
|
34
|
+
return dataAfter as D
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public async find(): Promise<SearchResponse<D>> {
|
|
38
|
+
return await this.search.search(this.repository)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public async getOne(model: D, params: any = {}): Promise<D> {
|
|
42
|
+
const data = await this.search.findOne(this.repository, model, params)
|
|
43
|
+
return data as D
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public async get(id: string, pop = "", sel = "", session?: ClientSession): Promise<D> {
|
|
47
|
+
const data = await this.repository.findById(id).populate(pop).select(sel).session(session!)
|
|
48
|
+
return data as D
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public async getById(id: string, session?: ClientSession): Promise<Partial<D>> {
|
|
52
|
+
if (isEmpty(this.search)) {
|
|
53
|
+
throw new Error("Search params not informed")
|
|
54
|
+
}
|
|
55
|
+
const data = await this.repository.findById(id)
|
|
56
|
+
.populate(this.search.populate)
|
|
57
|
+
.select(this.search.select)
|
|
58
|
+
.session(session!)
|
|
59
|
+
return data as D
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public async sumBy(fieldToSum: string, fieldToGroup: string): Promise<any> {
|
|
63
|
+
return await this.search.sumBy(this.repository, fieldToSum, fieldToGroup)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
55
67
|
export default CrudFlow
|