|
| 1 | +<p align="center" style="vertical-align:middle"> |
| 2 | + <a href="https://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo_text.svg" width="200" alt="Nest Logo" /></a><br /><a href="https://supabase.com/" target="blank"><img src="https://user-images.githubusercontent.com/8291514/213727225-56186826-bee8-43b5-9b15-86e839d89393.png#gh-dark-mode-only" width="500" alt="Supabase"></a> |
| 3 | +</p> |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +The [Supabase](https://github.com/supabase/supabase-js) module for [NestJS](https://github.com/nestjs/nest). |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +First install the dependencies via `npm`, `pnpm` or `yarn`: |
| 12 | + |
| 13 | +npm: |
| 14 | +```bash |
| 15 | +$ npm i -s @nest/common @supabase/supabase-js nest-supabase |
| 16 | +``` |
| 17 | + |
| 18 | +pnpm: |
| 19 | +```bash |
| 20 | +$ pnpm add @nest/common @supabase/supabase-js nest-supabase |
| 21 | +``` |
| 22 | + |
| 23 | +yarn: |
| 24 | +```bash |
| 25 | +$ yarn add @nest/common @supabase/supabase-js nest-supabase |
| 26 | +``` |
| 27 | + |
| 28 | +## Configuration |
| 29 | + |
| 30 | +First, import the module into your NestJS application and configure it using the configuration key provided by Supabase. |
| 31 | + |
| 32 | +```typescript |
| 33 | +import { Module } from '@nestjs/common'; |
| 34 | +import { SupabaseModule } from 'nestjs-supabase'; |
| 35 | + |
| 36 | +@Module({ |
| 37 | + imports: [ |
| 38 | + SupabaseModule.forRoot({ |
| 39 | + supabaseKey: 'YOUR_SUPABASE_KEY', |
| 40 | + supabaseUrl: 'YOUR_SUPABASE_URL', |
| 41 | + }), |
| 42 | + ], |
| 43 | +}) |
| 44 | +export class AppModule {} |
| 45 | +``` |
| 46 | + |
| 47 | +Or, You can configure it asynchronously as follows: |
| 48 | + |
| 49 | +```typescript |
| 50 | +import { Module } from '@nestjs/common'; |
| 51 | +import { SupabaseModule } from 'nestjs-supabase'; |
| 52 | + |
| 53 | +@Module({ |
| 54 | + imports: [ |
| 55 | + SupabaseModule.forRootAsync({ |
| 56 | + imports: [], |
| 57 | + inject: [], |
| 58 | + useFactory: () => ({ |
| 59 | + supabaseKey: 'YOUR_SUPABASE_KEY', |
| 60 | + supabaseUrl: 'YOUR_SUPABASE_URL', |
| 61 | + }), |
| 62 | + }), |
| 63 | + ], |
| 64 | +}) |
| 65 | +export class AppModule {} |
| 66 | +``` |
| 67 | + |
| 68 | +## Multiple connections |
| 69 | + |
| 70 | +In certain situations, we will need to connect to different Supabase projects, with this module this is possible: |
| 71 | + |
| 72 | +```typescript |
| 73 | +import { Module } from '@nestjs/common'; |
| 74 | +import { SupabaseModule } from 'nestjs-supabase'; |
| 75 | + |
| 76 | +@Module({ |
| 77 | + imports: [ |
| 78 | + SupabaseModule.forRoot([ |
| 79 | + { |
| 80 | + name: 'connection1', |
| 81 | + supabaseConfig: { |
| 82 | + supabaseKey: 'YOUR_SUPABASE_KEY', |
| 83 | + supabaseUrl: 'YOUR_SUPABASE_URL', |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: 'connection2', |
| 88 | + supabaseConfig: { |
| 89 | + supabaseKey: 'YOUR_SUPABASE_KEY', |
| 90 | + supabaseUrl: 'YOUR_SUPABASE_URL', |
| 91 | + }, |
| 92 | + }, |
| 93 | + ]), |
| 94 | + ], |
| 95 | +}) |
| 96 | +export class AppModule {} |
| 97 | +``` |
| 98 | + |
| 99 | +Or asynchronously: |
| 100 | + |
| 101 | +```typescript |
| 102 | +import { Module } from '@nestjs/common'; |
| 103 | +import { SupabaseModule } from 'nestjs-supabase'; |
| 104 | + |
| 105 | +@Module({ |
| 106 | + imports: [ |
| 107 | + SupabaseModule.forRootAsync({ |
| 108 | + imports: [], |
| 109 | + inject: [], |
| 110 | + useFactory: () => ([ |
| 111 | + { |
| 112 | + name: 'connection1', |
| 113 | + supabaseConfig: { |
| 114 | + supabaseKey: 'YOUR_SUPABASE_KEY', |
| 115 | + supabaseUrl: 'YOUR_SUPABASE_URL', |
| 116 | + }, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: 'connection2', |
| 120 | + supabaseConfig: { |
| 121 | + supabaseKey: 'YOUR_SUPABASE_KEY', |
| 122 | + supabaseUrl: 'YOUR_SUPABASE_URL', |
| 123 | + }, |
| 124 | + }, |
| 125 | + ]), |
| 126 | + }), |
| 127 | + ], |
| 128 | +}) |
| 129 | +export class AppModule {} |
| 130 | +``` |
| 131 | + |
| 132 | +## Usage |
| 133 | + |
| 134 | +First, inject the client into the module where you want to use it: |
| 135 | + |
| 136 | +```typescript |
| 137 | +import { Module } from '@nestjs/common'; |
| 138 | +import { SupabaseModule } from 'nestjs-supabase'; |
| 139 | + |
| 140 | +@Module({ |
| 141 | + imports: [ |
| 142 | + SupabaseModule.injectClient(), |
| 143 | + ], |
| 144 | +}) |
| 145 | +export class CatModule {} |
| 146 | +``` |
| 147 | + |
| 148 | +Or, for a specific connection: |
| 149 | + |
| 150 | +```typescript |
| 151 | +import { Module } from '@nestjs/common'; |
| 152 | +import { SupabaseModule } from 'nestjs-supabase'; |
| 153 | + |
| 154 | +@Module({ |
| 155 | + imports: [ |
| 156 | + SupabaseModule.injectClient('connection1', 'connection2'), |
| 157 | + ], |
| 158 | +}) |
| 159 | +export class CatModule {} |
| 160 | +``` |
| 161 | + |
| 162 | +Now you can use the Supabase client in any provider of your module: |
| 163 | + |
| 164 | +```typescript |
| 165 | +import { SupabaseClient } from '@supabase/supabase-js'; |
| 166 | + |
| 167 | +export class CatService { |
| 168 | + constructor(private readonly supabaseClient: SupabaseClient) {} |
| 169 | + |
| 170 | + public doSomething(): void { |
| 171 | + |
| 172 | + } |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +Or, for a specific connection: |
| 177 | + |
| 178 | +```typescript |
| 179 | +import { SupabaseClient } from '@supabase/supabase-js'; |
| 180 | +import { InjectSupabaseClient } from 'nestjs-supabase'; |
| 181 | + |
| 182 | +export class CatService { |
| 183 | + constructor( |
| 184 | + @InjectSupabaseClient('connection1') private readonly supabaseClient1: SupabaseClient, |
| 185 | + @InjectSupabaseClient('connection2') private readonly supabaseClient2: SupabaseClient, |
| 186 | + ) {} |
| 187 | + |
| 188 | + public doSomething(): void { |
| 189 | + |
| 190 | + } |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +It's also possible to use the InjectSupabaseClient decorator to inject the Supabase client when you don't want to explicitly type it with the client: |
| 195 | + |
| 196 | +```typescript |
| 197 | +import { SupabaseClient } from '@supabase/supabase-js'; |
| 198 | +import { InjectSupabaseClient } from 'nestjs-supabase'; |
| 199 | + |
| 200 | +export class CatService { |
| 201 | + constructor( |
| 202 | + @InjectSupabaseClient() private readonly supabaseClient: unknown, |
| 203 | + ) {} |
| 204 | + |
| 205 | + public doSomething(): void { |
| 206 | + |
| 207 | + } |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +## 🤝 Contributing |
| 212 | + |
| 213 | +Contributions, issues and feature requests are welcome. |
| 214 | + |
| 215 | +## Authors |
| 216 | + |
| 217 | +👤 **Adrián Martínez Jiménez** |
| 218 | + |
| 219 | +- Github: [@adrianmjim](https://github.com/adrianmjim) |
| 220 | + |
| 221 | +See also the list of contributors who [participated](https://github.com/adrianmjim/nestjs-supabase/contributors) in this project. |
| 222 | + |
| 223 | +## Show Your Support |
| 224 | + |
| 225 | +Please ⭐️ this repository if this project helped you! |
| 226 | + |
| 227 | +## 📝 License |
| 228 | + |
| 229 | +Copyright © 2024 [Adrián Martínez Jiménez](https://github.com/adrianmjim). |
| 230 | + |
| 231 | +This project is licensed under the MIT License - see the [LICENSE file](LICENSE) for details. |
0 commit comments