├── .env ├── .gitignore ├── babel.config.json ├── database.sql ├── package-lock.json ├── package.json ├── prisma ├── migrations │ ├── 20230602114552_create_all_model │ │ └── migration.sql │ ├── 20230602114823_add_description_to_sample_table │ │ └── migration.sql │ └── migration_lock.toml └── schema.prisma ├── src └── prisma-client.js └── tests ├── aggregate.test.js ├── auto-increment.test.js ├── count.test.js ├── crud-many.test.js ├── crud.test.js ├── execute-sql.test.js ├── many-to-many.test.js ├── one-to-many.test.js ├── one-to-one.test.js ├── paging.test.js ├── prisma-client.test.js ├── query-sql.test.js ├── select-fields.test.js ├── sorting.test.js ├── tag-function.test.js ├── transaction.test.js └── where.test.js /.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#accessing-environment-variables-from-the-schema 3 | 4 | # Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB. 5 | # See the documentation for all the connection string options: https://pris.ly/d/connection-strings 6 | 7 | #DATABASE_URL="mysql://root:@localhost:3306/belajar_nodejs_database?connection_limit=5" 8 | DATABASE_URL="mysql://root:@localhost:3306/belajar_nodejs_prisma?connection_limit=5" 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | node_modules 3 | -------------------------------------------------------------------------------- /babel.config.json: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["@babel/preset-env"] 3 | } 4 | -------------------------------------------------------------------------------- /database.sql: -------------------------------------------------------------------------------- 1 | create table sample 2 | ( 3 | id varchar(100) not null, 4 | name varchar(100) not null, 5 | primary key (id) 6 | ) engine innodb; 7 | 8 | SELECT * 9 | FROM sample; 10 | 11 | create table customers 12 | ( 13 | id varchar(100) not null, 14 | name varchar(100) not null, 15 | email varchar(100) not null, 16 | phone varchar(100) not null, 17 | primary key (id), 18 | constraint customers_email_unique unique (email), 19 | constraint customers_phone_unique unique (phone) 20 | ) engine innodb; 21 | 22 | select * 23 | from customers; 24 | 25 | select * 26 | from customers 27 | where name = 'Eko'; 28 | 29 | create table products 30 | ( 31 | id varchar(100) not null, 32 | name varchar(100) not null, 33 | price int not null, 34 | stock int not null, 35 | category varchar(100) not null, 36 | primary key (id) 37 | ) engine innodb; 38 | 39 | select * 40 | from products; 41 | 42 | insert into products(id, name, price, stock, category) 43 | value ('P0001', 'A', 1000, 100, 'K1'), 44 | ('P0002', 'B', 2000, 200, 'K1'), 45 | ('P0003', 'C', 3000, 300, 'K1'), 46 | ('P0004', 'D', 4000, 400, 'K1'), 47 | ('P0005', 'E', 5000, 500, 'K1'); 48 | 49 | insert into products(id, name, price, stock, category) 50 | value ('P0006', 'A', 1000, 100, 'K2'), 51 | ('P0007', 'B', 2000, 200, 'K2'), 52 | ('P0008', 'C', 3000, 300, 'K2'), 53 | ('P0009', 'D', 4000, 400, 'K2'), 54 | ('P0010', 'E', 5000, 500, 'K2'); 55 | 56 | SELECT * 57 | from products; 58 | 59 | create table categories 60 | ( 61 | id int not null auto_increment, 62 | name varchar(100) not null, 63 | primary key (id) 64 | ) engine innodb; 65 | 66 | select * 67 | from categories; 68 | 69 | create table wallet 70 | ( 71 | id varchar(100) not null, 72 | balance int not null, 73 | customer_id varchar(100) not null, 74 | primary key (id), 75 | constraint wallet_customer_id_fk foreign key (customer_id) references customers (id), 76 | constraint wallet_customer_id_unique unique (customer_id) 77 | ) engine innodb; 78 | 79 | select * 80 | from wallet; 81 | 82 | select * 83 | from customers; 84 | 85 | create table comments 86 | ( 87 | id int not null auto_increment, 88 | customer_id varchar(100) not null, 89 | title varchar(100) not null, 90 | description text, 91 | primary key (id), 92 | constraint comments_customer_id_fk foreign key (customer_id) references customers (id) 93 | ) engine InnoDB; 94 | 95 | select * 96 | FROm comments; 97 | 98 | insert into comments(customer_id, title, description) 99 | values ('eko', 'Comment 1', 'Sample comment 1'), 100 | ('eko', 'Comment 2', 'Sample comment 2'), 101 | ('budi', 'Comment 1', 'Sample comment 1'), 102 | ('budi', 'Comment 3', 'Sample comment 3'); 103 | 104 | select * 105 | From comments; 106 | 107 | create table likes 108 | ( 109 | customer_id varchar(100) not null, 110 | product_id varchar(100) not null, 111 | primary key (customer_id, product_id), 112 | constraint likes_customer_id_fk foreign key (customer_id) references customers (id), 113 | constraint likes_product_id_fk foreign key (product_id) references products (id) 114 | ) engine innodb; 115 | 116 | SELECT * 117 | FROM likes; 118 | 119 | create table _loves 120 | ( 121 | A varchar(100) not null, 122 | B varchar(100) not null, 123 | primary key (A, B), 124 | constraint customer_loves_fk foreign key (A) references customers (id), 125 | constraint product_loves_fk foreign key (B) references products (id) 126 | ) engine innodb; 127 | 128 | create database belajar_nodejs_prisma; 129 | 130 | use belajar_nodejs_prisma; 131 | 132 | show tables; 133 | 134 | DESC sample; 135 | 136 | SELECT * FROM _prisma_migrations; 137 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "belajar-nodejs-database", 3 | "version": "1.0.0", 4 | "description": "Belajar NodeJS Database", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "jest" 8 | }, 9 | "jest": { 10 | "transform": { 11 | "^.+\\.[t|j]sx?$": "babel-jest" 12 | } 13 | }, 14 | "author": "Eko Kurniawan Khannedy", 15 | "license": "ISC", 16 | "type": "module", 17 | "devDependencies": { 18 | "@babel/preset-env": "^7.22.4", 19 | "@types/jest": "^29.5.2", 20 | "babel-jest": "^29.5.0", 21 | "jest": "^29.5.0", 22 | "prisma": "^4.15.0" 23 | }, 24 | "dependencies": { 25 | "@prisma/client": "^4.15.0" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /prisma/migrations/20230602114552_create_all_model/migration.sql: -------------------------------------------------------------------------------- 1 | -- CreateTable 2 | CREATE TABLE `sample` ( 3 | `id` VARCHAR(100) NOT NULL, 4 | `name` VARCHAR(100) NOT NULL, 5 | 6 | PRIMARY KEY (`id`) 7 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 8 | 9 | -- CreateTable 10 | CREATE TABLE `customers` ( 11 | `id` VARCHAR(100) NOT NULL, 12 | `name` VARCHAR(100) NOT NULL, 13 | `email` VARCHAR(100) NOT NULL, 14 | `phone` VARCHAR(100) NOT NULL, 15 | 16 | UNIQUE INDEX `customers_email_key`(`email`), 17 | UNIQUE INDEX `customers_phone_key`(`phone`), 18 | PRIMARY KEY (`id`) 19 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 20 | 21 | -- CreateTable 22 | CREATE TABLE `comments` ( 23 | `id` INTEGER NOT NULL AUTO_INCREMENT, 24 | `title` VARCHAR(200) NOT NULL, 25 | `description` TEXT NULL, 26 | `customer_id` VARCHAR(100) NOT NULL, 27 | 28 | PRIMARY KEY (`id`) 29 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 30 | 31 | -- CreateTable 32 | CREATE TABLE `wallet` ( 33 | `id` VARCHAR(100) NOT NULL, 34 | `balance` INTEGER NOT NULL, 35 | `customer_id` VARCHAR(100) NOT NULL, 36 | 37 | UNIQUE INDEX `wallet_customer_id_key`(`customer_id`), 38 | PRIMARY KEY (`id`) 39 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 40 | 41 | -- CreateTable 42 | CREATE TABLE `products` ( 43 | `id` VARCHAR(100) NOT NULL, 44 | `name` VARCHAR(100) NOT NULL, 45 | `price` INTEGER NOT NULL, 46 | `stock` SMALLINT NOT NULL, 47 | `category` VARCHAR(100) NOT NULL, 48 | 49 | PRIMARY KEY (`id`) 50 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 51 | 52 | -- CreateTable 53 | CREATE TABLE `categories` ( 54 | `id` INTEGER NOT NULL AUTO_INCREMENT, 55 | `name` VARCHAR(100) NOT NULL, 56 | 57 | PRIMARY KEY (`id`) 58 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 59 | 60 | -- CreateTable 61 | CREATE TABLE `likes` ( 62 | `customer_id` VARCHAR(100) NOT NULL, 63 | `product_id` VARCHAR(100) NOT NULL, 64 | 65 | PRIMARY KEY (`customer_id`, `product_id`) 66 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 67 | 68 | -- CreateTable 69 | CREATE TABLE `_loves` ( 70 | `A` VARCHAR(100) NOT NULL, 71 | `B` VARCHAR(100) NOT NULL, 72 | 73 | UNIQUE INDEX `_loves_AB_unique`(`A`, `B`), 74 | INDEX `_loves_B_index`(`B`) 75 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 76 | 77 | -- AddForeignKey 78 | ALTER TABLE `comments` ADD CONSTRAINT `comments_customer_id_fkey` FOREIGN KEY (`customer_id`) REFERENCES `customers`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; 79 | 80 | -- AddForeignKey 81 | ALTER TABLE `wallet` ADD CONSTRAINT `wallet_customer_id_fkey` FOREIGN KEY (`customer_id`) REFERENCES `customers`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; 82 | 83 | -- AddForeignKey 84 | ALTER TABLE `likes` ADD CONSTRAINT `likes_customer_id_fkey` FOREIGN KEY (`customer_id`) REFERENCES `customers`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; 85 | 86 | -- AddForeignKey 87 | ALTER TABLE `likes` ADD CONSTRAINT `likes_product_id_fkey` FOREIGN KEY (`product_id`) REFERENCES `products`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; 88 | 89 | -- AddForeignKey 90 | ALTER TABLE `_loves` ADD CONSTRAINT `_loves_A_fkey` FOREIGN KEY (`A`) REFERENCES `customers`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; 91 | 92 | -- AddForeignKey 93 | ALTER TABLE `_loves` ADD CONSTRAINT `_loves_B_fkey` FOREIGN KEY (`B`) REFERENCES `products`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; 94 | -------------------------------------------------------------------------------- /prisma/migrations/20230602114823_add_description_to_sample_table/migration.sql: -------------------------------------------------------------------------------- 1 | -- AlterTable 2 | ALTER TABLE `sample` ADD COLUMN `description` TEXT 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/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 | generator client { 5 | provider = "prisma-client-js" 6 | } 7 | 8 | datasource db { 9 | provider = "mysql" 10 | url = env("DATABASE_URL") 11 | } 12 | 13 | model sample { 14 | id String @id @db.VarChar(100) 15 | name String @db.VarChar(100) 16 | description String? @db.Text 17 | } 18 | 19 | model Customer { 20 | id String @id @db.VarChar(100) 21 | name String @db.VarChar(100) 22 | email String @unique @db.VarChar(100) 23 | phone String @unique @db.VarChar(100) 24 | wallet Wallet? 25 | comments Comment[] 26 | likes Like[] 27 | loves Product[] @relation("loves") 28 | 29 | @@map("customers") 30 | } 31 | 32 | model Comment { 33 | id Int @id @default(autoincrement()) 34 | title String @db.VarChar(200) 35 | description String? @db.Text 36 | customer_id String @db.VarChar(100) 37 | customer Customer @relation(fields: [customer_id], references: [id]) 38 | 39 | @@map("comments") 40 | } 41 | 42 | model Wallet { 43 | id String @id @db.VarChar(100) 44 | balance Int @db.Int 45 | customer_id String @unique @db.VarChar(100) 46 | customer Customer @relation(fields: [customer_id], references: [id]) 47 | 48 | @@map("wallet") 49 | } 50 | 51 | model Product { 52 | id String @id @db.VarChar(100) 53 | name String @db.VarChar(100) 54 | price Int @db.Int 55 | stock Int @db.SmallInt 56 | category String @db.VarChar(100) 57 | likedBy Like[] 58 | lovedBy Customer[] @relation("loves") 59 | 60 | @@map("products") 61 | } 62 | 63 | model Category { 64 | id Int @id @default(autoincrement()) 65 | name String @db.VarChar(100) 66 | 67 | @@map("categories") 68 | } 69 | 70 | model Like { 71 | customer_id String @db.VarChar(100) 72 | product_id String @db.VarChar(100) 73 | customer Customer @relation(fields: [customer_id], references: [id]) 74 | product Product @relation(fields: [product_id], references: [id]) 75 | 76 | @@id([customer_id, product_id]) 77 | @@map("likes") 78 | } 79 | -------------------------------------------------------------------------------- /src/prisma-client.js: -------------------------------------------------------------------------------- 1 | import {PrismaClient} from "@prisma/client"; 2 | 3 | export const prismaClient = new PrismaClient({ 4 | errorFormat: "pretty", 5 | log: ["info", "warn", "error", "query"] 6 | }); 7 | -------------------------------------------------------------------------------- /tests/aggregate.test.js: -------------------------------------------------------------------------------- 1 | import {prismaClient} from "../src/prisma-client.js"; 2 | 3 | describe('Prisma Client', () => { 4 | it('should can do aggregate function', async () => { 5 | const result = await prismaClient.product.aggregate({ 6 | _min : { 7 | price: true, 8 | stock: true 9 | }, 10 | _max : { 11 | price: true, 12 | stock: true 13 | }, 14 | _avg : { 15 | price: true, 16 | stock: true 17 | }, 18 | }); 19 | 20 | console.info(result); 21 | }); 22 | 23 | it('should can do aggregate function with group by', async () => { 24 | const result = await prismaClient.product.groupBy({ 25 | by: ["category"], 26 | _min : { 27 | price: true, 28 | stock: true 29 | }, 30 | _max : { 31 | price: true, 32 | stock: true 33 | }, 34 | _avg : { 35 | price: true, 36 | stock: true 37 | }, 38 | }); 39 | 40 | console.info(result); 41 | }); 42 | 43 | it('should can do aggregate function with group by and having', async () => { 44 | const result = await prismaClient.product.groupBy({ 45 | by: ["category"], 46 | _min : { 47 | price: true, 48 | stock: true 49 | }, 50 | _max : { 51 | price: true, 52 | stock: true 53 | }, 54 | _avg : { 55 | price: true, 56 | stock: true 57 | }, 58 | having: { 59 | price: { 60 | _avg: { 61 | gt: 2000 62 | } 63 | } 64 | } 65 | }); 66 | 67 | console.info(result); 68 | }); 69 | }); 70 | -------------------------------------------------------------------------------- /tests/auto-increment.test.js: -------------------------------------------------------------------------------- 1 | import {prismaClient} from "../src/prisma-client.js"; 2 | 3 | describe("Prisma Client", () => { 4 | it('should be able to create with auto increment primary key', async () => { 5 | const category = await prismaClient.category.create({ 6 | data: { 7 | name: "Food" 8 | } 9 | }); 10 | 11 | console.info(category); 12 | expect(category).toHaveProperty("id"); 13 | }); 14 | }) 15 | -------------------------------------------------------------------------------- /tests/count.test.js: -------------------------------------------------------------------------------- 1 | import {prismaClient} from "../src/prisma-client.js"; 2 | 3 | describe("Prisma Client", () => { 4 | it('should can count', async ()=> { 5 | const total = await prismaClient.customer.count({ 6 | where: { 7 | name : "Eko" 8 | } 9 | }); 10 | 11 | expect(total).toBe(3); 12 | }); 13 | }) 14 | -------------------------------------------------------------------------------- /tests/crud-many.test.js: -------------------------------------------------------------------------------- 1 | import {prismaClient} from "../src/prisma-client.js"; 2 | 3 | describe("Prisma Client", () => { 4 | it('should can create many records', async () => { 5 | const {count} = await prismaClient.customer.createMany({ 6 | data: [ 7 | { 8 | id: "joko", 9 | email: "joko@pzn.com", 10 | phone: "34534534534545", 11 | name: "Joko" 12 | }, 13 | { 14 | id: "budi", 15 | email: "budi@pzn.com", 16 | phone: "34534545", 17 | name: "Budi" 18 | } 19 | ] 20 | }); 21 | 22 | expect(count).toBe(2); 23 | }); 24 | 25 | it('should can update many records', async () => { 26 | const {count} = await prismaClient.customer.updateMany({ 27 | data: { 28 | email: "budilagi@pzn.com" 29 | }, 30 | where: { 31 | name: "Budi" 32 | } 33 | }); 34 | 35 | expect(count).toBe(1); 36 | }); 37 | 38 | it('should can delete many records', async ()=> { 39 | const {count} = await prismaClient.customer.deleteMany({ 40 | where: { 41 | name: "Tidak ada" 42 | } 43 | }); 44 | 45 | expect(count).toBe(0); 46 | }); 47 | 48 | it('should can read many records', async () => { 49 | const customers = await prismaClient.customer.findMany({}); 50 | console.info(customers); 51 | expect(customers.length).toBe(8); 52 | }); 53 | }) 54 | -------------------------------------------------------------------------------- /tests/crud.test.js: -------------------------------------------------------------------------------- 1 | import {prismaClient} from "../src/prisma-client.js"; 2 | 3 | describe("Prisma Client", () => { 4 | it('should be able to create customer', async () => { 5 | const customer = await prismaClient.customer.create({ 6 | data: { 7 | id: "khannedy", 8 | email: "khannedy@pzn.com", 9 | name: "Eko Khannedy", 10 | phone: "0821241243124" 11 | } 12 | }); 13 | 14 | expect(customer.id).toBe("khannedy"); 15 | expect(customer.email).toBe("khannedy@pzn.com"); 16 | expect(customer.name).toBe("Eko Khannedy"); 17 | expect(customer.phone).toBe("0821241243124"); 18 | }); 19 | 20 | it('should be able to update customer', async () => { 21 | const customer = await prismaClient.customer.update({ 22 | data: { 23 | name: "Eko Kurniawan Khannedy" 24 | }, 25 | where: { 26 | id: "khannedy" 27 | } 28 | }); 29 | 30 | expect(customer.id).toBe("khannedy"); 31 | expect(customer.email).toBe("khannedy@pzn.com"); 32 | expect(customer.name).toBe("Eko Kurniawan Khannedy"); 33 | expect(customer.phone).toBe("0821241243124"); 34 | }); 35 | 36 | it('should be able to read customer', async () => { 37 | const customer = await prismaClient.customer.findUnique({ 38 | where: { 39 | id: "khannedy" 40 | } 41 | }); 42 | 43 | expect(customer.id).toBe("khannedy"); 44 | expect(customer.email).toBe("khannedy@pzn.com"); 45 | expect(customer.name).toBe("Eko Kurniawan Khannedy"); 46 | expect(customer.phone).toBe("0821241243124"); 47 | }); 48 | 49 | it('should be able to delete customer', async () => { 50 | const customer = await prismaClient.customer.delete({ 51 | where: { 52 | id: "khannedy" 53 | } 54 | }); 55 | 56 | expect(customer.id).toBe("khannedy"); 57 | expect(customer.email).toBe("khannedy@pzn.com"); 58 | expect(customer.name).toBe("Eko Kurniawan Khannedy"); 59 | expect(customer.phone).toBe("0821241243124"); 60 | }); 61 | }) 62 | -------------------------------------------------------------------------------- /tests/execute-sql.test.js: -------------------------------------------------------------------------------- 1 | import {prismaClient} from "../src/prisma-client.js"; 2 | 3 | describe("Prisma Client", () => { 4 | it('should be able to execute sql', async () => { 5 | const id = "1"; 6 | const name = "Eko Kurniawan"; 7 | 8 | const impacted = await prismaClient.$executeRaw`INSERT INTO sample(id, name) VALUES (${id}, ${name});`; 9 | expect(impacted).toBe(1); 10 | }); 11 | }); 12 | -------------------------------------------------------------------------------- /tests/many-to-many.test.js: -------------------------------------------------------------------------------- 1 | import {prismaClient} from "../src/prisma-client.js"; 2 | 3 | describe('Prisma Client', function () { 4 | it('should can insert many to many relation', async ()=> { 5 | const like = await prismaClient.like.create({ 6 | data: { 7 | customer_id: "joko", 8 | product_id: "P0001" 9 | }, 10 | include: { 11 | customer: true, 12 | product: true 13 | } 14 | }); 15 | 16 | console.info(like); 17 | }); 18 | 19 | it('should can find one with many to many relation', async ()=> { 20 | const customer = await prismaClient.customer.findUnique({ 21 | where: { 22 | id: "eko" 23 | }, 24 | include: { 25 | likes : { 26 | include: { 27 | product: true 28 | } 29 | } 30 | } 31 | }); 32 | 33 | console.info(JSON.stringify(customer)); 34 | }); 35 | 36 | it('should can find many with many to many relation', async ()=> { 37 | const customers = await prismaClient.customer.findMany({ 38 | where: { 39 | likes: { 40 | some: { 41 | product: { 42 | name: { 43 | contains: "A" 44 | } 45 | } 46 | } 47 | } 48 | }, 49 | include: { 50 | likes : { 51 | include: { 52 | product: true 53 | } 54 | } 55 | } 56 | }); 57 | 58 | console.info(JSON.stringify(customers)); 59 | }); 60 | 61 | it('should create implicit relation', async ()=> { 62 | const customer = await prismaClient.customer.update({ 63 | where: { 64 | id : "eko" 65 | }, 66 | data: { 67 | loves: { 68 | connect: [ 69 | { 70 | id: "P0001" 71 | }, 72 | { 73 | id: "P0002" 74 | } 75 | ] 76 | } 77 | }, 78 | include: { 79 | loves: true 80 | } 81 | }) 82 | 83 | console.info(customer); 84 | }); 85 | 86 | it('should find many implicit relation', async ()=> { 87 | const customers = await prismaClient.customer.findMany({ 88 | where: { 89 | loves: { 90 | some: { 91 | name: { 92 | contains: "A" 93 | } 94 | } 95 | } 96 | }, 97 | include: { 98 | loves: true 99 | } 100 | }); 101 | 102 | console.info(customers); 103 | }); 104 | }); 105 | -------------------------------------------------------------------------------- /tests/one-to-many.test.js: -------------------------------------------------------------------------------- 1 | import {prismaClient} from "../src/prisma-client.js"; 2 | 3 | describe('Prisma Client', function () { 4 | it('should can insert and include', async () => { 5 | const comment = await prismaClient.comment.create({ 6 | data: { 7 | customer_id: "eko", 8 | title: "Insert Comment", 9 | description: "Description Comment" 10 | }, 11 | include: { 12 | customer: true 13 | } 14 | }); 15 | 16 | console.info(comment); 17 | }); 18 | 19 | it('should can insert and many relation', async () => { 20 | const customer = await prismaClient.customer.create({ 21 | data: { 22 | id: "alex", 23 | name: "Alex", 24 | email: "alex@pzn.com", 25 | phone: "9898989898", 26 | comments: { 27 | createMany: { 28 | data: [ 29 | { 30 | title: "Comment 1", 31 | description: "Description 1" 32 | }, 33 | { 34 | title: "Comment 2", 35 | description: "Description 2" 36 | } 37 | ] 38 | } 39 | } 40 | }, 41 | include: { 42 | comments: true 43 | } 44 | }); 45 | 46 | console.info(customer); 47 | }); 48 | 49 | it('should can find many with filter relation', async () => { 50 | const customers = await prismaClient.customer.findMany({ 51 | where: { 52 | comments: { 53 | some: { 54 | title: { 55 | contains: "Comment" 56 | } 57 | } 58 | } 59 | }, 60 | include: { 61 | comments: true 62 | } 63 | }); 64 | 65 | console.info(JSON.stringify(customers)); 66 | }); 67 | }); 68 | -------------------------------------------------------------------------------- /tests/one-to-one.test.js: -------------------------------------------------------------------------------- 1 | import {prismaClient} from "../src/prisma-client.js"; 2 | 3 | describe('Prisma Client', function () { 4 | it('should can create one to one relation', async () => { 5 | const wallet = await prismaClient.wallet.create({ 6 | data: { 7 | id: "eko", 8 | customer_id: "eko", 9 | balance: 1000000 10 | }, 11 | include: { 12 | customer: true 13 | } 14 | }); 15 | 16 | console.info(wallet); 17 | }); 18 | 19 | it('should can create one to one with relation', async () => { 20 | const customer = await prismaClient.customer.create({ 21 | data: { 22 | id: "joko2", 23 | name: "Joko 2", 24 | email: "joko2@pzn.com", 25 | phone: "546565656", 26 | wallet: { 27 | create: { 28 | id: "joko2", 29 | balance: 500000 30 | } 31 | } 32 | }, 33 | include: { 34 | wallet: true 35 | } 36 | }); 37 | 38 | console.info(customer); 39 | 40 | }); 41 | 42 | it('should can find one to one with relation', async () => { 43 | const customer = await prismaClient.customer.findUnique({ 44 | where: { 45 | id: "eko" 46 | }, 47 | include: { 48 | wallet: true 49 | } 50 | }); 51 | 52 | console.info(customer); 53 | }); 54 | 55 | it('should can find one to one with relation filter', async () => { 56 | const customers = await prismaClient.customer.findMany({ 57 | where: { 58 | wallet: { 59 | isNot: null 60 | } 61 | }, 62 | include: { 63 | wallet: true 64 | } 65 | }); 66 | 67 | console.info(customers); 68 | }); 69 | }); 70 | -------------------------------------------------------------------------------- /tests/paging.test.js: -------------------------------------------------------------------------------- 1 | import {prismaClient} from "../src/prisma-client.js"; 2 | 3 | describe("Prisma Client", () => { 4 | it('should can do paging', async () => { 5 | const page1 = await prismaClient.customer.findMany({ 6 | skip: 0, 7 | take: 1 8 | }); 9 | expect(page1.length).toBe(1); 10 | 11 | const page2 = await prismaClient.customer.findMany({ 12 | skip: 1, 13 | take: 1 14 | }); 15 | expect(page2.length).toBe(1); 16 | 17 | const page3 = await prismaClient.customer.findMany({ 18 | skip: 2, 19 | take: 1 20 | }); 21 | expect(page3.length).toBe(1); 22 | }); 23 | }) 24 | -------------------------------------------------------------------------------- /tests/prisma-client.test.js: -------------------------------------------------------------------------------- 1 | import {PrismaClient} from "@prisma/client"; 2 | 3 | describe("Prisma Client", () => { 4 | it('should be able to connect to database', async () => { 5 | const prisma = new PrismaClient(); 6 | await prisma.$connect(); 7 | 8 | // do something 9 | 10 | await prisma.$disconnect(); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /tests/query-sql.test.js: -------------------------------------------------------------------------------- 1 | import {prismaClient} from "../src/prisma-client.js"; 2 | 3 | describe("Prisma Client", () => { 4 | it('should be able to query sql', async () => { 5 | const id = "1"; 6 | 7 | const samples = await prismaClient.$queryRaw`SELECT * FROM sample WHERE id = ${id}`; 8 | 9 | for (const sample of samples) { 10 | console.info(`Result sample id : ${sample.id} and name ${sample.name}`); 11 | } 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /tests/select-fields.test.js: -------------------------------------------------------------------------------- 1 | import {prismaClient} from "../src/prisma-client.js"; 2 | 3 | describe("Prisma Client", () => { 4 | it('should can create and select fields', async ()=> { 5 | const customer = await prismaClient.customer.create({ 6 | data: { 7 | id: "rully", 8 | email: "rully@pzn.com", 9 | phone: "345325353451434", 10 | name: "Rully Nugraha" 11 | }, 12 | select:{ 13 | id: true, 14 | name: true 15 | } 16 | }); 17 | 18 | expect(customer.id).toBe("rully"); 19 | expect(customer.name).toBe("Rully Nugraha"); 20 | expect(customer.email).toBeUndefined(); 21 | expect(customer.phone).toBeUndefined(); 22 | }); 23 | 24 | it('should can select fields', async ()=> { 25 | const customers = await prismaClient.customer.findMany({ 26 | select: { 27 | id: true, 28 | name: true 29 | } 30 | }); 31 | 32 | for (let customer of customers) { 33 | expect(customer.id).toBeDefined(); 34 | expect(customer.name).toBeDefined(); 35 | expect(customer.email).toBeUndefined(); 36 | expect(customer.phone).toBeUndefined(); 37 | } 38 | }); 39 | }) 40 | -------------------------------------------------------------------------------- /tests/sorting.test.js: -------------------------------------------------------------------------------- 1 | import {prismaClient} from "../src/prisma-client.js"; 2 | 3 | describe("Prisma Client", () => { 4 | it('should can do sorting', async () => { 5 | const customers = await prismaClient.customer.findMany({ 6 | skip: 0, 7 | take: 10, 8 | orderBy: [ 9 | { 10 | name: "desc" 11 | }, 12 | { 13 | email: "asc" 14 | } 15 | ] 16 | }); 17 | 18 | console.info(customers); 19 | }); 20 | }) 21 | -------------------------------------------------------------------------------- /tests/tag-function.test.js: -------------------------------------------------------------------------------- 1 | function tagFunction(array, ...args){ 2 | console.info(array); 3 | console.info(args); 4 | } 5 | 6 | test("tag function", () => { 7 | const name = "Eko"; 8 | const lastName = "Khannedy"; 9 | 10 | tagFunction`Hello ${name} ${lastName}!, How are you?`; 11 | tagFunction`Bye ${name} ${lastName}!, see you later`; 12 | }); 13 | 14 | test("tag function sql", () => { 15 | const name = "Eko'; DROP table users;"; 16 | const age = 30; 17 | 18 | tagFunction`SELECT * FROM users WHERE name = ${name} AND age = ${age}`; 19 | }) 20 | -------------------------------------------------------------------------------- /tests/transaction.test.js: -------------------------------------------------------------------------------- 1 | import {prismaClient} from "../src/prisma-client.js"; 2 | 3 | describe("Prisma Client", () => { 4 | it('should can execute sequential transaction', async () => { 5 | const [eko, kurniawan] = await prismaClient.$transaction([ 6 | prismaClient.customer.create({ 7 | data: { 8 | id: "eko", 9 | email: "eko@pzn.com", 10 | name: "Eko", 11 | phone: "25334534534543" 12 | } 13 | }), 14 | prismaClient.customer.create({ 15 | data: { 16 | id: "kurniawan", 17 | email: "kurniawan@pzn.com", 18 | name: "Kurniawan", 19 | phone: "3453453543" 20 | } 21 | }) 22 | ], { 23 | timeout: 5 24 | }) 25 | 26 | expect(eko.name).toBe("Eko"); 27 | expect(kurniawan.name).toBe("Kurniawan"); 28 | }); 29 | 30 | it('should can execute interactive transaction', async () => { 31 | const [eko, kurniawan] = await prismaClient.$transaction(async (prisma) => { 32 | const eko = await prisma.customer.create({ 33 | data: { 34 | id: "eko2", 35 | email: "eko2@pzn.com", 36 | name: "Eko", 37 | phone: "56456464565" 38 | } 39 | }) 40 | const kurniawan = await prisma.customer.create({ 41 | data: { 42 | id: "kurniawan2", 43 | email: "kurniawan2@pzn.com", 44 | name: "Kurniawan", 45 | phone: "4356345345" 46 | } 47 | }) 48 | 49 | return [eko, kurniawan]; 50 | }, { 51 | timeout: 5 52 | }); 53 | 54 | expect(eko.name).toBe("Eko"); 55 | expect(kurniawan.name).toBe("Kurniawan"); 56 | }); 57 | }) 58 | -------------------------------------------------------------------------------- /tests/where.test.js: -------------------------------------------------------------------------------- 1 | import {prismaClient} from "../src/prisma-client.js"; 2 | 3 | describe('Prisma Client', function () { 4 | it('should can using or operator', async ()=> { 5 | const products = await prismaClient.product.findMany({ 6 | where: { 7 | OR : [ 8 | { 9 | name: "A" 10 | }, 11 | { 12 | name: "B" 13 | } 14 | ] 15 | }, 16 | orderBy: [ 17 | { 18 | name: "asc" 19 | } 20 | ] 21 | }); 22 | 23 | console.info(products); 24 | 25 | expect(products.length).toBe(4); 26 | expect(products[0].name).toBe("A"); 27 | expect(products[1].name).toBe("A"); 28 | expect(products[2].name).toBe("B"); 29 | expect(products[3].name).toBe("B"); 30 | }); 31 | }); 32 | --------------------------------------------------------------------------------