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

prisma-cli-extension

v1.0.3

Published

## A PrismaDB extension library that add some usefull functionalities.

Downloads

140

Readme

Prisma Cli Extension

A PrismaDB extension library that add some usefull functionalities.

This library was created originally with the purpose of supporting prisma and sqlite on a Electron application. As it was evolving, it became a prisma extension adding extra funcionalities like:

  • Run prisma cli commands inside the application
  • Verify when database migrations are needed and run them programatically
  • Run prisma on packaged apps, like electron.

The solutions on this package where inspired (but not the same) by this Article and Repository from Ayron Wohletz.

Usage

This package is an extension to Prisma, so first you need to do the initial Prisma setup on your project.

npm install prisma

Then, we need to initiate it, with the command:

npx prisma init

This will create a folder called Prisma into your project root, with a schema.prisma file inside.

This file will be where you declare your models.

For more informations about setting up prisma, check the official documentation here

Now for this package, first we need to install it:

npm install prisma-cli-extension

And we can import it like this:

import { PrismaInitializer } from 'prisma-cli-extension'

This is a class that requires two params:

  • The first one is the database connection string, the same one you usually would put on the .env file. You could just passthrough the value from the .env, or you could read it from whenever you want, this is part of what is cool about this package.

  • The secone one is the name of your latest migration. You need to update this every time there's a new migration (Or you could write some code to get this automatically, that's up to you 😊)

import { PrismaInitializer } from 'prisma-cli-extension'

const dbConnection = 'mysql://user:password@host:port/database'
const dbMigration = '2023_mylatestcoolmigration'

const initializer = new PrismaInitializer(dbConnection, dbMigration)

Now that we have initiated our class, we can use our prisma connection like this:

import { PrismaInitializer } from 'prisma-cli-extension'

const dbConnection = 'mysql://user:password@host:port/database'
const dbMigration = '2023_mylatestcoolmigration'

const initializer = new PrismaInitializer(dbConnection, dbMigration)

const prisma = initializer.prisma

And now we have your regular Prisma Client object, with all the properties that you use for querying the database.

We also have a async function called verifyMigration(), that returns a boolean that tells you if you have a pending migration on the current database.

This function needs the current prisma connection as a parameter.

import { PrismaInitializer } from 'prisma-cli-extension'

const dbConnection = 'mysql://user:password@host:port/database'
const dbMigration = '2023_mylatestcoolmigration'

const initializer = new PrismaInitializer(dbConnection, dbMigration)

const prisma = initializer.prisma

initializer.checkMigration(prisma).then(() => {
  //Do something
})

And now we get to running the migration itself, and we have the function runMigration() that does exactly this.

import { PrismaInitializer } from 'prisma-cli-extension'

const dbConnection = 'mysql://user:password@host:port/database'
const dbMigration = '2023_mylatestcoolmigration'

const initializer = new PrismaInitializer(dbConnection, dbMigration)

const prisma = initializer.prisma

initializer.checkMigration(prisma).then((response) => {
  if (response) initializer.runMigration()
})

And just to finalize the features of this package, we can also run any Prisma CLI commands at execution time, using the async function runPrismaCommand(), that takes two params:

  • The command itself, using an string array of the words needed
  • The database connection string

As an example, let's copy how the runMigration() function does it's migration magic using the runPrismaCommand() function:

import { PrismaInitializer } from 'prisma-cli-extension'

const dbConnection = 'mysql://user:password@host:port/database'
const dbMigration = '2023_mylatestcoolmigration'

const initializer = new PrismaInitializer(dbConnection, dbMigration)

const prisma = initializer.prisma

initializer.checkMigration(prisma).then((response) => {
  if (response) initializer.runMigration()
})

initializer
  .runPrismaCommand({
    command: ['migrate', 'deploy', '--schema', 'path to schema file'],
    dbUrl: dbConnection
  })
  .then(() => {
    console.log('Hey, i did a migration by myself!')
  })

This package is still a work in progress, so all feedback is valid!