From 00d5b0d64002a11db8a4fa58560f9d1e6f9effc2 Mon Sep 17 00:00:00 2001 From: LokiSharp Date: Mon, 16 Oct 2023 21:24:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E7=B1=BB=20dbFindEx=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- storage/src/Database.ts | 38 ++++++++++++++++++---- storage/src/types.d.ts | 8 ++++- storage/tests/DataBase.test.ts | 59 ++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 7 deletions(-) diff --git a/storage/src/Database.ts b/storage/src/Database.ts index 3535708..d774025 100644 --- a/storage/src/Database.ts +++ b/storage/src/Database.ts @@ -94,12 +94,10 @@ export class DataBase { } public getRandomString(): string { - let val; - for ( - val = Math.floor(Math.random() * 0x10000).toString(16); - val.length < 4; - val = "0" + val - ); + let val = Math.floor(Math.random() * 0x10000).toString(16); + while (val.length < 4) { + val = "0" + val; + } return val; } @@ -281,4 +279,32 @@ export class DataBase { console.error(e); } } + + public dbFindEx( + collectionName: string, + query: Data, + opts: FindOps, + cb: CallBack, + ): void { + try { + this.recursReplaceNeNull(query); + const collection = this.getOrAddCollection(collectionName); + let chain = collection.chain().find(query); + if (opts.sort) { + for (const field in opts.sort) { + chain = chain.simplesort(field, opts.sort[field] == -1); + } + } + if (opts.offset) { + chain = chain.offset(opts.offset); + } + if (opts.limit) { + chain = chain.limit(opts.limit); + } + cb(null, chain.data()); + } catch (e) { + cb((e as Error).message); + console.error(e); + } + } } diff --git a/storage/src/types.d.ts b/storage/src/types.d.ts index 7b67374..fff48a5 100644 --- a/storage/src/types.d.ts +++ b/storage/src/types.d.ts @@ -13,7 +13,7 @@ type Data = { [propName: string]: DataType; }; -type CallBack = (message: string | null, obj?: object) => void; +type CallBack = (message: string | null, obj?: unknown) => void; type DataType = object | number | string | null | Data; @@ -22,3 +22,9 @@ type Method = "insert" | "find" | "findOne" | "count" | "removeWhere"; type BulkData = { op: BulkOp; id?: string; update?: Data; data?: Data }; type BulkOp = "update" | "insert" | "remove"; + +type FindOps = { + sort?: { [data: string]: number }; + offset?: number; + limit?: number; +}; diff --git a/storage/tests/DataBase.test.ts b/storage/tests/DataBase.test.ts index e60dee1..9f1d50f 100644 --- a/storage/tests/DataBase.test.ts +++ b/storage/tests/DataBase.test.ts @@ -223,4 +223,63 @@ describe("Database", () => { expect((result as Array).length).toBe(0); }); }); + + test("DataBase dbFindEx", () => { + db.dbRequest( + "users", + "insert", + [ + [ + { name: "Odin", type: "God" }, + { name: "Frigga", type: "God" }, + { name: "Thor", type: "God" }, + { name: "Sif", type: "God" }, + { name: "Loki", type: "God" }, + { name: "Baldur", type: "God" }, + { name: "Hodr", type: "God" }, + { name: "Forseti", type: "God" }, + { name: "Bragi", type: "God" }, + { name: "IĆ°unn", type: "God" }, + { name: "Njord", type: "God" }, + { name: "Freyr", type: "God" }, + { name: "Freyja", type: "God" }, + { name: "Tyr", type: "God" }, + { name: "Heimdall", type: "God" }, + ], + ], + (_, result) => expect(result).not.toBeUndefined(), + ); + db.dbFindEx( + "users", + { type: "God" }, + { sort: { name: 1 } }, + (_, result) => { + expect(result).not.toBeUndefined(); + expect((result as { name: string; type: string }[])[0].name).toBe( + "Baldur", + ); + expect((result as { name: string; type: string }[])[14].name).toBe( + "Tyr", + ); + }, + ); + db.dbFindEx( + "users", + { type: "God" }, + { sort: { name: 1 }, offset: 2 }, + (_, result) => { + expect(result).not.toBeUndefined(); + expect((result as []).length).toBe(13); + }, + ); + db.dbFindEx( + "users", + { type: "God" }, + { sort: { name: 1 }, limit: 2 }, + (_, result) => { + expect(result).not.toBeUndefined(); + expect((result as []).length).toBe(2); + }, + ); + }); });