Skip to content

Commit

Permalink
feat: impl egg-bin dal gen (#257)
Browse files Browse the repository at this point in the history
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide:
https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md

感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide:
https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to
[x]. -->

- [x] `npm test` passes
- [x] tests and/or benchmarks are included
- [x] documentation is changed or added
- [x] commit message follows commit guidelines

##### Affected core subsystem(s)
<!-- Provide affected core subsystem(s). -->


##### Description of change
<!-- Provide a description of the change below this comment. -->


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced a new command for generating data access layer (DAL) code,
enhancing the app's data management capabilities.
- **Documentation**
- Updated README with instructions on generating DAL code and added a
reference to further documentation.
- **Chores**
	- Updated `.gitignore` to exclude `.eslintcache` files.
- **Tests**
	- Added tests for the new DAL generation functionality.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
killagu committed Mar 22, 2024
1 parent cff838f commit 7195aef
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -32,3 +32,4 @@ package-lock.json
yarn.lock
.c8_output
.idea
.eslintcache
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -334,6 +334,16 @@ $ my-egg-bin nsp
run nsp check at /foo/bar with {}
```

### dal

Generate code for @eggjs/tegg-dal-plugin

```bash
egg-bin dal gen
```

dal document please read [tegg doc](https://github.com/eggjs/tegg/tree/master/plugin/dal).

## License

[MIT](LICENSE)
Expand Down
17 changes: 17 additions & 0 deletions lib/cmd/dal.js
@@ -0,0 +1,17 @@
'use strict';

const Command = require('../command');
const path = require('node:path');

class DalCommand extends Command {
constructor(rawArgv) {
super(rawArgv);
this.load(path.join(__dirname, 'dal'));
}

get description() {
return '生成 dal DAO、extensions、structure 代码';
}
}

module.exports = DalCommand;
39 changes: 39 additions & 0 deletions lib/cmd/dal/gen.js
@@ -0,0 +1,39 @@
const path = require('node:path');
const { ModuleConfigUtil } = require('@eggjs/tegg-common-util');
const Command = require('../../command');

class DalGenCommand extends Command {
constructor(rawArgv) {
super(rawArgv);
this.usage = 'Usage: egg-bin dal gen';

this.options = {
baseDir: {
description: 'directory of application, default to `process.cwd()`',
type: 'string',
},
};
this.genBin = path.join(__dirname, '../../dal-gen');
}

async run(context) {
const { cwd, argv } = context;
const baseDir = argv.baseDir || cwd;

const options = {
execArgv: context.execArgv,
env: context.env,
};

const moduleReferences = ModuleConfigUtil.readModuleReference(baseDir, {});
console.log('[egg-bin] dal gen get modules %j', moduleReferences);
for (const moduleReference of moduleReferences) {
await this.helper.forkNode(this.genBin, [
moduleReference.path,
moduleReference.name,
], options);
}
}
}

module.exports = DalGenCommand;
36 changes: 36 additions & 0 deletions lib/dal-gen.js
@@ -0,0 +1,36 @@
const assert = require('node:assert');
const { TableModel, TableInfoUtil } = require('@eggjs/dal-decorator');
const { CodeGenerator } = require('@eggjs/dal-runtime');
const { LoaderFactory } = require('@eggjs/tegg-loader');

const moduleDir = process.argv[2];
assert(moduleDir, 'miss module dir');

const moduleName = process.argv[3];
assert(moduleName, 'miss module name');

(async () => {
try {
console.log('[egg-bin] start dal gen for %s', moduleName);
const generator = new CodeGenerator({
moduleDir,
moduleName,
});
const loader = LoaderFactory.createLoader(moduleDir, 'MODULE');
const clazzList = loader.load();
for (const clazz of clazzList) {
if (TableInfoUtil.getIsTable(clazz)) {
const tableModel = TableModel.build(clazz);
console.log('[egg-bin] generate code for %s', clazz.name);
await generator.generate(tableModel);
}
}
console.log('[egg-bin] dal generate done');
process.exit(0);
} catch (e) {
e.message = `[egg-bin] generate dal code ${moduleDir} failed: ` + e.message;
console.error(e);
process.exit(1);
}
})();

9 changes: 7 additions & 2 deletions package.json
Expand Up @@ -33,7 +33,12 @@
"test": "^3.0.0",
"ts-node": "^10.8.0",
"tsconfig-paths": "^4.1.1",
"ypkgfiles": "^1.6.0"
"ypkgfiles": "^1.6.0",
"@eggjs/tegg-common-util": "^3.33.0",
"@eggjs/dal-runtime": "^3.33.0",
"@eggjs/dal-decorator": "^3.33.0",
"@eggjs/tegg-loader": "^3.33.0",
"@eggjs/tegg": "^3.33.0"
},
"peerDependencies": {
"egg-mock": ">=5.8.3"
Expand All @@ -56,7 +61,7 @@
"eslint-config-egg": "^12.0.0",
"git-contributor": "2",
"mm": "^3.2.0",
"typescript": "^4.7.2"
"typescript": "^5.0.4"
},
"repository": {
"type": "git",
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/dal/app/modules/dal/Bar.ts
@@ -0,0 +1,19 @@
import { Table, Column, ColumnType } from '@eggjs/tegg/dal';

@Table({
comment: 'foo table',
})
export class Bar {
@Column({
type: ColumnType.INT,
}, {
primaryKey: true,
})
id: number;

@Column({
type: ColumnType.VARCHAR,
length: 100,
})
name: string;
}
23 changes: 23 additions & 0 deletions test/fixtures/dal/app/modules/dal/Foo.ts
@@ -0,0 +1,23 @@
import { Table, Index, Column, ColumnType, IndexType } from '@eggjs/tegg/dal';

@Table({
comment: 'foo table',
})
@Index({
keys: [ 'name' ],
type: IndexType.UNIQUE,
})
export class Foo {
@Column({
type: ColumnType.INT,
}, {
primaryKey: true,
})
id: number;

@Column({
type: ColumnType.VARCHAR,
length: 100,
})
name: string;
}
6 changes: 6 additions & 0 deletions test/fixtures/dal/app/modules/dal/package.json
@@ -0,0 +1,6 @@
{
"name": "dal",
"eggModule": {
"name": "dal"
}
}
10 changes: 10 additions & 0 deletions test/fixtures/dal/package.json
@@ -0,0 +1,10 @@
{
"name": "dal",
"egg": {
"typescript": true
},
"repository": "git@github.com:eggjs/egg-bin.git",
"devDependencies": {
"@eggjs/tsconfig": "^1.3.3"
}
}
6 changes: 6 additions & 0 deletions test/fixtures/dal/tsconfig.json
@@ -0,0 +1,6 @@
{
"extends": "@eggjs/tsconfig",
"compilerOptions": {
"baseUrl": "."
}
}
42 changes: 42 additions & 0 deletions test/lib/cmd/dal.test.js
@@ -0,0 +1,42 @@
const path = require('node:path');
const coffee = require('coffee');
const mm = require('mm');
const fs = require('node:fs/promises');
const assert = require('assert');

describe('test/lib/cmd/dal.test.js', () => {
const eggBin = require.resolve('../../../bin/egg-bin.js');
const cwd = path.join(__dirname, '../../fixtures/dal');

afterEach(mm.restore);

describe('egg-bin dal gen', () => {
after(async () => {
await fs.rm(path.join(cwd, 'app/modules/dal/dal'), {
recursive: true,
});
});

it('egg-bin dal gen should work', async () => {
await coffee.fork(eggBin, [ 'dal', 'gen' ], { cwd })
.debug()
.expect('code', 0)
.end();

for (const file of [
'app/modules/dal/dal/dao/BarDAO.ts',
'app/modules/dal/dal/dao/FooDAO.ts',
'app/modules/dal/dal/dao/base/BaseBarDAO.ts',
'app/modules/dal/dal/dao/base/BaseFooDAO.ts',
'app/modules/dal/dal/extension/BarExtension.ts',
'app/modules/dal/dal/extension/FooExtension.ts',
'app/modules/dal/dal/structure/Bar.json',
'app/modules/dal/dal/structure/Bar.sql',
'app/modules/dal/dal/structure/Foo.json',
'app/modules/dal/dal/structure/Foo.sql',
]) {
assert.ok(fs.stat(path.join(cwd, file)));
}
});
});
});

0 comments on commit 7195aef

Please sign in to comment.