-
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! feat: added strong typed orama schema
- Loading branch information
Showing
7 changed files
with
99 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,105 @@ | ||
import { create, insert } from '@orama/orama' | ||
import t from 'tap' | ||
import { create, insert } from '@orama/orama'; | ||
import t from 'tap'; | ||
import { | ||
afterInsert, | ||
loadWithHighlight, | ||
OramaWithHighlight, | ||
saveWithHighlight, | ||
searchWithHighlight | ||
} from '../src/index.js' | ||
afterInsert, | ||
loadWithHighlight, | ||
OramaWithHighlight, | ||
saveWithHighlight, | ||
searchWithHighlight, | ||
} from '../src/index.js'; | ||
|
||
t.test('it should store the position of tokens', async t => { | ||
const schema = { | ||
text: 'string' | ||
} as const | ||
const db = await create({ | ||
schema: { | ||
text: 'string', | ||
}, components: { afterInsert: (orama, key, doc) => afterInsert(orama, key) }, | ||
}); | ||
|
||
const db = (await create({ schema, components: { afterInsert } })) as OramaWithHighlight | ||
const id = await insert(db, { text: 'hello world' }); | ||
|
||
const id = await insert(db, { text: 'hello world' }) | ||
|
||
t.same(db.data.positions[id], { | ||
text: { hello: [{ start: 0, length: 5 }], world: [{ start: 6, length: 5 }] } | ||
}) | ||
}) | ||
t.same((db as OramaWithHighlight<typeof db>).data.positions[id], { | ||
text: { hello: [{ start: 0, length: 5 }], world: [{ start: 6, length: 5 }] }, | ||
}); | ||
}); | ||
|
||
t.test('it should manage nested schemas', async t => { | ||
const schema = { | ||
other: { | ||
text: 'string' | ||
} | ||
} as const | ||
const schema = { | ||
other: { | ||
text: 'string', | ||
}, | ||
} as const; | ||
|
||
const db = (await create({ schema, components: { afterInsert } })) as OramaWithHighlight | ||
const db = (await create({ schema, components: { afterInsert: (orama, key, doc) => afterInsert(orama, key) } })); | ||
|
||
const id = await insert(db, { other: { text: 'hello world' } }) | ||
const id = await insert(db, { other: { text: 'hello world' } }); | ||
|
||
t.same(db.data.positions[id], { | ||
'other.text': { hello: [{ start: 0, length: 5 }], world: [{ start: 6, length: 5 }] } | ||
}) | ||
}) | ||
t.same((db as OramaWithHighlight<typeof db>).data.positions[id], { | ||
'other.text': { hello: [{ start: 0, length: 5 }], world: [{ start: 6, length: 5 }] }, | ||
}); | ||
}); | ||
|
||
t.test("it shouldn't stem tokens", async t => { | ||
const schema = { | ||
text: 'string' | ||
} as const | ||
t.test('it shouldn\'t stem tokens', async t => { | ||
const schema = { | ||
text: 'string', | ||
} as const; | ||
|
||
const db = (await create({ | ||
schema, | ||
components: { afterInsert, tokenizer: { stemming: false } } | ||
})) as OramaWithHighlight | ||
const db = (await create({ | ||
schema, | ||
components: { afterInsert: (orama, key, doc) => afterInsert(orama, key), tokenizer: { stemming: false } }, | ||
})); | ||
|
||
const id = await insert(db, { text: 'hello personalization' }) | ||
const id = await insert(db, { text: 'hello personalization' }); | ||
|
||
t.same(db.data.positions[id], { | ||
text: { hello: [{ start: 0, length: 5 }], personalization: [{ start: 6, length: 15 }] } | ||
}) | ||
}) | ||
t.same((db as OramaWithHighlight<typeof db>).data.positions[id], { | ||
text: { hello: [{ start: 0, length: 5 }], personalization: [{ start: 6, length: 15 }] }, | ||
}); | ||
}); | ||
|
||
t.test('should retrieve positions', async t => { | ||
const schema = { | ||
text: 'string' | ||
} as const | ||
const schema = { | ||
text: 'string', | ||
} as const; | ||
|
||
const db = (await create({ schema, components: { afterInsert } })) as OramaWithHighlight | ||
const db = (await create({ schema, components: { afterInsert } })); | ||
|
||
await insert(db, { text: 'hello world' }) | ||
await insert(db, { text: 'hello world' }); | ||
|
||
const results = await searchWithHighlight(db, { term: 'hello' }) | ||
t.same(results.hits[0].positions, { text: { hello: [{ start: 0, length: 5 }] } }) | ||
}) | ||
const results = await searchWithHighlight(db, { term: 'hello' }); | ||
t.same(results.hits[0].positions, { text: { hello: [{ start: 0, length: 5 }] } }); | ||
}); | ||
|
||
t.test('should work with texts containing constructor and __proto__ properties', async t => { | ||
const schema = { | ||
text: 'string' | ||
} as const | ||
const schema = { | ||
text: 'string', | ||
} as const; | ||
|
||
const db = (await create({ schema, components: { afterInsert } })) as OramaWithHighlight | ||
const db = (await create({ schema, components: { afterInsert } })); | ||
|
||
await insert(db, { text: 'constructor __proto__' }) | ||
await insert(db, { text: 'constructor __proto__' }); | ||
|
||
const results = await searchWithHighlight(db, { term: 'constructor' }) | ||
const results = await searchWithHighlight(db, { term: 'constructor' }); | ||
|
||
t.same(results.hits[0].positions, { | ||
text: { constructor: [{ start: 0, length: 11 }] } | ||
}) | ||
}) | ||
t.same(results.hits[0].positions, { | ||
text: { constructor: [{ start: 0, length: 11 }] }, | ||
}); | ||
}); | ||
|
||
t.test('should correctly save and load data with positions', async t => { | ||
const schema = { | ||
text: 'string' | ||
} as const | ||
const schema = { | ||
text: 'string', | ||
} as const; | ||
|
||
const originalDB = (await create({ schema, components: { afterInsert } })) as OramaWithHighlight | ||
const originalDB = (await create({ schema, components: { afterInsert } })); | ||
|
||
const id = await insert(originalDB, { text: 'hello world' }) | ||
const id = await insert(originalDB, { text: 'hello world' }); | ||
|
||
const DBData = await saveWithHighlight(originalDB) | ||
const DBData = await saveWithHighlight(originalDB); | ||
|
||
const newDB = (await create({ schema, components: { afterInsert } })) as OramaWithHighlight | ||
const newDB = (await create({ schema, components: { afterInsert } })); | ||
|
||
await loadWithHighlight(newDB, DBData) | ||
await loadWithHighlight(newDB, DBData); | ||
|
||
t.same(newDB.data.positions[id], { | ||
text: { hello: [{ start: 0, length: 5 }], world: [{ start: 6, length: 5 }] } | ||
}) | ||
}) | ||
t.same((newDB as OramaWithHighlight<typeof newDB>).data.positions[id], { | ||
text: { hello: [{ start: 0, length: 5 }], world: [{ start: 6, length: 5 }] }, | ||
}); | ||
}); |