import { getMysqlFetchers } from '../fetchers/mysql'; import { runCase, models } from './basecase'; import { DEFAULT_DB_FIELDS } from '@tcwd/weda-utils'; import * as dotenv from 'dotenv'; import * as pt from 'path'; dotenv.config({ path: pt.join(__dirname, '.env'), }); const dbName = 'weda_datasource_test'; let fetchers, knex, initKnex; const connection = { user: process.env.MYSQL_USER, password: process.env.MYSQL_PASSWORD, host: process.env.MYSQL_HOST, port: process.env.MYSQL_PORT, database: dbName, }; jest.setTimeout(10000); beforeAll(async () => { initKnex = getMysqlFetchers({ connection: { ...connection, database: 'mysql', }, }).knex; try { await initKnex.raw(`CREATE DATABASE ${dbName}`); } catch (e) { console.warn(e); } const res = getMysqlFetchers({ connection, }); fetchers = res.fetchers; knex = res.knex; await Promise.all( models.map((d) => knex.schema.createTable(d.name, async (table) => { const properties = d.schema.properties; table.string('_id').notNullable().primary(); table.bigInteger('createdAt'); table.bigInteger('updatedAt'); Object.keys(properties).forEach((key) => { const property = properties[key]; if (!DEFAULT_DB_FIELDS.includes(key)) { // TODO 补充字段类型映射 switch (property.type) { case 'string': table.string(key); break; // 关系型数据源不支持嵌套类型 case 'array': case 'object': table.jsonb(key); break; default: table.string(key); break; } } }); }), ), ); }); afterAll(async () => { try { await Promise.all(models.map((d) => knex.schema.dropTable(d.name))); await knex.destroy(); await initKnex.raw(`DROP DATABASE ${dbName}`); await initKnex.destroy(); } catch (e) { console.warn(e); } }); runCase(fetchers);