백엔드 설정
Backend
Database
CRUD
책을 관리하는 엔드포인트를 갖춘 백엔드 API를 만듭니다. 데이터베이스로 생성, 조회, 수정, 삭제 작업을 처리합니다.
도서관 CRUD (에피소드 1)
아카데미로 돌아가기
Database and CRUD Operations
Quick Summary: Build a complete database with CRUD operations (Create, Read, Update, Delete) to manage a book list using Luna Park.
1. Project Architecture
- Create two separate folders:
- Backend: for database and routes
- Frontend: for user interface
In this tutorial, we start with the backend.
2. Creating the Database
Initialize the database
- Create a new database named db-books
- The database contains by default:
- ID: unique identifier
- created_at: automatic creation date
Add custom columns
- Add a title column (type: string)
- Add an author column (type: string)
- Add a few rows with test data
In 15 seconds, your database is ready to use!
3. API Routes - General Concept
Routes are URLs that allow you to:
- Retrieve data (GET)
- Send new data (POST)
- Modify existing data (PUT)
- Delete data (DELETE)
We'll create 4 routes corresponding to CRUD operations.
4. GET Route - Retrieve All Books
Create the get-books route
- Create a new route named get-books
- Set the method: GET
Simple method: dbfind
- Create a dbfind node
- Select the db-books table
- The result returns an array of rows
- Connect the result to the response
- Connect the execution thread between dbfind and the route
- Test with Execute and Ctrl+Space to visualize
Advanced method: dbfrom with query
- Create a dbfrom node
- Select the db-books table
- Add a select to choose fields:
idtitleauthor
- Add a dbexecute
- Test with Ctrl+Space
- Connect the result to the response
Type the response (best practice)
- Define the expected response type:
- Array of objects
- Each object contains:
id,title,author
- Advantage: automatic type checking
- If a field is missing or extra → missingProperty error
5. POST Route - Create a New Book
Create the create-books route
- Create a new route named create-books
- Verify the method is POST
Define the body (incoming data)
In Route Input, add a body containing:
- title (string): book title
- author (string): book author
This data will come from frontend inputs
Insert into database
- Pull a wire from the body
- Create a dbinsert node
- Select the db-books table
- Connect the body to the data field
Return updated state (optional but recommended)
- Add a dbfind after insertion
- Select the db-books table
- Connect the result to the response
- Copy the response type from get-books (right-click → copy)
- Paste into create-books response
6. DELETE Route - Delete a Book
Create the delete-books route
- Create a new route named delete-books
- Change the method to DELETE
- Paste the response type (same as get-books)
Define the ID to delete
In Route Input, add a body containing:
- id (string): identifier of the book to delete
Delete by ID
- Pull a wire from delete-books
- Create a delete by ID node
- Select the db-books table
- Connect the ID:
- Pull a wire from the body
- Create a get property
- Select the id property
- Connect to the ID field of delete by ID
Return updated state
- Add a dbfind after deletion
- Select the db-books table
- Connect the result to the response
7. PUT Route - Update an Existing Book
Create the update-books route
- Create a new route named update-books
- Change the method to PUT
- Paste the response type
Define the body
In Route Input, add a body containing:
- id (string): book to modify
- title (string): new title
- author (string): new author
Update by ID
- Pull a wire from update-books
- Create a DBUpdateByID node
- Select the db-books table
Handle update data
For the ID:
- Create a spread from the body
- Select id
- Connect to the ID field
For the data to modify:
- Create a create object
- Add title and author properties
- Create a spread from the body
- Connect title and author to the object
- Connect the object to the update field
Return updated state
- Add a dbfind after the update
- Select the db-books table
- Connect the result to the response
Summary of Nodes Used
| Node | Usage |
|---|---|
| dbfind | Retrieve all data from a table |
| dbfrom + select | Flexible query with field selection |
| dbexecute | Execute a dbfrom query |
| dbinsert | Insert a new entry |
| delete by ID | Delete a specific entry |
| DBUpdateByID | Modify an existing entry |
| get property | Extract a property from an object |
| spread | Destructure an object |
| create object | Create a new object |
HTTP Methods
- GET: retrieve data
- POST: create new data
- PUT: modify existing data
- DELETE: delete data
Practical Tips
- Always type responses to avoid structure errors
- Return the current state of the database after each modification
- Use Ctrl+Space to visualize results during development
- The ID is the key to precisely identify each element
- The body transmits data from frontend to backend
- Always verify the HTTP method is correct for each route