fix(data-template): filter out foreign keys (#2033)
This commit is contained in:
parent
dd9cb723d4
commit
47859c0f99
@ -20,6 +20,7 @@ type TraverseOptions = {
|
|||||||
exclude?: string[];
|
exclude?: string[];
|
||||||
include?: string[];
|
include?: string[];
|
||||||
through?: string;
|
through?: string;
|
||||||
|
excludePk?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
const traverseHasMany = (arr: any[], { collection, exclude = [], include = [] }: TraverseOptions) => {
|
const traverseHasMany = (arr: any[], { collection, exclude = [], include = [] }: TraverseOptions) => {
|
||||||
@ -41,7 +42,10 @@ const traverseBelongsToMany = (arr: any[], { collection, exclude = [], through }
|
|||||||
} else {
|
} else {
|
||||||
delete item[through];
|
delete item[through];
|
||||||
}
|
}
|
||||||
return item;
|
return traverseJSON(item, {
|
||||||
|
collection,
|
||||||
|
excludePk: false,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -59,7 +63,10 @@ const parseInclude = (keys: string[]) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const traverseJSON = (data, options: TraverseOptions) => {
|
const traverseJSON = (data, options: TraverseOptions) => {
|
||||||
const { collection, exclude = [], include = [] } = options;
|
if (!data) {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
const { collection, exclude = [], include = [], excludePk = true } = options;
|
||||||
const map = parseInclude(include);
|
const map = parseInclude(include);
|
||||||
const result = {};
|
const result = {};
|
||||||
for (const key of Object.keys(data || {})) {
|
for (const key of Object.keys(data || {})) {
|
||||||
@ -78,7 +85,10 @@ const traverseJSON = (data, options: TraverseOptions) => {
|
|||||||
result[key] = data[key];
|
result[key] = data[key];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (field.options.primaryKey) {
|
if (field.options.primaryKey && excludePk) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (field.options.isForeignKey) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (['sort', 'password', 'sequence'].includes(field.type)) {
|
if (['sort', 'password', 'sequence'].includes(field.type)) {
|
||||||
@ -97,7 +107,12 @@ const traverseJSON = (data, options: TraverseOptions) => {
|
|||||||
include: subInclude,
|
include: subInclude,
|
||||||
});
|
});
|
||||||
} else if (field.type === 'belongsTo') {
|
} else if (field.type === 'belongsTo') {
|
||||||
result[key] = data[key];
|
result[key] = traverseJSON(data[key], {
|
||||||
|
collection: collection.db.getCollection(field.target),
|
||||||
|
// exclude: [field.foreignKey],
|
||||||
|
include: subInclude,
|
||||||
|
excludePk: false,
|
||||||
|
});
|
||||||
} else if (field.type === 'belongsToMany') {
|
} else if (field.type === 'belongsToMany') {
|
||||||
result[key] = traverseBelongsToMany(data[key], {
|
result[key] = traverseBelongsToMany(data[key], {
|
||||||
collection: collection.db.getCollection(field.target),
|
collection: collection.db.getCollection(field.target),
|
||||||
|
Loading…
Reference in New Issue
Block a user