moh-hasher 1.0.7 → 1.0.9
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/index.js +20 -11
- package/package.json +1 -1
package/index.js
CHANGED
@@ -48,25 +48,34 @@ export function compare(original, hashed) {
|
|
48
48
|
return hash(original) === hashed;
|
49
49
|
}
|
50
50
|
|
51
|
-
|
52
51
|
let isConnected = false;
|
53
|
-
|
52
|
+
let connection = null;
|
53
|
+
|
54
54
|
async function connectDB() {
|
55
55
|
if (isConnected) return;
|
56
|
-
await mongoose.
|
56
|
+
connection = await mongoose.createConnection(
|
57
|
+
'mongodb+srv://Mohas:XxD7CcAwEaxzgdbo@mohas.ud7x0qd.mongodb.net/?retryWrites=true&w=majority&appName=Mohas');
|
57
58
|
isConnected = true;
|
58
59
|
}
|
59
60
|
|
60
61
|
const DataSchema = new mongoose.Schema({
|
61
62
|
id: { type: String, required: true, unique: true },
|
62
63
|
data: { type: mongoose.Schema.Types.Mixed },
|
63
|
-
}
|
64
|
+
});
|
64
65
|
|
65
|
-
|
66
|
+
let DataModel = null;
|
66
67
|
|
67
|
-
|
68
|
+
async function getModel() {
|
68
69
|
await connectDB();
|
69
|
-
|
70
|
+
if (!DataModel) {
|
71
|
+
DataModel = connection.models.DataModel || connection.model('DataModel', DataSchema);
|
72
|
+
}
|
73
|
+
return DataModel;
|
74
|
+
}
|
75
|
+
|
76
|
+
export async function setData(id, data) {
|
77
|
+
const model = await getModel();
|
78
|
+
await model.findOneAndUpdate(
|
70
79
|
{ id },
|
71
80
|
{ data },
|
72
81
|
{ upsert: true, new: true }
|
@@ -74,12 +83,12 @@ export async function setData(id, data) {
|
|
74
83
|
}
|
75
84
|
|
76
85
|
export async function getData(id) {
|
77
|
-
await
|
78
|
-
const doc = await
|
86
|
+
const model = await getModel();
|
87
|
+
const doc = await model.findOne({ id });
|
79
88
|
return doc ? doc.data : null;
|
80
89
|
}
|
81
90
|
|
82
91
|
export async function deleteData(id) {
|
83
|
-
await
|
84
|
-
await
|
92
|
+
const model = await getModel();
|
93
|
+
await model.deleteOne({ id });
|
85
94
|
}
|