Browse Source

Merge pull request #6537 from nocodb/nc-fix/data-apis

fix: new data apis
pull/6557/head
mertmit 1 year ago committed by GitHub
parent
commit
23d9331bdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 57
      packages/nocodb/src/db/BaseModelSqlv2.ts
  2. 14
      packages/nocodb/src/services/data-table.service.ts

57
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -2884,17 +2884,32 @@ class BaseModelSqlv2 {
const newData = []; const newData = [];
const updatePkValues = []; const updatePkValues = [];
const toBeUpdated = []; const toBeUpdated = [];
const res = [];
for (const d of updateDatas) { for (const d of updateDatas) {
if (!raw) await this.validate(d); if (!raw) await this.validate(d);
const pkValues = await this._extractPksValues(d); const pkValues = await this._extractPksValues(d);
if (!pkValues) { if (!pkValues) {
// pk not specified - bypass // throw or skip if no pk provided
if (throwExceptionIfNotExist) {
NcError.unprocessableEntity(
`Record with pk ${JSON.stringify(pkValues)} not found`,
);
}
continue; continue;
} }
if (!raw) prevData.push(await this.readByPk(pkValues)); if (!raw) {
const oldRecord = await this.readByPk(pkValues);
if (!oldRecord) {
// throw or skip if no record found
if (throwExceptionIfNotExist) {
NcError.unprocessableEntity(
`Record with pk ${JSON.stringify(pkValues)} not found`,
);
}
continue;
}
prevData.push(oldRecord);
}
const wherePk = await this._wherePk(pkValues); const wherePk = await this._wherePk(pkValues);
res.push(wherePk);
toBeUpdated.push({ d, wherePk }); toBeUpdated.push({ d, wherePk });
updatePkValues.push(pkValues); updatePkValues.push(pkValues);
} }
@ -2909,12 +2924,8 @@ class BaseModelSqlv2 {
if (!raw) { if (!raw) {
for (const pkValues of updatePkValues) { for (const pkValues of updatePkValues) {
const oldRecord = await this.readByPk(pkValues); const updatedRecord = await this.readByPk(pkValues);
if (!oldRecord && throwExceptionIfNotExist) newData.push(updatedRecord);
NcError.unprocessableEntity(
`Record with pk ${JSON.stringify(pkValues)} not found`,
);
newData.push(oldRecord);
} }
} }
@ -2932,7 +2943,7 @@ class BaseModelSqlv2 {
} }
} }
return res; return newData;
} catch (e) { } catch (e) {
if (transaction) await transaction.rollback(); if (transaction) await transaction.rollback();
throw e; throw e;
@ -3025,16 +3036,26 @@ class BaseModelSqlv2 {
for (const d of deleteIds) { for (const d of deleteIds) {
const pkValues = await this._extractPksValues(d); const pkValues = await this._extractPksValues(d);
if (!pkValues) { if (!pkValues) {
// pk not specified - bypass // throw or skip if no pk provided
if (throwExceptionIfNotExist) {
NcError.unprocessableEntity(
`Record with pk ${JSON.stringify(pkValues)} not found`,
);
}
continue; continue;
} }
const oldRecord = await this.readByPk(pkValues); const deletedRecord = await this.readByPk(pkValues);
if (!oldRecord && throwExceptionIfNotExist) if (!deletedRecord) {
NcError.unprocessableEntity( // throw or skip if no record found
`Record with pk ${JSON.stringify(pkValues)} not found`, if (throwExceptionIfNotExist) {
); NcError.unprocessableEntity(
deleted.push(oldRecord); `Record with pk ${JSON.stringify(pkValues)} not found`,
);
}
continue;
}
deleted.push(deletedRecord);
res.push(d); res.push(d);
} }

14
packages/nocodb/src/services/data-table.service.ts

@ -212,7 +212,7 @@ export class DataTableService {
const result = (Array.isArray(body) ? body : [body]).map((row) => { const result = (Array.isArray(body) ? body : [body]).map((row) => {
return pkColumns.reduce((acc, col) => { return pkColumns.reduce((acc, col) => {
acc[col.title] = row[col.title]; acc[col.title] = row[col.title] ?? row[col.column_name];
return acc; return acc;
}, {}); }, {});
}); });
@ -238,13 +238,21 @@ export class DataTableService {
for (const row of rows) { for (const row of rows) {
let pk; let pk;
// if only one primary key then extract the value // if only one primary key then extract the value
if (model.primaryKeys.length === 1) pk = row[model.primaryKey.title]; if (model.primaryKeys.length === 1)
pk = row[model.primaryKey.title] ?? row[model.primaryKey.column_name];
// if composite primary key then join the values with ___ // if composite primary key then join the values with ___
else pk = model.primaryKeys.map((pk) => row[pk.title]).join('___'); else
pk = model.primaryKeys
.map((pk) => row[pk.title] ?? row[pk.column_name])
.join('___');
// if duplicate then throw error // if duplicate then throw error
if (keys.has(pk)) { if (keys.has(pk)) {
NcError.unprocessableEntity('Duplicate row with id ' + pk); NcError.unprocessableEntity('Duplicate row with id ' + pk);
} }
if (pk === undefined || pk === null) {
NcError.unprocessableEntity('Primary key is required');
}
keys.add(pk); keys.add(pk);
} }
} }

Loading…
Cancel
Save