mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
275 lines
6.5 KiB
275 lines
6.5 KiB
2 years ago
|
import { UITypes } from 'nocodb-sdk';
|
||
2 years ago
|
import request from 'supertest';
|
||
2 years ago
|
import Column from '../../../src/lib/models/Column';
|
||
|
import FormViewColumn from '../../../src/lib/models/FormViewColumn';
|
||
|
import GalleryViewColumn from '../../../src/lib/models/GalleryViewColumn';
|
||
|
import GridViewColumn from '../../../src/lib/models/GridViewColumn';
|
||
|
import Model from '../../../src/lib/models/Model';
|
||
|
import Project from '../../../src/lib/models/Project';
|
||
|
import View from '../../../src/lib/models/View';
|
||
2 years ago
|
import { isSqlite, isPg } from '../init/db';
|
||
2 years ago
|
|
||
2 years ago
|
const defaultColumns = function (context) {
|
||
2 years ago
|
return [
|
||
2 years ago
|
{
|
||
|
column_name: 'id',
|
||
|
title: 'Id',
|
||
|
uidt: 'ID',
|
||
|
},
|
||
|
{
|
||
|
column_name: 'title',
|
||
|
title: 'Title',
|
||
|
uidt: 'SingleLineText',
|
||
|
},
|
||
|
{
|
||
2 years ago
|
cdf: isPg(context) ? 'now()' : 'CURRENT_TIMESTAMP',
|
||
2 years ago
|
column_name: 'created_at',
|
||
|
title: 'CreatedAt',
|
||
|
dtxp: '',
|
||
|
dtxs: '',
|
||
|
uidt: 'DateTime',
|
||
2 years ago
|
dt: isPg(context) ? 'timestamp without time zone' : undefined,
|
||
2 years ago
|
},
|
||
|
{
|
||
|
cdf: isSqlite(context)
|
||
|
? 'CURRENT_TIMESTAMP'
|
||
2 years ago
|
: isPg(context)
|
||
|
? 'now()'
|
||
2 years ago
|
: 'CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP',
|
||
|
column_name: 'updated_at',
|
||
|
title: 'UpdatedAt',
|
||
|
dtxp: '',
|
||
|
dtxs: '',
|
||
|
uidt: 'DateTime',
|
||
2 years ago
|
dt: isPg(context) ? 'timestamp without time zone' : undefined,
|
||
2 years ago
|
},
|
||
|
];
|
||
2 years ago
|
};
|
||
2 years ago
|
|
||
2 years ago
|
const createColumn = async (context, table, columnAttr) => {
|
||
2 years ago
|
await request(context.app)
|
||
2 years ago
|
.post(`/api/v1/db/meta/tables/${table.id}/columns`)
|
||
2 years ago
|
.set('xc-auth', context.token)
|
||
2 years ago
|
.send({
|
||
|
...columnAttr,
|
||
|
});
|
||
2 years ago
|
|
||
2 years ago
|
const column: Column = (await table.getColumns()).find(
|
||
2 years ago
|
(column) => column.title === columnAttr.title
|
||
|
);
|
||
|
return column;
|
||
2 years ago
|
};
|
||
|
|
||
2 years ago
|
const createRollupColumn = async (
|
||
|
context,
|
||
|
{
|
||
|
project,
|
||
|
title,
|
||
|
rollupFunction,
|
||
2 years ago
|
table,
|
||
|
relatedTableName,
|
||
|
relatedTableColumnTitle,
|
||
2 years ago
|
}: {
|
||
2 years ago
|
project: Project;
|
||
2 years ago
|
title: string;
|
||
|
rollupFunction: string;
|
||
2 years ago
|
table: Model;
|
||
|
relatedTableName: string;
|
||
|
relatedTableColumnTitle: string;
|
||
2 years ago
|
}
|
||
|
) => {
|
||
2 years ago
|
const childBases = await project.getBases();
|
||
2 years ago
|
const childTable = await Model.getByIdOrName({
|
||
|
project_id: project.id,
|
||
2 years ago
|
base_id: childBases[0].id!,
|
||
2 years ago
|
table_name: relatedTableName,
|
||
2 years ago
|
});
|
||
|
const childTableColumns = await childTable.getColumns();
|
||
|
const childTableColumn = await childTableColumns.find(
|
||
2 years ago
|
(column) => column.title === relatedTableColumnTitle
|
||
2 years ago
|
);
|
||
|
|
||
2 years ago
|
const ltarColumn = (await table.getColumns()).find(
|
||
2 years ago
|
(column) =>
|
||
|
column.uidt === UITypes.LinkToAnotherRecord &&
|
||
|
column.colOptions?.fk_related_model_id === childTable.id
|
||
|
);
|
||
2 years ago
|
|
||
|
const rollupColumn = await createColumn(context, table, {
|
||
2 years ago
|
title: title,
|
||
|
uidt: UITypes.Rollup,
|
||
|
fk_relation_column_id: ltarColumn?.id,
|
||
|
fk_rollup_column_id: childTableColumn?.id,
|
||
|
rollup_function: rollupFunction,
|
||
2 years ago
|
table_name: table.table_name,
|
||
2 years ago
|
column_name: title,
|
||
|
});
|
||
|
|
||
|
return rollupColumn;
|
||
|
};
|
||
|
|
||
2 years ago
|
const createLookupColumn = async (
|
||
|
context,
|
||
|
{
|
||
|
project,
|
||
|
title,
|
||
|
table,
|
||
|
relatedTableName,
|
||
|
relatedTableColumnTitle,
|
||
|
}: {
|
||
2 years ago
|
project: Project;
|
||
2 years ago
|
title: string;
|
||
|
table: Model;
|
||
|
relatedTableName: string;
|
||
|
relatedTableColumnTitle: string;
|
||
|
}
|
||
|
) => {
|
||
2 years ago
|
const childBases = await project.getBases();
|
||
2 years ago
|
const childTable = await Model.getByIdOrName({
|
||
|
project_id: project.id,
|
||
2 years ago
|
base_id: childBases[0].id!,
|
||
2 years ago
|
table_name: relatedTableName,
|
||
|
});
|
||
|
const childTableColumns = await childTable.getColumns();
|
||
|
const childTableColumn = await childTableColumns.find(
|
||
|
(column) => column.title === relatedTableColumnTitle
|
||
|
);
|
||
|
|
||
|
if (!childTableColumn) {
|
||
|
throw new Error(
|
||
|
`Could not find column ${relatedTableColumnTitle} in ${relatedTableName}`
|
||
|
);
|
||
|
}
|
||
|
|
||
|
const ltarColumn = (await table.getColumns()).find(
|
||
|
(column) =>
|
||
|
column.uidt === UITypes.LinkToAnotherRecord &&
|
||
|
column.colOptions?.fk_related_model_id === childTable.id
|
||
|
);
|
||
2 years ago
|
const lookupColumn = await createColumn(context, table, {
|
||
2 years ago
|
title: title,
|
||
|
uidt: UITypes.Lookup,
|
||
|
fk_relation_column_id: ltarColumn?.id,
|
||
|
fk_lookup_column_id: childTableColumn?.id,
|
||
|
table_name: table.table_name,
|
||
|
column_name: title,
|
||
|
});
|
||
|
|
||
|
return lookupColumn;
|
||
|
};
|
||
|
|
||
2 years ago
|
const createQrCodeColumn = async (
|
||
|
context,
|
||
|
{
|
||
|
title,
|
||
|
table,
|
||
|
referencedQrValueTableColumnTitle,
|
||
|
}: {
|
||
|
title: string;
|
||
|
table: Model;
|
||
|
referencedQrValueTableColumnTitle: string;
|
||
|
}
|
||
|
) => {
|
||
|
const referencedQrValueTableColumnId = await table
|
||
|
.getColumns()
|
||
|
.then(
|
||
|
(cols) =>
|
||
|
cols.find(
|
||
|
(column) => column.title == referencedQrValueTableColumnTitle
|
||
|
)['id']
|
||
|
);
|
||
|
|
||
|
const qrCodeColumn = await createColumn(context, table, {
|
||
|
title: title,
|
||
|
uidt: UITypes.QrCode,
|
||
|
column_name: title,
|
||
|
fk_qr_value_column_id: referencedQrValueTableColumnId,
|
||
|
});
|
||
|
return qrCodeColumn;
|
||
|
};
|
||
|
|
||
2 years ago
|
const createBarcodeColumn = async (
|
||
|
context,
|
||
|
{
|
||
|
title,
|
||
|
table,
|
||
|
referencedBarcodeValueTableColumnTitle,
|
||
|
}: {
|
||
|
title: string;
|
||
|
table: Model;
|
||
|
referencedBarcodeValueTableColumnTitle: string;
|
||
|
}
|
||
|
) => {
|
||
|
const referencedBarcodeValueTableColumnId = await table
|
||
|
.getColumns()
|
||
|
.then(
|
||
|
(cols) =>
|
||
|
cols.find(
|
||
|
(column) => column.title == referencedBarcodeValueTableColumnTitle
|
||
|
)['id']
|
||
|
);
|
||
|
|
||
|
const barcodeColumn = await createColumn(context, table, {
|
||
|
title: title,
|
||
|
uidt: UITypes.Barcode,
|
||
|
column_name: title,
|
||
|
fk_barcode_value_column_id: referencedBarcodeValueTableColumnId,
|
||
|
});
|
||
|
return barcodeColumn;
|
||
|
};
|
||
|
|
||
2 years ago
|
const createLtarColumn = async (
|
||
|
context,
|
||
|
{
|
||
|
title,
|
||
|
parentTable,
|
||
|
childTable,
|
||
|
type,
|
||
|
}: {
|
||
|
title: string;
|
||
|
parentTable: Model;
|
||
|
childTable: Model;
|
||
|
type: string;
|
||
|
}
|
||
|
) => {
|
||
|
const ltarColumn = await createColumn(context, parentTable, {
|
||
|
title: title,
|
||
|
column_name: title,
|
||
|
uidt: UITypes.LinkToAnotherRecord,
|
||
|
parentId: parentTable.id,
|
||
|
childId: childTable.id,
|
||
|
type: type,
|
||
|
});
|
||
|
|
||
|
return ltarColumn;
|
||
|
};
|
||
|
|
||
2 years ago
|
const updateViewColumn = async (
|
||
|
context,
|
||
|
{ view, column, attr }: { column: Column; view: View; attr: any }
|
||
|
) => {
|
||
2 years ago
|
const res = await request(context.app)
|
||
2 years ago
|
.patch(`/api/v1/db/meta/views/${view.id}/columns/${column.id}`)
|
||
|
.set('xc-auth', context.token)
|
||
|
.send({
|
||
|
...attr,
|
||
|
});
|
||
2 years ago
|
|
||
2 years ago
|
const updatedColumn: FormViewColumn | GridViewColumn | GalleryViewColumn = (
|
||
|
await view.getColumns()
|
||
|
).find((column) => column.id === column.id)!;
|
||
2 years ago
|
|
||
|
return updatedColumn;
|
||
2 years ago
|
};
|
||
2 years ago
|
|
||
2 years ago
|
export {
|
||
|
defaultColumns,
|
||
|
createColumn,
|
||
2 years ago
|
createQrCodeColumn,
|
||
2 years ago
|
createBarcodeColumn,
|
||
2 years ago
|
createRollupColumn,
|
||
|
createLookupColumn,
|
||
|
createLtarColumn,
|
||
2 years ago
|
updateViewColumn,
|
||
2 years ago
|
};
|