Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(filtering): Bump FirestoreStorage dependency and enable Firestore Filter capabilities #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"dependencies": {
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
"ts-object-path": "^0.1.2"
"ts-object-path": "^0.1.2",
"@google-cloud/firestore": "^7.11.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That dependency would not work in the browser

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn - Forgot about web. A generic "Filter" class or type can be made instead of relying on the type from this import.

},
"gitHead": "0b06debcfa978dcfd12f74599cded3802179e34b"
}
11 changes: 8 additions & 3 deletions packages/core/src/storage/query.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { BaseModel, ModelQuery } from './types';
import { getPath } from 'ts-object-path';
import { Filter } from '@google-cloud/firestore';

export type WhereProp<T extends BaseModel> = string | ((t: T) => unknown);

export abstract class BaseQuery<T extends BaseModel, Op extends string, R> {
protected abstract applyWhere(key: string, operator: Op, value: any): this;
protected abstract applyWhere(key: string | Filter, operator: Op, value: any): this;
protected abstract applyWhere(filter: Filter): this;
abstract execute(): R;

where(prop: WhereProp<T>, op: Op, value: any) {
return this.applyWhere(this.getWhereProp(prop), op, value);
where(propOrFilter: WhereProp<T> | Filter, op?: Op, value?: any) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The firestore-storage-core package is also used in frontend applications. Is it possible to move that implementation to the storage package?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just copying it as is into the storage package?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it works, if you override the where method into the storage package

if (propOrFilter instanceof Filter) {
return this.applyWhere(propOrFilter);
}
return this.applyWhere(this.getWhereProp(propOrFilter), op!, value);
}

whereAll<K extends ModelQuery<T>>(attributes: K | null) {
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"license": "MIT",
"homepage": "https://github.com/freshfox/firestore-storage",
"dependencies": {
"@google-cloud/firestore": "^6.5.0"
"@google-cloud/firestore": "^7.11.0"
},
"devDependencies": {
"@firebase/rules-unit-testing": "^2.0.7",
Expand Down
19 changes: 15 additions & 4 deletions packages/firestore/src/lib/storage/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
QuerySnapshot,
WhereFilterOp,
Query as FSQuery,
Filter,
} from '@google-cloud/firestore';
import { BaseQuery, BaseModel, WhereProp } from 'firestore-storage-core';

Expand All @@ -13,10 +14,20 @@ export class Query<T extends BaseModel> extends BaseQuery<T, WhereFilterOp, Prom
super();
}

protected applyWhere(key: string, operator: FirebaseFirestore.WhereFilterOp, value: any) {
this.base = this.base.where(key, operator, value);
return this;
}
protected applyWhere<K extends string | Filter>(
keyOrFilter: K,
...args: K extends string
? [FirebaseFirestore.WhereFilterOp, any]
: []
): this {
if (typeof keyOrFilter === 'string') {
const [operator, value] = args as [FirebaseFirestore.WhereFilterOp, any];
this.base = this.base.where(keyOrFilter, operator, value);
} else {
this.base = this.base.where(keyOrFilter as Filter);
}
return this;
}

orderBy(prop: WhereProp<T>, direction: OrderByDirection) {
this.base = this.base.orderBy(this.getWhereProp(prop), direction);
Expand Down