Your own database — right from a script and from a website page
A lead from a landing page lands in your table before the page has even answered the visitor. A scheduled script checks your data before it touches campaigns. Both take a few lines of the same JavaScript in which Qubix scripts and site handlers are written.
This is Qubix.db — a connection to your own PostgreSQL or MySQL database (MariaDB too) right
from Qubix code. It works in two places: in scripts and in the page handlers of
an uploaded website. The code opens a connection with a connection string, reads
rows with a query and changes data — with the rights the database gives to the account from that
string.
What it gives you
Leads — straight into your database. A page handler accepts a form and writes it into your table
together with the visitor's country and the click markers the page passed on — click_id,
sub1…sub5. The write happens right during the request, so the row is already in the database when
the visitor sees the answer.
The page answers based on your data. Before answering, the handler can ask your database: check a promo code, find a record by the number from the link, decide where to send the visitor.
Your database decides — the script acts. A scheduled script reads your table — for example, a campaign stop list — and pauses or turns on campaigns by it. The reverse path works too: the script puts Qubix numbers into your table.
The connection goes where the administrator allowed. Qubix connects to your database directly and only to the hosts the administrator entered in the list.
How it works
Qubix.db.connect() takes a connection string — postgres:// or postgresql:// for PostgreSQL,
mysql:// or mariadb:// for MySQL and MariaDB — and returns a connection. On the connection, query runs a query and returns rows — each row comes
as an object with the column names — exec makes a change and returns the number of changed rows, and close
closes the connection.
Values are passed separately from the query text — $1, $2 for PostgreSQL, ? for MySQL — so what
a visitor sent stays a value and does not become part of the query itself. A connection the code left
open is closed on its own when the run is over. For a handler, every page request opens its own
connections and closes them before the answer to the visitor.
Where connections may go is decided by the administrator: the Database hosts for scripts list sits in the Qubix-Einstellungen window on the JavaScript tab. There is one list for scripts and page handlers, and an empty list turns connections off. A host name in the list opens only addresses on the internet; a database on an internal network is entered as an address or a network — after that it can also be reached by name. The same tab holds the limits: how many connections and queries per script run or per page request, how many rows per query, what answer size and what timeout a query has. Rows beyond the limit are not read, and a warning says so — in the script console or in the log of the handler's test run.
The password from the connection string does not get into error texts: if the database refused the login, the console shows the reason already without it.
Example: a lead from a landing page — straight into your database
The handler of the /lead path in the site card, on the Backend tab. It writes
the lead and sends the visitor to a thank-you page:
function handle(req, res) {
const lead = req.body || {}
const geo = req.variables.geo || ''
const db = Qubix.db.connect('postgres://landing:[email protected]:5432/crm')
db.exec(
'INSERT INTO leads (email, click_id, geo) VALUES ($1, $2, $3)',
[lead.email || null, req.variables.click_id || null, geo],
)
db.close()
const thanks = new URL('https://example.com/thanks')
thanks.searchParams.set('geo', geo)
res.redirect(thanks.toString())
}
req.body arrives already parsed — both JSON and form fields. Qubix determines the visitor's country
itself, and click_id and sub1…sub5 are taken from the request parameters.
Example: your database decides, the script acts
A scheduled script reads a stop list from your MySQL database and pauses those campaigns for a day:
function main() {
const db = Qubix.db.connect('mysql://qubix:[email protected]:3306/team')
const stop = db.query('SELECT campaign_id FROM stop_list WHERE active = 1')
.map((row) => String(row.campaign_id))
db.close()
const campaigns = QubixApp.campaigns().filter((c) => stop.includes(c.campaign_id)).get()
for (const campaign of campaigns) {
campaign.pause({ duration: '24h', reason: 'stop list from the team database' })
}
}
Where to turn it on
- An administrator opens Qubix-Einstellungen → JavaScript and enters your database host in the Database hosts for scripts field — one per line. While the field is empty, connections to databases are off.
- The limits for scripts are right there, under the list; the limits for handlers are in the Website-Seiten-Handler (Locations) block.
- A script is written in the Scripts section and checked with the Ausführen button. A handler is written in the site card on the Backend tab and checked with the Ausführen button.
The run buttons execute the code in full, database queries included: an INSERT from a handler run
with the Ausführen button adds a row to your table. Keep a separate table or a
separate database for tests.
The editor's hints know Qubix.db
The script editor and the handler editor on the Backend tab suggest
Qubix.db the same way as the other commands: start typing Qubix.db. — and the editor offers
connect, and on a connection — query, exec and close with descriptions.
Also in this release
A handler in the handle(req, res) form. The names are the same as in Node and Express: the
request body is already parsed into req.body, next to it are req.cookies and req.ip, the answer
is built with res.json, res.send, res.redirect and res.cookie, and console.log writes to the
run log. Handlers in the old handle(r) form work as before. More — The handler code.
URL and URLSearchParams across all Qubix JavaScript — in scripts, Britva rules and
page handlers: a link and a query string are parsed and built with these familiar names, so code that
parses links carries over from the browser almost as it is. More —
Which standard JavaScript is available.
Your data stays in your database, and the script and the website page now work with it directly.