# @hapipal/schwifty 中文文档

规范地址：https://npmcn.com/package/%40hapipal/schwifty/

版本：6\.2\.0
npm 元数据更新时间：2022-11-28T03:23:33.236Z
中文译文更新时间：2026-11-17T09:09:54.056Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：25.9 kB，9 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- Node.js 要求：\>=12
- 运行依赖：3 个
- peer 依赖：@hapi/hapi \>=19 \<22，joi \>=17 \<18，knex \>=0\.16，objection \>=1 \<4
- 首次发布：2020-11-11T03:48:58.764Z
- 最新版本发布：2022-11-28T03:23:33.151Z
- 近 30 天下载量：7531（2026-10-17 至 2026-11-15，npm 官方下载统计）
- 已发布版本数：5（近 12 个月 0 个）

## 中文简介

用于将反对 ORM 整合在一起的 hapi 模型层

## 安装

```sh
npm install @hapipal/schwifty
```

也可以使用：`pnpm add @hapipal/schwifty`、`yarn add @hapipal/schwifty`、`bun add @hapipal/schwifty`

## README

# 轻而易举

模式层 [霍比语name](https://hapi.dev/) 整合 [反对 ORM](https://vincit.github.io/objection.js/)

[![Build Status](https://app.travis-ci.com/hapipal/schwifty.svg?branch=main)](https://app.travis-ci.com/hapipal/schwifty) [![Coverage Status](https://coveralls.io/repos/github/hapipal/schwifty/badge.svg?branch=main)](https://coveralls.io/github/hapipal/schwifty?branch=main)

主维护者 - [变形常春藤](https://github.com/devinivy)

## 安装

```sh
npm install @hapipal/schwifty
```

## 使用量

> 另见 [API 参考](https://npmcn.com/package/%40hapipal/schwifty/API.md)
> 
> Schwifty的用途有:hapi v19+, nodejs v12+,Joi v17+,反对v1通过v3,和knex v0.16+ ()_ưμ㼯a_) (中文(简体) ).

Schwifty 用于定义 [乔](https://joi.dev/)\- 与反对ORM兼容的模型和knex连接。 这些模型在最方便的地方 在您的 hopi 服务器中可以使用。 它已经适应多插件部署,每个插件在定义自己的模型、knex数据库连接和迁移时可以设置明确的界限。 它可以安全地注册了多倍的schwifty,无论你想在哪里使用,因为它可以防止模型名称相撞和其他模棱两可的配置.

```js
// First, ensure your project includes knex, objection, and sqlite3

// To get started you might run,
// npm install --save @hapipal/schwifty @hapi/hapi joi knex objection sqlite3

'use strict';

const Hapi = require('@hapi/hapi');
const Schwifty = require('@hapipal/schwifty');
const Joi = require('joi');

(async () => {

    const server = Hapi.server({ port: 3000 });

    server.route({
        method: 'get',
        path: '/dogs/{id}',
        handler: async (request) => {

            const { Dog } = request.models();

            return await Dog.query().findById(request.params.id);
        }
    });

    await server.register({
        plugin: Schwifty,
        options: {
            knex: {
                client: 'sqlite3',
                useNullAsDefault: true,
                connection: {
                    filename: ':memory:'
                }
            }
        }
    });

    // Register a model with schwifty...

    server.registerModel(
        class Dog extends Schwifty.Model {
            static tableName = 'Dog';
            static joiSchema = Joi.object({
                id: Joi.number(),
                name: Joi.string()
            });
        }
    );

    await server.initialize();

    // ... then make a table ...

    const knex = server.knex();

    await knex.schema.createTable('Dog', (table) => {

        table.increments('id').primary();
        table.string('name');
    });

    // ... then add some records ...

    const { Dog } = server.models();

    await Promise.all([
        Dog.query().insert({ name: 'Guinness' }),
        Dog.query().insert({ name: 'Sully' }),
        Dog.query().insert({ name: 'Ren' })
    ]);

    // ... then start the server!

    await server.start();

    console.log(`Now, go find some dogs at ${server.info.uri}`);
})();
```

## 额外

-   兼容 [高考成绩](https://github.com/hapipal/haute-couture)
-   [反对文件](http://vincit.github.io/objection.js/)
-   [Knex 文档](https://knexjs.org/)

## 常见问题

### @hapipal/schwifty 是什么？

用于将反对 ORM 整合在一起的 hapi 模型层

### 如何安装 @hapipal/schwifty？

在项目目录运行 npm install @hapipal/schwifty，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@hapi/hapi（\>=19 \<22）、joi（\>=17 \<18）、knex（\>=0\.16）、objection（\>=1 \<4）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @hapipal/schwifty 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/hapipal\_\_schwifty 是否存在。

### @hapipal/schwifty 支持 ESM 还是 CommonJS？

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

### @hapipal/schwifty 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=12。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @hapipal/schwifty 可以商用吗？

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

### @hapipal/schwifty 的下载量有多少？

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

### @hapipal/schwifty 还在维护吗？

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

