Created
June 25, 2021 18:02
-
-
Save Orbis25/e9c379b04a29ef0e7c5f91c7087c6c3c to your computer and use it in GitHub Desktop.
Generic repository with firebase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import firebase from "firebase"; | |
import { firebaseConfiguration } from "../../firebase"; | |
import { BaseModel } from "../models/base.model"; | |
import { PaginationBaseModel } from "../models/pagination-base.model"; | |
interface IRepositoryBase<TEntity extends BaseModel> { | |
create(model: TEntity): Promise<TEntity>; | |
update(model: TEntity): Promise<TEntity>; | |
/** | |
* exec a softdelete | |
* @param id id de entidad | |
*/ | |
remove(id: string): Promise<void>; | |
/** | |
* generate a infinite scroll pagination | |
* @author Orbis Alonzo Gutierrez | |
* @param qyt quantity totake | |
* @param last lastElement | |
*/ | |
getPaginatedList( | |
qyt: number, | |
last?: any | |
): Promise<PaginationBaseModel<TEntity>>; | |
getAll(): Promise<TEntity[]>; | |
getDocId(id: string): Promise<string>; | |
getOne(id: string): Promise<TEntity | null>; | |
/** | |
* drop entity | |
* @param id id of entity | |
*/ | |
delete(id: string): Promise<void>; | |
getCollection(): firebase.firestore.Query<firebase.firestore.DocumentData>; | |
} | |
export abstract class RepositoryBase<TEntity extends BaseModel> | |
implements IRepositoryBase<TEntity> | |
{ | |
db = firebaseConfiguration.firestore(); | |
collection: firebase.firestore.CollectionReference<firebase.firestore.DocumentData>; | |
/** | |
* | |
*/ | |
constructor(collection: string) { | |
this.collection = this.db.collection(collection); | |
} | |
async delete(id: string): Promise<void> { | |
const docId = await this.getDocId(id); | |
return await this.collection.doc(docId).delete(); | |
} | |
async getOne(id: string): Promise<TEntity | null> { | |
const results = await this.collection | |
.where("id", "==", id) | |
.where("isDeleted", "==", false) | |
.get(); | |
const doc = results.docs[0]; | |
if (!doc) { | |
throw new Error("No encontrado"); | |
} | |
return doc.data() as TEntity; | |
} | |
async getDocId(id: string): Promise<string> { | |
const results = await this.collection | |
.where("id", "==", id) | |
.where("isDeleted", "==", false) | |
.get(); | |
return results.docs[0].id; | |
} | |
async update(model: TEntity): Promise<TEntity> { | |
const docId = await this.getDocId(model.id); | |
await this.collection | |
.doc(docId) | |
.update({ ...model, updatedAt: new Date() }); | |
return model; | |
} | |
async remove(id: string): Promise<void> { | |
const docId = await this.getDocId(id); | |
return await this.collection | |
.doc(docId) | |
.update({ isDeleted: true, updatedAt: new Date() }); | |
} | |
async getPaginatedList( | |
qyt: number = 10, | |
last?: any | |
): Promise<PaginationBaseModel<TEntity>> { | |
let all = await this.collection | |
.where("isDeleted", "==", false) | |
.orderBy("createdAt", "desc") | |
.limit(qyt) | |
.get(); | |
if (!!last) { | |
const entities = await this.collection | |
.where("isDeleted", "==", false) | |
.orderBy("createdAt", "desc") | |
.limit(qyt) | |
.startAfter(last) | |
.get(); | |
const results: TEntity[] = []; | |
entities.forEach((doc) => { | |
results.push(doc.data() as TEntity); | |
}); | |
return { | |
qyt, | |
results, | |
last: entities.docs[entities.docs.length - 1], | |
}; | |
} else { | |
const results: TEntity[] = []; | |
all.forEach((doc) => { | |
results.push(doc.data() as TEntity); | |
}); | |
return { | |
qyt, | |
results, | |
last: all.docs[all.docs.length - 1], | |
}; | |
} | |
} | |
async getAll(): Promise<TEntity[]> { | |
const result = await this.collection | |
.where("isDeleted", "==", false) | |
.orderBy("createdAt", "desc") | |
.get(); | |
const data: TEntity[] = []; | |
result.forEach((doc) => { | |
data.push(doc.data() as TEntity); | |
}); | |
return data; | |
} | |
async create(model: TEntity): Promise<TEntity> { | |
await this.collection.add({ ...model }); | |
return model; | |
} | |
getCollection() { | |
return this.collection.where("isDeleted", "==", false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
this example explain how to implement this and overrride the create method