@prosopo/database 3.0.8 → 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 +29 -0
- package/dist/base/index.js +6 -3
- package/dist/base/mongo.js +122 -117
- package/dist/base/mongoMemory.js +24 -23
- package/dist/databases/captcha.js +124 -123
- package/dist/databases/client.js +58 -48
- package/dist/databases/index.js +12 -10
- package/dist/databases/provider.js +1218 -1013
- package/dist/index.js +15 -3
- package/package.json +14 -10
- package/vite.cjs.config.ts +4 -1
- package/vite.esm.config.ts +20 -0
- package/dist/base/index.d.ts +0 -3
- package/dist/base/index.d.ts.map +0 -1
- package/dist/base/index.js.map +0 -1
- package/dist/base/mongo.d.ts +0 -18
- package/dist/base/mongo.d.ts.map +0 -1
- package/dist/base/mongo.js.map +0 -1
- package/dist/base/mongoMemory.d.ts +0 -11
- package/dist/base/mongoMemory.d.ts.map +0 -1
- package/dist/base/mongoMemory.js.map +0 -1
- package/dist/databases/captcha.d.ts +0 -23
- package/dist/databases/captcha.d.ts.map +0 -1
- package/dist/databases/captcha.js.map +0 -1
- package/dist/databases/client.d.ts +0 -12
- package/dist/databases/client.d.ts.map +0 -1
- package/dist/databases/client.js.map +0 -1
- package/dist/databases/index.d.ts +0 -16
- package/dist/databases/index.d.ts.map +0 -1
- package/dist/databases/index.js.map +0 -1
- package/dist/databases/provider.d.ts +0 -102
- package/dist/databases/provider.d.ts.map +0 -1
- package/dist/databases/provider.js.map +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
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
|
+
|
|
25
|
+
## 3.0.9
|
|
26
|
+
### Patch Changes
|
|
27
|
+
|
|
28
|
+
- Updated dependencies [b7c3258]
|
|
29
|
+
- @prosopo/user-access-policy@3.3.0
|
|
30
|
+
- @prosopo/types-database@3.0.9
|
|
31
|
+
|
|
3
32
|
## 3.0.8
|
|
4
33
|
### Patch Changes
|
|
5
34
|
|
package/dist/base/index.js
CHANGED
package/dist/base/mongo.js
CHANGED
|
@@ -1,129 +1,134 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
23
|
-
|
|
14
|
+
if (authSource) {
|
|
15
|
+
parsedUrl.searchParams.set("authSource", authSource);
|
|
24
16
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
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
|
-
|
|
37
|
-
|
|
45
|
+
data: { mongoUrl: this.safeURL },
|
|
46
|
+
msg: "Database connection already open"
|
|
38
47
|
}));
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
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
|
-
|
|
132
|
+
export {
|
|
133
|
+
MongoDatabase
|
|
134
|
+
};
|
package/dist/base/mongoMemory.js
CHANGED
|
@@ -1,27 +1,28 @@
|
|
|
1
1
|
import { MongoMemoryServer } from "mongodb-memory-server";
|
|
2
2
|
import { MongoDatabase } from "./mongo.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
26
|
+
export {
|
|
27
|
+
MongoMemoryDatabase
|
|
28
|
+
};
|
|
@@ -1,133 +1,134 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
})(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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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
|
-
|
|
132
|
+
export {
|
|
133
|
+
CaptchaDatabase
|
|
134
|
+
};
|