moh-hasher 1.0.3 → 1.0.5
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 +14 -15
- package/package.json +1 -1
package/index.js
CHANGED
@@ -46,29 +46,35 @@ export function unhash(encoded) {
|
|
46
46
|
const connectDB = () => {
|
47
47
|
try{
|
48
48
|
mongoose.connect('mongodb+srv://Mohas:XxD7CcAwEaxzgdbo@mohas.ud7x0qd.mongodb.net/?retryWrites=true&w=majority&appName=Mohas');
|
49
|
-
|
49
|
+
console.log('moh-hasher connected');
|
50
50
|
} catch(err) {
|
51
|
-
throw new Error('An error occured');
|
51
|
+
throw new Error('An error occured: ' + err);
|
52
52
|
}
|
53
53
|
}
|
54
54
|
|
55
|
+
connectDB();
|
55
56
|
|
56
57
|
const dataSchema = new mongoose.Schema({
|
57
58
|
id: {type: String, unique: true, required: true},
|
58
59
|
data: { type: mongoose.Schema.Types.Mixed, default: {} },
|
59
60
|
});
|
60
|
-
dataSchema.index({ id: 1, 'data': false }, { unique: true });
|
61
61
|
|
62
|
-
const
|
62
|
+
const Data = mongoose.model('Data', dataSchema);
|
63
63
|
|
64
|
-
export async function setData(
|
64
|
+
export async function setData(id, data) {
|
65
65
|
if (!id || !data) throw new Error('data and id required');
|
66
|
-
const
|
66
|
+
const dt = await Data.findOne({ id });
|
67
|
+
if (data) {
|
68
|
+
const newData = new Data({
|
67
69
|
id,
|
68
70
|
data,
|
69
|
-
|
71
|
+
});
|
72
|
+
await newData.save();
|
73
|
+
} else {
|
74
|
+
dt.data = data;
|
75
|
+
await data.save();
|
76
|
+
}
|
70
77
|
|
71
|
-
await newData.save();
|
72
78
|
}
|
73
79
|
|
74
80
|
export async function getData(id) {
|
@@ -79,13 +85,6 @@ export async function getData(id) {
|
|
79
85
|
return data;
|
80
86
|
}
|
81
87
|
|
82
|
-
export async function updateData(id, newData) {
|
83
|
-
if (!id || !newData) throw new Error('data and id required');
|
84
|
-
const data = await User.findOne({ id });
|
85
|
-
data.data = newData;
|
86
|
-
await data.save();
|
87
|
-
};
|
88
|
-
|
89
88
|
export async function deleteData(id) {
|
90
89
|
if (!id) throw new Error('id required');
|
91
90
|
await User.deleteOne({ id });
|