Skip to content

linefusion/directus-libsql

Repository files navigation

Directus with libsql

Experimental/PoC support for libsql in Directus.

Caution

This is an experimental project and consists in workarounds and small patches to make it work without modifying the core of Directus. In order to make Directus work nicely with libsql, some changes need to land in both Directus and libsql first.

Running

With Docker

Note that the published image is preconfigured to make it easier to test.

docker run --rm -it -p 8055:8055 linefusion/directus-libsql:latest

To run with Turso, set these variables:

DB_SYNC=true
DB_SYNC_URL=libsql://<project>-<org>.turso.io
DB_SYNC_PERIOD=60
DB_AUTH_TOKEN=<your-token>

Embedded replica will live inside /directus/database folder.

Create a test project

Run npm init @linefusion/directus-libsql.

Inside the project:

  • pnpm directus is an alias to Directus CLI with the config file set.
  • pnpm bootstrap is an alias to directus bootstrap with the config file set.
  • pnpm start is an alias to directus start with the config file set.

Manual installation

Example are given using pnpm, port it to your own package manager of choice.

Create a normal Directus project

  • Create a Directus project pnpm create directus-project some-project
    • Choose SQLite as the database
    • Set ./database.sqlite as your database filename
    • Choose email and password

Install libsql driver

  • Install these packages in your project:

  • Create a config.js inside of it:

    const { withLibsql } = require("@linefusion/directus-libsql");
    module.exports = withLibsql();
  • Configure your .env file or set these environment variables:

    • DB_FILENAME - where your local sqlite database will live.
    • DB_SYNC - if you want to sync changes (true/false).
      • DB_SYNC_URL - remote url to sync changes to/from (url).
      • DB_SYNC_PERIOD - period to automatically sync changes (in seconds, default 60).
      • DB_AUTH_TOKEN - token for syncing (string).
  • Run Directus with CONFIG_PATH pointing to the location fo the config.js you just created.

Examples

Check examples folder. Each example has an associated script in the root package.

To run an example use pnpm example:<name> command (it's 'an alias to directus cli).

Additional Work

  • libsql needs a fix on the parser in order to behave exactly like SQLite.

  • Directus needs to add support for libsql since databases are all hardcoded.

    • See getDatabaseClient function here
    • See getDatabase function here, here and here
    • See createInspector here
    • See createDBconnection here
  • Directus could make it easier to use libsql until referenced issue in libsql is fixed

    • See columnInfo here

      • await this.knex.select('name').from('sqlite_master').whereRaw(`sql LIKE "%AUTOINCREMENT%"`)

        to

        await this.knex.select('name').from('sqlite_master').whereLike('sql', '%AUTOINCREMENT%')