Remove by query¶
Deletes every row matching find and answers with the rows removed, their ids and their count. A body without find is refused : nothing is ever deleted without a filter.
| Method | POST |
| URL | /api/schema/admin/mysql8/inventory/customers/query/delete |
| Body | { find, select? } |
| Query params | none |
| Answer | data : { deletedRows, deletedRowsCount, ids } |
| 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.removeByQuery |
| API id | SCHEMA_POST_QUERY_DELETE (groups, settings, hooks, WebSocket subscriptions) |
| Without a schema | /api/gen/admin/mysql8/inventory/customers/query/delete : below |
Generated (/api/gen) or schema (/api/schema) ?
Both families offer the same operations on the same URL pattern. The schema APIs read the schema of the table : values are converted to their types, validated, encrypted, defaulted and numbered before they reach the database. The generated APIs are schemaless : they pass your data as it is, so a number sent as "22" stays a string. Use the schema APIs for the tables your application writes to, and the generated APIs for quick reads and for tables without a schema.
The URL¶
/api/schema/admin/mysql8/inventory/customers/query/delete : 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 |
The call¶
{
"success": true,
"statusCode": 200,
"data": {
"deletedRows": [ { "customer_id": 7, "first_name": "Old", "last_name": "Row", "isActive": 0 } ],
"deletedRowsCount": 1,
"ids": [ 7 ]
}
}
findtakes every operator of find, including dotted keys of related tables.{}matches everything.- The rows are read before the delete, so
deletedRowsholds them as they were ;selectkeeps only some of their fields.
More filters¶
{ "find": { "$and": [ { "isActive": 0 }, { "last_update": { "$lt": "2022-01-01T00:00:00.000Z" } } ] } }
{ "find": { "customer_id.email": "[email protected]" } }
Missing find¶
{ "success": false, "statusCode": 400, "errors": [ { "code": 400, "message": "Please provide 'find' property having query to remove data." } ] }
After the call¶
- The cache of the table is reset and WebSocket subscribers are notified once. Post hooks receive the answer in
g.res.output.
From your code¶
g.sys.db.removeByQuery makes the same call from custom APIs, hooks, events, schedulers and test cases, with the same headers and params. To get the whole response envelope instead of data, pass true as the second argument in TypeScript, or call it through g.sys.db.full in the other languages.
Without a schema : /api/gen¶
Every table also answers this operation under /api/gen, with or without a schema :
| URL | /api/gen/admin/mysql8/inventory/customers/query/delete |
| API id | GEN_POST_QUERY_DELETE (groups, settings, hooks, WebSocket subscriptions) |
| From code | g.sys.db.gen.removeByQueryGen |
The data passes as it is : nothing is converted, validated, defaulted, encrypted or numbered, and a filter matches the type you send ("22" does not match the number 22). On MongoDB, strings which look like ObjectIds are converted. The query params, headers and errors are the ones of this page.
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, or the table has no schema (use /api/gen). |
400 |
The query or the body is wrong : the error messages say which key and why. |
Related¶
- All APIs at a glance · Query params · Response format · Pre hooks and post hooks · Automatic caching
- Table schema : what the conversions and validations do to every write.