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.
Files changed (2) hide show
  1. package/index.js +20 -11
  2. 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.createConnect('mongodb+srv://Mohas:XxD7CcAwEaxzgdbo@mohas.ud7x0qd.mongodb.net/?retryWrites=true&w=majority&appName=Mohas');
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
- }, { timestamps: true });
64
+ });
64
65
 
65
- const DataModel = mongoose.models.DataModel || mongoose.model('DataModel', DataSchema);
66
+ let DataModel = null;
66
67
 
67
- export async function setData(id, data) {
68
+ async function getModel() {
68
69
  await connectDB();
69
- await DataModel.findOneAndUpdate(
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 connectDB();
78
- const doc = await DataModel.findOne({ id });
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 connectDB();
84
- await DataModel.deleteOne({ id });
92
+ const model = await getModel();
93
+ await model.deleteOne({ id });
85
94
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "moh-hasher",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "A simple encoding and decoding package with unique hashes",
5
5
  "main": "index.js",
6
6
  "type": "module",