Prisma Notebook

Project Cruise Log

Quick Summary

Generates and migrates Prisma Schema with SQLite type of DB.

  • install Packages: prisma, @prisma/client
  • Initialize prisma
  • Edit .env file with the database type and path
  • Edit some schema, define a data model
  • Generate the schema
  • Migrate the schema

How to

npm install prisma @prisma/client

npx prisma init

Update .env file

DATABASE_URL="file:./dev.db"

Change the prisma/schema.prisma according to SQLite type of DB.

// prisma/schema.prisma file
// ...

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

Define a model

// prisma/schema.prisma file
// ...

model User {
    id      Int         @id @default(autoincrement())
    email   String  @unique
    name    String?
}

Create the schema

npx prisma generate

Then, migrate the schema, makes a SQL schema according to the DB type

npx prisma migrate dev --name <migration_name>

Create a node app that retrieves users from DB

// retrieve.js
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function main() {
  const users = await prisma.user.findMany();
  console.log(users);
}

main()
  .catch(e => {
    throw e
  })
  .finally(async () => {
    await prisma.$disconnect()
  });

Create a node app that inserts some users

const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();

async function main(args) {
  const newUser = await prisma.user.create({
    data: {
      name : _name  || "Bob",
      email: _email || "[email protected]",
    },
  });

  console.log("Created user:", newUser);
}


// Access command line arguments using process.argv
const args = process.argv.slice(2);
// Check if the correct number of arguments are provided
if (args.length !== 4) {
  console.error("Please provide 4 arguments: name, email, title, content");
  process.exit(1);
}
// Destructure the command line arguments
const [_name, _email] = args;


main()
  .catch((e) => {
    throw e;
  })
  .finally(async () => {
    await prisma.$disconnect();
  });