@heliyos/heliyos-api-core 1.0.12 → 1.0.14
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/mongoose.d.ts +2 -2
- package/dist/mongoose.js +25 -5
- package/package.json +1 -1
package/dist/mongoose.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import mongoose, {
|
|
1
|
+
import mongoose, { Model, Schema, FilterQuery, UpdateQuery } from "mongoose";
|
|
2
2
|
export * from "mongoose";
|
|
3
3
|
export interface Pagination<T> {
|
|
4
4
|
data: T[];
|
|
@@ -16,7 +16,7 @@ declare module "mongoose" {
|
|
|
16
16
|
}
|
|
17
17
|
export declare const mongooseConnection: Promise<mongoose.Connection>;
|
|
18
18
|
export declare const mongooseReplicaConnection: Promise<mongoose.Connection>;
|
|
19
|
-
export declare const createModel: <T extends mongoose.Document<unknown, any, any>>(name: string, schema: Schema
|
|
19
|
+
export declare const createModel: <T extends mongoose.Document<unknown, any, any>>(name: string, schema: Schema) => mongoose.Model<T, {}, {}, {}, mongoose.IfAny<T, any, mongoose.Document<unknown, {}, T> & mongoose.Default__v<mongoose.Require_id<T>>>, any>;
|
|
20
20
|
export declare const createBulkOperations: <T>(operations: {
|
|
21
21
|
updateOne?: {
|
|
22
22
|
filter: mongoose.FilterQuery<T>;
|
package/dist/mongoose.js
CHANGED
|
@@ -36,8 +36,6 @@ __exportStar(require("mongoose"), exports);
|
|
|
36
36
|
let cachedConnection;
|
|
37
37
|
let cachedReplicaConnection;
|
|
38
38
|
const connectionOptions = {
|
|
39
|
-
useNewUrlParser: true,
|
|
40
|
-
useUnifiedTopology: true,
|
|
41
39
|
serverSelectionTimeoutMS: 5000,
|
|
42
40
|
socketTimeoutMS: 45000,
|
|
43
41
|
retryWrites: true,
|
|
@@ -120,9 +118,31 @@ mongoose_1.default.Query.prototype.paginate = function (page = 1, limit = 10) {
|
|
|
120
118
|
exports.mongooseConnection = mongooseConnected();
|
|
121
119
|
exports.mongooseReplicaConnection = replicaMongooseConnected();
|
|
122
120
|
// Helper for creating typed models
|
|
123
|
-
const createModel = (name, schema
|
|
124
|
-
|
|
125
|
-
|
|
121
|
+
const createModel = (name, schema) => {
|
|
122
|
+
logger_1.logger.info(`Creating model ${name}`);
|
|
123
|
+
try {
|
|
124
|
+
// If no cached connection exists, create one
|
|
125
|
+
if (!cachedConnection) {
|
|
126
|
+
logger_1.logger.info(`No cached connection found, establishing new connection for model ${name}`);
|
|
127
|
+
let mongoUrl = process.env.MONGO_DATABASE_URL;
|
|
128
|
+
if (!mongoUrl) {
|
|
129
|
+
logger_1.logger.error("MONGO_DATABASE_URL not set");
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
cachedConnection = mongoose_1.default.createConnection(mongoUrl, connectionOptions);
|
|
133
|
+
cachedConnection.on("error", (err) => {
|
|
134
|
+
console.error("MongoDB connection error:", err);
|
|
135
|
+
});
|
|
136
|
+
logger_1.logger.info(`New connection established for model ${name}`);
|
|
137
|
+
}
|
|
138
|
+
// Use the cached connection
|
|
139
|
+
logger_1.logger.info(`Creating model ${name} with connection`);
|
|
140
|
+
return cachedConnection.model(name, schema);
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
logger_1.logger.error(`Error creating model ${name}:`, error);
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
126
146
|
};
|
|
127
147
|
exports.createModel = createModel;
|
|
128
148
|
// Helper for bulk operations with better typing
|