Bỏ qua, đến nội dung

Tham chiếu API — Microsoft SQL Server 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-mssql',
    type: 'mssql',
    displayName: 'SQL Server Production',
    options: {
      host: '192.168.1.100',
      port: 1433,
      database: 'ERP_Production',
      username: 'app_user',
      password: '***',
      encrypt: true,
      trustServerCertificate: false,
    },
  },
});

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

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

Introspect lại Schema

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

Filter Operators

Hỗ trợ đầy đủ filter operators:

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%' } }
$inTrong danh sách{ status: { $in: ['active', 'pending'] } }
$notInKhông trong danh sách{ status: { $notIn: ['deleted'] } }

Ví dụ query kết hợp

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

Kiến trúc class

Đặc biệt: buildDatabaseOptions()

MSSQL plugin override buildDatabaseOptions() để:

  • Set dialectModule = tedious (TDS protocol driver)
  • Cấu hình dialectOptions.options.encrypt từ option encrypt
  • Cấu hình dialectOptions.options.trustServerCertificate

Đặc biệt: createDatabaseFromOptions()

Override để đăng ký các field type đặc biệt cho MSSQL: MSSQLDateField, MSSQLDatetimeNoTzField, MSSQLBitField.

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

  • Primary key bắt buộc để CRUD hoạt động đầy đủ
  • Identity columns (auto-increment) không thể ghi từ Digiforce
  • Stored procedures: Không hỗ trợ — chỉ truy vấn trực tiếp trên bảng
  • Views: Hỗ trợ đọc (read-only)
  • Schema: Mặc định đọc bảng trong schema dbo, có thể cấu hình schema khác
  • Phân trang: Mặc định 20 bản ghi/trang
  • Named instance: Nếu SQL Server chạy dạng named instance (ví dụ SERVER\SQLEXPRESS), cần SQL Server Browser service đang chạy