Comparing version 1.6.0 to 1.7.0
44
json.js
@@ -24,2 +24,3 @@ // json module | ||
/*#####################################################################################*/ | ||
// insert data to a exists json | ||
@@ -42,4 +43,21 @@ json.insert = (path, db, file, args, index, insert, callback)=>{ | ||
} | ||
// push data to json field array | ||
json.push = (path, db, file, args, index, push, callback)=>{ | ||
fs.readFile(`${path}/${db}/${file}.json`, (err, data)=>{ | ||
if(err) throw err; | ||
json.push = (path, db, file, args, index, insert, callback)=>{ | ||
let json = JSON.parse(data); | ||
// push data to the specific index and propertie | ||
if(args === '' || args === null || args === undefined) json[index].push(push); | ||
if(args !== '' && args !== null && args !== undefined) json[args][index].push(push); | ||
fs.writeFile(`${path}/${db}/${file}.json`, JSON.stringify(json), (err)=>{ | ||
if(err) throw err; | ||
if(callback) callback(); | ||
}); | ||
}); | ||
} | ||
// add a sum value to json field | ||
json.add = (path, db, file, args, index, add, callback)=>{ | ||
fs.readFile(`${path}/${db}/${file}.json`, (err, data)=>{ | ||
@@ -49,5 +67,5 @@ if(err) throw err; | ||
let json = JSON.parse(data); | ||
// insert data to the specific index and propertie | ||
if(args === '' || args === null || args === undefined) json[index].push(insert); | ||
if(args !== '' && args !== null && args !== undefined) json[args][index].push(insert); | ||
// add data to the specific index and propertie | ||
if(args === '' || args === null || args === undefined) json[index] += add; | ||
if(args !== '' && args !== null && args !== undefined) json[args][index] += add; | ||
@@ -61,3 +79,21 @@ fs.writeFile(`${path}/${db}/${file}.json`, JSON.stringify(json), (err)=>{ | ||
} | ||
// revome a value for json field | ||
json.remove = (path, db, file, args, index, remove, callback)=>{ | ||
fs.readFile(`${path}/${db}/${file}.json`, (err, data)=>{ | ||
if(err) throw err; | ||
let json = JSON.parse(data); | ||
// remove data to the specific index and propertie | ||
if(args === '' || args === null || args === undefined) json[index] -= remove; | ||
if(args !== '' && args !== null && args !== undefined) json[args][index] -= remove; | ||
fs.writeFile(`${path}/${db}/${file}.json`, JSON.stringify(json), (err)=>{ | ||
if(err) throw err; | ||
if(callback) callback(); | ||
}); | ||
}); | ||
} | ||
/*#####################################################################################*/ | ||
// parse a JSON to JS obj and get data | ||
@@ -64,0 +100,0 @@ json.getData = (path, db, file, callback)=>{ |
{ | ||
"name": "ldbjs", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "Create and manage local db files and data", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -171,4 +171,4 @@ # ldbjs | ||
ldb.json.createDBFile('.', 'myJSONDBName', 'myJSONFileName', ()=>{ | ||
// optional callback | ||
console.log('json dbfile created'); | ||
// optional callback | ||
console.log('json dbfile created'); | ||
}); | ||
@@ -179,4 +179,4 @@ | ||
let myJSObj = { | ||
name: "Jhon", | ||
age: 27 | ||
name: "Jhon", | ||
age: 27 | ||
}; | ||
@@ -188,3 +188,3 @@ | ||
ldb.json.getData('.', 'myJSONDBName', 'myJSONFileName', (data)=>{ | ||
cosole.log(data); | ||
cosole.log(data); | ||
}); | ||
@@ -203,7 +203,7 @@ ``` | ||
let myJSObj = { | ||
name: "Jhon", | ||
age: 27, | ||
parents: { | ||
bro: "Fred" | ||
} | ||
name: "Jhon", | ||
age: 27, | ||
parents: { | ||
bro: "Fred" | ||
} | ||
}; | ||
@@ -225,3 +225,3 @@ | ||
### JSON push | ||
### JSON push, add and remove | ||
@@ -234,3 +234,3 @@ ```javascript | ||
let obj = { | ||
arr: [] | ||
arr: [] | ||
}; | ||
@@ -241,2 +241,11 @@ | ||
// push 1 to json 'arr' array | ||
ldb.json.add('.', 'myJSONDBName', 'myJSONFileName', null, 'arr', 2); | ||
// add sum 2 to json 'arr' array | ||
// results its like {"arr": [3]} | ||
ldb.json.remove('.', 'myJSONDBName', 'myJSONFileName', null, 'arr', 1); | ||
// remove 1 from original value 3 | ||
// results its like {"arr": [2]} | ||
``` | ||
@@ -243,0 +252,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15787
226
280
7