Making sure patch/save rows API alternate between each other as required.

This commit is contained in:
mike12345567 2021-06-17 12:21:13 +01:00
parent 09895b901a
commit 8c21cd5a19
3 changed files with 10 additions and 8 deletions

View File

@ -136,9 +136,6 @@ exports.patch = async ctx => {
exports.save = async ctx => { exports.save = async ctx => {
const appId = ctx.appId const appId = ctx.appId
const inputs = ctx.request.body const inputs = ctx.request.body
if (inputs._id) {
return exports.patch(ctx)
}
const tableId = ctx.params.tableId const tableId = ctx.params.tableId
return handleRequest(appId, DataSourceOperation.CREATE, tableId, { return handleRequest(appId, DataSourceOperation.CREATE, tableId, {
row: inputs, row: inputs,

View File

@ -24,6 +24,11 @@ function getTableId(ctx) {
exports.patch = async ctx => { exports.patch = async ctx => {
const appId = ctx.appId const appId = ctx.appId
const tableId = getTableId(ctx) const tableId = getTableId(ctx)
const body = ctx.request.body
// if it doesn't have an _id then its save
if (body && !body._id) {
return exports.save(ctx)
}
try { try {
const { row, table } = await pickApi(tableId).patch(ctx) const { row, table } = await pickApi(tableId).patch(ctx)
ctx.eventEmitter && ctx.eventEmitter &&
@ -38,6 +43,11 @@ exports.patch = async ctx => {
exports.save = async function (ctx) { exports.save = async function (ctx) {
const appId = ctx.appId const appId = ctx.appId
const tableId = getTableId(ctx) const tableId = getTableId(ctx)
const body = ctx.request.body
// if it has an ID already then its a patch
if (body && body._id) {
return exports.patch(ctx)
}
try { try {
const { row, table } = await pickApi(tableId).save(ctx) const { row, table } = await pickApi(tableId).save(ctx)
ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:save`, appId, row, table) ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:save`, appId, row, table)

View File

@ -95,11 +95,6 @@ exports.save = async function (ctx) {
let inputs = ctx.request.body let inputs = ctx.request.body
inputs.tableId = ctx.params.tableId inputs.tableId = ctx.params.tableId
// if the row obj had an _id then it will have been retrieved
if (inputs._id && inputs._rev) {
return exports.patch(ctx)
}
if (!inputs._rev && !inputs._id) { if (!inputs._rev && !inputs._id) {
inputs._id = generateRowID(inputs.tableId) inputs._id = generateRowID(inputs.tableId)
} }