@kipicore/dbcore 1.1.14 → 1.1.16
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/cjs/helpers/utils.d.ts +3 -3
- package/dist/cjs/interfaces/approvalRequestInterface.d.ts +2 -2
- package/dist/cjs/interfaces/userInstituteMetaInterface.d.ts +1 -1
- package/dist/cjs/models/mongodb/taskManagementModel.js +1 -1
- package/dist/cjs/models/psql/userModel.js +0 -2
- package/dist/cjs/services/Concrete/mongooseCommonService.js +48 -49
- package/dist/cjs/services/Concrete/sequelizeCommonService.js +106 -107
- package/dist/esm/helpers/utils.d.ts +3 -3
- package/dist/esm/interfaces/approvalRequestInterface.d.ts +2 -2
- package/dist/esm/interfaces/userInstituteMetaInterface.d.ts +1 -1
- package/dist/esm/models/mongodb/taskManagementModel.js +1 -1
- package/dist/esm/models/psql/userModel.js +0 -2
- package/dist/esm/services/Concrete/mongooseCommonService.js +48 -49
- package/dist/esm/services/Concrete/sequelizeCommonService.js +106 -107
- package/package.json +8 -10
- package/dist/.sequelizerc +0 -13
|
@@ -153,9 +153,9 @@ export declare function extractFromObject(data: any[], config: {
|
|
|
153
153
|
userdata: any[];
|
|
154
154
|
columns: string[];
|
|
155
155
|
};
|
|
156
|
-
export declare const enumAlternatives: (enumObj: Record<string, string>, isRequired?: boolean, allowArray?: boolean) => Joi.
|
|
157
|
-
export declare const objectIdAlternatives: (isRequired?: boolean, allowArray?: boolean) => Joi.AlternativesSchema<any
|
|
158
|
-
export declare const uuidIdAlternatives: (isRequired?: boolean, allowArray?: boolean) => Joi.AlternativesSchema<any
|
|
156
|
+
export declare const enumAlternatives: (enumObj: Record<string, string>, isRequired?: boolean, allowArray?: boolean) => Joi.AlternativesSchema<any> | Joi.StringSchema<string>;
|
|
157
|
+
export declare const objectIdAlternatives: (isRequired?: boolean, allowArray?: boolean) => Joi.AlternativesSchema<any>;
|
|
158
|
+
export declare const uuidIdAlternatives: (isRequired?: boolean, allowArray?: boolean) => Joi.AlternativesSchema<any>;
|
|
159
159
|
export declare const customPagination: (data: any[], page: number, limit: number, totalRecords: number) => Promise<{
|
|
160
160
|
limit: number;
|
|
161
161
|
totalRecords: number;
|
|
@@ -21,7 +21,7 @@ export interface IUserInstituteMetaAttributes extends IDefaultAttributes, Docume
|
|
|
21
21
|
subJobTitle?: string;
|
|
22
22
|
jobDescription?: string;
|
|
23
23
|
employmentType?: EMPLOYMENT_TYPE;
|
|
24
|
-
isPrincipal?: IS_PRINCIPAL;
|
|
24
|
+
isPrincipal?: keyof typeof IS_PRINCIPAL;
|
|
25
25
|
rollNumber: string;
|
|
26
26
|
details: BOOLEAN_STATUS;
|
|
27
27
|
joinDate?: Date;
|
|
@@ -14,8 +14,6 @@ const areaModel_js_1 = __importDefault(require("./areaModel.js"));
|
|
|
14
14
|
const utils_js_1 = require("../../helpers/utils.js");
|
|
15
15
|
const uniqueNumberCounterModel_js_1 = __importDefault(require("../mongodb/uniqueNumberCounterModel.js"));
|
|
16
16
|
class UserModel extends sequelize_1.Model {
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
-
static updateById;
|
|
19
17
|
// declare addParent: BelongsToManyAddAssociationMixin<UserModel, string>;
|
|
20
18
|
// declare getParents: BelongsToManyGetAssociationsMixin<UserModel>;
|
|
21
19
|
static associate() { }
|
|
@@ -3,8 +3,55 @@ 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;
|
|
7
6
|
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
|
+
};
|
|
8
55
|
this.model = model;
|
|
9
56
|
}
|
|
10
57
|
async findAll(filter, options = {}, populate) {
|
|
@@ -25,54 +72,6 @@ class MongooseCommonService {
|
|
|
25
72
|
query.populate(populate);
|
|
26
73
|
return query.exec();
|
|
27
74
|
}
|
|
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
|
-
};
|
|
76
75
|
async count(filter) {
|
|
77
76
|
return this.model.countDocuments(filter).exec();
|
|
78
77
|
}
|
|
@@ -2,8 +2,113 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const app_js_1 = require("../../constants/app.js");
|
|
4
4
|
class SequelizeCommonService {
|
|
5
|
-
model;
|
|
6
5
|
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
|
+
};
|
|
7
112
|
this.model = model;
|
|
8
113
|
}
|
|
9
114
|
findByPk(identifier, options) {
|
|
@@ -14,111 +119,5 @@ class SequelizeCommonService {
|
|
|
14
119
|
throw err;
|
|
15
120
|
}
|
|
16
121
|
}
|
|
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
|
-
};
|
|
123
122
|
}
|
|
124
123
|
exports.default = SequelizeCommonService;
|
|
@@ -153,9 +153,9 @@ export declare function extractFromObject(data: any[], config: {
|
|
|
153
153
|
userdata: any[];
|
|
154
154
|
columns: string[];
|
|
155
155
|
};
|
|
156
|
-
export declare const enumAlternatives: (enumObj: Record<string, string>, isRequired?: boolean, allowArray?: boolean) => Joi.
|
|
157
|
-
export declare const objectIdAlternatives: (isRequired?: boolean, allowArray?: boolean) => Joi.AlternativesSchema<any
|
|
158
|
-
export declare const uuidIdAlternatives: (isRequired?: boolean, allowArray?: boolean) => Joi.AlternativesSchema<any
|
|
156
|
+
export declare const enumAlternatives: (enumObj: Record<string, string>, isRequired?: boolean, allowArray?: boolean) => Joi.AlternativesSchema<any> | Joi.StringSchema<string>;
|
|
157
|
+
export declare const objectIdAlternatives: (isRequired?: boolean, allowArray?: boolean) => Joi.AlternativesSchema<any>;
|
|
158
|
+
export declare const uuidIdAlternatives: (isRequired?: boolean, allowArray?: boolean) => Joi.AlternativesSchema<any>;
|
|
159
159
|
export declare const customPagination: (data: any[], page: number, limit: number, totalRecords: number) => Promise<{
|
|
160
160
|
limit: number;
|
|
161
161
|
totalRecords: number;
|
|
@@ -21,7 +21,7 @@ export interface IUserInstituteMetaAttributes extends IDefaultAttributes, Docume
|
|
|
21
21
|
subJobTitle?: string;
|
|
22
22
|
jobDescription?: string;
|
|
23
23
|
employmentType?: EMPLOYMENT_TYPE;
|
|
24
|
-
isPrincipal?: IS_PRINCIPAL;
|
|
24
|
+
isPrincipal?: keyof typeof IS_PRINCIPAL;
|
|
25
25
|
rollNumber: string;
|
|
26
26
|
details: BOOLEAN_STATUS;
|
|
27
27
|
joinDate?: Date;
|
|
@@ -9,8 +9,6 @@ import AreaModel from './areaModel.js';
|
|
|
9
9
|
import { getCityAreas, getNextLetter } from '../../helpers/utils.js';
|
|
10
10
|
import UniqueNumberCounterModel from '../mongodb/uniqueNumberCounterModel.js';
|
|
11
11
|
class UserModel extends Model {
|
|
12
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13
|
-
static updateById;
|
|
14
12
|
// declare addParent: BelongsToManyAddAssociationMixin<UserModel, string>;
|
|
15
13
|
// declare getParents: BelongsToManyGetAssociationsMixin<UserModel>;
|
|
16
14
|
static associate() { }
|
|
@@ -1,7 +1,54 @@
|
|
|
1
1
|
import { PAGINATION, PAGINATION_ORDER } from '../../constants/app.js';
|
|
2
2
|
export class MongooseCommonService {
|
|
3
|
-
model;
|
|
4
3
|
constructor(model) {
|
|
4
|
+
this.findAllWithPagination = async (filter, options = {}, populate) => {
|
|
5
|
+
try {
|
|
6
|
+
const convertOrderToSort = (order) => {
|
|
7
|
+
const sort = {};
|
|
8
|
+
for (const [key, direction] of order) {
|
|
9
|
+
sort[key] = direction.toUpperCase() === 'DESC' ? -1 : 1;
|
|
10
|
+
}
|
|
11
|
+
return sort;
|
|
12
|
+
};
|
|
13
|
+
const { order, projection, ...restOptions } = options;
|
|
14
|
+
let { page, limit } = options;
|
|
15
|
+
const sort = convertOrderToSort(order || PAGINATION_ORDER);
|
|
16
|
+
// Ensure page and limit are positive integers
|
|
17
|
+
page = Math.max(1, page || PAGINATION.PAGE);
|
|
18
|
+
limit = Math.max(1, limit || PAGINATION.LIMIT);
|
|
19
|
+
// Calculate offset
|
|
20
|
+
const skip = (page - 1) * limit;
|
|
21
|
+
// Count total records
|
|
22
|
+
const totalRecords = await this.model.countDocuments(filter).exec();
|
|
23
|
+
const totalPages = Math.ceil(totalRecords / limit);
|
|
24
|
+
// Query the records
|
|
25
|
+
const query = this.model.find(filter, projection, {
|
|
26
|
+
...restOptions,
|
|
27
|
+
limit,
|
|
28
|
+
skip,
|
|
29
|
+
sort,
|
|
30
|
+
});
|
|
31
|
+
// Apply populate if necessary
|
|
32
|
+
if (populate) {
|
|
33
|
+
query.populate(populate);
|
|
34
|
+
}
|
|
35
|
+
const recordList = await query.exec();
|
|
36
|
+
// Construct the pagination result
|
|
37
|
+
const paginationOptions = {
|
|
38
|
+
limit,
|
|
39
|
+
totalRecords,
|
|
40
|
+
totalPages,
|
|
41
|
+
hasPreviousPage: page > 1,
|
|
42
|
+
currentPage: Math.min(page, totalPages),
|
|
43
|
+
hasNextPage: page < totalPages,
|
|
44
|
+
recordList,
|
|
45
|
+
};
|
|
46
|
+
return paginationOptions;
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
throw err;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
5
52
|
this.model = model;
|
|
6
53
|
}
|
|
7
54
|
async findAll(filter, options = {}, populate) {
|
|
@@ -22,54 +69,6 @@ export class MongooseCommonService {
|
|
|
22
69
|
query.populate(populate);
|
|
23
70
|
return query.exec();
|
|
24
71
|
}
|
|
25
|
-
findAllWithPagination = async (filter, options = {}, populate) => {
|
|
26
|
-
try {
|
|
27
|
-
const convertOrderToSort = (order) => {
|
|
28
|
-
const sort = {};
|
|
29
|
-
for (const [key, direction] of order) {
|
|
30
|
-
sort[key] = direction.toUpperCase() === 'DESC' ? -1 : 1;
|
|
31
|
-
}
|
|
32
|
-
return sort;
|
|
33
|
-
};
|
|
34
|
-
const { order, projection, ...restOptions } = options;
|
|
35
|
-
let { page, limit } = options;
|
|
36
|
-
const sort = convertOrderToSort(order || PAGINATION_ORDER);
|
|
37
|
-
// Ensure page and limit are positive integers
|
|
38
|
-
page = Math.max(1, page || PAGINATION.PAGE);
|
|
39
|
-
limit = Math.max(1, limit || PAGINATION.LIMIT);
|
|
40
|
-
// Calculate offset
|
|
41
|
-
const skip = (page - 1) * limit;
|
|
42
|
-
// Count total records
|
|
43
|
-
const totalRecords = await this.model.countDocuments(filter).exec();
|
|
44
|
-
const totalPages = Math.ceil(totalRecords / limit);
|
|
45
|
-
// Query the records
|
|
46
|
-
const query = this.model.find(filter, projection, {
|
|
47
|
-
...restOptions,
|
|
48
|
-
limit,
|
|
49
|
-
skip,
|
|
50
|
-
sort,
|
|
51
|
-
});
|
|
52
|
-
// Apply populate if necessary
|
|
53
|
-
if (populate) {
|
|
54
|
-
query.populate(populate);
|
|
55
|
-
}
|
|
56
|
-
const recordList = await query.exec();
|
|
57
|
-
// Construct the pagination result
|
|
58
|
-
const paginationOptions = {
|
|
59
|
-
limit,
|
|
60
|
-
totalRecords,
|
|
61
|
-
totalPages,
|
|
62
|
-
hasPreviousPage: page > 1,
|
|
63
|
-
currentPage: Math.min(page, totalPages),
|
|
64
|
-
hasNextPage: page < totalPages,
|
|
65
|
-
recordList,
|
|
66
|
-
};
|
|
67
|
-
return paginationOptions;
|
|
68
|
-
}
|
|
69
|
-
catch (err) {
|
|
70
|
-
throw err;
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
72
|
async count(filter) {
|
|
74
73
|
return this.model.countDocuments(filter).exec();
|
|
75
74
|
}
|
|
@@ -1,7 +1,112 @@
|
|
|
1
1
|
import { PAGINATION, PAGINATION_ORDER } from '../../constants/app.js';
|
|
2
2
|
export default class SequelizeCommonService {
|
|
3
|
-
model;
|
|
4
3
|
constructor(model) {
|
|
4
|
+
this.findAll = (where, options) => {
|
|
5
|
+
try {
|
|
6
|
+
const finalOptions = {
|
|
7
|
+
...(options || {}),
|
|
8
|
+
order: options?.order || PAGINATION_ORDER,
|
|
9
|
+
};
|
|
10
|
+
return this.model.findAll({ where, ...finalOptions });
|
|
11
|
+
}
|
|
12
|
+
catch (err) {
|
|
13
|
+
throw err;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
this.findAllWithPagination = async (where, options) => {
|
|
17
|
+
try {
|
|
18
|
+
if (!options)
|
|
19
|
+
options = {};
|
|
20
|
+
const limit = options.limit || PAGINATION.LIMIT;
|
|
21
|
+
const currentPage = options.page || PAGINATION.PAGE;
|
|
22
|
+
// Clean options for count()
|
|
23
|
+
const countOptions = { ...options };
|
|
24
|
+
// options.subQuery = false;
|
|
25
|
+
delete countOptions.order;
|
|
26
|
+
delete countOptions.limit;
|
|
27
|
+
delete countOptions.offset;
|
|
28
|
+
const totalRecords = await this.model.count({
|
|
29
|
+
where,
|
|
30
|
+
include: options.include,
|
|
31
|
+
distinct: true,
|
|
32
|
+
});
|
|
33
|
+
const totalPages = Math.ceil(totalRecords / limit);
|
|
34
|
+
// Apply pagination + order for findAll
|
|
35
|
+
options.limit = limit;
|
|
36
|
+
options.offset = (currentPage - 1) * limit;
|
|
37
|
+
options.order = options.order || PAGINATION_ORDER;
|
|
38
|
+
const listOfRecords = await this.findAll(where, options);
|
|
39
|
+
const paginationOptions = {
|
|
40
|
+
limit,
|
|
41
|
+
totalRecords,
|
|
42
|
+
totalPages,
|
|
43
|
+
hasPreviousPage: currentPage - 1 > 0 ? true : false,
|
|
44
|
+
currentPage: currentPage > totalPages ? totalPages : currentPage,
|
|
45
|
+
hasNextPage: currentPage < totalPages,
|
|
46
|
+
recordList: listOfRecords,
|
|
47
|
+
};
|
|
48
|
+
return paginationOptions;
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
throw err;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
this.findOne = async (where, options) => {
|
|
55
|
+
try {
|
|
56
|
+
return this.model.findOne({ where, ...options });
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
throw err;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
this.update = async (where, updateData, options) => {
|
|
63
|
+
try {
|
|
64
|
+
return this.model.update(updateData, { where, ...options });
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
throw err;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
this.upsert = async (conflictWhere, updateData, options) => {
|
|
71
|
+
try {
|
|
72
|
+
return this.model.upsert(updateData, { conflictWhere, ...options });
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
throw err;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
this.create = async (createData, options) => {
|
|
79
|
+
try {
|
|
80
|
+
return this.model.create(createData, options);
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
throw err;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
this.bulkCreate = async (createData, options) => {
|
|
87
|
+
try {
|
|
88
|
+
return this.model.bulkCreate(createData, options);
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
throw err;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
this.delete = async (where, options) => {
|
|
95
|
+
try {
|
|
96
|
+
return this.model.destroy({ where, ...options });
|
|
97
|
+
}
|
|
98
|
+
catch (err) {
|
|
99
|
+
throw err;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
this.count = (where, options) => {
|
|
103
|
+
try {
|
|
104
|
+
return this.model.count({ where, ...options });
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
throw err;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
5
110
|
this.model = model;
|
|
6
111
|
}
|
|
7
112
|
findByPk(identifier, options) {
|
|
@@ -12,110 +117,4 @@ export default class SequelizeCommonService {
|
|
|
12
117
|
throw err;
|
|
13
118
|
}
|
|
14
119
|
}
|
|
15
|
-
findAll = (where, options) => {
|
|
16
|
-
try {
|
|
17
|
-
const finalOptions = {
|
|
18
|
-
...(options || {}),
|
|
19
|
-
order: options?.order || PAGINATION_ORDER,
|
|
20
|
-
};
|
|
21
|
-
return this.model.findAll({ where, ...finalOptions });
|
|
22
|
-
}
|
|
23
|
-
catch (err) {
|
|
24
|
-
throw err;
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
findAllWithPagination = async (where, options) => {
|
|
28
|
-
try {
|
|
29
|
-
if (!options)
|
|
30
|
-
options = {};
|
|
31
|
-
const limit = options.limit || PAGINATION.LIMIT;
|
|
32
|
-
const currentPage = options.page || PAGINATION.PAGE;
|
|
33
|
-
// Clean options for count()
|
|
34
|
-
const countOptions = { ...options };
|
|
35
|
-
// options.subQuery = false;
|
|
36
|
-
delete countOptions.order;
|
|
37
|
-
delete countOptions.limit;
|
|
38
|
-
delete countOptions.offset;
|
|
39
|
-
const totalRecords = await this.model.count({
|
|
40
|
-
where,
|
|
41
|
-
include: options.include,
|
|
42
|
-
distinct: true,
|
|
43
|
-
});
|
|
44
|
-
const totalPages = Math.ceil(totalRecords / limit);
|
|
45
|
-
// Apply pagination + order for findAll
|
|
46
|
-
options.limit = limit;
|
|
47
|
-
options.offset = (currentPage - 1) * limit;
|
|
48
|
-
options.order = options.order || PAGINATION_ORDER;
|
|
49
|
-
const listOfRecords = await this.findAll(where, options);
|
|
50
|
-
const paginationOptions = {
|
|
51
|
-
limit,
|
|
52
|
-
totalRecords,
|
|
53
|
-
totalPages,
|
|
54
|
-
hasPreviousPage: currentPage - 1 > 0 ? true : false,
|
|
55
|
-
currentPage: currentPage > totalPages ? totalPages : currentPage,
|
|
56
|
-
hasNextPage: currentPage < totalPages,
|
|
57
|
-
recordList: listOfRecords,
|
|
58
|
-
};
|
|
59
|
-
return paginationOptions;
|
|
60
|
-
}
|
|
61
|
-
catch (err) {
|
|
62
|
-
throw err;
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
findOne = async (where, options) => {
|
|
66
|
-
try {
|
|
67
|
-
return this.model.findOne({ where, ...options });
|
|
68
|
-
}
|
|
69
|
-
catch (err) {
|
|
70
|
-
throw err;
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
update = async (where, updateData, options) => {
|
|
74
|
-
try {
|
|
75
|
-
return this.model.update(updateData, { where, ...options });
|
|
76
|
-
}
|
|
77
|
-
catch (err) {
|
|
78
|
-
throw err;
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
upsert = async (conflictWhere, updateData, options) => {
|
|
82
|
-
try {
|
|
83
|
-
return this.model.upsert(updateData, { conflictWhere, ...options });
|
|
84
|
-
}
|
|
85
|
-
catch (err) {
|
|
86
|
-
throw err;
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
create = async (createData, options) => {
|
|
90
|
-
try {
|
|
91
|
-
return this.model.create(createData, options);
|
|
92
|
-
}
|
|
93
|
-
catch (err) {
|
|
94
|
-
throw err;
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
bulkCreate = async (createData, options) => {
|
|
98
|
-
try {
|
|
99
|
-
return this.model.bulkCreate(createData, options);
|
|
100
|
-
}
|
|
101
|
-
catch (err) {
|
|
102
|
-
throw err;
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
delete = async (where, options) => {
|
|
106
|
-
try {
|
|
107
|
-
return this.model.destroy({ where, ...options });
|
|
108
|
-
}
|
|
109
|
-
catch (err) {
|
|
110
|
-
throw err;
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
count = (where, options) => {
|
|
114
|
-
try {
|
|
115
|
-
return this.model.count({ where, ...options });
|
|
116
|
-
}
|
|
117
|
-
catch (err) {
|
|
118
|
-
throw err;
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
120
|
}
|
package/package.json
CHANGED
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kipicore/dbcore",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.16",
|
|
4
4
|
"description": "Reusable DB core package with Postgres, MongoDB, models, services, interfaces, and types",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "dist/cjs/index.
|
|
7
|
-
"module": "dist/esm/index.js",
|
|
8
|
-
"types": "dist/esm/index.d.ts",
|
|
6
|
+
"main": "./dist/cjs/index.cjs",
|
|
7
|
+
"module": "./dist/esm/index.js",
|
|
9
8
|
"exports": {
|
|
10
9
|
".": {
|
|
11
|
-
"require": "./dist/cjs/index.js",
|
|
12
10
|
"import": "./dist/esm/index.js",
|
|
13
|
-
"
|
|
11
|
+
"require": "./dist/cjs/index.js"
|
|
14
12
|
}
|
|
15
13
|
},
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
16
15
|
"scripts": {
|
|
17
16
|
"clean": "rm -rf dist",
|
|
18
|
-
"
|
|
19
|
-
"prebuild": "node scripts/generate-index.js",
|
|
20
|
-
"build:esm": "tsc -p tsconfig.json",
|
|
17
|
+
"generate:index": "node scripts/generate-index.js",
|
|
21
18
|
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
22
|
-
"build": "
|
|
19
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
20
|
+
"build": "npm run clean && npm run generate:index && npm run build:cjs && npm run build:esm"
|
|
23
21
|
},
|
|
24
22
|
"keywords": [
|
|
25
23
|
"postgres",
|
package/dist/.sequelizerc
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
// Import the path module to work with file paths
|
|
2
|
-
const path = require("path");
|
|
3
|
-
// Export an object with configuration options for Sequelize CLI
|
|
4
|
-
module.exports = {
|
|
5
|
-
// Configuration file path for Sequelize
|
|
6
|
-
config: path.resolve("configs", "postgresConfig.js"),
|
|
7
|
-
// Path to the directory where models are stored
|
|
8
|
-
"models-path": path.resolve("models", "psql"),
|
|
9
|
-
// Set the path to the directory to store seeders
|
|
10
|
-
"seeders-path": path.resolve("db/psql", "seeders"),
|
|
11
|
-
// Set the path to the directory to store migrations
|
|
12
|
-
"migrations-path": path.resolve("db/psql", "migrations"),
|
|
13
|
-
};
|