Skip to content

Fix _geo point not being correctly parsed #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 21, 2023
Merged
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
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ typings/
.env
.env.test

# jest snapshot
functions/__tests__/__snapshots__

# parcel-bundler cache (https://parceljs.org/)
.cache
Expand Down
11 changes: 11 additions & 0 deletions functions/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,15 @@ module.exports = {
},
],
},
settings: {
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx'],
},
'import/resolver': {
typescript: {
alwaysTryTypes: true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`
project: './tsconfig.json',
},
},
},
}
27 changes: 27 additions & 0 deletions functions/__tests__/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`extensions config Test MeilisearchIndex parameters param exists 1`] = `
{
"description": "What Meilisearch index do you want to index your data in?",
"example": "example: my_index",
"label": "Meilisearch Index Name",
"param": "MEILISEARCH_INDEX_NAME",
"required": true,
"type": "string",
"validationErrorMessage": "Must be a valid Index format. Index uid can be of type integer or string only composed of alphanumeric characters, hyphens (-) and underscores (_). Check out our guide on [index creation](https://docs.meilisearch.com/learn/core_concepts/indexes.html#index-creation).",
"validationRegex": "^[0-9A-Za-z_-]+$",
}
`;

exports[`extensions config Test fieldsToIndex parameter param exists 1`] = `
{
"default": "",
"description": "What fields do you want to index in Meilisearch? Create a comma-separated list of the field names, or leave it blank to include all fields. The id field is always indexed even when omitted from the list.",
"example": "example: name,description,...",
"label": "Fields to index in Meilisearch",
"param": "MEILISEARCH_FIELDS_TO_INDEX",
"required": false,
"validationErrorMessage": "Fields must be given through a comma-separated list.",
"validationRegex": "^[^,]?[a-zA-Z-_0-9,]*[^,]$",
}
`;
3 changes: 3 additions & 0 deletions functions/__tests__/__snapshots__/util.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getFieldsToIndex configuration detected from environment variables 1`] = `undefined`;
49 changes: 32 additions & 17 deletions functions/__tests__/adapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import * as firebaseFunctionsTestInit from 'firebase-functions-test'
import { mockConsoleInfo } from './__mocks__/console'
import { firestore } from 'firebase-admin/lib/firestore'
import { adaptDocument, adaptValues } from '../src/adapter'
import * as firestore from 'firebase-admin/firestore'
import {
adaptDocumentForMeilisearch,
adaptFieldsForMeilisearch,
} from '../src/meilisearch-adapter'
import defaultDocument from './data/document'

// Mocking of Firebase functions
Expand All @@ -15,7 +18,9 @@ describe('extensions process', () => {
`docs/${defaultDocument.id}`
)

expect(adaptDocument(defaultDocument.id, snapshot, '')).toStrictEqual({
expect(
adaptDocumentForMeilisearch(defaultDocument.id, snapshot, '')
).toStrictEqual({
_firestore_id: defaultDocument.id,
...defaultDocument.document,
})
Expand All @@ -27,7 +32,9 @@ describe('extensions process', () => {
`docs/${defaultDocument.id}`
)

expect(adaptDocument(defaultDocument.id, snapshot, '')).toStrictEqual({
expect(
adaptDocumentForMeilisearch(defaultDocument.id, snapshot, '')
).toStrictEqual({
_firestore_id: defaultDocument.id,
id: '12345',
...defaultDocument.document,
Expand All @@ -41,7 +48,7 @@ describe('extensions process', () => {
)

expect(
adaptDocument(
adaptDocumentForMeilisearch(
defaultDocument.id,
snapshot,
'title,overview,release_date'
Expand All @@ -57,32 +64,40 @@ describe('extensions process', () => {

describe('adaptValues', () => {
test('adaptValues an id value', () => {
expect(adaptValues('id', defaultDocument.id as any)).toStrictEqual([
'id',
defaultDocument.id,
])
expect(
adaptFieldsForMeilisearch(
{ id: defaultDocument.id } as firestore.DocumentData,
'id'
)
).toStrictEqual({ id: defaultDocument.id })
})
test('adaptValues a geo point value', () => {
const geoPoint = new firestore.GeoPoint(48.866667, 2.333333)

expect(adaptValues('_geo', geoPoint)).toStrictEqual([
'_geo',
{
expect(
adaptFieldsForMeilisearch(
{ _geo: geoPoint } as firestore.DocumentData,
'_geo'
)
).toStrictEqual({
_geo: {
lat: 48.866667,
lng: 2.333333,
},
])
})
expect(mockConsoleInfo).toBeCalledWith(
`A GeoPoint was found with the field name '_geo' for compatibility with Meilisearch the field 'latitude' was renamed to 'lat' and the field 'longitude' to 'lng'`
)
})
test('adaptValues a wrong geo point value', () => {
const geoPoint = new firestore.GeoPoint(48.866667, 2.333333)

expect(adaptValues('wrong_geo', geoPoint)).toStrictEqual([
'wrong_geo',
geoPoint,
])
expect(
adaptFieldsForMeilisearch(
{ wrong_geo: geoPoint } as firestore.DocumentData,
'wrong_geo'
)
).toStrictEqual({ wrong_geo: geoPoint })
expect(mockConsoleInfo).toBeCalledWith(
`A GeoPoint was found without the field name '_geo' if you want to use the geoSearch with Meilisearch rename it to '_geo'`
)
Expand Down
5 changes: 3 additions & 2 deletions functions/__tests__/functions.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as firebaseFunctionsTestInit from 'firebase-functions-test'
import mockedEnv from 'mocked-env'
import { mocked } from 'ts-jest/utils'
import { mocked } from 'jest-mock'
import {
mockConsoleLog,
mockConsoleInfo,
Expand All @@ -21,7 +21,7 @@ describe('extension', () => {
let restoreEnv

// Mocking of Meilisearch package
const mockedMeilisearch = mocked(MeiliSearch, true)
const mockedMeilisearch = mocked(MeiliSearch)
const mockedAddDocuments = jest.fn()
const mockedDeleteDocument = jest.fn()
const mockedIndex = jest.fn(() => ({
Expand All @@ -42,6 +42,7 @@ describe('extension', () => {
beforeEach(() => {
restoreEnv = mockedEnv(defaultEnvironment)
config = require('../src/config').config
config.collectionPath = 'collection'
})
afterEach(() => restoreEnv())

Expand Down
28 changes: 16 additions & 12 deletions functions/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as firebaseFunctionsTestInit from 'firebase-functions-test'
import mockedEnv from 'mocked-env'
import { getChangeType, ChangeType, getChangedDocumentId } from '../src/util'
import { ChangeType, getChangedDocumentId, getChangeType } from '../src/util'
import defaultEnvironment from './data/environment'

describe('getChangeType', () => {
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('getChangedDocumentId', () => {
})

describe('getFieldsToIndex', () => {
let util
let adapter
let restoreEnv
let mockParseFieldsToIndex
const config = global.config
Expand All @@ -149,32 +149,36 @@ describe('getFieldsToIndex', () => {
})

test('return empty list', () => {
util = require('../src/util')
mockParseFieldsToIndex = util.parseFieldsToIndex()
adapter = require('../src/meilisearch-adapter')
mockParseFieldsToIndex = adapter.parseFieldsToIndex()
expect(mockParseFieldsToIndex).toMatchObject([])
})

test('return list with one field', () => {
util = require('../src/util')
mockParseFieldsToIndex = util.parseFieldsToIndex('field')
adapter = require('../src/meilisearch-adapter')
mockParseFieldsToIndex = adapter.parseFieldsToIndex('field')
expect(mockParseFieldsToIndex).toMatchObject(['field'])
})

test('return list with multiple fields', () => {
util = require('../src/util')
mockParseFieldsToIndex = util.parseFieldsToIndex('field1,field2,field3')
adapter = require('../src/meilisearch-adapter')
mockParseFieldsToIndex = adapter.parseFieldsToIndex('field1,field2,field3')
expect(mockParseFieldsToIndex).toMatchObject(['field1', 'field2', 'field3'])
})

test('return list with multiple fields and spaces', () => {
util = require('../src/util')
mockParseFieldsToIndex = util.parseFieldsToIndex('field1, field2, field3')
adapter = require('../src/meilisearch-adapter')
mockParseFieldsToIndex = adapter.parseFieldsToIndex(
'field1, field2, field3'
)
expect(mockParseFieldsToIndex).toMatchObject(['field1', 'field2', 'field3'])
})

test('return list of fiels with underscore', () => {
util = require('../src/util')
mockParseFieldsToIndex = util.parseFieldsToIndex('field_1,field_2,field_3')
adapter = require('../src/meilisearch-adapter')
mockParseFieldsToIndex = adapter.parseFieldsToIndex(
'field_1,field_2,field_3'
)
expect(mockParseFieldsToIndex).toMatchObject([
'field_1',
'field_2',
Expand Down
11 changes: 7 additions & 4 deletions functions/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ process.env.FIREBASE_CONFIG = '{}'
module.exports = {
rootDir: './',
preset: 'ts-jest',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/__tests__/tsconfig.json',
},
transform: {
'^.+\\.[tj]sx?$': [
'ts-jest',
{
tsconfig: '<rootDir>/__tests__/tsconfig.json',
},
],
},
testEnvironment: 'node',
testMatch: ['**/__tests__/*.test.ts'],
Expand Down
53 changes: 0 additions & 53 deletions functions/lib/adapter.js

This file was deleted.

4 changes: 2 additions & 2 deletions functions/lib/import/index.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
const admin = require("firebase-admin");
const config_1 = require("./config");
const logs = require("../logs");
const adapter_1 = require("../adapter");
const meilisearch_adapter_1 = require("../meilisearch-adapter");
const create_index_1 = require("../meilisearch/create-index");
const run = async () => {
// Retrieve all arguments from the commande line.
Expand Down Expand Up @@ -80,7 +80,7 @@ async function retrieveCollectionFromFirestore(database, config, index) {
*/
async function sendDocumentsToMeilisearch(docs, index, fieldsToIndex) {
const document = docs.map(snapshot => {
return (0, adapter_1.adaptDocument)(snapshot.id, snapshot, fieldsToIndex);
return (0, meilisearch_adapter_1.adaptDocumentForMeilisearch)(snapshot.id, snapshot, fieldsToIndex);
});
try {
await index.addDocuments(document, { primaryKey: '_firestore_id' });
Expand Down
18 changes: 10 additions & 8 deletions functions/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const firebase_functions_1 = require("firebase-functions");
const create_index_1 = require("./meilisearch/create-index");
const util_1 = require("./util");
const logs = require("./logs");
const adapter_1 = require("./adapter");
const meilisearch_adapter_1 = require("./meilisearch-adapter");
const config_1 = require("./config");
const validate_1 = require("./validate");
const index = (0, create_index_1.initMeilisearchIndex)(config_1.config.meilisearch);
Expand All @@ -30,19 +30,21 @@ logs.init();
* IndexingWorker is responsible for aggregating a defined field from a Firestore collection into a Meilisearch index.
* It is controlled by a Firestore handler.
*/
exports.indexingWorker = functions.handler.firestore.document.onWrite(async (change) => {
exports.indexingWorker = functions.firestore
.document(config_1.config.collectionPath + '/{documentId}')
.onWrite(async (snapshot) => {
logs.start();
const changeType = (0, util_1.getChangeType)(change);
const documentId = (0, util_1.getChangedDocumentId)(change);
const changeType = (0, util_1.getChangeType)(snapshot);
const documentId = (0, util_1.getChangedDocumentId)(snapshot);
switch (changeType) {
case util_1.ChangeType.CREATE:
await handleAddDocument(documentId, change.after);
await handleAddDocument(documentId, snapshot.after);
break;
case util_1.ChangeType.DELETE:
await handleDeleteDocument(documentId);
break;
case util_1.ChangeType.UPDATE:
await handleUpdateDocument(documentId, change.after);
await handleUpdateDocument(documentId, snapshot.after);
break;
}
logs.complete();
Expand All @@ -56,7 +58,7 @@ async function handleAddDocument(documentId, snapshot) {
try {
logs.addDocument(documentId);
if ((0, validate_1.validateDocumentId)(documentId)) {
const document = (0, adapter_1.adaptDocument)(documentId, snapshot, config_1.config.meilisearch.fieldsToIndex || '');
const document = (0, meilisearch_adapter_1.adaptDocumentForMeilisearch)(documentId, snapshot, config_1.config.meilisearch.fieldsToIndex || '');
const { taskUid } = await index.addDocuments([document], {
primaryKey: '_firestore_id',
});
Expand Down Expand Up @@ -98,7 +100,7 @@ async function handleUpdateDocument(documentId, after) {
try {
logs.updateDocument(documentId);
if ((0, validate_1.validateDocumentId)(documentId)) {
const document = (0, adapter_1.adaptDocument)(documentId, after, config_1.config.meilisearch.fieldsToIndex || '');
const document = (0, meilisearch_adapter_1.adaptDocumentForMeilisearch)(documentId, after, config_1.config.meilisearch.fieldsToIndex || '');
const { taskUid } = await index.addDocuments([document]);
firebase_functions_1.logger.info(`Document update request for document with ID ${documentId} added to task list (task ID ${taskUid}).`);
}
Expand Down
Loading