Created
March 23, 2017 21:46
-
-
Save chaodoze/00c0b0dca4e591c192c09b9e78ff76a2 to your computer and use it in GitHub Desktop.
beginnings of a FireBase ORM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ = 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