taky_resourceful_mongo

resourceful engine using mongojs

npm install taky_resourceful_mongo
4 downloads in the last week
8 downloads in the last month

example

resourceful = require 'taky_resourceful_mongo'

for f in fs.readdirSync __dirname + '/../resource'
  require __dirname + '/../resource/' + f if f.match 'coffee'

for res of Resources
  do (res) ->
    resource = new Resources[res]

    resource.mid = (req,res,next) ->
      app.locals.singular = inflect.singularize resource.word
      app.locals.plural = inflect.pluralize app.locals.singular

      app.locals.mode = 'new'
      if app.locals.item then app.locals.mode = 'mod'

      next()

    if resource.schema
      resource.res = resourceful.define inflect.singularize resource.word
      resource.res.use 'mongodb', {
        database: 'db'
        collection: inflect.pluralize resource.word 
        uri: mongo_uri
      }
      resource.schema()

  if resource.routes then resource.routes()
namespace Resources:
  class Admin

    word: 'admin'

    schema: ->
      @res.string 'name', required:yes,minLength:4,unique:yes
      @res.string 'password', required:yes,minLength:5
      @res.bool 'active', required:yes
      @res.bool 'admin', required:yes

      @res.timestamps()

      @res.before 'update', (doc,cb) ->
        if doc.password?.length and doc.password.length isnt 32
          doc.password = md5 doc.password
        cb()
      @res.before 'save', (doc,cb) ->
        if doc.password?.length and doc.password.length isnt 32
          doc.password = md5 doc.password
        cb()

    on_post: (b) ->
      body = clone b
      body.active = bool body.active
      body.admin = bool body.admin
      body

    on_render: (b) ->
      body = clone b
      body.password = ''
      body.active = bool body.active
      body.admin = bool body.admin
      body

    routes: ->
      # table
      app.get '/admins', @mid, (req,res) =>
        @res.find {}, (e,r) =>
          data =
            items: r
          res.render 'admin_table.html', data

      # create
      app.get '/admins/new', @mid, (req,res) =>
        res.render 'admin_form.html'

      # create (submit)
      app.post '/admins/new', @mid, (req,res) =>
        item = new @res @on_post req.body
        item.save (e,r) =>
          return res.json ok:no,msg:res_errors(e) if e
          res.json ok:yes

      # modify
      app.get '/admins/mod/:id', @mid, (req,res) =>
        @res.get req.param('id'), (e,r) =>
          r = @on_render r
          data =
            modify: yes
            item: r
          res.render 'admin_form.html', data

      # modify (submit)
      app.post '/admins/mod/:id', @mid, (req,res) =>
        @res.find {_id:req.param('id')}
        @res.get req.param('id'), (e,r) =>
          return res.json ok:no,msg:["Item not found"] if e or !r

          update = @on_post req.body

          for x,y of update
            if update[x] is '' then delete update[x]

          log update

          @res.update r.id, update, (e2,r2) =>
            if e2?.validate?.errors?
              return res.json ok:no,msg:res_errors(e2.validate.errors)
            res.json ok:yes

      # destroy
      app.post '/admins/del', @mid, (req,res) =>
        @res.destroy req.body.id, (e,r) ->
          res.json ok:yes
npm loves you