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 #23 from rwl-dev/rename-file-format
Renamed file storage format
- Loading branch information
Showing
12 changed files
with
341 additions
and
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"deno.enable": true | ||
"deno.enable": true, | ||
"deno.inlayHints.parameterNames.enabled": "none" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { DatabaseFormat } from "../model.ts"; | ||
|
||
export const memoryFormat: DatabaseFormat = { | ||
add<T>(data: T[], object: T, keyword: keyof T) { | ||
const isDuplicate = | ||
data?.filter((item) => item[keyword] === object[keyword]).length; | ||
if (isDuplicate) return data; | ||
|
||
data?.push(object); | ||
return data; | ||
}, | ||
|
||
delete<T>(data: T[], key: keyof T, keyword: T[keyof T]) { | ||
return data?.filter((item) => item[key] !== keyword); | ||
}, | ||
|
||
find<T>(data: T[], key?: keyof T, keyword?: T[keyof T] | RegExp) { | ||
if (key && keyword instanceof RegExp) { | ||
return data?.filter((item) => keyword.test(`${item[key]}`)); | ||
} else if (key && keyword) { | ||
return data?.filter((item) => item[key] === keyword); | ||
} else return data; | ||
}, | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,8 @@ | ||
export { | ||
isExistFileSync, | ||
readFileSync, | ||
writeFile, | ||
writeFileSync, | ||
} from "https://pax.deno.dev/windchime-yk/[email protected]/file.ts"; | ||
|
||
export { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"; |
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,64 @@ | ||
import { | ||
isExistFileSync, | ||
readFileSync, | ||
writeFile, | ||
writeFileSync, | ||
} from "./deps.ts"; | ||
import { memoryFormat } from "./core/memory.ts"; | ||
import type { FileDBOption } from "./model.ts"; | ||
|
||
export class FileDB<T> { | ||
private readonly folder?: string; | ||
private readonly file: string; | ||
private data: T[]; | ||
|
||
constructor(option: FileDBOption) { | ||
const { folder = "./", filename = "main" } = option; | ||
|
||
this.folder = folder; | ||
this.file = `${folder}${ | ||
folder?.slice(-1) === "/" ? "" : "/" | ||
}${filename}.db`; | ||
this.data = []; | ||
|
||
if (this.folder && !isExistFileSync(this.folder)) { | ||
Deno.mkdirSync(this.folder, { recursive: true }); | ||
} | ||
if (this.folder && !isExistFileSync(this.file)) { | ||
writeFileSync(JSON.stringify(this.data), this.file); | ||
} | ||
if (isExistFileSync(this.file)) { | ||
const json: T[] = JSON.parse(readFileSync(this.file)); | ||
this.data = json; | ||
} | ||
} | ||
|
||
/** | ||
* Add an Object to the DB | ||
* @param object Object to be added to the DB | ||
* @param keyword Use the specified key to prevent duplication | ||
*/ | ||
async add(object: T, keyword: keyof T) { | ||
this.data = memoryFormat.add(this.data, object, keyword); | ||
await writeFile(JSON.stringify(this.data), this.file); | ||
} | ||
|
||
/** | ||
* Remove matching objects from DB | ||
* @param key The key of the Object you want to search | ||
* @param keyword Wording of search conditions | ||
*/ | ||
async delete(key: keyof T, keyword: T[keyof T]) { | ||
this.data = memoryFormat.delete(this.data, key, keyword); | ||
await writeFile(JSON.stringify(this.data), this.file); | ||
} | ||
|
||
/** | ||
* Searching the DB | ||
* @param key The key of the Object you want to search | ||
* @param keyword Wording of search conditions | ||
*/ | ||
find(key?: keyof T, keyword?: T[keyof T] | RegExp) { | ||
return memoryFormat.find(this.data, key, keyword); | ||
} | ||
} |
Oops, something went wrong.