Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Implement delete all rows in table editor

Joshen Limb9f8910936da
diff --git a/studio/components/grid/components/header/Header.tsx b/studio/components/grid/components/header/Header.tsx
index 4fb9411f417..5c0a29725ca 100644
--- a/studio/components/grid/components/header/Header.tsx
+++ b/studio/components/grid/components/header/Header.tsx
@@ -101,9 +101,18 @@ const RowHeader: FC<RowHeaderProps> = ({ sorts, filters }) => {
confirmAlert({
title: 'Confirm to delete',
message: 'Are you sure you want to delete the selected rows? This action cannot be undone.',
- onConfirm: async () => {
+ onAsyncConfirm: async () => {
if (allRowsSelected) {
- console.log('Need to query to delete everything')
+ const { error } = await state.rowService!.deleteAll(filters)
+ if (error) {
+ if (state.onError) state.onError(error)
+ } else {
+ dispatch({ type: 'REMOVE_ALL_ROWS' })
+ dispatch({
+ type: 'SELECTED_ROWS_CHANGE',
+ payload: { selectedRows: new Set() },
+ })
+ }
} else {
const rowIdxs = Array.from(selectedRows) as number[]
const rows = allRows.filter((x) => rowIdxs.includes(x.idx))
@@ -128,8 +137,6 @@ const RowHeader: FC<RowHeaderProps> = ({ sorts, filters }) => {
? await state.rowService!.fetchAllData(filters, sorts)
: allRows.filter((x) => selectedRows.has(x.idx))
- console.log('Rows', rows)
-
const csv = exportRowsToCsv(state.table!.columns, rows)
const csvData = new Blob([csv], { type: 'text/csv;charset=utf-8;' })
saveAs(csvData, `${state.table!.name}_rows.csv`)
diff --git a/studio/components/grid/components/menu/RowMenu.tsx b/studio/components/grid/components/menu/RowMenu.tsx
index 3840474f410..f6f78f85e34 100644
--- a/studio/components/grid/components/menu/RowMenu.tsx
+++ b/studio/components/grid/components/menu/RowMenu.tsx
@@ -7,6 +7,8 @@ import { confirmAlert } from 'components/to-be-cleaned/ModalsDeprecated/ConfirmM
type RowMenuProps = {}
+// [JOSHEN] This file will be deprecated and to be removed
+
const RowMenu: React.FC<RowMenuProps> = ({}) => {
const state = useTrackedState()
const dispatch = useDispatch()
diff --git a/studio/components/grid/services/row/SqlRowService.ts b/studio/components/grid/services/row/SqlRowService.ts
index a47a01db3e0..1ab0b9f4846 100644
--- a/studio/components/grid/services/row/SqlRowService.ts
+++ b/studio/components/grid/services/row/SqlRowService.ts
@@ -63,6 +63,20 @@ export class SqlRowService implements IRowService {
return {}
}
+ async deleteAll(filters: Filter[]) {
+ let queryChains = this.query.from(this.table.name, this.table.schema ?? undefined).delete()
+
+ filters
+ .filter((x) => x.value && x.value != '')
+ .forEach((x) => {
+ const value = this.formatFilterValue(x)
+ queryChains = queryChains.filter(x.column, x.operator, value)
+ })
+
+ const query = queryChains.toSql()
+ return await this.onSqlQuery(query)
+ }
+
async fetchPage(page: number, rowsPerPage: number, filters: Filter[], sorts: Sort[]) {
const pageFromZero = page > 0 ? page - 1 : page
const from = pageFromZero * rowsPerPage
diff --git a/studio/components/grid/services/row/index.tsx b/studio/components/grid/services/row/index.tsx
index d037050761a..c438ea7bdc5 100644
--- a/studio/components/grid/services/row/index.tsx
+++ b/studio/components/grid/services/row/index.tsx
@@ -15,7 +15,7 @@ export interface IRowService {
error?: ServiceError
}>
- fetchAllData: (filters: Filter[], sorts: Sort[]) => any
+ fetchAllData: (filters: Filter[], sorts: Sort[]) => Promise<any[]>
/**
* TODO: should return a promise.
@@ -26,6 +26,7 @@ export interface IRowService {
* and rows are already removed from the grid
*/
delete: (rows: SupaRow[]) => { error?: ServiceError }
+ deleteAll: (filters: Filter[]) => Promise<{ error?: ServiceError }>
update: (
row: SupaRow,
diff --git a/studio/components/grid/store/reducers/row.ts b/studio/components/grid/store/reducers/row.ts
index 731ef665faf..d8adbb90f5d 100644
--- a/studio/components/grid/store/reducers/row.ts
+++ b/studio/components/grid/store/reducers/row.ts
@@ -47,6 +47,7 @@ type ROW_ACTIONTYPE =
| { type: 'ADD_NEW_ROW'; payload: Dictionary<any> }
| { type: 'EDIT_ROW'; payload: { row: Dictionary<any>; idx: number } }
| { type: 'REMOVE_ROWS'; payload: { rowIdxs: number[] } }
+ | { type: 'REMOVE_ALL_ROWS' }
| { type: 'SET_IS_LOADING'; payload: { isLoading: boolean } }
| { type: 'SELECT_ALL_ROWS'; payload: { selectedRows: ReadonlySet<number> } }
@@ -143,6 +144,13 @@ const RowReducer = (state: RowInitialState, action: ROW_ACTIONTYPE) => {
totalRows: totalRows,
}
}
+ case 'REMOVE_ALL_ROWS': {
+ return {
+ ...state,
+ rows: [],
+ totalRows: 0,
+ }
+ }
case 'SET_IS_LOADING': {
return {
...state,