← Back
architecture knex orm postgres sql

ORM-less applications

Through this post https://www.executeprogram.com/blog/porting-to-typescript-solved-our-api-woes I've arrived to schemats and this other post https://www.cs.mcgill.ca/~mxia3/2016/11/18/Statically-typed-PostgreSQL-queries-and-typescript-schemats/

The idea is interesting: remove ORM from applicatons and reverse process. Instead of code -> data, do data -> code.

Not sure if I'm ready, but working on it

Generate TS definitions with schemats #

npm i knex
npm i -D schemats
{
"scripts": {
"db:schema": "schemats generate -c postgres://postgres:secret@localhost:54329/postgres -s public -o src/db/schema.ts"
}
}

src/db/index.ts:

import knex from "knex";
import { DATABASE_URL } from "./config";
import { usersFields } from "./schema";

const db = knex({
client: "pg",
connection: DATABASE_URL,
});

export type User = {
id: usersFields.id;
email: usersFields.email;
password: usersFields.password;
role: usersFields.role;
};

export const findUserByEmail = async (email: string): Promise<User> =>
await db.select("*").from("users").where("email", email).first();

export default db;