core: update to mongodb5 (#514)

This commit is contained in:
undefined authored and GitHub committed 2023-02-27 18:31:24 +08:00
1 parent 5a02010120
commit 0685b88066
59 files changed
+610 -601

No files matched your search

+10 -5
View File
@@ -1,12 +1,19 @@
import { Cursor } from 'mongodb';
import { FindCursor } from 'mongodb';
import { ValidationError } from '../error';
import db from '../service/db';
async function paginate<T>(
cursor: Cursor<T>, page: number, pageSize: number,
cursor: FindCursor<T>, page: number, pageSize: number,
): Promise<[docs: T[], numPages: number, count: number]> {
if (page <= 0) throw new ValidationError('page');
let filter = {};
for (const key of Object.getOwnPropertySymbols(cursor)) {
if (key.toString() !== 'Symbol(filter)') continue;
filter = cursor[key];
break;
}
const [count, pageDocs] = await Promise.all([
cursor.count(),
db.collection(cursor.namespace.collection as any).countDocuments(filter),
cursor.skip((page - 1) * pageSize).limit(pageSize).toArray(),
]);
const numPages = Math.floor((count + pageSize - 1) / pageSize);
@@ -14,5 +21,3 @@ async function paginate<T>(
}
export = paginate;
global.Hydro.lib.paginate = paginate;
+2 -2
View File
@@ -1,6 +1,6 @@
import { Cursor } from 'mongodb';
import { FindCursor } from 'mongodb';
type ranked = <T extends Record<string, any>>(cursor: Cursor<T>, equ: ((a: T, b: T) => boolean)) => Promise<[number, T][]>;
type ranked = <T extends Record<string, any>>(cursor: FindCursor<T>, equ: ((a: T, b: T) => boolean)) => Promise<[number, T][]>;
const ranked: ranked = async (cursor, equ) => {
let last = null;
+8 -4
View File
@@ -2,7 +2,7 @@ import assert from 'assert';
import emojiRegex from 'emoji-regex';
import { isSafeInteger } from 'lodash';
import moment from 'moment-timezone';
import { ObjectID } from 'mongodb';
import { ObjectId } from 'mongodb';
import saslprep from 'saslprep';
type InputType = string | number | Record<string, any> | any[];
@@ -35,7 +35,7 @@ export interface Types {
Float: Type<number>;
// Other
ObjectID: Type<ObjectID>;
ObjectId: Type<ObjectId>;
Boolean: Type<boolean>;
Date: Type<string>;
Time: Type<string>;
@@ -94,8 +94,8 @@ export const Types: Types = {
PositiveInt: [(v) => +v, (v) => /^\+?[1-9][0-9]*$/.test(v.toString().trim()) && isSafeInteger(+v)],
Float: [(v) => +v, (v) => Number.isFinite(+v)],
ObjectID: [(v) => new ObjectID(v), ObjectID.isValid],
Boolean: [(v) => !!(v && !['false', 'off'].includes(v)), null, 'convert'],
ObjectId: [(v) => new ObjectId(v), ObjectId.isValid],
Boolean: [(v) => !!(v && !['false', 'off'].includes(v)), null, true],
Date: [
(v) => {
const d = v.split('-');
@@ -186,3 +186,7 @@ export const Types: Types = {
(v) => types.some((type) => type[1](v)),
] as any,
};
// @ts-ignore
Types.ObjectID = Types.ObjectId;
// backward compatibility