2021-12-10 03:13:05 +00:00
|
|
|
import { Firestore, Query } from '@google-cloud/firestore';
|
2021-09-21 18:49:29 +00:00
|
|
|
|
|
|
|
|
export async function queryCollection(
|
|
|
|
|
db: Firestore,
|
|
|
|
|
collection: string,
|
|
|
|
|
limit: number,
|
|
|
|
|
where_operation: any,
|
|
|
|
|
where_field: string,
|
|
|
|
|
where_value: any,
|
|
|
|
|
order_field: string,
|
2021-12-10 03:13:05 +00:00
|
|
|
order_type: any
|
2021-09-21 18:49:29 +00:00
|
|
|
): Promise<object> {
|
|
|
|
|
const limitProvided = isNaN(limit) !== true;
|
|
|
|
|
const whereConditionProvided =
|
2021-12-10 03:13:05 +00:00
|
|
|
[undefined, ''].includes(where_field) === false &&
|
|
|
|
|
[undefined, ''].includes(where_operation) === false &&
|
|
|
|
|
where_value != undefined;
|
|
|
|
|
const orderProvided = [undefined, ''].includes(order_field) === false;
|
2021-09-21 18:49:29 +00:00
|
|
|
|
|
|
|
|
const collectionRef = db.collection(collection);
|
|
|
|
|
let query: Query = collectionRef;
|
|
|
|
|
|
2021-12-10 03:13:05 +00:00
|
|
|
if (whereConditionProvided) query = query.where(where_field, where_operation, where_value);
|
|
|
|
|
if (limitProvided) query = query.limit(limit);
|
|
|
|
|
if (orderProvided) query = query.orderBy(order_field, order_type);
|
2021-09-21 18:49:29 +00:00
|
|
|
|
|
|
|
|
const snapshot = await query.get();
|
2021-07-14 14:14:35 +00:00
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const data = [];
|
2021-07-14 14:14:35 +00:00
|
|
|
snapshot.forEach((doc) => {
|
2021-09-21 13:48:28 +00:00
|
|
|
data.push({
|
|
|
|
|
document_id: doc.id,
|
|
|
|
|
data: doc.data(),
|
|
|
|
|
});
|
2021-07-14 14:14:35 +00:00
|
|
|
});
|
|
|
|
|
|
2021-08-11 02:13:52 +00:00
|
|
|
return data;
|
2021-07-14 14:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
export async function getDocument(db, path: string): Promise<object> {
|
2021-07-14 14:14:35 +00:00
|
|
|
const docRef = db.doc(path);
|
|
|
|
|
const doc = await docRef.get();
|
|
|
|
|
// if (!doc.exists) {
|
|
|
|
|
|
|
|
|
|
return doc.data();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
export async function setDocument(db, path: string, body: string): Promise<object> {
|
2021-07-14 14:14:35 +00:00
|
|
|
const docRef = db.doc(path);
|
|
|
|
|
const result = await docRef.set(JSON.parse(body));
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
export async function addDocument(db, path: string, body: string): Promise<object> {
|
2021-07-14 14:14:35 +00:00
|
|
|
const docRef = db.doc(path);
|
|
|
|
|
const result = await docRef.set(JSON.parse(body));
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
export async function updateDocument(db, path: string, body: string): Promise<object> {
|
2021-07-14 14:14:35 +00:00
|
|
|
const docRef = db.doc(path);
|
|
|
|
|
const result = await docRef.update(JSON.parse(body));
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
export async function deleteDocument(db, path: string): Promise<object> {
|
2021-07-14 14:14:35 +00:00
|
|
|
const docRef = db.doc(path);
|
|
|
|
|
const result = await docRef.delete();
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
export async function bulkUpdate(
|
|
|
|
|
db,
|
|
|
|
|
collection: string,
|
|
|
|
|
records: Array<object>,
|
|
|
|
|
documentIdKey: string
|
|
|
|
|
): Promise<object> {
|
|
|
|
|
for (const record of records) {
|
2021-07-14 14:14:35 +00:00
|
|
|
const path = `${collection}/${record[documentIdKey]}`;
|
2021-12-10 03:13:05 +00:00
|
|
|
await updateDocument(db, path, JSON.stringify(record));
|
2021-07-14 14:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|