mirror of
https://github.com/cool-team-official/cool-admin-midway-packages.git
synced 2025-12-15 08:22:49 +00:00
模块数据支持导入子数据
This commit is contained in:
parent
fdb993683e
commit
99aa383e0b
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cool-midway/core",
|
"name": "@cool-midway/core",
|
||||||
"version": "7.1.3",
|
"version": "7.1.4",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "index.d.ts",
|
"typings": "index.d.ts",
|
||||||
|
|||||||
@ -103,24 +103,8 @@ export class CoolModuleImport {
|
|||||||
// 导入数据
|
// 导入数据
|
||||||
for (const key in data) {
|
for (const key in data) {
|
||||||
try {
|
try {
|
||||||
const repository = this.defaultDataSource.getRepository(
|
|
||||||
metadatas[key]
|
|
||||||
);
|
|
||||||
if (this.ormConfig.default.type == "postgres") {
|
|
||||||
for (const item of data[key]) {
|
for (const item of data[key]) {
|
||||||
const result: any = await repository.save(
|
await this.importData(metadatas, item, key);
|
||||||
repository.create(item)
|
|
||||||
);
|
|
||||||
if (item.id) {
|
|
||||||
await repository.update(result.id, { id: item.id });
|
|
||||||
// 更新pgsql序列
|
|
||||||
await this.defaultDataSource.query(
|
|
||||||
`SELECT setval('${key}_id_seq', (SELECT MAX(id) FROM ${key}));`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
await repository.insert(data[key]);
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.coreLogger.error(
|
this.coreLogger.error(
|
||||||
@ -140,4 +124,55 @@ export class CoolModuleImport {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导入数据
|
||||||
|
* @param metadatas
|
||||||
|
* @param datas
|
||||||
|
* @param tableName
|
||||||
|
*/
|
||||||
|
async importData(
|
||||||
|
metadatas: any[],
|
||||||
|
item: any,
|
||||||
|
tableName: string,
|
||||||
|
parentItem: any = null
|
||||||
|
) {
|
||||||
|
const repository = this.defaultDataSource.getRepository(
|
||||||
|
metadatas[tableName]
|
||||||
|
);
|
||||||
|
// 处理当前项中的引用
|
||||||
|
if (parentItem) {
|
||||||
|
for (const key in item) {
|
||||||
|
if (typeof item[key] === "string" && item[key].startsWith("@")) {
|
||||||
|
const parentKey = item[key].substring(1); // 移除"@"符号
|
||||||
|
if (parentItem.hasOwnProperty(parentKey)) {
|
||||||
|
item[key] = parentItem[parentKey];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 插入当前项到数据库
|
||||||
|
let insertedItem;
|
||||||
|
if (this.ormConfig.default.type == "postgres") {
|
||||||
|
insertedItem = await repository.save(repository.create(item));
|
||||||
|
if (item.id) {
|
||||||
|
await repository.update(insertedItem.id, { id: item.id });
|
||||||
|
await this.defaultDataSource.query(
|
||||||
|
`SELECT setval('${tableName}_id_seq', (SELECT MAX(id) FROM ${tableName}));`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
insertedItem = await repository.insert(item);
|
||||||
|
}
|
||||||
|
// 递归处理@childDatas
|
||||||
|
if (!_.isEmpty(item["@childDatas"])) {
|
||||||
|
const childDatas = item["@childDatas"];
|
||||||
|
delete item["@childDatas"];
|
||||||
|
for (const childKey in childDatas) {
|
||||||
|
for (const childItem of childDatas[childKey]) {
|
||||||
|
await this.importData(metadatas, childItem, childKey, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cool-midway/core",
|
"name": "@cool-midway/core",
|
||||||
"version": "7.1.3",
|
"version": "7.1.4",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"typings": "index.d.ts",
|
"typings": "index.d.ts",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user