Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
raiindev committed Oct 28, 2024
1 parent 21df237 commit aff41a2
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 24 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ When the **Scheduled job** is executed, it checks the collection status, to avoi
already in sync. You can always trigger a new deployment manually.

<img src="https://raw.githubusercontent.com/askorama/orama-plugin-strapi/main/misc/assets/deploy.gif" alt="Manual deploy" width="600" />

---
## Advanced usage
17 changes: 13 additions & 4 deletions admin/src/components/SchemaMapper/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import {
Box,
Button,
Checkbox,
Flex,
Switch,
Expand Down Expand Up @@ -119,7 +120,7 @@ const SchemaMapper = ({ collection, contentTypeSchema, onSchemaChange }) => {
setSelectedAttributes([])
setSearchableAttributes([])
} else {
setSelectedAttributes(schemaAttributes)
setSelectedAttributes(schemaAttributes.map((field) => field.field))
}
}

Expand All @@ -136,8 +137,16 @@ const SchemaMapper = ({ collection, contentTypeSchema, onSchemaChange }) => {
<Box>
{/*TODO: style this*/}
{collection.hasSettings && (
<Typography variant="gamma" color="grey-600">
The schema is handled by the Orama Cloud plugin settings.
<Typography variant="omega">
This is handled by the Orama Cloud plugin settings, under{' '}
<code
style={{
color: 'orange'
}}
>
config/plugins.js
</code>{' '}
directory.
</Typography>
)}
{!collection.hasSettings && (
Expand Down Expand Up @@ -175,7 +184,7 @@ const SchemaMapper = ({ collection, contentTypeSchema, onSchemaChange }) => {
</Td>
<Td
onClick={() => onCheck(field)}
style={{ cursor: "pointer", display: "flex", alignItems: "center" }}
style={{ cursor: 'pointer', display: 'flex', alignItems: 'center' }}
>
<Typography textColor="neutral800">{field}</Typography>
{!searchable && (
Expand Down
7 changes: 0 additions & 7 deletions admin/src/pages/PluginSettings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,6 @@ const HomePage = () => {
if (currentCollection) {
const contentType = contentTypes.find((ct) => ct.value === currentCollection.entity)
setCurrentContentType(contentType)

if (contentType?.availableRelations.length === 0) {
setCurrentCollection({
...currentCollection,
includedRelations: []
})
}
}
}, [currentCollection])

Expand Down
7 changes: 4 additions & 3 deletions server/services/orama-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ class OramaManager {
}

if (
(collectionSettings.schema && !collectionSettings.transformer) ||
(!collectionSettings.schema && collectionSettings.transformer)
collectionSettings &&
((collectionSettings.schema && !collectionSettings.transformer) ||
(!collectionSettings.schema && collectionSettings.transformer))
) {
this.strapi.log.error(`ERROR: Both schema and transformer are required in the collection settings`)
return false
Expand All @@ -77,7 +78,7 @@ class OramaManager {
return entries
}

return entries.map(transformer)
return entries.map((entry) => transformer(entry))
}

/*
Expand Down
61 changes: 52 additions & 9 deletions server/services/orama-manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ const { CloudManager } = require('@oramacloud/client')
const { mockCollection, mockNotValidCollection } = require('../__mocks__/collection')
const { mockedTestRecord } = require('../__mocks__/content-types')

const titleTransformer = jest.fn((title) => title.toUpperCase())
const mockTransformer = jest.fn((entry) => ({
...entry,
title: entry.title.toUpperCase()
}))

const strapi = {
plugin: jest.fn().mockReturnThis(),
Expand Down Expand Up @@ -109,6 +112,46 @@ describe('OramaManager', () => {
)
})

it('should return false if collection has settings but schema is missing', () => {
strapi.config.get = jest.fn((string) => {
if (string === 'plugin.orama-cloud.privateApiKey') return 'mockPrivateApiKey'
if (string === 'plugin.orama-cloud.collectionSettings')
return {
[mockCollection.indexId]: {
transformer: mockTransformer
}
}
})
oramaManager = new OramaManager({ strapi })

const result = oramaManager.validate(mockCollection)

expect(result).toBe(false)
expect(strapi.log.error).toHaveBeenCalledWith(
`ERROR: Both schema and transformer are required in the collection settings`
)
})

it('should return false if collection has settings but transformer is missing', () => {
strapi.config.get = jest.fn((string) => {
if (string === 'plugin.orama-cloud.privateApiKey') return 'mockPrivateApiKey'
if (string === 'plugin.orama-cloud.collectionSettings')
return {
[mockCollection.indexId]: {
schema: {}
}
}
})
oramaManager = new OramaManager({ strapi })

const result = oramaManager.validate(mockCollection)

expect(result).toBe(false)
expect(strapi.log.error).toHaveBeenCalledWith(
`ERROR: Both schema and transformer are required in the collection settings`
)
})

it('should return true if all validations pass', () => {
const result = oramaManager.validate(mockCollection)

Expand All @@ -129,9 +172,10 @@ describe('OramaManager', () => {
if (string === 'plugin.orama-cloud.collectionSettings')
return {
indexId: {
documentsTransformer: {
title: titleTransformer
}
schema: {
title: 'string'
},
transformer: mockTransformer
}
}
})
Expand Down Expand Up @@ -234,7 +278,7 @@ describe('OramaManager', () => {

expect(insert).toHaveBeenCalledWith([{ id: 1, title: 'Test Entry' }])
expect(documentsTransformerSpy).toHaveBeenCalledWith(mockCollection.indexId, [{ id: 1, title: 'Test Entry' }])
expect(titleTransformer).not.toHaveBeenCalledWith('Test Entry')
expect(mockTransformer).not.toHaveBeenCalledWith('Test Entry')
})

it('should call documentsTransformer fn if declared in plugin config', async () => {
Expand All @@ -243,9 +287,8 @@ describe('OramaManager', () => {
if (string === 'plugin.orama-cloud.collectionSettings')
return {
[mockCollection.indexId]: {
documentsTransformer: {
title: titleTransformer
}
schema: {},
transformer: mockTransformer
}
}
})
Expand All @@ -259,7 +302,7 @@ describe('OramaManager', () => {
})

expect(documentsTransformerSpy).toHaveBeenCalledWith(mockCollection.indexId, [{ id: 1, title: 'Test Entry' }])
expect(titleTransformer).toHaveBeenCalledWith('Test Entry')
expect(mockTransformer).toHaveBeenCalledWith({ id: 1, title: 'Test Entry' })
expect(insert).toHaveBeenCalledWith([{ id: 1, title: 'TEST ENTRY' }])
})

Expand Down
1 change: 0 additions & 1 deletion utils/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { collection } = require('../server/content-types')
const getSelectedPropsFromObj = ({ props, obj }) => {
return props.reduce((acc, field) => {
if (field.includes('.')) {
Expand Down

0 comments on commit aff41a2

Please sign in to comment.