Skip to content
wael zekri edited this page Feb 27, 2018 · 14 revisions

install

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!!!"}
  1. initiate DataFs class

    • const {DataFs} = require("./index")
    • var file = new DataFs(filepath)
  2. insert

    • file.insert(id,value)
      • id : a unique alphanumeric value
      • value : any type supported by Serialization API string ,number ,json object ,map ,set
  3. delete

    • delete(id)
  4. update

    • update(id,value)
  5. get size

    • getLength() //return the count of records
  6. get record

    • getRecord(id) //return the registered value
  7. 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})
      
  8. 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']
        
  9. dataMap

  • dataMap()

a function return an array represent the real state of records ,this function can be used for graphic representation

  1. 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

  1. defrag
  • defrag()

this function is used to defragment the file

  1. onError
  • onError(callback)

     file.onError(function(error){
       console.log(error)
     })
    
Clone this wiki locally