0.4.0 • Published 7 years ago

samjs-files v0.4.0

Weekly downloads
31
License
-
Repository
github
Last release
7 years ago

samjs-files

Adds a model and interface for file/folder interaction.

Client: samjs-files-client

Getting Started

npm install --save samjs-files

Usage

// server-side
samjs
.plugins(require("samjs-files"))
.options()
.configs()
.models({
  name:"someFile",
  db:"files",
  files:"package.json",
  access: {
    read:true
  }

  },{
  name:"someFolder",
  db:"files",
  folders:"assets/",
  access: {
    read:true
  }
}
})
.startup(server)
// client-side
samjs.plugins(require("samjs-files-client"))

model1 = samjs.getFilesModel("someFile")
model1.get("package.json") // filename can be omitted when model only has one file
.then(function(response){
  // success
})
.catch(function(){
  // failed
})

model1.set("package.json","{'content':'newContent'}")
.then(function(){
  // success
})
.catch(function(){
  // failed
})

model1.on("update",function(){
  // file has changed
})

model2 = samjs.getFilesModel("someFolder")
model2.get("someFileInSomeFolder").then(function(){
  // success
})
model2.get("someFileInSomeFolder").then(function(){
  // success
})

model props

nametypedefaultdescription
cacheBooleanfalseset false when files are large, many or seldom used. Will load the files in memory and activate a fs.watch
optionsObject{encoding: null}will be passed to fs.readFile
accessObject{}use to control access, with, e.g. samjs-files-auth

model hooks

each hook has to return its arguments.

nameargumentsdescription
beforeGet{file, client}will be called before each get
afterGetdatawill be called after each get
beforeSet{data, file, client}will be called before each set
afterSetfilewill be called after each set
before_Set{data, file}will be called before each server-side _set
after_Setfilewill be called after each server-side _set
beforeCreatemodelwill be called before model creation
afterCreatemodelwill be called after model creation

example:

samjs
.plugins(require("samjs-files"))
.options()
.configs()
.models({
  name:"someFile",
  db:"files",
  files:"package.json",
  access: {
    read: true
  },
  beforeGet: [
    function(obj) {
      if (notPermitted){
        throw new Error("no Permission")
      }
      return obj
    }
  ]
})

plugins

plugins are activated on model level

samjs
.plugins(require("samjs-files"),require("samjs-files-auth"))
.options()
.configs()
.models({
  name:"someFile",
  db:"files",
  files:"package.json",
  read:true,
  plugins: {
    "auth": null // or a options object to interact with the plugin
    }
  }
})
0.4.0

7 years ago

0.3.0

7 years ago

0.2.4

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago