@adonify/lucid-ordering

中文文档
1.1.0发布于 2022/11/26MITTypeScriptCommonJS零依赖近 30 天下载 1893

Simple ordering for lucid models with AdonisJS.

原始使用文档
文档目录11 节
  1. Introduction
  2. Installation
  3. Configuration
  4. Usage
  5. Add the mixin to any Lucid model
  6. Create the Todo migration or add an order field to your exi…
  7. Ordered models with respect to an order key
  8. Creating new models
  9. Maintaining order
  10. Model functions
  11. Issues

Adonis Lucid Ordering

A simple way to maintain order with your Lucid models.

npm-image license-image typescript-image

Introduction

This package provides a convenient mixin to create ordered behavior (sequences) with your Lucid models. This sequence order can be maintained at a per table level or via an ordered column.

Installation

npm i @adonify/lucid-ordering

# Or using Yarn

yarn add @adonify/lucid-ordering

Configuration

node ace configure @adonify/lucid-ordering

Usage

Add the mixin to any Lucid model

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) {}

Create the Todo migration or add an order field to your existing model.

The column must be called order, however it doesn't need to be an integer. Any datatype that supports equality comparison will work.

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);
  }
}

Ordered models with respect to an order key

We often need to maintain different sequences on one table with respect to a unique field. For example we may have a Board model with Todo's that we need we need to keep in order corresponding to the Board's id.

You can do this using the orderKey() decorator as follows. You can use the decorator on multiple columns to create unique combinations to maintain order with, but be aware this results in extra queries under the hood.

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);
  }
}

Now the mixin will maintain separate sequences on the todos table based on the grouping of their boardId.

Creating new models

When a new ordered model is created the order will automatically be set to the largest number in the sequence + 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

If you wish to manually set the order when you create a model, you can supply it and the order will not be auto-generated.

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

With an order key

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

Maintaining order

Deletes

When a model is deleted, the order of the sequence will be automatically synced.

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

Note

Models will only be automatically synced if you use the model delete() method as it relies on the afterDelete() hook under the hood.

Manually syncing orders

If your sequence were to ever become out of order, you can manually sync the sequence.

await todo.syncSequence()

This will sync all models in the sequence, with respect to any order keys present.

Warning

This package relies on keeping all models in an ordered sequence (ie. 1, 2, 3, ...n). If you manually update these values through the database, you will need to sync the sequence again for the various methods and hooks to work correctly.

Furthermore, this approach relies on potentially doing an update on each row of the sequence when a row is deleted, and on multiple rows during order moves. This means large datasets may not be optimal for your performance needs and you should use this package with caution.

Model functions

Model functionDescription
setOrder(order: number): Promise<void>Set a new order on the model.
isFirst(): booleanFlag for whether the model is first in it's sequence.
isLast(): Promise<boolean>Flag for whether the model is last in it's sequence.
moveToStart(): Promise<void>Move the model to the start of it's sequence.
moveToEnd(): Promise<void>Move the model to the end of it's sequence.
moveUp(): Promise<void>Move the model one position up in it's sequence.
swapOrder(modelToSwap: OrderedModel): Promise<void>Swap the order of two models.
moveBefore(model: OrderedModel): Promise<void>Move a model before another in it's sequence.
moveAfter(model: OrderedModel): Promise<void>Move a model after another in it's sequence.
syncSequence(): Promise<void>Syncs the sequence of the model called on.

Issues

If you have a question or found a bug, feel free to open an issue.

版本与发布记录

@adonify/lucid-ordering 共发布 5 个版本,首次发布于 2022/11/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2022/11/263 年前
1.0.32022/11/253 年前
1.0.22022/11/233 年前
1.0.12022/11/233 年前
1.0.02022/11/223 年前

关于 @adonify/lucid-ordering 的常见问题

@adonify/lucid-ordering 是什么?

Simple ordering for lucid models with 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 和提交记录判断。