Skip to content

Commit

Permalink
refactor: add raw query util
Browse files Browse the repository at this point in the history
  • Loading branch information
elrrrrrrr committed Jan 21, 2023
1 parent e3348c6 commit 3932081
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
19 changes: 4 additions & 15 deletions app/repository/PackageRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import type { Maintainer as MaintainerModel } from './model/Maintainer';
import type { User as UserModel } from './model/User';
import { User as UserEntity } from '../core/entity/User';
import { AbstractRepository } from './AbstractRepository';
import { LeoricRegister } from '@eggjs/tegg-orm-plugin/lib/LeoricRegister';
import { Bone } from 'leoric';
import { EggAppConfig } from 'egg';
import { RawQueryUtil } from './util/RawQueryUtil';

@SingletonProto({
accessLevel: AccessLevel.PUBLIC,
Expand Down Expand Up @@ -44,10 +42,8 @@ export class PackageRepository extends AbstractRepository {
private readonly User: typeof UserModel;

@Inject()
private readonly leoricRegister: LeoricRegister;
private readonly rawQueryUtil : RawQueryUtil;

@Inject()
private readonly config: EggAppConfig;

async findPackage(scope: string, name: string): Promise<PackageEntity | null> {
const model = await this.Package.findOne({ scope, name });
Expand Down Expand Up @@ -262,7 +258,7 @@ export class PackageRepository extends AbstractRepository {
lastPackage = lastPkg.scope ? `${lastPkg.scope}/${lastPkg.name}` : lastPkg.name;
// FIXME: id will be out of range number
// 可能存在 id 增长不连续的情况,通过 count 查询
packageCount = await this.getCount(PackageModel);
packageCount = await this.rawQueryUtil.getCount(PackageModel);
}

if (lastVersion) {
Expand All @@ -271,7 +267,7 @@ export class PackageRepository extends AbstractRepository {
const fullname = pkg.scope ? `${pkg.scope}/${pkg.name}` : pkg.name;
lastPackageVersion = `${fullname}@${lastVersion.version}`;
}
packageVersionCount = await this.getCount(PackageVersionModel);
packageVersionCount = await this.rawQueryUtil.getCount(PackageVersionModel);
}
return {
packageCount,
Expand Down Expand Up @@ -338,11 +334,4 @@ export class PackageRepository extends AbstractRepository {
return entities;
}

async getCount(model: typeof Bone): Promise<number> {
const real = await this.leoricRegister.getOrCreateRealm(undefined);
const { database } = this.config.orm;
const sql = `select table_rows from information_schema.tables where table_schema='${database}' and table_name = '${model.table}'`;
const queryRes = await real.query(sql);
return queryRes?.rows?.[0]?.TABLE_ROWS || 0;
}
}
43 changes: 43 additions & 0 deletions app/repository/util/RawQueryUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
AccessLevel,
EggObjectLifecycle,
Inject,
SingletonProto,
} from '@eggjs/tegg';
import { LeoricRegister } from '@eggjs/tegg-orm-plugin/lib/LeoricRegister';
import { EggAppConfig } from 'egg';
import Realm, { Bone } from 'leoric';

@SingletonProto({
name: 'rawQueryUtil',
accessLevel: AccessLevel.PUBLIC,
})
export class RawQueryUtil implements EggObjectLifecycle {
@Inject()
private leoricRegister: LeoricRegister;

@Inject()
private config: EggAppConfig;

private client: Realm;

async init() {
this.client = await this.leoricRegister.getOrCreateRealm(undefined);
}

public async getCount(model: typeof Bone): Promise<number> {
const { database } = this.config.orm;
const sql = `
SELECT
table_rows
FROM
information_schema.tables
WHERE
table_schema = '${database}'
AND table_name = '${model.table}'
`;
const queryRes = await this.client.query(sql);
return queryRes.rows?.[0]?.TABLE_ROWS as number || 0;
}

}
13 changes: 8 additions & 5 deletions test/repository/PackageRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import { UserService } from 'app/core/service/UserService';

describe('test/repository/PackageRepository.test.ts', () => {
let packageRepository: PackageRepository;
let packageManagerService: PackageManagerService
let packageManagerService: PackageManagerService;
let userService: UserService;

describe('getCount', () => {
beforeEach(async () => {
packageRepository = await app.getEggObject(PackageRepository);
packageManagerService = await app.getEggObject(PackageManagerService);
userService = await app.getEggObject(UserService);

});
it('should work', async () => {
const { packageCount, packageVersionCount } = await packageRepository.queryTotal();
const { user } = await userService.create({
name: 'test-user',
password: 'this-is-password',
Expand All @@ -33,11 +37,10 @@ describe('test/repository/PackageRepository.test.ts', () => {
version: '1.0.0',
isPrivate: true,
}, user);
});
it('should work', async () => {
const res = await packageRepository.queryTotal();
assert(res.packageCount === 1);
assert(res.packageVersionCount === 1);
// information_schema 只能返回大概值,仅验证增加
assert(res.packageCount > packageCount);
assert(res.packageVersionCount > packageVersionCount);
});
});
});

0 comments on commit 3932081

Please sign in to comment.