Database

Novel makes use of PostgreSQL exclusively because it has very wide adoption across cloud and a vibrant plugin community.

Knex.js with Objection.js is the ORM of choice for the project.

To install PostgreSQL, follow the Start tutorial.

Setup

If you have not followed the Getting started tutorial, it would be best to start with that

Your Novel instance will use the environment variable below to create a connection pool against your database.

.env
DB_HOST=postgres://username:password@postgres-server:5432/novel

Why not Drizzle/Prisma/TypeORM/Kysely?

  • Prisma uses a different DSL that you most likely would not touch frequently. It also has abstraction overhead and troubleshooting issues due to having a different runtime engine.

  • Drizzle has a smaller community but with comparable pedigree as knex

  • TypeORM is more of an ORM but also suffers from abstraction overhead. It has better support for typescript than knex.

  • Kysely is a newer player and have a smaller community similar to Drizzle.

Managing the Data

There are a lot of tools out there you can use to connec to your database. What we recommend is Datagrip by Jetbrains, or pgAdmin for OSX.

Novel ships with default database tables that function as a foundation for all the relevant features you have access to.

These are located in /packages/novel/migrations.

These migrations are executed during development runtime as well as when you start the server.

Usage

You can use the internal database connection like below

feature.ts
import db from 'novel/db';

await db('accounts').select();

You can use knex methods here.

Request Bound Transactions

You can create a request bound transaction

feature.ts
import db from 'novel/db';

function handler(request, reply) {
    await db().session();
    // this will run in a transaction
    await db('accounts').where('id', 1).update({ test: 1 });
    // and any db calls from required functions
    await updateAccount();
}

All database calls within the request lifecycle uses the transaction.

Base Models

These models follows the multi-tenancy principle. These are also covered under warranty and not advised to be overwritten.

These models are located in /packages/novel/models/.

Overview of the Base Models included with Novel

A deeper discussion and explanation of the way models work are explained below

Changelog

  • 2024-12-20 - Initial Documentation

Last updated

Was this helpful?