阿多尼斯·路西德 订购
一个简单的方法来维持你的Lucid模型的秩序.
导言
这个软件包提供了一种方便的混合来创建命令行为(序列)与你的Lucid模型. 这种序列顺序可以维持在每表级或通过被命令的列.
安装
npm i @adonify/lucid-ordering
# Or using Yarn
yarn add @adonify/lucid-ordering
配置
node ace configure @adonify/lucid-ordering
使用量
将混音添加到任何 Lucid 模式
import { BaseModel } from "@ioc:Adonis/Lucid/Orm";
import { compose } from '@ioc:Adonis/Core/Helpers'
import { Ordered } from "@ioc:Adonify/LucidOrdering";
export default class Todo extends compose(BaseModel, Ordered) {}
创建 Todo 迁移或为您现有的模式添加命令字段 .
栏必须叫 order但是,它不需要是一个整数。 任何支持平等比较的数据类型都会奏效.
import BaseSchema from "@ioc:Adonis/Lucid/Schema";
export default class extends BaseSchema {
protected tableName = "todos";
public async up() {
this.schema.createTable(this.tableName, (table) => {
table.increments("id");
table.timestamp("created_at", { useTz: true });
table.timestamp("updated_at", { useTz: true });
table.integer("order").notNullable(); // 👈 the column is needed
});
}
public async down() {
this.schema.dropTable(this.tableName);
}
}
排序键的有序模式
我们常常需要在一个独特的领域上保持不同的顺序。 例如,我们可能有一个与Todo's的董事会模型,我们需要保持这个模型,以便与董事会的ID相对应.
你可以用这个 orderKey() 装饰如下. 您可以在多列上使用装饰来创建独特的组合来维持与该组合的秩序,但请注意这个结果会在引擎盖下额外查询.
import { BaseModel, BelongsTo, belongsTo, column } from "@ioc:Adonis/Lucid/Orm";
import Board from "App/Models/Board";
import { compose } from '@ioc:Adonis/Core/Helpers'
import { Ordered, orderKey } from "@ioc:Adonify/LucidOrdering";
export default class Todo extends compose(BaseModel, Ordered) {
@column()
@orderKey()
public boardId: number;
@belongsTo(() => Board)
public board: BelongsTo<typeof Board>;
}
export default class extends BaseSchema {
protected tableName = "todos";
public async up() {
this.schema.createTable(this.tableName, (table) => {
table.increments("id");
table.timestamp("created_at", { useTz: true });
table.timestamp("updated_at", { useTz: true });
table.integer("order").notNullable(); // 👈 order in the sequence
table
.integer("board_id")
.unsigned()
.references("boards.id")
.onDelete("CASCADE"); // 👈 key to track the sequence on the table
});
}
public async down() {
this.schema.dropTable(this.tableName);
}
}
现在混音将保持 单独的序列在 todos 表格基于其分类 boardId。 。 。 .
创建新模式
当新订购的模型被创建后,顺序将自动被设定为序列+1中的最大数字.
// Assume no todos are present in the database yet
const todo1 = await Todo.create({}) // will have order of 0
const todo2 = await Todo.create({}) // will have order of 1
如果您想要在创建模型时手动设定顺序,您可以提供它,订单将不会自动生成.
// Assume no todos are present in the database yet
const todo1 = await Todo.create({ order: 0 }) // will have order of 0
const todo2 = await Todo.create({ order: 2 }) // will have order of 2
用顺序键
// Assume no todos are present in the database yet
const todo1 = await Todo.create({ boardId: 1 }) // will have order of 0
const todo2 = await Todo.create({ boardId: 1 }) // will have order of 1
const todo3 = await Todo.create({ boardId: 2 }) // will have order of 0
const todo4 = await Todo.create({ boardId: 2 }) // will have order of 1
维持秩序
删除
当一个模型被删除时,顺序会自动同步.
// Assume no todos are present in the database yet
const todo1 = await Todo.create({})
const todo2 = await Todo.create({})
const todo3 = await Todo.create({})
await todo2.delete()
// todo1 and todo3 will now be synced to have orders of 0 and 1 respectively
说明
模型只有在使用模型删除()方法时才会自动同步,因为它依赖于引擎盖下Delete()后钩.
手动同步命令
如果你的序列变得异常, 你可以手动同步序列.
await todo.syncSequence()
这将同步序列中的所有模型, 相对于任何显示的顺序键 .
3⁄4 ̄ ̧漯B
这个软件包依赖于将所有模型都保留在一个有顺序的序列中(即1,2,3,.n). 如果您通过数据库手动更新这些值, 您需要再次同步序列, 以使各种方法和钩子正确工作 .
此外,这种方法还依赖于在删除一行时对每一行序列进行更新,在顺序移动时对多行序列进行更新。 这意味着大型数据集可能无法满足您的性能需要,你应该谨慎使用这个软件包.
示范职能
| 模型函数 | 说明 |
|---|---|
| set Order( 顺序: 号码): 保证< 无效> | 在模型上设定新顺序 . |
| 为 First (): 布尔 | 标出模型是否在它的序列中第一. |
| 是 Last (): 承诺<布尔> | 标出模型是否在它的序列中最后的标记 . |
| 移动到Start (): 保证<有效> | 将模型移动到其序列的起始. |
| 移动到 End (): 承诺 < void> | 将模型移到它的序列的结尾. |
| moveUp( ): 承诺< 无效> | 把模型一个位置移到它的序列上. |
| 互换命令( 型号 ToSwap: 有序模式): 保证< 无效> | 换取两个型号的顺序. |
| 移动前( 型号: 有序模式): 承诺 < void > | 将一个模型移动到另一个它的序列之前. |
| 移动后( 型号: 有序模式): 保证< 无效> | 将模型依次移动在它的序列中. |
| 同步序列( ): 承诺< 无效> | 同步调用的模型序列 . |
问题
如果你有问题或发现错误 请随意 打开一个问题。 。 。 .