Skip to content

Instantly share code, notes, and snippets.

@rutvik110
Created December 20, 2021 06:53
Show Gist options
  • Save rutvik110/7eec3e5174852fe94b3516a8ae703fe5 to your computer and use it in GitHub Desktop.
Save rutvik110/7eec3e5174852fe94b3516a8ae703fe5 to your computer and use it in GitHub Desktop.
Normal Data to Algolia Format data parser
import { dataProcessor } from "./processors";
export const parseDataToAlgoliaFormat = (snapshot: { [key: string]: any }) => {
let payload: {
[key: string]: boolean | string | number;
} = {
objectID: snapshot.id ?? snapshot.objectID,
path: snapshot.path
};
payload = {
...dataProcessor(snapshot),
...payload
};
// adding the objectId in the return to make sure to restore to original if changed in the post processing.
return payload;
}
'use strict';
/*
* Copyright 2021 Algolia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { firestore } from 'firebase-admin/lib/firestore';
import DocumentReference = firestore.DocumentReference;
import GeoPoint = firestore.GeoPoint;
// import Timestamp = firestore.Timestamp;
const processObject = (objectVal: any) => {
const payload: { [key: string]: any } = {};
for (const [key, val] of Object.entries(objectVal)) {
const [field, value] = processValue(key, val);
payload[field] = value;
}
return payload;
}
const processValue: any = (field: string, value: any) => {
if (value instanceof DocumentReference) {
return [field, processDocumentReference(value)];
} else if (value instanceof GeoPoint) {
// Algolia has a prescribed field name for Geo data.
return ['_geoloc', processGeoPoint(value)];
} else if (value instanceof Date) {
return [field, processDate(value)];
}
else if (value instanceof firestore.Timestamp) {
return [field, processDate(value.toDate())];
}
else if (value instanceof Array) {
return [field, processArray(value)];
} else if (value instanceof Object) {
return [field, processObject(value)];
}
return [field, value]
}
const processArray = (arrayVal: Array<any>) => {
return arrayVal.map((val, index) => {
if (val instanceof Object) {
const [_, value] = processValue(index, val);
return value;
}
return val;
})
}
const processDate = (timestampVal: Date) => {
return timestampVal.getTime();
}
const processDocumentReference = (referenceVal: DocumentReference) => {
return referenceVal.path
}
const processGeoPoint = (geoPointVal: GeoPoint) => {
return {
lat: geoPointVal.latitude,
lng: geoPointVal.longitude
}
}
/**
* The data processor will process the Firestore document. It will loop through the fields and process the values.
* The value can be simple or complex types that are supported by Firestore.
*
* @param data
*/
export const dataProcessor: any = (data: any) => {
return processObject(data);
}
/**
* The field processor will process the Firestore document field value.
*
* @param field
* @param value
*/
export const valueProcessor = (field: string, value: any) => {
return processValue(field, value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment