@heliyos/heliyos-api-core 1.0.11 → 1.0.13
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/env.d.ts +1 -1
- package/dist/env.js +9 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -3
- package/dist/mongoose.d.ts +1 -1
- package/dist/mongoose.js +53 -11
- package/package.json +1 -1
package/dist/env.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const loadAppEnv: () => Promise<any>;
|
|
1
|
+
export declare const loadAppEnv: (isSetToEnv?: boolean) => Promise<any>;
|
package/dist/env.js
CHANGED
|
@@ -10,9 +10,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.loadAppEnv = void 0;
|
|
13
|
+
const logger_1 = require("./logger");
|
|
13
14
|
const ssm_1 = require("./ssm");
|
|
14
|
-
const loadAppEnv = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
-
const secretsJSON = yield (0, ssm_1.getSecretsManagerSecret)(process.env.ENV_SECRET_NAME);
|
|
15
|
+
const loadAppEnv = (isSetToEnv = true) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
const secretsJSON = (yield (0, ssm_1.getSecretsManagerSecret)(process.env.ENV_SECRET_NAME));
|
|
17
|
+
if (isSetToEnv) {
|
|
18
|
+
for (const envKey of Object.keys(secretsJSON)) {
|
|
19
|
+
process.env[envKey] = secretsJSON[envKey];
|
|
20
|
+
}
|
|
21
|
+
logger_1.logger.info(`Environment variables loaded successfully.`);
|
|
22
|
+
}
|
|
16
23
|
return secretsJSON;
|
|
17
24
|
});
|
|
18
25
|
exports.loadAppEnv = loadAppEnv;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export { core_app } from "./app";
|
|
|
3
3
|
export { core_axios as axios } from "./axios";
|
|
4
4
|
export { core_knex, core_replica_knex } from "./knex";
|
|
5
5
|
export { validate, joiObject } from "./validate";
|
|
6
|
+
export { loadAppEnv } from "./env";
|
|
6
7
|
export { auth_policy } from "./static/auth_policy_file";
|
|
7
8
|
export { database_config } from "./knexfile";
|
|
8
9
|
export { authorize_user } from "./authorization";
|
|
@@ -11,7 +12,6 @@ export { SQSUtil } from "./sqs";
|
|
|
11
12
|
export { logger } from "./logger";
|
|
12
13
|
export { allowed_origin } from "./allowed_origin";
|
|
13
14
|
export { getEmailTemplate, emailTemplates } from "./email";
|
|
14
|
-
export { loadAppEnv } from "./env";
|
|
15
15
|
export { pusher, pusherTrigger, pusherTriggerBatch } from "./pusher";
|
|
16
16
|
export { resendSendEmail } from "./resend";
|
|
17
17
|
export { getSecretsManagerSecret } from "./ssm";
|
package/dist/index.js
CHANGED
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.safeDelete = exports.checkDatabaseHealth = exports.upsertDocument = exports.withTransaction = exports.closeConnections = exports.ensureIndexes = exports.createBulkOperations = exports.mongooseReplicaConnection = exports.mongooseConnection = exports.createModel = exports.Types = exports.Connection = exports.Model = exports.Document = exports.Schema = exports.HttpError = exports.getSecretsManagerSecret = exports.resendSendEmail = exports.pusherTriggerBatch = exports.pusherTrigger = exports.pusher = exports.
|
|
26
|
+
exports.safeDelete = exports.checkDatabaseHealth = exports.upsertDocument = exports.withTransaction = exports.closeConnections = exports.ensureIndexes = exports.createBulkOperations = exports.mongooseReplicaConnection = exports.mongooseConnection = exports.createModel = exports.Types = exports.Connection = exports.Model = exports.Document = exports.Schema = exports.HttpError = exports.getSecretsManagerSecret = exports.resendSendEmail = exports.pusherTriggerBatch = exports.pusherTrigger = exports.pusher = exports.emailTemplates = exports.getEmailTemplate = exports.allowed_origin = exports.logger = exports.SQSUtil = exports.createRedisClient = exports.authorize_user = exports.database_config = exports.auth_policy = exports.loadAppEnv = exports.joiObject = exports.validate = exports.core_replica_knex = exports.core_knex = exports.axios = exports.core_app = exports.authentication = void 0;
|
|
27
27
|
const dotenv = __importStar(require("dotenv"));
|
|
28
28
|
dotenv.config();
|
|
29
29
|
// Core exports
|
|
@@ -39,6 +39,8 @@ Object.defineProperty(exports, "core_replica_knex", { enumerable: true, get: fun
|
|
|
39
39
|
var validate_1 = require("./validate");
|
|
40
40
|
Object.defineProperty(exports, "validate", { enumerable: true, get: function () { return validate_1.validate; } });
|
|
41
41
|
Object.defineProperty(exports, "joiObject", { enumerable: true, get: function () { return validate_1.joiObject; } });
|
|
42
|
+
var env_1 = require("./env");
|
|
43
|
+
Object.defineProperty(exports, "loadAppEnv", { enumerable: true, get: function () { return env_1.loadAppEnv; } });
|
|
42
44
|
var auth_policy_file_1 = require("./static/auth_policy_file");
|
|
43
45
|
Object.defineProperty(exports, "auth_policy", { enumerable: true, get: function () { return auth_policy_file_1.auth_policy; } });
|
|
44
46
|
var knexfile_1 = require("./knexfile");
|
|
@@ -57,8 +59,6 @@ Object.defineProperty(exports, "allowed_origin", { enumerable: true, get: functi
|
|
|
57
59
|
var email_1 = require("./email");
|
|
58
60
|
Object.defineProperty(exports, "getEmailTemplate", { enumerable: true, get: function () { return email_1.getEmailTemplate; } });
|
|
59
61
|
Object.defineProperty(exports, "emailTemplates", { enumerable: true, get: function () { return email_1.emailTemplates; } });
|
|
60
|
-
var env_1 = require("./env");
|
|
61
|
-
Object.defineProperty(exports, "loadAppEnv", { enumerable: true, get: function () { return env_1.loadAppEnv; } });
|
|
62
62
|
var pusher_1 = require("./pusher");
|
|
63
63
|
Object.defineProperty(exports, "pusher", { enumerable: true, get: function () { return pusher_1.pusher; } });
|
|
64
64
|
Object.defineProperty(exports, "pusherTrigger", { enumerable: true, get: function () { return pusher_1.pusherTrigger; } });
|
package/dist/mongoose.d.ts
CHANGED
|
@@ -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, connection?: Connection | Promise<Connection>) => mongoose.Model<T, {}, {}, {}, mongoose.IfAny<T, any, mongoose.Document<unknown, {}, T> & mongoose.Default__v<mongoose.Require_id<T>>>, any
|
|
19
|
+
export declare const createModel: <T extends mongoose.Document<unknown, any, any>>(name: string, schema: Schema, connection?: Connection | Promise<Connection>) => mongoose.Model<T, {}, {}, {}, mongoose.IfAny<T, any, mongoose.Document<unknown, {}, T> & mongoose.Default__v<mongoose.Require_id<T>>>, any> | Promise<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
|
@@ -10,6 +10,18 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
13
25
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
26
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
27
|
};
|
|
@@ -22,12 +34,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
22
34
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
23
35
|
});
|
|
24
36
|
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
38
|
exports.closeConnections = exports.checkDatabaseHealth = exports.upsertDocument = exports.safeDelete = exports.withTransaction = exports.ensureIndexes = exports.createBulkOperations = exports.createModel = exports.mongooseReplicaConnection = exports.mongooseConnection = void 0;
|
|
30
|
-
const mongoose_1 =
|
|
39
|
+
const mongoose_1 = __importStar(require("mongoose"));
|
|
31
40
|
const ssm_1 = require("./ssm");
|
|
32
41
|
const logger_1 = require("./logger");
|
|
33
42
|
// Export commonly used mongoose types
|
|
@@ -35,7 +44,11 @@ __exportStar(require("mongoose"), exports);
|
|
|
35
44
|
// Cache connections
|
|
36
45
|
let cachedConnection;
|
|
37
46
|
let cachedReplicaConnection;
|
|
38
|
-
const connectionOptions = {
|
|
47
|
+
const connectionOptions = {
|
|
48
|
+
serverSelectionTimeoutMS: 5000,
|
|
49
|
+
socketTimeoutMS: 45000,
|
|
50
|
+
retryWrites: true,
|
|
51
|
+
};
|
|
39
52
|
const mongooseConnected = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
40
53
|
if (cachedConnection) {
|
|
41
54
|
logger_1.logger.info("Cached MongoDB Connection");
|
|
@@ -54,6 +67,10 @@ const mongooseConnected = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
54
67
|
logger_1.logger.info(`Failed to get mongoose configuration for env:${nodeEnv}`);
|
|
55
68
|
return null;
|
|
56
69
|
}
|
|
70
|
+
if (!mongoUrl) {
|
|
71
|
+
logger_1.logger.info("MONGO_DATABASE_URL not set");
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
57
74
|
logger_1.logger.info("New MongoDB Connection");
|
|
58
75
|
const connection = mongoose_1.default.createConnection(mongoUrl, connectionOptions);
|
|
59
76
|
connection.on("error", (err) => {
|
|
@@ -71,10 +88,6 @@ const replicaMongooseConnected = () => __awaiter(void 0, void 0, void 0, functio
|
|
|
71
88
|
let replicaUrl;
|
|
72
89
|
if (nodeEnv === "local") {
|
|
73
90
|
replicaUrl = process.env.MONGO_REPLICA_DATABASE_URL;
|
|
74
|
-
if (!replicaUrl) {
|
|
75
|
-
logger_1.logger.info("MONGO_REPLICA_DATABASE_URL not set");
|
|
76
|
-
return null;
|
|
77
|
-
}
|
|
78
91
|
}
|
|
79
92
|
else if (["development", "production"].includes(nodeEnv)) {
|
|
80
93
|
const secrets = yield (0, ssm_1.getSecretsManagerSecret)(process.env.ENV_SECRET_NAME);
|
|
@@ -84,6 +97,10 @@ const replicaMongooseConnected = () => __awaiter(void 0, void 0, void 0, functio
|
|
|
84
97
|
logger_1.logger.info(`Failed to get mongoose configuration for env:${nodeEnv}`);
|
|
85
98
|
return null;
|
|
86
99
|
}
|
|
100
|
+
if (!replicaUrl) {
|
|
101
|
+
logger_1.logger.info("MONGO_REPLICA_DATABASE_URL not set");
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
87
104
|
logger_1.logger.info("New MongoDB Replica Connection");
|
|
88
105
|
const connection = mongoose_1.default.createConnection(replicaUrl, connectionOptions);
|
|
89
106
|
connection.on("error", (err) => {
|
|
@@ -111,8 +128,33 @@ exports.mongooseConnection = mongooseConnected();
|
|
|
111
128
|
exports.mongooseReplicaConnection = replicaMongooseConnected();
|
|
112
129
|
// Helper for creating typed models
|
|
113
130
|
const createModel = (name, schema, connection = exports.mongooseConnection) => {
|
|
114
|
-
|
|
115
|
-
|
|
131
|
+
// Add debug logging
|
|
132
|
+
logger_1.logger.info(`Creating model ${name}`);
|
|
133
|
+
// If we already have a cached connection, use it synchronously
|
|
134
|
+
if (connection instanceof mongoose_1.Connection) {
|
|
135
|
+
try {
|
|
136
|
+
logger_1.logger.info(`Using existing connection for model ${name}`);
|
|
137
|
+
return connection.model(name, schema);
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
logger_1.logger.info(`Error creating model ${name}:`, error);
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// Otherwise, handle it asynchronously
|
|
145
|
+
return Promise.resolve(connection)
|
|
146
|
+
.then((conn) => {
|
|
147
|
+
if (!conn) {
|
|
148
|
+
logger_1.logger.info(`Database connection not established for model: ${name}`);
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
logger_1.logger.info(`Creating model ${name} with async connection`);
|
|
152
|
+
return conn.model(name, schema);
|
|
153
|
+
})
|
|
154
|
+
.catch((error) => {
|
|
155
|
+
logger_1.logger.info(`Error creating model ${name}:`, error);
|
|
156
|
+
return null;
|
|
157
|
+
});
|
|
116
158
|
};
|
|
117
159
|
exports.createModel = createModel;
|
|
118
160
|
// Helper for bulk operations with better typing
|