[Module] trilogy - sqlite, embedded-style


#1

Hi everyone! An uber popular question around here is which embedded database to choose for an Electron app, and another is how to get sqlite3 working. Maybe I can answer both for you today.

I’ve been working on a module for quite a while that originally began as a part of an app I was working on in Electron. I was tired of dealing with node-gyp or pre-gyp just to use sqlite3. I considered using nedb, MarsDB, and any number of other noSQL-style embedded databases before deciding to go with SQL.js. But unfortunately, no ORMs are built to simplify using it.

And that’s when I started building trilogy. It’s a database management layer on top of SQL.js and knex - a popular SQL query string builder. trilogy gives you a much more JS-friendly syntax for dealing with SQLite and has flexible variadic ( polymorphic, overloaded, etc. ) functions. If you’re interested in that last bit, also check out arify - another module I’ve been working on recently.

Here’s a super quick usage example. If you want more examples check out the documentation. trilogy is built on Promises so this uses async/await. If you’re not familiar here’s a gist for vanilla Promises.

import Trilogy from 'trilogy'

const db = new Trilogy('./storage.db')

;(async () => {
  await db.createTable('people', [
    'name',
    { name: 'age', type: 'integer' }
  ])

  await db.insert('people', { name: 'Ben Button', age: 100 })

  // example of the variadic API - amount defaults to 1
  await db.decrement('people', 'age', { name: 'Ben Button' })

  // we can also shorten the table & column arguments with dot-notation
  await db.decrement('people.age', 2, { name: 'Ben Button' })

  await db.count('people')
  await db.count('people', ['age', '<', '100'])
  // -> 1
})()

I’ve been using it for a bit, and have never released anything before, so I totally welcome discussion, feedback, and tips.

Check out trilogy at these places:

Thank you!


#2

Hey @citycide – thanks for putting this together! I’ve had exactly the problems you describe with sqlite3 et al., so I’ll definitely give this a look.


#3

We are using lowdb and we recommend it if JSON format solves your storage needs.


#4

Hope it has worked for you if you’ve had the opportunity to try it. I recently pushed some changes in an effort to lighten up the initial install ( like removing a dependency on the main lodash library and on bluebird ).

My next mission will be pushing type definitions ( flow & typescript ), but other than that if you’ve got any feedback let me know!

@joelwallis lowdb would probably be a fine alternative for someone needing a non-relational, flat file storage method. But trilogy provides the full relational power of SQLite and true .sqlite database files.