Skip to content

Commit d9dc3e6

Browse files
meili-bors[bot]bidoubiwaoplik0
authored
Merge #147
147: Fix _geo point not being correctly parsed r=bidoubiwa a=bidoubiwa fixes: #143 If a user provides a field named `_geo` with a `GeoPoint` type, the type will be transformed into a Meilisearch geopoint compatible format. In firestore: ``` { "_geo": { "latitude": x, "longitude": y } } ``` In Meilisearch it becomes: ``` { "_geo": { "lng": x, "lat": y } } ``` Co-authored-by: Charlotte Vermandel <[email protected]> Co-authored-by: oplik0 <[email protected]>
2 parents 30f84f2 + 342e4ff commit d9dc3e6

21 files changed

+1912
-1608
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ typings/
8383
.env
8484
.env.test
8585

86-
# jest snapshot
87-
functions/__tests__/__snapshots__
8886

8987
# parcel-bundler cache (https://parceljs.org/)
9088
.cache

functions/.eslintrc.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,15 @@ module.exports = {
5151
},
5252
],
5353
},
54+
settings: {
55+
'import/parsers': {
56+
'@typescript-eslint/parser': ['.ts', '.tsx'],
57+
},
58+
'import/resolver': {
59+
typescript: {
60+
alwaysTryTypes: true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`
61+
project: './tsconfig.json',
62+
},
63+
},
64+
},
5465
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`extensions config Test MeilisearchIndex parameters param exists 1`] = `
4+
{
5+
"description": "What Meilisearch index do you want to index your data in?",
6+
"example": "example: my_index",
7+
"label": "Meilisearch Index Name",
8+
"param": "MEILISEARCH_INDEX_NAME",
9+
"required": true,
10+
"type": "string",
11+
"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).",
12+
"validationRegex": "^[0-9A-Za-z_-]+$",
13+
}
14+
`;
15+
16+
exports[`extensions config Test fieldsToIndex parameter param exists 1`] = `
17+
{
18+
"default": "",
19+
"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.",
20+
"example": "example: name,description,...",
21+
"label": "Fields to index in Meilisearch",
22+
"param": "MEILISEARCH_FIELDS_TO_INDEX",
23+
"required": false,
24+
"validationErrorMessage": "Fields must be given through a comma-separated list.",
25+
"validationRegex": "^[^,]?[a-zA-Z-_0-9,]*[^,]$",
26+
}
27+
`;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`getFieldsToIndex configuration detected from environment variables 1`] = `undefined`;

functions/__tests__/adapter.test.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import * as firebaseFunctionsTestInit from 'firebase-functions-test'
22
import { mockConsoleInfo } from './__mocks__/console'
3-
import { firestore } from 'firebase-admin/lib/firestore'
4-
import { adaptDocument, adaptValues } from '../src/adapter'
3+
import * as firestore from 'firebase-admin/firestore'
4+
import {
5+
adaptDocumentForMeilisearch,
6+
adaptFieldsForMeilisearch,
7+
} from '../src/meilisearch-adapter'
58
import defaultDocument from './data/document'
69

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

18-
expect(adaptDocument(defaultDocument.id, snapshot, '')).toStrictEqual({
21+
expect(
22+
adaptDocumentForMeilisearch(defaultDocument.id, snapshot, '')
23+
).toStrictEqual({
1924
_firestore_id: defaultDocument.id,
2025
...defaultDocument.document,
2126
})
@@ -27,7 +32,9 @@ describe('extensions process', () => {
2732
`docs/${defaultDocument.id}`
2833
)
2934

30-
expect(adaptDocument(defaultDocument.id, snapshot, '')).toStrictEqual({
35+
expect(
36+
adaptDocumentForMeilisearch(defaultDocument.id, snapshot, '')
37+
).toStrictEqual({
3138
_firestore_id: defaultDocument.id,
3239
id: '12345',
3340
...defaultDocument.document,
@@ -41,7 +48,7 @@ describe('extensions process', () => {
4148
)
4249

4350
expect(
44-
adaptDocument(
51+
adaptDocumentForMeilisearch(
4552
defaultDocument.id,
4653
snapshot,
4754
'title,overview,release_date'
@@ -57,32 +64,40 @@ describe('extensions process', () => {
5764

5865
describe('adaptValues', () => {
5966
test('adaptValues an id value', () => {
60-
expect(adaptValues('id', defaultDocument.id as any)).toStrictEqual([
61-
'id',
62-
defaultDocument.id,
63-
])
67+
expect(
68+
adaptFieldsForMeilisearch(
69+
{ id: defaultDocument.id } as firestore.DocumentData,
70+
'id'
71+
)
72+
).toStrictEqual({ id: defaultDocument.id })
6473
})
6574
test('adaptValues a geo point value', () => {
6675
const geoPoint = new firestore.GeoPoint(48.866667, 2.333333)
6776

68-
expect(adaptValues('_geo', geoPoint)).toStrictEqual([
69-
'_geo',
70-
{
77+
expect(
78+
adaptFieldsForMeilisearch(
79+
{ _geo: geoPoint } as firestore.DocumentData,
80+
'_geo'
81+
)
82+
).toStrictEqual({
83+
_geo: {
7184
lat: 48.866667,
7285
lng: 2.333333,
7386
},
74-
])
87+
})
7588
expect(mockConsoleInfo).toBeCalledWith(
7689
`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'`
7790
)
7891
})
7992
test('adaptValues a wrong geo point value', () => {
8093
const geoPoint = new firestore.GeoPoint(48.866667, 2.333333)
8194

82-
expect(adaptValues('wrong_geo', geoPoint)).toStrictEqual([
83-
'wrong_geo',
84-
geoPoint,
85-
])
95+
expect(
96+
adaptFieldsForMeilisearch(
97+
{ wrong_geo: geoPoint } as firestore.DocumentData,
98+
'wrong_geo'
99+
)
100+
).toStrictEqual({ wrong_geo: geoPoint })
86101
expect(mockConsoleInfo).toBeCalledWith(
87102
`A GeoPoint was found without the field name '_geo' if you want to use the geoSearch with Meilisearch rename it to '_geo'`
88103
)

functions/__tests__/functions.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as firebaseFunctionsTestInit from 'firebase-functions-test'
22
import mockedEnv from 'mocked-env'
3-
import { mocked } from 'ts-jest/utils'
3+
import { mocked } from 'jest-mock'
44
import {
55
mockConsoleLog,
66
mockConsoleInfo,
@@ -21,7 +21,7 @@ describe('extension', () => {
2121
let restoreEnv
2222

2323
// Mocking of Meilisearch package
24-
const mockedMeilisearch = mocked(MeiliSearch, true)
24+
const mockedMeilisearch = mocked(MeiliSearch)
2525
const mockedAddDocuments = jest.fn()
2626
const mockedDeleteDocument = jest.fn()
2727
const mockedIndex = jest.fn(() => ({
@@ -42,6 +42,7 @@ describe('extension', () => {
4242
beforeEach(() => {
4343
restoreEnv = mockedEnv(defaultEnvironment)
4444
config = require('../src/config').config
45+
config.collectionPath = 'collection'
4546
})
4647
afterEach(() => restoreEnv())
4748

functions/__tests__/util.test.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as firebaseFunctionsTestInit from 'firebase-functions-test'
22
import mockedEnv from 'mocked-env'
3-
import { getChangeType, ChangeType, getChangedDocumentId } from '../src/util'
3+
import { ChangeType, getChangedDocumentId, getChangeType } from '../src/util'
44
import defaultEnvironment from './data/environment'
55

66
describe('getChangeType', () => {
@@ -132,7 +132,7 @@ describe('getChangedDocumentId', () => {
132132
})
133133

134134
describe('getFieldsToIndex', () => {
135-
let util
135+
let adapter
136136
let restoreEnv
137137
let mockParseFieldsToIndex
138138
const config = global.config
@@ -149,32 +149,36 @@ describe('getFieldsToIndex', () => {
149149
})
150150

151151
test('return empty list', () => {
152-
util = require('../src/util')
153-
mockParseFieldsToIndex = util.parseFieldsToIndex()
152+
adapter = require('../src/meilisearch-adapter')
153+
mockParseFieldsToIndex = adapter.parseFieldsToIndex()
154154
expect(mockParseFieldsToIndex).toMatchObject([])
155155
})
156156

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

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

169169
test('return list with multiple fields and spaces', () => {
170-
util = require('../src/util')
171-
mockParseFieldsToIndex = util.parseFieldsToIndex('field1, field2, field3')
170+
adapter = require('../src/meilisearch-adapter')
171+
mockParseFieldsToIndex = adapter.parseFieldsToIndex(
172+
'field1, field2, field3'
173+
)
172174
expect(mockParseFieldsToIndex).toMatchObject(['field1', 'field2', 'field3'])
173175
})
174176

175177
test('return list of fiels with underscore', () => {
176-
util = require('../src/util')
177-
mockParseFieldsToIndex = util.parseFieldsToIndex('field_1,field_2,field_3')
178+
adapter = require('../src/meilisearch-adapter')
179+
mockParseFieldsToIndex = adapter.parseFieldsToIndex(
180+
'field_1,field_2,field_3'
181+
)
178182
expect(mockParseFieldsToIndex).toMatchObject([
179183
'field_1',
180184
'field_2',

functions/jest.config.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ process.env.FIREBASE_CONFIG = '{}'
33
module.exports = {
44
rootDir: './',
55
preset: 'ts-jest',
6-
globals: {
7-
'ts-jest': {
8-
tsconfig: '<rootDir>/__tests__/tsconfig.json',
9-
},
6+
transform: {
7+
'^.+\\.[tj]sx?$': [
8+
'ts-jest',
9+
{
10+
tsconfig: '<rootDir>/__tests__/tsconfig.json',
11+
},
12+
],
1013
},
1114
testEnvironment: 'node',
1215
testMatch: ['**/__tests__/*.test.ts'],

functions/lib/adapter.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

functions/lib/import/index.js

100755100644
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
1919
const admin = require("firebase-admin");
2020
const config_1 = require("./config");
2121
const logs = require("../logs");
22-
const adapter_1 = require("../adapter");
22+
const meilisearch_adapter_1 = require("../meilisearch-adapter");
2323
const create_index_1 = require("../meilisearch/create-index");
2424
const run = async () => {
2525
// Retrieve all arguments from the commande line.
@@ -80,7 +80,7 @@ async function retrieveCollectionFromFirestore(database, config, index) {
8080
*/
8181
async function sendDocumentsToMeilisearch(docs, index, fieldsToIndex) {
8282
const document = docs.map(snapshot => {
83-
return (0, adapter_1.adaptDocument)(snapshot.id, snapshot, fieldsToIndex);
83+
return (0, meilisearch_adapter_1.adaptDocumentForMeilisearch)(snapshot.id, snapshot, fieldsToIndex);
8484
});
8585
try {
8686
await index.addDocuments(document, { primaryKey: '_firestore_id' });

0 commit comments

Comments
 (0)