Skip to content
Closed
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
22 changes: 11 additions & 11 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module.exports = {
"root": true,
"env": {
root: true,
env: {
es6: true,
node: true,
jest: true,
},
"parserOptions": {
"ecmaVersion": 2020,
parserOptions: {
ecmaVersion: 2020,
},
"extends": [
"eslint:recommended",
"google",
],
"rules": {
"quotes": ["error", "double"],
"max-len": [1, {"code": 200}],
extends: ["eslint:recommended", "google"],
rules: {
quotes: ["error", "double"],
"max-len": [1, {code: 200}],
"quote-props": [1, "as-needed"],
indent: ["error", 2],
"linebreak-style": 0,
},
};
17 changes: 15 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ on: [push, pull_request_target]
jobs:
# Source: https://iterative.ai/blog/testing-external-contributions-using-github-actions-secrets
authorize:
environment:
${{ github.event_name == 'pull_request_target' &&
environment: ${{ github.event_name == 'pull_request_target' &&
github.event.pull_request.head.repo.full_name != github.repository &&
'sandbox' || 'internal' }}
runs-on: ubuntu-latest
Expand All @@ -16,6 +15,17 @@ jobs:
build_and_test:
needs: authorize
runs-on: ubuntu-latest
services:
typesense:
image: typesense/typesense:27.0
ports:
- 8108:8108/tcp
volumes:
- /tmp/typesense-server-data:/data
env:
TYPESENSE_DATA_DIR: /data
TYPESENSE_API_KEY: xyz
TYPESENSE_ENABLE_CORS: true
steps:
- uses: actions/checkout@v3
with:
Expand All @@ -32,6 +42,9 @@ jobs:
- run: npm install
- run: npm install -g firebase-tools
- run: cd functions && npm install
- name: Run ESLint
run: npm run lint
continue-on-error: false # Ensure the workflow fails if ESLint fails
- name: Run Tests
env:
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
Expand Down
10 changes: 10 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"trailingComma": "all",
"semi": true,
"singleQuote": false,
"useTabs": false,
"tabWidth": 2,
"bracketSpacing": false,
"endOfLine": "lf",
"printWidth": 200
}
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
typesense:
image: typesense/typesense:27.0
restart: on-failure
ports:
- "8108:8108"
volumes:
- ./typesense-server-data:/data
command: "--data-dir /data --api-key=xyz --enable-cors"
4 changes: 1 addition & 3 deletions firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
"ui": {
"enabled": true
},
"extensions": {

},
"extensions": {},
"singleProjectMode": true
},
"extensions": {
Expand Down
30 changes: 17 additions & 13 deletions functions/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
const config = require("./config.js");

const mapValue = (value) => {
if (typeof value === "object" && value !== null && value.seconds != null && value.nanoseconds != null) {
const isObject = typeof value === "object";
const notNull = value !== null;
const length = isObject && notNull ? Object.keys(value).length : 0;

const latitude = value?.latitude ?? value?.lat;
const longitude = value?.longitude ?? value?.lng;
const hasGeohashField = value?.geohash != null && length == 3;
const isGeopointType = latitude != null && longitude != null && (length == 2 || hasGeohashField);

if (isObject && notNull && value.seconds != null && value.nanoseconds != null) {
// convert date to Unix timestamp
// https://typesense.org/docs/0.22.2/api/collections.html#indexing-dates
return Math.floor(value.toDate().getTime() / 1000);
} else if (typeof value === "object" && value !== null && value.latitude != null && value.longitude != null) {
return [value.latitude, value.longitude];
} else if (typeof value === "object" && value !== null && value.firestore != null && value.path != null) {
return {"path": value.path};
} else if (isObject && notNull && isGeopointType) {
return [latitude, longitude];
} else if (isObject && notNull && value.firestore != null && value.path != null) {
return {path: value.path};
} else if (Array.isArray(value)) {
return value.map(mapValue);
} else if (typeof value === "object" && value !== null) {
} else if (isObject && notNull) {
return Object.fromEntries(Object.entries(value).map(([key, value]) => [key, mapValue(value)]));
} else {
return value;
Expand All @@ -23,10 +32,7 @@ const mapValue = (value) => {
* @param {Array} fieldsToExtract
* @return {Object} typesenseDocument
*/
exports.typesenseDocumentFromSnapshot = async (
firestoreDocumentSnapshot,
fieldsToExtract = config.firestoreCollectionFields,
) => {
exports.typesenseDocumentFromSnapshot = async (firestoreDocumentSnapshot, fieldsToExtract = config.firestoreCollectionFields) => {
const flat = await import("flat");
const data = firestoreDocumentSnapshot.data();

Expand All @@ -41,9 +47,7 @@ exports.typesenseDocumentFromSnapshot = async (

// using flat to flatten nested objects for older versions of Typesense that did not support nested fields
// https://typesense.org/docs/0.22.2/api/collections.html#indexing-nested-fields
const typesenseDocument = config.shouldFlattenNestedDocuments ?
flat.flatten(mappedDocument, {safe: true}) :
mappedDocument;
const typesenseDocument = config.shouldFlattenNestedDocuments ? flat.flatten(mappedDocument, {safe: true}) : mappedDocument;

typesenseDocument.id = firestoreDocumentSnapshot.id;

Expand Down
2 changes: 0 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
module.exports = {
globalSetup: "./test/support/globalSetup.js",
globalTeardown: "./test/support/globalTeardown.js",
setupFiles: ["./test/support/dotenv.js"],
verbose: true,
testTimeout: 300000,
Expand Down
Loading