Skip to content

Commit

Permalink
fix: fix custom sql extension (#207)
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]. -->

- [ ] `npm test` passes
- [ ] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [ ] 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. -->

<!--
- any feature?
- close https://github.com/eggjs/egg/ISSUE_URL
-->

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

## Summary by CodeRabbit

- **Refactor**
- Improved how custom SQL maps are imported in the backend to enhance
system stability.

- **Tests**
- Added a new test case for verifying custom SQL generation in database
operations.

- **New Features**
- Introduced a new method for fetching all records from a specific
database table, enhancing data retrieval capabilities.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
killagu committed Apr 10, 2024
1 parent 8652c9d commit a405233
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion core/dal-runtime/src/SqlMapLoader.ts
Expand Up @@ -19,7 +19,7 @@ export class SqlMapLoader {
load(): TableSqlMap {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const customSqlMap: Record<string, SqlMap> = require(this.sqlMapPath);
const { default: customSqlMap }: { default: Record<string, SqlMap> } = require(this.sqlMapPath);
const baseSqlMapGenerator = new BaseSqlMapGenerator(this.tableModel, this.logger);
const baseSqlMap = baseSqlMapGenerator.load();
const sqlMap = {
Expand Down
17 changes: 17 additions & 0 deletions core/dal-runtime/test/TableSqlMap.test.ts
@@ -0,0 +1,17 @@
import assert from 'node:assert';
import { TableModel } from '@eggjs/dal-decorator';
import { Foo } from './fixtures/modules/dal/Foo';
import { SqlMapLoader } from '../src/SqlMapLoader';
import path from 'node:path';

describe('test/TableSqlMap.test.ts', () => {
it('custom sql should work', () => {
// const generator = new SqlGenerator();
const fooModel = TableModel.build(Foo);
// const sql = generator.generate(fooModel);
const sqlMapLoader = new SqlMapLoader(fooModel, path.join(__dirname, 'fixtures/modules/dal'), console);
const tableSqlMap = sqlMapLoader.load();
const sql = tableSqlMap.generate('findAll', {}, 'UTC');
assert.equal(sql, 'SELECT `id`,`name`,`col1`,`bit_column`,`bool_column`,`tiny_int_column`,`small_int_column`,`medium_int_column`,`int_column`,`big_int_column`,`decimal_column`,`float_column`,`double_column`,`date_column`,`date_time_column`,`timestamp_column`,`time_column`,`year_column`,`var_char_column`,`binary_column`,`var_binary_column`,`tiny_blob_column`,`tiny_text_column`,`blob_column`,`text_column`,`medium_blob_column`,`long_blob_column`,`medium_text_column`,`long_text_column`,`enum_column`,`set_column`,`geometry_column`,`point_column`,`line_string_column`,`polygon_column`,`multipoint_column`,`multi_line_string_column`,`multi_polygon_column`,`geometry_collection_column`,`json_column` from egg_foo;');
});
});
@@ -1,5 +1,8 @@
import { SqlMap } from '@eggjs/dal-decorator';
import { SqlMap, SqlType } from '@eggjs/dal-decorator';

export default {

findAll: {
type: SqlType.SELECT,
sql: 'SELECT {{ allColumns}} from egg_foo;'
},
} as Record<string, SqlMap>;

0 comments on commit a405233

Please sign in to comment.