Browse Source

Merge branch 'develop' of https://github.com/nocodb/nocodb into develop

pull/1710/head
Wing-Kam Wong 2 years ago
parent
commit
87c553811a
  1. 4
      packages/nocodb/src/lib/noco-models/Column.ts
  2. 3
      packages/nocodb/src/lib/noco-models/LinkToAnotherRecordColumn.ts
  3. 70
      packages/nocodb/src/lib/noco/meta/api/columnApis.ts
  4. 4
      packages/nocodb/src/lib/noco/meta/api/dataApis/dataAliasNestedApis.ts
  5. 14
      packages/nocodb/src/lib/noco/meta/api/metaDiffApis.ts
  6. 70
      packages/nocodb/src/lib/noco/upgrader/jobs/ncProjectUpgraderV2_0090000.ts
  7. 4
      packages/nocodb/src/lib/utils/projectAcl.ts

4
packages/nocodb/src/lib/noco-models/Column.ts

@ -207,7 +207,9 @@ export default class Column<T = any> implements ColumnType {
dr: column.dr, dr: column.dr,
fk_index_name: column.fk_index_name, fk_index_name: column.fk_index_name,
fk_related_model_id: column.fk_related_model_id fk_related_model_id: column.fk_related_model_id,
virtual: column.virtual
}, },
ncMeta ncMeta
); );

3
packages/nocodb/src/lib/noco-models/LinkToAnotherRecordColumn.ts

@ -107,7 +107,8 @@ export default class LinkToAnotherRecordColumn {
dr: data.dr, dr: data.dr,
fk_index_name: data.fk_index_name, fk_index_name: data.fk_index_name,
fk_related_model_id: data.fk_related_model_id fk_related_model_id: data.fk_related_model_id,
virtual: data.virtual
}); });
return this.read(data.fk_column_id, ncMeta); return this.read(data.fk_column_id, ncMeta);
} }

70
packages/nocodb/src/lib/noco/meta/api/columnApis.ts

@ -45,6 +45,7 @@ async function createHmAndBtColumn(
childColumn: Column, childColumn: Column,
type?: RelationTypes, type?: RelationTypes,
alias?: string, alias?: string,
virtual = false,
isSystemCol = false isSystemCol = false
) { ) {
// save bt column // save bt column
@ -65,6 +66,7 @@ async function createHmAndBtColumn(
fk_child_column_id: childColumn.id, fk_child_column_id: childColumn.id,
fk_parent_column_id: parent.primaryKey.id, fk_parent_column_id: parent.primaryKey.id,
fk_related_model_id: parent.id, fk_related_model_id: parent.id,
virtual,
system: isSystemCol system: isSystemCol
}); });
} }
@ -82,6 +84,7 @@ async function createHmAndBtColumn(
fk_child_column_id: childColumn.id, fk_child_column_id: childColumn.id,
fk_parent_column_id: parent.primaryKey.id, fk_parent_column_id: parent.primaryKey.id,
fk_related_model_id: child.id, fk_related_model_id: child.id,
virtual,
system: isSystemCol system: isSystemCol
}); });
} }
@ -275,23 +278,27 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
childColumn = await Column.get({ colId: id }); childColumn = await Column.get({ colId: id });
// create relation // ignore relation creation if virtual
await sqlMgr.sqlOpPlus(base, 'relationCreate', { if (!req.body.virtual) {
childColumn: fkColName, // create relation
childTable: child.table_name, await sqlMgr.sqlOpPlus(base, 'relationCreate', {
parentTable: parent.table_name, childColumn: fkColName,
onDelete: 'NO ACTION', childTable: child.table_name,
onUpdate: 'NO ACTION', parentTable: parent.table_name,
type: 'real', onDelete: 'NO ACTION',
parentColumn: parent.primaryKey.column_name onUpdate: 'NO ACTION',
}); type: 'real',
parentColumn: parent.primaryKey.column_name
});
}
} }
await createHmAndBtColumn( await createHmAndBtColumn(
child, child,
parent, parent,
childColumn, childColumn,
req.body.type, req.body.type,
req.body.title req.body.title,
req.body.virtual
); );
} else if (req.body.type === 'mm') { } else if (req.body.type === 'mm') {
const aTn = `${project?.prefix ?? ''}_nc_m2m_${randomID()}`; const aTn = `${project?.prefix ?? ''}_nc_m2m_${randomID()}`;
@ -352,26 +359,27 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
columns: associateTableCols columns: associateTableCols
}); });
const rel1Args = { if (!req.body.virtual) {
...req.body, const rel1Args = {
childTable: aTn, ...req.body,
childColumn: parentCn, childTable: aTn,
parentTable: parent.table_name, childColumn: parentCn,
parentColumn: parentPK.column_name, parentTable: parent.table_name,
type: 'real' parentColumn: parentPK.column_name,
}; type: 'real'
const rel2Args = { };
...req.body, const rel2Args = {
childTable: aTn, ...req.body,
childColumn: childCn, childTable: aTn,
parentTable: child.table_name, childColumn: childCn,
parentColumn: childPK.column_name, parentTable: child.table_name,
type: 'real' parentColumn: childPK.column_name,
}; type: 'real'
};
await sqlMgr.sqlOpPlus(base, 'relationCreate', rel1Args);
await sqlMgr.sqlOpPlus(base, 'relationCreate', rel2Args);
await sqlMgr.sqlOpPlus(base, 'relationCreate', rel1Args);
await sqlMgr.sqlOpPlus(base, 'relationCreate', rel2Args);
}
const parentCol = (await assocModel.getColumns())?.find( const parentCol = (await assocModel.getColumns())?.find(
c => c.column_name === parentCn c => c.column_name === parentCn
); );
@ -385,6 +393,7 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
childCol, childCol,
null, null,
null, null,
req.body.virtual,
true true
); );
await createHmAndBtColumn( await createHmAndBtColumn(
@ -393,6 +402,7 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
parentCol, parentCol,
null, null,
null, null,
req.body.virtual,
true true
); );

4
packages/nocodb/src/lib/noco/meta/api/dataApis/dataAliasNestedApis.ts

@ -191,7 +191,7 @@ export async function hmList(req: Request, res: Response, next) {
} }
//@ts-ignore //@ts-ignore
async function relationDataDelete(req, res) { async function relationDataRemove(req, res) {
const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); const { model, view } = await getViewAndModelFromRequestByAliasOrId(req);
if (!model) NcError.notFound('Table not found'); if (!model) NcError.notFound('Table not found');
@ -273,7 +273,7 @@ router.post(
); );
router.delete( router.delete(
'/api/v1/db/data/:orgs/:projectName/:tableName/:rowId/:relationType/:columnName/:refRowId', '/api/v1/db/data/:orgs/:projectName/:tableName/:rowId/:relationType/:columnName/:refRowId',
ncMetaAclMw(relationDataDelete, 'relationDataDelete') ncMetaAclMw(relationDataRemove, 'relationDataRemove')
); );
router.get( router.get(

14
packages/nocodb/src/lib/noco/meta/api/metaDiffApis.ts

@ -31,8 +31,6 @@ export enum MetaDiffType {
VIEW_COLUMN_REMOVE = 'VIEW_COLUMN_REMOVE', VIEW_COLUMN_REMOVE = 'VIEW_COLUMN_REMOVE',
TABLE_RELATION_ADD = 'TABLE_RELATION_ADD', TABLE_RELATION_ADD = 'TABLE_RELATION_ADD',
TABLE_RELATION_REMOVE = 'TABLE_RELATION_REMOVE', TABLE_RELATION_REMOVE = 'TABLE_RELATION_REMOVE',
// TABLE_VIRTUAL_RELATION_ADD = 'TABLE_VIRTUAL_RELATION_ADD',
// TABLE_VIRTUAL_RELATION_REMOVE = 'TABLE_VIRTUAL_RELATION_REMOVE',
TABLE_VIRTUAL_M2M_REMOVE = 'TABLE_VIRTUAL_M2M_REMOVE' TABLE_VIRTUAL_M2M_REMOVE = 'TABLE_VIRTUAL_M2M_REMOVE'
} }
@ -718,12 +716,12 @@ export async function metaDiffSync(req, res) {
res.json({ msg: 'success' }); res.json({ msg: 'success' });
} }
async function isMMRelationAvailable( async function isMMRelationExist(
model: Model, model: Model,
assocModel: Model, assocModel: Model,
belongsToCol: Column<LinkToAnotherRecordColumn> belongsToCol: Column<LinkToAnotherRecordColumn>
) { ) {
let isAvail = false; let isExist = false;
const colChildOpt = await belongsToCol.getColOptions< const colChildOpt = await belongsToCol.getColOptions<
LinkToAnotherRecordColumn LinkToAnotherRecordColumn
>(); >();
@ -737,12 +735,12 @@ async function isMMRelationAvailable(
colOpt.fk_child_column_id === colChildOpt.fk_parent_column_id && colOpt.fk_child_column_id === colChildOpt.fk_parent_column_id &&
colOpt.fk_mm_child_column_id === colChildOpt.fk_child_column_id colOpt.fk_mm_child_column_id === colChildOpt.fk_child_column_id
) { ) {
isAvail = true; isExist = true;
break; break;
} }
} }
} }
return isAvail; return isExist;
} }
// @ts-ignore // @ts-ignore
@ -772,12 +770,12 @@ export async function extractAndGenerateManyToManyRelations(
await modelB.getColumns(); await modelB.getColumns();
// check tableA already have the relation or not // check tableA already have the relation or not
const isRelationAvailInA = await isMMRelationAvailable( const isRelationAvailInA = await isMMRelationExist(
modelA, modelA,
assocModel, assocModel,
belongsToCols[0] belongsToCols[0]
); );
const isRelationAvailInB = await isMMRelationAvailable( const isRelationAvailInB = await isMMRelationExist(
modelB, modelB,
assocModel, assocModel,
belongsToCols[1] belongsToCols[1]

70
packages/nocodb/src/lib/noco/upgrader/jobs/ncProjectUpgraderV2_0090000.ts

@ -312,12 +312,36 @@ const filterV1toV2CompOpMap = {
'is not like': 'nlike' 'is not like': 'nlike'
}; };
interface Relationv1 {
project_id?: string;
db_alias?: string;
tn?: string;
rtn?: string;
_tn?: string;
_rtn?: string;
cn?: string;
rcn?: string;
_cn?: string;
_rcn?: string;
referenced_db_alias?: string;
type?: string;
db_type?: string;
ur?: string;
dr?: string;
}
async function migrateProjectModels( async function migrateProjectModels(
ncMeta = Noco.ncMeta ncMeta = Noco.ncMeta
): Promise<MigrateCtxV1> { ): Promise<MigrateCtxV1> {
// @ts-ignore // @ts-ignore
const metas: ModelMetav1[] = await ncMeta.metaList(null, null, 'nc_models'); const metas: ModelMetav1[] = await ncMeta.metaList(null, null, 'nc_models');
// @ts-ignore
const relations: Relationv1[] = await ncMeta.metaList(
null,
null,
'nc_relations'
);
const models: Model[] = []; const models: Model[] = [];
// variable for keeping all // variable for keeping all
@ -443,6 +467,37 @@ async function migrateProjectModels(
fk_mm_parent_column_id = fk_mm_parent_column_id =
projectModelColumnRefs[rel.vtn][rel.vrcn].id; projectModelColumnRefs[rel.vtn][rel.vrcn].id;
} }
let virtual = false;
if (columnMeta.mm) {
const relation = relations.find(
r =>
r.rtn === columnMeta.mm.tn &&
r.rcn === columnMeta.mm.cn &&
r.tn === columnMeta.mm.vtn &&
r.cn === columnMeta.mm.vcn
);
virtual = relation?.type === 'virtual';
} else if (columnMeta.hm) {
virtual =
relations.find(
r =>
r.rtn === columnMeta.hm.rtn &&
r.tn === columnMeta.hm.tn &&
r.rcn === columnMeta.hm.rcn &&
r.cn === columnMeta.hm.cn
)?.type === 'virtual';
} else if (columnMeta.bt) {
virtual =
relations.find(
r =>
r.rtn === columnMeta.bt.rtn &&
r.tn === columnMeta.bt.tn &&
r.rcn === columnMeta.bt.rcn &&
r.cn === columnMeta.bt.cn
)?.type === 'virtual';
}
const column = await Column.insert<LinkToAnotherRecordColumn>( const column = await Column.insert<LinkToAnotherRecordColumn>(
{ {
project_id: project.id, project_id: project.id,
@ -460,7 +515,8 @@ async function migrateProjectModels(
fk_mm_model_id, fk_mm_model_id,
fk_mm_child_column_id, fk_mm_child_column_id,
fk_mm_parent_column_id, fk_mm_parent_column_id,
fk_related_model_id: columnMeta.hm ? tnId : rtnId fk_related_model_id: columnMeta.hm ? tnId : rtnId,
virtual
}, },
ncMeta ncMeta
); );
@ -636,6 +692,15 @@ async function migrateProjectModels(
// const rtnId = projectModelRefs?.[rel.rtn]?.id; // const rtnId = projectModelRefs?.[rel.rtn]?.id;
const virtual =
relations.find(
r =>
r.rtn === rel.rtn &&
r.tn === rel.tn &&
r.rcn === rel.rcn &&
r.cn === rel.cn
)?.type === 'virtual';
const column = await Column.insert<LinkToAnotherRecordColumn>( const column = await Column.insert<LinkToAnotherRecordColumn>(
{ {
project_id: project.id, project_id: project.id,
@ -651,7 +716,8 @@ async function migrateProjectModels(
ur: rel.ur, ur: rel.ur,
dr: rel.dr, dr: rel.dr,
fk_related_model_id: tnId, fk_related_model_id: tnId,
system: true system: true,
virtual
}, },
ncMeta ncMeta
); );

4
packages/nocodb/src/lib/utils/projectAcl.ts

@ -122,7 +122,9 @@ export default {
bulkDataUpdate: true, bulkDataUpdate: true,
bulkDataUpdateAll: true, bulkDataUpdateAll: true,
bulkDataDelete: true, bulkDataDelete: true,
bulkDataDeleteAll: true bulkDataDeleteAll: true,
relationDataRemove: true,
relationDataAdd: true
}, },
commenter: { commenter: {
formViewGet: true, formViewGet: true,

Loading…
Cancel
Save