moh-hasher 1.0.5 → 1.0.7
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 +27 -38
- package/package.json +1 -1
package/index.js
CHANGED
@@ -43,54 +43,43 @@ export function unhash(encoded) {
|
|
43
43
|
if (temp) decoded += temp;
|
44
44
|
return decoded;
|
45
45
|
}
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
console.log('moh-hasher connected');
|
50
|
-
} catch(err) {
|
51
|
-
throw new Error('An error occured: ' + err);
|
52
|
-
}
|
46
|
+
|
47
|
+
export function compare(original, hashed) {
|
48
|
+
return hash(original) === hashed;
|
53
49
|
}
|
54
50
|
|
55
|
-
connectDB();
|
56
51
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
52
|
+
let isConnected = false;
|
53
|
+
|
54
|
+
async function connectDB() {
|
55
|
+
if (isConnected) return;
|
56
|
+
await mongoose.createConnect('mongodb+srv://Mohas:XxD7CcAwEaxzgdbo@mohas.ud7x0qd.mongodb.net/?retryWrites=true&w=majority&appName=Mohas');
|
57
|
+
isConnected = true;
|
58
|
+
}
|
59
|
+
|
60
|
+
const DataSchema = new mongoose.Schema({
|
61
|
+
id: { type: String, required: true, unique: true },
|
62
|
+
data: { type: mongoose.Schema.Types.Mixed },
|
63
|
+
}, { timestamps: true });
|
61
64
|
|
62
|
-
const
|
65
|
+
const DataModel = mongoose.models.DataModel || mongoose.model('DataModel', DataSchema);
|
63
66
|
|
64
67
|
export async function setData(id, data) {
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
});
|
72
|
-
await newData.save();
|
73
|
-
} else {
|
74
|
-
dt.data = data;
|
75
|
-
await data.save();
|
76
|
-
}
|
77
|
-
|
68
|
+
await connectDB();
|
69
|
+
await DataModel.findOneAndUpdate(
|
70
|
+
{ id },
|
71
|
+
{ data },
|
72
|
+
{ upsert: true, new: true }
|
73
|
+
);
|
78
74
|
}
|
79
75
|
|
80
76
|
export async function getData(id) {
|
81
|
-
|
82
|
-
const
|
83
|
-
data
|
84
|
-
data._id = null;
|
85
|
-
return data;
|
77
|
+
await connectDB();
|
78
|
+
const doc = await DataModel.findOne({ id });
|
79
|
+
return doc ? doc.data : null;
|
86
80
|
}
|
87
81
|
|
88
82
|
export async function deleteData(id) {
|
89
|
-
|
90
|
-
await
|
91
|
-
};
|
92
|
-
|
93
|
-
|
94
|
-
export function compare(original, hashed) {
|
95
|
-
return hash(original) === hashed;
|
83
|
+
await connectDB();
|
84
|
+
await DataModel.deleteOne({ id });
|
96
85
|
}
|