-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
144 lines (127 loc) · 4.61 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const AbstractDriver = require("database-js-sqlparser");
const Common = require('database-js-common');
var firebase = require('firebase');
var m_root = Symbol('root');
var m_credentials = Symbol('credentials');
var m_authenticated = Symbol('authenticated');
var m_authenticator = Symbol('authenticator');
class Firebase extends AbstractDriver {
/**
* Creates an instance of Firebase.
* @param {string} root
* @param {object} credentials
* @memberof Firebase
*/
constructor(root, credentials) {
super();
var resolveAuth;
this[m_root] = firebase.database().ref(root);
this[m_credentials] = credentials;
this[m_authenticated] = false;
this[m_authenticator] = new Promise((resolve, reject) => {
resolveAuth = resolve;
});
function onAuth(auth) {
if (auth) {
this[m_authenticated] = true;
resolveAuth();
} else {
this[m_authenticated] = false;
firebase.auth().signInWithEmailAndPassword(credentials.email, credentials.password);
}
}
firebase.auth().onAuthStateChanged(onAuth);
}
/**
* Load all rows from a given table. Promise returns each row associated with
* and index value that is string or integer
* @param {string} table The table name to load rows from
* @returns {Promise<{[key:string|number]:any}>}
*/
load(table) {
return new Promise((resolve, reject) => {
this[m_root].child(table).once('value').then((snapshot) => {
resolve(snapshot.val());
}).catch(err => reject(err));;
});
}
/**
* Stores a row into the table
* @param {string} table The name of the destination table
* @param {number|string} index The array index or object key for the table row, null to insert
* @param {any} row The data to store
* @returns {number|string} Then index or object key which was stored
*/
store(table, index, row) {
return new Promise((resolve, reject) => {
if (index) {
this[m_root].child(table).child(index).set(row).then(() => resolve(index)).catch(err => reject(err));
} else {
var ref = this[m_root].child(table).push(row).then(() => resolve(ref.key)).catch(err => reject(err));
}
});
}
/**
* Removes a row from the table
* @param {string} table The name of the table
* @param {number|string} index The array index or object key for the table row
*/
remove(table, index) {
return new Promise((resolve, reject) => {
this[m_root].child(table).child(index).remove().then(() => resolve(index)).catch(err => reject(err));
});
}
/**
* Creates a new table - actually does nothing since Firebase doesn't need the definition created
* @param {string} table The name of the table to create
* @param {Array<{name:string,index:number,type:string,length?:number,pad?:string}>} definition The definition of the table to create
*/
create(table, definition) {
return Promise.resolve(true);
}
/**
* Drops a table, deletes all the data associated with the table
* @param {string} table The name of the table to drop
*/
drop(table) {
return new Promise((resolve, reject) => {
this[m_root].child(table).remove().then(() => resolve(true));
});
}
/**
* Closes the connection, sets Firebase to offline mode.
*
* @returns {Promise<boolean>}
* @memberof Firebase
*/
close() {
firebase.database().goOffline();
return Promise.resolve(true);
}
ready() {
return this[m_authenticator];
}
}
module.exports = {
/**
* Opens the connection using the connection object.
* @param {object} connection
* @returns {Firebase}
*/
open: function(connection) {
let params = connection.Parameters ? Common.parseConnectionParams(connection.Parameters) : {};
let config = {
apiKey: params.apiKey || "",
authDomain: connection.Hostname + ".firebaseapp.com",
databaseURL: "https://" + connection.Hostname + ".firebaseio.com",
projectId: connection.Hostname,
storageBucket: connection.Hostname + ".appspot.com",
messagingSenderId: params.messagingSenderId || ""
}
firebase.initializeApp(config);
return new Firebase(connection.Database, {
email: connection.Username,
password: connection.Password
});
}
};