sourev1 0.0.1-security → 1.2.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sourev1 might be problematic. Click here for more details.

Files changed (85) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/DOCS.md +1738 -0
  3. package/Extra/Database/index.js +398 -0
  4. package/Extra/Database/methods.js +286 -0
  5. package/Extra/Database/test/test.js +23 -0
  6. package/Extra/ExtraAddons.js +213 -0
  7. package/Extra/ExtraFindUID.js +48 -0
  8. package/Extra/ExtraGetThread.js +1 -0
  9. package/Extra/ExtraTranslate.js +62 -0
  10. package/Extra/ExtraUptimeRobot.js +3 -0
  11. package/Extra/Src/Last-Run.js +48 -0
  12. package/LICENSE-MIT +21 -0
  13. package/Language/index.json +149 -0
  14. package/README.md +132 -3
  15. package/Settings/Location.json +24 -0
  16. package/StateCrypt.js +22 -0
  17. package/broadcast.js +38 -0
  18. package/index.js +1071 -0
  19. package/logger.js +20 -0
  20. package/package.json +83 -3
  21. package/src/Dev_ChangeUptimeStatus.js +27 -0
  22. package/src/Dev_CheckLogin.js +14 -0
  23. package/src/addExternalModule.js +16 -0
  24. package/src/addUserToGroup.js +78 -0
  25. package/src/changeAdminStatus.js +79 -0
  26. package/src/changeArchivedStatus.js +41 -0
  27. package/src/changeBio.js +65 -0
  28. package/src/changeBlockedStatus.js +36 -0
  29. package/src/changeGroupImage.js +106 -0
  30. package/src/changeNickname.js +45 -0
  31. package/src/changeThreadColor.js +62 -0
  32. package/src/changeThreadEmoji.js +42 -0
  33. package/src/createNewGroup.js +70 -0
  34. package/src/createPoll.js +60 -0
  35. package/src/deleteMessage.js +45 -0
  36. package/src/deleteThread.js +43 -0
  37. package/src/forwardAttachment.js +48 -0
  38. package/src/getAccessToken.js +32 -0
  39. package/src/getCurrentUserID.js +7 -0
  40. package/src/getEmojiUrl.js +27 -0
  41. package/src/getFriendsList.js +73 -0
  42. package/src/getMessage.js +80 -0
  43. package/src/getThreadHistory.js +537 -0
  44. package/src/getThreadHistoryDeprecated.js +71 -0
  45. package/src/getThreadInfo.js +191 -0
  46. package/src/getThreadInfoDeprecated.js +56 -0
  47. package/src/getThreadList.js +213 -0
  48. package/src/getThreadListDeprecated.js +46 -0
  49. package/src/getThreadPictures.js +59 -0
  50. package/src/getUID.js +52 -0
  51. package/src/getUserID.js +62 -0
  52. package/src/getUserInfo.js +65 -0
  53. package/src/getUserInfoV2.js +35 -0
  54. package/src/handleFriendRequest.js +46 -0
  55. package/src/handleMessageRequest.js +49 -0
  56. package/src/httpGet.js +49 -0
  57. package/src/httpPost.js +48 -0
  58. package/src/httpPostFormData.js +41 -0
  59. package/src/listenMqtt.js +678 -0
  60. package/src/logout.js +68 -0
  61. package/src/markAsDelivered.js +48 -0
  62. package/src/markAsRead.js +70 -0
  63. package/src/markAsReadAll.js +43 -0
  64. package/src/markAsSeen.js +51 -0
  65. package/src/muteThread.js +47 -0
  66. package/src/removeUserFromGroup.js +49 -0
  67. package/src/resolvePhotoUrl.js +37 -0
  68. package/src/searchForThread.js +43 -0
  69. package/src/sendMessage.js +334 -0
  70. package/src/sendTypingIndicator.js +80 -0
  71. package/src/setMessageReaction.js +109 -0
  72. package/src/setPostReaction.js +102 -0
  73. package/src/setTitle.js +74 -0
  74. package/src/threadColors.js +39 -0
  75. package/src/unfriend.js +43 -0
  76. package/src/unsendMessage.js +40 -0
  77. package/test/data/shareAttach.js +146 -0
  78. package/test/data/something.mov +0 -0
  79. package/test/data/test.png +0 -0
  80. package/test/data/test.txt +7 -0
  81. package/test/example-config.json +18 -0
  82. package/test/test-page.js +140 -0
  83. package/test/test.js +385 -0
  84. package/test/testv2.js +3 -0
  85. package/utils.js +1288 -0
@@ -0,0 +1,398 @@
1
+ /* eslint-disable linebreak-style */
2
+ /* eslint-disable no-global-assign */
3
+
4
+ /**
5
+ *
6
+ */
7
+
8
+ // Require Database
9
+ var Database = require("better-sqlite3");
10
+ var db;
11
+ // Create Database Under Conditions
12
+ if (!db) db = new Database(__dirname + "/Databasethread.sqlite");
13
+
14
+
15
+ var { fetch,set,add,subtract,push,deleteDB,has,all,type,clear } = require("./methods");
16
+
17
+ // Declare Methods
18
+ var methods = {
19
+ fetch: fetch,
20
+ set: set,
21
+ add: add,
22
+ subtract: subtract,
23
+ push: push,
24
+ delete: deleteDB,
25
+ has: has,
26
+ all: all,
27
+ type: type,
28
+ clear: clear
29
+ };
30
+
31
+ module.exports = {
32
+ /**
33
+ * Package version. Community requested feature.
34
+ * console.log(require('quick.db').version);
35
+ */
36
+ version: "1.1.1",
37
+
38
+ /**
39
+ * This function fetches data from a key in the database. (alias: .get())
40
+ * @param {key} input any string as a key. Also allows for dot notation following the key.
41
+ * @param {options} [input={ target: null }] Any options to be added to the request.
42
+ * @returns {data} the data requested.
43
+ */
44
+
45
+ fetch: function (key, ops) {
46
+ if (!key)
47
+ throw new TypeError(
48
+ "No key specified."
49
+ );
50
+ return arbitrate("fetch", { id: key, ops: ops || {} });
51
+ },
52
+ get: function (key, ops) {
53
+ if (!key)
54
+ throw new TypeError(
55
+ "No key specified."
56
+ );
57
+ return arbitrate("fetch", { id: key, ops: ops || {} });
58
+ },
59
+
60
+ /**
61
+ * This function sets new data based on a key in the database.
62
+ * @param {key} input any string as a key. Also allows for dot notation following the key.
63
+ * @param {options} [input={ target: null }] Any options to be added to the request.
64
+ * @returns {data} the updated data.
65
+ */
66
+
67
+ set: function (key, value, ops) {
68
+ if (!key)
69
+ throw new TypeError(
70
+ "No key specified."
71
+ );
72
+ if (value === undefined)
73
+ throw new TypeError(
74
+ "No value specified."
75
+ );
76
+ return arbitrate("set", {
77
+ stringify: false,
78
+ id: key,
79
+ data: value,
80
+ ops: ops || {},
81
+ });
82
+ },
83
+
84
+ /**
85
+ * This function adds a number to a key in the database. (If no existing number, it will add to 0)
86
+ * @param {key} input any string as a key. Also allows for dot notation following the key.
87
+ * @param {options} [input={ target: null }] Any options to be added to the request.
88
+ * @returns {data} the updated data.
89
+ */
90
+
91
+ add: function (key, value, ops) {
92
+ if (!key)
93
+ throw new TypeError(
94
+ "No key specified."
95
+ );
96
+ if (isNaN(value))
97
+ throw new TypeError(
98
+ "Must specify value to add."
99
+ );
100
+ return arbitrate("add", { id: key, data: value, ops: ops || {} });
101
+ },
102
+
103
+ /**
104
+ * This function subtracts a number to a key in the database. (If no existing number, it will subtract from 0)
105
+ * @param {key} input any string as a key. Also allows for dot notation following the key.
106
+ * @param {options} [input={ target: null }] Any options to be added to the request.
107
+ * @returns {data} the updated data.
108
+ */
109
+
110
+ subtract: function (key, value, ops) {
111
+ if (!key)
112
+ throw new TypeError(
113
+ "No key specified."
114
+ );
115
+ if (isNaN(value))
116
+ throw new TypeError(
117
+ "Must specify value to add."
118
+ );
119
+ return arbitrate("subtract", { id: key, data: value, ops: ops || {} });
120
+ },
121
+
122
+ /**
123
+ * This function will push into an array in the database based on the key. (If no existing array, it will create one)
124
+ * @param {key} input any string as a key. Also allows for dot notation following the key.
125
+ * @param {options} [input={ target: null }] Any options to be added to the request.
126
+ * @returns {data} the updated data.
127
+ */
128
+
129
+ push: function (key, value, ops) {
130
+ if (!key)
131
+ throw new TypeError(
132
+ "No key specified."
133
+ );
134
+ if (!value && value != 0)
135
+ throw new TypeError(
136
+ "Must specify value to push."
137
+ );
138
+ return arbitrate("push", {
139
+ stringify: true,
140
+ id: key,
141
+ data: value,
142
+ ops: ops || {},
143
+ });
144
+ },
145
+
146
+ /**
147
+
148
+ */
149
+
150
+ /**
151
+ * This function will delete an object (or property) in the database.
152
+ * @param {key} input any string as a key. Also allows for dot notation following the key, this will delete the prop in the object.
153
+ * @param {options} [input={ target: null }] Any options to be added to the request.
154
+ * @returns {boolean} if it was a success or not.
155
+ */
156
+
157
+ delete: function (key, ops) {
158
+ if (!key)
159
+ throw new TypeError(
160
+ "No key specified."
161
+ );
162
+ return arbitrate("delete", { id: key, ops: ops || {} });
163
+ },
164
+
165
+ /**
166
+ * This function returns a boolean indicating whether an element with the specified key exists or not.
167
+ * @param {key} input any string as a key. Also allows for dot notation following the key, this will return if the prop exists or not.
168
+ * @param {options} [input={ target: null }] Any options to be added to the request.
169
+ * @returns {boolean} if it exists.
170
+ */
171
+
172
+ has: function (key, ops) {
173
+ if (!key)
174
+ throw new TypeError(
175
+ "No key specified."
176
+ );
177
+ return arbitrate("has", { id: key, ops: ops || {} });
178
+ },
179
+
180
+ includes: function (key, ops) {
181
+ if (!key)
182
+ throw new TypeError(
183
+ "No key specified."
184
+ );
185
+ return arbitrate("has", { id: key, ops: ops || {} });
186
+ },
187
+
188
+ /**
189
+ * This function fetches the entire active table
190
+ * @param {options} [input={ target: null }] Any options to be added to the request.
191
+ * @returns {boolean} if it exists.
192
+ */
193
+
194
+ all: function (ops) {
195
+ return arbitrate("all", { ops: ops || {} });
196
+ },
197
+
198
+ fetchAll: function (ops) {
199
+ return arbitrate("all", { ops: ops || {} });
200
+ },
201
+
202
+ /*
203
+ * Used to get the type of the value.
204
+ */
205
+
206
+ type: function (key, ops) {
207
+ if (!key)
208
+ throw new TypeError(
209
+ "No key specified."
210
+ );
211
+ return arbitrate("type", { id: key, ops: ops || {} });
212
+ },
213
+
214
+ /**
215
+ * Using 'new' on this function creates a new instance of a table.
216
+ * @param {name} input any string as the name of the table.
217
+ * @param {options} options.
218
+ */
219
+
220
+ table: function (tableName, options = {}) {
221
+ // Set Name
222
+ if (typeof tableName !== "string")
223
+ throw new TypeError(
224
+ "Table name has to be a string."
225
+ );
226
+ else if (tableName.includes(" "))
227
+ throw new TypeError(
228
+ "Table name cannot include spaces."
229
+ );
230
+ this.tableName = tableName;
231
+
232
+ // Methods
233
+ this.fetch = function (key, ops) {
234
+ if (!key)
235
+ throw new TypeError(
236
+ "No key specified."
237
+ );
238
+ return arbitrate(
239
+ "fetch",
240
+ { id: key, ops: ops || {} },
241
+ this.tableName
242
+ );
243
+ };
244
+
245
+ this.get = function (key, ops) {
246
+ if (!key)
247
+ throw new TypeError(
248
+ "No key specified."
249
+ );
250
+ return arbitrate(
251
+ "fetch",
252
+ { id: key, ops: ops || {} },
253
+ this.tableName
254
+ );
255
+ };
256
+
257
+ this.set = function (key, value, ops) {
258
+ if (!key)
259
+ throw new TypeError(
260
+ "No key specified."
261
+ );
262
+ if (!value && value != 0)
263
+ throw new TypeError(
264
+ "No value specified."
265
+ );
266
+ return arbitrate(
267
+ "set",
268
+ { stringify: true, id: key, data: value, ops: ops || {} },
269
+ this.tableName
270
+ );
271
+ };
272
+
273
+ this.add = function (key, value, ops) {
274
+ if (!key)
275
+ throw new TypeError(
276
+ "No key specified."
277
+ );
278
+ if (isNaN(value))
279
+ throw new TypeError(
280
+ "Must specify value to add."
281
+ );
282
+ return arbitrate(
283
+ "add",
284
+ { id: key, data: value, ops: ops || {} },
285
+ this.tableName
286
+ );
287
+ };
288
+
289
+ this.subtract = function (key, value, ops) {
290
+ if (!key)
291
+ throw new TypeError(
292
+ "No key specified."
293
+ );
294
+ if (isNaN(value))
295
+ throw new TypeError(
296
+ "Must specify value to add."
297
+ );
298
+ return arbitrate(
299
+ "subtract",
300
+ { id: key, data: value, ops: ops || {} },
301
+ this.tableName
302
+ );
303
+ };
304
+
305
+ this.push = function (key, value, ops) {
306
+ if (!key)
307
+ throw new TypeError(
308
+ "No key specified."
309
+ );
310
+ if (!value && value != 0)
311
+ throw new TypeError(
312
+ "Must specify value to push."
313
+ );
314
+ return arbitrate(
315
+ "push",
316
+ { stringify: true, id: key, data: value, ops: ops || {} },
317
+ this.tableName
318
+ );
319
+ };
320
+
321
+ this.delete = function (key, ops) {
322
+ if (!key)
323
+ throw new TypeError(
324
+ "No key specified."
325
+ );
326
+ return arbitrate(
327
+ "delete",
328
+ { id: key, ops: ops || {} },
329
+ this.tableName
330
+ );
331
+ };
332
+
333
+ this.has = function (key, ops) {
334
+ if (!key)
335
+ throw new TypeError(
336
+ "No key specified."
337
+ );
338
+ return arbitrate(
339
+ "has",
340
+ { id: key, ops: ops || {} },
341
+ this.tableName
342
+ );
343
+ };
344
+
345
+ this.includes = function (key, ops) {
346
+ if (!key)
347
+ throw new TypeError(
348
+ "No key specified."
349
+ );
350
+ return arbitrate(
351
+ "has",
352
+ { id: key, ops: ops || {} },
353
+ this.tableName
354
+ );
355
+ };
356
+
357
+ this.fetchAll = function (ops) {
358
+ return arbitrate("all", { ops: ops || {} }, this.tableName);
359
+ };
360
+
361
+ this.all = function (ops) {
362
+ return arbitrate("all", { ops: ops || {} }, this.tableName);
363
+ };
364
+ },
365
+ };
366
+
367
+ function arbitrate(method, params, tableName) {
368
+ // Configure Options
369
+ let options = {table: tableName || params.ops.table || "json",};
370
+
371
+ // Access Database
372
+ db.prepare(`CREATE TABLE IF NOT EXISTS ${options.table} (ID TEXT, json TEXT)`).run();
373
+
374
+ // Verify Options
375
+ if (params.ops.target && params.ops.target[0] === ".") params.ops.target = params.ops.target.slice(1); // Remove prefix if necessary
376
+ if (params.data && params.data === Infinity) throw new TypeError(`You cannot set Infinity into the database @ ID: ${params.id}`);
377
+
378
+ // Stringify
379
+ // if (params.stringify) {
380
+ // try {
381
+ // params.data = JSON.stringify(params.data);
382
+ // } catch (e) {
383
+ // throw new TypeError(
384
+ // `Please supply a valid input @ ID: ${params.id}\nError: ${e.message}`
385
+ // );
386
+ // }
387
+ // }
388
+
389
+ // Translate dot notation from keys
390
+ if (params.id && typeof params.id == "string" && params.id.includes(".")) {
391
+ let unparsed = params.id.split(".");
392
+ params.id = unparsed.shift();
393
+ params.ops.target = unparsed.join(".");
394
+ }
395
+
396
+ // Run & Return Method
397
+ return methods[method](db, params, options);
398
+ }
@@ -0,0 +1,286 @@
1
+ /* eslint-disable linebreak-style */
2
+
3
+ function addDB(db, params, options) {
4
+ const get = require('lodash/get');
5
+ const set = require('lodash/set');
6
+
7
+ // Fetch entry
8
+ let fetched = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id);
9
+
10
+ // If not found, create empty row
11
+ if (!fetched) {
12
+ db.prepare(`INSERT INTO ${options.table} (ID,json) VALUES (?,?)`).run(params.id, '{}');
13
+ fetched = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id);
14
+ }
15
+
16
+ // Check if a target was supplied
17
+ if (params.ops.target) {
18
+ fetched = JSON.parse(fetched.json);
19
+ try { fetched = JSON.parse(fetched) } catch (e) {}
20
+ params.data = JSON.parse(params.data);
21
+ let oldValue = get(fetched, params.ops.target);
22
+ if (oldValue === undefined) oldValue = 0;
23
+ else if (isNaN(oldValue)) throw new Error(`Data @ ID: "${params.id}" IS NOT A number.\nFOUND: ${fetched}\nEXPECTED: number`);
24
+ params.data = set(fetched, params.ops.target, oldValue + params.data);
25
+ } else {
26
+ if (fetched.json === '{}') fetched.json = 0;
27
+ else fetched.json = JSON.parse(fetched.json)
28
+ try { fetched.json = JSON.parse(fetched) } catch (e) {}
29
+ if (isNaN(fetched.json)) throw new Error(`Data @ ID: "${params.id}" IS NOT A number.\nFOUND: ${fetched.json}\nEXPECTED: number`);
30
+ params.data = parseInt(fetched.json, 10) + parseInt(params.data, 10);
31
+ }
32
+ // Should do the trick!
33
+ // Stringify data
34
+ params.data = JSON.stringify(params.data);
35
+
36
+ // Update entry with new data
37
+ db.prepare(`UPDATE ${options.table} SET json = (?) WHERE ID = (?)`).run(params.data, params.id);
38
+
39
+ // Fetch & return new data
40
+ let newData = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id).json;
41
+ if (newData === '{}') return null;
42
+ else {
43
+ newData = JSON.parse(newData)
44
+ try { newData = JSON.parse(newData); } catch (e) {}
45
+ return newData;
46
+ }
47
+ }
48
+
49
+ function allDB(db, params, options) {
50
+
51
+ // Fetch Entry
52
+ var stmt = db.prepare(`SELECT * FROM ${options.table} WHERE ID IS NOT NULL`);
53
+ let resp = [];
54
+ for (var row of stmt.iterate()) {
55
+ try {
56
+ resp.push({
57
+ ID: row.ID,
58
+ data: JSON.parse(row.json)
59
+ });
60
+ } catch (e) {
61
+ return [];
62
+ }
63
+ }
64
+
65
+ return resp;
66
+ }
67
+
68
+ function clearDB(db, params, options) {
69
+
70
+ // Delete all Rows
71
+ let fetched = db.prepare(`DELETE FROM ${options.table}`).run();
72
+ if(!fetched) return null;
73
+
74
+ // Return Amount of Rows Deleted
75
+ return fetched.changes;
76
+
77
+ }
78
+
79
+ function deleteDB(db, params, options) {
80
+ const unset = require('lodash/unset');
81
+ // Fetch Entry
82
+ let fetched = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id);
83
+ if (!fetched) return false; // If empty, return null
84
+ else fetched = JSON.parse(fetched.json);
85
+ try { fetched = JSON.parse(fetched); } catch (e) {}
86
+
87
+ // Check if the user wants to delete a prop inside an object
88
+ if (typeof fetched === 'object' && params.ops.target) {
89
+ unset(fetched, params.ops.target);
90
+ fetched = JSON.stringify(fetched);
91
+ db.prepare(`UPDATE ${options.table} SET json = (?) WHERE ID = (?)`).run(fetched, params.id);
92
+ return true;
93
+ }
94
+ else if (params.ops.target) throw new TypeError('Target is not an object.');
95
+ else db.prepare(`DELETE FROM ${options.table} WHERE ID = (?)`).run(params.id);
96
+
97
+ // Resolve
98
+ return true;
99
+ }
100
+
101
+ function fetchDB(db, params, options) {
102
+ const get = require('lodash/get');
103
+ // Fetch Entry
104
+ let fetched = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id);
105
+ if (!fetched) return null; // If empty, return null
106
+ fetched = JSON.parse(fetched.json)
107
+ try { fetched = JSON.parse(fetched) } catch (e) {}
108
+
109
+ // Check if target was supplied
110
+ if (params.ops.target) fetched = get(fetched, params.ops.target); // Get prop using dot notation
111
+
112
+ // Return data
113
+ return fetched;
114
+ }
115
+
116
+ function hasDB(db, params, options) {
117
+ const get = require('lodash/get');
118
+ // Fetch Entry
119
+ let fetched = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id);
120
+ if (!fetched) return false; // If empty, return false
121
+ else fetched = JSON.parse(fetched.json);
122
+ try { fetched = JSON.parse(fetched) } catch (e) {}
123
+
124
+ // Check if target was supplied
125
+ if (params.ops.target) fetched = get(fetched, params.ops.target); // Get prop using dot notation
126
+
127
+ // Return boolean
128
+ return (typeof fetched != 'undefined');
129
+ } // Papa bless, you here? I think we need update, push wasn't working.
130
+
131
+ function pushDB(db, params, options) {
132
+ const get = require('lodash/get');
133
+ const set = require('lodash/set');
134
+ // Fetch entry
135
+ let fetched = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id);
136
+
137
+ // If not found, create empty row
138
+ if (!fetched) {
139
+ db.prepare(`INSERT INTO ${options.table} (ID,json) VALUES (?,?)`).run(params.id, '{}');
140
+ fetched = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id);
141
+ }
142
+
143
+ // Check if a target was supplied
144
+ if (params.ops.target) {
145
+ fetched = JSON.parse(fetched.json);
146
+ try { fetched = JSON.parse(fetched) } catch (e) {}
147
+ params.data = JSON.parse(params.data);
148
+ if (typeof fetched !== 'object') throw new TypeError('Cannot push into a non-object.');
149
+ let oldArray = get(fetched, params.ops.target);
150
+ if (oldArray === undefined) oldArray = [];
151
+ else if (!Array.isArray(oldArray)) throw new TypeError('Target is not an array.');
152
+ oldArray.push(params.data);
153
+ params.data = set(fetched, params.ops.target, oldArray);
154
+ } else {
155
+ if (fetched.json === '{}') fetched.json = [];
156
+ else fetched.json = JSON.parse(fetched.json);
157
+ try { fetched.json = JSON.parse(fetched.json); } catch (e) {}
158
+ params.data = JSON.parse(params.data);
159
+ if (!Array.isArray(fetched.json)) throw new TypeError('Target is not an array.');
160
+ fetched.json.push(params.data);
161
+ params.data = fetched.json;
162
+ }
163
+
164
+ // Stringify data
165
+ params.data = JSON.stringify(params.data);
166
+
167
+ // Update entry with new data
168
+ db.prepare(`UPDATE ${options.table} SET json = (?) WHERE ID = (?)`).run(params.data, params.id);
169
+
170
+ // Fetch & return new data
171
+ let newData = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id).json;
172
+ if (newData === '{}') return null;
173
+ else {
174
+ newData = JSON.parse(newData)
175
+ try { newData = JSON.parse(newData) } catch (e) {}
176
+ return newData
177
+ }
178
+ }
179
+
180
+ function setDB(db, params, options) {
181
+ const set = require('lodash/set');
182
+ // Fetch entry
183
+ let fetched = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id);
184
+ // If not found, create empty row
185
+ if (!fetched) {
186
+ db.prepare(`INSERT INTO ${options.table} (ID,json) VALUES (?,?)`).run(params.id, '{}');
187
+ fetched = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id);
188
+ }
189
+
190
+ // Parse fetched
191
+
192
+ try { fetched = JSON.parse(fetched); console.log(fetched) } catch (e) {}
193
+
194
+ // Check if a target was supplied
195
+ if (typeof fetched === 'object' && params.ops.target) {
196
+ params.data = JSON.parse(params.data);
197
+ params.data = set(fetched, params.ops.target, params.data);
198
+ } else if (params.ops.target) throw new TypeError('Cannot target a non-object.');
199
+
200
+ // Stringify data
201
+ params.data = JSON.stringify(params.data);
202
+
203
+ // Update entry with new data
204
+ db.prepare(`UPDATE ${options.table} SET json = (?) WHERE ID = (?)`).run(params.data, params.id);
205
+
206
+ // Fetch & return new data
207
+ let newData = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id).json;
208
+ if (newData === '{}') return null;
209
+ else {
210
+ try { newData = JSON.parse(newData); } catch (e) {
211
+ console.log(e);
212
+ }
213
+ return newData;
214
+ }
215
+ }
216
+
217
+ function subtractDB(db, params, options) {
218
+ const get = require('lodash/get');
219
+ const set = require('lodash/set');
220
+ // Fetch entry
221
+ let fetched = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id);
222
+
223
+ // If not found, create empty row
224
+ if (!fetched) {
225
+ db.prepare(`INSERT INTO ${options.table} (ID,json) VALUES (?,?)`).run(params.id, '{}');
226
+ fetched = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id);
227
+ }
228
+
229
+ // Check if a target was supplied
230
+ if (params.ops.target) {
231
+ try { fetched = JSON.parse(fetched); } catch (e) {}
232
+ params.data = JSON.parse(params.data);
233
+ let oldValue = get(fetched, params.ops.target);
234
+ if (oldValue === undefined) oldValue = 0;
235
+ else if (isNaN(oldValue)) throw new Error('Target is not a number.');
236
+ params.data = set(fetched, params.ops.target, oldValue - params.data);
237
+ } else {
238
+ if (fetched.json === '{}') fetched.json = 0;
239
+ else fetched.json = JSON.parse(fetched.json);
240
+ try { fetched.json = JSON.parse(fetched); } catch (e) {}
241
+ if (isNaN(fetched.json)) throw new Error('Target is not a number.');
242
+ params.data = parseInt(fetched.json, 10) - parseInt(params.data, 10);
243
+ }
244
+
245
+ // Stringify data
246
+ params.data = JSON.stringify(params.data);
247
+
248
+ // Update entry with new data
249
+ db.prepare(`UPDATE ${options.table} SET json = (?) WHERE ID = (?)`).run(params.data, params.id);
250
+
251
+ // Fetch & return new data
252
+ let newData = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id).json;
253
+ if (newData === '{}') return null;
254
+ else {
255
+ try { newData = JSON.parse(newData); } catch (e) {}
256
+ return newData;
257
+ }
258
+ }
259
+
260
+ function typeDB(db, params, options) {
261
+ const get = require('lodash/get');
262
+ // Fetch Entry
263
+ let fetched = db.prepare(`SELECT * FROM ${options.table} WHERE ID = (?)`).get(params.id);
264
+ if (!fetched) return null; // If empty, return null
265
+ fetched = JSON.parse(fetched.json);
266
+ try { fetched = JSON.parse(fetched); } catch (e) {}
267
+
268
+ // Check if target was supplied
269
+ if (params.ops.target) fetched = get(fetched, params.ops.target); // Get prop using dot notation
270
+
271
+ // Return data
272
+ return typeof fetched;
273
+ }
274
+
275
+ module.exports = {
276
+ add:addDB,
277
+ all:allDB,
278
+ clear:clearDB,
279
+ deleteDB:deleteDB,
280
+ fetch:fetchDB,
281
+ has:hasDB,
282
+ push:pushDB,
283
+ set:setDB,
284
+ subtract:subtractDB,
285
+ type:typeDB
286
+ };
@@ -0,0 +1,23 @@
1
+ const db = require('../index');
2
+
3
+ // Setting an object in the database:
4
+ db.set('userInfo', { difficulty: 'Easy' })
5
+ // -> { difficulty: 'Easy' }
6
+
7
+ // Pushing an element to an array (that doesn't exist yet) in an object:
8
+ db.push('userInfo.items', 'Sword')
9
+ // -> { difficulty: 'Easy', items: ['Sword'] }
10
+
11
+ // Adding to a number (that doesn't exist yet) in an object:
12
+ db.add('userInfo.balance', 500)
13
+ // -> { difficulty: 'Easy', items: ['Sword'], balance: 500 }
14
+
15
+ // Repeating previous examples:
16
+ db.push('userInfo.items', 'Watch')
17
+ // -> { difficulty: 'Easy', items: ['Sword', 'Watch'], balance: 500 }
18
+ db.add('userInfo.balance', 500)
19
+ // -> { difficulty: 'Easy', items: ['Sword', 'Watch'], balance: 1000 }
20
+
21
+ // Fetching individual properties
22
+ console.log(db.get('userInfo.balance')) // -> 1000
23
+ console.log(db.get('userInfo.items')) // ['Sword', 'Watch']