2017-03-02 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* Database representation wrapper
|
|
|
|
*/
|
2017-06-30 16:23:32 +00:00
|
|
|
import { NativeModules } from 'react-native';
|
2017-03-02 11:40:08 +00:00
|
|
|
|
2017-03-24 22:53:56 +00:00
|
|
|
import Reference from './reference';
|
|
|
|
import TransactionHandler from './transaction';
|
2017-07-30 06:34:41 +00:00
|
|
|
import ModuleBase from './../../utils/ModuleBase';
|
2017-03-02 11:40:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Database
|
|
|
|
*/
|
2017-06-30 16:23:32 +00:00
|
|
|
export default class Database extends ModuleBase {
|
2017-08-17 16:58:28 +00:00
|
|
|
static _NAMESPACE = 'database';
|
|
|
|
static _NATIVE_MODULE = 'RNFirebaseDatabase';
|
|
|
|
|
2017-06-30 16:23:32 +00:00
|
|
|
constructor(firebaseApp: Object, options: Object = {}) {
|
2017-08-17 16:58:28 +00:00
|
|
|
super(firebaseApp, options, true);
|
2017-07-30 06:34:41 +00:00
|
|
|
this._transactionHandler = new TransactionHandler(this);
|
2017-08-02 09:38:30 +00:00
|
|
|
|
|
|
|
if (this._options.persistence) {
|
|
|
|
this._native.setPersistence(this._options.persistence);
|
|
|
|
}
|
2017-03-02 11:40:08 +00:00
|
|
|
|
2017-07-30 06:34:41 +00:00
|
|
|
// todo serverTimeOffset event/listener - make ref natively and switch to events
|
2017-08-15 20:29:50 +00:00
|
|
|
this._serverTimeOffset = 0; // TODO ----^
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 09:38:30 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return {number}
|
|
|
|
*/
|
|
|
|
getServerTime() {
|
|
|
|
return new Date().getTime() + this._serverTimeOffset;
|
|
|
|
}
|
|
|
|
|
2017-03-02 11:40:08 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
goOnline() {
|
2017-06-30 16:23:32 +00:00
|
|
|
this._native.goOnline();
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2017-07-30 06:34:41 +00:00
|
|
|
goOffline() {
|
|
|
|
this._native.goOffline();
|
2017-03-07 16:54:04 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 11:40:08 +00:00
|
|
|
/**
|
2017-07-30 06:34:41 +00:00
|
|
|
* Returns a new firebase reference instance
|
|
|
|
* @param path
|
|
|
|
* @returns {Reference}
|
2017-03-02 11:40:08 +00:00
|
|
|
*/
|
2017-07-30 06:34:41 +00:00
|
|
|
ref(path: string) {
|
|
|
|
return new Reference(this, path);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-22 20:15:02 +00:00
|
|
|
|
|
|
|
export const statics = {
|
2017-08-25 16:37:52 +00:00
|
|
|
ServerValue: NativeModules.RNFirebaseDatabase ? {
|
|
|
|
TIMESTAMP: NativeModules.RNFirebaseDatabase.serverValueTimestamp || { '.sv': 'timestamp' },
|
2017-05-31 14:45:14 +00:00
|
|
|
} : {},
|
2017-03-22 20:15:02 +00:00
|
|
|
};
|