Javascript

Javascript runtime is based on ECMAScript 5.1(+). You don't need to install any other js runtime like nodejs.

On top of ECMAScript apizilla provides additional logic for interacting with mysql, postgres, rabbit, rest

When apizilla is started in dev env, then js code is hot-reloaded on every request

Running single query, if error occur (i.e sql error) then exception gets raised. You can catch it, or do nothing and let apizilla to pass exception to further.

try {
    let results = run(
        "query-name", 
        {
            "paramName1": "value",
            "param2": true
        }
    )
} catch (e) {
    console.log(JSON.stringify(e))
    //change error data
    //setErrorResult({ "ok": false })
    //or pass it as it is
    throw e
}

To improve performance you could run any number of queries in parallel. In case of an error multiRun does not throw an exception, you must manually check if error status is empty


let multiRunOutput = multiRun([
    {
        "query": "insert-order",
        "params": {
            "totalPrice": 100,
        }
    },
    {
        "query": "select-product-by-id",
        "params": {
            "id": "123",
        }
    },
    {
        "query": "http-get-map",
        "params": {
            "long": 1.23456,
            "lat": 2.3456,
        }
    },
    {
        "query": "push-random-value-to-queue",
        "params": {
            "random": "thisIsSomeRandomText"
        }
    },
])

console.log(JSON.stringify(multiRunOutput))

setStatusCode(200)
setResult(multiRunOutput)

multiRun has an alias runMulti

transaction starts new database transaction on postgres or mysql database connection

//------------------------ SAMPLE TRANSACTION ---------------------------
let currentTransaction = transaction("sample-database-connection")
try {
    let orderData = currentTransaction.run("orders-insert", {
        "id": orderId,
        "amount": totalPrice,
        "currentUserId": user.id,
        "customId": uuid(),
    }).Data

    let orderItems = correctProducts.map(row => {
        return currentTransaction.run("order-item-insert", {
            "orderId": orderData.id,
            "productId": row.product.id,
            "quantity": row.quantity
        })
    }).map(row => row.Data)

    currentTransaction.commit()
} catch (e) {
    currentTransaction.rollback()
    throw {"error": "order process has failed"}
}
// method is responsible for setting http status code
setStatusCode(200)

//method is responsible for returning query results
setResult("query result")
setResult({"ok": true})

// setErrorResult method is responsible for returning error result
setErrorResult("query result")
setErrorResult({"ok": true})

//setting http headers
setHeader('Content-Disposition', 'attachment; filename="test.txt"')
setHeader('Content-Type', 'text/plain')

uuid generated uuid4 identifier

let newPublicId = uuid(); //return true

hasPermission checks if current user has defined permission

users.json

  {
    "login": "demo",
    "password": "demo",
    "permissions": ["admin", "demo_perm"],
    "id": 1,
    "otherData": {
      "test": 1
    }
  }
let isOk = hasPermission("demo_perm") //return true

Define javascript query

{
  "type": "javascript",
  "id": "demo-js-query",
  "file": "demo.js",
  "public": true,
  "params": []
}
console.log(JSON.stringify({
    request: request, //contains request data
    user: user, //contains current user data (that was logged in via admin panel)
    config: config, //config.json file content
    params: request.params //all params defined in query
}))
Last modified April 16, 2024: content fix (a2d9b96)