Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 2.70 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #Sneak peek on what I am planning to create
  2.  
  3. Base = require './base'
  4. Env = require '/.env'
  5. {extend, capFirst, forEach} = require '/.util'
  6.  
  7. class Model extends Base
  8.        
  9.         @setenv: (env) ->
  10.                 if Env.isAncestorOf env
  11.                         @_env = env
  12.                        
  13.        
  14.         @schema: (schema) ->
  15.                 if schema?
  16.                         @index(['_date_updated'])
  17.                         @_schema = extend {_date_created:Date, _date_updated:Date}, @_schema
  18.                         @_schema = extend @_schema, schema
  19.                
  20.                 @_schema or= {_date_created:Date, _date_updated:Date}
  21.        
  22.         @validator: (validator) ->
  23.                 if validator?
  24.                         @_validator = extend {}, @_validator
  25.                         @_validator = extend @_validator, validator
  26.                
  27.                 @_validator or= {}
  28.        
  29.         @constraints: (constrains) ->
  30.                 if constraints?
  31.                         @_constraints = extend {}, @_constraints
  32.                         @_constraints = extend @_constraints, constraints
  33.                
  34.                 @_constraints or= {}
  35.        
  36.         @index: (idx) ->
  37.                 if idx?
  38.                         @_index = extend {}, @_index
  39.                         @_index[i] = 1 for i in idx
  40.                
  41.                 @_index or= {}
  42.        
  43.         @queries: (queries) ->
  44.                 if typeof queries is 'string'
  45.                         return @_queries[queries]
  46.                
  47.                 @_queries = extend @_queries or {}, queries
  48.                 forEach (name for name of queries), (name) =>
  49.                         @["query#{capFirst name}"] = (obj, opt, callback) ->
  50.                                 @query name, obj, opt, callback
  51.                
  52.                 return @_queries
  53.  
  54.         @access: ->
  55.                
  56.         @migration: (fn) ->
  57.                 if fn
  58.                         @_migration = fn
  59.                 return @_migration
  60.        
  61.         @parseQuery: (query) ->
  62.                 query or= {}
  63.                 JSON.encode JSON.stringify query
  64.        
  65.         @collect: (callback) ->
  66.                 @_env.db?.collection @_modelName, (error, collection) =>
  67.                         if error
  68.                                 throw new Error "Can't retrieve collection '#{@_modelName}'"
  69.                        
  70.                         collection.ensureIndex @_index, ->
  71.                                 callback collection
  72.                
  73.         @find: (args...) ->
  74.                 if args.length >= 3
  75.                         [query, options, callback] = args
  76.                 else if args.length is 2
  77.                         [query, callback] = args
  78.                 else if args.length is 1
  79.                         [callback] = args
  80.                 else
  81.                         return
  82.                
  83.                 query = @parseQuery query
  84.                 options or= {}
  85.                 Self = @
  86.                
  87.                 @collect (collection) ->
  88.                         collection.find query, options, (err, cursor) ->
  89.                                 if err
  90.                                         throw new Error "Can't retrieve documents in '#{Self._modelName}'"
  91.                                 cursor.toArray (err, docs) ->
  92.                                         records = new Self(docs[i]) for i in docs
  93.                                         callback.call Self, records
  94.        
  95.         @findOne: (args...) ->
  96.                 if args.length >= 2
  97.                         [query, callback] = args
  98.                 else if args.length is 1
  99.                         [callback] = args
  100.                 else
  101.                         return
  102.                
  103.                 query = @parseQuery query
  104.                 Self = @
  105.                
  106.                 @collect (collection) ->
  107.                         collection.findOne (err, doc) ->
  108.                                 if err
  109.                                         throw new Error "Can't retrieve document in '#{Self._modelName}'"
  110.                                 callback.call Self, new Self(doc)
  111.        
  112.         @query: (queryName, obj, opt, callback) ->
  113.                 query = extend opt.filter or {}, @_queries[queryName].call(this, obj)
  114.                 @find @query, opt, callback
  115.                
  116.         constructor: ->
  117.        
  118.         validate: ->
  119.                
  120.         save: (callback)->
  121.                
  122.         destroy: (callback)->
  123.        
  124.  
  125. module.exports = Model