Skip to content

Commit

Permalink
refactor: use tegg orm client
Browse files Browse the repository at this point in the history
  • Loading branch information
elrrrrrrr committed Jan 28, 2023
1 parent 96bd8af commit 994cc43
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 47 deletions.
28 changes: 24 additions & 4 deletions app/repository/PackageRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AccessLevel, SingletonProto, Inject } from '@eggjs/tegg';
import { Orm } from '@eggjs/tegg-orm-plugin/lib/SingletonORM';
import { Package as PackageModel } from './model/Package';
import { Package as PackageEntity } from '../core/entity/Package';
import { ModelConvertor } from './util/ModelConvertor';
Expand All @@ -14,7 +15,8 @@ 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 { RawQueryUtil } from './util/RawQueryUtil';
import { EggAppConfig } from 'egg';
import { Bone } from 'leoric';

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

@Inject()
private readonly rawQueryUtil : RawQueryUtil;
private readonly config: EggAppConfig;

@Inject()
private readonly orm: Orm;

async findPackage(scope: string, name: string): Promise<PackageEntity | null> {
const model = await this.Package.findOne({ scope, name });
Expand Down Expand Up @@ -246,6 +250,20 @@ export class PackageRepository extends AbstractRepository {
return ModelConvertor.convertModelToEntity(model, this.PackageVersionManifest);
}

private getCountSql(model: typeof Bone):string {
const { database } = this.config.orm;
const sql = `
SELECT
TABLE_ROWS
FROM
information_schema.tables
WHERE
table_schema = '${database}'
AND table_name = '${model.table}'
`;
return sql;
}

public async queryTotal() {
const lastPkg = await this.Package.findOne().order('id', 'desc');
const lastVersion = await this.PackageVersion.findOne().order('id', 'desc');
Expand All @@ -258,7 +276,8 @@ 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.rawQueryUtil.getCount(PackageModel);
const queryRes = await this.orm.client.query(this.getCountSql(PackageModel));
packageCount = queryRes.rows?.[0].TABLE_ROWS as number;
}

if (lastVersion) {
Expand All @@ -267,7 +286,8 @@ export class PackageRepository extends AbstractRepository {
const fullname = pkg.scope ? `${pkg.scope}/${pkg.name}` : pkg.name;
lastPackageVersion = `${fullname}@${lastVersion.version}`;
}
packageVersionCount = await this.rawQueryUtil.getCount(PackageVersionModel);
const queryRes = await this.orm.client.query(this.getCountSql(PackageVersionModel));
packageVersionCount = queryRes.rows?.[0].TABLE_ROWS as number;
}
return {
packageCount,
Expand Down
43 changes: 0 additions & 43 deletions app/repository/util/RawQueryUtil.ts

This file was deleted.

0 comments on commit 994cc43

Please sign in to comment.