mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 09:46:28 +08:00
core: deprecate export =
This commit is contained in:
16 files changed
+73
-84
No files matched your search
@@ -1,9 +1,10 @@
|
||||
/* eslint-disable no-await-in-loop */
|
||||
import {
|
||||
Collection, Db, IndexDescription, MongoClient,
|
||||
Collection, Db, FindCursor, IndexDescription, MongoClient,
|
||||
} from 'mongodb';
|
||||
import mongoUri from 'mongodb-uri';
|
||||
import { Time } from '@hydrooj/utils';
|
||||
import { ValidationError } from '../error';
|
||||
import { Logger } from '../logger';
|
||||
import { load } from '../options';
|
||||
import * as bus from './bus';
|
||||
@@ -102,6 +103,44 @@ class MongoService {
|
||||
}
|
||||
}
|
||||
|
||||
async paginate<T>(
|
||||
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 coll = this.db.collection(cursor.namespace.collection as any);
|
||||
const [count, pageDocs] = await Promise.all([
|
||||
Object.keys(filter).length ? coll.count(filter) : coll.countDocuments(filter),
|
||||
cursor.skip((page - 1) * pageSize).limit(pageSize).toArray(),
|
||||
]);
|
||||
const numPages = Math.floor((count + pageSize - 1) / pageSize);
|
||||
return [pageDocs, numPages, count];
|
||||
}
|
||||
|
||||
async ranked<T extends Record<string, any>>(cursor: FindCursor<T>, equ: (a: T, b: T) => boolean): Promise<[number, T][]> {
|
||||
let last = null;
|
||||
let r = 0;
|
||||
let count = 0;
|
||||
const results = [];
|
||||
const docs = await cursor.toArray();
|
||||
for (const doc of docs) {
|
||||
if ((doc as any).unrank) {
|
||||
results.push([0, doc]);
|
||||
continue;
|
||||
}
|
||||
count++;
|
||||
if (!last || !equ(last, doc)) r = count;
|
||||
last = doc;
|
||||
results.push([r, doc]);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public async apply(ctx) {
|
||||
await this.start();
|
||||
ctx.on('dispose', () => this.client.close());
|
||||
|
||||
@@ -11,13 +11,13 @@ import { errorMessage, Time } from '@hydrooj/utils';
|
||||
import { Context } from '../context';
|
||||
import { PermissionError, PrivilegeError } from '../error';
|
||||
import type { DomainDoc } from '../interface';
|
||||
import paginate from '../lib/paginate';
|
||||
import { Logger } from '../logger';
|
||||
import { PERM, PRIV } from '../model/builtin';
|
||||
import * as opcount from '../model/opcount';
|
||||
import * as OplogModel from '../model/oplog';
|
||||
import * as system from '../model/system';
|
||||
import { builtinConfig } from '../settings';
|
||||
import db from './db';
|
||||
import baseLayer from './layers/base';
|
||||
import domainLayer from './layers/domain';
|
||||
import userLayer from './layers/user';
|
||||
@@ -228,7 +228,7 @@ export async function apply(ctx: Context) {
|
||||
return res;
|
||||
},
|
||||
paginate<T>(cursor: FindCursor<T>, page: number, key: string) {
|
||||
return paginate(cursor, page, this.ctx.setting.get(`pagination.${key}`));
|
||||
return db.paginate(cursor, page, this.ctx.setting.get(`pagination.${key}`));
|
||||
},
|
||||
checkPerm(...args: bigint[]) {
|
||||
if (!this.user.hasPerm(...args)) {
|
||||
|
||||
Reference in new issue
Block a user