-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: impl egg-bin dal gen #257
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,4 @@ package-lock.json | |
yarn.lock | ||
.c8_output | ||
.idea | ||
.eslintcache |
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; |
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; |
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); | ||
} | ||
})(); | ||
|
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; | ||
} |
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "dal", | ||
"eggModule": { | ||
"name": "dal" | ||
} | ||
} |
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" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "@eggjs/tsconfig", | ||
"compilerOptions": { | ||
"baseUrl": "." | ||
} | ||
} |
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))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+7
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test suite for - assert.ok(fs.stat(path.join(cwd, file)));
+ assert.ok(await fs.stat(path.join(cwd, file))); Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 厉害了。。。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fengmk2, 谢谢您的认可!如果您有任何问题或需要进一步的帮助,请随时告诉我。 😊
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new section on the
dal
command is clear and concise, providing straightforward instructions on how to use theegg-bin dal gen
command and where to find further documentation. However, to maintain consistency with the rest of the document, consider starting the description with an uppercase letter and adding a comma after "dal document please read" for better readability.Committable suggestion
Dal document, please read tegg doc.