Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/app/lib/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ const mapOfDeprecationReplacements: DeprecationMap = {
statics: {
ServerValue: 'ServerValue',
},
ServerValue: {
increment: 'increment()',
serverTimestamp: 'serverTimestamp()',
},
DatabaseReference: {
child: 'child()',
set: 'set()',
Expand Down Expand Up @@ -595,6 +599,10 @@ function getNamespace(target: any): string | undefined {
if (target.constructor.name === 'DatabaseReference') {
return 'database';
}
// Check if target is ServerValue object (has increment method and TIMESTAMP property)
if (target.increment && target.TIMESTAMP && target.TIMESTAMP['.sv'] === 'timestamp') {
return 'database';
}
if (target.GeoPoint || target.CustomProvider) {
// target is statics object. GeoPoint - Firestore, CustomProvider - AppCheck
return 'firestore';
Expand Down Expand Up @@ -627,6 +635,11 @@ function getInstanceName(target: any): string {
return 'default';
}

// Check if target is ServerValue object (has increment method and TIMESTAMP property)
if (target.increment && target.TIMESTAMP && target.TIMESTAMP['.sv'] === 'timestamp') {
return 'ServerValue';
}

if (target.constructor.name === 'StorageReference') {
// if path passed into ref(), it will pass in the arg as target.name
return target.constructor.name;
Expand Down
9 changes: 9 additions & 0 deletions packages/database/__tests__/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ describe('Database', function () {
const db = getDatabase();
databaseV9Deprecation(
() => connectDatabaseEmulator(db, 'localhost', 9000),
// @ts-expect-error Combines modular and namespace API
() => db.useEmulator('localhost', 9000),
'useEmulator',
);
Expand All @@ -366,6 +367,7 @@ describe('Database', function () {
const db = getDatabase();
databaseV9Deprecation(
() => goOffline(db),
// @ts-expect-error Combines modular and namespace API
() => db.goOffline(),
'goOffline',
);
Expand All @@ -375,6 +377,7 @@ describe('Database', function () {
const db = getDatabase();
databaseV9Deprecation(
() => goOnline(db),
// @ts-expect-error Combines modular and namespace API
() => db.goOnline(),
'goOnline',
);
Expand All @@ -384,6 +387,7 @@ describe('Database', function () {
const db = getDatabase();
databaseV9Deprecation(
() => ref(db, 'test'),
// @ts-expect-error Combines modular and namespace API
() => db.ref('test'),
'ref',
);
Expand All @@ -395,6 +399,7 @@ describe('Database', function () {
(db as any)._customUrlOrRegion = 'https://test.firebaseio.com';
databaseV9Deprecation(
() => refFromURL(db, 'https://test.firebaseio.com'),
// @ts-expect-error Combines modular and namespace API
() => db.refFromURL('https://test.firebaseio.com'),
'refFromURL',
);
Expand All @@ -404,6 +409,7 @@ describe('Database', function () {
const db = getDatabase();
databaseV9Deprecation(
() => setPersistenceEnabled(db, true),
// @ts-expect-error Combines modular and namespace API
() => db.setPersistenceEnabled(true),
'setPersistenceEnabled',
);
Expand All @@ -413,6 +419,7 @@ describe('Database', function () {
const db = getDatabase();
databaseV9Deprecation(
() => setLoggingEnabled(db, true),
// @ts-expect-error Combines modular and namespace API
() => db.setLoggingEnabled(true),
'setLoggingEnabled',
);
Expand All @@ -422,6 +429,7 @@ describe('Database', function () {
const db = getDatabase();
databaseV9Deprecation(
() => setPersistenceCacheSizeBytes(db, 10000000),
// @ts-expect-error Combines modular and namespace API
() => db.setPersistenceCacheSizeBytes(10000000),
'setPersistenceCacheSizeBytes',
);
Expand All @@ -431,6 +439,7 @@ describe('Database', function () {
const db = getDatabase();
databaseV9Deprecation(
() => getServerTime(db),
// @ts-expect-error Combines modular and namespace API
() => db.getServerTime(),
'getServerTime',
);
Expand Down
11 changes: 10 additions & 1 deletion packages/database/e2e/issues.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ describe('database issues', function () {
testRef._modifiers.toString().should.be.a.String();
testRef._modifiers.toArray()[0].name.should.equal('orderByChild');

// Seed some data that matches the query to ensure callback fires
const seedRef = firebase.database().ref(`${TEST_PATH}/item1`);
await seedRef.set({ disabled: false, name: 'test' });

testRef.on('value', snapshot => {
callback(snapshot.val());
});
Expand Down Expand Up @@ -180,7 +184,8 @@ describe('database issues', function () {
});

it('#2833 should not mutate modifiers ordering', async function () {
const { getDatabase, ref, child, query, equalTo, orderByChild, onValue } = databaseModular;
const { getDatabase, ref, child, query, equalTo, orderByChild, onValue, set } =
databaseModular;

const callback = sinon.spy();
const testRef = query(
Expand All @@ -192,6 +197,10 @@ describe('database issues', function () {
testRef._modifiers.toString().should.be.a.String();
testRef._modifiers.toArray()[0].name.should.equal('orderByChild');

// Seed some data that matches the query to ensure callback fires
const seedRef = child(ref(getDatabase()), `${TEST_PATH}/item1`);
await set(seedRef, { disabled: false, name: 'test' });

const unsubscribe = onValue(testRef, snapshot => {
callback(snapshot.val());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,34 @@ import {
import { deepGet } from '@react-native-firebase/app/dist/module/common/deeps';

import { MODULAR_DEPRECATION_ARG } from '@react-native-firebase/app/dist/module/common';
import type DatabaseReference from './DatabaseReference';
import type DatabaseQuery from './DatabaseQuery';

interface SnapshotData {
value: unknown;
key: string | null;
exists: boolean;
childKeys: string[];
priority: string | number | null;
childPriorities?: { [key: string]: string | number | null };
}

export default class DatabaseDataSnapshot {
constructor(reference, snapshot) {
_snapshot: SnapshotData;
_ref: DatabaseReference;

constructor(reference: DatabaseReference | DatabaseQuery, snapshot: SnapshotData) {
this._snapshot = snapshot;

if (reference.key !== snapshot.key) {
// reference is a query?
this._ref = reference.ref.child.call(reference.ref, snapshot.key, MODULAR_DEPRECATION_ARG);
this._ref = (reference.ref.child as any).call(
reference.ref,
snapshot.key,
MODULAR_DEPRECATION_ARG,
) as DatabaseReference;
} else {
this._ref = reference;
this._ref = reference as DatabaseReference;
}

// TODO #894
Expand All @@ -43,11 +62,11 @@ export default class DatabaseDataSnapshot {
// }
}

get key() {
get key(): string | null {
return this._snapshot.key;
}

get ref() {
get ref(): DatabaseReference {
return this._ref;
}

Expand All @@ -56,22 +75,26 @@ export default class DatabaseDataSnapshot {
* @param path
* @returns {DatabaseDataSnapshot}
*/
child(path) {
child(path: string): DatabaseDataSnapshot {
if (!isString(path)) {
throw new Error("snapshot().child(*) 'path' must be a string value");
}

let value = deepGet(this._snapshot.value, path);
let value = deepGet(this._snapshot.value as Record<string, unknown> | unknown[], path);

if (value === undefined) {
value = null;
}

const childRef = this._ref.child.call(this._ref, path, MODULAR_DEPRECATION_ARG);
const childRef = (this._ref.child as any).call(
this._ref,
path,
MODULAR_DEPRECATION_ARG,
) as DatabaseReference;

let childPriority = null;
let childPriority: string | number | null = null;
if (this._snapshot.childPriorities) {
const childPriorityValue = this._snapshot.childPriorities[childRef.key];
const childPriorityValue = this._snapshot.childPriorities[childRef.key || ''];
if (isString(childPriorityValue) || isNumber(childPriorityValue)) {
childPriority = childPriorityValue;
}
Expand All @@ -84,15 +107,15 @@ export default class DatabaseDataSnapshot {
childKeys: isObject(value) ? Object.keys(value) : [],
priority: childPriority,
}),
);
) as DatabaseDataSnapshot;
}

/**
* Returns whether the value exists
*
* @returns {(function())|((path: PathLike, callback: (exists: boolean) => void) => void)|boolean|exists|(() => boolean)}
*/
exists() {
exists(): boolean {
return this._snapshot.exists;
}

Expand All @@ -101,7 +124,7 @@ export default class DatabaseDataSnapshot {
*
* @returns {{'.priority': *, '.value': *}}
*/
exportVal() {
exportVal(): { '.value': unknown; '.priority': string | number | null } {
let { value } = this._snapshot;

if (isObject(value) || isArray(value)) {
Expand All @@ -120,7 +143,7 @@ export default class DatabaseDataSnapshot {
* @param action
* @return {boolean}
*/
forEach(action) {
forEach(action: (snapshot: DatabaseDataSnapshot, index: number) => true | undefined): boolean {
if (!isFunction(action)) {
throw new Error("snapshot.forEach(*) 'action' must be a function.");
}
Expand All @@ -141,7 +164,7 @@ export default class DatabaseDataSnapshot {

for (let i = 0; i < this._snapshot.childKeys.length; i++) {
const key = this._snapshot.childKeys[i];
const snapshot = this.child.call(this, key, MODULAR_DEPRECATION_ARG);
const snapshot = (this.child as any).call(this, key, MODULAR_DEPRECATION_ARG);
const actionReturn = action(snapshot, i);

if (actionReturn === true) {
Expand All @@ -153,7 +176,7 @@ export default class DatabaseDataSnapshot {
return cancelled;
}

getPriority() {
getPriority(): string | number | null {
return this._snapshot.priority;
}

Expand All @@ -163,20 +186,20 @@ export default class DatabaseDataSnapshot {
* @param path
* @returns {boolean}
*/
hasChild(path) {
hasChild(path: string): boolean {
if (!isString(path)) {
throw new Error("snapshot.hasChild(*) 'path' must be a string value.");
}

return deepGet(this._snapshot.value, path) !== undefined;
return deepGet(this._snapshot.value as Record<string, unknown> | unknown[], path) !== undefined;
}

/**
* Returns whether the snapshot has any children
*
* @returns {boolean}
*/
hasChildren() {
hasChildren(): boolean {
return this.numChildren() > 0;
}

Expand All @@ -185,7 +208,7 @@ export default class DatabaseDataSnapshot {
*
* @returns {number}
*/
numChildren() {
numChildren(): number {
const { value } = this._snapshot;
if (isArray(value)) {
return value.length;
Expand All @@ -201,7 +224,7 @@ export default class DatabaseDataSnapshot {
* Same as snapshot.val()
* @returns {any}
*/
toJSON() {
toJSON(): unknown {
return this.val();
}

Expand All @@ -210,7 +233,7 @@ export default class DatabaseDataSnapshot {
*
* @returns {any}
*/
val() {
val(): unknown {
const { value } = this._snapshot;

if (isObject(value) || isArray(value)) {
Expand Down
Loading
Loading