gristlabs_grist-core/app/server/lib/DocApiTypes.ts
Alex Hall 0de0cb0f4a (core) Add PUT /records DocApi endpoint to AddOrUpdate records
Summary:
As designed in https://grist.quip.com/fZSrAnJKgO5j/Add-or-Update-Records-API

Current `POST /records` adds records, and `PATCH /records` updates them by row ID. This adds `PUT /records` to 'upsert' records, applying the AddOrUpdate user action. PUT was chosen because it's idempotent. Using a separate method (instead of inferring based on the request body) also cleanly separates validation, documentation, etc.

The name `require` for the new property was suggested by Paul because `where` isn't very clear when adding records.

Test Plan: New DocApi tests

Reviewers: jarek

Reviewed By: jarek

Differential Revision: https://phab.getgrist.com/D3251
2022-02-12 09:44:34 +02:00

46 lines
1.2 KiB
TypeScript

import { CellValue } from "app/plugin/GristData";
/**
* JSON schema for api /record endpoint. Used in POST method for adding new records.
*/
export interface NewRecord {
fields?: { [coldId: string]: CellValue }; // fields is optional, user can create blank records
}
/**
* JSON schema for api /record endpoint. Used in PATCH method for updating existing records.
*/
export interface Record {
id: number;
fields: { [coldId: string]: CellValue };
}
/**
* JSON schema for api /record endpoint. Used in PUT method for adding or updating records.
*/
export interface AddOrUpdateRecord {
require: { [coldId: string]: CellValue } & { id?: number };
fields?: { [coldId: string]: CellValue };
}
/**
* JSON schema for the body of api /record PATCH endpoint
*/
export interface RecordsPatch {
records: [Record, ...Record[]]; // at least one record is required
}
/**
* JSON schema for the body of api /record POST endpoint
*/
export interface RecordsPost {
records: [NewRecord, ...NewRecord[]]; // at least one record is required
}
/**
* JSON schema for the body of api /record PUT endpoint
*/
export interface RecordsPut {
records: [AddOrUpdateRecord, ...AddOrUpdateRecord[]]; // at least one record is required
}