Cara menggunakan JSON-SCHEMA-TO-TYPESCRIPT pada JavaScript

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Section 2

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Section 3

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.


Sidenav overlay
Sidenav push
Sidenav push w/opacity

Open Modal

×

Modal Header

Hello World!

Modals are awesome!

Go Back To W3 How TO Examples


×


London

London is the capital city of England.

Paris

Paris is the capital of France.

Tokyo

Tokyo is the capital of Japan.

Success Info Warning Danger Default

Success Info Warning Danger Default

Resize the browser window to see the effect:

Ever heard about W3Schools Spaces? Here you can create your own website, or save your favorite code snippets.

Kamu juga akan mempelajari gimana cara menggunakan Prisma ORM untuk akses database PostgreSQL. Serta setup Docker untuk serive PosgreSQL.

Mari kita mulai!

Step 1 – Persiapan dan Setup Project

Oke, mari kita suapkan project-nya dari nol.

Membuat Project Baru

Pertama, buat folder baru dengan nama Wrote to /.../microblog/package.json: { "name": "my-blog", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }7.

Buka Termial atau CMD, lalu ketik perintah ini:

mkdir microblog

Setelah itu, masuk ke folder tersebut dengan Wrote to /.../microblog/package.json: { "name": "my-blog", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }8 dan lakukan inisialisasi project Nodejs.

Ketik perintah ini:

cd microblog npm init -y

Perintah ini akan membaut file baru dengan nama Wrote to /.../microblog/package.json: { "name": "my-blog", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }9 yang merupakan file untuk meyimpan data project dan depedency yang dibutuhkan.

Isinya akan saeperti ini:

Wrote to /.../microblog/package.json: { "name": "my-blog", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }

Oke, lanjut!

Biar enak, silahkan buka project ini dengan teks editor.

Bisa pakai teks editor apa aja, tapi saya sarankan pakai VS Code biar gampang.

Install Node Module yang dibutuhkan

Install node module untuk aplikasi:

npm install express @prisma/client @types/http-errors

Install node module untuk development aplikasi:

npm install --save-dev typescript ts-node @types/node @types/express prisma nodemon

Tunggulah sampai prosesnya selesai.

Sambil menunggu, mari kita pelajari fungsi-fungsi tiap modul:

  • npm install express @prisma/client @types/http-errors0 adalah modul untuk framework express;
  • npm install express @prisma/client @types/http-errors1 ini ORM client dari Prisma buat akses database;
  • npm install express @prisma/client @types/http-errors2 modul yang berisi tools untuk bahasa Typecript seprti compiler;
  • npm install express @prisma/client @types/http-errors3 dan npm install express @prisma/client @types/http-errors4 ini runtime untuk menjalankan Typescript tanpa harus compile ke Javasript;
  • npm install express @prisma/client @types/http-errors5 modul ini membaut kita bisa gunakan Typescript di Express;
  • npm install express @prisma/client @types/http-errors6 modul ini membaut HTTP Error;
  • npm install express @prisma/client @types/http-errors7 ORM untuk akses database biar gak ribet nge-query manual;
  • npm install express @prisma/client @types/http-errors8 untuk menjalankan dev server dan auto reload.

Membuat Config untuk Typescript

Berikutnya, buatlah file baru dengan nama npm install express @prisma/client @types/http-errors9 di root direktori project dengan isi seperti ini:

{ "compilerOptions": { "sourceMap": true, "outDir": "dist", "strict": true, "lib": ["esnext"], "esModuleInterop": true } }

Ini adalah konfigurasi yang akan digunakan Typescript untuk compile dan build project.

Ini adalah penampakan project kita saat ini.

Karena saya pakai npm install --save-dev typescript ts-node @types/node @types/express prisma nodemon0, saya jadi punya file npm install --save-dev typescript ts-node @types/node @types/express prisma nodemon1.

Tenang.. file ini sama seperti file npm install --save-dev typescript ts-node @types/node @types/express prisma nodemon2 untuk npm install --save-dev typescript ts-node @types/node @types/express prisma nodemon3.

Oke, lanjut!

Membuat Config untuk Nodemon

Buat file baru dengan nama npm install --save-dev typescript ts-node @types/node @types/express prisma nodemon4 di root direktori project dengan isi seperti ini:

{ "execMap": { "ts": "ts-node" } }

Ini adalah konfigurasi agar npm install express @prisma/client @types/http-errors8 menggunakan npm install express @prisma/client @types/http-errors3 untuk menjalankan server.

Konfigurasi Script di package.json

Terakhir, ubah Wrote to /.../microblog/package.json: { "name": "my-blog", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }9 menjadi seperti ini:

{ "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }

Ini nanti agar kita bisa menjalankan dev server dengan perintah:

npm run dev

Tapi.. untuk saat ini, perintah belum bisa dijalankan.. karena kita belum membuat file npm install --save-dev typescript ts-node @types/node @types/express prisma nodemon8.

Lanjut dulu ke step ke-2.

Step 2 – Setup Docker untuk Service PostgreSQL (Opsional)

Jika kamu sudah menginstall dan setup PostgreSQL di komputermu, maka step ini tidak perlu kamu ikuti.

Silahkan lompat ke step berikutnya.

Tapi jika belum, mari kita setup service PosgreSQL dengan Docker.

O ya, pastikan kamu sudah install Docker juga.

Jika belum, saya sarankan untuk baca:

  • Tutorial Docker: Cara Install Docker dengan Benar

Oke lanjut!

Buatlah file baru di root direktori project dengan nama npm install --save-dev typescript ts-node @types/node @types/express prisma nodemon9 dan isi dengan kode berikut:

version: '3.8' services: postgres: image: postgres:10.3 restart: always environment: - POSTGRES_USER=microblogger - POSTGRES_PASSWORD=sip4lingR4hasia volumes: - postgres:/var/lib/postgresql/data ports: - '5432:5432' volumes: postgres:

Ini adalah konfigurasi untuk menjalankan service PostgreSQL dengan Docker compose.

Perhatikan pada bagian { "compilerOptions": { "sourceMap": true, "outDir": "dist", "strict": true, "lib": ["esnext"], "esModuleInterop": true } }0 dan { "compilerOptions": { "sourceMap": true, "outDir": "dist", "strict": true, "lib": ["esnext"], "esModuleInterop": true } }1, kamu bisa mengganti nilainya dengan yang kamu inginkan.

Tapi ingat, ini nanti akan dipakai untuk menyambungkan aplikasi dengan database PostgreSQL.

Setelah membua npm install --save-dev typescript ts-node @types/node @types/express prisma nodemon9, beriutnya silahkan jalankan service Docker dengan perintah:

cd microblog npm init -y0

Kita memberikan paramter { "compilerOptions": { "sourceMap": true, "outDir": "dist", "strict": true, "lib": ["esnext"], "esModuleInterop": true } }3 supaya service ini dijalankan secara background.

Hasil output di Terminal akan seperti ini:

Terminal Output

cd microblog npm init -y1

Kita juga bisa ngecek prosesnya dengan perintah:

cd microblog npm init -y2

Output:

Output

cd microblog npm init -y3

Ini artinya service PostgreSQL sedang berjalan di port { "compilerOptions": { "sourceMap": true, "outDir": "dist", "strict": true, "lib": ["esnext"], "esModuleInterop": true } }4 dengan nama container { "compilerOptions": { "sourceMap": true, "outDir": "dist", "strict": true, "lib": ["esnext"], "esModuleInterop": true } }5.

Sekarang kita bisa akses PostgreSQL di localhost dengan port { "compilerOptions": { "sourceMap": true, "outDir": "dist", "strict": true, "lib": ["esnext"], "esModuleInterop": true } }4.

Step 3 – Membuat Model Data

Sebelum buat model data, kita harus melakukan inisialiasi Prisma dulu.

Buka terminal, lalu jalankan perintah ini:

cd microblog npm init -y4

Jika berhasil, outputnya akan seperti ini:

Terminal Output

cd microblog npm init -y5

Perintah ini akan membuat folder npm install express @prisma/client @types/http-errors7 dan di dalamnya akan ada file { "compilerOptions": { "sourceMap": true, "outDir": "dist", "strict": true, "lib": ["esnext"], "esModuleInterop": true } }8.

Selain itu, perintah tadi juga akan membuat file { "compilerOptions": { "sourceMap": true, "outDir": "dist", "strict": true, "lib": ["esnext"], "esModuleInterop": true } }9.

Silahkan ubah isi file { "compilerOptions": { "sourceMap": true, "outDir": "dist", "strict": true, "lib": ["esnext"], "esModuleInterop": true } }9 menjadi seperti ini:

microblog/.env

cd microblog npm init -y6

Ini adalah env variabel yang akan dipakai oleh Prisma untuk konek ke service PostgreSQL.

Perihatikan di bagian ini:

cd microblog npm init -y7

Ini 👆 adalah username dan password database yang kita buat tadi di npm install --save-dev typescript ts-node @types/node @types/express prisma nodemon9. Silahkan sesuaikan dengan akun di server postgre-mu.

Lalu perhatikan juga di bagian ini:

cd microblog npm init -y8

Ini 👆 adalah nomor port dan nama data base yang akan dipakai.

Oke lanjut!

Sekarang kita bisa mulai membuat model data.

Silahkan buka file { "execMap": { "ts": "ts-node" } }2, lalu ubah menjadi seperti ini:

cd microblog npm init -y9

Setelah itu, lakukan migrasi untuk membuat tabel baru di PostgreSQL.

Jalankan perintah ini:

Wrote to /.../microblog/package.json: { "name": "my-blog", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }0

Perintah ini akan membuat folder { "execMap": { "ts": "ts-node" } }3 di dalam folder npm install express @prisma/client @types/http-errors7 dan akan berisi { "execMap": { "ts": "ts-node" } }5.

Hasilnya:

Terminal Output

Wrote to /.../microblog/package.json: { "name": "my-blog", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }1

Folder { "execMap": { "ts": "ts-node" } }3 akan menyimpan semua perubahan migrasi database yang kita lakukan.

Jika kamu belum paham konsep migrasi daabase, saya sarankan untuk baca:

  • Tutorial Migrasi Database dengan Phinx
  • Migrasi Database di Codeigniter 4

Unuk mengecek hasil migrasi, kita bisa pakai Beekeeper Studio.

Buat koneksi baru di BeeKeeper Studio, masukan username dan password database PostgreSQL kita.

Lalu klik Connect.

Hasilnya, akan ada tabel baru dengan sekema yang sama seperti yang kita definisikan di Model Data.

Jika ada migrasi atau perubahan data baru, nanti tabelnya akan di-update.

Tinggal jalankan saja perintah migrasi tadi dan tentukan nama migrasi barunya.

Kamu bisa baca di dokumentasi Prisma.

Oke, semua setup sudah siap.

Selanjutnya kita akan mulai coding.

Step 4 – Membuat REST API

Buat folder baru dengan nama { "execMap": { "ts": "ts-node" } }7 kemudian di dalamnya buat file npm install --save-dev typescript ts-node @types/node @types/express prisma nodemon8 dengan isi seperti ini:

microblog/src/index.ts

Wrote to /.../microblog/package.json: { "name": "my-blog", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }2

Sekarang kita bisa jalankan dev server dengan perintah:

npm run dev

Hasilnya:

Server berhasil dijalankan pada { "execMap": { "ts": "ts-node" } }9.

Kalau kita buka di browser, maka hasilnya:

Yap, hasilnya akan error 404.

Ini karena kita belum mendefinisikan route endpoint-nya.

Sekarang folder project kita sudah mejadi seperti ini.

Berikutnya, kita akan buat route endpoint.

Namun..

Sebelum kita menulis route endpoint, kita harus tentukan dulu route apa saja yang akan dibuat.

Ini penting!

Supaya kita tidak bingung nantinya.

Berikut ini adalah route yang akan kita buat di tutorial ini.

EndpointMethodKeterangan{ "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }0{ "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }1List semua post{ "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }2{ "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }3Buat create post baru{ "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }4{ "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }1Buat ambil 1 post berdasarkan id{ "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }4{ "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }7Buat update post{ "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }4{ "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }9Buat hapus postnpm run dev0{ "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }3Buat create user barunpm run dev2{ "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }1Buat ambil user berdasarkan username

Sisanya yang belum ada, nanti kamu bisa buat sendiri ya..

Oke, mari kita buat satu per satu.

Membuat route { "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }0

Tambahkan kode route ini di bawah npm run dev5 pada npm install --save-dev typescript ts-node @types/node @types/express prisma nodemon8.

Wrote to /.../microblog/package.json: { "name": "my-blog", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }4

Pada route ini, kita coba mengakses data pada tabel npm run dev7 dengan Prisma.

Kita menggunakan method npm run dev8 untuk mengambil banyak data.

Jika kita coba akses route ini dari browser, maka kita akan mendapakan array kosong.

Ini karena kita belum punya data untuk ditampilkan.

Karena itu, mari kita lanjut membuat route yang lainnya.

Membuat route { "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }2

Tambahkan route ini di dalam npm install --save-dev typescript ts-node @types/node @types/express prisma nodemon8, tepat di bawah route yang baru saja kita buat.

Wrote to /.../microblog/package.json: { "name": "my-blog", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }5

Kita sudah menambahkan empat route untuk melakukan CRUD (Create, Read, Update, dan Delete) post.

Berikutnya, tambahkan juga route untuk version: '3.8' services: postgres: image: postgres:10.3 restart: always environment: - POSTGRES_USER=microblogger - POSTGRES_PASSWORD=sip4lingR4hasia volumes: - postgres:/var/lib/postgresql/data ports: - '5432:5432' volumes: postgres:1.

Membuat route version: '3.8' services: postgres: image: postgres:10.3 restart: always environment: - POSTGRES_USER=microblogger - POSTGRES_PASSWORD=sip4lingR4hasia volumes: - postgres:/var/lib/postgresql/data ports: - '5432:5432' volumes: postgres:2

Tambahkan route ini di dalam npm install --save-dev typescript ts-node @types/node @types/express prisma nodemon8 tepat di bawah route yang adi kita buat.

Wrote to /.../microblog/package.json: { "name": "my-blog", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }6

Step 5 – Uji Coba REST API

Pertama kita coba tambahakan data user dulu ya.

Karena skenarionya, user harus bikin akun atau daftar dulu baru bisa membuat post.

Percobaan endpoint version: '3.8' services: postgres: image: postgres:10.3 restart: always environment: - POSTGRES_USER=microblogger - POSTGRES_PASSWORD=sip4lingR4hasia volumes: - postgres:/var/lib/postgresql/data ports: - '5432:5432' volumes: postgres:2

Silahkan buka postman, lalu coba lakukan request baru ke endpoint version: '3.8' services: postgres: image: postgres:10.3 restart: always environment: - POSTGRES_USER=microblogger - POSTGRES_PASSWORD=sip4lingR4hasia volumes: - postgres:/var/lib/postgresql/data ports: - '5432:5432' volumes: postgres:2 dengan mehod POST.

Isi datanya seperti ini:

Setelah itu, klik Send untuk mengirim request.

Kemudian, coba cek dari BeeKeeper.

Hasilnya:

Data user baru sudah berhasil dibuat.

Sekarang kita bisa coba akses endpoint npm run dev2.

Karena tadi saya membaut username version: '3.8' services: postgres: image: postgres:10.3 restart: always environment: - POSTGRES_USER=microblogger - POSTGRES_PASSWORD=sip4lingR4hasia volumes: - postgres:/var/lib/postgresql/data ports: - '5432:5432' volumes: postgres:7, maka saya bisa buka version: '3.8' services: postgres: image: postgres:10.3 restart: always environment: - POSTGRES_USER=microblogger - POSTGRES_PASSWORD=sip4lingR4hasia volumes: - postgres:/var/lib/postgresql/data ports: - '5432:5432' volumes: postgres:8.

Hasilnya:

Percobaan endpoint { "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }2

Berikutnya kita akan membuat post berdasarkan email user.

Buat request ke endpoin { "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }2 dengan method { "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }3.

Lalu pada body, isi dengan data JSON seperti ini.

Maka hasilnya di Beekeeper studio:

Data post atau tweet berhasil di tambahkan.

Mantap! 👍

Sekarang jika kita coba buka endpoint { "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }0, maka akan ada 1 post di sana.

Selanjutnya coba untuk melakukan update dan hapus post.

Percobaan update dan hapus post

Kita coba untuk mengubah data dulu.

Silahkan buat request ke cd microblog npm init -y03 di Postman dengan method { "name": "microblog", "version": "1.0.0", "description": "Micro Blogging Platform Backend", "main": "src/index.ts", "scripts": { "dev": "npx nodemon ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Dian", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "@types/http-errors": "^2.0.1", "@types/node": "^18.11.9", "nodemon": "^2.0.20", "prisma": "^4.6.1", "ts-node": "^10.9.1", "typescript": "^4.9.3" }, "dependencies": { "@prisma/client": "^4.6.1", "express": "^4.18.2" } }7 dan isi data baru seperti ini.

Postingan terbaru

LIHAT SEMUA