0.2.2 • Published 7 years ago

samjs-mongo v0.2.2

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

samjs-mongo

adds a model and interface to connect with mongoDB.

Client: samjs-mongo-client

Getting Started

npm install --save samjs-mongo
npm install --save-dev samjs-mongo-client

Usage

// server-side
samjs
.plugins(require("samjs-mogno"))
.options()
.configs()
.models({name: "someModel", db: "mongo", schema:{someProp: String}})
.startup(server)


// will be in config mode after startup to setup the mongo connection
// after successsful configuration:
samjs.state.onceStarted.then(function(){
  // not in install mode anymore
})

//client in browser

samjs.plugins(require "samjs-mongo-client")

// when mongoURI isn't setted in config, samjs will go into
// install mode, there you can set it
samjs.install.set("mongoURI", "mongodb://localhost/tableName")
.then(function(){
  // success
})
.catch(function(){
  // failed
})

var someModel = new samjs.Mongo("someModel")
// has insert / count / find / update / remove
someModel.insert(someProp:"someValue")

// in another client events: inserted / updated / removed
someModel.on("inserted", function(id) {
  // has id of inserted item
})

model props

nametypedefaultdescription
schemaobject or factory function-(required) mongoose schema

model hooks

each hook has to return its arguments.

nameargumentsdescription
beforeFind{query, client}will be called before each find and count
afterFindresultswill be called after each find and count
beforeInsert{query, client}will be called before each insert
afterInsertresultswill be called after each insert
beforeUpdate{query, client}will be called before each server-side update
afterUpdateresultswill be called after each server-side update
beforeRemove{query, client}will be called before each server-side remove
afterRemoveresultswill be called after each server-side remove
beforeCreatemodelwill be called before model creation
afterCreatemodelwill be called after model creation

example:

samjs
.plugins(require("samjs-mongo"))
.options()
.configs()
.models({
  name:"someModel",
  db:"mongo",
  schema:{someProp: String},
  beforeGet: [
    function(obj) {
      if (notPermitted){
        throw new Error("no Permission")
      }
      return obj
    }
  ]
})

plugins

plugins are activated on model level

samjs
.plugins(require("samjs-mongo"),require("samjs-mongo-auth"))
.options()
.configs()
.models({
  name:"someModel",
  db:"mongo",
  schema:{
    someProp: String
    },
  plugins: {
    "auth": null // or a options object to interact with the plugin
    }
  }
})
0.2.2

7 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.1

9 years ago

0.1.0

9 years ago