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