const[apiUser,databaseUser]=awaitg.sys.system.getToken([{"u":"USER_NAME","p":"USER_PASSWORD"},{"name":"TOKEN_GENERATOR_NAME","u":"USER_NAME","p":"PASSWORD"}]);// apiUser.token goes in x-am-authorization, databaseUser.token in x-am-user-authorization
awaitg.sys.system.executeQuery({instance:'INSTANCE_NAME',database:'DATABASE_NAME',collection:'COLLECTION_NAME',query:"select * from DATABASE_NAME.COLLECTION_NAME"});
If the database is Mongodb API Maker supports all the commands listed in below.
import*asTfrom'types';letSecret:T.ISecretType|any={// Place your keys here in json format.common:<T.ISecretTypeCommon>{dbConstrain:{instance:'mysql',database:'inventory',collection:''}}};module.exports=Secret;
awaitg.sys.system.executeQuery({instance:"mysql",query:"SELECT * FROM inventory.employees LEFT JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;"})
instance: "crm::acme" runs the query in the database of the tenant acme of the multi-tenant instance crm.
In a custom API called for a tenant, instance: "crm" runs it in the database of that tenant: the x-am-tenant-username header is passed on. Without a tenant, it runs in the structure database.
// The database of acme.constordersOfAcme=awaitg.sys.system.executeQuery({instance:'crm::acme',database:'crm',query:"select status, count(*) as orders from orders group by status"});// The database of the tenant of the request.constcustomers=awaitg.sys.system.executeQuery({instance:'crm',database:'crm',query:"select count(*) as customers from customers"});
instance: "crm::acme" reads the table in the database of the tenant acme of the multi-tenant instance crm. In a custom API called for a tenant, instance: "crm" reads it in the database of that tenant.
Create indexes using global object 'g'. The same request works on MongoDB, MySQL, MariaDB, SQL Server, PostgreSQL and Oracle ; an index which exists already is left alone (EXISTS), a field the database can not index is SKIPPED with the reason, a refusal of the database is FAILED with its message.
constresults=awaitg.sys.system.createIndexes({indexes:[{instance:'mysql8',database:'inventory',table:'orders',fields:[{name:'customer_id'},{name:'status',order:'DESC'}],},{instance:'mongodb',database:'inventory',collection:'products',name:'ux_products_sku',unique:true,fields:[{name:'sku'}],},],ifNotExists:true,// default : nothing is created twicecontinueOnError:true,// default : one refused index does not stop the othersskipUnindexable:true,// default : TEXT, json, CLOB... are skipped before the database is asked});// results[0] : { status: 'CREATED', name: 'am_ix_orders_customer_id_status_3f9a1c', fields: ['customer_id', 'status'], ... }// results[1] : { status: 'EXISTS', message: 'The index "ux_products_sku" exists already.', ... }
Without a name, the index is called am_ix_<table>_<fields>_<hash> (am_ixu_ when unique), the same in every environment, cut to the limit of the database (30 characters on Oracle).
order : ASC, DESC, HASHED and TEXT (MongoDB) ; nullsFirst: true on PostgreSQL. type : FULLTEXT or SPATIAL (MySQL), CLUSTERED, XML, SPATIAL (SQL Server), hash, gin, gist, spgist, brin (PostgreSQL). method : BTREE, HASH, RTREE (MySQL).
This is the call the migration script am_index_maker of Index Maker makes on deploy : a migration script of your own can do the same.
instance: "crm::acme" creates the index in the database of the tenant acme of the multi-tenant instance crm. In a custom API called for a tenant, instance: "crm" creates it in the database of that tenant.
constresults=awaitg.sys.system.dropIndexes({instance:'mysql8',database:'inventory',table:'orders',indexes:['am_ix_orders_customer_id_status_3f9a1c','old_idx_status'],ifExists:true,// default : a missing index is NOT_FOUNDcontinueOnError:true,// default : one refused drop does not stop the others});// results[0] : { status: 'DROPPED', fields: ['customer_id', 'status'], ... }// results[1] : { status: 'NOT_FOUND', message: 'The index "old_idx_status" does not exist.', ... }
Several tables at once : give a list of such objects, the results come back in one flat list.
instance: "crm::acme" drops in the database of the tenant acme of the multi-tenant instance crm.
g.sys.system.isValidDataForTable calls the is valid data for table system API from your code, whatever the settings of that API say about HTTP callers.
g.sys.system.isValidDataForCustomAPI calls the is valid data for custom API system API from your code, whatever the settings of that API say about HTTP callers.
awaitg.sys.system.isValidDataForThirdPartyAPI({apiBundleName:"bundle_name",apiVersionName:"api_version",name:"api_name",type:"BODY",// BODY | QUERY_PARAMSdata:{name:'hello'},isArray:false});
g.sys.system.multiTenantInstanceUpdated calls the multi tenant instance updated system API from your code, whatever the settings of that API say about HTTP callers.
After the connection string of a tenant of a multi-tenant instance changed in the tenants table, tell API Maker. Every server drops the connection pools of that tenant, and the next request of the tenant connects with the new connection string.