Thao tác CRUD
Thao tác CRUD Khi tạo một bảng trong Enfyra (ví dụ products , orders , customers ), bảng đó tự có các endpoint REST. Dùng chúng để liệt kê, tạo, cập nhật và xóa bản ghi từ ứng dụng của bạn. URL gốc: {appUrl}/api/{routePath} routePath khớp tên bảng hoặc custom route của bạn (ví dụ
Thao tác CRUD
Khi tạo một bảng trong Enfyra (ví dụ products, orders, customers), bảng đó tự có các endpoint REST. Dùng chúng để liệt kê, tạo, cập nhật và xóa bản ghi từ ứng dụng của bạn.
URL gốc: {appUrl}/api/{routePath}
routePath khớp tên bảng hoặc custom route của bạn (ví dụ /products, /orders). Kiểm tra Collections trong Enfyra để biết các route hiện có.
GET /{routePath}
Liệt kê bản ghi; có thể filter, sort và phân trang.
URL: {appUrl}/api/{routePath}
Query parameter: xem Query Parameters.
curl "http://localhost:3000/api/products?limit=10&fields=id,name,price" \
-H "Authorization: Bearer YOUR_TOKEN"
{
"statusCode": 200,
"message": "Success",
"data": [
{ "id": 1, "email": "[email protected]", "name": "User 1" },
{ "id": 2, "email": "[email protected]", "name": "User 2" }
],
"meta": {
"totalCount": 50,
"filterCount": 2
}
}
Lấy một bản ghi
Dynamic REST route không có GET /{routePath}/:id. Hãy dùng list endpoint với filter khóa chính và limit=1.
URL: {appUrl}/api/{routePath}?filter={"id":{"_eq":1}}&limit=1
curl 'http://localhost:3000/api/enfyra_user?filter={"id":{"_eq":1}}&limit=1' \
-H "Authorization: Bearer YOUR_TOKEN"
{
"statusCode": 200,
"message": "Success",
"data": [
{
"id": 1,
"email": "[email protected]",
"name": "User 1",
"role": { "id": 1, "name": "Admin" }
}
]
}
Đọc phần tử đầu tiên trong data. Với MongoDB, dùng _id thay cho id nếu schema của bạn dùng trường này.
POST /{routePath}
Tạo bản ghi mới. Gửi JSON object có các field của bảng.
curl -X POST "http://localhost:3000/api/enfyra_user" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"email":"[email protected]","name":"New User","password":"secret123"}'
{
"statusCode": 200,
"message": "Success",
"data": [
{
"id": 3,
"email": "[email protected]",
"name": "New User",
"createdAt": "2024-01-15T12:00:00.000Z"
}
]
}
Response mặc định trả object kết quả có bản ghi đã tạo trong data.
PATCH /{routePath}/:id
Cập nhật một phần bản ghi hiện có.
curl -X PATCH "http://localhost:3000/api/enfyra_user/3" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"name":"Updated Name"}'
Default update handler trả kết quả dạng collection với bản ghi được cập nhật ở data[0].
DELETE /{routePath}/:id
Xóa bản ghi.
curl -X DELETE "http://localhost:3000/api/enfyra_user/3" \
-H "Authorization: Bearer YOUR_TOKEN"
Default delete handler trả { "message": "Success", "statusCode": 200 }.
Kiểm tra body (POST / PATCH)
Nếu bảng có validateBody = true (mặc định cho bảng mới), server kiểm tra request body theo schema của bảng và column rules gắn với các cột trước khi handler chạy.
Thứ tự kiểm tra:
- Kiểu cột (
int,varchar,boolean, ...) - Cho phép null (
isNullable: falsetừ chốinull) - Giới hạn độ dài của
varchar(options.length) - Column rules (min/max, minLength/maxLength, pattern, format, minItems/maxItems)
- Strictness — từ chối top-level field không rõ
Response lỗi (HTTP 400):
{
"statusCode": 400,
"message": [
"name: String must contain at least 3 character(s)",
"email: Invalid email",
"age: Number must be greater than or equal to 18"
],
"error": "Bad Request"
}
message luôn là mảng chuỗi, mỗi chuỗi ứng với một vi phạm. Phần trước dấu : là tên field; client có thể tách theo : để ánh xạ lỗi về trường của form.
- Cascade create (ví dụ
POST /postkèmcomments: [...]) cũng kiểm tra child record nếu bảng liên quan cóvalidateBody = true. - Dạng connect-by-id (
{ author: 5 }hoặc{ author: { id: 5 } }) bỏ qua nested validation. - Bảng có
validateBody = falsechỉ áp dụng constraint ở database khi insert/update; rules không được dùng. - Field permission
denychocreate/updatetrả 403, không phải 400 — đó không phải lỗi validation.
Để cấu hình rule cho từng cột, xem Column Rules.
Custom route
Bạn có thể thêm custom route (ví dụ /register, /orders/:orderId/items) trong Enfyra Settings. Mỗi route cung cấp các HTTP method đã cấu hình và dùng cùng base URL, cơ chế xác thực như table route.