fire-entity
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -5,7 +5,4 @@ import * as firebase from 'firebase/app'; | ||
import { Entity, EntityInsert } from './entity'; | ||
declare type QueryFn = (ref: firebase.firestore.CollectionReference) => firebase.firestore.Query; | ||
export declare type CollectionSelector = (path: string) => firebase.firestore.CollectionReference; | ||
export declare type EntityInserter = (collectionPath: string, values: {}) => Promise<void>; | ||
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 declare type DocumentSelector = (path: string) => firebase.firestore.DocumentReference; | ||
export interface QueryParam { | ||
@@ -17,5 +14,5 @@ prop: string; | ||
export declare abstract class EntityStore { | ||
private selector; | ||
private _prepareInsert; | ||
constructor(selector: CollectionSelector); | ||
private collection; | ||
private doc; | ||
constructor(collection: CollectionSelector, doc: DocumentSelector); | ||
insert<T extends EntityInsert>(insert: T): Promise<void>; | ||
@@ -26,3 +23,3 @@ selectAll<T extends Entity>(path: string): Promise<T[]>; | ||
listenWhere<T extends Entity>(path: string, filters: QueryParam[]): Observable<T[]>; | ||
delete<T extends Entity>(entity: T): Promise<void>; | ||
} | ||
export {}; |
@@ -32,9 +32,10 @@ import * as tslib_1 from "tslib"; | ||
} | ||
function prepareDocumentInsert(data) { | ||
return Object.assign({}, data, { lastUpdated: new Date() }); | ||
} | ||
export class EntityStore { | ||
constructor(selector) { | ||
this.selector = selector; | ||
constructor(collection, doc) { | ||
this.collection = collection; | ||
this.doc = doc; | ||
} | ||
_prepareInsert(data) { | ||
return Object.assign({}, data, { lastUpdated: new Date() }); | ||
} | ||
insert(insert) { | ||
@@ -44,4 +45,4 @@ return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const collectionPath = getCollectionPath(insert); | ||
const preparedData = this._prepareInsert(data); | ||
yield this.selector(collectionPath).add(preparedData); | ||
const preparedData = prepareDocumentInsert(data); | ||
yield this.collection(collectionPath).add(preparedData); | ||
return undefined; | ||
@@ -52,3 +53,3 @@ }); | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const snap = yield this.selector(path).get(); | ||
const snap = yield this.collection(path).get(); | ||
return snap.docs.map(doc => extractEntity(doc)); | ||
@@ -59,3 +60,3 @@ }); | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
let query = buildQuery(this.selector(path), filters); | ||
let query = buildQuery(this.collection(path), filters); | ||
const snap = yield query.get(); | ||
@@ -66,9 +67,12 @@ return snap.docs.map(doc => extractEntity(doc)); | ||
listenAll(path) { | ||
return createSnapshotObservable(this.selector(path)); | ||
return createSnapshotObservable(this.collection(path)); | ||
} | ||
listenWhere(path, filters) { | ||
let query = buildQuery(this.selector(path), filters); | ||
let query = buildQuery(this.collection(path), filters); | ||
return createSnapshotObservable(query); | ||
} | ||
delete(entity) { | ||
return this.doc(entity.path).delete(); | ||
} | ||
} | ||
//# sourceMappingURL=store.js.map |
{ | ||
"name": "fire-entity", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Allows for storing and retrieving documents in a standardized format from Firestore.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -15,2 +15,9 @@ export interface EntitySystemProps { | ||
parentPath?: string; | ||
} | ||
} | ||
export const EntityAttributes = [ | ||
'id', | ||
'type', | ||
'path', | ||
'parentPath' | ||
]; |
@@ -5,5 +5,14 @@ import * as firebase from 'firebase/app'; | ||
import {Entity, EntityInsert} from './entity'; | ||
import {Entity, EntityInsert, EntityAttributes} from './entity'; | ||
import {convertFirestoreTimestamps} from './util'; | ||
export type CollectionSelector = (path: string) => firebase.firestore.CollectionReference; | ||
export type DocumentSelector = (path: string) => firebase.firestore.DocumentReference; | ||
export interface QueryParam { | ||
prop: string; | ||
op: firebase.firestore.WhereFilterOp; | ||
val: any; | ||
} | ||
function extractEntity<T extends Entity>(snap: firebase.firestore.QueryDocumentSnapshot): T { | ||
@@ -22,20 +31,2 @@ const data = snap.data() as T; | ||
type QueryFn = (ref: firebase.firestore.CollectionReference) => firebase.firestore.Query; | ||
export type CollectionSelector = | ||
(path: string) => firebase.firestore.CollectionReference; | ||
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[]>; | ||
export interface QueryParam { | ||
prop: string; | ||
op: firebase.firestore.WhereFilterOp; | ||
val: any; | ||
} | ||
function getCollectionPath<T extends Entity, E extends EntityInsert>(entity: T | E): string { | ||
@@ -66,11 +57,15 @@ return !entity.parentPath ? entity.type : `${entity.parentPath}/${entity.type}`; | ||
export abstract class EntityStore { | ||
private _prepareInsert(data: {}): {} { | ||
return { | ||
...data, | ||
lastUpdated: new Date(), | ||
} | ||
function prepareDocumentData(data: {[key: string]: any}): {} { | ||
data = { | ||
...data, | ||
lastUpdated: new Date(), | ||
}; | ||
for (const attr of EntityAttributes) { | ||
delete data[attr]; | ||
} | ||
return data; | ||
} | ||
constructor(private selector: CollectionSelector) {} | ||
export abstract class EntityStore { | ||
constructor(private collection: CollectionSelector, private doc: DocumentSelector) {} | ||
@@ -80,4 +75,4 @@ public async insert<T extends EntityInsert>(insert: T): Promise<void> { | ||
const collectionPath = getCollectionPath(insert); | ||
const preparedData = this._prepareInsert(data); | ||
await this.selector(collectionPath).add(preparedData); | ||
const preparedData = prepareDocumentData(data); | ||
await this.collection(collectionPath).add(preparedData); | ||
return undefined; | ||
@@ -87,3 +82,3 @@ } | ||
public async selectAll<T extends Entity>(path: string): Promise<T[]> { | ||
const snap = await this.selector(path).get(); | ||
const snap = await this.collection(path).get(); | ||
return snap.docs.map(doc => extractEntity<T>(doc)); | ||
@@ -93,3 +88,3 @@ } | ||
public async selectWhere<T extends Entity>(path: string, filters: QueryParam[]): Promise<T[]> { | ||
let query = buildQuery(this.selector(path), filters); | ||
let query = buildQuery(this.collection(path), filters); | ||
const snap = await query.get(); | ||
@@ -100,9 +95,19 @@ return snap.docs.map(doc => extractEntity<T>(doc)); | ||
public listenAll<T extends Entity>(path: string): Observable<T[]> { | ||
return createSnapshotObservable(this.selector(path)); | ||
return createSnapshotObservable(this.collection(path)); | ||
} | ||
public listenWhere<T extends Entity>(path: string, filters: QueryParam[]): Observable<T[]> { | ||
let query = buildQuery(this.selector(path), filters); | ||
let query = buildQuery(this.collection(path), filters); | ||
return createSnapshotObservable(query); | ||
} | ||
public delete<T extends Entity>(entity: T): Promise<void> { | ||
return this.doc(entity.path).delete(); | ||
} | ||
public update<T extends Entity>(entity: T): Promise<void> { | ||
const path = entity.path; | ||
const data = prepareDocumentData(entity); | ||
return this.doc(path).update(data); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14920
278