Bỏ qua, đến nội dung

Tham chiếu API — MySQL Data Source

Các thao tác CRUD được hỗ trợ

Thao tácPhương thứcMô tả
listGETLấy danh sách bản ghi có phân trang, filter, sort
getGETLấy một bản ghi theo Primary Key
createPOSTTạo bản ghi mới
updatePUTCập nhật bản ghi
destroyDELETEXóa bản ghi

Tạo Data Source qua API

typescript
await agent.resource('dataSources').create({
  values: {
    key: 'external-mysql',
    type: 'mysql',
    displayName: 'MySQL Production',
    options: {
      host: '192.168.1.100',
      port: 3306,
      database: 'my_erp',
      username: 'app_user',
      password: '***',
    },
  },
});

Kiểm tra kết nối qua API

typescript
await agent.resource('dataSources').testConnection({
  values: {
    type: 'mysql',
    options: {
      host: '192.168.1.100',
      port: 3306,
      database: 'my_erp',
      username: 'app_user',
      password: '***',
    },
  },
});

Introspect lại Schema

typescript
await agent.resource('dataSources').refresh({
  filterByTk: 'external-mysql',
});

Filter Operators

Hỗ trợ đầy đủ filter operators trên dữ liệu MySQL:

OperatorMô tảVí dụ
$eqBằng{ name: { $eq: 'value' } }
$neKhác{ status: { $ne: 'deleted' } }
$gt / $gteLớn hơn / Lớn hơn bằng{ age: { $gt: 18 } }
$lt / $lteNhỏ hơn / Nhỏ hơn bằng{ price: { $lt: 1000 } }
$likeChứa (LIKE){ name: { $like: '%keyword%' } }
$notLikeKhông chứa{ name: { $notLike: '%spam%' } }
$inTrong danh sách{ status: { $in: ['active', 'pending'] } }
$notInKhông trong danh sách{ status: { $notIn: ['deleted'] } }
$emptyRỗng/null{ email: { $empty: true } }
$notEmptyKhông rỗng{ email: { $notEmpty: true } }

Ví dụ filter kết hợp

typescript
await agent.resource('external-mysql.orders').list({
  filter: {
    $and: [
      { status: { $eq: 'completed' } },
      { total: { $gt: 1000 } },
      { createdAt: { $gte: '2024-01-01' } },
    ],
  },
  sort: ['-createdAt'],
  page: 1,
  pageSize: 20,
});

Kiến trúc class

Hỗ trợ quan hệ (Relations)

Plugin hỗ trợ tự động phát hiện và tạo quan hệ giữa các bảng trong cùng data source:

Quan hệMô tả
hasManyMột-nhiều (dựa trên foreign key)
belongsToNhiều-một (dựa trên foreign key)

Plugin phát hiện foreign key từ INFORMATION_SCHEMA.KEY_COLUMN_USAGE và tạo quan hệ tương ứng.

Giới hạn và lưu ý

  • Primary key bắt buộc để CRUD hoạt động đầy đủ. Bảng composite primary key cũng được hỗ trợ
  • Auto-increment: Trường auto-increment không thể ghi từ Digiforce
  • Stored procedures: Không hỗ trợ
  • Views: Hỗ trợ đọc (read-only)
  • Phân trang mặc định: 20 bản ghi/trang
  • Decimal precision: Kiểu DECIMAL được giữ nguyên precision khi hiển thị