Code Monkey home page Code Monkey logo

tenanti's Introduction

Multi-tenant Database Schema Manager for Laravel

Tenanti allow you to manage multi-tenant data schema and migration manager for your Laravel application.

Latest Stable Version Total Downloads Build Status Coverage Status Scrutinizer Quality Score

Version Compatibility

Laravel Tenanti
4.2.x 2.2.x
5.0.x 3.0.x@dev

Installation

To install through composer, simply put the following in your composer.json file:

{
	"require": {
		"orchestra/tenanti": "3.0.*"
	}
}

And then run composer install to fetch the package.

Quick Installation

You could also simplify the above code by using the following command:

composer require "orchestra/tenanti=3.0.*"

Setup

Next add the following service provider in app/config/app.php.

'providers' => array(

	// ...
	'Orchestra\Tenanti\TenantiServiceProvider',
	'Orchestra\Tenanti\CommandServiceProvider',
),

The command utility is enabled via Orchestra\Tenanti\CommandServiceProvider.

Usage

Configuration

First, let's export the configuration to your application configuration folder to customize the option:

php artisan config:publish orchestra/tenanti

Now when you browse to app/config/packages/orchestra/tenanti/config.php you should be welcome with the following config:

<?php

return array(

	// ...

	'drivers' => array(
        'user' => array(
            'model' => 'User',
            'path'  => app_path().'/database/tenant/users',
        ),
    ),
);

You can customize, or add new driver in the configuration. It is important to note that model configuration only work with Eloquent instance.

Setup migration autoload

For each driver, you should also consider adding the migration path into autoload. To do this you can either edit app/start/global.php or composer.json.

global.php
<?php

ClassLoader::addDirectories(array(
	app_path().'/database/tenant/users',
));
composer.json
{
	"autoload": {
		"classmap": [
			"app/database/tenant/users"
		]
	}
}

Setup Model Observer

Now that we have setup the configuration, let add an observer to our User class (preferly in app/start/global.php):

<?php

User::observe(new UserObserver);

and your UserObserver class should consist of the following:

<?php

class UserObserver extends \Orchestra\Tenanti\Observer
{
	public function getDriverName()
	{
		return 'user';
	}
}

Console Support

Tenanti include additional command to help you run bulk migration when a new schema is created, the available command resemble the usage available from php artisan migrate namespace.

Command Description
php artisan tenanti:install {driver} Setup migration table on each entry for a given driver.
php artisan tenanti:make {driver} {name} Make a new Schema generator for a given driver.
php artisan tenanti:migrate {driver} Run migration on each entry for a given driver.
php artisan tenanti:rollback {driver} Rollback migration on each entry for a given driver.
php artisan tenanti:reset {driver} Reset migration on each entry for a given driver.
php artisan tenanti:refresh {driver} Refresh migration (reset and migrate) on each entry for a given driver.

Multi Database Connection Setup

Instead of using Tenanti with a single database connection, you could also setup a database connection for each tenant.

Configuration

By introducing a migration config, you can now setup the migration table name to be tenant_migrations instead of user_{id}_migrations.

<?php

return array(

	// ...

	'drivers' => array(
        'user' => array(
            'model'     => 'User',
            'migration' => 'tenant_migrations',
            'path'      => app_path().'/database/tenant/users',
        ),
    ),
);

Observer

Adding an override method for getConnectionName() would allow you to force the migration to be executed on the desire connection.

<?php

class UserObserver extends \Orchestra\Tenanti\Observer
{
	public function getDriverName()
	{
		return 'user';
	}

	public function getConnectionName()
	{
		return 'tenant_{id}';
	}
}

tenanti's People

Contributors

crynobone avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.