@prosopo/database 2.1.5 → 2.1.6

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.
Files changed (64) hide show
  1. package/dist/base/index.d.ts +3 -0
  2. package/dist/base/index.d.ts.map +1 -0
  3. package/dist/base/index.js +3 -0
  4. package/dist/base/index.js.map +1 -0
  5. package/dist/base/mongo.d.ts +16 -0
  6. package/dist/base/mongo.d.ts.map +1 -0
  7. package/dist/base/mongo.js +89 -0
  8. package/dist/base/mongo.js.map +1 -0
  9. package/dist/base/mongoMemory.d.ts +11 -0
  10. package/dist/base/mongoMemory.d.ts.map +1 -0
  11. package/dist/base/mongoMemory.js +27 -0
  12. package/dist/base/mongoMemory.js.map +1 -0
  13. package/dist/captchaDatabase/captchaDatabse.d.ts +3 -0
  14. package/dist/captchaDatabase/captchaDatabse.d.ts.map +1 -0
  15. package/dist/captchaDatabase/captchaDatabse.js +22 -0
  16. package/dist/captchaDatabase/captchaDatabse.js.map +1 -0
  17. package/dist/captchaDatabase/index.d.ts +2 -0
  18. package/dist/captchaDatabase/index.d.ts.map +1 -0
  19. package/dist/captchaDatabase/index.js +2 -0
  20. package/dist/captchaDatabase/index.js.map +1 -0
  21. package/dist/cjs/base/index.cjs +6 -0
  22. package/dist/cjs/base/mongo.cjs +97 -0
  23. package/dist/cjs/base/mongoMemory.cjs +28 -0
  24. package/dist/cjs/databases/captcha.cjs +82 -0
  25. package/dist/cjs/databases/client.cjs +52 -0
  26. package/dist/cjs/databases/index.cjs +19 -0
  27. package/dist/cjs/databases/provider.cjs +983 -0
  28. package/dist/cjs/index.cjs +15 -0
  29. package/dist/databases/captcha.d.ts +16 -0
  30. package/dist/databases/captcha.d.ts.map +1 -0
  31. package/dist/databases/captcha.js +75 -0
  32. package/dist/databases/captcha.js.map +1 -0
  33. package/dist/databases/client.d.ts +12 -0
  34. package/dist/databases/client.d.ts.map +1 -0
  35. package/dist/databases/client.js +48 -0
  36. package/dist/databases/client.js.map +1 -0
  37. package/dist/databases/index.d.ts +16 -0
  38. package/dist/databases/index.d.ts.map +1 -0
  39. package/dist/databases/index.js +17 -0
  40. package/dist/databases/index.js.map +1 -0
  41. package/dist/databases/mongo.d.ts +58 -0
  42. package/dist/databases/mongo.d.ts.map +1 -0
  43. package/dist/databases/mongo.js +682 -0
  44. package/dist/databases/mongo.js.map +1 -0
  45. package/dist/databases/mongoMemory.d.ts +10 -0
  46. package/dist/databases/mongoMemory.d.ts.map +1 -0
  47. package/dist/databases/mongoMemory.js +30 -0
  48. package/dist/databases/mongoMemory.js.map +1 -0
  49. package/dist/databases/provider.d.ts +73 -0
  50. package/dist/databases/provider.d.ts.map +1 -0
  51. package/dist/databases/provider.js +823 -0
  52. package/dist/databases/provider.js.map +1 -0
  53. package/dist/eventsDatabase/eventsDatabase.d.ts +3 -0
  54. package/dist/eventsDatabase/eventsDatabase.d.ts.map +1 -0
  55. package/dist/eventsDatabase/eventsDatabase.js +46 -0
  56. package/dist/eventsDatabase/eventsDatabase.js.map +1 -0
  57. package/dist/eventsDatabase/index.d.ts +2 -0
  58. package/dist/eventsDatabase/index.d.ts.map +1 -0
  59. package/dist/eventsDatabase/index.js +2 -0
  60. package/dist/eventsDatabase/index.js.map +1 -0
  61. package/dist/index.d.ts +3 -0
  62. package/dist/index.d.ts.map +1 -0
  63. package/dist/index.js.map +1 -0
  64. package/package.json +5 -5
@@ -0,0 +1,3 @@
1
+ export * from "./mongo.js";
2
+ export * from "./mongoMemory.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/base/index.ts"],"names":[],"mappings":"AAaA,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from "./mongo.js";
2
+ export * from "./mongoMemory.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/base/index.ts"],"names":[],"mappings":"AAaA,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1,16 @@
1
+ import { type Logger } from "@prosopo/common";
2
+ import mongoose, { type Connection } from "mongoose";
3
+ export declare class MongoDatabase {
4
+ protected readonly _url: string;
5
+ safeURL: string;
6
+ dbname: string;
7
+ connection?: Connection;
8
+ logger: Logger;
9
+ connected: boolean;
10
+ constructor(url: string, dbname?: string, authSource?: string, logger?: Logger);
11
+ get url(): string;
12
+ getConnection(): mongoose.Connection;
13
+ connect(): Promise<void>;
14
+ close(): Promise<void>;
15
+ }
16
+ //# sourceMappingURL=mongo.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mongo.d.ts","sourceRoot":"","sources":["../../src/base/mongo.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,KAAK,MAAM,EAAoC,MAAM,iBAAiB,CAAC;AAEhF,OAAO,QAAQ,EAAE,EAAE,KAAK,UAAU,EAAE,MAAM,UAAU,CAAC;AAarD,qBAAa,aAAa;IACzB,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,UAAS;gBAGjB,GAAG,EAAE,MAAM,EACX,MAAM,CAAC,EAAE,MAAM,EACf,UAAU,CAAC,EAAE,MAAM,EACnB,MAAM,CAAC,EAAE,MAAM;IAgBhB,IAAI,GAAG,IAAI,MAAM,CAEhB;IAED,aAAa,IAAI,QAAQ,CAAC,UAAU;IAa9B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAgExB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAI5B"}
@@ -0,0 +1,89 @@
1
+ import { ProsopoDBError, getLoggerDefault } from "@prosopo/common";
2
+ import { ServerApiVersion } from "mongodb";
3
+ import mongoose from "mongoose";
4
+ mongoose.set("strictQuery", false);
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 || getLoggerDefault();
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
+ });
31
+ }
32
+ return this.connection;
33
+ }
34
+ async connect() {
35
+ this.logger.info(`Mongo url: ${this.safeURL}`);
36
+ if (this.connected) {
37
+ this.logger.info(`Database connection to ${this.safeURL} already open`);
38
+ return;
39
+ }
40
+ this.connection = await new Promise((resolve, reject) => {
41
+ const connection = mongoose.createConnection(this.url, {
42
+ dbName: this.dbname,
43
+ serverApi: ServerApiVersion.v1,
44
+ });
45
+ connection.on("open", () => {
46
+ this.logger.info(`Database connection to ${this.safeURL} opened`);
47
+ this.connected = true;
48
+ resolve(connection);
49
+ });
50
+ connection.on("error", (err) => {
51
+ this.connected = false;
52
+ this.logger.error(`Database error: ${err}`);
53
+ reject(err);
54
+ });
55
+ connection.on("connected", () => {
56
+ this.logger.info(`Database connected to ${this.safeURL}`);
57
+ this.connected = true;
58
+ resolve(connection);
59
+ });
60
+ connection.on("disconnected", () => {
61
+ this.connected = false;
62
+ this.logger.info(`Database disconnected from ${this.safeURL}`);
63
+ });
64
+ connection.on("reconnected", () => {
65
+ this.logger.info(`Database reconnected to ${this.safeURL}`);
66
+ this.connected = true;
67
+ resolve(connection);
68
+ });
69
+ connection.on("reconnectFailed", () => {
70
+ this.connected = false;
71
+ this.logger.error(`Database reconnect failed to ${this.safeURL}`);
72
+ });
73
+ connection.on("close", () => {
74
+ this.connected = false;
75
+ this.logger.info(`Database connection to ${this.safeURL} closed`);
76
+ });
77
+ connection.on("fullsetup", () => {
78
+ this.connected = true;
79
+ this.logger.info(`Database connection to ${this.safeURL} is fully setup`);
80
+ resolve(connection);
81
+ });
82
+ });
83
+ }
84
+ async close() {
85
+ this.logger.debug(`Closing connection to ${this.safeURL}`);
86
+ await this.connection?.close();
87
+ }
88
+ }
89
+ //# sourceMappingURL=mongo.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mongo.js","sourceRoot":"","sources":["../../src/base/mongo.ts"],"names":[],"mappings":"AAaA,OAAO,EAAe,cAAc,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAChF,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,QAA6B,MAAM,UAAU,CAAC;AAErD,QAAQ,CAAC,GAAG,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC;AAGnC,MAAM,gBAAgB,GAAG,2BAA2B,CAAC;AAQrD,MAAM,OAAO,aAAa;IAQzB,YACC,GAAW,EACX,MAAe,EACf,UAAmB,EACnB,MAAe;QANhB,cAAS,GAAG,KAAK,CAAC;QAQjB,MAAM,YAAY,GAAG,GAAG,IAAI,gBAAgB,CAAC;QAC7C,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,YAAY,CAAC,CAAC;QACxC,IAAI,MAAM,EAAE,CAAC;YACZ,SAAS,CAAC,QAAQ,GAAG,MAAM,CAAC;QAC7B,CAAC;QACD,IAAI,UAAU,EAAE,CAAC;YAChB,SAAS,CAAC,YAAY,CAAC,GAAG,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;QACtD,CAAC;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AE,GAAG,EAAE;gBACrC,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;gBACvB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,gCAAgC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;YACnE,CAAC,CAAC,CAAC;YAEH,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;gBAC3B,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;gBACvB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,0BAA0B,IAAI,CAAC,OAAO,SAAS,CAAC,CAAC;YACnE,CAAC,CAAC,CAAC;YAEH,UAAU,CAAC,EAAE,CAAC,WAAW,EAAE,GAAG,EAAE;gBAC/B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;gBACtB,IAAI,CAAC,MAAM,CAAC,IAAI,CACf,0BAA0B,IAAI,CAAC,OAAO,iBAAiB,CACvD,CAAC;gBACF,OAAO,CAAC,UAAU,CAAC,CAAC;YACrB,CAAC,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;IACJ,CAAC;IAGD,KAAK,CAAC,KAAK;QACV,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,yBAAyB,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;QAC3D,MAAM,IAAI,CAAC,UAAU,EAAE,KAAK,EAAE,CAAC;IAChC,CAAC;CACD"}
@@ -0,0 +1,11 @@
1
+ import type { Logger } from "@prosopo/common";
2
+ import { MongoDatabase } from "./mongo.js";
3
+ export declare class MongoMemoryDatabase extends MongoDatabase {
4
+ protected readonly _url: string;
5
+ private mongod;
6
+ private running;
7
+ constructor(url: string, dbname: string, logger: Logger, authSource?: string);
8
+ connect(): Promise<void>;
9
+ close(): Promise<void>;
10
+ }
11
+ //# sourceMappingURL=mongoMemory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mongoMemory.d.ts","sourceRoot":"","sources":["../../src/base/mongoMemory.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAE9C,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3C,qBAAa,mBAAoB,SAAQ,aAAa;IACrD,mBAA4B,IAAI,EAAE,MAAM,CAAC;IACzC,OAAO,CAAC,MAAM,CAAgC;IAC9C,OAAO,CAAC,OAAO,CAAS;gBAGvB,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,UAAU,CAAC,EAAE,MAAM;IASX,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAYlB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAOrC"}
@@ -0,0 +1,27 @@
1
+ import { MongoMemoryServer } from "mongodb-memory-server";
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;
25
+ }
26
+ }
27
+ //# sourceMappingURL=mongoMemory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mongoMemory.js","sourceRoot":"","sources":["../../src/base/mongoMemory.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3C,MAAM,OAAO,mBAAoB,SAAQ,aAAa;IAKrD,YACC,GAAW,EACX,MAAc,EACd,MAAc,EACd,UAAmB;QAEnB,MAAM,MAAM,GAAG,IAAI,iBAAiB,EAAE,CAAC;QACvC,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;QACvC,KAAK,CAAC,cAAc,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;QAV3C,YAAO,GAAG,KAAK,CAAC;QAWvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC5B,CAAC;IAEQ,OAAO;QACf,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;YAGnB,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC;YACrB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACrB,CAAC;aAAM,CAAC;QAER,CAAC;QACD,OAAO,KAAK,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAEQ,KAAK,CAAC,KAAK;QACnB,MAAM,KAAK,CAAC,KAAK,EAAE,CAAC;QAGpB,MAAM,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC;QAC1B,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;IACtB,CAAC;CACD"}
@@ -0,0 +1,3 @@
1
+ import { type UserCommitmentRecord } from '@prosopo/types-database';
2
+ export declare const saveCaptchas: (events: UserCommitmentRecord[], atlasUri: string) => Promise<void>;
3
+ //# sourceMappingURL=captchaDatabse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"captchaDatabse.d.ts","sourceRoot":"","sources":["../../src/captchaDatabase/captchaDatabse.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,KAAK,oBAAoB,EAA8B,MAAM,yBAAyB,CAAA;AAO/F,eAAO,MAAM,YAAY,WAAkB,oBAAoB,EAAE,YAAY,MAAM,kBAelF,CAAA"}
@@ -0,0 +1,22 @@
1
+ import { UserCommitmentRecordSchema } from '@prosopo/types-database';
2
+ import { getLoggerDefault } from '@prosopo/common';
3
+ import mongoose from 'mongoose';
4
+ const logger = getLoggerDefault();
5
+ const StoredCaptcha = mongoose.model('StoredCaptcha', UserCommitmentRecordSchema);
6
+ export const saveCaptchas = async (events, atlasUri) => {
7
+ const connection = mongoose.createConnection(atlasUri, {
8
+ authSource: 'admin',
9
+ });
10
+ await new Promise((resolve, reject) => {
11
+ connection
12
+ .once('open', () => {
13
+ logger.info('Connected to MongoDB Atlas');
14
+ resolve();
15
+ })
16
+ .on('error', reject);
17
+ });
18
+ await StoredCaptcha.insertMany(events);
19
+ logger.info('Mongo Saved Events');
20
+ await connection.close();
21
+ };
22
+ //# sourceMappingURL=captchaDatabse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"captchaDatabse.js","sourceRoot":"","sources":["../../src/captchaDatabase/captchaDatabse.ts"],"names":[],"mappings":"AAaA,OAAO,EAA6B,0BAA0B,EAAE,MAAM,yBAAyB,CAAA;AAC/F,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,QAAQ,MAAM,UAAU,CAAA;AAC/B,MAAM,MAAM,GAAG,gBAAgB,EAAE,CAAA;AAEjC,MAAM,aAAa,GAAG,QAAQ,CAAC,KAAK,CAAC,eAAe,EAAE,0BAA0B,CAAC,CAAA;AAEjF,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,EAAE,MAA8B,EAAE,QAAgB,EAAE,EAAE;IACnF,MAAM,UAAU,GAAG,QAAQ,CAAC,gBAAgB,CAAC,QAAQ,EAAE;QACnD,UAAU,EAAE,OAAO;KACtB,CAAC,CAAA;IACF,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACxC,UAAU;aACL,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE;YACf,MAAM,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAAA;YACzC,OAAO,EAAE,CAAA;QACb,CAAC,CAAC;aACD,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;IAC5B,CAAC,CAAC,CAAA;IACF,MAAM,aAAa,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;IACtC,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAA;IACjC,MAAM,UAAU,CAAC,KAAK,EAAE,CAAA;AAC5B,CAAC,CAAA"}
@@ -0,0 +1,2 @@
1
+ export * from './captchaDatabse.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/captchaDatabase/index.ts"],"names":[],"mappings":"AAaA,cAAc,qBAAqB,CAAA"}
@@ -0,0 +1,2 @@
1
+ export * from './captchaDatabse.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/captchaDatabase/index.ts"],"names":[],"mappings":"AAaA,cAAc,qBAAqB,CAAA"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const mongo = require("./mongo.cjs");
4
+ const mongoMemory = require("./mongoMemory.cjs");
5
+ exports.MongoDatabase = mongo.MongoDatabase;
6
+ exports.MongoMemoryDatabase = mongoMemory.MongoMemoryDatabase;
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const common = require("@prosopo/common");
4
+ const mongodb = require("mongodb");
5
+ const mongoose = require("mongoose");
6
+ mongoose.set("strictQuery", false);
7
+ const DEFAULT_ENDPOINT = "mongodb://127.0.0.1:27017";
8
+ class MongoDatabase {
9
+ constructor(url, dbname, authSource, logger) {
10
+ this.connected = false;
11
+ const baseEndpoint = url || DEFAULT_ENDPOINT;
12
+ const parsedUrl = new URL(baseEndpoint);
13
+ if (dbname) {
14
+ parsedUrl.pathname = dbname;
15
+ }
16
+ if (authSource) {
17
+ parsedUrl.searchParams.set("authSource", authSource);
18
+ }
19
+ this._url = parsedUrl.toString();
20
+ this.safeURL = this.url.replace(/\w+:\w+/, "<Credentials>");
21
+ this.dbname = dbname || parsedUrl.pathname.replace("/", "");
22
+ this.logger = logger || common.getLoggerDefault();
23
+ }
24
+ get url() {
25
+ return this._url;
26
+ }
27
+ getConnection() {
28
+ if (!this.connection) {
29
+ throw new common.ProsopoDBError("DATABASE.CONNECTION_UNDEFINED", {
30
+ context: { failedFuncName: this.getConnection.name },
31
+ logger: this.logger
32
+ });
33
+ }
34
+ return this.connection;
35
+ }
36
+ /**
37
+ * @description Connect to the database and set the various tables
38
+ */
39
+ async connect() {
40
+ this.logger.info(`Mongo url: ${this.safeURL}`);
41
+ if (this.connected) {
42
+ this.logger.info(`Database connection to ${this.safeURL} already open`);
43
+ return;
44
+ }
45
+ this.connection = await new Promise((resolve, reject) => {
46
+ const connection = mongoose.createConnection(this.url, {
47
+ dbName: this.dbname,
48
+ serverApi: mongodb.ServerApiVersion.v1
49
+ });
50
+ connection.on("open", () => {
51
+ this.logger.info(`Database connection to ${this.safeURL} opened`);
52
+ this.connected = true;
53
+ resolve(connection);
54
+ });
55
+ connection.on("error", (err) => {
56
+ this.connected = false;
57
+ this.logger.error(`Database error: ${err}`);
58
+ reject(err);
59
+ });
60
+ connection.on("connected", () => {
61
+ this.logger.info(`Database connected to ${this.safeURL}`);
62
+ this.connected = true;
63
+ resolve(connection);
64
+ });
65
+ connection.on("disconnected", () => {
66
+ this.connected = false;
67
+ this.logger.info(`Database disconnected from ${this.safeURL}`);
68
+ });
69
+ connection.on("reconnected", () => {
70
+ this.logger.info(`Database reconnected to ${this.safeURL}`);
71
+ this.connected = true;
72
+ resolve(connection);
73
+ });
74
+ connection.on("reconnectFailed", () => {
75
+ this.connected = false;
76
+ this.logger.error(`Database reconnect failed to ${this.safeURL}`);
77
+ });
78
+ connection.on("close", () => {
79
+ this.connected = false;
80
+ this.logger.info(`Database connection to ${this.safeURL} closed`);
81
+ });
82
+ connection.on("fullsetup", () => {
83
+ this.connected = true;
84
+ this.logger.info(
85
+ `Database connection to ${this.safeURL} is fully setup`
86
+ );
87
+ resolve(connection);
88
+ });
89
+ });
90
+ }
91
+ /** Close connection to the database */
92
+ async close() {
93
+ this.logger.debug(`Closing connection to ${this.safeURL}`);
94
+ await this.connection?.close();
95
+ }
96
+ }
97
+ exports.MongoDatabase = MongoDatabase;
@@ -0,0 +1,28 @@
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
+ const mongod = new mongodbMemoryServer.MongoMemoryServer();
8
+ const mongoMemoryURL = mongod.getUri();
9
+ super(mongoMemoryURL, dbname, authSource, logger);
10
+ this.running = false;
11
+ this.mongod = mongod;
12
+ this._url = mongoMemoryURL;
13
+ }
14
+ connect() {
15
+ if (!this.running) {
16
+ this.mongod?.start();
17
+ this.running = true;
18
+ } else {
19
+ }
20
+ return super.connect();
21
+ }
22
+ async close() {
23
+ await super.close();
24
+ await this.mongod?.stop();
25
+ this.running = false;
26
+ }
27
+ }
28
+ exports.MongoMemoryDatabase = MongoMemoryDatabase;
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const common = require("@prosopo/common");
4
+ const typesDatabase = require("@prosopo/types-database");
5
+ require("../base/index.cjs");
6
+ const mongo = require("../base/mongo.cjs");
7
+ const logger = common.getLoggerDefault();
8
+ var TableNames = /* @__PURE__ */ ((TableNames2) => {
9
+ TableNames2["commitment"] = "commitment";
10
+ TableNames2["powcaptcha"] = "powcaptcha";
11
+ return TableNames2;
12
+ })(TableNames || {});
13
+ const CAPTCHA_TABLES = [
14
+ {
15
+ collectionName: "powcaptcha",
16
+ modelName: "PowCaptcha",
17
+ schema: typesDatabase.PoWCaptchaRecordSchema
18
+ },
19
+ {
20
+ collectionName: "commitment",
21
+ modelName: "UserCommitment",
22
+ schema: typesDatabase.UserCommitmentRecordSchema
23
+ }
24
+ ];
25
+ class CaptchaDatabase extends mongo.MongoDatabase {
26
+ constructor(url, dbname, authSource, logger2) {
27
+ super(url, dbname, authSource, logger2);
28
+ this.tables = {};
29
+ }
30
+ async connect() {
31
+ await super.connect();
32
+ CAPTCHA_TABLES.map(({ collectionName, modelName, schema }) => {
33
+ if (this.connection) {
34
+ this.tables[collectionName] = this.connection.model(modelName, schema);
35
+ }
36
+ });
37
+ }
38
+ getTables() {
39
+ if (!this.tables) {
40
+ throw new common.ProsopoDBError("DATABASE.TABLES_UNDEFINED", {
41
+ context: { failedFuncName: this.getTables.name },
42
+ logger: this.logger
43
+ });
44
+ }
45
+ return this.tables;
46
+ }
47
+ async saveCaptchas(imageCaptchaEvents, powCaptchaEvents) {
48
+ await this.connect();
49
+ if (imageCaptchaEvents.length) {
50
+ const result = await this.tables.commitment.bulkWrite(
51
+ imageCaptchaEvents.map((doc) => {
52
+ const { _id, ...safeDoc } = doc;
53
+ return {
54
+ updateOne: {
55
+ filter: { id: safeDoc.id },
56
+ update: { $set: safeDoc },
57
+ upsert: true
58
+ }
59
+ };
60
+ })
61
+ );
62
+ logger.info("Mongo Saved Image Events", result);
63
+ }
64
+ if (powCaptchaEvents.length) {
65
+ const result = await this.tables.powcaptcha.bulkWrite(
66
+ powCaptchaEvents.map((doc) => {
67
+ const { _id, ...safeDoc } = doc;
68
+ return {
69
+ updateOne: {
70
+ filter: { challenge: safeDoc.challenge },
71
+ update: { $set: safeDoc },
72
+ upsert: true
73
+ }
74
+ };
75
+ })
76
+ );
77
+ logger.info("Mongo Saved PoW Events", result);
78
+ }
79
+ await this.close();
80
+ }
81
+ }
82
+ exports.CaptchaDatabase = CaptchaDatabase;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const common = require("@prosopo/common");
4
+ const typesDatabase = require("@prosopo/types-database");
5
+ require("../base/index.cjs");
6
+ const mongo = require("../base/mongo.cjs");
7
+ const CLIENT_TABLES = [
8
+ {
9
+ collectionName: typesDatabase.TableNames.emails,
10
+ modelName: "Email",
11
+ schema: typesDatabase.UserDataSchema
12
+ }
13
+ ];
14
+ class ClientDatabase extends mongo.MongoDatabase {
15
+ constructor(url, dbname, authSource, logger) {
16
+ super(url, dbname, authSource, logger);
17
+ this.tables = {};
18
+ }
19
+ async connect() {
20
+ await super.connect();
21
+ CLIENT_TABLES.map(({ collectionName, modelName, schema }) => {
22
+ if (this.connection) {
23
+ this.tables[collectionName] = this.connection.model(modelName, schema);
24
+ }
25
+ });
26
+ }
27
+ getTables() {
28
+ if (!this.tables) {
29
+ throw new common.ProsopoDBError("DATABASE.TABLES_UNDEFINED", {
30
+ context: { failedFuncName: this.getTables.name },
31
+ logger: this.logger
32
+ });
33
+ }
34
+ return this.tables;
35
+ }
36
+ async getUpdatedClients(updatedAtTimestamp) {
37
+ await this.connect();
38
+ const newClientRecords = await this.tables.emails.find(
39
+ {
40
+ $or: [
41
+ { updatedAtTimestamp: { $gt: updatedAtTimestamp } },
42
+ { updatedAtTimestamp: { $exists: false } }
43
+ ],
44
+ activated: true
45
+ },
46
+ { account: 1, settings: 1 }
47
+ ).lean();
48
+ await this.close();
49
+ return newClientRecords;
50
+ }
51
+ }
52
+ exports.ClientDatabase = ClientDatabase;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const types = require("@prosopo/types");
4
+ const mongo = require("../base/mongo.cjs");
5
+ const mongoMemory = require("../base/mongoMemory.cjs");
6
+ const captcha = require("./captcha.cjs");
7
+ const client = require("./client.cjs");
8
+ const provider = require("./provider.cjs");
9
+ const Databases = {
10
+ [types.DatabaseTypes.Values.mongo]: mongo.MongoDatabase,
11
+ [types.DatabaseTypes.Values.provider]: provider.ProviderDatabase,
12
+ [types.DatabaseTypes.Values.client]: client.ClientDatabase,
13
+ [types.DatabaseTypes.Values.captcha]: captcha.CaptchaDatabase,
14
+ [types.DatabaseTypes.Values.mongoMemory]: mongoMemory.MongoMemoryDatabase
15
+ };
16
+ exports.CaptchaDatabase = captcha.CaptchaDatabase;
17
+ exports.ClientDatabase = client.ClientDatabase;
18
+ exports.ProviderDatabase = provider.ProviderDatabase;
19
+ exports.Databases = Databases;