Git Product home page Git Product logo

adonis-prisma's Introduction

Adonis JS 6 Prisma Adapter

This package is useful if you want to give a try to PrismaJS within Adonis JS 6.

Getting Started

Installation

 npm install @arthurfranckpat/adonis-prisma

You should use your favorite package manager.

Then configure the package via :

node ace configure @arthurfranckpat/adonis-prisma

This command will scaffold the config files, providers and create a prisma folder with prisma.schema file.

Post Installation

After installation, you should run the proper commands to migrate your schema and/or generate the Prisma Client :

  npx prisma generate

Usage

You have two options to use the Prisma Client. First,via Adonis IoC Container :

const prisma = await app.container.make('prisma:db')

Or by destructuring HttpContextobject :

//route.ts

router
  .get('/', async function ({ prisma }: HttpContext) {
    ...
    const posts = await prisma.post.findMany())
    ...
  })

Authentication

First,you should install the @adonisjs/auth and configure it with Session as Auth Guard. Then, you should replace the provider key in the config/auth.ts file with this:

//config/auth.ts

  import { configProvider } from '@adonisjs/core'
  ... other imports

  ...
      provider: configProvider.create(async () => {
        const { SessionPrismaUserProvider } = await import(
          '@arthurfranckpat/adonis-prisma/prisma_user_provider'
        )
        return new SessionPrismaUserProvider()
      })

After that, you can use the provided methods to facilitate the authentication flow. Like, the @adonisjs/lucid, there is two methods for authentication (NB : these methods are available only with user model :

  • To verify user credentials, you can use this method : const user = await prisma.user.verifyCredentials('email', 'password')

Notes on authentication :

  • First, you should have a user model defined to have access to authentication methods
  • You can configure the auth behavior inside config/prisma.ts : You can modify the uuids and pasword columns names to fit your needs. If you define many uuids columns, you can use the findForAuth method to query the user in the database.
  • The password is automatically hashed via the @adonisjs/hash package when creating or updating an user, based on the default hasher configured inside the config/hash.ts.
  • In config/prisma.ts, you can define whether you want sanitize (remove the hashed from the response returned by prisma). This option is defined via the sanitizePassword key.

Database Seeding

You can define seeders for your DB with the following command :

node ace prisma:make-seeder <name_of_the_seeder>

It will create a seeder file inside the prisma/seeders directory.

Then, to seed the database you should run : node ace prisma:seed command. NB: This command runs all the seeders files inside prisma/seeders directory.

Before leaving...

This package is my first ever package. Feel free to make feedbacks if something needs to be improved.

adonis-prisma's People

Contributors

arthurfranckpat avatar mathieu-r avatar

Stargazers

wailroth avatar  avatar  avatar Gerald Onyango avatar  avatar Mishaa avatar

Watchers

 avatar

Forkers

mathieu-r hcncc

adonis-prisma's Issues

Add Typesafety

image

can you add a type for the prism client inside the prisma context?
i've tried using const prisma = await app.container.make('prisma:db') ioc container, but both have the same result

this is what I was expecting. Thanks
image

Command "prisma:make-provider" is not defined

When I try to run the command

node ace prisma:make-provider init

I get the following message: Command "prisma:make-provider" is not defined

EDIT :
I seems that the correct command is

node ace prisma:make-seeder init

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.