ToolJet/server/plugins/datasources/firestore/operations.ts
Akshay 5b30aa2007
Chore: Setup pipeline (#1539)
* github actions for PR and push to develop branch

* test workflow

* move to workflows folder

* add setup node action

* modify build

* specify npm version

* config unit test

* specify host postgres

* specify container to run on

* add postgresql dependency

* add specify ws adapter for test

* add e2e test

* fix linting

* only log errors on tests

* update eslint config

* fix linting

* run e2e test in silent mode

* fix library app spec

* dont send email on test env

* fix org scope

* mock env vars

* remove reset modules

* force colors

* explicitly close db connection

* add eslint rule for floating promises

* update workflow

* fix floating promise

* fix lint

* update workflow

* run on all push and pulls

* update lint check files

* simplify workflow

* increase js heap size on env

* separate lint and build

Co-authored-by: arpitnath <arpitnath42@gmail.com>
2021-12-10 08:43:05 +05:30

88 lines
2.3 KiB
TypeScript

import { Firestore, Query } from '@google-cloud/firestore';
export async function queryCollection(
db: Firestore,
collection: string,
limit: number,
where_operation: any,
where_field: string,
where_value: any,
order_field: string,
order_type: any
): Promise<object> {
const limitProvided = isNaN(limit) !== true;
const whereConditionProvided =
[undefined, ''].includes(where_field) === false &&
[undefined, ''].includes(where_operation) === false &&
where_value != undefined;
const orderProvided = [undefined, ''].includes(order_field) === false;
const collectionRef = db.collection(collection);
let query: Query = collectionRef;
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);
const snapshot = await query.get();
const data = [];
snapshot.forEach((doc) => {
data.push({
document_id: doc.id,
data: doc.data(),
});
});
return data;
}
export async function getDocument(db, path: string): Promise<object> {
const docRef = db.doc(path);
const doc = await docRef.get();
// if (!doc.exists) {
return doc.data();
}
export async function setDocument(db, path: string, body: string): Promise<object> {
const docRef = db.doc(path);
const result = await docRef.set(JSON.parse(body));
return result;
}
export async function addDocument(db, path: string, body: string): Promise<object> {
const docRef = db.doc(path);
const result = await docRef.set(JSON.parse(body));
return result;
}
export async function updateDocument(db, path: string, body: string): Promise<object> {
const docRef = db.doc(path);
const result = await docRef.update(JSON.parse(body));
return result;
}
export async function deleteDocument(db, path: string): Promise<object> {
const docRef = db.doc(path);
const result = await docRef.delete();
return result;
}
export async function bulkUpdate(
db,
collection: string,
records: Array<object>,
documentIdKey: string
): Promise<object> {
for (const record of records) {
const path = `${collection}/${record[documentIdKey]}`;
await updateDocument(db, path, JSON.stringify(record));
}
return {};
}