-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!-- 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
Showing
12 changed files
with
216 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,4 @@ package-lock.json | |
yarn.lock | ||
.c8_output | ||
.idea | ||
.eslintcache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
})(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "dal", | ||
"eggModule": { | ||
"name": "dal" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "dal", | ||
"egg": { | ||
"typescript": true | ||
}, | ||
"repository": "[email protected]:eggjs/egg-bin.git", | ||
"devDependencies": { | ||
"@eggjs/tsconfig": "^1.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "@eggjs/tsconfig", | ||
"compilerOptions": { | ||
"baseUrl": "." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))); | ||
} | ||
}); | ||
}); | ||
}); |