Master save (generated API)¶
One call which saves or updates a whole object graph : each object is inserted when it has no primary key and updated when it has one, at every level of nesting, across tables and even databases.
| Method | POST |
| URL | /api/gen/admin/mysql8/inventory/customers/master-save |
| Body | one object, or an array of objects, with nested related objects |
| Query params | select, deep : applied to the answer |
| Answer | 201 and data : the saved object(s), with the ids of the nested rows |
| Databases | MongoDB, MySQL, MariaDB, PostgreSQL, SQL Server, Oracle, TiDB, Percona |
| Cached | no (a write ; it resets the cache of the table) |
| From code | g.sys.db.gen.masterSaveGen |
| API id | GEN_MASTER_SAVE (groups, settings, hooks, WebSocket subscriptions) |
| The other family | Master save as a schema API |
Schemaless : the data passes as it is
The generated APIs never read the schema of the table. Nothing is converted, validated, defaulted, encrypted or numbered : what you send is what the database gets, and a filter matches the type you send ("22" does not match the number 22). On MongoDB, strings which look like ObjectIds are converted. Every table has these APIs, with or without a schema ; the same operations under /api/schema apply the schema.
The URL¶
/api/gen/admin/mysql8/inventory/customers/master-save : replace admin with the user path of your account, mysql8 with your instance, inventory with the database and customers with the table. The examples use this table :
| customer_id | first_name | last_name | last_update | pincode | isActive |
|---|---|---|---|---|---|
| 1 | Bob | lin | 2022-11-14 04: 34: 58 | 382345 | 1 |
| 2 | Alice | Page | 2022-10-15 02: 10: 40 | 382346 | 1 |
| 3 | Mallory | Brown | 2022-09-13 03: 44: 05 | 382347 | 1 |
| 4 | Eve | Mathly | 2022-11-12 01: 59: 33 | 382348 | 1 |
| 5 | Eve | Page | 2022-11-12 01: 59: 33 | 382349 | 1 |
Save or update¶
{ "customer_id": 27, "pincode": 382330 }
- The rule is the same at every level : primary key present, the row is read and updated ; absent, the row is inserted. An update sends only the fields you give.
- An array in the body saves or updates each object and answers an array.
When something fails¶
- Every row written by the call is reverted when a later one fails : a wrong field in the country of the example above leaves no city and no state behind.
- The errors name the object and the field, with
dataIndexfor arrays.
Without a schema¶
- Without relations, a nested object is stored as an embedded document on MongoDB and refused on SQL. The tree of tables needs the schema API.
- Save or update by primary key works :
_idon MongoDB, the primary key column on SQL.
From your code¶
g.sys.db.gen.masterSaveGen makes the same call from custom APIs, hooks, events, schedulers and test cases, with the same headers and params. Pass true as the second argument to get the whole response envelope instead of data.
Headers¶
Every call takes the request headers : the tokens, x-am-response-case, x-am-content-type-response (JSON, XML, YAML…), x-am-response-object-type: make_flat, x-am-cache-control, x-am-get-encrypted-data, x-am-internationalization, x-am-tenant-username, x-am-meta.
Errors¶
| Code | When |
|---|---|
401 |
The token in x-am-authorization is missing or invalid. |
403 |
No group of the API user grants this API of this table, or a field of the request. |
404 |
The instance, database or table of the URL does not exist. |
400 |
The query or the body is wrong : the error messages say which key and why. |