Skip to content

Commit

Permalink
fix: objects not being included in schema
Browse files Browse the repository at this point in the history
  • Loading branch information
raiindev committed Oct 23, 2024
1 parent 308ce44 commit 2df39d2
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 45 deletions.
8 changes: 4 additions & 4 deletions admin/src/components/SchemaMapper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
Typography
} from '@strapi/design-system'
import WarningIcon from '../WarningIcon'
import { getSchemaFromAttributes, getSelectedAttributesFromSchema } from '../../../../utils/schema'
import { getSelectedPropsFromObj, getSelectedAttributesFromSchema } from '../../../../utils/schema'

const isCollection = (value) => Array.isArray(value) && value.length > 0 && typeof value[0] === 'object'

Expand Down Expand Up @@ -63,9 +63,9 @@ const SchemaMapper = ({ collection, contentTypeSchema, onSchemaChange }) => {
})

React.useEffect(() => {
const schema = getSchemaFromAttributes({
attributes: selectedAttributes,
schema: contentTypeSchema
const schema = getSelectedPropsFromObj({
props: selectedAttributes,
obj: contentTypeSchema
})

onSchemaChange({ schema, searchableAttributes })
Expand Down
4 changes: 2 additions & 2 deletions admin/src/pages/PluginSettings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import pluginId from '../../pluginId'
import CollectionForm from '../../components/CollectionForm'

const isValidCollection = (collections, newCollection) => {
if (collections.some((col) => col.indexId === newCollection.indexId)) {
if (collections.filter((col) => col.indexId === newCollection.indexId).length > 1) {
return {
error: 'Index ID must be unique'
}
Expand Down Expand Up @@ -189,7 +189,7 @@ const HomePage = () => {
}

const handleUpdate = async () => {
const { error } = isValidCollection(currentCollection)
const { error } = isValidCollection(collections, currentCollection)
if (error) {
return onValidateError(error)
}
Expand Down
20 changes: 2 additions & 18 deletions server/services/orama-manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { CloudManager } = require('@oramacloud/client')
const { getSchemaFromEntryStructure } = require('../../utils/schema')
const { getSchemaFromEntryStructure, getSelectedPropsFromObj } = require('../../utils/schema')

class OramaManager {
constructor({ strapi }) {
Expand Down Expand Up @@ -53,19 +53,6 @@ class OramaManager {
return true
}

/*
* Filters out non-searchable attributes from an entry
* @param {Object} entry - Entry object
* */
filterOutNonSearchableAttributes(entry, searchableAttributes) {
return Object.entries(entry).reduce((acc, [key, value]) => {
if (searchableAttributes.includes(key)) {
acc[key] = value
}
return acc
}, {})
}

/*
* Transforms the documents before inserting them into the index
* The transformer function is defined in the collection settings
Expand Down Expand Up @@ -163,10 +150,7 @@ class OramaManager {
if (entries.length > 0) {
if (offset === 0) {
const transformedEntries = this.documentsTransformer(collection.indexId, entries)
const filteredEntry = this.filterOutNonSearchableAttributes(
transformedEntries[0],
collection.searchableAttributes
)
const filteredEntry = getSelectedPropsFromObj({ props: collection.searchableAttributes, obj: transformedEntries[0] })

await this.oramaUpdateSchema({
indexId: collection.indexId,
Expand Down
8 changes: 0 additions & 8 deletions server/services/orama-manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,6 @@ describe('OramaManager', () => {
})
})

describe('filterOutNonSearchableAttributes', () => {
it('should filter out non searchable attributes', () => {
const result = oramaManager.filterOutNonSearchableAttributes({ id: '2', title: 'hello' }, ['title'])

expect(result).toEqual({ title: 'hello' })
})
})

describe('documentsTransformer', () => {
it('should return entries if no transformer functions are found', () => {
const result = oramaManager.documentsTransformer('unknown', [{ id: 1, title: 'Test Entry' }])
Expand Down
10 changes: 5 additions & 5 deletions utils/schema.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const getSchemaFromAttributes = ({ attributes, schema }) => {
return attributes.reduce((acc, field) => {
const getSelectedPropsFromObj = ({ props, obj }) => {
return props.reduce((acc, field) => {
if (field.includes('.')) {
const [parent, child] = field.split('.')
if (!acc[parent]) {
acc[parent] = {}
}

acc[parent][child] = schema[parent][child]
acc[parent][child] = obj[parent][child]
} else {
acc[field] = schema[field]
acc[field] = obj[field]
}
return acc
}, {})
Expand Down Expand Up @@ -42,7 +42,7 @@ const getSchemaFromEntryStructure = (entry) => {
}

module.exports = {
getSchemaFromAttributes,
getSelectedPropsFromObj,
getSelectedAttributesFromSchema,
getSchemaFromEntryStructure
}
16 changes: 8 additions & 8 deletions utils/schema.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
const { getSchemaFromAttributes, getSelectedAttributesFromSchema, getSchemaFromEntryStructure } = require('./schema')
const { getSelectedPropsFromObj, getSelectedAttributesFromSchema, getSchemaFromEntryStructure } = require('./schema')

describe('Schema Utils', () => {
describe('getSchemaFromAttributes', () => {
describe('getSelectedPropsFromObj', () => {
it('should return the correct schema for flat attributes', () => {
const attributes = ['name', 'age']
const schema = {
const props = ['name', 'age']
const obj = {
name: { type: 'string' },
age: { type: 'number' },
address: { type: 'object' }
}
const result = getSchemaFromAttributes({ attributes, schema })
const result = getSelectedPropsFromObj({ props, obj })
expect(result).toEqual({
name: { type: 'string' },
age: { type: 'number' }
})
})

it('should return the correct schema for nested attributes', () => {
const attributes = ['address.city', 'address.zip']
const schema = {
const props = ['address.city', 'address.zip']
const obj = {
address: {
city: { type: 'string' },
zip: { type: 'number' },
street: { type: 'string' }
}
}
const result = getSchemaFromAttributes({ attributes, schema })
const result = getSelectedPropsFromObj({ props, obj })
expect(result).toEqual({
address: {
city: { type: 'string' },
Expand Down

0 comments on commit 2df39d2

Please sign in to comment.