-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathcreate-reference.ts
More file actions
25 lines (24 loc) · 1.06 KB
/
create-reference.ts
File metadata and controls
25 lines (24 loc) · 1.06 KB
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
import { map } from 'rxjs/operators';
import { AngularFireObject, DatabaseQuery } from '../interfaces';
import { createObjectSnapshotChanges } from './snapshot-changes';
import { AngularFireDatabase } from '../database';
export function createObjectReference<T= any>(query: DatabaseQuery, afDatabase: AngularFireDatabase): AngularFireObject<T> {
return {
query,
snapshotChanges<T>() {
return createObjectSnapshotChanges<T>(query, afDatabase.schedulers.outsideAngular)().pipe(
afDatabase.keepUnstableUntilFirst
);
},
update(data: Partial<T>) { return query.ref.update(data as any) as Promise<void>; },
set(data: T) { return query.ref.set(data) as Promise<void>; },
remove() { return query.ref.remove() as Promise<void>; },
valueChanges<T>() {
const snapshotChanges$ = createObjectSnapshotChanges(query, afDatabase.schedulers.outsideAngular)();
return snapshotChanges$.pipe(
afDatabase.keepUnstableUntilFirst,
map(action => action.payload.exists() ? action.payload.val() as T : null)
);
},
};
}