fff11df2ba
* feat: add schema prefix to view only if view name exists * chore: view-collection name without schema prefix * chore: skip list views already connected * chore: comment * fix: update field error * refactor: collection edit can not config fields * fix: viewName set * fix: transaction * refactor: viewname * chore: collection view name * refactor: viewname * chore: remove rename view collection name * refactor: loading wthen action submjit --------- Co-authored-by: katherinehhh <katherine_15995@163.com>
74 lines
2.0 KiB
TypeScript
74 lines
2.0 KiB
TypeScript
import { Database, ViewFieldInference } from '@nocobase/database';
|
|
|
|
export default {
|
|
name: 'dbViews',
|
|
actions: {
|
|
async get(ctx, next) {
|
|
const { filterByTk, schema } = ctx.action.params;
|
|
const db = ctx.app.db as Database;
|
|
|
|
const fields = await ViewFieldInference.inferFields({
|
|
db,
|
|
viewName: filterByTk,
|
|
viewSchema: schema,
|
|
});
|
|
|
|
ctx.body = {
|
|
fields,
|
|
sources: [
|
|
...new Set(
|
|
Object.values(fields)
|
|
.map((field) => field.source)
|
|
.filter(Boolean)
|
|
.map((source) => source.split('.')[0]),
|
|
),
|
|
],
|
|
};
|
|
|
|
await next();
|
|
},
|
|
|
|
list: async function (ctx, next) {
|
|
const db = ctx.app.db as Database;
|
|
const dbViews = await db.queryInterface.listViews();
|
|
|
|
const viewCollections = Array.from(db.collections.values()).filter((collection) => collection.isView());
|
|
|
|
ctx.body = dbViews
|
|
.map((dbView) => {
|
|
return {
|
|
...dbView,
|
|
};
|
|
})
|
|
.filter((dbView) => {
|
|
// if view is connected, skip
|
|
return !viewCollections.find((collection) => {
|
|
const viewName = dbView.name;
|
|
const schema = dbView.schema;
|
|
|
|
const collectionViewName = collection.options.viewName || collection.options.name;
|
|
|
|
return collectionViewName === viewName && collection.options.schema === schema;
|
|
});
|
|
});
|
|
|
|
await next();
|
|
},
|
|
|
|
async query(ctx, next) {
|
|
const { filterByTk, schema = 'public', page = 1, pageSize = 10 } = ctx.action.params;
|
|
|
|
const offset = (page - 1) * pageSize;
|
|
const limit = 1 * pageSize;
|
|
|
|
const sql = `SELECT *
|
|
FROM ${ctx.app.db.utils.quoteTable(
|
|
ctx.app.db.utils.addSchema(filterByTk, schema),
|
|
)} LIMIT ${limit} OFFSET ${offset}`;
|
|
|
|
ctx.body = await ctx.app.db.sequelize.query(sql, { type: 'SELECT' });
|
|
await next();
|
|
},
|
|
},
|
|
};
|