DINHDANH Creator · Bài EM03 · Easy · 15 method kho dữ liệu

Kho dữ liệu JSON
đủ method - tham số · trả về · lưu ý

Một sổ cho cả loại dữ liệu. readFile trả chuỗi → phải JSON.parse (EM02). Easy không có login admin - mọi gọi cần chìa khóa phiên trang.

Quy tắc vàng: Không có appendData/readList. Thêm dòng = đọc sổ → sửa object → writeFile cả sổ. Field: value đã decode - thiếu file → success: false (khác Aura).

A. Đọc / ghi sổ

appifio_readFile(fileName) · Cần chìa khóa phiên trang

Việc: Đọc cả file trong kho dữ liệu.

Tham số: fileName - vd. blog.json, settings.json.

const r = await appifio_client.appifio_readFile('blog.json');
// r.data.content = chuỗi thô | ""
// r.data.exists = true | false

Trả về: { success, data: { content: string, exists: boolean, … } }

Lưu ý: Chưa có file → success: true, exists: false, content: "" (không 404). JSON → bắt buộc JSON.parse trong try/catch. Khác Aura: Aura đã parse sẵn.

appifio_writeFile(fileName, content)

Việc: Ghi đè hoặc tạo mới.

Tham số: content = chuỗi (thường JSON.stringify(obj, null, 2)).

await appifio_client.appifio_writeFile('blog.json', JSON.stringify(data, null, 2));

Trả về: { success: true } hoặc lỗi kèm message.

appifio_createFile(fileName, content?)

Việc: Tạo file mới. content mặc định ''.

await appifio_client.appifio_createFile('blog.json', '{"blogs":{},"metadata":{"total":0}}');

Trả về: { success: true } - lỗi nếu đã tồn tại (khác writeFile ghi đè).

B. Xóa · khôi phục · đổi tên · sao chép

appifio_deleteFile(fileName)
await appifio_client.appifio_deleteFile('old.json');

Trả về: { success: true } - xóa mềm (có thể khôi phục).

appifio_restoreFile(fileName)
await appifio_client.appifio_restoreFile('old.json');

Trả về: { success: true } - lấy lại file đã xóa mềm.

appifio_hardDeleteFile(fileName)
await appifio_client.appifio_hardDeleteFile('spam.json');

Trả về: { success: true }. Lưu ý: Xóa vĩnh viễn - không hoàn tác. (Aura thường siết quyền siêu quản trị; Easy chỉ cần chìa khóa phiên trang.)

appifio_renameFile(oldName, newName)

Tham số: Đuôi file nên giống nhau (vd. cả hai .json).

await appifio_client.appifio_renameFile('a.json', 'b.json');

Trả về: { success: true }

appifio_copyFile(sourceFile, destFile)
await appifio_client.appifio_copyFile('blog.json', 'blog-backup.json');

Trả về: { success: true }

C. Kiểm tra · liệt kê · tìm

appifio_fileExists(fileName)
const r = await appifio_client.appifio_fileExists('blog.json');
// r.success === true; r.data.exists === true|false

Trả về: luôn có thể success: true kèm exists - chưa có file không phải lỗi mạng.

appifio_getFileInfo(fileName)
const r = await appifio_client.appifio_getFileInfo('blog.json');
// thành công: metadata (tên, size, thời gian…)
// chưa có file → success: false
appifio_listFiles(includeDeleted?)

Tham số: includeDeleted - mặc định false.

const r = await appifio_client.appifio_listFiles();
const r2 = await appifio_client.appifio_listFiles(true); // gồm đã xóa mềm

Trả về: danh sách file trong kho dữ liệu (xem data / mảng files tùy phiên bản trả về - luôn kiểm success).

appifio_searchFiles(pattern)

Tham số: wildcard * và ? trên tên file.

const r = await appifio_client.appifio_searchFiles('blog-*.json');
appifio_findByName(fileName)

Việc: Tìm theo chuỗi tên (kiểu “chứa”, LIKE).

const r = await appifio_client.appifio_findByName('blog.json');

Lưu ý Easy: Client mặc định chỉ tìm trong kho dữ liệu - không quét file trang HTML. Không nhầm với “exact name only” của Aura.

D. Field (dot-path)

appifio_readField(fileName, fieldPath)

Tham số: fieldPath - vd. theme.color, user.profile.name.

const r = await appifio_client.appifio_readField('settings.json', 'theme.color');
if (r.success) {
  const color = r.data.value; // đã decode - KHÔNG JSON.parse
} else {
  // file hoặc field chưa có
}

Trả về: { success, data: { value } }. Thiếu file/field → success: false. Aura: thường success: true, value: null.

appifio_writeField(fileName, fieldPath, value)

Việc: Ghi một ô; có thể tạo file + các tầng path nếu chưa có.

await appifio_client.appifio_writeField('settings.json', 'theme.color', '#ff0000');

Trả về: { success: true }. value có thể là string/number/object - client gửi đúng kiểu.

Checklist EM03 (15/15): createFile · readFile · writeFile · deleteFile · restoreFile · hardDeleteFile · fileExists · getFileInfo · renameFile · copyFile · listFiles · searchFiles · findByName · readField · writeField

EM05 - File trang & upload (đủ method)

DINHDANH Creator · Loạt Easy Storage · EM03