Skip to content

Latest commit

 

History

History
105 lines (80 loc) · 2.93 KB

SETUP.md

File metadata and controls

105 lines (80 loc) · 2.93 KB

Setup

Installation

You will need:

To install node:

  • under fedora: sudo dnf install nodejs.
  • under ubuntu: sudo apt install nodejs npm.

Then sudo npm install -g npx.

To start using prisma, just run the following command:

curl https://codeload.github.com/prisma/quickstart/tar.gz/master | tar -xz --strip=2 quickstart-master/javascript/starter

This will download a small project provided by Prisma developers to help you understand the basics. Just enter your newly created starter folder and run:

npm install

There are 5 important files in the stater folder:

  • package.json: lists the npm dependencies.
  • prisma/schema.prisma: the Prisma schema file that defines the database models.
  • prisma/.env: Sets the database connection with its URL as environment variable.
  • prisma/dev.db: SQLite database file.
  • script.js: File in which you will code your functions.

You can also display the database via a web interface:

npx prisma studio

💡 If you have an error while running this command, try to update the prisma packages:

npm add -D [email protected] && npm add @prisma/[email protected]

Prisma in javascript code

We will see line by line what is in the script.js file:

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

Node will look for the @prisma/client dependency in the node_modules folder (created with npm install).


const prisma = new PrismaClient();

This line creates a new instance of a prisma client.


async function main() {
  // ... you will write your Prisma Client queries here
}

Here we create an asynchronous function. We will code inside later.

if you need more information on how async functions work, read this


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

Finally, this part of the code calls our main function, displays a message if an error has occurred and disconnects the prisma client once all actions have been completed.

Currently, if you do npm run dev, nothing should happen. We will prepare the basics for the next exercises, so add the following line to your main function:

console.log("getUsers:\n", await getUsers())

and add above your main function:

function getUsers() {
  return prisma.user.findMany();
}

If you run npm run dev again, you should have this output:

getUsers:
 [ { id: 1, email: '[email protected]', name: 'Sarah' },
  { id: 2, email: '[email protected]', name: 'Maria' } ]

If you have finished all these steps, you can now move on to the exercises.

Go back to the exercises