Skip to content

darkterminal/turso-laravel-tenancy

Repository files navigation

Tenancy for Laravel with Turso/libSQL Database

Requirement

Use Case

  • libSQL Multi Tenant Database - Local File Connection ✅
  • libSQL Multi Tenant Database - Remote Connection ✅
  • libSQL Multi Tenant Database - Multi-DB Schemas (Only work with Remote Connection) ✅

Tech Stacks

Supported Tenant Commands

Commands Local Remote Multi-DB Schema
tenants:list
tenants:migrate
tenants:migrate-fresh
tenants:rollback
tenants:run
tenants:seed

Local Connection Usage

DB_CONNECTION=libsql
DB_DATABASE=database.sqlite

Remote Connection Usage

  1. Create new database
turso db create <your-db-name>
  1. Get database URL and add to DB_SYNC_URL
turso db show <your-db-name>

Name:           <your-db-name>
URL:            libsql://<your-db-name>-<your-organization-name>.turso.io
ID:             *******-****-****-****-************
Group:          <your-group-name>
Version:        *.**.**
Locations:      ***
Size:           *** kB
Archived:       No
Bytes Synced:   0 B
Is Schema:      No

Database Instances:
NAME     TYPE        LOCATION
***      primary     ***
  1. Create database auth token and add to DB_AUTH_TOKEN
turso db tokens create <your-db-name>
  1. Create Platform API Token and add to TURSO_API_TOKEN
turso auth api-tokens mint <your-app-name> # named whatever you want

Setting up your Environment Variables

DB_CONNECTION=libsql
DB_AUTH_TOKEN=<your-database-auth-token>
DB_SYNC_URL=<your-database-url>
DB_REMOTE_ONLY=true

TURSO_API_TOKEN=<your-platform-api-token>
TURSO_DB_PRIMARY_NAME=<your-db-name>
TURSO_DB_PRIMARY_ORG=<your-organization-name>
TURSO_DB_DEFAULT_GROUP=<your-group-name>

Multi-DB Schema in Remote Connection Usage

  1. Create new database
turso db create <your-db-name> --type schema
  1. Get database URL and add to DB_SYNC_URL
turso db show <your-db-name>

Name:           <your-db-name>
URL:            libsql://<your-db-name>-<your-organization-name>.turso.io
ID:             *******-****-****-****-************
Group:          <your-group-name>
Version:        *.**.**
Locations:      ***
Size:           *** kB
Archived:       No
Bytes Synced:   0 B
Is Schema:      Yes

Database Instances:
NAME     TYPE        LOCATION
***      primary     ***
  1. Create group auth token and add to DB_AUTH_TOKEN
turso db group tokens create <your-group-name>
  1. Create Platform API Token and add to TURSO_API_TOKEN
turso auth api-tokens mint <your-app-name> # named whatever you want

Setting up your Environment Variables

DB_CONNECTION=libsql
DB_AUTH_TOKEN=<your-group-auth-token>
DB_SYNC_URL=<your-database-url>
DB_REMOTE_ONLY=true

TURSO_API_TOKEN=<your-platform-api-token>
TURSO_DB_PRIMARY_NAME=<your-db-name>
TURSO_DB_PRIMARY_ORG=<your-organization-name>
TURSO_DB_DEFAULT_GROUP=<your-group-name>
TURSO_MULTIDB_SCHEMA=true

Support Me is Not Illegal