├── .gitignore ├── prisma ├── migrations │ ├── 20210424163611_init │ │ └── migration.sql │ ├── migration_lock.toml │ ├── 20210424184224_init │ │ └── migration.sql │ ├── 20210424163509_init │ │ └── migration.sql │ ├── 20210424163702_init │ │ └── migration.sql │ ├── 20210424162215_init │ │ └── migration.sql │ ├── 20210424164737_init │ │ └── migration.sql │ └── 20210424150213_init │ │ └── migration.sql └── schema.prisma ├── index.js ├── .env ├── package.json └── routes ├── users.js └── posts.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /prisma/migrations/20210424163611_init/migration.sql: -------------------------------------------------------------------------------- 1 | -- AlterTable 2 | ALTER TABLE `Creator` MODIFY `username` VARCHAR(255) NOT NULL; 3 | -------------------------------------------------------------------------------- /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 = "mysql" -------------------------------------------------------------------------------- /prisma/migrations/20210424184224_init/migration.sql: -------------------------------------------------------------------------------- 1 | -- CreateTable 2 | CREATE TABLE `test` ( 3 | `id` INTEGER NOT NULL AUTO_INCREMENT, 4 | 5 | PRIMARY KEY (`id`) 6 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 7 | -------------------------------------------------------------------------------- /prisma/migrations/20210424163509_init/migration.sql: -------------------------------------------------------------------------------- 1 | -- CreateTable 2 | CREATE TABLE `Creator` ( 3 | `id` INTEGER NOT NULL AUTO_INCREMENT, 4 | `username` VARCHAR(191) NOT NULL, 5 | 6 | PRIMARY KEY (`id`) 7 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const app = express() 3 | 4 | app.use(express.json()) 5 | 6 | app.use('/api/users', require('./routes/users')) 7 | app.use('/api/posts', require('./routes/posts')) 8 | 9 | app.listen(5000, () => { 10 | console.log("Listening on port 5000") 11 | }) 12 | -------------------------------------------------------------------------------- /prisma/migrations/20210424163702_init/migration.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Warnings: 3 | 4 | - A unique constraint covering the columns `[username]` on the table `Creator` will be added. If there are existing duplicate values, this will fail. 5 | 6 | */ 7 | -- CreateIndex 8 | CREATE UNIQUE INDEX `Creator.username_unique` ON `Creator`(`username`); 9 | -------------------------------------------------------------------------------- /.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 and SQLite. 5 | # See the documentation for all the connection string options: https://pris.ly/d/connection-strings 6 | 7 | DATABASE_URL="mysql://root:password@localhost:3306/prisma" -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "prisma-starter", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "devDependencies": { 13 | "prisma": "^2.21.2" 14 | }, 15 | "dependencies": { 16 | "@prisma/client": "^2.21.2", 17 | "express": "^4.17.1" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /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 = "mysql" 6 | url = env("DATABASE_URL") 7 | } 8 | 9 | generator client { 10 | provider = "prisma-client-js" 11 | } 12 | 13 | 14 | model User { 15 | id Int @default(autoincrement()) @id 16 | username String @db.VarChar(255) @unique 17 | posts Post[] 18 | } 19 | 20 | model Post { 21 | id Int @default(autoincrement()) @id 22 | title String 23 | post String 24 | created_at DateTime @default(now()) 25 | updated_at DateTime @updatedAt 26 | user User @relation(fields: [user_id], references: [id]) 27 | user_id Int 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /prisma/migrations/20210424162215_init/migration.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Warnings: 3 | 4 | - You are about to drop the `Post` table. If the table is not empty, all the data it contains will be lost. 5 | - You are about to drop the `Profile` table. If the table is not empty, all the data it contains will be lost. 6 | - You are about to drop the `User` table. If the table is not empty, all the data it contains will be lost. 7 | 8 | */ 9 | -- DropForeignKey 10 | ALTER TABLE `Post` DROP FOREIGN KEY `post_ibfk_1`; 11 | 12 | -- DropForeignKey 13 | ALTER TABLE `Profile` DROP FOREIGN KEY `profile_ibfk_1`; 14 | 15 | -- DropTable 16 | DROP TABLE `Post`; 17 | 18 | -- DropTable 19 | DROP TABLE `Profile`; 20 | 21 | -- DropTable 22 | DROP TABLE `User`; 23 | -------------------------------------------------------------------------------- /routes/users.js: -------------------------------------------------------------------------------- 1 | const router = require('express').Router(); 2 | const { PrismaClient } = require('@prisma/client'); 3 | const { user } = new PrismaClient(); 4 | 5 | router.get('/', async (req, res) => { 6 | let users = await user.findMany({ 7 | select: { 8 | username: true, 9 | posts: true 10 | } 11 | }) 12 | 13 | res.json(users) 14 | }) 15 | 16 | router.post('/', async (req, res) => { 17 | const { username } = req.body; 18 | 19 | const userExists = await user.findUnique({ 20 | where: { 21 | username 22 | }, 23 | select: { 24 | username: true 25 | } 26 | }) 27 | 28 | if(userExists) { 29 | return res.status(400).json({ 30 | msg: "user already exists" 31 | }) 32 | } 33 | 34 | let newUser = await user.create({ 35 | data: { 36 | username 37 | } 38 | }) 39 | 40 | res.json(newUser) 41 | }); 42 | 43 | module.exports = router -------------------------------------------------------------------------------- /prisma/migrations/20210424164737_init/migration.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Warnings: 3 | 4 | - You are about to drop the `Creator` table. If the table is not empty, all the data it contains will be lost. 5 | 6 | */ 7 | -- DropTable 8 | DROP TABLE `Creator`; 9 | 10 | -- CreateTable 11 | CREATE TABLE `User` ( 12 | `id` INTEGER NOT NULL AUTO_INCREMENT, 13 | `username` VARCHAR(255) NOT NULL, 14 | UNIQUE INDEX `User.username_unique`(`username`), 15 | 16 | PRIMARY KEY (`id`) 17 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 18 | 19 | -- CreateTable 20 | CREATE TABLE `Post` ( 21 | `id` INTEGER NOT NULL AUTO_INCREMENT, 22 | `title` VARCHAR(191) NOT NULL, 23 | `post` VARCHAR(191) NOT NULL, 24 | `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), 25 | `updated_at` DATETIME(3) NOT NULL, 26 | `user_id` INTEGER NOT NULL, 27 | 28 | PRIMARY KEY (`id`) 29 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 30 | 31 | -- AddForeignKey 32 | ALTER TABLE `Post` ADD FOREIGN KEY (`user_id`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; 33 | -------------------------------------------------------------------------------- /routes/posts.js: -------------------------------------------------------------------------------- 1 | const router = require('express').Router(); 2 | const { PrismaClient } = require('@prisma/client'); 3 | const { post, user, test } = new PrismaClient(); 4 | 5 | router.get('/:user_id', async (req, res) => { 6 | 7 | const { user_id } = req.params 8 | 9 | let posts = await post.findMany({ 10 | where: { 11 | user_id: parseInt(user_id) 12 | }, select: { 13 | title: true, 14 | created_at: true, 15 | post: true, 16 | user: true, 17 | } 18 | }); 19 | 20 | res.send(posts); 21 | }) 22 | 23 | router.post('/', async (req, res) => { 24 | 25 | const { title, user_id, content } = req.body; 26 | 27 | let userExists = await user.findUnique({ 28 | where: { 29 | id: user_id 30 | } 31 | }); 32 | 33 | if(!userExists) { 34 | return res.status(400).json({ 35 | msg: "user not found" 36 | }) 37 | } 38 | 39 | let newPost = await post.create({ 40 | data: { 41 | title, 42 | user_id, 43 | post: content 44 | } 45 | }); 46 | 47 | res.json(newPost) 48 | 49 | 50 | }); 51 | 52 | module.exports = router -------------------------------------------------------------------------------- /prisma/migrations/20210424150213_init/migration.sql: -------------------------------------------------------------------------------- 1 | -- CreateTable 2 | CREATE TABLE `Post` ( 3 | `id` INTEGER NOT NULL AUTO_INCREMENT, 4 | `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), 5 | `updatedAt` DATETIME(3) NOT NULL, 6 | `title` VARCHAR(255) NOT NULL, 7 | `content` VARCHAR(191), 8 | `published` BOOLEAN NOT NULL DEFAULT false, 9 | `authorId` INTEGER NOT NULL, 10 | 11 | PRIMARY KEY (`id`) 12 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 13 | 14 | -- CreateTable 15 | CREATE TABLE `Profile` ( 16 | `id` INTEGER NOT NULL AUTO_INCREMENT, 17 | `bio` VARCHAR(191), 18 | `userId` INTEGER NOT NULL, 19 | UNIQUE INDEX `Profile.userId_unique`(`userId`), 20 | 21 | PRIMARY KEY (`id`) 22 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 23 | 24 | -- CreateTable 25 | CREATE TABLE `User` ( 26 | `id` INTEGER NOT NULL AUTO_INCREMENT, 27 | `email` VARCHAR(191) NOT NULL, 28 | `name` VARCHAR(191), 29 | UNIQUE INDEX `User.email_unique`(`email`), 30 | 31 | PRIMARY KEY (`id`) 32 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 33 | 34 | -- AddForeignKey 35 | ALTER TABLE `Post` ADD FOREIGN KEY (`authorId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; 36 | 37 | -- AddForeignKey 38 | ALTER TABLE `Profile` ADD FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; 39 | --------------------------------------------------------------------------------