-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
103 lines (80 loc) · 2.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// firestore - Firebase implementation of a simple key/value store - Copyright (C) 2015 by yieme - All Rights Reserved - License: MIT
;(function() {
'use strict';
var _ = require('lodash')
, Firebase = require('firebase')
, FirebaseTokenGenerator = require('firebase-token-generator')
function Firestore(options, callback) { // TODO: co upgrade
if(!_.isFunction(callback)) {
return _.partial(Firestore, options)
}
var firestore = this
options = options || {}
options.child = options.child || 'kv'
options.path = options.path || options.child
if (options.name) options.url = 'https://' + options.name + '.firebaseio.com/'
if (options.url && options.url.substr(-1) != '/') options.url += '/'
var Ref = options.ref || (options.url && new Firebase(options.url)) || false
if (!Ref) {
callback(new Error("Firestore: ref or url required"))
return
}
function complete() {
callback(null, firestore)
}
if (options.path) {
Ref = Ref.child(options.path)
} else {
var path = '/' + Ref.toString().split('.com/')[1]
Ref = Ref.root().child(path) // generate new firebase reference
}
firestore.get = function (key, callback) { // get key value
if(!_.isFunction(callback)) {
return _.partial(Firestore.get, key)
}
var ref = Ref.child(key)
ref.once('value', function (snap) {
var val = snap.val()
callback(null, val)
}, callback);
}
firestore.set = function (key, value, callback) { // set key/value
if(callback && !_.isFunction(callback)) {
return _.partial(Firestore.set, key, value)
}
try {
Ref.child(key).set(value, callback)
} catch (err) {
if (callback) {
callback(err)
} else {
throw err
}
}
}
firestore.del = function (key, callback) { // clear key
if(callback && !_.isFunction(callback)) {
return _.partial(Firestore.del, key)
}
Ref.child(key).remove(callback);
}
if (options.token) {
var token = options.token
if (options.auth) {
var tokenGen = new FirebaseTokenGenerator(options.token)
token = tokenGen.createToken(options.auth)
}
try {
Ref.authWithCustomToken(token, complete)
} catch (err) { callback(err) }
} else {
complete()
}
if (options.autoRemove) {
Ref.onDisconnect().remove()
}
}
if (typeof exports === 'object') module.exports = Firestore // support CommonJS
// else if (typeof define === 'function' && define.amd) define(function() { return Firestore }) // support AMD
// else this.Firestore = Firestore // support browser
})();