hero

this time the SQL really is better than the original

Get Started →



Version License Travis CI Written in TypeScript JavaScript Standard Style Gitter



trilogy is a simple Promise-based wrapper for SQLite databases. It supports both the native C++ sqlite3 (opens new window) driver and the pure JavaScript sql.js (opens new window) backend — compile natively for speed when you need it, or use sql.js headache-free in cross-platform environments and Electron (opens new window) apps.

It's not an ORM and isn't intended to be one — it doesn't have any relationship features. Instead it focuses on providing a simple, clear API that's influenced more by Mongoose (opens new window) than by SQL.


featuresquick start


# features

  • 🔗 automatically casts data between JavaScript & SQLite types

    Define schemas with types like String, Date, or 'increments' — trilogy will handle all the type-casting involved to map accurately between JavaScript and the underlying SQLite database.

  • 🔋 powered by the knex (opens new window) query builder

    trilogy uses knex internally to build its queries, but it's also exposed so you can use it to build your own. No need to mess with ridiculous multi-line strings.

  • 🔩 supports multiple swappable backends ( plus in-memory storage )

    Both the native sqlite3 (opens new window) module and sql.js (opens new window) (pure JavaScript!) are supported. There is also memory-only storage for fast, unpersisted data handling, which is great for tests and performance critical situations.

    You can even swap the backend after you've started, with no changes to the rest of your code!

    learn more

  • 👮 written in TypeScript (opens new window)

    trilogy is written in and provides a first-class experience for TypeScript.

  • 🔌 lifecycle hooks

    Any number of hooks (aka subscribers or listeners) can be attached at several points in the lifecycle — for example onQuery, beforeCreate, afterUpdate. These are useful for debugging and extensibility.

  • 💞 perfect for Electron (opens new window) & NW.js (opens new window)

    Compiling the sqlite3 module for all the platforms you target with Electron or NW.js can be difficult. That's why trilogy also supports the sql.js backend, which doesn't need to be compiled at all!

# quick start

yarn add trilogy sqlite3 # or sql.js
import { connect } from 'trilogy'

const db = connect(':memory:')

;(async function () {
  const games = await db.model('games', {
    name: { type: String },
    genre: String,            // type shorthand
    released: Date,
    awards: Array,
    id: 'increments'          // special type, primary key
  })

  await games.create({
    name: 'Overwatch',
    genre: 'FPS',
    released: new Date('May 23, 2016'),
    awards: [
      'Game of the Year',
      'Best Multiplayer Game',
      'Best ESports Game'
    ]
  })

  const overwatch = await games.findOne({ name: 'Overwatch' })

  console.log(overwatch.awards[1])
  // -> 'Best Multiplayer Game'
})()

Looking for a more thorough guide?