ライブデモを試す
npm、
yarn、を使用して Lyra をインストールできます
pnpm。
npm i @nearform/lyra
yarn add @nearform/lyra
pnpm add @nearform/lyra
Lyra の使い方は非常に簡単です。最初に行うことは、新しいデータベース インスタンスを作成し、インデックス スキーマを設定することです。
import { create, insert, search, remove } from '@nearform/lyra';
const db = create({
schema: {
author: 'string',
quote: 'string'
}
});
Lyra は文字列プロパティのみをインデックスに登録しますが、必要に応じて追加のデータを設定および保存できるようにします。
db インスタンスが作成されたら、いくつかのドキュメントの追加を開始できます。
insert(db, {
quote: 'It is during our darkest moments that we must focus to see the light.',
author: 'Aristotle'
});
insert(db, {
quote: 'If you really look closely, most overnight successes took a long time.',
author: 'Steve Jobs'
});
insert(db, {
quote: 'If you are not willing to risk the usual, you will have to settle for the ordinary.',
author: 'Jim Rohn'
});
insert(db, {
quote: 'You miss 100% of the shots you don\'t take',
author: 'Wayne Gretzky - Michael Scott'
});
データが挿入されたら、最終的にデータベースのクエリを開始できます。
const searchResult = search(db, {
term: 'if',
properties: '*'
});
上記の場合、単語 を含むすべてのドキュメントを検索し、すべての
ifスキーマ プロパティ (AKA インデックス)を検索します。
{
elapsed: 99, // elapsed time is in microseconds
hits: [
{
id: 'ckAOPGTA5qLXx0MgNr1Zy',
quote: 'If you really look closely, most overnight successes took a long time.',
author: 'Steve Jobs'
},
{
id: 'fyl-_1veP78IO-wszP86Z',
quote: 'If you are not willing to risk the usual, you will have to settle for the ordinary.',
author: 'Jim Rohn'
}
],
count: 2
}
検索を特定のプロパティに制限することもできます。
const searchResult = search(db, {
term: 'Michael',
properties: ['author']
});
結果:
{
elapsed: 111,
hits: [
{
id: 'L1tpqQxc0c2djrSN2a6TJ',
quote: "You miss 100% of the shots you don't take",
author: 'Wayne Gretzky - Michael Scott'
}
],
count: 1
}
必要に応じて、次の
removeメソッドを使用して特定のドキュメントを削除することもできます。
remove(db, 'L1tpqQxc0c2djrSN2a6TJ');
Lyra はApache 2.0ライセンスの下でライセンスされています。