Skip to content

ts-query-model

API documentation

Types for database queries with minimum fuss. Supports MySQL, PostgreSQL and SQLite.

Quick overview

1. Connect to your database

ts
import { 
Database
} from "ts-query-model";
import
MySQLConnection
from "ts-query-model/mysql";
const
db
= new
Database
(
new
MySQLConnection
({
uri
: "mysql://your-database-connection-string",
}) );

2. Build your model

ts
const 
myAwesomeModel
= {
getUsers
:
db
.
getMany
({
name
: "get-all-users",
columns
: {
id
:
columns
.
numberAutoIncrement
(),
name
:
columns
.
string
(),
dateCreated
:
columns
.
date
(),
isAwesome
:
columns
.
booleanInt
(),
},
query
: (
limit
: number) =>
SQL
`SELECT id, name, dateCreated, isAwesome FROM users LIMIT ${
limit
}`,
}), };

3. Easy, correctly typed queries

ts
const 
awesomeUsers
= await
myAwesomeModel
.
getUsers
(10);