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.
Files changed (2) hide show
  1. package/index.js +27 -38
  2. 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
- const connectDB = () => {
47
- try{
48
- mongoose.connect('mongodb+srv://Mohas:XxD7CcAwEaxzgdbo@mohas.ud7x0qd.mongodb.net/?retryWrites=true&w=majority&appName=Mohas');
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
- const dataSchema = new mongoose.Schema({
58
- id: {type: String, unique: true, required: true},
59
- data: { type: mongoose.Schema.Types.Mixed, default: {} },
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 Data = mongoose.model('Data', dataSchema);
65
+ const DataModel = mongoose.models.DataModel || mongoose.model('DataModel', DataSchema);
63
66
 
64
67
  export async function setData(id, data) {
65
- if (!id || !data) throw new Error('data and id required');
66
- const dt = await Data.findOne({ id });
67
- if (data) {
68
- const newData = new Data({
69
- id,
70
- data,
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
- if (!id) throw new Error('id required');
82
- const data = await User.findOne({ id });
83
- data.id = null;
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
- if (!id) throw new Error('id required');
90
- await User.deleteOne({ id });
91
- };
92
-
93
-
94
- export function compare(original, hashed) {
95
- return hash(original) === hashed;
83
+ await connectDB();
84
+ await DataModel.deleteOne({ id });
96
85
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "moh-hasher",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "A simple encoding and decoding package with unique hashes",
5
5
  "main": "index.js",
6
6
  "type": "module",