From 56b259d7215a9d9542b36e421996623819369846 Mon Sep 17 00:00:00 2001 From: killa Date: Sat, 23 Mar 2024 01:02:42 +0800 Subject: [PATCH] feat: impl dal for standalone tegg (#197) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ##### Checklist - [ ] `npm test` passes - [ ] tests and/or benchmarks are included - [ ] documentation is changed or added - [ ] commit message follows commit guidelines ##### Affected core subsystem(s) ##### Description of change ## Summary by CodeRabbit - **Refactor** - Updated logging system across various components for consistency and improved performance. - **New Features** - Enhanced Data Access Layer (DAL) functionalities for improved data handling. - Added classes for lifecycle management and custom SQL extensions. - **Documentation** - Restructured and refined documentation for better clarity and readability. --- core/dal-runtime/src/BaseSqlMap.ts | 6 +- core/dal-runtime/src/SqlMapLoader.ts | 6 +- core/dal-runtime/test/DataSource.test.ts | 1 - core/tegg/index.ts | 8 + plugin/controller/README.md | 5 +- plugin/dal/README.md | 30 +- plugin/dal/app.ts | 2 + plugin/dal/lib/DalTableEggPrototypeHook.ts | 6 +- plugin/dal/package.json | 1 + standalone/standalone/package.json | 3 +- standalone/standalone/src/Runner.ts | 33 +- .../test/fixtures/dal-module/Foo.ts | 292 +++++++++++ .../fixtures/dal-module/dal/dao/FooDAO.ts | 20 + .../dal-module/dal/dao/base/BaseFooDAO.ts | 494 ++++++++++++++++++ .../dal-module/dal/extension/FooExtension.ts | 20 + .../dal-module/dal/structure/Foo.json | 421 +++++++++++++++ .../fixtures/dal-module/dal/structure/Foo.sql | 44 ++ .../test/fixtures/dal-module/main.ts | 93 ++++ .../test/fixtures/dal-module/module.yml | 8 + .../test/fixtures/dal-module/package.json | 9 + .../test/fixtures/dal-module/tsconfig.json | 6 + standalone/standalone/test/index.test.ts | 37 ++ 22 files changed, 1522 insertions(+), 23 deletions(-) create mode 100644 standalone/standalone/test/fixtures/dal-module/Foo.ts create mode 100644 standalone/standalone/test/fixtures/dal-module/dal/dao/FooDAO.ts create mode 100644 standalone/standalone/test/fixtures/dal-module/dal/dao/base/BaseFooDAO.ts create mode 100644 standalone/standalone/test/fixtures/dal-module/dal/extension/FooExtension.ts create mode 100644 standalone/standalone/test/fixtures/dal-module/dal/structure/Foo.json create mode 100644 standalone/standalone/test/fixtures/dal-module/dal/structure/Foo.sql create mode 100644 standalone/standalone/test/fixtures/dal-module/main.ts create mode 100644 standalone/standalone/test/fixtures/dal-module/module.yml create mode 100644 standalone/standalone/test/fixtures/dal-module/package.json create mode 100644 standalone/standalone/test/fixtures/dal-module/tsconfig.json diff --git a/core/dal-runtime/src/BaseSqlMap.ts b/core/dal-runtime/src/BaseSqlMap.ts index b97f56a4..28fb81e7 100644 --- a/core/dal-runtime/src/BaseSqlMap.ts +++ b/core/dal-runtime/src/BaseSqlMap.ts @@ -1,6 +1,6 @@ import _ from 'lodash'; import { ColumnType, IndexType, SqlType, SqlMap, TableModel } from '@eggjs/dal-decorator'; -import { type EggLogger } from 'egg'; +import { Logger } from '@eggjs/tegg'; import { TemplateUtil } from './TemplateUtil'; export interface GenerateSqlMap { @@ -11,9 +11,9 @@ export interface GenerateSqlMap { export class BaseSqlMapGenerator { private readonly tableModel: TableModel; - private readonly logger: EggLogger; + private readonly logger: Logger; - constructor(tableModel: TableModel, logger: EggLogger) { + constructor(tableModel: TableModel, logger: Logger) { this.tableModel = tableModel; this.logger = logger; } diff --git a/core/dal-runtime/src/SqlMapLoader.ts b/core/dal-runtime/src/SqlMapLoader.ts index 1bbb800a..f20e93b9 100644 --- a/core/dal-runtime/src/SqlMapLoader.ts +++ b/core/dal-runtime/src/SqlMapLoader.ts @@ -1,15 +1,15 @@ import path from 'node:path'; import { TableModel, SqlMap } from '@eggjs/dal-decorator'; -import { type EggLogger } from 'egg'; +import { Logger } from '@eggjs/tegg'; import { BaseSqlMapGenerator } from './BaseSqlMap'; import { TableSqlMap } from './TableSqlMap'; export class SqlMapLoader { - private readonly logger: EggLogger; + private readonly logger: Logger; private readonly tableModel: TableModel; private readonly sqlMapPath: string; - constructor(tableModel: TableModel, moduleDir: string, logger: EggLogger) { + constructor(tableModel: TableModel, moduleDir: string, logger: Logger) { this.sqlMapPath = path.join(moduleDir, 'dal/extension', `${tableModel.clazz.name}Extension.ts`); this.logger = logger; this.tableModel = tableModel; diff --git a/core/dal-runtime/test/DataSource.test.ts b/core/dal-runtime/test/DataSource.test.ts index 1c4c81c1..2a6ab357 100644 --- a/core/dal-runtime/test/DataSource.test.ts +++ b/core/dal-runtime/test/DataSource.test.ts @@ -29,7 +29,6 @@ describe('test/Datasource.test.ts', () => { const sqlGenerator = new SqlGenerator(); const createTableSql = sqlGenerator.generate(tableModel); - console.log('create table: ', createTableSql); await mysql.query(createTableSql); diff --git a/core/tegg/index.ts b/core/tegg/index.ts index a69300f1..cd21f003 100644 --- a/core/tegg/index.ts +++ b/core/tegg/index.ts @@ -8,3 +8,11 @@ export * as aop from '@eggjs/aop-decorator'; export * as orm from '@eggjs/tegg-orm-decorator'; export * as schedule from '@eggjs/tegg-schedule-decorator'; export { RuntimeConfig, ModuleConfigs, ModuleConfigHolder } from '@eggjs/tegg-common-util'; + +export interface Logger { + debug(message?: any, ...optionalParams: any[]): void; + log(message?: any, ...optionalParams: any[]): void; + info(message?: any, ...optionalParams: any[]): void; + warn(message?: any, ...optionalParams: any[]): void; + error(message?: any, ...optionalParams: any[]): void; +} diff --git a/plugin/controller/README.md b/plugin/controller/README.md index 351ded61..eefe8702 100644 --- a/plugin/controller/README.md +++ b/plugin/controller/README.md @@ -17,10 +17,7 @@ npm i --save @eggjs/tegg-controller-plugin ```json // tsconfig.json { - "compilerOptions": { - // 注解特性需要显示打开 - "experimentalDecorators": true - } + "extends": "@eggjs/tsconfig" } ``` diff --git a/plugin/dal/README.md b/plugin/dal/README.md index 9f83b7df..05e33853 100644 --- a/plugin/dal/README.md +++ b/plugin/dal/README.md @@ -2,8 +2,9 @@ @eggjs/tegg-dal-plugin 支持使用注解的方式来开发 egg 中的 dal。 -## Install +## egg 模式 +### Install ```shell # tegg 注解 npm i --save @eggjs/tegg @@ -13,18 +14,15 @@ npm i --save @eggjs/tegg-plugin npm i --save @eggjs/tegg-dal-plugin ``` -## Prepare +### Prepare ```json // tsconfig.json { - "compilerOptions": { - // 注解特性需要显示打开 - "experimentalDecorators": true - } + "extends": "@eggjs/tsconfig" } ``` -## Config +### Config ```js // config/plugin.js @@ -39,6 +37,24 @@ exports.teggDal = { }; ``` +## standalone 模式 + +### Install +```shell +# tegg 注解 +npm i --save @eggjs/tegg +# tegg dal 插件 +npm i --save @eggjs/tegg-dal-plugin +``` + +### Prepare +```json +// tsconfig.json +{ + "extends": "@eggjs/tsconfig" +} +``` + ## Usage ### module.yml diff --git a/plugin/dal/app.ts b/plugin/dal/app.ts index 45e2e65e..0f86c014 100644 --- a/plugin/dal/app.ts +++ b/plugin/dal/app.ts @@ -1,3 +1,5 @@ +/// + import { Application } from 'egg'; import { DalTableEggPrototypeHook } from './lib/DalTableEggPrototypeHook'; import { MysqlDataSourceManager } from './lib/MysqlDataSourceManager'; diff --git a/plugin/dal/lib/DalTableEggPrototypeHook.ts b/plugin/dal/lib/DalTableEggPrototypeHook.ts index 96577974..ca572b06 100644 --- a/plugin/dal/lib/DalTableEggPrototypeHook.ts +++ b/plugin/dal/lib/DalTableEggPrototypeHook.ts @@ -1,15 +1,15 @@ import { LifecycleHook } from '@eggjs/tegg-lifecycle'; import { EggPrototype, EggPrototypeLifecycleContext } from '@eggjs/tegg-metadata'; import { TableInfoUtil, TableModel } from '@eggjs/dal-decorator'; -import { EggLogger } from 'egg'; +import { Logger } from '@eggjs/tegg'; import { SqlMapLoader } from '@eggjs/dal-runtime'; import { TableModelManager } from './TableModelManager'; import { SqlMapManager } from './SqlMapManager'; export class DalTableEggPrototypeHook implements LifecycleHook { - private readonly logger: EggLogger; + private readonly logger: Logger; - constructor(logger: EggLogger) { + constructor(logger: Logger) { this.logger = logger; } diff --git a/plugin/dal/package.json b/plugin/dal/package.json index 497626ea..2610a7be 100644 --- a/plugin/dal/package.json +++ b/plugin/dal/package.json @@ -54,6 +54,7 @@ "@eggjs/tegg": "^3.33.1" }, "devDependencies": { + "@eggjs/tegg-config": "^3.33.1", "@eggjs/tegg-plugin": "^3.33.1", "@types/mocha": "^10.0.1", "@types/node": "^20.2.4", diff --git a/standalone/standalone/package.json b/standalone/standalone/package.json index 38bfdf0d..c757ca03 100644 --- a/standalone/standalone/package.json +++ b/standalone/standalone/package.json @@ -47,7 +47,8 @@ "@eggjs/tegg-lifecycle": "^3.33.1", "@eggjs/tegg-loader": "^3.33.1", "@eggjs/tegg-metadata": "^3.33.1", - "@eggjs/tegg-runtime": "^3.33.1" + "@eggjs/tegg-runtime": "^3.33.1", + "@eggjs/tegg-dal-plugin": "^3.33.1" }, "publishConfig": { "access": "public" diff --git a/standalone/standalone/src/Runner.ts b/standalone/standalone/src/Runner.ts index 7d5bfa40..6ff7aeb9 100644 --- a/standalone/standalone/src/Runner.ts +++ b/standalone/standalone/src/Runner.ts @@ -12,7 +12,14 @@ import { LoadUnitInstanceFactory, ModuleLoadUnitInstance, } from '@eggjs/tegg-runtime'; -import { EggProtoImplClass, PrototypeUtil, ModuleConfigHolder, ModuleConfigs, ConfigSourceQualifierAttribute } from '@eggjs/tegg'; +import { + EggProtoImplClass, + PrototypeUtil, + ModuleConfigHolder, + ModuleConfigs, + ConfigSourceQualifierAttribute, + Logger, +} from '@eggjs/tegg'; import { StandaloneUtil, MainRunner } from '@eggjs/tegg/standalone'; import { CrosscutAdviceFactory } from '@eggjs/tegg/aop'; import { EggObjectAopHook, EggPrototypeCrossCutHook, LoadUnitAopHook } from '@eggjs/tegg-aop-runtime'; @@ -23,6 +30,11 @@ import { StandaloneContext } from './StandaloneContext'; import { StandaloneContextHandler } from './StandaloneContextHandler'; import { ConfigSourceLoadUnitHook } from './ConfigSourceLoadUnitHook'; import { LoadUnitInnerClassHook } from './LoadUnitInnerClassHook'; +import { DalTableEggPrototypeHook } from '@eggjs/tegg-dal-plugin/lib/DalTableEggPrototypeHook'; +import { DalModuleLoadUnitHook } from '@eggjs/tegg-dal-plugin/lib/DalModuleLoadUnitHook'; +import { MysqlDataSourceManager } from '@eggjs/tegg-dal-plugin/lib/MysqlDataSourceManager'; +import { SqlMapManager } from '@eggjs/tegg-dal-plugin/lib/SqlMapManager'; +import { TableModelManager } from '@eggjs/tegg-dal-plugin/lib/TableModelManager'; export interface ModuleDependency extends ReadModuleReferenceOptions { baseDir: string; @@ -50,6 +62,8 @@ export class Runner { private runnerProto: EggPrototype; private configSourceEggPrototypeHook: ConfigSourceLoadUnitHook; private loadUnitMultiInstanceProtoHook: LoadUnitMultiInstanceProtoHook; + private dalTableEggPrototypeHook: DalTableEggPrototypeHook; + private dalModuleLoadUnitHook: DalModuleLoadUnitHook; private readonly loadUnitInnerClassHook: LoadUnitInnerClassHook; private readonly crosscutAdviceFactory: CrosscutAdviceFactory; @@ -139,6 +153,13 @@ export class Runner { this.loadUnitMultiInstanceProtoHook = new LoadUnitMultiInstanceProtoHook(); LoadUnitLifecycleUtil.registerLifecycle(this.loadUnitMultiInstanceProtoHook); + + this.dalModuleLoadUnitHook = new DalModuleLoadUnitHook(this.moduleConfigs); + const loggerInnerObject = this.innerObjects.logger && this.innerObjects.logger[0]; + const logger = loggerInnerObject?.obj || console; + this.dalTableEggPrototypeHook = new DalTableEggPrototypeHook(logger as Logger); + EggPrototypeLifecycleUtil.registerLifecycle(this.dalTableEggPrototypeHook); + LoadUnitLifecycleUtil.registerLifecycle(this.dalModuleLoadUnitHook); } async load() { @@ -229,5 +250,15 @@ export class Runner { if (this.loadUnitMultiInstanceProtoHook) { LoadUnitLifecycleUtil.deleteLifecycle(this.loadUnitMultiInstanceProtoHook); } + + if (this.dalTableEggPrototypeHook) { + await EggPrototypeLifecycleUtil.deleteLifecycle(this.dalTableEggPrototypeHook); + } + if (this.dalModuleLoadUnitHook) { + await LoadUnitLifecycleUtil.deleteLifecycle(this.dalModuleLoadUnitHook); + } + MysqlDataSourceManager.instance.clear(); + SqlMapManager.instance.clear(); + TableModelManager.instance.clear(); } } diff --git a/standalone/standalone/test/fixtures/dal-module/Foo.ts b/standalone/standalone/test/fixtures/dal-module/Foo.ts new file mode 100644 index 00000000..44f02610 --- /dev/null +++ b/standalone/standalone/test/fixtures/dal-module/Foo.ts @@ -0,0 +1,292 @@ +import { + Column, + ColumnType, + Geometry, + GeometryCollection, + Index, + IndexType, + Line, MultiLine, MultiPoint, MultiPolygon, Point, Polygon, + Table, +} from '@eggjs/dal-decorator'; +import { IndexStoreType } from '@eggjs/dal-decorator/src/enum/IndexStoreType'; + +@Table({ + name: 'egg_foo', + comment: 'foo table', + characterSet: 'utf8mb4', + collate: 'utf8mb4_unicode_ci', +}) +@Index({ + keys: [ 'name', 'col1' ], + type: IndexType.UNIQUE, + storeType: IndexStoreType.BTREE, + comment: 'index comment\n', +}) +@Index({ + keys: [ 'col1' ], + type: IndexType.FULLTEXT, + comment: 'index comment\n', +}) +export class Foo { + @Column({ + type: ColumnType.INT, + }, { + primaryKey: true, + autoIncrement: true, + comment: 'the primary key', + }) + id: number; + + @Column({ + type: ColumnType.VARCHAR, + length: 100, + }, { + uniqueKey: true, + }) + name: string; + + @Column({ + type: ColumnType.VARCHAR, + length: 100, + }, { + name: 'col1', + }) + col1: string; + + @Column({ + type: ColumnType.BIT, + length: 10, + }) + bitColumn: Buffer; + + @Column({ + type: ColumnType.BOOL, + }) + boolColumn: 0 | 1; + + @Column({ + type: ColumnType.TINYINT, + length: 5, + unsigned: true, + zeroFill: true, + }) + tinyIntColumn: number; + + @Column({ + type: ColumnType.SMALLINT, + length: 5, + unsigned: true, + zeroFill: true, + }) + smallIntColumn: number; + + @Column({ + type: ColumnType.MEDIUMINT, + length: 5, + unsigned: true, + zeroFill: true, + }) + mediumIntColumn: number; + + @Column({ + type: ColumnType.INT, + length: 5, + unsigned: true, + zeroFill: true, + }) + intColumn: number; + + @Column({ + type: ColumnType.BIGINT, + length: 5, + unsigned: true, + zeroFill: true, + }) + bigIntColumn: string; + + @Column({ + type: ColumnType.DECIMAL, + length: 10, + fractionalLength: 5, + unsigned: true, + zeroFill: true, + }) + decimalColumn: string; + + @Column({ + type: ColumnType.FLOAT, + length: 10, + fractionalLength: 5, + unsigned: true, + zeroFill: true, + }) + floatColumn: number; + + @Column({ + type: ColumnType.DOUBLE, + length: 10, + fractionalLength: 5, + unsigned: true, + zeroFill: true, + }) + doubleColumn: number; + + @Column({ + type: ColumnType.DATE, + }) + dateColumn: Date; + + @Column({ + type: ColumnType.DATETIME, + precision: 3, + }) + dateTimeColumn: Date; + + @Column({ + type: ColumnType.TIMESTAMP, + precision: 3, + }) + timestampColumn: Date; + + @Column({ + type: ColumnType.TIME, + precision: 3, + }) + timeColumn: string; + + @Column({ + type: ColumnType.YEAR, + }) + yearColumn: number; + + @Column({ + type: ColumnType.VARCHAR, + length: 100, + characterSet: 'utf8mb4', + collate: 'utf8mb4_unicode_ci', + }) + varCharColumn: string; + + @Column({ + type: ColumnType.BINARY, + }) + binaryColumn: Buffer; + + @Column({ + type: ColumnType.VARBINARY, + length: 100, + }) + varBinaryColumn: Buffer; + + @Column({ + type: ColumnType.TINYBLOB, + }) + tinyBlobColumn: Buffer; + + @Column({ + type: ColumnType.TINYTEXT, + characterSet: 'utf8mb4', + collate: 'utf8mb4_unicode_ci', + }) + tinyTextColumn: string; + + @Column({ + type: ColumnType.BLOB, + length: 100, + }) + blobColumn: Buffer; + + @Column({ + type: ColumnType.TEXT, + length: 100, + characterSet: 'utf8mb4', + collate: 'utf8mb4_unicode_ci', + }) + textColumn: string; + + @Column({ + type: ColumnType.MEDIUMBLOB, + }) + mediumBlobColumn: Buffer; + + @Column({ + type: ColumnType.LONGBLOB, + }) + longBlobColumn: Buffer; + + @Column({ + type: ColumnType.MEDIUMTEXT, + characterSet: 'utf8mb4', + collate: 'utf8mb4_unicode_ci', + }) + mediumTextColumn: string; + + @Column({ + type: ColumnType.LONGTEXT, + characterSet: 'utf8mb4', + collate: 'utf8mb4_unicode_ci', + }) + longTextColumn: string; + + @Column({ + type: ColumnType.ENUM, + enums: [ 'A', 'B' ], + characterSet: 'utf8mb4', + collate: 'utf8mb4_unicode_ci', + }) + enumColumn: string; + + @Column({ + type: ColumnType.SET, + enums: [ 'A', 'B' ], + characterSet: 'utf8mb4', + collate: 'utf8mb4_unicode_ci', + }) + setColumn: string; + + @Column({ + type: ColumnType.GEOMETRY, + }) + geometryColumn: Geometry; + + + @Column({ + type: ColumnType.POINT, + }) + pointColumn: Point; + + @Column({ + type: ColumnType.LINESTRING, + }) + lineStringColumn: Line; + + @Column({ + type: ColumnType.POLYGON, + }) + polygonColumn: Polygon; + + @Column({ + type: ColumnType.MULTIPOINT, + }) + multipointColumn: MultiPoint; + + @Column({ + type: ColumnType.MULTILINESTRING, + }) + multiLineStringColumn: MultiLine; + + @Column({ + type: ColumnType.MULTIPOLYGON, + }) + multiPolygonColumn: MultiPolygon; + + @Column({ + type: ColumnType.GEOMETRYCOLLECTION, + }) + geometryCollectionColumn: GeometryCollection; + + @Column({ + type: ColumnType.JSON, + }) + jsonColumn: object; +} diff --git a/standalone/standalone/test/fixtures/dal-module/dal/dao/FooDAO.ts b/standalone/standalone/test/fixtures/dal-module/dal/dao/FooDAO.ts new file mode 100644 index 00000000..1095d886 --- /dev/null +++ b/standalone/standalone/test/fixtures/dal-module/dal/dao/FooDAO.ts @@ -0,0 +1,20 @@ +import { SingletonProto, AccessLevel } from '@eggjs/tegg'; +import { BaseFooDAO } from './base/BaseFooDAO'; +import { Foo } from '../../Foo'; + +/** + * FooDAO 类 + * @class FooDAO + * @classdesc 在此扩展关于 Foo 数据的一切操作 + * @augments BaseFooDAO + */ +@SingletonProto({ + accessLevel: AccessLevel.PUBLIC, +}) +export default class FooDAO extends BaseFooDAO { + async findByName(name: string): Promise { + return this.dataSource.execute('findByName', { + name, + }); + } +} diff --git a/standalone/standalone/test/fixtures/dal-module/dal/dao/base/BaseFooDAO.ts b/standalone/standalone/test/fixtures/dal-module/dal/dao/base/BaseFooDAO.ts new file mode 100644 index 00000000..db0e23f8 --- /dev/null +++ b/standalone/standalone/test/fixtures/dal-module/dal/dao/base/BaseFooDAO.ts @@ -0,0 +1,494 @@ +import type { InsertResult, UpdateResult, DeleteResult } from '@eggjs/rds/lib/types'; +import { SingletonProto, AccessLevel, Inject } from '@eggjs/tegg'; +import { DataSource, DataSourceInjectName, DataSourceQualifier } from '@eggjs/tegg/dal'; +import { Foo } from '../../../Foo'; + +type Optional = Omit & Partial; +/** + * 自动生成的 FooDAO 基类 + * @class BaseFooDAO + * @classdesc 该文件由 @eggjs/tegg 自动生成,请**不要**修改它! + */ + +/* istanbul ignore next */ +@SingletonProto({ + accessLevel: AccessLevel.PUBLIC, +}) +export class BaseFooDAO { + static clazzModel = Foo; + + @Inject({ + name: DataSourceInjectName, + }) + @DataSourceQualifier('dal.foo.Foo') + protected readonly dataSource: DataSource; + + public async insert(raw: Optional): Promise { + const data: Record = {}; + let tmp; + + tmp = raw.id; + if (tmp !== undefined) { + data.$id = tmp; + } + + tmp = raw.name; + if (tmp !== undefined) { + data.$name = tmp; + } + + tmp = raw.col1; + if (tmp !== undefined) { + data.$col1 = tmp; + } + + tmp = raw.bitColumn; + if (tmp !== undefined) { + data.$bitColumn = tmp; + } + + tmp = raw.boolColumn; + if (tmp !== undefined) { + data.$boolColumn = tmp; + } + + tmp = raw.tinyIntColumn; + if (tmp !== undefined) { + data.$tinyIntColumn = tmp; + } + + tmp = raw.smallIntColumn; + if (tmp !== undefined) { + data.$smallIntColumn = tmp; + } + + tmp = raw.mediumIntColumn; + if (tmp !== undefined) { + data.$mediumIntColumn = tmp; + } + + tmp = raw.intColumn; + if (tmp !== undefined) { + data.$intColumn = tmp; + } + + tmp = raw.bigIntColumn; + if (tmp !== undefined) { + data.$bigIntColumn = tmp; + } + + tmp = raw.decimalColumn; + if (tmp !== undefined) { + data.$decimalColumn = tmp; + } + + tmp = raw.floatColumn; + if (tmp !== undefined) { + data.$floatColumn = tmp; + } + + tmp = raw.doubleColumn; + if (tmp !== undefined) { + data.$doubleColumn = tmp; + } + + tmp = raw.dateColumn; + if (tmp !== undefined) { + data.$dateColumn = tmp; + } + + tmp = raw.dateTimeColumn; + if (tmp !== undefined) { + data.$dateTimeColumn = tmp; + } + + tmp = raw.timestampColumn; + if (tmp !== undefined) { + data.$timestampColumn = tmp; + } + + tmp = raw.timeColumn; + if (tmp !== undefined) { + data.$timeColumn = tmp; + } + + tmp = raw.yearColumn; + if (tmp !== undefined) { + data.$yearColumn = tmp; + } + + tmp = raw.varCharColumn; + if (tmp !== undefined) { + data.$varCharColumn = tmp; + } + + tmp = raw.binaryColumn; + if (tmp !== undefined) { + data.$binaryColumn = tmp; + } + + tmp = raw.varBinaryColumn; + if (tmp !== undefined) { + data.$varBinaryColumn = tmp; + } + + tmp = raw.tinyBlobColumn; + if (tmp !== undefined) { + data.$tinyBlobColumn = tmp; + } + + tmp = raw.tinyTextColumn; + if (tmp !== undefined) { + data.$tinyTextColumn = tmp; + } + + tmp = raw.blobColumn; + if (tmp !== undefined) { + data.$blobColumn = tmp; + } + + tmp = raw.textColumn; + if (tmp !== undefined) { + data.$textColumn = tmp; + } + + tmp = raw.mediumBlobColumn; + if (tmp !== undefined) { + data.$mediumBlobColumn = tmp; + } + + tmp = raw.longBlobColumn; + if (tmp !== undefined) { + data.$longBlobColumn = tmp; + } + + tmp = raw.mediumTextColumn; + if (tmp !== undefined) { + data.$mediumTextColumn = tmp; + } + + tmp = raw.longTextColumn; + if (tmp !== undefined) { + data.$longTextColumn = tmp; + } + + tmp = raw.enumColumn; + if (tmp !== undefined) { + data.$enumColumn = tmp; + } + + tmp = raw.setColumn; + if (tmp !== undefined) { + data.$setColumn = tmp; + } + + tmp = raw.geometryColumn; + if (tmp !== undefined) { + data.$geometryColumn = tmp; + } + + tmp = raw.pointColumn; + if (tmp !== undefined) { + data.$pointColumn = tmp; + } + + tmp = raw.lineStringColumn; + if (tmp !== undefined) { + data.$lineStringColumn = tmp; + } + + tmp = raw.polygonColumn; + if (tmp !== undefined) { + data.$polygonColumn = tmp; + } + + tmp = raw.multipointColumn; + if (tmp !== undefined) { + data.$multipointColumn = tmp; + } + + tmp = raw.multiLineStringColumn; + if (tmp !== undefined) { + data.$multiLineStringColumn = tmp; + } + + tmp = raw.multiPolygonColumn; + if (tmp !== undefined) { + data.$multiPolygonColumn = tmp; + } + + tmp = raw.geometryCollectionColumn; + if (tmp !== undefined) { + data.$geometryCollectionColumn = tmp; + } + + tmp = raw.jsonColumn; + if (tmp !== undefined) { + data.$jsonColumn = tmp; + } + + return this.dataSource.executeRawScalar('insert', data); + } + + public async update(id: number, data: Partial): Promise { + + const newData: Record = { + primary: { + id, + }, + }; + let tmp; + + tmp = data.id; + if (tmp !== undefined) { + newData.$id = tmp; + } + + tmp = data.name; + if (tmp !== undefined) { + newData.$name = tmp; + } + + tmp = data.col1; + if (tmp !== undefined) { + newData.$col1 = tmp; + } + + tmp = data.bitColumn; + if (tmp !== undefined) { + newData.$bitColumn = tmp; + } + + tmp = data.boolColumn; + if (tmp !== undefined) { + newData.$boolColumn = tmp; + } + + tmp = data.tinyIntColumn; + if (tmp !== undefined) { + newData.$tinyIntColumn = tmp; + } + + tmp = data.smallIntColumn; + if (tmp !== undefined) { + newData.$smallIntColumn = tmp; + } + + tmp = data.mediumIntColumn; + if (tmp !== undefined) { + newData.$mediumIntColumn = tmp; + } + + tmp = data.intColumn; + if (tmp !== undefined) { + newData.$intColumn = tmp; + } + + tmp = data.bigIntColumn; + if (tmp !== undefined) { + newData.$bigIntColumn = tmp; + } + + tmp = data.decimalColumn; + if (tmp !== undefined) { + newData.$decimalColumn = tmp; + } + + tmp = data.floatColumn; + if (tmp !== undefined) { + newData.$floatColumn = tmp; + } + + tmp = data.doubleColumn; + if (tmp !== undefined) { + newData.$doubleColumn = tmp; + } + + tmp = data.dateColumn; + if (tmp !== undefined) { + newData.$dateColumn = tmp; + } + + tmp = data.dateTimeColumn; + if (tmp !== undefined) { + newData.$dateTimeColumn = tmp; + } + + tmp = data.timestampColumn; + if (tmp !== undefined) { + newData.$timestampColumn = tmp; + } + + tmp = data.timeColumn; + if (tmp !== undefined) { + newData.$timeColumn = tmp; + } + + tmp = data.yearColumn; + if (tmp !== undefined) { + newData.$yearColumn = tmp; + } + + tmp = data.varCharColumn; + if (tmp !== undefined) { + newData.$varCharColumn = tmp; + } + + tmp = data.binaryColumn; + if (tmp !== undefined) { + newData.$binaryColumn = tmp; + } + + tmp = data.varBinaryColumn; + if (tmp !== undefined) { + newData.$varBinaryColumn = tmp; + } + + tmp = data.tinyBlobColumn; + if (tmp !== undefined) { + newData.$tinyBlobColumn = tmp; + } + + tmp = data.tinyTextColumn; + if (tmp !== undefined) { + newData.$tinyTextColumn = tmp; + } + + tmp = data.blobColumn; + if (tmp !== undefined) { + newData.$blobColumn = tmp; + } + + tmp = data.textColumn; + if (tmp !== undefined) { + newData.$textColumn = tmp; + } + + tmp = data.mediumBlobColumn; + if (tmp !== undefined) { + newData.$mediumBlobColumn = tmp; + } + + tmp = data.longBlobColumn; + if (tmp !== undefined) { + newData.$longBlobColumn = tmp; + } + + tmp = data.mediumTextColumn; + if (tmp !== undefined) { + newData.$mediumTextColumn = tmp; + } + + tmp = data.longTextColumn; + if (tmp !== undefined) { + newData.$longTextColumn = tmp; + } + + tmp = data.enumColumn; + if (tmp !== undefined) { + newData.$enumColumn = tmp; + } + + tmp = data.setColumn; + if (tmp !== undefined) { + newData.$setColumn = tmp; + } + + tmp = data.geometryColumn; + if (tmp !== undefined) { + newData.$geometryColumn = tmp; + } + + tmp = data.pointColumn; + if (tmp !== undefined) { + newData.$pointColumn = tmp; + } + + tmp = data.lineStringColumn; + if (tmp !== undefined) { + newData.$lineStringColumn = tmp; + } + + tmp = data.polygonColumn; + if (tmp !== undefined) { + newData.$polygonColumn = tmp; + } + + tmp = data.multipointColumn; + if (tmp !== undefined) { + newData.$multipointColumn = tmp; + } + + tmp = data.multiLineStringColumn; + if (tmp !== undefined) { + newData.$multiLineStringColumn = tmp; + } + + tmp = data.multiPolygonColumn; + if (tmp !== undefined) { + newData.$multiPolygonColumn = tmp; + } + + tmp = data.geometryCollectionColumn; + if (tmp !== undefined) { + newData.$geometryCollectionColumn = tmp; + } + + tmp = data.jsonColumn; + if (tmp !== undefined) { + newData.$jsonColumn = tmp; + } + + return this.dataSource.executeRawScalar('update', newData); + } + + public async delete(id: number): Promise { + return this.dataSource.executeRawScalar('delete', { + id, + }); + } + + public async del(id: number): Promise { + return this.dataSource.executeRawScalar('delete', { + id, + }); + } + + public async findByCol1($col1: string): Promise { + return this.dataSource.execute('findByCol1', { + $col1, + }); + } + + public async findOneByCol1($col1: string): Promise { + return this.dataSource.executeScalar('findOneByCol1', { + $col1, + }); + } + + public async findByUkNameCol1($name: string, $col1: string): Promise { + return this.dataSource.execute('findByUkNameCol1', { + $name, + $col1, + }); + } + + public async findOneByUkNameCol1($name: string, $col1: string): Promise { + return this.dataSource.executeScalar('findOneByUkNameCol1', { + $name, + $col1, + }); + } + + public async findById($id: number): Promise { + return this.dataSource.executeScalar('findById', { + $id, + }); + } + + public async findByPrimary($id: number): Promise { + return this.dataSource.executeScalar('findById', { + $id, + }); + } +} diff --git a/standalone/standalone/test/fixtures/dal-module/dal/extension/FooExtension.ts b/standalone/standalone/test/fixtures/dal-module/dal/extension/FooExtension.ts new file mode 100644 index 00000000..a919cb15 --- /dev/null +++ b/standalone/standalone/test/fixtures/dal-module/dal/extension/FooExtension.ts @@ -0,0 +1,20 @@ +import { SqlMap } from '@eggjs/tegg/dal'; + +/** + * Define Custom SQLs + * + * import { SqlMap, SqlType } from '@eggjs/tegg/dal'; + * + * export default { + * findByName: { + * type: SqlType.SELECT, + * sql: 'SELECT {{ allColumns }} from foo where name = {{ name }}' + * }, + * } + */ +export default { + findByName: { + type: 'SELECT', + sql: 'SELECT {{ allColumns }} FROM egg_foo WHERE name = {{ name }}', + }, +} as Record; diff --git a/standalone/standalone/test/fixtures/dal-module/dal/structure/Foo.json b/standalone/standalone/test/fixtures/dal-module/dal/structure/Foo.json new file mode 100644 index 00000000..1edd9fff --- /dev/null +++ b/standalone/standalone/test/fixtures/dal-module/dal/structure/Foo.json @@ -0,0 +1,421 @@ +{ + "name": "egg_foo", + "dataSourceName": "default", + "columns": [ + { + "columnName": "id", + "propertyName": "id", + "type": { + "type": "INT" + }, + "canNull": false, + "comment": "the primary key", + "autoIncrement": true, + "primaryKey": true + }, + { + "columnName": "name", + "propertyName": "name", + "type": { + "type": "VARCHAR", + "length": 100 + }, + "canNull": true, + "uniqueKey": true + }, + { + "columnName": "col1", + "propertyName": "col1", + "type": { + "type": "VARCHAR", + "length": 100 + }, + "canNull": true + }, + { + "columnName": "bit_column", + "propertyName": "bitColumn", + "type": { + "type": "BIT", + "length": 10 + }, + "canNull": true + }, + { + "columnName": "bool_column", + "propertyName": "boolColumn", + "type": { + "type": "BOOL" + }, + "canNull": true + }, + { + "columnName": "tiny_int_column", + "propertyName": "tinyIntColumn", + "type": { + "type": "TINYINT", + "length": 5, + "unsigned": true, + "zeroFill": true + }, + "canNull": true + }, + { + "columnName": "small_int_column", + "propertyName": "smallIntColumn", + "type": { + "type": "SMALLINT", + "length": 5, + "unsigned": true, + "zeroFill": true + }, + "canNull": true + }, + { + "columnName": "medium_int_column", + "propertyName": "mediumIntColumn", + "type": { + "type": "MEDIUMINT", + "length": 5, + "unsigned": true, + "zeroFill": true + }, + "canNull": true + }, + { + "columnName": "int_column", + "propertyName": "intColumn", + "type": { + "type": "INT", + "length": 5, + "unsigned": true, + "zeroFill": true + }, + "canNull": true + }, + { + "columnName": "big_int_column", + "propertyName": "bigIntColumn", + "type": { + "type": "BIGINT", + "length": 5, + "unsigned": true, + "zeroFill": true + }, + "canNull": true + }, + { + "columnName": "decimal_column", + "propertyName": "decimalColumn", + "type": { + "type": "DECIMAL", + "length": 10, + "fractionalLength": 5, + "unsigned": true, + "zeroFill": true + }, + "canNull": true + }, + { + "columnName": "float_column", + "propertyName": "floatColumn", + "type": { + "type": "FLOAT", + "length": 10, + "fractionalLength": 5, + "unsigned": true, + "zeroFill": true + }, + "canNull": true + }, + { + "columnName": "double_column", + "propertyName": "doubleColumn", + "type": { + "type": "DOUBLE", + "length": 10, + "fractionalLength": 5, + "unsigned": true, + "zeroFill": true + }, + "canNull": true + }, + { + "columnName": "date_column", + "propertyName": "dateColumn", + "type": { + "type": "DATE" + }, + "canNull": true + }, + { + "columnName": "date_time_column", + "propertyName": "dateTimeColumn", + "type": { + "type": "DATETIME", + "precision": 3 + }, + "canNull": true + }, + { + "columnName": "timestamp_column", + "propertyName": "timestampColumn", + "type": { + "type": "TIMESTAMP", + "precision": 3 + }, + "canNull": true + }, + { + "columnName": "time_column", + "propertyName": "timeColumn", + "type": { + "type": "TIME", + "precision": 3 + }, + "canNull": true + }, + { + "columnName": "year_column", + "propertyName": "yearColumn", + "type": { + "type": "YEAR" + }, + "canNull": true + }, + { + "columnName": "var_char_column", + "propertyName": "varCharColumn", + "type": { + "type": "VARCHAR", + "length": 100, + "characterSet": "utf8mb4", + "collate": "utf8mb4_unicode_ci" + }, + "canNull": true + }, + { + "columnName": "binary_column", + "propertyName": "binaryColumn", + "type": { + "type": "BINARY" + }, + "canNull": true + }, + { + "columnName": "var_binary_column", + "propertyName": "varBinaryColumn", + "type": { + "type": "VARBINARY", + "length": 100 + }, + "canNull": true + }, + { + "columnName": "tiny_blob_column", + "propertyName": "tinyBlobColumn", + "type": { + "type": "TINYBLOB" + }, + "canNull": true + }, + { + "columnName": "tiny_text_column", + "propertyName": "tinyTextColumn", + "type": { + "type": "TINYTEXT", + "characterSet": "utf8mb4", + "collate": "utf8mb4_unicode_ci" + }, + "canNull": true + }, + { + "columnName": "blob_column", + "propertyName": "blobColumn", + "type": { + "type": "BLOB", + "length": 100 + }, + "canNull": true + }, + { + "columnName": "text_column", + "propertyName": "textColumn", + "type": { + "type": "TEXT", + "length": 100, + "characterSet": "utf8mb4", + "collate": "utf8mb4_unicode_ci" + }, + "canNull": true + }, + { + "columnName": "medium_blob_column", + "propertyName": "mediumBlobColumn", + "type": { + "type": "MEDIUMBLOB" + }, + "canNull": true + }, + { + "columnName": "long_blob_column", + "propertyName": "longBlobColumn", + "type": { + "type": "LONGBLOB" + }, + "canNull": true + }, + { + "columnName": "medium_text_column", + "propertyName": "mediumTextColumn", + "type": { + "type": "MEDIUMTEXT", + "characterSet": "utf8mb4", + "collate": "utf8mb4_unicode_ci" + }, + "canNull": true + }, + { + "columnName": "long_text_column", + "propertyName": "longTextColumn", + "type": { + "type": "LONGTEXT", + "characterSet": "utf8mb4", + "collate": "utf8mb4_unicode_ci" + }, + "canNull": true + }, + { + "columnName": "enum_column", + "propertyName": "enumColumn", + "type": { + "type": "ENUM", + "enums": [ + "A", + "B" + ], + "characterSet": "utf8mb4", + "collate": "utf8mb4_unicode_ci" + }, + "canNull": true + }, + { + "columnName": "set_column", + "propertyName": "setColumn", + "type": { + "type": "SET", + "enums": [ + "A", + "B" + ], + "characterSet": "utf8mb4", + "collate": "utf8mb4_unicode_ci" + }, + "canNull": true + }, + { + "columnName": "geometry_column", + "propertyName": "geometryColumn", + "type": { + "type": "GEOMETRY" + }, + "canNull": true + }, + { + "columnName": "point_column", + "propertyName": "pointColumn", + "type": { + "type": "POINT" + }, + "canNull": true + }, + { + "columnName": "line_string_column", + "propertyName": "lineStringColumn", + "type": { + "type": "LINESTRING" + }, + "canNull": true + }, + { + "columnName": "polygon_column", + "propertyName": "polygonColumn", + "type": { + "type": "POLYGON" + }, + "canNull": true + }, + { + "columnName": "multipoint_column", + "propertyName": "multipointColumn", + "type": { + "type": "MULTIPOINT" + }, + "canNull": true + }, + { + "columnName": "multi_line_string_column", + "propertyName": "multiLineStringColumn", + "type": { + "type": "MULTILINESTRING" + }, + "canNull": true + }, + { + "columnName": "multi_polygon_column", + "propertyName": "multiPolygonColumn", + "type": { + "type": "MULTIPOLYGON" + }, + "canNull": true + }, + { + "columnName": "geometry_collection_column", + "propertyName": "geometryCollectionColumn", + "type": { + "type": "GEOMETRYCOLLECTION" + }, + "canNull": true + }, + { + "columnName": "json_column", + "propertyName": "jsonColumn", + "type": { + "type": "JSON" + }, + "canNull": true + } + ], + "indices": [ + { + "name": "idx_col1", + "keys": [ + { + "propertyName": "col1", + "columnName": "col1" + } + ], + "type": "FULLTEXT", + "comment": "index comment\n" + }, + { + "name": "uk_name_col1", + "keys": [ + { + "propertyName": "name", + "columnName": "name" + }, + { + "propertyName": "col1", + "columnName": "col1" + } + ], + "type": "UNIQUE", + "storeType": "BTREE", + "comment": "index comment\n" + } + ], + "comment": "foo table", + "characterSet": "utf8mb4", + "collate": "utf8mb4_unicode_ci" +} \ No newline at end of file diff --git a/standalone/standalone/test/fixtures/dal-module/dal/structure/Foo.sql b/standalone/standalone/test/fixtures/dal-module/dal/structure/Foo.sql new file mode 100644 index 00000000..104f7260 --- /dev/null +++ b/standalone/standalone/test/fixtures/dal-module/dal/structure/Foo.sql @@ -0,0 +1,44 @@ +CREATE TABLE IF NOT EXISTS egg_foo ( + id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'the primary key', + name VARCHAR(100) NULL UNIQUE KEY, + col1 VARCHAR(100) NULL, + bit_column BIT(10) NULL, + bool_column BOOL NULL, + tiny_int_column TINYINT(5) UNSIGNED ZEROFILL NULL, + small_int_column SMALLINT(5) UNSIGNED ZEROFILL NULL, + medium_int_column MEDIUMINT(5) UNSIGNED ZEROFILL NULL, + int_column INT(5) UNSIGNED ZEROFILL NULL, + big_int_column BIGINT(5) UNSIGNED ZEROFILL NULL, + decimal_column DECIMAL(10,5) UNSIGNED ZEROFILL NULL, + float_column FLOAT(10,5) UNSIGNED ZEROFILL NULL, + double_column DOUBLE(10,5) UNSIGNED ZEROFILL NULL, + date_column DATE NULL, + date_time_column DATETIME(3) NULL, + timestamp_column TIMESTAMP(3) NULL, + time_column TIME(3) NULL, + year_column YEAR NULL, + var_char_column VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL, + binary_column BINARY NULL, + var_binary_column VARBINARY(100) NULL, + tiny_blob_column TINYBLOB NULL, + tiny_text_column TINYTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL, + blob_column BLOB(100) NULL, + text_column TEXT(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL, + medium_blob_column MEDIUMBLOB NULL, + long_blob_column LONGBLOB NULL, + medium_text_column MEDIUMTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL, + long_text_column LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL, + enum_column ENUM('A','B') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL, + set_column SET('A','B') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL, + geometry_column GEOMETRY NULL, + point_column POINT NULL, + line_string_column LINESTRING NULL, + polygon_column POLYGON NULL, + multipoint_column MULTIPOINT NULL, + multi_line_string_column MULTILINESTRING NULL, + multi_polygon_column MULTIPOLYGON NULL, + geometry_collection_column GEOMETRYCOLLECTION NULL, + json_column JSON NULL, + FULLTEXT KEY idx_col1 (col1) COMMENT 'index comment\n', + UNIQUE KEY uk_name_col1 (name,col1) USING BTREE COMMENT 'index comment\n' +) DEFAULT CHARACTER SET utf8mb4, DEFAULT COLLATE utf8mb4_unicode_ci, COMMENT='foo table'; \ No newline at end of file diff --git a/standalone/standalone/test/fixtures/dal-module/main.ts b/standalone/standalone/test/fixtures/dal-module/main.ts new file mode 100644 index 00000000..4b693350 --- /dev/null +++ b/standalone/standalone/test/fixtures/dal-module/main.ts @@ -0,0 +1,93 @@ +import { ContextProto, Inject } from '@eggjs/tegg'; +import { Runner, MainRunner } from '@eggjs/tegg/standalone'; +import FooDAO from './dal/dao/FooDAO'; +import { Foo } from './Foo'; + + +@Runner() +@ContextProto() +export class FooRunner implements MainRunner { + @Inject() + fooDAO: FooDAO; + + async main(): Promise { + const foo = new Foo(); + foo.col1 = '2333'; + foo.name = 'test_service_worker'; + foo.bitColumn = Buffer.from([ 0, 0 ]); + foo.boolColumn = 0; + foo.tinyIntColumn = 0; + foo.smallIntColumn = 1; + foo.mediumIntColumn = 3; + foo.intColumn = 3; + foo.bigIntColumn = '00099'; + foo.decimalColumn = '00002.33333'; + foo.floatColumn = 2.3; + foo.doubleColumn = 2.3; + foo.dateColumn = new Date('2020-03-15T16:00:00.000Z'); + foo.dateTimeColumn = new Date('2024-03-16T01:26:58.677Z'); + foo.timestampColumn = new Date('2024-03-16T01:26:58.677Z'); + foo.timeColumn = '838:59:50.123'; + foo.yearColumn = 2024; + foo.varCharColumn = 'var_char'; + foo.binaryColumn = Buffer.from('b'); + foo.varBinaryColumn = Buffer.from('var_binary'); + foo.tinyBlobColumn = Buffer.from('tiny_blob'); + foo.tinyTextColumn = 'text'; + foo.blobColumn = Buffer.from('blob'); + foo.textColumn = 'text'; + foo.mediumBlobColumn = Buffer.from('medium_blob'); + foo.longBlobColumn = Buffer.from('long_blob'); + foo.mediumTextColumn = 'medium_text'; + foo.longTextColumn = 'long_text'; + foo.enumColumn = 'A'; + foo.setColumn = 'B'; + foo.geometryColumn = { x: 10, y: 10 }; + foo.pointColumn = { x: 10, y: 10 }; + foo.lineStringColumn = [ + { x: 15, y: 15 }, + { x: 20, y: 20 }, + ]; + foo.polygonColumn = [ + [ + { x: 0, y: 0 }, { x: 10, y: 0 }, { x: 10, y: 10 }, { x: 0, y: 10 }, { x: 0, y: 0 }, + ], [ + { x: 5, y: 5 }, { x: 7, y: 5 }, { x: 7, y: 7 }, { x: 5, y: 7 }, { x: 5, y: 5 }, + ], + ]; + foo.multipointColumn = [ + { x: 0, y: 0 }, { x: 20, y: 20 }, { x: 60, y: 60 }, + ]; + foo.multiLineStringColumn = [ + [ + { x: 10, y: 10 }, { x: 20, y: 20 }, + ], [ + { x: 15, y: 15 }, { x: 30, y: 15 }, + ], + ]; + foo.multiPolygonColumn = [ + [ + [ + { x: 0, y: 0 }, { x: 10, y: 0 }, { x: 10, y: 10 }, { x: 0, y: 10 }, { x: 0, y: 0 }, + ], + ], + [ + [ + { x: 5, y: 5 }, { x: 7, y: 5 }, { x: 7, y: 7 }, { x: 5, y: 7 }, { x: 5, y: 5 }, + ], + ], + ]; + foo.geometryCollectionColumn = [ + { x: 10, y: 10 }, + { x: 30, y: 30 }, + [ + { x: 15, y: 15 }, { x: 20, y: 20 }, + ], + ]; + foo.jsonColumn = { + hello: 'json', + }; + await this.fooDAO.insert(foo); + return this.fooDAO.findOneByCol1('2333'); + } +} diff --git a/standalone/standalone/test/fixtures/dal-module/module.yml b/standalone/standalone/test/fixtures/dal-module/module.yml new file mode 100644 index 00000000..191f15f1 --- /dev/null +++ b/standalone/standalone/test/fixtures/dal-module/module.yml @@ -0,0 +1,8 @@ +dataSource: + foo: + connectionLimit: 100 + database: 'test' + host: '127.0.0.1' + user: root + port: 3306 + timezone: '+08:00' diff --git a/standalone/standalone/test/fixtures/dal-module/package.json b/standalone/standalone/test/fixtures/dal-module/package.json new file mode 100644 index 00000000..d5703f1c --- /dev/null +++ b/standalone/standalone/test/fixtures/dal-module/package.json @@ -0,0 +1,9 @@ +{ + "name": "dal", + "eggModule": { + "name": "dal" + }, + "dependencies": { + "@eggjs/tegg-dal-plugin": "*" + } +} diff --git a/standalone/standalone/test/fixtures/dal-module/tsconfig.json b/standalone/standalone/test/fixtures/dal-module/tsconfig.json new file mode 100644 index 00000000..c1fad13e --- /dev/null +++ b/standalone/standalone/test/fixtures/dal-module/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "../../../tsconfig.json", + "compilerOptions": { + "skipLibCheck": true + } +} diff --git a/standalone/standalone/test/index.test.ts b/standalone/standalone/test/index.test.ts index bff3af19..8b3a3302 100644 --- a/standalone/standalone/test/index.test.ts +++ b/standalone/standalone/test/index.test.ts @@ -5,6 +5,9 @@ import { main, StandaloneContext, Runner } from '..'; import { ModuleConfigs } from '@eggjs/tegg'; import { ModuleConfig } from 'egg'; import { crosscutAdviceParams, pointcutAdviceParams } from './fixtures/aop-module/Hello'; +import { Foo } from './fixtures/dal-module/Foo'; +import { MysqlDataSource, SqlGenerator } from '@eggjs/dal-runtime'; +import { TableModel } from '@eggjs/dal-decorator'; describe('test/index.test.ts', () => { describe('simple runner', () => { @@ -212,4 +215,38 @@ describe('test/index.test.ts', () => { } }); }); + + describe('dal runner', () => { + let mysqlDataSource: MysqlDataSource; + + before(async () => { + mysqlDataSource = new MysqlDataSource({ + name: 'foo', + host: '127.0.0.1', + user: 'root', + database: 'test', + timezone: '+08:00', + initSql: 'SET GLOBAL time_zone = \'+08:00\';', + }); + await mysqlDataSource.ready(); + await mysqlDataSource.query('DROP TABLE IF EXISTS egg_foo'); + + const tableModel = TableModel.build(Foo); + + const sqlGenerator = new SqlGenerator(); + const createTableSql = sqlGenerator.generate(tableModel); + + await mysqlDataSource.query(createTableSql); + }); + + after(async () => { + await mysqlDataSource.query('DROP TABLE IF EXISTS egg_foo'); + }); + + it('should work', async () => { + const foo: Foo = await main(path.join(__dirname, './fixtures/dal-module')); + assert(foo); + assert.equal(foo.col1, '2333'); + }); + }); });