Get table meta¶
Describes a table : the name and the type of every column (or of every field found in a MongoDB collection), in the same format for every database. Migration scripts use it to check whether a column exists before adding it.
| Method | POST |
| URL | /api/system-api/admin/get-table-meta : admin is the user path of your account |
| Body | { instance, database, collection } or an array of them |
| Answer | data : an array of fields, or an array of arrays |
| From code | g.sys.system.getTableMeta |
The call¶
Answer
{
"success": true,
"statusCode": 200,
"data": [
{ "name": "customer_id", "path": "customer_id", "count": 24, "total_count": 24, "type": "Number", "has_duplicates": false, "probability": 1 },
{ "name": "first_name", "path": "first_name", "count": 24, "total_count": 24, "type": [ "String", "Null" ], "has_duplicates": true, "probability": 1 }
]
}
| Key | Meaning |
|---|---|
name, path |
The column, with its path for nested MongoDB fields. |
type |
The type, or the list of types seen (MongoDB). |
count, total_count, probability |
For MongoDB, in how many of the sampled documents the field appears. |
has_duplicates |
Whether the same value appears twice in the sample. |
tableworks in place ofcollection. An array of tables answers an array of arrays, in the same order.- A table of a tenant :
"instance": "crm::acme", or the headerx-am-tenant-username.
From code¶
A migration which adds a column once
const meta = await g.sys.system.getTableMeta({ instance: 'mysql8', database: 'inventory', table: 'order_transactions' });
if (!meta.find(c => c.name === 'description')) {
await g.sys.system.executeQuery({ instance: 'mysql8', query: 'ALTER TABLE `inventory`.`order_transactions` ADD COLUMN `description` varchar(255) NULL' });
}
A migration which adds a column once
meta = await g.sys.system.get_table_meta(instance='mysql8', database='inventory', table='order_transactions')
if not any(column.name == 'description' for column in meta):
await g.sys.system.execute_query('mysql8', 'ALTER TABLE `inventory`.`order_transactions` ADD COLUMN `description` varchar(255) NULL')
A migration which adds a column once
$meta = $g->sys->system->getTableMeta(instance: 'mysql8', database: 'inventory', table: 'order_transactions');
if (!array_filter($meta, fn ($column) => $column->name === 'description')) {
$g->sys->system->executeQuery('mysql8', 'ALTER TABLE `inventory`.`order_transactions` ADD COLUMN `description` varchar(255) NULL');
}
A migration which adds a column once
var meta = await g.Sys.System.GetTableMeta(new GetTableMeta { Instance = "mysql8", Database = "inventory", Table = "order_transactions" });
if (!meta.Any(column => column.Name == "description"))
{
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql8", "ALTER TABLE `inventory`.`order_transactions` ADD COLUMN `description` varchar(255) NULL"));
}
A migration which adds a column once
meta, err := g.Sys.System.GetTableMeta(am.GetTableMeta{Instance: "mysql8", Database: "inventory", Table: "order_transactions"})
if err != nil {
return nil, err
}
found := false
for _, column := range meta {
found = found || column.Name == "description"
}
if !found {
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{Instance: "mysql8", Query: "ALTER TABLE `inventory`.`order_transactions` ADD COLUMN `description` varchar(255) NULL"}); err != nil {
return nil, err
}
}
A migration which adds a column once
let meta = g.sys.system.get_table_meta(GetTableMeta::default().instance("mysql8").database("inventory").table("order_transactions"))?;
if !meta.iter().any(|column| column.name == "description") {
g.sys.system.execute_query(ExecuteQuery::new("mysql8", "ALTER TABLE `inventory`.`order_transactions` ADD COLUMN `description` varchar(255) NULL"))?;
}
A migration which adds a column once
List<TableMetaField> meta = g.sys.system.getTableMeta(new GetTableMeta().instance("mysql8").database("inventory").table("order_transactions"));
if (meta.stream().noneMatch(column -> "description".equals(column.getName()))) {
g.sys.system.executeQuery(new ExecuteQuery("mysql8", "ALTER TABLE `inventory`.`order_transactions` ADD COLUMN `description` varchar(255) NULL"));
}
A migration which adds a column once
val meta = g.sys.system.getTableMeta(GetTableMeta().instance("mysql8").database("inventory").table("order_transactions"))
if (meta.none { it.name == "description" }) {
g.sys.system.executeQuery(ExecuteQuery("mysql8", "ALTER TABLE `inventory`.`order_transactions` ADD COLUMN `description` varchar(255) NULL"))
}
A table of a tenant¶
instance: "crm::acme"reads the table in the database of the tenantacmeof the multi-tenant instancecrm. In a custom API called for a tenant,instance: "crm"reads it in the database of that tenant.
Access, headers and errors¶
- Over HTTP, a system API answers only once its settings open it :
TOKEN_ACCESS(the token of an API user whose group grants this system API) orIS_PUBLIC. Without settings it isNO_ACCESS: only your code (g.sys) and the admin panel reach it. - The settings can also keep the answer in the cache, or ask for the token of a person (
authProviders). Pre and post hooks run around it, as around any API. - It takes the request headers and answers the envelope.
401: no valid token, or the API isNO_ACCESS.403: no group grants it.400: the body is wrong, and the message names the key.
Related : All APIs · System API settings · System APIs from code