Liên kết cần chứa id để có thể nhận định dạng dữ liệu cần xóa, liên kết này được viết dưới dạng gửi dữ liệu, hành động tới trang xử lý xử lý xóa [thực tế là điều hướng tới Bộ điều khiển xử lý dữ liệu], phương thức được
Plugin này là một nhánh rẽ từ phản đối-xóa mềm khi tác giả ngừng duy trì plugin. Vì lý do đó, dự án đã được viết lại hoàn toàn và việc sử dụng các phương pháp không dùng nữa đã được khắc phục.
9yarn add objection-js-soft-delete
Một plugin bổ sung chức năng xóa mềm cho Phản đối. js
- Cài đặt
- Đăng ký plugin
- Cách sử dụng
- phương pháp
- whereNotDeleted[]
- đã xóa ở đâu[]
- phục hồi []
- cứngDelete[]
- bộ lọc
- không bị xóa
- đã xóa
- trong một mối quan hệ
- Sử dụng với upsertGraph
- Chức năng vòng đời
- phương pháp
Cài đặt
NPM
npm i objection-js-soft-delete
sợi
yarn add objection-js-soft-delete
Đăng ký plugin
Mixin cung cấp cấu hình sau để ghi đè các tùy chọn mặc định
tên cột dọc. tên cột cho biết bản ghi có bị xóa hay không. Cột phải tồn tại trên bảng cho mô hình. Mặc định.
0// Import objection and the plugin.
import { Model } from 'objection';
import objectionSoftDelete from 'objection-js-soft-delete';
// Specify the options for this plugin. This are the defaults.
const softDelete = objectionSoftDelete[{
columnName: 'deleted_at',
deletedValue: new Date[],
notDeletedValue: null,
}];
// Inject the plugin to the model
class User extends softDelete[Model] {
static get tableName[] {
return 'Users';
}
}
đã xóa Giá trị. xác định giá trị nào cho biết nếu bản ghi bị xóa. Mặc định.
1 [múi giờ địa phương của máy chủ]// Import objection and the plugin.
import { Model } from 'objection';
import objectionSoftDelete from 'objection-js-soft-delete';
// Specify the options for this plugin. This are the defaults.
const softDelete = objectionSoftDelete[{
columnName: 'deleted_at',
deletedValue: new Date[],
notDeletedValue: null,
}];
// Inject the plugin to the model
class User extends softDelete[Model] {
static get tableName[] {
return 'Users';
}
}
Cũng có thể sử dụng thời gian từ cơ sở dữ liệu.
2// Import objection and the plugin.
import { Model } from 'objection';
import objectionSoftDelete from 'objection-js-soft-delete';
// Specify the options for this plugin. This are the defaults.
const softDelete = objectionSoftDelete[{
columnName: 'deleted_at',
deletedValue: new Date[],
notDeletedValue: null,
}];
// Inject the plugin to the model
class User extends softDelete[Model] {
static get tableName[] {
return 'Users';
}
}
notDeletedValue. xác định giá trị nào cho biết nếu bản ghi không bị xóa. Bạn có thể đặt [và nên] tùy chọn này cùng với
3. Mặc định.// Import objection and the plugin.
import { Model } from 'objection';
import objectionSoftDelete from 'objection-js-soft-delete';
// Specify the options for this plugin. This are the defaults.
const softDelete = objectionSoftDelete[{
columnName: 'deleted_at',
deletedValue: new Date[],
notDeletedValue: null,
}];
// Inject the plugin to the model
class User extends softDelete[Model] {
static get tableName[] {
return 'Users';
}
}
4// Import objection and the plugin.
import { Model } from 'objection';
import objectionSoftDelete from 'objection-js-soft-delete';
// Specify the options for this plugin. This are the defaults.
const softDelete = objectionSoftDelete[{
columnName: 'deleted_at',
deletedValue: new Date[],
notDeletedValue: null,
}];
// Inject the plugin to the model
class User extends softDelete[Model] {
static get tableName[] {
return 'Users';
}
}
// Import objection and the plugin.
import { Model } from 'objection';
import objectionSoftDelete from 'objection-js-soft-delete';
// Specify the options for this plugin. This are the defaults.
const softDelete = objectionSoftDelete[{
columnName: 'deleted_at',
deletedValue: new Date[],
notDeletedValue: null,
}];
// Inject the plugin to the model
class User extends softDelete[Model] {
static get tableName[] {
return 'Users';
}
}
Ghi chú. Giá trị
3 của// Import objection and the plugin.
import { Model } from 'objection';
import objectionSoftDelete from 'objection-js-soft-delete';
// Specify the options for this plugin. This are the defaults.
const softDelete = objectionSoftDelete[{
columnName: 'deleted_at',
deletedValue: new Date[],
notDeletedValue: null,
}];
// Inject the plugin to the model
class User extends softDelete[Model] {
static get tableName[] {
return 'Users';
}
}
4 sẽ dẫn đến plugin này có hành vi không mong muốn// Import objection and the plugin.
import { Model } from 'objection';
import objectionSoftDelete from 'objection-js-soft-delete';
// Specify the options for this plugin. This are the defaults.
const softDelete = objectionSoftDelete[{
columnName: 'deleted_at',
deletedValue: new Date[],
notDeletedValue: null,
}];
// Inject the plugin to the model
class User extends softDelete[Model] {
static get tableName[] {
return 'Users';
}
}
Cách sử dụng
- Bản ghi xóa mềm. Khi một bản ghi sẽ bị xóa thì trường đã xóa được đặt thành giá trị được chỉ định là giá trị đã xóa
1npm i objection-js-soft-delete
phương pháp
- whereNotDeleted[]. Chỉ trả về các bản ghi không bị xóa
2npm i objection-js-soft-delete
- đã xóa ở đâu[]. Chỉ trả về các bản ghi đã bị xóa
3npm i objection-js-soft-delete
- phục hồi []. Khôi phục bản ghi đã xóa
4npm i objection-js-soft-delete
- cứngDelete[]. Xóa vĩnh viễn bản ghi
5npm i objection-js-soft-delete
bộ lọc
Bộ lọc
7 và// Import objection and the plugin.
import { Model } from 'objection';
import objectionSoftDelete from 'objection-js-soft-delete';
// Specify the options for this plugin. This are the defaults.
const softDelete = objectionSoftDelete[{
columnName: 'deleted_at',
deletedValue: new Date[],
notDeletedValue: null,
}];
// Inject the plugin to the model
class User extends softDelete[Model] {
static get tableName[] {
return 'Users';
}
}
8 sẽ được thêm vào danh sách các bộ lọc được đặt tên cho bất kỳ mô hình nào sử dụng mixin này. Trong nội bộ, họ đang sử dụng các phương pháp// Import objection and the plugin.
import { Model } from 'objection';
import objectionSoftDelete from 'objection-js-soft-delete';
// Specify the options for this plugin. This are the defaults.
const softDelete = objectionSoftDelete[{
columnName: 'deleted_at',
deletedValue: new Date[],
notDeletedValue: null,
}];
// Inject the plugin to the model
class User extends softDelete[Model] {
static get tableName[] {
return 'Users';
}
}
9 và// Import objection and the plugin.
import { Model } from 'objection';
import objectionSoftDelete from 'objection-js-soft-delete';
// Specify the options for this plugin. This are the defaults.
const softDelete = objectionSoftDelete[{
columnName: 'deleted_at',
deletedValue: new Date[],
notDeletedValue: null,
}];
// Inject the plugin to the model
class User extends softDelete[Model] {
static get tableName[] {
return 'Users';
}
}
10 từ phía trênnpm i objection-js-soft-delete
- không bị xóa. Chỉ trả về các bản ghi từ mối quan hệ không bị xóa
0yarn add objection-js-soft-delete
- đã xóa. Chỉ trả về các bản ghi từ mối quan hệ đã bị xóa
1yarn add objection-js-soft-delete
- Bộ lọc mối quan hệ. Một bộ lọc cũng có thể được áp dụng trực tiếp cho một mối quan hệ
0yarn add objection-js-soft-delete
sau đó
1yarn add objection-js-soft-delete
Sử dụng với upsertGraph
Plugin này thực sự được sinh ra từ nhu cầu xóa mềm
11 trong một số bảng và xóa cứng trong các bảng khácnpm i objection-js-soft-delete
2yarn add objection-js-soft-delete
Chức năng vòng đời
Một vấn đề xảy ra khi thực hiện xóa mềm là lệnh gọi của bạn tới
12 sẽ thực sự kích hoạt các chức năng vòng đời chonpm i objection-js-soft-delete
13, điều này có thể không được mong đợi hoặc mong muốn. Để giúp giải quyết vấn đề này, một số cờ ngữ cảnh đã được thêm vàonpm i objection-js-soft-delete
14 được chuyển vào các chức năng vòng đời để giúp phân biệt xem sự kiện đã kích hoạt [e. g. ]npm i objection-js-soft-delete
15 là một bản cập nhật thực sự, một bản xóa mềm hoặc một bản phục hồinpm i objection-js-soft-delete