Skip to content

jleifeld/adonis-prisma

 
 

Repository files navigation

Adonis Prisma

Prisma Provider for AdonisJS

npm-image license-image typescript-image

If you want to use Prisma on AdonisJS, this package provides you with Prisma Client Provider and Auth Provider.

Getting Started

Installation

Make sure you've already installed Prisma related packages:

npm i --save-dev prisma && npm i @prisma/client

Install this package:

npm i @wahyubucil/adonis-prisma

Setup

node ace configure @wahyubucil/adonis-prisma

It will install the provider, and add typings.

Usage

Prisma Client Provider

Import the Prisma Client from @ioc:Adonis/Addons/Prisma. For example:

import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import { prisma } from '@ioc:Adonis/Addons/Prisma'

export default class UsersController {
  public async index({}: HttpContextContract) {
    const users = await prisma.user.findMany()
    return users
  }
}

Authentication (Prisma Auth Provider)

Install and configure Adonis Auth first:

npm i @adonisjs/auth
node ace configure @adonisjs/auth

When configuring Adonis Auth, you'll be asked some questions related to the provider. Because we're not using the default provider, answer the following questions like these so we can complete the configuration:

❯ Select provider for finding users · database
...
❯ Enter the database table name to look up users · users
❯ Create migration for the users table? (y/N) · false
...

Other questions like guard, storing API tokens, etc, are based on your preference.

After configuring the Adonis Auth, you need to config Prisma Auth Provider. Here's the example.

First, define the schema. Example schema.prisma:

// prisma/schema.prisma

...

model User {
  id              String  @id @default(cuid())
  email           String  @unique
  password        String
  rememberMeToken String?
  name            String
}

...

IMPORTANT: You need to define password and rememberMeToken fields like that because those fields are required.

After configuring the schema, you need to config Prisma Auth Provider on contracts/auth.ts and config/auth.ts. For example:

// contracts/auth.ts
import { PrismaAuthProviderContract, PrismaAuthProviderConfig } from '@ioc:Adonis/Addons/Prisma'
import { User } from '@prisma/client'

declare module '@ioc:Adonis/Addons/Auth' {
  interface ProvidersList {
    user: {
      implementation: PrismaAuthProviderContract<User>
      config: PrismaAuthProviderConfig<User>
    }
  }

  ......
}
// config/auth.ts
import { AuthConfig } from '@ioc:Adonis/Addons/Auth'

const authConfig: AuthConfig = {
  guard: 'api',

  guards: {
    api: {
      driver: 'oat',
      provider: {
        driver: 'prisma',
        identifierKey: 'id',
        uids: ['email'],
        model: 'user',
      },
    },
  },
}

export default authConfig

Then, you're ready to go!

The rest usage is the same as other providers. You can refer to the AdonisJS Authentication guide about the implementation.

Configuration options

Following is the list of all the available configuration options.

{
  provider: {
    driver: 'prisma',
    identifierKey: 'id',
    uids: ['email'],
    model: 'user',
    hashDriver: 'argon',
  }
}
driver

The driver name must always be set to prisma.


identifierKey

The identifierKey is usually the primary key on the configured model. The authentication package needs it to uniquely identify a user.


uids

An array of model columns to use for the user lookup. The auth.login method uses the uids to find a user by the provided value.

For example: If your application allows login with email and username both, then you must define them as uids. Also, you need to define the model column names and not the database column names.


model

The model to use for user lookup.


hashDriver (optional)

The driver to use for verifying the user password hash. It is used by the auth.login method. If not defined, we will use the default hash driver from the config/hash.ts file.


Seeder (Prisma Seeder)

Init Prisma Seeder first with the following Ace command:

node ace prisma-seeder:init

It will create a file prisma/seeders/index.ts to define all of the seeders later.

You can create a new seeder file by running the following Ace command:

node ace prisma-seeder:make User

It will create a file prisma/seeders/User.ts.

Every seeder file must extend the PrismaSeederBase class and implement the run method. Here's an example implementation:

// prisma/seeders/User.ts

import { prisma, PrismaSeederBase } from '@ioc:Adonis/Addons/Prisma'

export default class UserSeeder extends PrismaSeederBase {
  public static developmentOnly = false

  public async run() {
    await prisma.user.upsert({
      where: {
        email: '[email protected]',
      },
      update: {
        name: 'Viola the Magnificent',
      },
      create: {
        email: '[email protected]',
        name: 'Viola the Magnificent',
      },
    })

    await prisma.user.createMany({
      data: [
        { name: 'Bob', email: '[email protected]' },
        { name: 'Bobo', email: '[email protected]' },
        { name: 'Yewande', email: '[email protected]' },
        { name: 'Angelique', email: '[email protected]' },
      ],
      skipDuplicates: true,
    })
  }
}

After creating a seeder, add the file name to prisma/seeders/index.ts. That file is useful to arrange the execution order of all seeders. For example:

// prisma/seeders/index.ts

/**
 * Put all seeders filename here. It will be executed based on the order
 */
export default ['User', 'Category', 'Article']

Running seeders

Before running seeders, make sure you've already config prisma/seeders/index.ts because the execution order will be based on that file.

To run seeders, just run the following ace command:

node ace prisma-seeder:run

Development only seeders

You can mark a seeder file as development only. This ensures that you don't seed your production database with dummy data by mistake. Seeders for development will only run when the NODE_ENV environment variable is set to development.

You can create a development only seeder with --dev as the argument. For example:

node ace prisma-seeder:make User --dev

Or, if you want to make an existing seeder to development only, just change developmentOnly property to true on the implementation. For example:

import { prisma, PrismaSeederBase } from '@ioc:Adonis/Addons/Prisma'

export default class UserSeeder extends PrismaSeederBase {
  public static developmentOnly = true // <-- change this

  public async run() {
    // Write your database queries inside the run method
  }
}

Built with ❤︎ by Wahyu "The GOAT" Bucil

About

Prisma Provider for AdonisJS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 97.8%
  • Shell 1.1%
  • JavaScript 1.1%