fire-entity
Advanced tools
Comparing version 0.1.2 to 0.2.0
import * as firebase from 'firebase/app'; | ||
import 'firebase/firestore'; | ||
import { Observable } from 'rxjs'; | ||
import { Entity, EntityInsert } from './entity'; | ||
@@ -7,2 +8,3 @@ declare type QueryFn = (ref: firebase.firestore.CollectionReference) => firebase.firestore.Query; | ||
export declare type EntitySelector = (collectionPath: string, queryFn?: QueryFn) => Promise<firebase.firestore.QuerySnapshot>; | ||
export declare type EntityListener = (collectionPath: string, queryFn?: QueryFn) => Observable<firebase.firestore.DocumentChange[]>; | ||
export interface QueryParam { | ||
@@ -16,8 +18,11 @@ prop: string; | ||
private selector; | ||
private listener; | ||
private _prepareInsert; | ||
constructor(inserter: EntityInserter, selector: EntitySelector); | ||
constructor(inserter: EntityInserter, selector: EntitySelector, listener: EntityListener); | ||
insert<T extends EntityInsert>(insert: T): Promise<void>; | ||
selectAll<T extends Entity>(path: string): Promise<T[]>; | ||
selectWhere<T extends Entity>(path: string, filters: QueryParam[]): Promise<T[]>; | ||
listenAll<T extends Entity>(path: string): Observable<T[]>; | ||
listenWhere<T extends Entity>(path: string, filters: QueryParam[]): Observable<T[]>; | ||
} | ||
export {}; |
import * as tslib_1 from "tslib"; | ||
import 'firebase/firestore'; | ||
import { map } from 'rxjs/operators'; | ||
import { convertFirestoreTimestamps } from './util'; | ||
@@ -13,6 +14,10 @@ function extractEntity(snap) { | ||
} | ||
function extractChanges(changes) { | ||
return changes.map(c => extractEntity(c.doc)); | ||
} | ||
export class EntityStore { | ||
constructor(inserter, selector) { | ||
constructor(inserter, selector, listener) { | ||
this.inserter = inserter; | ||
this.selector = selector; | ||
this.listener = listener; | ||
} | ||
@@ -46,3 +51,17 @@ _prepareInsert(data) { | ||
} | ||
listenAll(path) { | ||
const changes$ = this.listener(path); | ||
return changes$.pipe(map(v => extractChanges(v))); | ||
} | ||
listenWhere(path, filters) { | ||
const changes$ = this.listener(path, ref => { | ||
let query = ref; | ||
for (const f of filters) { | ||
query = ref.where(f.prop, f.op, f.val); | ||
} | ||
return query; | ||
}); | ||
return changes$.pipe(map(v => extractChanges(v))); | ||
} | ||
} | ||
//# sourceMappingURL=store.js.map |
{ | ||
"name": "fire-entity", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Allows for storing and retrieving documents in a standardized format from Firestore.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
import * as firebase from 'firebase/app'; | ||
import 'firebase/firestore'; | ||
import {Observable} from 'rxjs'; | ||
import {map} from 'rxjs/operators'; | ||
@@ -20,6 +22,14 @@ import {Entity, EntityInsert} from './entity'; | ||
function extractChanges<T extends Entity>(changes: firebase.firestore.DocumentChange[]): T[] { | ||
return changes.map(c => extractEntity(c.doc)); | ||
} | ||
type QueryFn = (ref: firebase.firestore.CollectionReference) => firebase.firestore.Query; | ||
export type EntityInserter = (collectionPath: string, values: {}) => Promise<void>; | ||
export type EntitySelector = (collectionPath: string, queryFn?: QueryFn) => Promise<firebase.firestore.QuerySnapshot>; | ||
export type EntityInserter = | ||
(collectionPath: string, values: {}) => Promise<void>; | ||
export type EntitySelector = | ||
(collectionPath: string, queryFn?: QueryFn) => Promise<firebase.firestore.QuerySnapshot>; | ||
export type EntityListener = | ||
(collectionPath: string, queryFn?: QueryFn) => Observable<firebase.firestore.DocumentChange[]>; | ||
@@ -40,3 +50,7 @@ export interface QueryParam { | ||
constructor(private inserter: EntityInserter, private selector: EntitySelector) {} | ||
constructor( | ||
private inserter: EntityInserter, | ||
private selector: EntitySelector, | ||
private listener: EntityListener, | ||
) {} | ||
@@ -65,2 +79,18 @@ public insert<T extends EntityInsert>(insert: T): Promise<void> { | ||
} | ||
public listenAll<T extends Entity>(path: string): Observable<T[]> { | ||
const changes$ = this.listener(path); | ||
return changes$.pipe(map(v => extractChanges(v))) | ||
} | ||
public listenWhere<T extends Entity>(path: string, filters: QueryParam[]): Observable<T[]> { | ||
const changes$ = this.listener(path, ref => { | ||
let query: any = ref; | ||
for (const f of filters) { | ||
query = ref.where(f.prop, f.op, f.val); | ||
} | ||
return query; | ||
}); | ||
return changes$.pipe(map(v => extractChanges(v))) | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
13976
258