@kipicore/dbcore 1.1.2 → 1.1.3
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.
|
@@ -15,6 +15,8 @@ const areaModel_js_1 = __importDefault(require("./areaModel.js"));
|
|
|
15
15
|
const utils_js_1 = require("../../helpers/utils.js");
|
|
16
16
|
const uniqueNumberCounterModel_js_1 = __importDefault(require("../mongodb/uniqueNumberCounterModel.js"));
|
|
17
17
|
class UserModel extends sequelize_1.Model {
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
+
static updateById;
|
|
18
20
|
// declare addParent: BelongsToManyAddAssociationMixin<UserModel, string>;
|
|
19
21
|
// declare getParents: BelongsToManyGetAssociationsMixin<UserModel>;
|
|
20
22
|
static associate() { }
|
|
@@ -3,55 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.MongooseCommonService = void 0;
|
|
4
4
|
const app_js_1 = require("../../constants/app.js");
|
|
5
5
|
class MongooseCommonService {
|
|
6
|
+
model;
|
|
6
7
|
constructor(model) {
|
|
7
|
-
this.findAllWithPagination = async (filter, options = {}, populate) => {
|
|
8
|
-
try {
|
|
9
|
-
const convertOrderToSort = (order) => {
|
|
10
|
-
const sort = {};
|
|
11
|
-
for (const [key, direction] of order) {
|
|
12
|
-
sort[key] = direction.toUpperCase() === "DESC" ? -1 : 1;
|
|
13
|
-
}
|
|
14
|
-
return sort;
|
|
15
|
-
};
|
|
16
|
-
const { order, projection, ...restOptions } = options;
|
|
17
|
-
let { page, limit } = options;
|
|
18
|
-
const sort = convertOrderToSort(order || app_js_1.PAGINATION_ORDER);
|
|
19
|
-
// Ensure page and limit are positive integers
|
|
20
|
-
page = Math.max(1, page || app_js_1.PAGINATION.PAGE);
|
|
21
|
-
limit = Math.max(1, limit || app_js_1.PAGINATION.LIMIT);
|
|
22
|
-
// Calculate offset
|
|
23
|
-
const skip = (page - 1) * limit;
|
|
24
|
-
// Count total records
|
|
25
|
-
const totalRecords = await this.model.countDocuments(filter).exec();
|
|
26
|
-
const totalPages = Math.ceil(totalRecords / limit);
|
|
27
|
-
// Query the records
|
|
28
|
-
const query = this.model.find(filter, projection, {
|
|
29
|
-
...restOptions,
|
|
30
|
-
limit,
|
|
31
|
-
skip,
|
|
32
|
-
sort,
|
|
33
|
-
});
|
|
34
|
-
// Apply populate if necessary
|
|
35
|
-
if (populate) {
|
|
36
|
-
query.populate(populate);
|
|
37
|
-
}
|
|
38
|
-
const recordList = await query.exec();
|
|
39
|
-
// Construct the pagination result
|
|
40
|
-
const paginationOptions = {
|
|
41
|
-
limit,
|
|
42
|
-
totalRecords,
|
|
43
|
-
totalPages,
|
|
44
|
-
hasPreviousPage: page > 1,
|
|
45
|
-
currentPage: Math.min(page, totalPages),
|
|
46
|
-
hasNextPage: page < totalPages,
|
|
47
|
-
recordList,
|
|
48
|
-
};
|
|
49
|
-
return paginationOptions;
|
|
50
|
-
}
|
|
51
|
-
catch (err) {
|
|
52
|
-
throw err;
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
8
|
this.model = model;
|
|
56
9
|
}
|
|
57
10
|
async findAll(filter, options = {}, populate) {
|
|
@@ -72,6 +25,54 @@ class MongooseCommonService {
|
|
|
72
25
|
query.populate(populate);
|
|
73
26
|
return query.exec();
|
|
74
27
|
}
|
|
28
|
+
findAllWithPagination = async (filter, options = {}, populate) => {
|
|
29
|
+
try {
|
|
30
|
+
const convertOrderToSort = (order) => {
|
|
31
|
+
const sort = {};
|
|
32
|
+
for (const [key, direction] of order) {
|
|
33
|
+
sort[key] = direction.toUpperCase() === "DESC" ? -1 : 1;
|
|
34
|
+
}
|
|
35
|
+
return sort;
|
|
36
|
+
};
|
|
37
|
+
const { order, projection, ...restOptions } = options;
|
|
38
|
+
let { page, limit } = options;
|
|
39
|
+
const sort = convertOrderToSort(order || app_js_1.PAGINATION_ORDER);
|
|
40
|
+
// Ensure page and limit are positive integers
|
|
41
|
+
page = Math.max(1, page || app_js_1.PAGINATION.PAGE);
|
|
42
|
+
limit = Math.max(1, limit || app_js_1.PAGINATION.LIMIT);
|
|
43
|
+
// Calculate offset
|
|
44
|
+
const skip = (page - 1) * limit;
|
|
45
|
+
// Count total records
|
|
46
|
+
const totalRecords = await this.model.countDocuments(filter).exec();
|
|
47
|
+
const totalPages = Math.ceil(totalRecords / limit);
|
|
48
|
+
// Query the records
|
|
49
|
+
const query = this.model.find(filter, projection, {
|
|
50
|
+
...restOptions,
|
|
51
|
+
limit,
|
|
52
|
+
skip,
|
|
53
|
+
sort,
|
|
54
|
+
});
|
|
55
|
+
// Apply populate if necessary
|
|
56
|
+
if (populate) {
|
|
57
|
+
query.populate(populate);
|
|
58
|
+
}
|
|
59
|
+
const recordList = await query.exec();
|
|
60
|
+
// Construct the pagination result
|
|
61
|
+
const paginationOptions = {
|
|
62
|
+
limit,
|
|
63
|
+
totalRecords,
|
|
64
|
+
totalPages,
|
|
65
|
+
hasPreviousPage: page > 1,
|
|
66
|
+
currentPage: Math.min(page, totalPages),
|
|
67
|
+
hasNextPage: page < totalPages,
|
|
68
|
+
recordList,
|
|
69
|
+
};
|
|
70
|
+
return paginationOptions;
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
75
76
|
async count(filter) {
|
|
76
77
|
return this.model.countDocuments(filter).exec();
|
|
77
78
|
}
|
|
@@ -2,113 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const app_js_1 = require("../../constants/app.js");
|
|
4
4
|
class SequelizeCommonService {
|
|
5
|
+
model;
|
|
5
6
|
constructor(model) {
|
|
6
|
-
this.findAll = (where, options) => {
|
|
7
|
-
try {
|
|
8
|
-
const finalOptions = {
|
|
9
|
-
...(options || {}),
|
|
10
|
-
order: options?.order || app_js_1.PAGINATION_ORDER,
|
|
11
|
-
};
|
|
12
|
-
return this.model.findAll({ where, ...finalOptions });
|
|
13
|
-
}
|
|
14
|
-
catch (err) {
|
|
15
|
-
throw err;
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
this.findAllWithPagination = async (where, options) => {
|
|
19
|
-
try {
|
|
20
|
-
if (!options)
|
|
21
|
-
options = {};
|
|
22
|
-
const limit = options.limit || app_js_1.PAGINATION.LIMIT;
|
|
23
|
-
const currentPage = options.page || app_js_1.PAGINATION.PAGE;
|
|
24
|
-
// Clean options for count()
|
|
25
|
-
const countOptions = { ...options };
|
|
26
|
-
// options.subQuery = false;
|
|
27
|
-
delete countOptions.order;
|
|
28
|
-
delete countOptions.limit;
|
|
29
|
-
delete countOptions.offset;
|
|
30
|
-
const totalRecords = await this.model.count({
|
|
31
|
-
where,
|
|
32
|
-
include: options.include,
|
|
33
|
-
distinct: true,
|
|
34
|
-
});
|
|
35
|
-
const totalPages = Math.ceil(totalRecords / limit);
|
|
36
|
-
// Apply pagination + order for findAll
|
|
37
|
-
options.limit = limit;
|
|
38
|
-
options.offset = (currentPage - 1) * limit;
|
|
39
|
-
options.order = options.order || app_js_1.PAGINATION_ORDER;
|
|
40
|
-
const listOfRecords = await this.findAll(where, options);
|
|
41
|
-
const paginationOptions = {
|
|
42
|
-
limit,
|
|
43
|
-
totalRecords,
|
|
44
|
-
totalPages,
|
|
45
|
-
hasPreviousPage: currentPage - 1 > 0 ? true : false,
|
|
46
|
-
currentPage: currentPage > totalPages ? totalPages : currentPage,
|
|
47
|
-
hasNextPage: currentPage < totalPages,
|
|
48
|
-
recordList: listOfRecords,
|
|
49
|
-
};
|
|
50
|
-
return paginationOptions;
|
|
51
|
-
}
|
|
52
|
-
catch (err) {
|
|
53
|
-
throw err;
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
this.findOne = async (where, options) => {
|
|
57
|
-
try {
|
|
58
|
-
return this.model.findOne({ where, ...options });
|
|
59
|
-
}
|
|
60
|
-
catch (err) {
|
|
61
|
-
throw err;
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
this.update = async (where, updateData, options) => {
|
|
65
|
-
try {
|
|
66
|
-
return this.model.update(updateData, { where, ...options });
|
|
67
|
-
}
|
|
68
|
-
catch (err) {
|
|
69
|
-
throw err;
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
this.upsert = async (conflictWhere, updateData, options) => {
|
|
73
|
-
try {
|
|
74
|
-
return this.model.upsert(updateData, { conflictWhere, ...options });
|
|
75
|
-
}
|
|
76
|
-
catch (err) {
|
|
77
|
-
throw err;
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
this.create = async (createData, options) => {
|
|
81
|
-
try {
|
|
82
|
-
return this.model.create(createData, options);
|
|
83
|
-
}
|
|
84
|
-
catch (err) {
|
|
85
|
-
throw err;
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
this.bulkCreate = async (createData, options) => {
|
|
89
|
-
try {
|
|
90
|
-
return this.model.bulkCreate(createData, options);
|
|
91
|
-
}
|
|
92
|
-
catch (err) {
|
|
93
|
-
throw err;
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
this.delete = async (where, options) => {
|
|
97
|
-
try {
|
|
98
|
-
return this.model.destroy({ where, ...options });
|
|
99
|
-
}
|
|
100
|
-
catch (err) {
|
|
101
|
-
throw err;
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
this.count = (where, options) => {
|
|
105
|
-
try {
|
|
106
|
-
return this.model.count({ where, ...options });
|
|
107
|
-
}
|
|
108
|
-
catch (err) {
|
|
109
|
-
throw err;
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
7
|
this.model = model;
|
|
113
8
|
}
|
|
114
9
|
findByPk(identifier, options) {
|
|
@@ -119,5 +14,111 @@ class SequelizeCommonService {
|
|
|
119
14
|
throw err;
|
|
120
15
|
}
|
|
121
16
|
}
|
|
17
|
+
findAll = (where, options) => {
|
|
18
|
+
try {
|
|
19
|
+
const finalOptions = {
|
|
20
|
+
...(options || {}),
|
|
21
|
+
order: options?.order || app_js_1.PAGINATION_ORDER,
|
|
22
|
+
};
|
|
23
|
+
return this.model.findAll({ where, ...finalOptions });
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
throw err;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
findAllWithPagination = async (where, options) => {
|
|
30
|
+
try {
|
|
31
|
+
if (!options)
|
|
32
|
+
options = {};
|
|
33
|
+
const limit = options.limit || app_js_1.PAGINATION.LIMIT;
|
|
34
|
+
const currentPage = options.page || app_js_1.PAGINATION.PAGE;
|
|
35
|
+
// Clean options for count()
|
|
36
|
+
const countOptions = { ...options };
|
|
37
|
+
// options.subQuery = false;
|
|
38
|
+
delete countOptions.order;
|
|
39
|
+
delete countOptions.limit;
|
|
40
|
+
delete countOptions.offset;
|
|
41
|
+
const totalRecords = await this.model.count({
|
|
42
|
+
where,
|
|
43
|
+
include: options.include,
|
|
44
|
+
distinct: true,
|
|
45
|
+
});
|
|
46
|
+
const totalPages = Math.ceil(totalRecords / limit);
|
|
47
|
+
// Apply pagination + order for findAll
|
|
48
|
+
options.limit = limit;
|
|
49
|
+
options.offset = (currentPage - 1) * limit;
|
|
50
|
+
options.order = options.order || app_js_1.PAGINATION_ORDER;
|
|
51
|
+
const listOfRecords = await this.findAll(where, options);
|
|
52
|
+
const paginationOptions = {
|
|
53
|
+
limit,
|
|
54
|
+
totalRecords,
|
|
55
|
+
totalPages,
|
|
56
|
+
hasPreviousPage: currentPage - 1 > 0 ? true : false,
|
|
57
|
+
currentPage: currentPage > totalPages ? totalPages : currentPage,
|
|
58
|
+
hasNextPage: currentPage < totalPages,
|
|
59
|
+
recordList: listOfRecords,
|
|
60
|
+
};
|
|
61
|
+
return paginationOptions;
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
throw err;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
findOne = async (where, options) => {
|
|
68
|
+
try {
|
|
69
|
+
return this.model.findOne({ where, ...options });
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
throw err;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
update = async (where, updateData, options) => {
|
|
76
|
+
try {
|
|
77
|
+
return this.model.update(updateData, { where, ...options });
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
throw err;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
upsert = async (conflictWhere, updateData, options) => {
|
|
84
|
+
try {
|
|
85
|
+
return this.model.upsert(updateData, { conflictWhere, ...options });
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
throw err;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
create = async (createData, options) => {
|
|
92
|
+
try {
|
|
93
|
+
return this.model.create(createData, options);
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
throw err;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
bulkCreate = async (createData, options) => {
|
|
100
|
+
try {
|
|
101
|
+
return this.model.bulkCreate(createData, options);
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
throw err;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
delete = async (where, options) => {
|
|
108
|
+
try {
|
|
109
|
+
return this.model.destroy({ where, ...options });
|
|
110
|
+
}
|
|
111
|
+
catch (err) {
|
|
112
|
+
throw err;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
count = (where, options) => {
|
|
116
|
+
try {
|
|
117
|
+
return this.model.count({ where, ...options });
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
throw err;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
122
123
|
}
|
|
123
124
|
exports.default = SequelizeCommonService;
|