Skip to content

Commit

Permalink
refactor: use information_schema
Browse files Browse the repository at this point in the history
  • Loading branch information
elrrrrrrr committed Jan 21, 2023
1 parent 954ae40 commit e3348c6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
25 changes: 21 additions & 4 deletions app/repository/PackageRepository.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AccessLevel, SingletonProto, Inject } from '@eggjs/tegg';
import type { Package as PackageModel } from './model/Package';
import { Package as PackageModel } from './model/Package';
import { Package as PackageEntity } from '../core/entity/Package';
import { ModelConvertor } from './util/ModelConvertor';
import { PackageVersion as PackageVersionEntity } from '../core/entity/PackageVersion';
import type { PackageVersion as PackageVersionModel } from './model/PackageVersion';
import { PackageVersion as PackageVersionModel } from './model/PackageVersion';
import { PackageVersionManifest as PackageVersionManifestEntity } from '../core/entity/PackageVersionManifest';
import type { PackageVersionManifest as PackageVersionManifestModel } from './model/PackageVersionManifest';
import type { Dist as DistModel } from './model/Dist';
Expand All @@ -14,6 +14,9 @@ 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';

@SingletonProto({
accessLevel: AccessLevel.PUBLIC,
Expand All @@ -40,6 +43,12 @@ export class PackageRepository extends AbstractRepository {
@Inject()
private readonly User: typeof UserModel;

@Inject()
private readonly leoricRegister: LeoricRegister;

@Inject()
private readonly config: EggAppConfig;

async findPackage(scope: string, name: string): Promise<PackageEntity | null> {
const model = await this.Package.findOne({ scope, name });
if (!model) return null;
Expand Down Expand Up @@ -253,7 +262,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.Package.find().count();
packageCount = await this.getCount(PackageModel);
}

if (lastVersion) {
Expand All @@ -262,7 +271,7 @@ export class PackageRepository extends AbstractRepository {
const fullname = pkg.scope ? `${pkg.scope}/${pkg.name}` : pkg.name;
lastPackageVersion = `${fullname}@${lastVersion.version}`;
}
packageVersionCount = await this.PackageVersion.find().count();
packageVersionCount = await this.getCount(PackageVersionModel);
}
return {
packageCount,
Expand Down Expand Up @@ -328,4 +337,12 @@ 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 test/repository/PackageRepository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import assert from 'assert';
import { app } from 'egg-mock/bootstrap';
import { PackageRepository } from 'app/repository/PackageRepository';
import { PackageManagerService } from 'app/core/service/PackageManagerService';
import { UserService } from 'app/core/service/UserService';

describe('test/repository/PackageRepository.test.ts', () => {
let packageRepository: PackageRepository;
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);
const { user } = await userService.create({
name: 'test-user',
password: 'this-is-password',
email: 'hello@example.com',
ip: '127.0.0.1',
});
await packageManagerService.publish({
dist: {
content: Buffer.alloc(0),
},
tag: '',
scope: '',
name: 'foo',
description: 'foo description',
packageJson: {},
readme: '',
version: '1.0.0',
isPrivate: true,
}, user);
});
it('should work', async () => {
const res = await packageRepository.queryTotal();
assert(res.packageCount === 1);
assert(res.packageVersionCount === 1);
});
});
});

0 comments on commit e3348c6

Please sign in to comment.