npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@wahyubucil/adonis-prisma

v1.0.1

Published

Prisma Provider for AdonisJS

Downloads

332

Readme

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
  }
}