Welcome to the world of Trilogy, a simple and flexible Promise-based wrapper for SQLite databases designed to enhance your development experience. Whether you are building applications in JavaScript or TypeScript, Trilogy has you covered with a seamless API that combines the power of SQLite with ease of use.
What is Trilogy?
Trilogy is not your typical Object-Relational Mapping (ORM) tool; instead, it focuses on providing a straightforward API that behaves more like Mongoose than native SQL. With support for both the native C++ sqlite3 driver and the pure JavaScript sql.js, Trilogy empowers developers to choose between performance and ease of use.
Features of Trilogy
- :link: Automatic Type Casting – Automatically translates data types between JavaScript and SQLite.
- :battery: Powered by Knex – Uses the powerful Knex query builder for custom queries.
- :nut_and_bolt: Multiple Backends Support – Easily swap between SQLite3 and sql.js, including an in-memory option for performance-critical applications.
- :cop: TypeScript Support – Provides excellent TypeScript support for enhanced development practices.
- :electric_plug: Lifecycle Hooks – Add custom behavior at various stages of your database operations.
- :revolving_hearts: Perfect for Electron Apps – Easily integrate with Electron and NW.js without complicated native compilations.
Installation
To get started with Trilogy, you need to install the package along with your chosen backend. Here’s how to do it:
// Install Trilogy
# using yarn
yarn add trilogy
# using npm
npm i trilogy
// Install either backend
# for sqlite3
# using yarn
yarn add sqlite3
# using npm
npm i sqlite3
// or for sql.js
# using yarn
yarn add sql.js
# using npm
npm i sql.js
Using Trilogy
Here’s a quick overview of how to use Trilogy once it is installed. You can use either async/await or plain Promises:
import connect from 'trilogy';
// Default to using the sqlite3 backend
const db = connect('file.db'); // choose sql.js to avoid native compilation
const db = connect('file.db', { client: 'sql.js' });
// For in-memory storage (fast, unpersisted data)
const db = connect(':memory:', { client: 'sql.js' });
(async function () {
const games = await db.model('games', {
name: { type: String },
genre: { type: String },
released: { type: Date },
awards: { type: Array },
id: { increments: true } // 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]); // Output: Best Multiplayer Game
})();
Think of Trilogy like a Swiss Army Knife for your database needs. It provides various tools – from type casting and query building to backend swaps – catering to both speed and simplicity. Just as you would choose a specific tool from a Swiss Army Knife for your unique task, you can switch between SQLite’s C++ driver for speed and sql.js for hassle-free JavaScript integration.
Troubleshooting
If you encounter any issues during your journey with Trilogy, here are some troubleshooting tips:
- Ensure that you have installed the correct backend and that it’s compatible with your environment.
- Check your database connection paths and ensure that the database files exist in the specified locations.
- Examine the console for any error messages that may point to improper data types or malformed queries.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.
Now go ahead and unleash the potential of Trilogy in your projects! Happy coding!

