# @adonify/lucid-ordering 中文文档

规范地址：https://npmcn.com/package/%40adonify/lucid-ordering/

版本：1\.1\.0
npm 元数据更新时间：2022-11-26T04:46:40.875Z
中文译文更新时间：2026-11-08T05:29:32.708Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40adonify/lucid-ordering
原文对照：https://npmcn.com/package/%40adonify/lucid-ordering/?lang=original
项目主页：https://github.com/AdonifyCode/lucid-ordering#readme
源码仓库：https://github.com/AdonifyCode/lucid-ordering

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：24.8 kB，18 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- peer 依赖：@adonisjs/core ^5\.8\.7，@adonisjs/lucid ^18\.2\.0
- 首次发布：2022-11-22T04:38:17.859Z
- 最新版本发布：2022-11-26T04:46:40.796Z
- 近 30 天下载量：1893（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：5（近 12 个月 0 个）

## 中文简介

AdonisJS为清晰的模型简单订购.

## 安装

```sh
npm install @adonify/lucid-ordering
```

也可以使用：`pnpm add @adonify/lucid-ordering`、`yarn add @adonify/lucid-ordering`、`bun add @adonify/lucid-ordering`

## README

# 阿多尼斯·路西德 订购

> 一个简单的方法来维持你的Lucid模型的秩序.

[![npm-image](https://img.shields.io/npm/v/@adonify/lucid-ordering.svg?style=for-the-badge&logo=npm)](https://npmjs.org/package/@adonify/lucid-ordering "npm") [![license-image](https://img.shields.io/npm/l/@adonify/lucid-ordering?color=blueviolet&style=for-the-badge)](https://npmcn.com/package/%40adonify/lucid-ordering/LICENSE.md "license") [![typescript-image](https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript)](https://npmcn.com/package/%40adonify/lucid-ordering/%22typescript%22)

## 导言

这个软件包提供了一种方便的混合来创建命令行为(序列)与你的Lucid模型. 这种序列顺序可以维持在每表级或通过被命令的列.

## 安装

```bash
npm i @adonify/lucid-ordering

# Or using Yarn

yarn add @adonify/lucid-ordering
```

## 配置

```bash
node ace configure @adonify/lucid-ordering
```

## 使用量

### 将混音添加到任何 Lucid 模式

```ts
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`但是,它不需要是一个整数。 任何支持平等比较的数据类型都会奏效.

```ts
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()` 装饰如下. 您可以在多列上使用装饰来创建独特的组合来维持与该组合的秩序,但请注意这个结果会在引擎盖下额外查询.

```ts
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>;
}
```

```ts
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中的最大数字.

```ts
// 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
```

如果您想要在创建模型时手动设定顺序,您可以提供它,订单将不会自动生成.

```ts
// 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 
```

#### 用顺序键

```ts
// 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
```

### 维持秩序

#### 删除

当一个模型被删除时,顺序会自动同步.

```ts
// 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()后钩.

#### 手动同步命令

如果你的序列变得异常, 你可以手动同步序列.

```ts
await todo.syncSequence()
```

这将同步序列中的所有模型, 相对于任何显示的顺序键 .

> **3⁄4 ̄ ̧漯B**
> 
> 这个软件包依赖于将所有模型都保留在一个有顺序的序列中(即1,2,3,.n). 如果您通过数据库手动更新这些值, 您需要再次同步序列, 以使各种方法和钩子正确工作 .
> 
> 此外,这种方法还依赖于在删除一行时对每一行序列进行更新,在顺序移动时对多行序列进行更新。 这意味着大型数据集可能无法满足您的性能需要,你应该谨慎使用这个软件包.

### 示范职能

| 模型函数 | 说明 |
| --- | --- |
| set Order( 顺序: 号码): 保证< 无效> | 在模型上设定新顺序 . |
| 为 First (): 布尔 | 标出模型是否在它的序列中第一. |
| 是 Last (): 承诺<布尔> | 标出模型是否在它的序列中最后的标记 . |
| 移动到Start (): 保证<有效> | 将模型移动到其序列的起始. |
| 移动到 End (): 承诺 < void> | 将模型移到它的序列的结尾. |
| moveUp( ): 承诺< 无效> | 把模型一个位置移到它的序列上. |
| 互换命令( 型号 ToSwap: 有序模式): 保证< 无效> | 换取两个型号的顺序. |
| 移动前( 型号: 有序模式): 承诺 < void > | 将一个模型移动到另一个它的序列之前. |
| 移动后( 型号: 有序模式): 保证< 无效> | 将模型依次移动在它的序列中. |
| 同步序列( ): 承诺< 无效> | 同步调用的模型序列 . |

## 问题

如果你有问题或发现错误 请随意 [打开一个问题](https://github.com/AdonifyCode/lucid-ordering/issues)。 。 。 .

## 常见问题

### @adonify/lucid-ordering 是什么？

AdonisJS为清晰的模型简单订购\.

### 如何安装 @adonify/lucid-ordering？

在项目目录运行 npm install @adonify/lucid-ordering，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@adonisjs/core（^5\.8\.7）、@adonisjs/lucid（^18\.2\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @adonify/lucid-ordering 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @adonify/lucid-ordering 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @adonify/lucid-ordering 可以商用吗？

@adonify/lucid-ordering 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @adonify/lucid-ordering 的下载量有多少？

根据 npm 官方下载统计，@adonify/lucid-ordering 在 2026/10/05 – 2026/11/03 这 30 天内被下载 1,893 次，日均约 63 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @adonify/lucid-ordering 还在维护吗？

最新版本 1\.1\.0 发布于 2022/11/26（3 年前）；至今共发布 5 个版本，其中过去 12 个月发布了 0 个；首次发布于 2022/11/22。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

