System APIs
Encrypt data
Encrypt System API
Get encrypted data using global object 'g'.
let encryptedData = await g.sys.system.encrypt({
"data": {
"name": "Bob"
}
});
Decrypt data
Decrypt System API
Get decrypted data using global object 'g'.
let decryptedData = await g.sys.system.decrypt("U2FsdGVkX191c+lmyM80ctf4QV18dZ/x17yDh/fRxs4=");
Hash data
Hash System API
Get hash of the given data using the global object 'g'.
let hashData = await g.sys.system.hash({ "Name": "Alice" });
Get token
Get token using global object 'g'.
Get token api authorization
- By default getToken api is public.
- When settings are available with authTokenInfo array, it is not public otherwise it is public.
- when value in secret provided for authTokenInfo, it has no impact on getToken system api.
| await g.sys.system.getToken({
"authTokenType": "AM",
"authTokenAM": {
"u": "USER_NAME",
"p": "USER_PASSWORD"
}
});
|
- Get Token of database_user (application user).
| await g.sys.system.getToken({
"authTokenType": "AM_DB",
"authTokenAMDB": {
"instance": "INSTANCE_NAME",
"database": "DATABASE_NAME",
"collection": "COLLECTION_NAME",
"usernameColumn": "USER_NAME_COLUMN",
"passwordColumn": "USER_PASSWORD_COLUMN",
"u": "USER_NAME",
"p": "PASSWORD"
}
});
|
Execute query
Execute query System API
Execute database query using global object 'g'.
| await g.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.
- https://www.mongodb.com/docs/drivers/node/current/usage-examples/command/
- https://www.mongodb.com/docs/v6.0/reference/command/
| await g.sys.system.executeQuery({
instance: 'INSTANCE_NAME',
database: 'DATABASE_NAME',
query: {
dbStats: 1,
}
});
|
How we use Native Query in API Maker.
Native query using secret management
Various types of Database & It`s Native Query In API Maker
Please follow these rules when writing a query or statement:
- Use backticks (`) to enclose your query or statement.
- Do not add a semicolon (;) at the end of your query.
| await g.sys.system.executeQuery({
instance: "oracle",
query: `SELECT * FROM "INVENTORY"."employees"`
});
|
DDL Operation in API Maker
-
CREATE TABLE
| await g.sys.system.executeQuery({
instance: "mysql",
query: "CREATE TABLE inventory.employees(id int AUTO_INCREMENT PRIMARY KEY);"
})
|
-
ALTER TABLE
| await g.sys.system.executeQuery({
instance: "mysql",
query: "ALTER TABLE inventory.employees ADD COLUMN emp_name VARCHAR(30) NOT NULL;"
})
|
-
RENAME TABLE
| await g.sys.system.executeQuery({
instance: "mysql",
query: "ALTER TABLE inventory.employees RENAME COLUMN id TO emp_id;"
})
|
-
TRUNCATE TABLE
| await g.sys.system.executeQuery({
instance: "mysql",
query: "TRUNCATE TABLE inventory.employees;"
})
|
-
DROP TABLE
| await g.sys.system.executeQuery({
instance: "mysql",
query: "DROP TABLE IF EXISTS inventory.employees;"
})
|
DML Operation in API Maker
API Maker allows us to perform various DML operations, such as the following examples.
-
INSERT
| await g.sys.system.executeQuery({
instance: "mysql",
query: "INSERT into inventory.employees Values(101, 'Bob', 'M');"
})
|
-
DELETE
| await g.sys.system.executeQuery({
instance: "mysql",
query: "DELETE FROM inventory.employees WHERE emp_Id = '101';"
})
|
-
UPDATE
| await g.sys.system.executeQuery({
instance: "mysql",
query: "UPDATE inventory.employees SET emp_Name = 'Alice' WHERE emp_Id = '101';"
})
|
DQL Operation in API Maker
API Maker allows us to perform various DQL operations, such as the following examples.
-
SELECT
| await g.sys.system.executeQuery({
instance: "mysql",
query: "SELECT * FROM inventory.employees;"
})
|
-
DISTINCT
| await g.sys.system.executeQuery({
instance: "mysql",
query: "SELECT DISTINCT emp_Name FROM inventory.employees;"
})
|
-
ORDER BY
| await g.sys.system.executeQuery({
instance: "mysql",
query: "SELECT * FROM inventory.employees order by emp_Gen,emp_Name;"
})
|
Aggregated Function in API Maker
API Maker allows us to perform various Aggregated Function, such as the following examples.
-
SUM
| await g.sys.system.executeQuery({
instance: "mysql",
query: "SELECT SUM(emp_Sal) as Total_Salary FROM inventory.employees;"
})
|
-
AVG
| await g.sys.system.executeQuery({
instance: "mysql",
query: "SELECT AVG(emp_Sal) as Average_Salary FROM inventory.employees;"
})
|
Joins in API Maker
API Maker allows us to perform Joins, such as the following examples.
-
INNER JOIN
| await g.sys.system.executeQuery({
instance: "mysql",
query: "SELECT * FROM inventory.employees INNER JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;"
})
|
-
LEFT JOIN
| await g.sys.system.executeQuery({
instance: "mysql",
query: "SELECT * FROM inventory.employees LEFT JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;"
})
|
How to call SQL Server Store Procedure
- Just replace your procedure name with below "procedureName".
| await g.sys.system.executeQuery({
instance: "sql_server",
query: "USE inventory; exec dbo.procedureName;"
});
|
How to create index in Mongodb
| import * as T from 'types';
import * as db from 'db-interfaces';
import * as C from 'iot/Constants';
const createIndexList: IIndexObj[] = [{
collectionName: "collectionName",
indexName: 'indexName',
isUnique: true,
fields: {
"category": 1,
"productName": 1,
},
}];
async function main(g: T.IAMGlobal) {
const colMap = {};
for (const iObj of createIndexList) {
await dropIndexListOfCollection(g, iObj.collectionName, iObj.indexName);
await createIndexOnCollection(g, iObj.collectionName, iObj.fields, iObj.indexName, iObj.isUnique);
colMap[iObj.collectionName] = 1;
}
for (const colName in colMap) {
const indexList = await getIndexListOfCollection(g, C.col.configuration_transactions);
colMap[colName] = indexList;
}
return { hello: 'world', colMap };
};
module.exports = main;
async function getIndexListOfCollection(
g: T.IAMGlobal,
collectionName: string,
) {
const listIndexResp = await g.sys.system.executeQuery({
instance: C.insDB.instance,
database: C.insDB.database,
query: {
listIndexes: collectionName,
}
});
const indexList = [];
for (const item of listIndexResp?.cursor?.firstBatch || []) {
indexList.push(item.name);
}
return indexList;
}
async function createIndexOnCollection(
g: T.IAMGlobal,
collectionName: string,
fields: any,
indexName: string,
isUnique: boolean,
) {
const createIndexResp = await g.sys.system.executeQuery({
instance: C.insDB.instance,
database: C.insDB.database,
query: {
createIndexes: collectionName,
indexes: [
{
key: fields,
name: indexName,
unique: isUnique,
},
],
}
});
const indexList = [];
for (const item of createIndexResp?.cursor?.firstBatch || []) {
indexList.push(item.name);
}
return indexList;
}
async function dropIndexListOfCollection(
g: T.IAMGlobal,
collectionName: string,
indexName: string,
) {
try {
const dropResp = await g.sys.system.executeQuery({
instance: C.insDB.instance,
database: C.insDB.database,
query: {
dropIndexes: collectionName,
index: indexName,
}
});
return dropResp;
} catch (e) {
}
}
interface IIndexObj {
indexName: string;
collectionName: string;
fields: any;
isUnique: boolean;
}
|
Call external API
Call external API using global object 'g'.
Simple call external apis
| await g.sys.system.callExternalApi([
{
"url": `your_url`,
"method": "GET",
"timeout": 5000,
"queryParams": { "select": "first_name" },
"headers": { "x-am-authorization": "TOKEN" },
}
]);
|
Parallel type
- If the type is 'parallel' the external APIs are executed in parallel.
| await g.sys.system.callExternalApi([
{
url: "FULL_URL",
method: "POST",
body: [
{
first_name: "James",
last_name: "Bond"
}
],
id: "saveApi",
postProcess: [
{
from: "saveApi.output.data.first_name",
to: "setApi.body.first_name"
}
],
queryParams: {
deep: JSON.stringify([{
s_key: "SOURCE_COLLECTION_COLUMN_NAME",
t_key: "TARGET_COLLECTION_COLUMN_NAME",
t_col: "TARGET_COLLECTION_NAME",
select: "COLUMN_NAMES"
}]),
select: "COLUMN_NAMES",
limit: 2,
sort: "COLUMN_NAMES"
},
timeout: 1000,
headers: { "x-am-authorization": "TOKEN" },
},
{
type: 'parallel',
data: [
{
url: "FULL_URL",
method: 'PUT',
body: {},
id: "setApi",
headers: { "x-am-authorization": "TOKEN" }
},
{
url: "FULL_URL",
method: 'GET',
headers: { "x-am-authorization": "TOKEN" }
}
]
}
]);
|
Sequential type
- If the type is 'sequential' the API calls execute one by one.
| await g.sys.system.callExternalApi([
{
url: "your_url",
method: "GET",
timeout: 5000,
queryParams: { "select": "first_name" },
headers: { "x-am-meta": true },
id: "getData",
output: "any"
},
{
type: "sequential",
data: [
{
url: "your_url",
method: "POST",
body: {},
id: "addData",
preProcess: [
{
from: "getData.output.data",
to: "addData.body"
}
],
headers: { "x-am-authorization": "TOKEN", "x-am-response-case": "capitalCase" }
},
{
url: "your_url",
method: "GET",
headers: { "x-am-response-case": "capitalCase" }
}
]
}
]);
|
Get secret key/keys
Get secret System API
Get secret using global object 'g'.
- Get only one secret key value.
| await g.sys.system.getSecret("common.secret");
|
- Get multiple secret keys values.
| await g.sys.system.getSecret(["common.secret","common.secret"]);
|
Get table metadata using global object 'g'.
| await g.sys.system.getTableMeta({
instance: 'INSTANCE_NAME',
database: 'DATABASE_NAME',
collection: 'COLLECTION_NAME'
});
|
Emit event
Emit event System API
Emit event using global object 'g'.
| await g.sys.system.emitEvent("EVENT_NAME", "EVENT_DATA", ["LISTENERS_NAME"]);
|
Emit event websocket
Emit event web-socket System API
Emit event of websocket using global object 'g'.
| await g.sys.system.emitEventWS("EVENT_NAME", "EVENT_DATA");
|
| await g.sys.system.emitEventWS(
"myEvent",
{ "name": "Bob" }
);
|
Is valid data for table/collection
Validate table data
Test data valid or not using global object 'g'.
| await g.sys.system.isValidDataForTable({
instance: "INSTANCE_NAME",
database: "DATABASE_NAME",
collection: "COLLECTION_NAME",
data: {
"field": "fieldValue",
"field": "fieldValue"
}
});
|
| await g.sys.system.isValidDataForTable({
instance: "mongoDB",
database: "db_inventory",
collection: "tbl_customers",
data: {
"customer_id": 1,
"first_name": "Bob",
"last_name": "Lin",
"last_update": "2006-02-14T23:04:33.000Z",
"pincode": 382330,
"isActive": 1
}
});
|
Is valid data for custom API
Validate custom API body object
Test custom API data valid or not using global object 'g'.
| await g.sys.system.isValidDataForCustomAPI({
name: "custom_api_name",
data: {
"some": "data",
"some": "data"
},
type: "BODY" // BODY | QUERY_PARAMS
});
|
Is valid data for third party API
Validate third party request body
Test the data is valid for Third party API, using global object 'g'.
| await g.sys.system.isValidDataForThirdPartyAPI({
apiBundleName: "bundle_name",
apiVersionName: "api_version",
name: "api_name",
type: "BODY", // BODY | QUERY_PARAMS
data: {
name: 'hello'
},
isArray: false
});
|