@prosopo/database 3.15.18 → 3.15.20

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.
@@ -1,144 +1,141 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const node_url = require("node:url");
4
- const common = require("@prosopo/common");
5
- const logger = require("@prosopo/logger");
6
- const mongoose = require("mongoose");
7
- const mongooseOptions = require("../mongooseOptions.cjs");
8
- var _documentCurrentScript = typeof document !== "undefined" ? document.currentScript : null;
9
- mongoose.set("strictQuery", false);
10
- const DEFAULT_ENDPOINT = "mongodb://127.0.0.1:27017";
11
- class MongoDatabase {
12
- constructor(url, dbname, authSource, logger$1) {
13
- this.connected = false;
14
- const baseEndpoint = url || DEFAULT_ENDPOINT;
15
- const parsedUrl = new URL(baseEndpoint);
16
- if (dbname) {
17
- parsedUrl.pathname = dbname;
18
- }
19
- if (authSource) {
20
- parsedUrl.searchParams.set("authSource", authSource);
21
- }
22
- this._url = parsedUrl.toString();
23
- this.safeURL = this.url.replace(/\w+:\w+/, "<Credentials>");
24
- this.dbname = dbname || parsedUrl.pathname.replace("/", "");
25
- this.logger = logger$1 || logger.getLogger("info", "database:mongo");
26
- }
27
- get url() {
28
- return this._url;
29
- }
30
- getConnection() {
31
- if (!this.connection) {
32
- throw new common.ProsopoDBError("DATABASE.CONNECTION_UNDEFINED", {
33
- context: { failedFuncName: this.getConnection.name },
34
- logger: this.logger
35
- });
36
- }
37
- return this.connection;
38
- }
39
- /**
40
- * @description Connect to the database and set the various tables
41
- */
42
- async connect() {
43
- this.logger.debug(() => ({
44
- data: { mongoUrl: this.safeURL },
45
- msg: "Connecting to database"
46
- }));
47
- try {
48
- if (this.connected) {
49
- this.logger.debug(() => ({
50
- data: { mongoUrl: this.safeURL },
51
- msg: "Database connection already open"
52
- }));
53
- return;
54
- }
55
- if (this.connecting) {
56
- this.logger.debug(() => ({
57
- data: { mongoUrl: this.safeURL },
58
- msg: "Database connection in progress, waiting for it to finish"
59
- }));
60
- return this.connecting;
61
- }
62
- this.connecting = new Promise((resolve, reject) => {
63
- const appName = node_url.fileURLToPath(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === "SCRIPT" && _documentCurrentScript.src || new URL("base/mongo.cjs", document.baseURI).href);
64
- const connection = mongoose.createConnection(
65
- this.url,
66
- mongooseOptions.getMongoConnectionOptions({
67
- url: this.url,
68
- appName,
69
- dbName: this.dbname,
70
- maxPoolSize: 50,
71
- minPoolSize: 5
72
- })
73
- );
74
- const onConnected = () => {
75
- this.logger.debug(() => ({
76
- data: { mongoUrl: this.safeURL },
77
- msg: "Database connection opened"
78
- }));
79
- this.connected = true;
80
- this.connection = connection;
81
- this.connecting = void 0;
82
- resolve();
83
- };
84
- const onError = (err) => {
85
- this.logger.error(() => ({
86
- err,
87
- data: { mongoUrl: this.safeURL },
88
- msg: "Database error"
89
- }));
90
- this.connected = false;
91
- this.connecting = void 0;
92
- reject(err);
93
- };
94
- connection.once("open", onConnected);
95
- connection.once("error", onError);
96
- connection.on("disconnected", () => {
97
- this.connected = false;
98
- this.logger.debug(() => ({
99
- data: { mongoUrl: this.safeURL },
100
- msg: "Database disconnected"
101
- }));
102
- });
103
- connection.on("reconnected", () => {
104
- this.connected = true;
105
- this.logger.debug(() => ({
106
- data: { mongoUrl: this.safeURL },
107
- msg: "Database reconnected"
108
- }));
109
- });
110
- connection.on("close", () => {
111
- this.connected = false;
112
- this.logger.debug(() => ({
113
- data: { mongoUrl: this.safeURL },
114
- msg: "Database connection closed"
115
- }));
116
- });
117
- connection.on("fullsetup", () => {
118
- this.connected = true;
119
- this.logger.debug(() => ({
120
- data: { mongoUrl: this.safeURL },
121
- msg: "Database connection is fully setup"
122
- }));
123
- });
124
- });
125
- return this.connecting;
126
- } catch (e) {
127
- this.logger.error(() => ({
128
- err: e,
129
- data: { mongoUrl: this.safeURL },
130
- msg: "Database connection error"
131
- }));
132
- throw e;
133
- }
134
- }
135
- /** Close connection to the database */
136
- async close() {
137
- this.logger.debug(() => ({
138
- data: { mongoUrl: this.safeURL },
139
- msg: "Closing connection"
140
- }));
141
- await this.connection?.close();
142
- }
143
- }
1
+ const require_runtime = require("../_virtual/_rolldown/runtime.cjs");
2
+ const require_mongooseOptions = require("../mongooseOptions.cjs");
3
+ let node_url = require("node:url");
4
+ let _prosopo_common = require("@prosopo/common");
5
+ let _prosopo_logger = require("@prosopo/logger");
6
+ let mongoose = require("mongoose");
7
+ mongoose = require_runtime.__toESM(mongoose, 1);
8
+ //#region src/base/mongo.ts
9
+ mongoose.default.set("strictQuery", false);
10
+ var DEFAULT_ENDPOINT = "mongodb://127.0.0.1:27017";
11
+ /**
12
+ * Returns a generic Mongo database layer
13
+ * @param {string} url The database endpoint
14
+ * @param {string} dbname The database name
15
+ * @return {MongoDatabase} Database layer
16
+ */
17
+ var MongoDatabase = class {
18
+ constructor(url, dbname, authSource, logger) {
19
+ this.connected = false;
20
+ const parsedUrl = new URL(url || DEFAULT_ENDPOINT);
21
+ if (dbname) parsedUrl.pathname = dbname;
22
+ if (authSource) parsedUrl.searchParams.set("authSource", authSource);
23
+ this._url = parsedUrl.toString();
24
+ this.safeURL = this.url.replace(/\w+:\w+/, "<Credentials>");
25
+ this.dbname = dbname || parsedUrl.pathname.replace("/", "");
26
+ this.logger = logger || (0, _prosopo_logger.getLogger)("info", "database:mongo");
27
+ }
28
+ get url() {
29
+ return this._url;
30
+ }
31
+ getConnection() {
32
+ if (!this.connection) throw new _prosopo_common.ProsopoDBError("DATABASE.CONNECTION_UNDEFINED", {
33
+ context: { failedFuncName: this.getConnection.name },
34
+ logger: this.logger
35
+ });
36
+ return this.connection;
37
+ }
38
+ /**
39
+ * @description Connect to the database and set the various tables
40
+ */
41
+ async connect() {
42
+ this.logger.debug(() => ({
43
+ data: { mongoUrl: this.safeURL },
44
+ msg: "Connecting to database"
45
+ }));
46
+ try {
47
+ if (this.connected) {
48
+ this.logger.debug(() => ({
49
+ data: { mongoUrl: this.safeURL },
50
+ msg: "Database connection already open"
51
+ }));
52
+ return;
53
+ }
54
+ if (this.connecting) {
55
+ this.logger.debug(() => ({
56
+ data: { mongoUrl: this.safeURL },
57
+ msg: "Database connection in progress, waiting for it to finish"
58
+ }));
59
+ return this.connecting;
60
+ }
61
+ this.connecting = new Promise((resolve, reject) => {
62
+ const appName = (0, node_url.fileURLToPath)(require("url").pathToFileURL(__filename).href);
63
+ const connection = mongoose.default.createConnection(this.url, require_mongooseOptions.getMongoConnectionOptions({
64
+ url: this.url,
65
+ appName,
66
+ dbName: this.dbname,
67
+ maxPoolSize: 50,
68
+ minPoolSize: 5
69
+ }));
70
+ const onConnected = () => {
71
+ this.logger.debug(() => ({
72
+ data: { mongoUrl: this.safeURL },
73
+ msg: "Database connection opened"
74
+ }));
75
+ this.connected = true;
76
+ this.connection = connection;
77
+ this.connecting = void 0;
78
+ resolve();
79
+ };
80
+ const onError = (err) => {
81
+ this.logger.error(() => ({
82
+ err,
83
+ data: { mongoUrl: this.safeURL },
84
+ msg: "Database error"
85
+ }));
86
+ this.connected = false;
87
+ this.connecting = void 0;
88
+ reject(err);
89
+ };
90
+ connection.once("open", onConnected);
91
+ connection.once("error", onError);
92
+ connection.on("disconnected", () => {
93
+ this.connected = false;
94
+ this.logger.debug(() => ({
95
+ data: { mongoUrl: this.safeURL },
96
+ msg: "Database disconnected"
97
+ }));
98
+ });
99
+ connection.on("reconnected", () => {
100
+ this.connected = true;
101
+ this.logger.debug(() => ({
102
+ data: { mongoUrl: this.safeURL },
103
+ msg: "Database reconnected"
104
+ }));
105
+ });
106
+ connection.on("close", () => {
107
+ this.connected = false;
108
+ this.logger.debug(() => ({
109
+ data: { mongoUrl: this.safeURL },
110
+ msg: "Database connection closed"
111
+ }));
112
+ });
113
+ connection.on("fullsetup", () => {
114
+ this.connected = true;
115
+ this.logger.debug(() => ({
116
+ data: { mongoUrl: this.safeURL },
117
+ msg: "Database connection is fully setup"
118
+ }));
119
+ });
120
+ });
121
+ return this.connecting;
122
+ } catch (e) {
123
+ this.logger.error(() => ({
124
+ err: e,
125
+ data: { mongoUrl: this.safeURL },
126
+ msg: "Database connection error"
127
+ }));
128
+ throw e;
129
+ }
130
+ }
131
+ /** Close connection to the database */
132
+ async close() {
133
+ this.logger.debug(() => ({
134
+ data: { mongoUrl: this.safeURL },
135
+ msg: "Closing connection"
136
+ }));
137
+ await this.connection?.close();
138
+ }
139
+ };
140
+ //#endregion
144
141
  exports.MongoDatabase = MongoDatabase;
@@ -1,23 +1,23 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const mongodbMemoryServer = require("mongodb-memory-server");
4
- const mongo = require("./mongo.cjs");
5
- class MongoMemoryDatabase extends mongo.MongoDatabase {
6
- constructor(_url, dbname, logger, authSource) {
7
- super("", dbname, authSource, logger);
8
- this._url = "";
9
- }
10
- async connect() {
11
- if (!this.mongod) {
12
- this.mongod = await mongodbMemoryServer.MongoMemoryServer.create();
13
- this._url = this.mongod.getUri();
14
- }
15
- await super.connect();
16
- }
17
- async close() {
18
- await super.close();
19
- await this.mongod?.stop();
20
- this.mongod = void 0;
21
- }
22
- }
1
+ const require_mongo = require("./mongo.cjs");
2
+ let mongodb_memory_server = require("mongodb-memory-server");
3
+ //#region src/base/mongoMemory.ts
4
+ var MongoMemoryDatabase = class extends require_mongo.MongoDatabase {
5
+ constructor(_url, dbname, logger, authSource) {
6
+ super("", dbname, authSource, logger);
7
+ this._url = "";
8
+ }
9
+ async connect() {
10
+ if (!this.mongod) {
11
+ this.mongod = await mongodb_memory_server.MongoMemoryServer.create();
12
+ this._url = this.mongod.getUri();
13
+ }
14
+ await super.connect();
15
+ }
16
+ async close() {
17
+ await super.close();
18
+ await this.mongod?.stop();
19
+ this.mongod = void 0;
20
+ }
21
+ };
22
+ //#endregion
23
23
  exports.MongoMemoryDatabase = MongoMemoryDatabase;