Bỏ qua, đến nội dung

Tham chiếu API — MariaDB Data Source

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

Plugin hỗ trợ đầy đủ các thao tác CRUD thông qua resource API chuẩn của Digiforce:

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-mariadb',
    type: 'mariadb',
    displayName: 'MariaDB 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: 'mariadb',
    options: {
      host: '192.168.1.100',
      port: 3306,
      database: 'my_erp',
      username: 'app_user',
      password: '***',
    },
  },
});

Introspect lại Schema

Sau khi tạo data source, hệ thống tự động introspect. Để introspect lại thủ công (khi schema MariaDB thay đổi):

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

Filter Operators

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

OperatorMô tảVí dụ
$eqBằng{ name: { $eq: 'value' } }
$neKhác{ status: { $ne: 'deleted' } }
$gtLớn hơn{ age: { $gt: 18 } }
$gteLớn hơn hoặc bằng{ age: { $gte: 18 } }
$ltNhỏ hơn{ price: { $lt: 1000 } }
$lteNhỏ hơn hoặc bằng{ price: { $lte: 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-mariadb.orders').list({
  filter: {
    $and: [
      { status: { $eq: 'completed' } },
      { total: { $gt: 1000 } },
      { createdAt: { $gte: '2024-01-01' } },
    ],
  },
  sort: ['-createdAt'],
  page: 1,
  pageSize: 20,
});

Cấu trúc nội bộ Plugin

Kiến trúc class

Quy trình Introspect

  1. MariaDatabaseIntrospector.getTableList() — gọi showAllTables() qua Sequelize QueryInterface
  2. Với mỗi bảng, đọc cấu trúc cột qua describeTable()
  3. Map kiểu cột MariaDB sang field type qua getFieldTypeMap() (từ @digiforce-nc/database)
  4. Xác định interface UI qua getTypeInterfaceConfig() (từ type-interface-map.ts)
  5. Đăng ký collection vào CollectionManager

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

  • Primary key bắt buộc: Bảng không có primary key sẽ không hỗ trợ CRUD đầy đủ
  • Auto-increment: Trường auto-increment không thể ghi từ phía Digiforce
  • Stored procedures: Không được hỗ trợ — chỉ truy vấn trực tiếp trên bảng
  • Views: Hỗ trợ đọc (read-only), không ghi
  • Phân trang mặc định: 20 bản ghi/trang, có thể thay đổi qua pageSize
  • Kết nối: Sử dụng Sequelize connection pool mặc định