Skip to content

Commit e1ab433

Browse files
committed
feat: impl egg-bin dal gen
1 parent cff838f commit e1ab433

File tree

12 files changed

+216
-2
lines changed

12 files changed

+216
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ package-lock.json
3232
yarn.lock
3333
.c8_output
3434
.idea
35+
.eslintcache

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,16 @@ $ my-egg-bin nsp
334334
run nsp check at /foo/bar with {}
335335
```
336336

337+
### dal
338+
339+
Generate code for @eggjs/tegg-dal-plugin
340+
341+
```bash
342+
egg-bin dal gen
343+
```
344+
345+
dal document please read [tegg doc](https://github.com/eggjs/tegg/tree/master/plugin/dal).
346+
337347
## License
338348

339349
[MIT](LICENSE)

lib/cmd/dal.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const Command = require('../command');
4+
const path = require('node:path');
5+
6+
class DalCommand extends Command {
7+
constructor(rawArgv) {
8+
super(rawArgv);
9+
this.load(path.join(__dirname, 'dal'));
10+
}
11+
12+
get description() {
13+
return '生成 dal DAO、extensions、structure 代码';
14+
}
15+
}
16+
17+
module.exports = DalCommand;

lib/cmd/dal/gen.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const path = require('node:path');
2+
const { ModuleConfigUtil } = require('@eggjs/tegg-common-util');
3+
const Command = require('../../command');
4+
5+
class DalGenCommand extends Command {
6+
constructor(rawArgv) {
7+
super(rawArgv);
8+
this.usage = 'Usage: egg-bin dal gen';
9+
10+
this.options = {
11+
baseDir: {
12+
description: 'directory of application, default to `process.cwd()`',
13+
type: 'string',
14+
},
15+
};
16+
this.genBin = path.join(__dirname, '../../dal-gen');
17+
}
18+
19+
async run(context) {
20+
const { cwd, argv } = context;
21+
const baseDir = argv.baseDir || cwd;
22+
23+
const options = {
24+
execArgv: context.execArgv,
25+
env: context.env,
26+
};
27+
28+
const moduleReferences = ModuleConfigUtil.readModuleReference(baseDir, {});
29+
console.log('[egg-bin] dal gen get modules %j', moduleReferences);
30+
for (const moduleReference of moduleReferences) {
31+
await this.helper.forkNode(this.genBin, [
32+
moduleReference.path,
33+
moduleReference.name,
34+
], options);
35+
}
36+
}
37+
}
38+
39+
module.exports = DalGenCommand;

lib/dal-gen.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const assert = require('node:assert');
2+
const { TableModel, TableInfoUtil } = require('@eggjs/dal-decorator');
3+
const { CodeGenerator } = require('@eggjs/dal-runtime');
4+
const { LoaderFactory } = require('@eggjs/tegg-loader');
5+
6+
const moduleDir = process.argv[2];
7+
assert(moduleDir, 'miss module dir');
8+
9+
const moduleName = process.argv[3];
10+
assert(moduleName, 'miss module name');
11+
12+
(async () => {
13+
try {
14+
console.log('[egg-bin] start dal gen for %s', moduleName);
15+
const generator = new CodeGenerator({
16+
moduleDir,
17+
moduleName,
18+
});
19+
const loader = LoaderFactory.createLoader(moduleDir, 'MODULE');
20+
const clazzList = loader.load();
21+
for (const clazz of clazzList) {
22+
if (TableInfoUtil.getIsTable(clazz)) {
23+
const tableModel = TableModel.build(clazz);
24+
console.log('[egg-bin] generate code for %s', clazz.name);
25+
await generator.generate(tableModel);
26+
}
27+
}
28+
console.log('[egg-bin] dal generate done');
29+
process.exit(0);
30+
} catch (e) {
31+
e.message = `[egg-bin] generate dal code ${moduleDir} failed: ` + e.message;
32+
console.error(e);
33+
process.exit(1);
34+
}
35+
})();
36+

package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@
3333
"test": "^3.0.0",
3434
"ts-node": "^10.8.0",
3535
"tsconfig-paths": "^4.1.1",
36-
"ypkgfiles": "^1.6.0"
36+
"ypkgfiles": "^1.6.0",
37+
"@eggjs/tegg-common-util": "^3.33.0",
38+
"@eggjs/dal-runtime": "^3.33.0",
39+
"@eggjs/dal-decorator": "^3.33.0",
40+
"@eggjs/tegg-loader": "^3.33.0",
41+
"@eggjs/tegg": "^3.33.0"
3742
},
3843
"peerDependencies": {
3944
"egg-mock": ">=5.8.3"
@@ -56,7 +61,7 @@
5661
"eslint-config-egg": "^12.0.0",
5762
"git-contributor": "2",
5863
"mm": "^3.2.0",
59-
"typescript": "^4.7.2"
64+
"typescript": "^5.0.4"
6065
},
6166
"repository": {
6267
"type": "git",
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Table, Column, ColumnType } from '@eggjs/tegg/dal';
2+
3+
@Table({
4+
comment: 'foo table',
5+
})
6+
export class Bar {
7+
@Column({
8+
type: ColumnType.INT,
9+
}, {
10+
primaryKey: true,
11+
})
12+
id: number;
13+
14+
@Column({
15+
type: ColumnType.VARCHAR,
16+
length: 100,
17+
})
18+
name: string;
19+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Table, Index, Column, ColumnType, IndexType } from '@eggjs/tegg/dal';
2+
3+
@Table({
4+
comment: 'foo table',
5+
})
6+
@Index({
7+
keys: [ 'name' ],
8+
type: IndexType.UNIQUE,
9+
})
10+
export class Foo {
11+
@Column({
12+
type: ColumnType.INT,
13+
}, {
14+
primaryKey: true,
15+
})
16+
id: number;
17+
18+
@Column({
19+
type: ColumnType.VARCHAR,
20+
length: 100,
21+
})
22+
name: string;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "dal",
3+
"eggModule": {
4+
"name": "dal"
5+
}
6+
}

test/fixtures/dal/package.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "dal",
3+
"egg": {
4+
"typescript": true
5+
},
6+
"repository": "[email protected]:eggjs/egg-bin.git",
7+
"devDependencies": {
8+
"@eggjs/tsconfig": "^1.3.3"
9+
}
10+
}

test/fixtures/dal/tsconfig.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "@eggjs/tsconfig",
3+
"compilerOptions": {
4+
"baseUrl": "."
5+
}
6+
}

test/lib/cmd/dal.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const path = require('node:path');
2+
const coffee = require('coffee');
3+
const mm = require('mm');
4+
const fs = require('node:fs/promises');
5+
const assert = require('assert');
6+
7+
describe('test/lib/cmd/dal.test.js', () => {
8+
const eggBin = require.resolve('../../../bin/egg-bin.js');
9+
const cwd = path.join(__dirname, '../../fixtures/dal');
10+
11+
afterEach(mm.restore);
12+
13+
describe('egg-bin dal gen', () => {
14+
after(async () => {
15+
await fs.rm(path.join(cwd, 'app/modules/dal/dal'), {
16+
recursive: true,
17+
});
18+
});
19+
20+
it('egg-bin dal gen should work', async () => {
21+
await coffee.fork(eggBin, [ 'dal', 'gen' ], { cwd })
22+
.debug()
23+
.expect('code', 0)
24+
.end();
25+
26+
for (const file of [
27+
'app/modules/dal/dal/dao/BarDAO.ts',
28+
'app/modules/dal/dal/dao/FooDAO.ts',
29+
'app/modules/dal/dal/dao/base/BaseBarDAO.ts',
30+
'app/modules/dal/dal/dao/base/BaseFooDAO.ts',
31+
'app/modules/dal/dal/extension/BarExtension.ts',
32+
'app/modules/dal/dal/extension/FooExtension.ts',
33+
'app/modules/dal/dal/structure/Bar.json',
34+
'app/modules/dal/dal/structure/Bar.sql',
35+
'app/modules/dal/dal/structure/Foo.json',
36+
'app/modules/dal/dal/structure/Foo.sql',
37+
]) {
38+
assert.ok(fs.stat(path.join(cwd, file)));
39+
}
40+
});
41+
});
42+
});

0 commit comments

Comments
 (0)