@prosopo/database 3.0.9 → 3.0.10

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/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @prosopo/database
2
2
 
3
+ ## 3.0.10
4
+ ### Patch Changes
5
+
6
+ - 3573f0b: fix npm scripts bundle command
7
+ - 3573f0b: build using vite, typecheck using tsc
8
+ - efd8102: Add tests for unwrap error helper
9
+ - 3573f0b: standardise all vite based npm scripts for bundling
10
+ - Updated dependencies [93d5e50]
11
+ - Updated dependencies [3573f0b]
12
+ - Updated dependencies [3573f0b]
13
+ - Updated dependencies [efd8102]
14
+ - Updated dependencies [93d5e50]
15
+ - Updated dependencies [63519d7]
16
+ - Updated dependencies [f29fc7e]
17
+ - Updated dependencies [3573f0b]
18
+ - Updated dependencies [2d0dd8a]
19
+ - @prosopo/types@3.0.4
20
+ - @prosopo/user-access-policy@3.3.1
21
+ - @prosopo/types-database@3.0.10
22
+ - @prosopo/common@3.1.0
23
+ - @prosopo/config@3.1.1
24
+
3
25
  ## 3.0.9
4
26
  ### Patch Changes
5
27
 
@@ -1,3 +1,6 @@
1
- export * from "./mongo.js";
2
- export * from "./mongoMemory.js";
3
- //# sourceMappingURL=index.js.map
1
+ import { MongoDatabase } from "./mongo.js";
2
+ import { MongoMemoryDatabase } from "./mongoMemory.js";
3
+ export {
4
+ MongoDatabase,
5
+ MongoMemoryDatabase
6
+ };
@@ -1,129 +1,134 @@
1
- import { ProsopoDBError, getLogger } from "@prosopo/common";
1
+ import { getLogger, ProsopoDBError } from "@prosopo/common";
2
2
  import { ServerApiVersion } from "mongodb";
3
3
  import mongoose from "mongoose";
4
4
  mongoose.set("strictQuery", false);
5
5
  const DEFAULT_ENDPOINT = "mongodb://127.0.0.1:27017";
6
- export class MongoDatabase {
7
- constructor(url, dbname, authSource, logger) {
8
- this.connected = false;
9
- const baseEndpoint = url || DEFAULT_ENDPOINT;
10
- const parsedUrl = new URL(baseEndpoint);
11
- if (dbname) {
12
- parsedUrl.pathname = dbname;
13
- }
14
- if (authSource) {
15
- parsedUrl.searchParams.set("authSource", authSource);
16
- }
17
- this._url = parsedUrl.toString();
18
- this.safeURL = this.url.replace(/\w+:\w+/, "<Credentials>");
19
- this.dbname = dbname || parsedUrl.pathname.replace("/", "");
20
- this.logger = logger || getLogger("info", import.meta.url);
6
+ class MongoDatabase {
7
+ constructor(url, dbname, authSource, logger) {
8
+ this.connected = false;
9
+ const baseEndpoint = url || DEFAULT_ENDPOINT;
10
+ const parsedUrl = new URL(baseEndpoint);
11
+ if (dbname) {
12
+ parsedUrl.pathname = dbname;
21
13
  }
22
- get url() {
23
- return this._url;
14
+ if (authSource) {
15
+ parsedUrl.searchParams.set("authSource", authSource);
24
16
  }
25
- getConnection() {
26
- if (!this.connection) {
27
- throw new ProsopoDBError("DATABASE.CONNECTION_UNDEFINED", {
28
- context: { failedFuncName: this.getConnection.name },
29
- logger: this.logger,
30
- });
31
- }
32
- return this.connection;
17
+ this._url = parsedUrl.toString();
18
+ this.safeURL = this.url.replace(/\w+:\w+/, "<Credentials>");
19
+ this.dbname = dbname || parsedUrl.pathname.replace("/", "");
20
+ this.logger = logger || getLogger("info", import.meta.url);
21
+ }
22
+ get url() {
23
+ return this._url;
24
+ }
25
+ getConnection() {
26
+ if (!this.connection) {
27
+ throw new ProsopoDBError("DATABASE.CONNECTION_UNDEFINED", {
28
+ context: { failedFuncName: this.getConnection.name },
29
+ logger: this.logger
30
+ });
33
31
  }
34
- async connect() {
32
+ return this.connection;
33
+ }
34
+ /**
35
+ * @description Connect to the database and set the various tables
36
+ */
37
+ async connect() {
38
+ this.logger.info(() => ({
39
+ data: { mongoUrl: this.safeURL },
40
+ msg: "Connecting to database"
41
+ }));
42
+ try {
43
+ if (this.connected) {
35
44
  this.logger.info(() => ({
36
- data: { mongoUrl: this.safeURL },
37
- msg: "Connecting to database",
45
+ data: { mongoUrl: this.safeURL },
46
+ msg: "Database connection already open"
38
47
  }));
39
- try {
40
- if (this.connected) {
41
- this.logger.info(() => ({
42
- data: { mongoUrl: this.safeURL },
43
- msg: "Database connection already open",
44
- }));
45
- return;
46
- }
47
- if (this.connecting) {
48
- this.logger.info(() => ({
49
- data: { mongoUrl: this.safeURL },
50
- msg: "Database connection in progress, waiting for it to finish",
51
- }));
52
- return this.connecting;
53
- }
54
- this.connecting = new Promise((resolve, reject) => {
55
- const connection = mongoose.createConnection(this.url, {
56
- dbName: this.dbname,
57
- serverApi: ServerApiVersion.v1,
58
- });
59
- const onConnected = () => {
60
- this.logger.info(() => ({
61
- data: { mongoUrl: this.safeURL },
62
- msg: "Database connection opened",
63
- }));
64
- this.connected = true;
65
- this.connection = connection;
66
- this.connecting = undefined;
67
- resolve();
68
- };
69
- const onError = (err) => {
70
- this.logger.error(() => ({
71
- err,
72
- data: { mongoUrl: this.safeURL },
73
- msg: "Database error",
74
- }));
75
- this.connected = false;
76
- this.connecting = undefined;
77
- reject(err);
78
- };
79
- connection.once("open", onConnected);
80
- connection.once("error", onError);
81
- connection.on("disconnected", () => {
82
- this.connected = false;
83
- this.logger.info(() => ({
84
- data: { mongoUrl: this.safeURL },
85
- msg: "Database disconnected",
86
- }));
87
- });
88
- connection.on("reconnected", () => {
89
- this.connected = true;
90
- this.logger.info(() => ({
91
- data: { mongoUrl: this.safeURL },
92
- msg: "Database reconnected",
93
- }));
94
- });
95
- connection.on("close", () => {
96
- this.connected = false;
97
- this.logger.info(() => ({
98
- data: { mongoUrl: this.safeURL },
99
- msg: "Database connection closed",
100
- }));
101
- });
102
- connection.on("fullsetup", () => {
103
- this.connected = true;
104
- this.logger.info(() => ({
105
- data: { mongoUrl: this.safeURL },
106
- msg: "Database connection is fully setup",
107
- }));
108
- });
109
- });
110
- return this.connecting;
111
- }
112
- catch (e) {
113
- this.logger.error(() => ({
114
- err: e,
115
- data: { mongoUrl: this.safeURL },
116
- msg: "Database connection error",
117
- }));
118
- throw e;
119
- }
120
- }
121
- async close() {
122
- this.logger.debug(() => ({
123
- data: { mongoUrl: this.safeURL },
124
- msg: "Closing connection",
48
+ return;
49
+ }
50
+ if (this.connecting) {
51
+ this.logger.info(() => ({
52
+ data: { mongoUrl: this.safeURL },
53
+ msg: "Database connection in progress, waiting for it to finish"
125
54
  }));
126
- await this.connection?.close();
55
+ return this.connecting;
56
+ }
57
+ this.connecting = new Promise((resolve, reject) => {
58
+ const connection = mongoose.createConnection(this.url, {
59
+ dbName: this.dbname,
60
+ serverApi: ServerApiVersion.v1
61
+ });
62
+ const onConnected = () => {
63
+ this.logger.info(() => ({
64
+ data: { mongoUrl: this.safeURL },
65
+ msg: "Database connection opened"
66
+ }));
67
+ this.connected = true;
68
+ this.connection = connection;
69
+ this.connecting = void 0;
70
+ resolve();
71
+ };
72
+ const onError = (err) => {
73
+ this.logger.error(() => ({
74
+ err,
75
+ data: { mongoUrl: this.safeURL },
76
+ msg: "Database error"
77
+ }));
78
+ this.connected = false;
79
+ this.connecting = void 0;
80
+ reject(err);
81
+ };
82
+ connection.once("open", onConnected);
83
+ connection.once("error", onError);
84
+ connection.on("disconnected", () => {
85
+ this.connected = false;
86
+ this.logger.info(() => ({
87
+ data: { mongoUrl: this.safeURL },
88
+ msg: "Database disconnected"
89
+ }));
90
+ });
91
+ connection.on("reconnected", () => {
92
+ this.connected = true;
93
+ this.logger.info(() => ({
94
+ data: { mongoUrl: this.safeURL },
95
+ msg: "Database reconnected"
96
+ }));
97
+ });
98
+ connection.on("close", () => {
99
+ this.connected = false;
100
+ this.logger.info(() => ({
101
+ data: { mongoUrl: this.safeURL },
102
+ msg: "Database connection closed"
103
+ }));
104
+ });
105
+ connection.on("fullsetup", () => {
106
+ this.connected = true;
107
+ this.logger.info(() => ({
108
+ data: { mongoUrl: this.safeURL },
109
+ msg: "Database connection is fully setup"
110
+ }));
111
+ });
112
+ });
113
+ return this.connecting;
114
+ } catch (e) {
115
+ this.logger.error(() => ({
116
+ err: e,
117
+ data: { mongoUrl: this.safeURL },
118
+ msg: "Database connection error"
119
+ }));
120
+ throw e;
127
121
  }
122
+ }
123
+ /** Close connection to the database */
124
+ async close() {
125
+ this.logger.debug(() => ({
126
+ data: { mongoUrl: this.safeURL },
127
+ msg: "Closing connection"
128
+ }));
129
+ await this.connection?.close();
130
+ }
128
131
  }
129
- //# sourceMappingURL=mongo.js.map
132
+ export {
133
+ MongoDatabase
134
+ };
@@ -1,27 +1,28 @@
1
1
  import { MongoMemoryServer } from "mongodb-memory-server";
2
2
  import { MongoDatabase } from "./mongo.js";
3
- export class MongoMemoryDatabase extends MongoDatabase {
4
- constructor(url, dbname, logger, authSource) {
5
- const mongod = new MongoMemoryServer();
6
- const mongoMemoryURL = mongod.getUri();
7
- super(mongoMemoryURL, dbname, authSource, logger);
8
- this.running = false;
9
- this.mongod = mongod;
10
- this._url = mongoMemoryURL;
11
- }
12
- connect() {
13
- if (!this.running) {
14
- this.mongod?.start();
15
- this.running = true;
16
- }
17
- else {
18
- }
19
- return super.connect();
20
- }
21
- async close() {
22
- await super.close();
23
- await this.mongod?.stop();
24
- this.running = false;
3
+ class MongoMemoryDatabase extends MongoDatabase {
4
+ constructor(url, dbname, logger, authSource) {
5
+ const mongod = new MongoMemoryServer();
6
+ const mongoMemoryURL = mongod.getUri();
7
+ super(mongoMemoryURL, dbname, authSource, logger);
8
+ this.running = false;
9
+ this.mongod = mongod;
10
+ this._url = mongoMemoryURL;
11
+ }
12
+ connect() {
13
+ if (!this.running) {
14
+ this.mongod?.start();
15
+ this.running = true;
16
+ } else {
25
17
  }
18
+ return super.connect();
19
+ }
20
+ async close() {
21
+ await super.close();
22
+ await this.mongod?.stop();
23
+ this.running = false;
24
+ }
26
25
  }
27
- //# sourceMappingURL=mongoMemory.js.map
26
+ export {
27
+ MongoMemoryDatabase
28
+ };
@@ -1,133 +1,134 @@
1
- import { ProsopoDBError, getLogger } from "@prosopo/common";
2
- import { StoredPoWCaptchaRecordSchema, StoredSessionRecordSchema, StoredUserCommitmentRecordSchema, } from "@prosopo/types-database";
3
- import { MongoDatabase } from "../base/index.js";
1
+ import { getLogger, ProsopoDBError } from "@prosopo/common";
2
+ import { StoredSessionRecordSchema, StoredPoWCaptchaRecordSchema, StoredUserCommitmentRecordSchema } from "@prosopo/types-database";
3
+ import "../base/index.js";
4
+ import { MongoDatabase } from "../base/mongo.js";
4
5
  const logger = getLogger("info", import.meta.url);
5
- var TableNames;
6
- (function (TableNames) {
7
- TableNames["frictionlessToken"] = "frictionlessToken";
8
- TableNames["session"] = "session";
9
- TableNames["commitment"] = "commitment";
10
- TableNames["powcaptcha"] = "powcaptcha";
11
- })(TableNames || (TableNames = {}));
6
+ var TableNames = /* @__PURE__ */ ((TableNames2) => {
7
+ TableNames2["frictionlessToken"] = "frictionlessToken";
8
+ TableNames2["session"] = "session";
9
+ TableNames2["commitment"] = "commitment";
10
+ TableNames2["powcaptcha"] = "powcaptcha";
11
+ return TableNames2;
12
+ })(TableNames || {});
12
13
  const CAPTCHA_TABLES = [
13
- {
14
- collectionName: TableNames.session,
15
- modelName: "Session",
16
- schema: StoredSessionRecordSchema,
17
- },
18
- {
19
- collectionName: TableNames.powcaptcha,
20
- modelName: "PowCaptcha",
21
- schema: StoredPoWCaptchaRecordSchema,
22
- },
23
- {
24
- collectionName: TableNames.commitment,
25
- modelName: "UserCommitment",
26
- schema: StoredUserCommitmentRecordSchema,
27
- },
14
+ {
15
+ collectionName: "session",
16
+ modelName: "Session",
17
+ schema: StoredSessionRecordSchema
18
+ },
19
+ {
20
+ collectionName: "powcaptcha",
21
+ modelName: "PowCaptcha",
22
+ schema: StoredPoWCaptchaRecordSchema
23
+ },
24
+ {
25
+ collectionName: "commitment",
26
+ modelName: "UserCommitment",
27
+ schema: StoredUserCommitmentRecordSchema
28
+ }
28
29
  ];
29
- export class CaptchaDatabase extends MongoDatabase {
30
- constructor(url, dbname, authSource, logger) {
31
- super(url, dbname, authSource, logger);
32
- this.tables = {};
30
+ class CaptchaDatabase extends MongoDatabase {
31
+ constructor(url, dbname, authSource, logger2) {
32
+ super(url, dbname, authSource, logger2);
33
+ this.tables = {};
34
+ }
35
+ async connect() {
36
+ await super.connect();
37
+ CAPTCHA_TABLES.map(({ collectionName, modelName, schema }) => {
38
+ if (this.connection) {
39
+ this.tables[collectionName] = this.connection.model(modelName, schema);
40
+ }
41
+ });
42
+ }
43
+ getTables() {
44
+ if (!this.tables) {
45
+ throw new ProsopoDBError("DATABASE.TABLES_UNDEFINED", {
46
+ context: { failedFuncName: this.getTables.name },
47
+ logger: this.logger
48
+ });
33
49
  }
34
- async connect() {
35
- await super.connect();
36
- CAPTCHA_TABLES.map(({ collectionName, modelName, schema }) => {
37
- if (this.connection) {
38
- this.tables[collectionName] = this.connection.model(modelName, schema);
50
+ return this.tables;
51
+ }
52
+ async saveCaptchas(sessionEvents, imageCaptchaEvents, powCaptchaEvents) {
53
+ await this.connect();
54
+ if (sessionEvents.length) {
55
+ const result = await this.tables.session.bulkWrite(
56
+ sessionEvents.map((document) => {
57
+ const { _id, ...safeDoc } = document;
58
+ return {
59
+ insertOne: {
60
+ document: safeDoc
39
61
  }
40
- });
62
+ };
63
+ })
64
+ );
65
+ logger.info(() => ({
66
+ data: { insertedCount: result.insertedCount },
67
+ msg: "Mongo Saved Session Events"
68
+ }));
41
69
  }
42
- getTables() {
43
- if (!this.tables) {
44
- throw new ProsopoDBError("DATABASE.TABLES_UNDEFINED", {
45
- context: { failedFuncName: this.getTables.name },
46
- logger: this.logger,
47
- });
48
- }
49
- return this.tables;
70
+ if (imageCaptchaEvents.length) {
71
+ const result = await this.tables.commitment.bulkWrite(
72
+ imageCaptchaEvents.map((doc) => {
73
+ const { _id, ...safeDoc } = doc;
74
+ return {
75
+ updateOne: {
76
+ filter: { id: safeDoc.id },
77
+ update: { $set: safeDoc },
78
+ upsert: true
79
+ }
80
+ };
81
+ })
82
+ );
83
+ logger.info(() => ({
84
+ data: { upsertedCount: result.upsertedCount },
85
+ msg: "Mongo Saved Image Events"
86
+ }));
50
87
  }
51
- async saveCaptchas(sessionEvents, imageCaptchaEvents, powCaptchaEvents) {
52
- await this.connect();
53
- if (sessionEvents.length) {
54
- const result = await this.tables.session.bulkWrite(sessionEvents.map((document) => {
55
- const { _id, ...safeDoc } = document;
56
- return {
57
- insertOne: {
58
- document: safeDoc,
59
- },
60
- };
61
- }));
62
- logger.info(() => ({
63
- data: { insertedCount: result.insertedCount },
64
- msg: "Mongo Saved Session Events",
65
- }));
66
- }
67
- if (imageCaptchaEvents.length) {
68
- const result = await this.tables.commitment.bulkWrite(imageCaptchaEvents.map((doc) => {
69
- const { _id, ...safeDoc } = doc;
70
- return {
71
- updateOne: {
72
- filter: { id: safeDoc.id },
73
- update: { $set: safeDoc },
74
- upsert: true,
75
- },
76
- };
77
- }));
78
- logger.info(() => ({
79
- data: { upsertedCount: result.upsertedCount },
80
- msg: "Mongo Saved Image Events",
81
- }));
82
- }
83
- if (powCaptchaEvents.length) {
84
- const result = await this.tables.powcaptcha.bulkWrite(powCaptchaEvents.map((doc) => {
85
- const { _id, ...safeDoc } = doc;
86
- return {
87
- updateOne: {
88
- filter: { challenge: safeDoc.challenge },
89
- update: { $set: safeDoc },
90
- upsert: true,
91
- },
92
- };
93
- }));
94
- logger.info(() => ({
95
- data: { upsertedCount: result.upsertedCount },
96
- msg: "Mongo Saved PoW Events",
97
- }));
98
- }
99
- await this.close();
88
+ if (powCaptchaEvents.length) {
89
+ const result = await this.tables.powcaptcha.bulkWrite(
90
+ powCaptchaEvents.map((doc) => {
91
+ const { _id, ...safeDoc } = doc;
92
+ return {
93
+ updateOne: {
94
+ filter: { challenge: safeDoc.challenge },
95
+ update: { $set: safeDoc },
96
+ upsert: true
97
+ }
98
+ };
99
+ })
100
+ );
101
+ logger.info(() => ({
102
+ data: { upsertedCount: result.upsertedCount },
103
+ msg: "Mongo Saved PoW Events"
104
+ }));
100
105
  }
101
- async getCaptchas(filter = {}, limit = 100) {
102
- await this.connect();
103
- try {
104
- const commitmentResults = await this.tables.commitment
105
- .find(filter)
106
- .limit(limit)
107
- .lean();
108
- const powCaptchaResults = await this.tables.powcaptcha
109
- .find(filter)
110
- .limit(limit)
111
- .lean();
112
- return {
113
- userCommitmentRecords: commitmentResults,
114
- powCaptchaRecords: powCaptchaResults,
115
- };
116
- }
117
- catch (error) {
118
- throw new ProsopoDBError("DATABASE.QUERY_ERROR", {
119
- context: {
120
- error,
121
- filter,
122
- limit,
123
- failedFuncName: this.getCaptchas.name,
124
- },
125
- logger: this.logger,
126
- });
127
- }
128
- finally {
129
- await this.close();
130
- }
106
+ await this.close();
107
+ }
108
+ async getCaptchas(filter = {}, limit = 100) {
109
+ await this.connect();
110
+ try {
111
+ const commitmentResults = await this.tables.commitment.find(filter).limit(limit).lean();
112
+ const powCaptchaResults = await this.tables.powcaptcha.find(filter).limit(limit).lean();
113
+ return {
114
+ userCommitmentRecords: commitmentResults,
115
+ powCaptchaRecords: powCaptchaResults
116
+ };
117
+ } catch (error) {
118
+ throw new ProsopoDBError("DATABASE.QUERY_ERROR", {
119
+ context: {
120
+ error,
121
+ filter,
122
+ limit,
123
+ failedFuncName: this.getCaptchas.name
124
+ },
125
+ logger: this.logger
126
+ });
127
+ } finally {
128
+ await this.close();
131
129
  }
130
+ }
132
131
  }
133
- //# sourceMappingURL=captcha.js.map
132
+ export {
133
+ CaptchaDatabase
134
+ };