mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-23 20:10:05 +08:00
[android,js][database] transactions support implemented
This commit is contained in:
@@ -17,15 +17,15 @@ const FirebaseDatabase = NativeModules.RNFirebaseDatabase;
|
||||
*/
|
||||
export default class Reference extends ReferenceBase {
|
||||
|
||||
db: FirebaseDatabase;
|
||||
database: FirebaseDatabase;
|
||||
query: Query;
|
||||
|
||||
constructor(db: FirebaseDatabase, path: string, existingModifiers?: Array<string>) {
|
||||
super(db.firebase, path);
|
||||
this.db = db;
|
||||
constructor(database: FirebaseDatabase, path: string, existingModifiers?: Array<string>) {
|
||||
super(database.firebase, path);
|
||||
this.database = database;
|
||||
this.namespace = 'firebase:db:ref';
|
||||
this.query = new Query(this, path, existingModifiers);
|
||||
this.log.debug('Created new Reference', this.db._handle(path, existingModifiers));
|
||||
this.log.debug('Created new Reference', this.database._handle(path, existingModifiers));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,7 +34,7 @@ export default class Reference extends ReferenceBase {
|
||||
* @returns {*}
|
||||
*/
|
||||
keepSynced(bool: boolean) {
|
||||
const path = this._dbPath();
|
||||
const path = this.path;
|
||||
return promisify('keepSynced', FirebaseDatabase)(path, bool);
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ export default class Reference extends ReferenceBase {
|
||||
* @returns {*}
|
||||
*/
|
||||
set(value: any) {
|
||||
const path = this._dbPath();
|
||||
const path = this.path;
|
||||
const _value = this._serializeAnyType(value);
|
||||
return promisify('set', FirebaseDatabase)(path, _value);
|
||||
}
|
||||
@@ -55,7 +55,7 @@ export default class Reference extends ReferenceBase {
|
||||
* @returns {*}
|
||||
*/
|
||||
update(val: Object) {
|
||||
const path = this._dbPath();
|
||||
const path = this.path;
|
||||
const value = this._serializeObject(val);
|
||||
return promisify('update', FirebaseDatabase)(path, value);
|
||||
}
|
||||
@@ -65,7 +65,7 @@ export default class Reference extends ReferenceBase {
|
||||
* @returns {*}
|
||||
*/
|
||||
remove() {
|
||||
return promisify('remove', FirebaseDatabase)(this._dbPath());
|
||||
return promisify('remove', FirebaseDatabase)(this.path);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,15 +76,15 @@ export default class Reference extends ReferenceBase {
|
||||
*/
|
||||
push(value: any, onComplete: Function) {
|
||||
if (value === null || value === undefined) {
|
||||
return new Reference(this.db, `${this.path}/${generatePushID(this.db.serverTimeOffset)}`);
|
||||
return new Reference(this.database, `${this.path}/${generatePushID(this.database.serverTimeOffset)}`);
|
||||
}
|
||||
|
||||
const path = this._dbPath();
|
||||
const path = this.path;
|
||||
const _value = this._serializeAnyType(value);
|
||||
|
||||
return promisify('push', FirebaseDatabase)(path, _value)
|
||||
.then(({ ref }) => {
|
||||
const newRef = new Reference(this.db, ref);
|
||||
const newRef = new Reference(this.database, ref);
|
||||
if (isFunction(onComplete)) return onComplete(null, newRef);
|
||||
return newRef;
|
||||
}).catch((e) => {
|
||||
@@ -104,11 +104,12 @@ export default class Reference extends ReferenceBase {
|
||||
on(eventType: string, successCallback: () => any, failureCallback: () => any) {
|
||||
if (!isFunction(successCallback)) throw new Error('The specified callback must be a function');
|
||||
if (failureCallback && !isFunction(failureCallback)) throw new Error('The specified error callback must be a function');
|
||||
const path = this._dbPath();
|
||||
const path = this.path;
|
||||
const modifiers = this.query.getModifiers();
|
||||
const modifiersString = this.query.getModifiersString();
|
||||
this.log.debug('adding reference.on', path, modifiersString, eventType);
|
||||
return this.db.on(path, modifiersString, modifiers, eventType, successCallback, failureCallback);
|
||||
this.database.on(path, modifiersString, modifiers, eventType, successCallback, failureCallback);
|
||||
return successCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,7 +121,7 @@ export default class Reference extends ReferenceBase {
|
||||
* @returns {Promise.<TResult>}
|
||||
*/
|
||||
once(eventType: string = 'value', successCallback: (snapshot: Object) => void, failureCallback: (error: Error) => void) {
|
||||
const path = this._dbPath();
|
||||
const path = this.path;
|
||||
const modifiers = this.query.getModifiers();
|
||||
const modifiersString = this.query.getModifiersString();
|
||||
return promisify('once', FirebaseDatabase)(path, modifiersString, modifiers, eventType)
|
||||
@@ -130,7 +131,7 @@ export default class Reference extends ReferenceBase {
|
||||
return snapshot;
|
||||
})
|
||||
.catch((error) => {
|
||||
const firebaseError = this.db._toFirebaseError(error);
|
||||
const firebaseError = this.database._toFirebaseError(error);
|
||||
if (isFunction(failureCallback)) return failureCallback(firebaseError);
|
||||
return Promise.reject(firebaseError);
|
||||
});
|
||||
@@ -143,10 +144,40 @@ export default class Reference extends ReferenceBase {
|
||||
* @returns {*}
|
||||
*/
|
||||
off(eventType?: string = '', origCB?: () => any) {
|
||||
const path = this._dbPath();
|
||||
const path = this.path;
|
||||
const modifiersString = this.query.getModifiersString();
|
||||
this.log.debug('ref.off(): ', path, modifiersString, eventType);
|
||||
return this.db.off(path, modifiersString, eventType, origCB);
|
||||
return this.database.off(path, modifiersString, eventType, origCB);
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomically modifies the data at this location.
|
||||
* @url https://firebase.google.com/docs/reference/js/firebase.database.Reference#transaction
|
||||
* @param transactionUpdate
|
||||
* @param onComplete
|
||||
* @param applyLocally
|
||||
*/
|
||||
transaction(transactionUpdate, onComplete?: () => any, applyLocally: boolean = false) {
|
||||
if (!isFunction(transactionUpdate)) return Promise.reject(new Error('Missing transactionUpdate function argument.'));
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const onCompleteWrapper = (error, committed, snapshotData) => {
|
||||
if (error) {
|
||||
if (isFunction(onComplete)) onComplete(error, committed, null);
|
||||
return reject(error);
|
||||
}
|
||||
|
||||
const snapshot = new Snapshot(this, snapshotData);
|
||||
|
||||
if (isFunction(onComplete)) {
|
||||
onComplete(null, committed, snapshot);
|
||||
}
|
||||
|
||||
return resolve({ committed, snapshot });
|
||||
};
|
||||
|
||||
this.database.transaction.add(this, transactionUpdate, onCompleteWrapper, applyLocally);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -193,7 +224,7 @@ export default class Reference extends ReferenceBase {
|
||||
* @returns {Reference}
|
||||
*/
|
||||
orderBy(name: string, key?: string): Reference {
|
||||
const newRef = new Reference(this.db, this.path, this.query.getModifiers());
|
||||
const newRef = new Reference(this.database, this.path, this.query.getModifiers());
|
||||
newRef.query.setOrderBy(name, key);
|
||||
return newRef;
|
||||
}
|
||||
@@ -227,7 +258,7 @@ export default class Reference extends ReferenceBase {
|
||||
* @returns {Reference}
|
||||
*/
|
||||
limit(name: string, limit: number): Reference {
|
||||
const newRef = new Reference(this.db, this.path, this.query.getModifiers());
|
||||
const newRef = new Reference(this.database, this.path, this.query.getModifiers());
|
||||
newRef.query.setLimit(name, limit);
|
||||
return newRef;
|
||||
}
|
||||
@@ -274,7 +305,7 @@ export default class Reference extends ReferenceBase {
|
||||
* @returns {Reference}
|
||||
*/
|
||||
filter(name: string, value: any, key?: string): Reference {
|
||||
const newRef = new Reference(this.db, this.path, this.query.getModifiers());
|
||||
const newRef = new Reference(this.database, this.path, this.query.getModifiers());
|
||||
newRef.query.setFilter(name, value, key);
|
||||
return newRef;
|
||||
}
|
||||
@@ -293,7 +324,7 @@ export default class Reference extends ReferenceBase {
|
||||
* @returns {Reference}
|
||||
*/
|
||||
child(path: string) {
|
||||
return new Reference(this.db, `${this.path}/${path}`);
|
||||
return new Reference(this.database, `${this.path}/${path}`);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -301,7 +332,7 @@ export default class Reference extends ReferenceBase {
|
||||
* @returns {string}
|
||||
*/
|
||||
toString(): string {
|
||||
return this._dbPath();
|
||||
return this.path;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -314,7 +345,7 @@ export default class Reference extends ReferenceBase {
|
||||
*/
|
||||
get parent(): Reference|null {
|
||||
if (this.path === '/') return null;
|
||||
return new Reference(this.db, this.path.substring(0, this.path.lastIndexOf('/')));
|
||||
return new Reference(this.database, this.path.substring(0, this.path.lastIndexOf('/')));
|
||||
}
|
||||
|
||||
|
||||
@@ -323,16 +354,13 @@ export default class Reference extends ReferenceBase {
|
||||
* @returns {Reference}
|
||||
*/
|
||||
get root(): Reference {
|
||||
return new Reference(this.db, '/');
|
||||
return new Reference(this.database, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* INTERNALS
|
||||
*/
|
||||
|
||||
_dbPath(): string {
|
||||
return this.path;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user