add get_from_key function
This commit is contained in:
parent
a87ddc44d1
commit
2409e5c430
41
README.md
41
README.md
@ -117,3 +117,44 @@ output...
|
|||||||
```javascript
|
```javascript
|
||||||
[{'ID': 0, 'content': 'content 1'}, {'ID': 1, 'content': 'other content'}, {'ID': 2, 'content': 'Content number 3 :)'}, {'ID': 3, 'content': 'etc.'}]
|
[{'ID': 0, 'content': 'content 1'}, {'ID': 1, 'content': 'other content'}, {'ID': 2, 'content': 'Content number 3 :)'}, {'ID': 3, 'content': 'etc.'}]
|
||||||
```
|
```
|
||||||
|
### getIndex
|
||||||
|
Use this method from getting index value.
|
||||||
|
```javascript
|
||||||
|
const sqlite = require('SQLEasy.js');
|
||||||
|
var database = sqlite.database('/path/to/database.db');
|
||||||
|
|
||||||
|
console.log(database.get('table'));
|
||||||
|
console.log(database.getIndex('table', 'ID'));
|
||||||
|
```
|
||||||
|
output...
|
||||||
|
```javascript
|
||||||
|
[{'ID': 0, 'content': 'content 1'}, {'ID': 1, 'content': 'other content'}, {'ID': 3, 'content': 'Content number 3 :)'}, {'ID': 4, 'content': 'edited'}]
|
||||||
|
2
|
||||||
|
```
|
||||||
|
## Other functions that you can use in this module
|
||||||
|
### get_from_key
|
||||||
|
This function needs from works data in buffer.
|
||||||
|
The main advantage of this method is that you do not need to request data from the database every time, it is enough to use the data uploaded to memory:
|
||||||
|
```javascript
|
||||||
|
// Legacy method!
|
||||||
|
const sqlite = require('SQLEasy.js');
|
||||||
|
var database = sqlite.database('/path/to/database.db');
|
||||||
|
var rolesData = database.get('users').map(i => {
|
||||||
|
return {
|
||||||
|
user: i,
|
||||||
|
role_data: database.get('role', [{id: i.role}])
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
```javascript
|
||||||
|
// New method!
|
||||||
|
const sqlite = require('SQLEasy.js');
|
||||||
|
var database = sqlite.database('/path/to/database.db');
|
||||||
|
var roleData = database.get('role', [{id: i.role}])
|
||||||
|
var rolesData = database.get('users').map(i => {
|
||||||
|
return {
|
||||||
|
user: i,
|
||||||
|
role_data: sqlite.get_from_key(roleData, [{id: i.role}])
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
32
index.js
32
index.js
@ -23,6 +23,35 @@ class SQLEasy_error extends Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function get_from_key (db_data, conditions) {
|
||||||
|
conditions = conditions.filter(i => i != {});
|
||||||
|
if (conditions == [] || !conditions) return db_data;
|
||||||
|
let item = new Object();
|
||||||
|
let bool_conditions = new Array();
|
||||||
|
let condition_item = new Object();
|
||||||
|
let int_condition = 1;
|
||||||
|
|
||||||
|
let out_objs = new Array();
|
||||||
|
|
||||||
|
for (let i in db_data) {
|
||||||
|
bool_conditions = new Array();
|
||||||
|
item = db_data[i];
|
||||||
|
|
||||||
|
for (let index in conditions) {
|
||||||
|
int_condition = 1;
|
||||||
|
condition_item = conditions[index];
|
||||||
|
for (key in condition_item) {
|
||||||
|
int_condition *= Number(item[key] == condition_item[key]);
|
||||||
|
}
|
||||||
|
bool_conditions.push(int_condition);
|
||||||
|
}
|
||||||
|
if (bool_conditions.reduce((a, b) => Boolean(a + b))) out_objs.push(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
return out_objs;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class database {
|
class database {
|
||||||
constructor(path){
|
constructor(path){
|
||||||
this.PATH = path;
|
this.PATH = path;
|
||||||
@ -165,5 +194,6 @@ class database {
|
|||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
database: database
|
database: database,
|
||||||
|
get_from_key: get_from_key
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user