From cc83190a6282e0a5c466f3fe395ecfd452c6ea51 Mon Sep 17 00:00:00 2001 From: killagu Date: Wed, 10 Apr 2024 13:10:48 +0800 Subject: [PATCH] fix: fix custom sql extension --- core/dal-runtime/src/SqlMapLoader.ts | 2 +- core/dal-runtime/test/TableSqlMap.test.ts | 17 +++++++++++++++++ .../modules/dal/dal/extension/FooExtension.ts | 7 +++++-- 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 core/dal-runtime/test/TableSqlMap.test.ts diff --git a/core/dal-runtime/src/SqlMapLoader.ts b/core/dal-runtime/src/SqlMapLoader.ts index 7f808d52..9a88b002 100644 --- a/core/dal-runtime/src/SqlMapLoader.ts +++ b/core/dal-runtime/src/SqlMapLoader.ts @@ -19,7 +19,7 @@ export class SqlMapLoader { load(): TableSqlMap { try { // eslint-disable-next-line @typescript-eslint/no-var-requires - const customSqlMap: Record = require(this.sqlMapPath); + const { default: customSqlMap }: { default: Record } = require(this.sqlMapPath); const baseSqlMapGenerator = new BaseSqlMapGenerator(this.tableModel, this.logger); const baseSqlMap = baseSqlMapGenerator.load(); const sqlMap = { diff --git a/core/dal-runtime/test/TableSqlMap.test.ts b/core/dal-runtime/test/TableSqlMap.test.ts new file mode 100644 index 00000000..35f5918f --- /dev/null +++ b/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;'); + }); +}); diff --git a/core/dal-runtime/test/fixtures/modules/dal/dal/extension/FooExtension.ts b/core/dal-runtime/test/fixtures/modules/dal/dal/extension/FooExtension.ts index 61f4f89e..bf83dfac 100644 --- a/core/dal-runtime/test/fixtures/modules/dal/dal/extension/FooExtension.ts +++ b/core/dal-runtime/test/fixtures/modules/dal/dal/extension/FooExtension.ts @@ -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;