@kipicore/dbcore 1.1.2 → 1.1.4

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.
@@ -21,7 +21,7 @@ export declare function groupByFieldOrFields<T>(array: T[], fields: keyof T | (k
21
21
  export declare const isTimeWithinRange: (time: Date, rangeStart: Date, rangeEnd: Date) => boolean;
22
22
  export declare const isDateWithinRange: (date: Date | string, rangeStart: Date | string, rangeEnd: Date | string) => boolean;
23
23
  export declare const startOfDay: (date: string | Date) => Date;
24
- export declare const generateUuidToNumber: (uuid?: string) => string;
24
+ export declare const generateUuidToNumber: (uuid1?: string) => string;
25
25
  export declare const endOfDay: (date: string | Date) => Date;
26
26
  type GroupByFields<T> = (array: T[], fields: keyof T | (keyof T)[]) => Record<string, T[]>;
27
27
  export declare const groupByFields: GroupByFields<any>;
@@ -48,7 +48,7 @@ const fs_1 = __importDefault(require("fs"));
48
48
  const dateFunction = __importStar(require("date-fns"));
49
49
  const app_js_1 = require("../constants/app.js");
50
50
  const joi_1 = __importDefault(require("joi"));
51
- const uuid_1 = require("uuid");
51
+ const uuid_1 = __importDefault(require("uuid"));
52
52
  const crypto_1 = __importDefault(require("crypto"));
53
53
  const sequelize_1 = require("sequelize");
54
54
  const axios_1 = __importDefault(require("axios"));
@@ -230,11 +230,11 @@ const startOfDay = (date) => {
230
230
  return dateFunction.startOfDay(dateFunction.parseISO(new Date(date).toISOString()));
231
231
  };
232
232
  exports.startOfDay = startOfDay;
233
- const generateUuidToNumber = (uuid) => {
234
- if (!uuid)
235
- uuid = (0, uuid_1.v4)();
233
+ const generateUuidToNumber = (uuid1) => {
234
+ if (!uuid1)
235
+ uuid1 = uuid_1.default.v4();
236
236
  // Remove dashes from the UUID
237
- let numericId = uuid.replace(/-/g, '');
237
+ let numericId = uuid1.replace(/-/g, '');
238
238
  // Convert hex characters to numbers (keeping only digits)
239
239
  numericId = BigInt('0x' + numericId).toString();
240
240
  return numericId;
@@ -470,9 +470,9 @@ const removeFalsyValues = (obj) => {
470
470
  };
471
471
  exports.removeFalsyValues = removeFalsyValues;
472
472
  const isValidUUID = (id, expectedVersion) => {
473
- if (!(0, uuid_1.validate)(id))
473
+ if (!uuid_1.default.validate(id))
474
474
  return false;
475
- if (expectedVersion && (0, uuid_1.version)(id) !== expectedVersion)
475
+ if (expectedVersion && uuid_1.default.version(id) !== expectedVersion)
476
476
  return false;
477
477
  return true;
478
478
  };
@@ -39,7 +39,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
39
39
  /* eslint-disable @typescript-eslint/no-explicit-any */
40
40
  const mongoose_1 = __importStar(require("mongoose"));
41
41
  const app_js_1 = require("../../constants/app.js");
42
- const uuid_1 = require("uuid");
42
+ const uuid_1 = __importDefault(require("uuid"));
43
43
  const utils_js_1 = require("../../helpers/utils.js");
44
44
  const sequelize_1 = require("sequelize");
45
45
  const env_js_1 = require("../../configs/env.js");
@@ -351,7 +351,7 @@ const creatOrUpdateHook = async (subjectIndex) => {
351
351
  }
352
352
  for (const chapterIndex of subjectIndex.chapterIndex) {
353
353
  if (!chapterIndex.id)
354
- chapterIndex.id = (0, uuid_1.v4)();
354
+ chapterIndex.id = uuid_1.default.v4();
355
355
  if (chapterIndex.type !== app_js_1.CHAPTER_INDEX_TYPE.CHAPTER) {
356
356
  const parentChapterIndex = subjectIndex.chapterIndex.find((index) => index.id === chapterIndex.parentId);
357
357
  if (!parentChapterIndex)
@@ -373,7 +373,7 @@ const creatOrUpdateHook = async (subjectIndex) => {
373
373
  // }
374
374
  chapterIndex.files = chapterIndex.files.map((file) => {
375
375
  if (!file.id)
376
- file.id = (0, uuid_1.v4)();
376
+ file.id = uuid_1.default.v4();
377
377
  return file;
378
378
  });
379
379
  }
@@ -1,4 +1,4 @@
1
- import { Model } from 'mongoose';
2
- import { ISubscriptionPlanModelAttributes } from '../../interfaces/subscriptionPlanInterface.js';
1
+ import { Model } from "mongoose";
2
+ import { ISubscriptionPlanModelAttributes } from "../../interfaces/subscriptionPlanInterface.js";
3
3
  declare const SubscriptionPlanModel: Model<ISubscriptionPlanModelAttributes>;
4
4
  export default SubscriptionPlanModel;
@@ -32,9 +32,12 @@ var __importStar = (this && this.__importStar) || (function () {
32
32
  return result;
33
33
  };
34
34
  })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
35
38
  Object.defineProperty(exports, "__esModule", { value: true });
36
39
  const mongoose_1 = __importStar(require("mongoose"));
37
- const uuid_1 = require("uuid");
40
+ const uuid_1 = __importDefault(require("uuid"));
38
41
  const app_js_1 = require("../../constants/app.js");
39
42
  const subscriptionPlanFilesSchema = new mongoose_1.Schema({
40
43
  type: {
@@ -55,7 +58,7 @@ const subscriptionPlanModelSchema = new mongoose_1.Schema({
55
58
  uuid: {
56
59
  type: String,
57
60
  required: true,
58
- default: uuid_1.v4,
61
+ default: uuid_1.default.v4(),
59
62
  },
60
63
  title: {
61
64
  type: String,
@@ -129,5 +132,5 @@ const subscriptionPlanModelSchema = new mongoose_1.Schema({
129
132
  timestamps: true,
130
133
  versionKey: false,
131
134
  });
132
- const SubscriptionPlanModel = mongoose_1.default.model('subscription_plans', subscriptionPlanModelSchema);
135
+ const SubscriptionPlanModel = mongoose_1.default.model("subscription_plans", subscriptionPlanModelSchema);
133
136
  exports.default = SubscriptionPlanModel;
@@ -37,7 +37,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  const mongoose_1 = __importStar(require("mongoose"));
40
- const uuid_1 = require("uuid");
40
+ const uuid_1 = __importDefault(require("uuid"));
41
41
  const app_js_1 = require("../../constants/app.js");
42
42
  const sequelize_1 = require("sequelize");
43
43
  const errorMessages_js_1 = require("../../constants/errorMessages.js");
@@ -46,7 +46,7 @@ const userModel_js_1 = __importDefault(require("../psql/userModel.js"));
46
46
  const TaskConversationsSchema = new mongoose_1.Schema({
47
47
  messageId: {
48
48
  type: String,
49
- default: uuid_1.v4,
49
+ default: uuid_1.default.v4(),
50
50
  },
51
51
  userId: {
52
52
  type: String,
@@ -10,11 +10,13 @@ const countryModel_js_1 = __importDefault(require("./countryModel.js"));
10
10
  const stateModel_js_1 = __importDefault(require("./stateModel.js"));
11
11
  const cityModel_js_1 = __importDefault(require("./cityModel.js"));
12
12
  const tokenModel_js_1 = __importDefault(require("./tokenModel.js"));
13
- const uuid_1 = require("uuid");
13
+ const uuid_1 = __importDefault(require("uuid"));
14
14
  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() { }
@@ -359,7 +361,7 @@ const userBeforeCreateHook = async (user) => {
359
361
  if (user.type) {
360
362
  user.globalId = await generateGlobalUniqueNumber(app_js_1.TYPE_OF_USER.USER, user.type);
361
363
  }
362
- user.uuid = (0, uuid_1.v4)();
364
+ user.uuid = uuid_1.default.v4();
363
365
  const where = {
364
366
  email: user.email,
365
367
  uuid: { [sequelize_1.Op.ne]: null }, // uuid must not be null
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kipicore/dbcore",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "Reusable DB core package with Postgres, MongoDB, models, services, interfaces, and types",
5
5
  "type": "commonjs",
6
6
  "main": "dist/index.js",