PostgreSQL and MySQL/MariaDB driver for Praia. Implements the database wire protocols using raw TCP sockets — no external C libraries required.
sand install github.com/viggou/praia-dbuse 'db'
let pg = db.postgres({
host: "localhost",
port: 5432,
user: "admin",
password: "secret",
database: "myapp"
})
let rows = pg.query("SELECT * FROM users WHERE age > ?", [18])
for (row in rows) {
print(row.name, row.age)
}
pg.run("INSERT INTO users (name, age) VALUES (?, ?)", ["Ada", 36])
pg.close()
use 'db'
let my = db.mysql({
host: "localhost",
port: 3306,
user: "admin",
password: "secret",
database: "myapp"
})
let rows = my.query("SELECT * FROM products")
my.run("INSERT INTO products (name, price) VALUES (?, ?)", ["Widget", 9.99])
my.close()
Both db.postgres() and db.mysql() return a client with the same interface:
| Method | Description |
|---|---|
client.query(sql, params?) |
Execute a query, return an array of row maps |
client.run(sql, params?) |
Execute a statement, return {changes, lastId} |
client.close() |
Close the connection |
Parameters use ? placeholders and are safely escaped.
| Field | Default | Description |
|---|---|---|
host |
"localhost" |
Server hostname |
port |
5432 |
Server port |
user |
(required) | Username |
password |
"" |
Password |
database |
same as user | Database name |
Supported auth methods: trust, cleartext, MD5, SCRAM-SHA-256.
| Field | Default | Description |
|---|---|---|
host |
"localhost" |
Server hostname |
port |
3306 |
Server port |
user |
(required) | Username |
password |
"" |
Password |
database |
"" |
Database name (optional) |
Supported auth: mysql_native_password (MariaDB default, MySQL 5.x default).
For MySQL 8+ which defaults to caching_sha2_password, create the user with:
CREATE USER 'user'@'%' IDENTIFIED WITH mysql_native_password BY 'password';See the examples/ directory:
postgres_example.praia— CRUD operations with PostgreSQLmysql_example.praia— CRUD operations with MySQL/MariaDBweb_api.praia— REST API backed by PostgreSQL using Praia's router grain
MIT