mirror of https://github.com/nocodb/nocodb
10 changed files with 277 additions and 9 deletions
@ -0,0 +1,34 @@
|
||||
import { MetaTable } from '../../utils/globals'; |
||||
|
||||
const up = async (knex) => { |
||||
await knex.schema.createTable(MetaTable.MAP_VIEW_COLUMNS, (table) => { |
||||
table.string('id', 20).primary().notNullable(); |
||||
|
||||
table.string('base_id', 20); |
||||
// table.foreign('base_id').references(`${MetaTable.BASES}.id`);
|
||||
table.string('project_id', 128); |
||||
// table.foreign('project_id').references(`${MetaTable.PROJECT}.id`);
|
||||
|
||||
table.string('fk_view_id', 20); |
||||
table.foreign('fk_view_id').references(`${MetaTable.MAP_VIEW}.fk_view_id`); |
||||
table.string('fk_column_id', 20); |
||||
table.foreign('fk_column_id').references(`${MetaTable.COLUMNS}.id`); |
||||
|
||||
table.string('uuid'); |
||||
|
||||
// todo: type
|
||||
table.string('label'); |
||||
table.string('help'); |
||||
|
||||
table.boolean('show'); |
||||
table.float('order'); |
||||
|
||||
table.timestamps(true, true); |
||||
}); |
||||
}; |
||||
|
||||
const down = async (knex) => { |
||||
await knex.schema.dropTable(MetaTable.MAP_VIEW_COLUMNS); |
||||
}; |
||||
|
||||
export { up, down }; |
@ -0,0 +1,106 @@
|
||||
import Noco from '../Noco'; |
||||
import { CacheGetType, CacheScope, MetaTable } from '../utils/globals'; |
||||
import View from './View'; |
||||
import NocoCache from '../cache/NocoCache'; |
||||
|
||||
export default class MapViewColumn { |
||||
id: string; |
||||
title?: string; |
||||
show?: boolean; |
||||
order?: number; |
||||
|
||||
fk_view_id: string; |
||||
fk_column_id: string; |
||||
project_id?: string; |
||||
base_id?: string; |
||||
|
||||
constructor(data: MapViewColumn) { |
||||
Object.assign(this, data); |
||||
} |
||||
|
||||
public static async get(mapViewColumnId: string, ncMeta = Noco.ncMeta) { |
||||
let view = |
||||
mapViewColumnId && |
||||
(await NocoCache.get( |
||||
`${CacheScope.MAP_VIEW_COLUMN}:${mapViewColumnId}`, |
||||
CacheGetType.TYPE_OBJECT |
||||
)); |
||||
if (!view) { |
||||
view = await ncMeta.metaGet2( |
||||
null, |
||||
null, |
||||
MetaTable.MAP_VIEW_COLUMNS, |
||||
mapViewColumnId |
||||
); |
||||
await NocoCache.set( |
||||
`${CacheScope.MAP_VIEW_COLUMN}:${mapViewColumnId}`, |
||||
view |
||||
); |
||||
} |
||||
return view && new MapViewColumn(view); |
||||
} |
||||
static async insert(column: Partial<MapViewColumn>, ncMeta = Noco.ncMeta) { |
||||
const insertObj = { |
||||
fk_view_id: column.fk_view_id, |
||||
fk_column_id: column.fk_column_id, |
||||
order: await ncMeta.metaGetNextOrder(MetaTable.MAP_VIEW_COLUMNS, { |
||||
fk_view_id: column.fk_view_id, |
||||
}), |
||||
show: column.show, |
||||
project_id: column.project_id, |
||||
base_id: column.base_id, |
||||
}; |
||||
|
||||
if (!(column.project_id && column.base_id)) { |
||||
const viewRef = await View.get(column.fk_view_id, ncMeta); |
||||
insertObj.project_id = viewRef.project_id; |
||||
insertObj.base_id = viewRef.base_id; |
||||
} |
||||
|
||||
const { id, fk_column_id } = await ncMeta.metaInsert2( |
||||
null, |
||||
null, |
||||
MetaTable.MAP_VIEW_COLUMNS, |
||||
insertObj |
||||
); |
||||
|
||||
await NocoCache.set(`${CacheScope.MAP_VIEW_COLUMN}:${fk_column_id}`, id); |
||||
|
||||
// if cache is not present skip pushing it into the list to avoid unexpected behaviour
|
||||
if ( |
||||
(await NocoCache.getList(CacheScope.MAP_VIEW_COLUMN, [column.fk_view_id])) |
||||
?.length |
||||
) |
||||
await NocoCache.appendToList( |
||||
CacheScope.MAP_VIEW_COLUMN, |
||||
[column.fk_view_id], |
||||
`${CacheScope.MAP_VIEW_COLUMN}:${id}` |
||||
); |
||||
|
||||
return this.get(id, ncMeta); |
||||
} |
||||
|
||||
public static async list( |
||||
viewId: string, |
||||
ncMeta = Noco.ncMeta |
||||
): Promise<MapViewColumn[]> { |
||||
let views = await NocoCache.getList(CacheScope.MAP_VIEW_COLUMN, [viewId]); |
||||
if (!views.length) { |
||||
views = await ncMeta.metaList2(null, null, MetaTable.MAP_VIEW_COLUMNS, { |
||||
condition: { |
||||
fk_view_id: viewId, |
||||
}, |
||||
orderBy: { |
||||
order: 'asc', |
||||
}, |
||||
}); |
||||
await NocoCache.setList(CacheScope.MAP_VIEW_COLUMN, [viewId], views); |
||||
} |
||||
views.sort( |
||||
(a, b) => |
||||
(a.order != null ? a.order : Infinity) - |
||||
(b.order != null ? b.order : Infinity) |
||||
); |
||||
return views?.map((v) => new MapViewColumn(v)); |
||||
} |
||||
} |
Loading…
Reference in new issue