Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
BaseRepository
✓ import { BaseRepository } from 'egg-orm-mysql';
✗ const { BaseRepository } = require('egg-orm-mysql');
ESM import required if using TypeScript or modern Node. CommonJS require may not work if package is ESM-only.
BaseModel
✓ import { BaseModel } from 'egg-orm-mysql';
No common mistake for this import; ensure correct spelling.
repository
✓ import { repository } from 'egg-orm-mysql';
✗ import { Repository } from 'egg-orm-mysql';
The decorator is lowercase 'repository'. Using 'Repository' will result in undefined.
column
✓ import { column } from 'egg-orm-mysql';
✗ import { Column } from 'egg-orm-mysql';
Case-sensitive; the decorator is lowercase 'column'.
manyToOne
✓ import { manyToOne } from 'egg-orm-mysql';
Rarely misused; ensure correct import path.
Demonstrates setting up a model with decorators, a repository class, and a service using dependency injection.
import { Application } from 'egg';
import { BaseRepository, BaseModel, repository, column, manyToOne } from 'egg-orm-mysql';
import { context, lazyInject } from 'egg-typed-di';
// Define a model
class ActivityModel extends BaseModel {
@column()
id: number;
@manyToOne('owner', UserModel, 'getByUsername')
user: Promise<UserModel>;
}
// Define a repository
@repository(ActivityModel)
class ActivityRepository extends BaseRepository<ActivityModel> {
async findByName(name: string) {
return await this.getOne({ where: { name } });
}
}
// Use in a service
@context()
class ActivityService {
@lazyInject()
private activityRepository: ActivityRepository;
async getActivity(id: number) {
return await this.activityRepository.getById(id);
}
}
export default (app: Application) => {
app.get('/activity/:id', async (ctx) => {
const service = await ctx.getContextAsync(ActivityService);
ctx.body = await service.getActivity(parseInt(ctx.params.id, 10));
});
};
Errors
Common errors & fixes
Error: Cannot find module 'egg-typed-di'
Required peer dependency not installed.
fixRun: npm install egg-typed-di --save
TypeError: Class extends value undefined is not a constructor or null
Missing or incorrect import of BaseModel/BaseRepository.
fixEnsure you import BaseModel and BaseRepository from 'egg-orm-mysql'.
Cannot read property 'getById' of undefined
Repository not injected (missing @lazyInject in service or domain).
fixAdd @lazyInject() decorator to the repository property in the service/domain class.
Decorators are not valid here
Decorator syntax used without enabling experimentalDecorators in tsconfig.json.
fixAdd 'experimentalDecorators': true to tsconfig.json under compilerOptions.
Audit
Dependencies
egg-typed-direquiredProvides the dependency injection framework (decorators like @context, @lazyInject) used by this ORM.