Skip to content

Instantly share code, notes, and snippets.

@chaodoze
Created March 23, 2017 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaodoze/00c0b0dca4e591c192c09b9e78ff76a2 to your computer and use it in GitHub Desktop.
Save chaodoze/00c0b0dca4e591c192c09b9e78ff76a2 to your computer and use it in GitHub Desktop.
beginnings of a FireBase ORM
_ = require 'lodash'
db = require './db'
Function::property = (prop, desc)->
Object.defineProperty @::, prop, desc
ORM =
findAll: (options={})->
klass = @
classStr = @name
db.getOnce("/#{classStr}", options).then (results)->
_.values(results).map (raw)-> new klass(raw)
belongsTo: (klass)->
classStr = klass.name
fieldStr = _.camelCase(classStr)
@property fieldStr,
get: ->
id = @["#{fieldStr}_id"]
return Promise.resolve(null) unless id?
db.getOnce("/#{classStr}/#{id}").then (raw)->new klass(raw)
extended: ->
module.exports = ORM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment