Browse Source

fix: exist logic - check row with provided primary key(s) exists or not

pull/2070/head
Wing-Kam Wong 3 years ago
parent
commit
6ca98683ed
  1. 120
      packages/nocodb-sdk/src/lib/Api.ts
  2. 34
      packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSqlv2.ts
  3. 18
      packages/nocodb/src/lib/noco/meta/api/dataApis/dataAliasApis.ts
  4. 317
      scripts/sdk/swagger.json

120
packages/nocodb-sdk/src/lib/Api.ts

@ -2258,54 +2258,6 @@ export class Api<
...params,
}),
/**
* @description check if table row with conditions exists or not
*
* @tags DB table row
* @name Exist
* @summary Table row Exist
* @request GET:/api/v1/db/data/{orgs}/{projectName}/{tableName}/exist
* @response `200` `any` OK
*/
exist: (
orgs: string,
projectName: string,
tableName: string,
query?: { fields?: any[]; sort?: any[]; where?: string },
params: RequestParams = {}
) =>
this.request<any, any>({
path: `/api/v1/db/data/${orgs}/${projectName}/${tableName}/exist`,
method: 'GET',
query: query,
format: 'json',
...params,
}),
/**
* No description
*
* @tags DB table row
* @name Count
* @summary table rows count
* @request GET:/api/v1/db/data/{orgs}/{projectName}/{tableName}/count
* @response `200` `any` OK
*/
count: (
orgs: string,
projectName: string,
tableName: string,
query?: { where?: string; nested?: any },
params: RequestParams = {}
) =>
this.request<any, any>({
path: `/api/v1/db/data/${orgs}/${projectName}/${tableName}/count`,
method: 'GET',
query: query,
format: 'json',
...params,
}),
/**
* No description
*
@ -2377,6 +2329,29 @@ export class Api<
...params,
}),
/**
* @description check row with provided primary key exists or not
*
* @tags DB table row
* @name Exist
* @summary Table row exist
* @request GET:/api/v1/db/data/{orgs}/{projectName}/{tableName}/{rowId}/exist
* @response `201` `any` Created
*/
exist: (
orgs: string,
projectName: string,
tableName: string,
rowId: string,
params: RequestParams = {}
) =>
this.request<any, any>({
path: `/api/v1/db/data/${orgs}/${projectName}/${tableName}/${rowId}/exist`,
method: 'GET',
format: 'json',
...params,
}),
/**
* No description
*
@ -2710,31 +2685,6 @@ export class Api<
...params,
}),
/**
* @description check if table row with conditions exists or not
*
* @tags DB view row
* @name Exist
* @summary Table view row Exist
* @request GET:/api/v1/db/data/{orgs}/{projectName}/{tableName}/views/{viewName}/exist
* @response `200` `any` OK
*/
exist: (
orgs: string,
projectName: string,
tableName: string,
viewName: string,
query?: { fields?: any[]; sort?: any[]; where?: string; nested?: any },
params: RequestParams = {}
) =>
this.request<any, any>({
path: `/api/v1/db/data/${orgs}/${projectName}/${tableName}/views/${viewName}/exist`,
method: 'GET',
query: query,
format: 'json',
...params,
}),
/**
* No description
*
@ -2834,6 +2784,30 @@ export class Api<
...params,
}),
/**
* @description check row with provided primary key exists or not
*
* @tags DB view row
* @name Exist
* @summary Table view row exist
* @request GET:/api/v1/db/data/{orgs}/{projectName}/{tableName}/views/{viewName}/{rowId}/exist
* @response `201` `any` Created
*/
exist: (
orgs: string,
projectName: string,
tableName: string,
viewName: string,
rowId: string,
params: RequestParams = {}
) =>
this.request<any, any>({
path: `/api/v1/db/data/${orgs}/${projectName}/${tableName}/views/${viewName}/${rowId}/exist`,
method: 'GET',
format: 'json',
...params,
}),
/**
* @description CSV or Excel export
*

34
packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSqlv2.ts

@ -89,36 +89,14 @@ class BaseModelSqlv2 {
return data;
}
public async exist(
args: {
where?: string;
filterArr?: Filter[];
} = {}
): Promise<any> {
public async exist(id?: any): Promise<any> {
const qb = this.dbDriver(this.model.table_name);
await this.selectObject({ qb });
const aliasColObjMap = await this.model.getAliasColObjMap();
const filterObj = extractFilterFromXwhere(args?.where, aliasColObjMap);
await conditionV2(
[
new Filter({
children: args.filterArr || [],
is_group: true,
logical_op: 'and'
}),
new Filter({
children: filterObj,
is_group: true,
logical_op: 'and'
}),
...(args.filterArr || [])
],
qb,
this.dbDriver
);
return !!(await qb.first());
const pks = this.model.primaryKeys;
if ((id + '').split('___').length != pks.length) {
return false;
}
return !!(await qb.where(_wherePk(pks, id)).first());
}
public async findOne(

18
packages/nocodb/src/lib/noco/meta/api/dataApis/dataAliasApis.ts

@ -161,10 +161,7 @@ async function dataRead(req: Request, res: Response) {
async function dataExist(req: Request, res: Response) {
const { model, view } = await getViewAndModelFromRequestByAliasOrId(req);
res.json(await getExist(model, view, req));
}
async function getExist(model, view: View, req) {
const base = await Base.get(model.base_id);
const baseModel = await Model.getBaseModelSQL({
@ -173,17 +170,8 @@ async function getExist(model, view: View, req) {
dbDriver: NcConnectionMgrv2.get(base)
});
const args: any = { ...req.query };
try {
args.filterArr = JSON.parse(args.filterArrJson);
} catch (e) {}
try {
args.sortArr = JSON.parse(args.sortArrJson);
} catch (e) {}
return await baseModel.exist(args);
res.json(await baseModel.exist(req.params.rowId));
}
const router = Router({ mergeParams: true });
// table data crud apis
@ -200,7 +188,7 @@ router.get(
);
router.get(
'/api/v1/db/data/:orgs/:projectName/:tableName/exist',
'/api/v1/db/data/:orgs/:projectName/:tableName/:rowId/exist',
apiMetrics,
ncMetaAclMw(dataExist, 'dataExist')
);
@ -255,7 +243,7 @@ router.get(
);
router.get(
'/api/v1/db/data/:orgs/:projectName/:tableName/views/:viewName/exist',
'/api/v1/db/data/:orgs/:projectName/:tableName/views/:viewName/:rowId/exist',
apiMetrics,
ncMetaAclMw(dataExist, 'dataExist')
);

317
scripts/sdk/swagger.json

@ -2600,136 +2600,6 @@
}
}
},
"/api/v1/db/data/{orgs}/{projectName}/{tableName}/exist": {
"parameters": [
{
"schema": {
"type": "string"
},
"name": "orgs",
"in": "path",
"required": true
},
{
"schema": {
"type": "string"
},
"name": "projectName",
"in": "path",
"required": true
},
{
"schema": {
"type": "string"
},
"name": "tableName",
"in": "path",
"required": true
}
],
"get": {
"summary": "Table row Exist",
"operationId": "db-table-row-exist",
"description": "check if table row with conditions exists or not",
"tags": [
"DB table row"
],
"parameters": [
{
"schema": {
"type": "array"
},
"in": "query",
"name": "fields"
},
{
"schema": {
"type": "array"
},
"in": "query",
"name": "sort"
},
{
"schema": {
"type": "string"
},
"in": "query",
"name": "where"
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {}
}
}
}
}
}
},
"/api/v1/db/data/{orgs}/{projectName}/{tableName}/count": {
"parameters": [
{
"schema": {
"type": "string"
},
"name": "orgs",
"in": "path",
"required": true
},
{
"schema": {
"type": "string"
},
"name": "projectName",
"in": "path",
"required": true
},
{
"schema": {
"type": "string"
},
"name": "tableName",
"in": "path",
"required": true
}
],
"get": {
"summary": "table rows count",
"operationId": "db-table-row-count",
"description": "",
"tags": [
"DB table 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": {}
}
}
}
}
}
},
"/api/v1/db/data/{orgs}/{projectName}/{tableName}/views/{viewName}": {
"parameters": [
{
@ -2920,7 +2790,7 @@
}
}
},
"/api/v1/db/data/{orgs}/{projectName}/{tableName}/views/{viewName}/exist": {
"/api/v1/db/data/{orgs}/{projectName}/{tableName}/views/{viewName}/count": {
"parameters": [
{
"schema": {
@ -2956,27 +2826,13 @@
}
],
"get": {
"summary": "Table view row Exist",
"operationId": "db-view-row-exist",
"description": "check if table row with conditions exists or not",
"summary": "Table view rows count",
"operationId": "db-view-row-count",
"description": "",
"tags": [
"DB view row"
],
"parameters": [
{
"schema": {
"type": "array"
},
"in": "query",
"name": "fields"
},
{
"schema": {
"type": "array"
},
"in": "query",
"name": "sort"
},
{
"schema": {
"type": "string"
@ -3003,7 +2859,7 @@
}
}
},
"/api/v1/db/data/{orgs}/{projectName}/{tableName}/views/{viewName}/count": {
"/api/v1/db/data/{orgs}/{projectName}/{tableName}/views/{viewName}/{rowId}": {
"parameters": [
{
"schema": {
@ -3036,30 +2892,37 @@
"name": "viewName",
"in": "path",
"required": true
},
{
"schema": {
"type": "string"
},
"name": "rowId",
"in": "path",
"required": true
}
],
"get": {
"summary": "Table view rows count",
"operationId": "db-view-row-count",
"summary": "Table view row read",
"operationId": "db-view-row-read",
"responses": {
"201": {
"description": "Created",
"content": {
"application/json": {
"schema": {}
}
}
}
},
"description": "",
"tags": [
"DB view row"
],
"parameters": [
{
"schema": {
"type": "string"
},
"in": "query",
"name": "where"
]
},
{
"schema": {},
"in": "query",
"name": "nested",
"description": "Query params for nested data"
}
],
"patch": {
"summary": "Table view row update",
"operationId": "db-view-row-update",
"responses": {
"200": {
"description": "OK",
@ -3069,10 +2932,33 @@
}
}
}
},
"tags": [
"DB view row"
],
"requestBody": {
"content": {
"application/json": {
"schema": {}
}
}
}
},
"delete": {
"summary": "Table view row delete",
"operationId": "db-view-row-delete",
"responses": {
"200": {
"description": "OK"
}
},
"tags": [
"DB view row"
],
"description": ""
}
},
"/api/v1/db/data/{orgs}/{projectName}/{tableName}/views/{viewName}/{rowId}": {
"/api/v1/db/data/{orgs}/{projectName}/{tableName}/views/{viewName}/{rowId}/exist": {
"parameters": [
{
"schema": {
@ -3116,8 +3002,8 @@
}
],
"get": {
"summary": "Table view row read",
"operationId": "db-view-row-read",
"summary": "Table view row exist",
"operationId": "db-view-row-exist",
"responses": {
"201": {
"description": "Created",
@ -3128,47 +3014,10 @@
}
}
},
"description": "",
"description": "check row with provided primary key exists or not",
"tags": [
"DB view row"
]
},
"patch": {
"summary": "Table view row update",
"operationId": "db-view-row-update",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {}
}
}
}
},
"tags": [
"DB view row"
],
"requestBody": {
"content": {
"application/json": {
"schema": {}
}
}
}
},
"delete": {
"summary": "Table view row delete",
"operationId": "db-view-row-delete",
"responses": {
"200": {
"description": "OK"
}
},
"tags": [
"DB view row"
],
"description": ""
}
},
"/api/v1/db/data/{orgs}/{projectName}/{tableName}/{rowId}": {
@ -3262,6 +3111,60 @@
"description": ""
}
},
"/api/v1/db/data/{orgs}/{projectName}/{tableName}/{rowId}/exist": {
"parameters": [
{
"schema": {
"type": "string"
},
"name": "orgs",
"in": "path",
"required": true
},
{
"schema": {
"type": "string"
},
"name": "projectName",
"in": "path",
"required": true
},
{
"schema": {
"type": "string"
},
"name": "tableName",
"in": "path",
"required": true
},
{
"schema": {
"type": "string"
},
"name": "rowId",
"in": "path",
"required": true
}
],
"get": {
"summary": "Table row exist",
"operationId": "db-table-row-exist",
"responses": {
"201": {
"description": "Created",
"content": {
"application/json": {
"schema": {}
}
}
}
},
"description": "check row with provided primary key exists or not",
"tags": [
"DB table row"
]
}
},
"/api/v1/db/data/bulk/{orgs}/{projectName}/{tableName}": {
"parameters": [
{

Loading…
Cancel
Save