Base object¶
Base object provide a way to manipulate data in a base.
Table¶
getActiveTable¶
Get the currently selected table and return a table object
Example¶
const table = base.getActiveTable();
output.markdown(`#### ${table.name}`);
getTables¶
Get all tables
Example¶
const tables = base.getTables();
output.text(tables.length);
getTableByName¶
Get a table object via its name
const table = base.getTableByName(tableName: String);
Example¶
const table = base.getTableByName('Table1');
output.text(table._id);
addTable¶
Add a table
base.addTable(tableName: String);
Example¶
base.addTable('New table');
renameTable¶
Rename a table
base.renameTable(oldName: String, newName: String);
Example¶
base.renameTable('Old name', 'New name');
deleteTable¶
Delete a table
base.deleteTable(tableName: String);
Example¶
base.deleteTable('Old table');
View¶
getActiveView¶
Get the current view, the method return a view object
Example¶
const view = base.getActiveView();
output.text(view._id);
getViews¶
Get all the views of the current table, and return all the views in an array
const views = base.getViews(table: Object/String);
Example¶
const table = base.getTableByName('Table1');
const views = base.getViews(table);
output.text(views.length);
const views = base.getViews('Table1');
output.text(views.length);
getViewByName¶
Get a view object via its name, and return a view object
const view = base.getViewByName(table: Object/String, viewName: String);
Example¶
const table = base.getTableByName('Table1');
const view = base.getViewByName(table, 'view 1');
output.text(view.name);
const view = base.getViewByName('Table1', 'view 1');
output.text(view.name);
addView¶
Add a view to a table
base.addView(table: Object/String, viewName: String);
Example¶
const table = base.getTableByName('Table1');
base.addView(table, 'view 2');
base.addView('Table1', 'view 2');
renameView¶
Rename a view in the table
base.renameView(table: Object/String, currentViewName: String, nextViewName: String);
Example¶
const table = base.getTableByName('Table1');
base.renameView(table, 'view1', 'view2');
base.renameView('Table1', 'view1', 'view2');
deleteView¶
Delete a view
base.deleteView(table: Object/String, viewName: String);
Example¶
const table = base.getTableByName('Table1');
base.deleteView(table, 'view2');
base.deleteView('Table1', 'view2');
Column¶
getColumns¶
Get all the columns in the table, and return all the column objects in an array
const columns = base.getColumns(table: Object/String);
Example¶
const table = base.getTableByName('Table1');
const columns = base.getColumns(table);
column.forEach((column) => {
output.text(column.name);
})
const columns = base.getColumns('Table1');
getShownColumns¶
Get all the displayed columns in a view, excluding the hidden columns in the view, and return an array
const columns = base.getShownColumns(table: Object/String, view: Object/String);
Example¶
const table = base.getTableByName('Table1');
const view = base.getViewByName(table, 'view 1');
const columns = base.getShownColumns(table, view);
column.forEach((column) => {
output.text(column.name);
})
const columns = base.getShownColumns('Table1', 'view 1');
getColumnByName¶
Get the column object via its name
const column = base.getColumnByName(table: Object/String, name: String);
Example¶
const column = base.getColumnByName(table, 'Column name');
output.text(column.name);
const column = base.getColumnByName('Table1', 'Column name');
getColumnsByType¶
Get all specific types of columns in the table
const columns = base.getColumnsByType(table: Object/String, type: String);
Example¶
const table = base.getTableByName('Table1');
const columns = base.getColumnsByType(table, 'text');
output.text(column.length);
const columns = base.getColumnsByType('Table1', 'text');
output.text(column.length);
Row¶
getRows¶
Get all the rows of the view and return an array
const rows = base.getRows(table: Object/String, view: Object/String);
Example¶
const table = base.getTableByName('Table1');
const view = base.getViewByName(table, 'view1');
const rows = base.getRows(table, view);
const rows = base.getRows('Table1', 'view1');
getGroupedRows¶
Get rows in the grouped view
base.getGroupedRows(table: Object/String, view: Object/String);
Example¶
const table = base.getTableByName('Table1');
const view = base.getViewByName(table, 'GroupedView');
const groupViewRows = base.getGroupedRows(table, view);
const groupViewRows = base.getGroupedRows('Table1', 'GroupedView');
getRowById¶
Get a row via its id and return a row object
const row = base.getRowById(table: Object/String, rowId: String);
Example¶
const table = base.getTableByName('Table1');
const row = base.getRowById(table, "M_lSEOYYTeuKTaHCEOL7nw");
const row = base.getRowById('Table1', "M_lSEOYYTeuKTaHCEOL7nw");
deleteRowById¶
Delete a row in a table by its ID.
base.deleteRowById(table: Object/String, rowId: String);
Example¶
const table = base.getTableByName('Table1');
base.deleteRowById(table, 'M_lSEOYYTeuKTaHCEOL7nw');
base.deleteRowById('Table1', 'M_lSEOYYTeuKTaHCEOL7nw');
addRow¶
Add a row to a table
base.addRow(table: Object/String, rowData: Object, viewName?: String)
Example¶
const table = base.getTableByName('Table1');
base.addRow(table, {'Name': 'Alex', 'Age': '18'});
base.addRow(table, {'Name': 'Alex', 'Age': '18'}, 'Default View');
base.addRow('Table1', {'Name': 'Alex', 'Age': '18'});
base.addRow('Table1', {'Name': 'Alex', 'Age': '18'}, 'Default View');
modifyRow¶
Modify a row in the table
base.modifyRow(table: Object/String, row: Object, updateRowData: Object);
Example¶
const table = base.getTableByName('Table1');
const row = base.getRowById(table, "M_lSEOYYTeuKTaHCEOL7nw");
base.modifyRow(table, row, {'Name': 'new name', 'number': 100});
const row = base.getRowById('Table1', "M_lSEOYYTeuKTaHCEOL7nw");
base.modifyRow('Table1', row, {'Name': 'new name', 'number': 100});
modifyRows¶
Modify multiple rows in the table at once
base.modifyRow(table: Object/String, rows: Array, updatedRows: Array);
Example¶
const table = base.getTableByName('Table1');
const rows = base.getRows('Table1', 'Default view');
const selectedColumnName = 'Name';
const selectedRows = [], updatedRows = [];
rows.forEach((row) => {
if (row[columnName] === 'name') {
selectedRows.push(row);
updatedRows.push({columnName: 'name1'});
}
});
base.modifyRow(table, selectedRows, updatedRows);
filter¶
Pass a conditional statement, filter out the rows that meet the conditions in the table, and return a querySet object
Example¶
// Filter out rows whose number column is equal to 5, and return a querySet object
const querySet = base.filter('Table1', 'Default', 'number = 5');
Links¶
addLink¶
Add link, link other table records
base.addLink(linkId, tableName, linkedTableName, rowId, linkedRowId)
- linkId: link_id in the data attribute of the link column
- tableName: the name of the link table
- linkedTableName: the name of the linked table
- rowId: id of link row
- linkedRowId: id of the linked row
Example¶
base.addLink('5WeC', 'real-img-files', 'contact', 'CGtoJB1oQM60RiKT-c5J-g', 'PALm2wPKTCy-jdJNv_UWaQ')
removeLink¶
Delete the link row
base.removeLink(linkId, tableName, linkedTableName, rowId, linkedRowId)
Example¶
base.removeLink('5WeC', 'real-img-files', 'contact', 'CGtoJB1oQM60RiKT-c5J-g', 'PALm2wPKTCy-jdJNv_UWaQ')
getColumnLinkId¶
Get the link id by column name
base.getColumnLinkId(tableName, columnName)
Example¶
base.getColumnLinkId('Table1', 'Record')
updateLinks¶
Remove all existing row links and add new links
base.updateLinks(linkId, tableName, linkedTableName, rowId, updatedlinkedRowIds)
Example¶
const rows = base.getRows('contact', 'Default_view');
// Update row links to [rows[0]._id, rows[1]._id, rows[2]._id, rows[3]._id]
base.updateLinks('5WeC', 'real-img-files', 'contact', 'CGtoJB1oQM60RiKT-c5J-g', [rows[0]._id, rows[1]._id, rows[2]._id, rows[3]._id])
query¶
Use sql to query a base
await base.query(sql)
BASIC¶
const data = await base.query('select name, price, year from Bill')
output.text(data)
result
[
{"name":"Bob","price":"300","year":"2021"},
{"name":"Bob","price":"300","year":"2019"},
{"price":"100","year":"2019","name":"Tom"},
{"name":"Tom","price":"100","year":"2020"},
{"name":"Tom","price":"200","year":"2021"},
{"name":"Jane","price":"200","year":"2020"},
{"name":"Jane","price":"200","year":"2021"}
]
WHERE¶
const data = await base.query('select name, price from Bill where year = 2021')
output.text(data)
Result
[
{"name":"Bob","price":"300"},
{"name":"Tom","price":"200"},
{"name":"Jane","price":"200"}
]
GROUP BY¶
const data = await base.query('select name, price from Bill where year = 2021')
output.text(data)
Result
[
{"price":"300","year":2019,"name":"Bob"},
{"price":"100","year":2019,"name":"Tom"},
{"name":"Tom","price":"100","year":2020},
{"price":"200","year":2020,"name":"Jane"},
{"name":"Bob","price":"300","year":2021},
{"name":"Tom","price":"200","year":2021},
{"name":"Jane","price":"200","year":2021}
]
DISTINCT¶
const data = await base.query('select distinct name from Bill')
output.text(data)
Result
[
{"name":"Bob"},
{"name":"Jane"},
{"name":"Tom"}
]