This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from windchime-yk/add-partial-search
Implement partial search
- Loading branch information
Showing
6 changed files
with
203 additions
and
132 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
# Deno JSON DB | ||
【[日本語](./README_JP.md) / English】 | ||
|
||
A simple DB based on Deno. | ||
DB module in JSON format created by Deno. | ||
|
||
## Notes | ||
**This module is a work in progress.** | ||
We are working on a pilot implementation of a module like NeDB. | ||
|
||
If the feature you need is not present in the "Upcoming features", please let us know what you want in [Issue](https://github.com/windchime-yk/deno-simple-db/issues/new). | ||
If the feature you need is not present in the "Upcoming features", please let us know what you want in [Issue](https://github.com/windchime-yk/deno-json-db/issues/new). | ||
|
||
## Feature | ||
- Deno Modules | ||
|
@@ -20,65 +20,70 @@ If the feature you need is not present in the "Upcoming features", please let us | |
- [x] Return data that matches the condition | ||
- [x] Save the data as a file | ||
- [x] Asynchronous support | ||
- [ ] Partial search with regular expressions | ||
- [x] Partial search with regular expressions | ||
|
||
## API | ||
When creating a file, you must add `--allow-read` and `--allow-write` at execution to read and write the file. | ||
|
||
### Create DB | ||
The first argument is the DB type. If it is "file", it is a file; if it is "memory", it is managed in-memory. | ||
The second argument is the DB path. If it is "file", it will be an error if it is not written. | ||
`type` is the DB type. If it is "file", it is a file; if it is "memory", it is managed in-memory. | ||
`folder` is the DB path. Default path is project root. | ||
`filename` is the DB name. Default name is `main`. | ||
|
||
``` typescript | ||
import { SimpleDB } from 'https://github.com/windchime-yk/deno-simple-db/raw/master/mod.ts' | ||
import { JsonDB } from "https://github.com/windchime-yk/deno-json-db/raw/master/mod.ts"; | ||
|
||
interface DB { | ||
_id: string, | ||
name?: string | ||
name?: string; | ||
description?: string; | ||
} | ||
|
||
const db = new SimpleDB<DB>({ | ||
type: 'file', | ||
folder: './db/', | ||
}) | ||
const db = new JsonDB<DB>({ | ||
type: "file", | ||
folder: "./db/", | ||
filename: "test", | ||
}); | ||
``` | ||
|
||
### Add an Object to the DB | ||
The first argument is the Object to add to the DB. | ||
The second argument is the key used in the duplication prevention process. | ||
``` typescript | ||
import { v4 } from 'https://deno.land/[email protected]/uuid/mod.ts'; | ||
|
||
const test = { | ||
_id: v4.generate(), | ||
name: 'Asomaka Toika' | ||
} | ||
name: "Toika Asomaka", | ||
description: "A name that just popped into my head", | ||
}; | ||
|
||
await db.add(test, 'name') | ||
await db.add(test, "name"); | ||
``` | ||
|
||
### Remove matching objects from DB | ||
The first argument is the key, and the second argument is the value of the key. | ||
``` typescript | ||
await db.delete('name', 'Asomaka Toika') | ||
await db.delete("name", "Toika Asomaka"); | ||
``` | ||
|
||
### Searching the DB | ||
Perfect match and partial match supported. | ||
Returns an Object that matches the conditions, with the name of the key as the first argument and the value of the key as the second argument. | ||
No arguments return all DB data. | ||
``` typescript | ||
const data = await db.find('name', 'Asomaka Toika') | ||
const dataAll = await db.find() | ||
// Perfect match | ||
const data = await db.find("name", "Toika Asomaka"); | ||
// Partial match | ||
const dataPartial = await db.find("name", /Toika/); | ||
// All DB data | ||
const dataAll = await db.find(); | ||
``` | ||
|
||
### Test | ||
Execute the following command. | ||
``` bash | ||
$ git clone [email protected]:windchime-yk/deno-simple-db.git | ||
$ cd path/to/deno-simple-db | ||
$ git clone [email protected]:windchime-yk/deno-json-db.git | ||
$ cd path/to/deno-json-db | ||
|
||
# If there is no Denon | ||
$ deno run --allow-write --allow-read test.ts | ||
$ deno test --allow-write --allow-read | ||
# If you have a Denon | ||
$ denon test | ||
``` | ||
|
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,17 +1,17 @@ | ||
# Deno JSON DB | ||
【日本語 / [English](./README.md)】 | ||
|
||
Denoベースの簡単なDB。 | ||
Denoで作られたJSON形式のDBモジュール。 | ||
|
||
## 注意事項 | ||
**まだ開発中のモジュールです。** | ||
NeDBのようなモジュールを目指し、試験的に実装しています。 | ||
|
||
もし必要な機能が『今後追加される機能』になければ、[Issues](https://github.com/windchime-yk/deno-simple-db/issues/new)で教えてください。 | ||
もし必要な機能が『今後追加される機能』になければ、[Issues](https://github.com/windchime-yk/deno-json-db/issues/new)で教えてください。 | ||
|
||
## 特徴 | ||
- Denoモジュール | ||
- JSONベースの保存形式 | ||
- JSON形式でのデータ保存 | ||
- インメモリとファイルでのデータ保存に対応 | ||
|
||
## 今後追加される機能 | ||
|
@@ -20,67 +20,70 @@ NeDBのようなモジュールを目指し、試験的に実装しています | |
- [x] 条件に合致するデータを返す | ||
- [x] データをファイルとして保存 | ||
- [x] 非同期対応 | ||
- [ ] 正規表現による部分検索 | ||
- [x] 正規表現による部分検索 | ||
|
||
## API | ||
ファイルを作成する保存形式では、ファイルの読み込みと書き込みを行なうため、実行の際に`--allow-read`と`--allow-write`をつけてください。 | ||
|
||
### DBの作成 | ||
第1引数はDBの種類です。`file`ならファイル、`memory`ならインメモリで管理します。 | ||
第2引数はDBのパス。fileの場合は書かないとエラーになります。 | ||
`type`はDBの種類です。`file`ならファイル、`memory`ならインメモリで管理します。 | ||
`folder`はDBファイルを格納するフォルダのパスです。デフォルトではプロジェクトルートに生成されます。 | ||
`filename`はDBファイルの名前です。デフォルトは`main`です。 | ||
``` typescript | ||
import { SimpleDB } from 'https://github.com/windchime-yk/deno-simple-db/raw/master/mod.ts' | ||
import { JsonDB } from "https://github.com/windchime-yk/deno-json-db/raw/master/mod.ts"; | ||
|
||
interface DB { | ||
_id: string, | ||
name?: string | ||
name?: string; | ||
description?: string; | ||
} | ||
|
||
const db = new SimpleDB<DB>({ | ||
type: 'file', | ||
folder: './db/', | ||
}) | ||
const db = new JsonDB<DB>({ | ||
type: "file", | ||
folder: "./db/", | ||
filename: "test", | ||
}); | ||
``` | ||
|
||
### データの追加 | ||
第1引数はDBに追加するObject、第2引数は重複防止処理で利用するkeyです。 | ||
以下の例の場合、`name`の値が重複すると追加されません。なお、現状は無警告で重複を弾きます。 | ||
|
||
``` typescript | ||
import { v4 } from 'https://deno.land/[email protected]/uuid/mod.ts'; | ||
|
||
const test = { | ||
_id: v4.generate(), | ||
name: 'あそまか といか' | ||
} | ||
name: "あそまか といか", | ||
description: "ふと思い浮かんだ名前", | ||
}; | ||
|
||
await db.add(test, 'name') | ||
await db.add(test, "name"); | ||
``` | ||
|
||
### データの削除 | ||
DBから該当するObjectを削除します。 | ||
第1引数はkey、第2引数はその値です。 | ||
``` typescript | ||
await db.delete('name', 'あそまか といか') | ||
await db.delete("name", "あそまか といか"); | ||
``` | ||
|
||
### データの検索 | ||
現状は、完全一致のみに対応しています。 | ||
完全一致と正規表現による部分一致に対応しています。 | ||
第1引数にkeyの名前、第2引数にkeyの値を指定し、該当するObjectを返します。 | ||
引数なしは、DBデータすべてを返します。 | ||
``` typescript | ||
const data = await db.find('name', 'あそまか といか') | ||
const dataAll = await db.find() | ||
// 完全一致 | ||
const data = await db.find("name", "あそまか といか"); | ||
// 部分検索 | ||
const dataPartial = await db.find("name", /といか/); | ||
// すべてのDBデータ | ||
const dataAll = await db.find(); | ||
``` | ||
|
||
## テスト | ||
以下のコマンドを実行してください。 | ||
``` bash | ||
$ git clone [email protected]:windchime-yk/deno-simple-db.git | ||
$ cd path/to/deno-simple-db | ||
$ git clone [email protected]:windchime-yk/deno-json-db.git | ||
$ cd path/to/deno-json-db | ||
|
||
# Denonがない場合 | ||
$ deno run --allow-write --allow-read test.ts | ||
$ deno test --allow-write --allow-read | ||
# Denonがある場合 | ||
$ denon test | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; | ||
import { JsonDB } from "./mod.ts"; | ||
|
||
interface Test { | ||
name?: string; | ||
description?: string; | ||
show: boolean; | ||
} | ||
|
||
const dataList: Test[] = [ | ||
{ | ||
name: "Toika Asomaka", | ||
description: "A name that just popped into my head", | ||
show: true, | ||
}, | ||
{ | ||
name: "Momotaro", | ||
description: "Japanese demon slayer", | ||
show: false, | ||
}, | ||
{ | ||
name: "Kintaro", | ||
description: "Sumo with a bear", | ||
show: true, | ||
}, | ||
{ | ||
name: "Urashimataro", | ||
description: "The person left behind", | ||
show: false, | ||
}, | ||
]; | ||
|
||
const folder = "./db/store"; | ||
const filename = "test"; | ||
const addDb = async (db: JsonDB<Test>): Promise<void> => { | ||
for (const item of dataList) { | ||
await db.add( | ||
{ | ||
name: item.name, | ||
description: item.description, | ||
show: item.show, | ||
}, | ||
"name" | ||
); | ||
} | ||
}; | ||
const deleteDbFile = async (): Promise<void> => { | ||
await Deno.remove(`${folder}/${filename}.db`); | ||
}; | ||
|
||
Deno.test( | ||
"Add item", | ||
async (): Promise<void> => { | ||
try { | ||
const db = new JsonDB<Test>({ | ||
type: "file", | ||
folder, | ||
filename, | ||
}); | ||
await addDb(db); | ||
assertEquals(await db.find(), dataList); | ||
} finally { | ||
await deleteDbFile(); | ||
} | ||
} | ||
); | ||
|
||
Deno.test( | ||
"Delete item", | ||
async (): Promise<void> => { | ||
try { | ||
const db = new JsonDB<Test>({ | ||
type: "file", | ||
folder, | ||
filename, | ||
}); | ||
await addDb(db); | ||
await db.delete("name", "Kintaro"); | ||
const dataDeleteList = dataList.filter((item) => item.name !== "Kintaro"); | ||
assertEquals(await db.find(), dataDeleteList); | ||
} finally { | ||
await deleteDbFile(); | ||
} | ||
} | ||
); | ||
|
||
Deno.test( | ||
"Find item", | ||
async (): Promise<void> => { | ||
try { | ||
const db = new JsonDB<Test>({ | ||
type: "file", | ||
folder, | ||
filename, | ||
}); | ||
await addDb(db); | ||
const data = await db.find("name", "Kintaro"); | ||
const dataFindList = dataList.filter((item) => item.name === "Kintaro"); | ||
assertEquals(data, dataFindList); | ||
} finally { | ||
await deleteDbFile(); | ||
} | ||
} | ||
); | ||
|
||
Deno.test( | ||
"Partial find item", | ||
async (): Promise<void> => { | ||
try { | ||
const db = new JsonDB<Test>({ | ||
type: "file", | ||
folder, | ||
filename, | ||
}); | ||
await addDb(db); | ||
const data = await db.find("name", /taro/); | ||
const dataFindList = dataList.filter((item) => | ||
/taro/.test(item.name || "") | ||
); | ||
console.log(data); | ||
console.log(dataFindList); | ||
assertEquals(data, dataFindList); | ||
} finally { | ||
await deleteDbFile(); | ||
} | ||
} | ||
); |
Oops, something went wrong.