What we're doing differently?
|
---|
Not an ORM like Prisma or Drizzle, and yet, not an ordinary database query client!
Here's a breif tour:
|
A SQL-native experience
If you miss the art and power of SQL, then you'll love Linked QL! While SQL as a language may have come to be the exception in the database tooling ecosystem, it is the default in Linked QL! That is a go-ahead to, in fact, #usethelanguage whenever it feels inclined!
└ Preview:
const result = await client.query(
`SELECT
name,
email
FROM users
WHERE role = $1`,
['admin']
);
console.log(result);
const result = await client.query(
`CREATE TABLE users (
id int primary key generated always as identity,
name varchar,
email varchar,
phone varchar,
role varchar,
created_time timestamp
)`
);
console.log(result);
|
Powerful syntax sugars
Go ahead and model structures and traverse relationships like they were plain JSON objects—right within the language! Meet Linked QL's set of syntax extensions to SQL that do the hard work, cut your query in half, and even save you multiple round trips! (See ➞ JSON Sugars, Magic Paths, Upserts)
└ Preview:
const result = await client.query(
`SELECT
name,
email,
{ email, phone AS mobile } AS format1,
[ email, phone ] AS format2
FROM users`
);
console.log(result);
const result = await client.query(
`SELECT
title,
content,
author ~> name AS author_name
FROM books
WHERE author ~> role = $1`,
['admin']
);
console.log(result);
const result = await client.query(
`UPSERT INTO public.users
( name, email, role )
VALUES
( 'John Doe', 'jd@example.com', 'admin' ),
( 'Alice Blue', 'ab@example.com', 'guest' )`
);
console.log(result);
|
Progressive enhancement
While the typical ORM often imposes a high level of abstraction where that's not desired, Linked QL offers a SQL-by-default, progressive enhancement workflow that lets you think from the ground up! And at whatever part of that spectrum you find a sweet spot, you also get the same powerful set of features that Linked QL has to offer! (See ➞ Examples)
└ Preview:
const result = await client.query(
`SELECT
name,
email
FROM users
WHERE role = $1 OR role = $2`,
['admin', 'contributor']
);
const result = await client.database('public').table('users').select({
fields: [ 'name', 'email' ],
where: { some: [
{ eq: ['role', { binding: 'admin' }] },
{ eq: ['role', { binding: 'contributor' }] }
] }
});
const result = await client.database('public').table('users').select({
fields: [ 'name', 'email' ],
where: (q) => q.some(
(r) => r.eq('role', (s) => s.binding('admin')),
(r) => r.eq('role', (s) => s.binding('contributor')),
)
});
|
Automatic schema inference
Whereas the typical ORM requires you to feed them with your database schema (case in point: Drizzle), Linked QL automatically infers it and magically maintains 100% schema-awareness throughout (without necessarily looking again)! You get a whole lot of manual work entirely taken out of the equation! (See ➞ Automatic Schema Inference)
└ Preview:
Simply plug to your database and play:
import pg from 'pg';
import { SQLClient } from '@linked-db/linked-ql/sql';
const connectionParams = { connectionString: process.env.SUPABASE_CONNECTION_STRING }
const pgClient = new pg.Client(connectionParams);
await pgClient.connect();
const client = new SQLClient(pgClient, { dialect: 'postgres' });
Query structures on the fly... without the upfront schema work:
const result = await client.query(
`SELECT
access_token,
user_id: { email, phone, role } AS user,
last_active
FROM auth.users
WHERE user_id ~> email = $1`,
['johndoe@example.com']
);
|
Automatic schema versioning
While the typical database has no concept of versioning, Linked QL comes with it to your database, and along with it a powerful rollback (and rollforward) mechanism! On each DDL operation you make against your database (CREATE , ALTER , DROP ), you get a savepoint automatically created for you and a seamless rollback path you can take anytime! (See ➞ Automatic Schema Versioning)
└ Preview:
Perform a DDL operation and obtain a reference to the automatically created savepoint:
const savepoint = await client.query(
`CREATE TABLE public.users (
id int,
name varchar
)
RETURNING SAVEPOINT`,
{ desc: 'Create users table' }
);
const savepoint = await client.database('public').savepoint();
Either way, see what you got there:
console.log(savepoint.versionTag());
console.log(savepoint.commitDesc());
console.log(savepoint.commitDate());
console.log(savepoint.reverseSQL());
await savepoint.rollback({
desc: 'Users table no more necessary'
});
|
Diff-based migration
Whereas schema evolution remains a drag in the database tooling ecosystem, it comes as a particularly nifty experience in Linked QL! As against the conventional script-based migrations approach, Linked QL follows a diff-based approach that lets you manage your entire DB structure declaratively out of a single schema.json (or schema.yml ) file! (See ➞ Migrations)
└ Preview:
Declare your project's DB structure:
./database/schema.json
[
{
"name": "database_1",
"tables": []
},
{
"name": "database_2",
"tables": []
}
]
For an existing DB, usa a command to generate your DB structure: npx linkedql generate .
Extend your database with tables and columns. Remove existing ibjects or edit them in-place. Then, use a command to commit your changes to your DB:
npx linkedql commit
|
And we've got a few things in the radar: extensive TypeScript support (something we love about Prisma); Linked QL Realtime—a realtime data API for offline-first applications.
|