├── .gitignore ├── prisma ├── migrations │ ├── migration_lock.toml │ └── 20210807223435_initialize │ │ └── migration.sql └── schema.prisma ├── tsconfig.json ├── .env ├── README.md ├── package.json └── index.ts /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /prisma/migrations/migration_lock.toml: -------------------------------------------------------------------------------- 1 | # Please do not edit this file manually 2 | # It should be added in your version-control system (i.e. Git) 3 | provider = "sqlite" -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "sourceMap": true, 4 | "outDir": "dist", 5 | "strict": true, 6 | "lib": ["esnext"], 7 | "esModuleInterop": true 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /.env: -------------------------------------------------------------------------------- 1 | # Environment variables declared in this file are automatically made available to Prisma. 2 | # See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables 3 | 4 | # Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server (Preview) and MongoDB (Preview). 5 | # See the documentation for all the connection string options: https://pris.ly/d/connection-strings 6 | 7 | DATABASE_URL="file:./dev.db" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Prisma JS with SQLite 2 | 3 | This repository is part of the ["Build and Manage your next Node.Js Database using Prisma" freeCodeCamp article](https://www.freecodecamp.org/news/build-nodejs-database-using-prisma-orm/). 4 | 5 | ## Requirements to build the project 6 | 7 | - Node.js (12.2 or higher) 8 | 9 | ## Build and run the project 10 | 11 | Run the database migrations: 12 | 13 | ``` 14 | $ npx prisma migrate dev 15 | ``` 16 | 17 | Run the project: 18 | 19 | ``` 20 | $ npm run dev 21 | ``` 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "minimalistic-twitter", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "dev": "ts-node ./index.ts", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "devDependencies": { 14 | "@types/node": "^16.4.13", 15 | "prisma": "^2.28.0", 16 | "ts-node": "^10.1.0", 17 | "typescript": "^4.3.5" 18 | }, 19 | "dependencies": { 20 | "@prisma/client": "^2.28.0" 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /prisma/migrations/20210807223435_initialize/migration.sql: -------------------------------------------------------------------------------- 1 | -- CreateTable 2 | CREATE TABLE "User" ( 3 | "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 4 | "email" TEXT NOT NULL, 5 | "username" TEXT NOT NULL 6 | ); 7 | 8 | -- CreateTable 9 | CREATE TABLE "Tweet" ( 10 | "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 11 | "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, 12 | "text" TEXT NOT NULL, 13 | "userId" INTEGER NOT NULL, 14 | FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE 15 | ); 16 | 17 | -- CreateIndex 18 | CREATE UNIQUE INDEX "User.email_unique" ON "User"("email"); 19 | -------------------------------------------------------------------------------- /index.ts: -------------------------------------------------------------------------------- 1 | import { PrismaClient } from "@prisma/client"; 2 | 3 | const prisma = new PrismaClient(); 4 | 5 | async function main() { 6 | // We create a new user 7 | const newUser = await prisma.user.create({ 8 | data: { 9 | email: "hello@herewecode.io", 10 | username: "gaelgthomas", // <- it's also my Twitter username 😄 11 | }, 12 | }); 13 | 14 | console.log("New User:"); 15 | console.log(newUser); 16 | 17 | // We create a new tweet and we link it to our new user 18 | const firstTweet = await prisma.tweet.create({ 19 | data: { 20 | text: "Hello world!", 21 | userId: newUser.id, 22 | }, 23 | }); 24 | 25 | console.log("First tweet:"); 26 | console.log(firstTweet); 27 | 28 | // We fetch the new user again (by its unique email address) 29 | // and we ask to fetch its tweets at the same time 30 | const newUserWithTweets = await prisma.user.findUnique({ 31 | where: { 32 | email: "hello@herewecode.io", 33 | }, 34 | include: { tweets: true }, 35 | }); 36 | 37 | console.log("User object with Tweets:"); 38 | console.dir(newUserWithTweets); 39 | } 40 | 41 | main() 42 | .catch((e) => { 43 | throw e; 44 | }) 45 | .finally(async () => { 46 | await prisma.$disconnect(); 47 | }); 48 | -------------------------------------------------------------------------------- /prisma/schema.prisma: -------------------------------------------------------------------------------- 1 | // This is your Prisma schema file, 2 | // learn more about it in the docs: https://pris.ly/d/prisma-schema 3 | 4 | datasource db { 5 | provider = "sqlite" 6 | url = env("DATABASE_URL") 7 | } 8 | 9 | generator client { 10 | provider = "prisma-client-js" 11 | } 12 | 13 | model User { 14 | // We set an `id` variable 15 | // With a `Int` type (number) 16 | // Then, we set the Prisma decorators: 17 | // - @id (because it's an ID) 18 | // - @default(autoincrement()) (default value is auto-incremented) 19 | id Int @id @default(autoincrement()) 20 | 21 | // We set an `email` variable 22 | // With a `String` type 23 | // Then, we set the Prisma decorator: 24 | // - @unique (because we want the user to be unique 25 | // based on the email - two users can't have the same) 26 | email String @unique 27 | 28 | // We set an `username` variable 29 | // With a `String` type 30 | username String 31 | 32 | // We set an `tweets` variable 33 | // With a `Tweet[]` type (one-to-many relationship) 34 | // Because each user can have between 35 | // 0 and an infinite number of tweets 36 | tweets Tweet[] 37 | } 38 | 39 | model Tweet { 40 | // We set an `id` variable 41 | // With an `Int` type (number) 42 | // Then, we set the Prisma decorators: 43 | // - @id (because it's an ID) 44 | // - @default(autoincrement()) (default value is auto-incremented) 45 | id Int @id @default(autoincrement()) 46 | 47 | // Save the tweet creation time 48 | createdAt DateTime @default(now()) 49 | 50 | // We set an `text` variable 51 | // With a `String` type 52 | text String 53 | 54 | // We set an `userId` variable 55 | // With a `Int` type (number) 56 | // It will link the `id` of the `User` model 57 | userId Int 58 | 59 | // We set an `user` variable 60 | // With a `User` type (many-to-one relationship) 61 | // Because each tweet has an author 62 | // This author is an `User` 63 | // We link the `User` to a `Tweet` based on: 64 | // - the `userId` in the `Tweet` model 65 | // - the `id` in the `User` model 66 | user User @relation(fields: [userId], references: [id]) 67 | } 68 | --------------------------------------------------------------------------------