Skip to content

Commit ab98074

Browse files
committed
New Exists Method
1 parent bf4bde8 commit ab98074

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ await db.set('my_table', 'foo', 'bar');
107107
await db.get('my_table', 'foo');
108108
// -> Gets foo key name value (which is bar) in the table 'my_table'
109109
```
110+
- exists (table, key)
111+
```js
112+
await db.exists('my_table', 'foo');
113+
// -> Checks if a specific data exists
114+
```
110115
- base_set (table, key, value)
111116
```js
112117
await db.base_set('my_table', 'foo', 'bar');

examples/example.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ async function run(){
5656
data = await db.get("my_table", "tariq");
5757
console.log(data); // { age: 14 }
5858

59+
data = await db.exists("my_table", "tq"); // checks if a specific data exists
60+
console.log(data); // false
61+
5962
await db.base_set("my_table", "foo", "bar"); // stores 'bar' in 'foo' key name in the table 'my_table' but base encrypted
6063

6164
data = await db.base_get("my_table", "foo"); // gets foo key name value in the table 'my_table' for encrypted rows using base_set method

structures/classes/MySQL.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ module.exports = class MySQL extends EventEmitter{
3232
base_set = require('../methods/base_set');
3333
base_get = require('../methods/base_get');
3434
end = require('../methods/end');
35+
exists = require('../methods/exists');
3536
}

structures/methods/exists.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
const errors = require('../errors/strings.js');
4+
5+
module.exports = async function(table, key){
6+
if(!table || typeof table !== "string") throw new TypeError(errors.table.replace("{received}", typeof table));
7+
if(!key || typeof key !== "string") throw new TypeError(errors.key.replace("{received}", typeof key));
8+
9+
let tables = await this.tables();
10+
if(!tables.includes(table)) return false;
11+
let data = await this.get(table, key);
12+
return (data != null) ? true : false;
13+
}

0 commit comments

Comments
 (0)