From ff9db2dd720ae122ec0f95bd1a33026f0fc03417 Mon Sep 17 00:00:00 2001 From: Mert Ersoy Date: Fri, 8 Apr 2022 01:20:05 +0300 Subject: [PATCH] fix: update record counter with insertion/deletion Signed-off-by: Mert Ersoy --- .../project/spreadsheet/rowsXcDataTable.vue | 8 +++ packages/nocodb-sdk/src/lib/Api.ts | 24 +++++++ .../noco/meta/api/dataApis/dataAliasApis.ts | 25 +++++++ scripts/sdk/swagger.json | 69 +++++++++++++++++++ 4 files changed, 126 insertions(+) diff --git a/packages/nc-gui/components/project/spreadsheet/rowsXcDataTable.vue b/packages/nc-gui/components/project/spreadsheet/rowsXcDataTable.vue index 01994ec987..5e59ad100c 100644 --- a/packages/nc-gui/components/project/spreadsheet/rowsXcDataTable.vue +++ b/packages/nc-gui/components/project/spreadsheet/rowsXcDataTable.vue @@ -1157,6 +1157,7 @@ export default { await this.$api.data.delete(this.meta.id, id) } this.data.splice(this.rowContextMenu.index, 1) + this.syncCount() // this.$toast.success('Deleted row successfully').goAway(3000) } catch (e) { this.$toast.error(`Failed to delete row : ${e.message}`).goAway(3000) @@ -1187,6 +1188,7 @@ export default { return this.$toast.error(`Failed to delete row : ${e.message}`).goAway(3000) } } + this.syncCount() }, async clearCellValue() { @@ -1246,6 +1248,8 @@ export default { this.expandRow(data.length - 1, rowMeta) } } + await this.save() + this.syncCount() }, async handleKeyDown({ @@ -1536,6 +1540,10 @@ export default { console.log(e) this.$toast.error(e.message).goAway(3000) } + }, + async syncCount() { + const { count } = await this.$api.dbViewRow.count('noco', this.$store.getters['project/GtrProjectName'], this.meta.title, this.selectedView.title) + this.count = count } }, computed: { diff --git a/packages/nocodb-sdk/src/lib/Api.ts b/packages/nocodb-sdk/src/lib/Api.ts index 9ddcf95453..a54c149304 100644 --- a/packages/nocodb-sdk/src/lib/Api.ts +++ b/packages/nocodb-sdk/src/lib/Api.ts @@ -3122,6 +3122,30 @@ export class Api< ...params, }), + /** + * No description + * + * @tags DB View Row + * @name Count + * @request GET:/data/{orgs}/{projectName}/{tableAlias}/views/{viewName}/count + * @response `200` `any` OK + */ + count: ( + orgs: string, + projectName: string, + tableAlias: string, + viewName: string, + query?: { where?: string; nested?: any }, + params: RequestParams = {} + ) => + this.request({ + path: `/data/${orgs}/${projectName}/${tableAlias}/views/${viewName}/count`, + method: 'GET', + query: query, + format: 'json', + ...params, + }), + /** * No description * diff --git a/packages/nocodb/src/lib/noco/meta/api/dataApis/dataAliasApis.ts b/packages/nocodb/src/lib/noco/meta/api/dataApis/dataAliasApis.ts index d301c05a7d..919e8c963a 100644 --- a/packages/nocodb/src/lib/noco/meta/api/dataApis/dataAliasApis.ts +++ b/packages/nocodb/src/lib/noco/meta/api/dataApis/dataAliasApis.ts @@ -14,6 +14,27 @@ export async function dataList(req: Request, res: Response) { res.json(await getDataList(model, view, req)); } +async function dataCount(req: Request, res: Response) { + const { model, view } = await getViewAndModelFromRequest(req); + + const base = await Base.get(model.base_id); + + const baseModel = await Model.getBaseModelSQL({ + id: model.id, + viewId: view?.id, + dbDriver: NcConnectionMgrv2.get(base) + }); + + const countArgs: any = { ...req.query }; + try { + countArgs.filterArr = JSON.parse(countArgs.filterArrJson); + } catch (e) {} + + const count = await baseModel.count(countArgs); + + res.json({ count }); +} + async function dataInsert(req: Request, res: Response) { const { model, view } = await getViewAndModelFromRequest(req); @@ -130,6 +151,10 @@ router.get( '/data/:orgs/:projectName/:tableName/views/:viewName', ncMetaAclMw(dataList) ); +router.get( + '/data/:orgs/:projectName/:tableName/views/:viewName/count', + ncMetaAclMw(dataCount) +); router.post( '/data/:orgs/:projectName/:tableName/views/:viewName', diff --git a/scripts/sdk/swagger.json b/scripts/sdk/swagger.json index 7df510d1c4..eac855e9a4 100644 --- a/scripts/sdk/swagger.json +++ b/scripts/sdk/swagger.json @@ -3017,6 +3017,75 @@ } } }, + "/data/{orgs}/{projectName}/{tableAlias}/views/{viewName}/count": { + "parameters": [ + { + "schema": { + "type": "string" + }, + "name": "orgs", + "in": "path", + "required": true + }, + { + "schema": { + "type": "string" + }, + "name": "projectName", + "in": "path", + "required": true + }, + { + "schema": { + "type": "string" + }, + "name": "tableAlias", + "in": "path", + "required": true + }, + { + "schema": { + "type": "string" + }, + "name": "viewName", + "in": "path", + "required": true + } + ], + "get": { + "summary": "", + "operationId": "db-view-row-count", + "description": "", + "tags": [ + "DB View Row" + ], + "parameters": [ + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "where" + }, + { + "schema": {}, + "in": "query", + "name": "nested", + "description": "Query params for nested data" + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": {} + } + } + } + } + } + }, "/data/{tableId}/export/{type}": { "parameters": [ {