Skip to content

Commit

Permalink
feat: adjust to support others properties
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsad committed Oct 16, 2023
1 parent 33a7e93 commit 58911b5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/JsonDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,11 @@ export class JsonDB {

/**
* Convert a router style path to a normal path
* By default propertyName to search is "id"
* @param path router based path to a correct base path
* @param propertyName name of the property to look for searchValue
*/
public async toPath(path: string ): Promise<string> {
public async toPath(path: string, propertyName:string = 'id' ): Promise<string> {

const [,...pathToQuery] = path.split("/")

Expand All @@ -441,7 +443,7 @@ export class JsonDB {

const pathValue = pathObject[pathKey]
try {
const pathIndex = await this.getIndex(normalPath.join(""), pathValue)
const pathIndex = await this.getIndex(normalPath.join(""), pathValue, propertyName)
normalPath.push(`[${pathIndex}]`)
} catch (error) {
throw new DataError(`DataPath: ${normalPath.join("")}/${pathValue} not found.`, 13, error)
Expand Down
40 changes: 40 additions & 0 deletions test/04-array-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,46 @@ describe('Array Utils', () => {

const normalPath = await db.toPath(routerPathStyle)

expect(normalPath).toEqual('/recipes[1]/nested[1]')
})
test('should convert a router style path to a normal path using other propertyName', async () => {
const recipe_1 = {
_id: '78687873783',
name: 'Gratin',
category: 'Dish',
}
const recipe_2 = {
_id: '65464646155',
name: 'Cheesecake',
category: 'Dessert',
nested: [
{
_id: '458445',
name: 'test-1',
},
{
_id: '88488',
name: 'test-2',
},
{
_id: '458455',
name: 'test-3',
},
],
}
const recipe_3 = {
_id: '12335373873',
name: 'Soupe',
category: 'Starter',
}
await db.push('/recipes[0]', recipe_1, true)
await db.push('/recipes[1]', recipe_2, true)
await db.push('/recipes[2]', recipe_3, true)

const routerPathStyle = '/recipes/65464646155/nested/88488'

const normalPath = await db.toPath(routerPathStyle, '_id')

expect(normalPath).toEqual('/recipes[1]/nested[1]')
})
})
Expand Down

0 comments on commit 58911b5

Please sign in to comment.