oms/backend/database/migrations/20261105074000-create-shop-database-configs.js
liufumingren eb8c94eacf feat(shop): 新增数据库店铺类型与连接/选表/字段映射配置
- 迁移 shop_database_configs:一店一条,存数据库类型/IP/端口/库名/账号/密码、
  商品表/订单表/订单明细表名、三份字段映射与最近校验结果
- 新增 shopDatabase 控制器与 lib:服务端用 mysql2 连接客户库(8s 超时、utf8mb4),
  提供配置读取/保存、测试连通、连接拉表、拉表列表与表字段;表名走白名单校验
- 保存时连得上则校验表与列真实存在(不存在拒绝保存),连不上则保存并提示未校验
- 店铺新增支持 type=3 数据库店铺;删除店铺时一并清理数据库配置
- 前端:新增店铺可选「数据库店铺」,创建后自动打开配置弹窗;列表对数据库店铺
  显示「配置」,弹窗内填写连接信息、测试连通、连接拉表、选三张表并逐个字段映射
- 契约与路由:补数据库店铺配置相关接口定义

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-05 15:44:06 +08:00

50 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 数据库店铺(shops.type=3)的连接配置与表/字段映射
*
* 数据库店铺直接连客户自有数据库拉取明文订单:连接信息(host/port/账号/密码)
* 与「商品表 / 订单表 / 订单明细表」的选表结果、字段映射都存放在本表(一店一配置)。
* 密码按明文存储(与仓库接口 app_secret 口径一致),接口不回显。
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('shop_database_configs', {
id: { type: Sequelize.BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
principal_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' },
shop_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '店铺ID(shops.id,type=3 数据库店铺)',
references: { model: 'shops', key: 'id' },
},
db_type: { type: Sequelize.STRING(20), allowNull: false, defaultValue: 'mysql', comment: '数据库类型:mysql' },
host: { type: Sequelize.STRING(255), allowNull: true, comment: '数据库地址(IP 或域名)' },
port: { type: Sequelize.INTEGER, allowNull: true, comment: '数据库端口' },
database_name: { type: Sequelize.STRING(100), allowNull: true, comment: '数据库名' },
username: { type: Sequelize.STRING(100), allowNull: true, comment: '数据库账号' },
password: { type: Sequelize.STRING(255), allowNull: true, comment: '数据库密码(明文,接口不回显)' },
product_table: { type: Sequelize.STRING(100), allowNull: true, comment: '商品表名' },
order_table: { type: Sequelize.STRING(100), allowNull: true, comment: '订单表名' },
order_item_table: { type: Sequelize.STRING(100), allowNull: true, comment: '订单明细表名' },
product_fields: { type: Sequelize.JSON, allowNull: true, comment: '商品表字段映射:{ oms字段: 源列名 }' },
order_fields: { type: Sequelize.JSON, allowNull: true, comment: '订单表字段映射:{ oms字段: 源列名 }' },
order_item_fields: { type: Sequelize.JSON, allowNull: true, comment: '订单明细表字段映射:{ oms字段: 源列名 }' },
last_test_at: { type: Sequelize.DATE, allowNull: true, comment: '最近一次测试连通时间' },
last_test_success: { type: Sequelize.TINYINT, allowNull: true, comment: '最近一次测试结果:1成功 0失败' },
last_test_message: { type: Sequelize.STRING(500), allowNull: true, comment: '最近一次测试结果说明' },
created_at: { type: Sequelize.DATE, allowNull: true, comment: '创建时间' },
updated_at: { type: Sequelize.DATE, allowNull: true, comment: '更新时间' },
});
await queryInterface.addIndex('shop_database_configs', [ 'principal_id', 'shop_id' ], {
name: 'uk_shop_database_configs_shop',
unique: true,
});
},
async down(queryInterface) {
await queryInterface.dropTable('shop_database_configs');
},
};