Home
wael zekri edited this page Feb 27, 2018
·
14 revisions
npm i data-fs -S
exemples
const {DataFs} = require("data-fs")
var file = new DataFs("database.db")
file.insert(1,"hello world")
file.insert(2,"hello world!!!")
file.loop(function(record ,id){
console.log(record,id)
})
// console.log
// {id=1,value:"hello world"}
// {id=1,value:"hello world!!!"}
-
initiate DataFs class
- const {DataFs} = require("./index")
- var file = new DataFs(filepath)
-
insert
- file.insert(id,value)
- id : a unique alphanumeric value
- value : any type supported by Serialization API string ,number ,json object ,map ,set
- file.insert(id,value)
-
delete
- delete(id)
-
update
- update(id,value)
-
get size
- getLength() //return the count of records
-
get record
- getRecord(id) //return the registered value
-
loop through records
-
loop(callback,options)
- callback : function receive the current record
- options : json object to set the start and end point for the loop
- from : start position
- to : end position [ a negative number can be user to count from last element example -1 = to last item -2 = last item -1 ]
example:
file.loop(function(record,id){ console.log(record,id) },{from:2,to:10})
-
-
find record
- find(callback)
-
callback a function accept the current record and work like an array filter . the function return every record match the condition
var results = file.find(function(record){ return record== "hello world" }) // return an array // results equal to ['hello world']
-
- find(callback)
-
dataMap
- dataMap()
a function return an array represent the real state of records ,this function can be used for graphic representation
- fragIndex
- fragIndex()
this function return the fragmentation index of the file this function maybe used for maintenance and to decide when defragmentation must be executed
- defrag
- defrag()
this function is used to defragment the file
- onError
-
onError(callback)
file.onError(function(error){ console.log(error) })