react-native-firebase/lib/modules/database/index.js

67 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-03-02 11:40:08 +00:00
/**
* @flow
* Database representation wrapper
*/
import { NativeModules } from 'react-native';
2017-03-02 11:40:08 +00:00
import Reference from './reference';
import TransactionHandler from './transaction';
import ModuleBase from './../../utils/ModuleBase';
2017-03-02 11:40:08 +00:00
/**
* @class Database
*/
export default class Database extends ModuleBase {
static _NAMESPACE = 'database';
static _NATIVE_MODULE = 'RNFirebaseDatabase';
constructor(firebaseApp: Object, options: Object = {}) {
super(firebaseApp, options, true);
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
// todo serverTimeOffset event/listener - make ref natively and switch to events
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() {
this._native.goOnline();
2017-03-02 11:40:08 +00:00
}
/**
*
*/
goOffline() {
this._native.goOffline();
}
2017-03-02 11:40:08 +00:00
/**
* Returns a new firebase reference instance
* @param path
* @returns {Reference}
2017-03-02 11:40:08 +00:00
*/
ref(path: string) {
return new Reference(this, path);
2017-03-02 11:40:08 +00:00
}
}
export const statics = {
ServerValue: NativeModules.RNFirebaseDatabase ? {
TIMESTAMP: NativeModules.RNFirebaseDatabase.serverValueTimestamp || { '.sv': 'timestamp' },
} : {},
};