Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
🏗️ Split into sub-packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Errorname committed Jul 17, 2020
1 parent f2b85e9 commit ca6df16
Show file tree
Hide file tree
Showing 80 changed files with 6,736 additions and 5,183 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
build
tests/playground/*
!tests/playground/example
!tests/playground/test-lib.sh
!tests/playground/test-client.sh
packages/cli/README.md
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ npm i -g prisma-multi-tenant
```
$> prisma-multi-tenant help
🧭 prisma-multi-tenant v2.1.0
🧭 prisma-multi-tenant v2.2.0
USAGE
Expand Down Expand Up @@ -82,7 +82,7 @@ $> prisma-multi-tenant help
```

```js
const { MultiTenant } = require('prisma-multi-tenant')
const { MultiTenant } = require('@prisma-multi-tenant/client')

const multiTenant = new MultiTenant()

Expand All @@ -105,8 +105,10 @@ Read more on how `prisma-multi-tenant` can help you achieve multi-tenancy for yo
- [Examples](/docs/examples) - For everyone
- [Basic (JS)](/docs/examples/basic-js)
- [Basic (TS)](/docs/examples/basic-ts)
- Nexus (Work in progress)
- [Issues with Zeit Now](/docs/Zeit_Now.md)
- Nexus (TODO)
- Redwood (TODO)
- Blitz (TODO)
- [Issues with Vercel](/docs/Vercel.md)

## Author

Expand Down
49 changes: 23 additions & 26 deletions docs/Complete_Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

Prisma-multi-tenant uses a "**management**" datasource in order to keep track of all the tenants of your application.

Thanks to this management datasource, prisma-multi-tenant is able to migrate all your tenants, as well as providing you with a simple way to access the data of whichever tenant you want.
Thanks to this management datasource, prisma-multi-tenant is able to migrate all your tenants at once, as well as providing you with a simple way to access the data of whichever tenant you want.

Prisma-multi-tenant is a two-part project:

- The [CLI](#CLI) that you will use to init, develop, and deploy your tenants
- The [Library](#Library) that you will use in your app to access the data in your tenants
- The [CLI](#CLI) (`prisma-multi-tenant`) that you will use to init, develop, and deploy your tenants
- The [Client](#Library) (`@prisma-multi-tenant/client`) that you will use in your app to access the data in your tenants

**Table of content:**

Expand All @@ -33,37 +33,38 @@ Prisma-multi-tenant is a two-part project:

## CLI

> Note: You can run the CLI with either `prisma-multi-tenant` or `pmt`. Example: `pmt init`
### `init`

Init multi-tenancy for your application

**Options**

| Name | Type | Description |
| -------- | ------ | ------------------------------- |
| provider | String | Type of the management provider |
| url | String | URL of the management database |
| Name | Type | Description |
| ---- | ------ | ------------------------------ |
| url | String | URL of the management database |

**Examples**

```sh
prisma-multi-tenant init
prisma-multi-tenant init --provider=sqlite --url=file:management.db
prisma-multi-tenant init --url=file:management.db
```

**Explanations**

The `init` command is used to initialize your application to use `prisma-multi-tenant`. It will do the following:

1. Install `prisma-multi-tenant` locally in your app _(in order to use the library)_
2. Prompt for the management datasource (provider and url)
3. Update the `prisma/.env` file with the management's provider and url
1. Install `@prisma-multi-tenant/client` locally in your app
2. Prompt for the management datasource url
3. Update the `prisma/.env` file with the management's url
4. Generate PrismaClient (for tenants & management)
5. Set up the management datasource
6. Create first tenant based on the `DATABASE_URL` env variable
7. Create an example script (`multi-tenancy-example.js`)

> Note: Make sure you are using `DATABASE_URL` in your schema.prisma
> Note: Make sure you are using `DATABASE_URL` in the default datasource of your schema.prisma file
### `list`

Expand Down Expand Up @@ -101,22 +102,21 @@ Create a new tenant or management
| Name | Type | Description |
| ------------- | ------- | ---------------------------------------------------------------- |
| name | String | Name of the tenant |
| provider | String | Provider of the tenant |
| url | String | URL of the database |
| no-management | Boolean | The new tenant will not be registered in the management database |

**Examples**

```sh
prisma-multi-tenant new
prisma-multi-tenant new --name=company_b --provider=postgresql --url=postgres://...
prisma-multi-tenant new --name=company_b --url=postgres://...
prisma-multi-tenant new --no-management
prisma-multi-tenant new management
```

**Explanations**

The `new` command create a new database using your schema. It will use a name, a provider, and an url (that you can provide as options).
The `new` command create a new database using your schema. It will use a name, and an url (that you can provide as options).

If you want to create a tenant without tracking it in the management datasource, you can use `--no-management`. However be careful, because you will need to manually migrate up and down this tenant after that.

Expand Down Expand Up @@ -254,7 +254,7 @@ Eject prisma-multi-tenant from your application

**Explanations**

The `eject` command can be used if you no longer need `prisma-multi-tenant` in your application. This command will uninstall `prisma-multi-tenant`. It will not touch your databases as you may have important data in them.
The `eject` command can be used if you no longer need `prisma-multi-tenant` in your application. This command will uninstall `@prisma-multi-tenant/client`. It will not touch your databases as you may have important data in them.

### `help`

Expand All @@ -268,7 +268,7 @@ prisma-multi-tenant help

## Library

> Note: You are responsible to provide the environment variable for management any way you want
> Note: The client will try to read the `MANAGEMENT_URL` environment variables in `prisma/.env`, but you can also provide it yourself.
### `constructor(options?: MultiTenantOptions)`

Expand All @@ -277,7 +277,7 @@ Constructor of the `MultiTenant` class.
**Usage (JavaScript)**

```js
const { MultiTenant } = require('prisma-multi-tenant')
const { MultiTenant } = require('@prisma-multi-tenant/client')

const multiTenant = new MultiTenant()
```
Expand All @@ -288,7 +288,7 @@ This will give you autocompletion on your tenants

```ts
const { PrismaClient } = require('@prisma/client')
const { MultiTenant } = require('prisma-multi-tenant')
const { MultiTenant } = require('@prisma-multi-tenant/client')

const multiTenant = new MultiTenant<PrismaClient>()
```
Expand All @@ -299,7 +299,7 @@ If you do not want to connect to the Management database when connecting to a ne

```js
const multiTenant = new MultiTenant({
useManagement: false
useManagement: false,
})
```

Expand Down Expand Up @@ -332,29 +332,26 @@ This method does not connect to management.
```js
const prisma = await multiTenant.directGet({
name: 'your_other_tenant',
url: 'file:something.db'
url: 'file:something.db',
})

const users = await prisma.user.findMany()

console.log(users)
```

### `createTenant(tenant: { name: string, provider: string, url: string }, options?: any): Promise<PrismaClient>`
### `createTenant(tenant: { name: string, url: string }, options?: any): Promise<PrismaClient>`

Creates a new tenant in management and returns the corresponding PrismaClient. Any options passed as second argument will be given to the PrismaClient constructor.

This method will migrate up the new database to be up-to-date with the migrations.

> Note: You currently can't have tenants from multiple datasources. (See #8)
**Usage**

```js
const prisma = await multiTenant.createTenant({
name: 'a_new_tenant',
provider: 'postgresql',
url: 'postgresql://the_postgres_url'
url: 'postgresql://the_postgres_url',
})

const users = await prisma.user.findMany()
Expand Down
40 changes: 32 additions & 8 deletions docs/Contributing_Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,52 @@ Once you have an issue, you can clone and install Prisma-multi-tenant:
```sh
git clone https://github.com/Errorname/prisma-multi-tenant.git

cd prisma-multi-tenant

npm install
npm run install # Not `npm install`!
```

## 3. Running and linking Prisma-multi-tenant

Prisma-multi-tenant is written in Typescript. You will need to build it with the following command:
Prisma-multi-tenant is a mono-repository hosting 3 packages:

- `prisma-multi-tenant` found in `/packages/cli`
- `@prisma-multi-tenant/client` found in `/packages/client`
- `@prisma-multi-tenant/shared` found in `/packages/shared`

Those packages are written in Typescript. For each one of those package, you will need to build it with the following command:

```sh
cd packages/shared
npm run build -- -w

cd packages/cli
npm run build -- -w

cd packages/client
npm run build -- -w
```

Prisma-multi-tenant is a CLI. When running in your console, you will want the command to execute your own builded version of `prisma-multi-tenant`. To do that, you will need to "link" it:
Since NPM installs dependencies from its own repositories, we need to locally link our packages between them:

```sh
# In the Prisma-multi-tenant repository
cd packages/shared
npm link

cd packages/cli
npm link
npm link @prisma-multi-tenant/shared

cd packages/client
npm link
npm link @prisma-multi-tenant/shared
```

Prisma-multi-tenant is also a library. When using it in a project, you will also want to execute your own builded version of `prisma-multi-tenant`. To do that, go into your project's directory and "link" it:
Now, whenever you use the command `prisma-multi-tenant` or `pmt`, it will use your local version.

When using `@prisma-multi-tenant/client` in a project, you will also want to execute your own builded version of `@prisma-multi-tenant/client`. To do that, go into your project's directory and "link" it:

```sh
# In your project's directory
npm link prisma-multi-tenant
npm link @prisma-multi-tenant/client
```

You can now work on your issue! 🥳
Expand All @@ -62,12 +83,15 @@ Your contribution is working beyond expectation? Great, let's add it to the proj
First, make sure your code is correctly formatted:

```sh
# In the root of prisma-multi-tenant
npm run lint
npm run prettier
```

Then, make sure you don't introduce regressions:

```sh
# In the root of prisma-multi-tenant
npm run test
```

Expand Down
30 changes: 16 additions & 14 deletions docs/Getting_Started.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Thanks to this management datasource, prisma-multi-tenant is able to migrate all

Prisma-multi-tenant is a two-part project:

- The **CLI** that you will use to init, develop, and deploy your tenants
- The **library** that you will use in your app to access the data in your tenants
- The **CLI** (`prisma-multi-tenant`) that you will use to init, develop, and deploy your tenants
- The **Client** (`@prisma-multi-tenant/client`) that you will use in your app to access the data in your tenants

## 1. Install Prisma-multi-tenant

Expand All @@ -45,17 +45,19 @@ Now that `prisma-multi-tenant` is available globally on your system, you can use
prisma-multi-tenant init
```

> Note: You can either use the command line `prisma-multi-tenant` or its alias: `pmt`. Example: `pmt init`
Running this command will do the following:

1. Install `prisma-multi-tenant` locally in your app _(in order to use the library)_
2. Prompt for the management datasource (provider and url)
3. Update the `prisma/.env` file with the management's provider and url
4. Generate PrismaClient (for tenants & management)
5. Set up the management datasource
6. Create first tenant based on the `DATABASE_URL` env variable
7. Create an example script (`multi-tenancy-example.js`)
1. Installs `@prisma-multi-tenant/client` in your app
2. Prompts for the management datasource url
3. Updates the `prisma/.env` file with the management's url
4. Generates PrismaClient (for tenants & management)
5. Sets up the management datasource
6. Creates first tenant based on the `DATABASE_URL` env variable
7. Creates an example script (`multi-tenancy-example.js`)

> Note: Make sure you are using `DATABASE_URL` in your schema.prisma
> Note: Make sure you are using `DATABASE_URL` in the default datasource of your schema.prisma file
## 3. Add a new tenant

Expand Down Expand Up @@ -97,23 +99,23 @@ If you want to know how to use the library to access the tenants in your applica

It can be split in 3 parts:

**1. Instantiating MultiTenant**
**a. Instantiating MultiTenant**

```js
const { MultiTenant } = require('prisma-multi-tenant')
const { MultiTenant } = require('@prisma-multi-tenant/client')

const multiTenant = new MultiTenant()
```

**2. Get the tenant**
**b. Get the tenant**

The name can come from anywhere (headers, token, ...)

```js
const prisma = await multiTenant.get('your_tenant_name')
```

**3. Use PrismaClient as usual**
**c. Use PrismaClient as usual**

```js
const users = await prisma.user.findMany()
Expand Down
4 changes: 3 additions & 1 deletion docs/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

- [Basic (JS)](/docs/examples/basic-js)
- [Basic (TS)](/docs/examples/basic-ts)
- Nexus (Work in progress)
- Nexus (TODO)
- Redwood (TODO)
- Blitz (TODO)
4 changes: 2 additions & 2 deletions docs/examples/basic-js/multi-tenancy-example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { MultiTenant } = require('prisma-multi-tenant')
const { MultiTenant } = require('@prisma-multi-tenant/client')

// This is the name of your first tenant, try with another one: "prod"
const name = 'dev'
Expand All @@ -16,7 +16,7 @@ async function main() {
}

main()
.catch(e => console.error(e))
.catch((e) => console.error(e))
.finally(async () => {
await multiTenant.disconnect()
})
Loading

0 comments on commit ca6df16

Please sign in to comment.