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 updatePkValues = [];
const toBeUpdated = [];
const res = [];
for (const d of updateDatas) {
if (!raw) await this.validate(d);
const pkValues = await this._extractPksValues(d);
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;
}
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);
res.push(wherePk);
toBeUpdated.push({ d, wherePk });
updatePkValues.push(pkValues);
}
@ -2909,12 +2924,8 @@ class BaseModelSqlv2 {
if (!raw) {
for (const pkValues of updatePkValues) {
const oldRecord = await this.readByPk(pkValues);
if (!oldRecord && throwExceptionIfNotExist)
NcError.unprocessableEntity(
`Record with pk ${JSON.stringify(pkValues)} not found`,
);
newData.push(oldRecord);
const updatedRecord = await this.readByPk(pkValues);
newData.push(updatedRecord);
}
}
@ -2932,7 +2943,7 @@ class BaseModelSqlv2 {
}
}
return res;
return newData;
} catch (e) {
if (transaction) await transaction.rollback();
throw e;
@ -3025,16 +3036,26 @@ class BaseModelSqlv2 {
for (const d of deleteIds) {
const pkValues = await this._extractPksValues(d);
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;
}
const oldRecord = await this.readByPk(pkValues);
if (!oldRecord && throwExceptionIfNotExist)
NcError.unprocessableEntity(
`Record with pk ${JSON.stringify(pkValues)} not found`,
);
deleted.push(oldRecord);
const deletedRecord = await this.readByPk(pkValues);
if (!deletedRecord) {
// throw or skip if no record found
if (throwExceptionIfNotExist) {
NcError.unprocessableEntity(
`Record with pk ${JSON.stringify(pkValues)} not found`,
);
}
continue;
}
deleted.push(deletedRecord);
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) => {
return pkColumns.reduce((acc, col) => {
acc[col.title] = row[col.title];
acc[col.title] = row[col.title] ?? row[col.column_name];
return acc;
}, {});
});
@ -238,13 +238,21 @@ export class DataTableService {
for (const row of rows) {
let pk;
// 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 ___
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 (keys.has(pk)) {
NcError.unprocessableEntity('Duplicate row with id ' + pk);
}
if (pk === undefined || pk === null) {
NcError.unprocessableEntity('Primary key is required');
}
keys.add(pk);
}
}

Loading…
Cancel
Save