Skip to content
This page View Markdown Open in ChatGPT Open in Claude

Get by id

Reads one row by its primary key, or by another column. With select and deep, it fills the detail screen of your app in one call.

In short

  • GET /api/schema/<user-path>/<instance>/<database>/<table>/get-by-id/<id>
  • Add a column after the id to look in that column : /get-by-id/Bob/first_name.
  • It takes select and deep. No other query param.
  • An unknown id answers data: null, with success: true.
Method GET
URL /api/schema/admin/mysql8/inventory/customers/get-by-id/:id[/:primaryKey]
Body none
Query params select, deep
Answer data : the row, or null
Databases MongoDB, MySQL, MariaDB, PostgreSQL, SQL Server, Oracle, TiDB, Percona
Cached yes, when enableCaching is on for the table or the database
From code g.sys.db.getById
API id SCHEMA_GET_BY_ID
Without a schema /api/gen/admin/mysql8/inventory/customers/get-by-id/:id[/:primaryKey] : below

The examples read the table of get all.

By primary key

GET /api/schema/admin/mysql8/inventory/customers/get-by-id/1
{
    "success": true,
    "statusCode": 200,
    "data": { "customer_id": 1, "first_name": "Bob", "last_name": "Lin", "last_update": "2022-11-14T04:34:58.000Z", "pincode": 382345, "isActive": 1 }
}

The primary key is _id on MongoDB, and the primary key column on SQL (isPrimaryKey in the schema).

By another column

Write the column after the id :

GET /api/schema/admin/mysql8/inventory/customers/get-by-id/Bob/first_name
GET /api/schema/admin/mysql8/inventory/customers/get-by-id/382345/pincode
  • When several rows match, the first one comes back.
  • The id takes the type of the column : 382345 is compared as a number. A value that does not fit is refused : Unable to convert 'abc' to number based on your schema type.
GET /api/schema/admin/mysql8/inventory/customers/get-by-id/1?select=first_name,last_name
GET /api/schema/admin/mysql8/inventory/customers/get-by-id/1?deep=[{s_key:'customer_id',t_col:'orders',t_key:'customer_id',isMultiple:true}]

deep works as on get all.

Only the rows of the caller

Get by id reads no find from the URL. A pre hook can add one to g.req.query.find : the row must then match both the id and that find. That is how you keep each person to their own rows : see Handle role based permissions.

From your code

g.sys.db.getById makes the same call from custom APIs, hooks, events, schedulers and test cases.

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/get-by-id/:id[/:primaryKey]
API id GEN_GET_BY_ID
From code g.sys.db.gen.getByIdGen

Without a schema, the id takes the type it looks like : on MongoDB, an id that looks like an ObjectId is one, a number is a number, anything else is a text.

Headers and errors

This operation takes the request headers of every API. It answers the envelope, with the status codes and the messages of every API.

Related : All APIs · Table schema · Pre hooks · Post hooks · API group permission