Skip to content

Commit

Permalink
feat: 实现数据库类 dbFindEx 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
LokiSharp committed Oct 16, 2023
1 parent f54be3d commit 00d5b0d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 7 deletions.
38 changes: 32 additions & 6 deletions storage/src/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
}
}
8 changes: 7 additions & 1 deletion storage/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

'message' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

'obj' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 16.x)

'message' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 16.x)

'obj' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'message' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'obj' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'message' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'obj' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, latest)

'message' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, latest)

'obj' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'message' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'obj' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20.x)

'message' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20.x)

'obj' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, latest)

'message' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, latest)

'obj' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'message' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'obj' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

'message' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

'obj' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20.x)

'message' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20.x)

'obj' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

'message' is defined but never used

Check warning on line 16 in storage/src/types.d.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

'obj' is defined but never used

type DataType = object | number | string | null | Data;

Expand All @@ -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;
};
59 changes: 59 additions & 0 deletions storage/tests/DataBase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,63 @@ describe("Database", () => {
expect((result as Array<unknown>).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);
},
);
});
});

0 comments on commit 00d5b0d

Please sign in to comment.