npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@seepine/hono-typeorm

v0.1.2

Published

Hono middleware for TypeORM. Easily integrate TypeORM database access, connection, and transaction management into Hono apps.

Readme

hono-typeorm

npm version npm downloads bundle License

基于 Hono 的 TypeORM 数据库中间件。让你可以在 Hono 应用中便捷集成 TypeORM 。

快速使用

1. 安装

npm install @seepine/hono-typeorm typeorm

# 安装你所使用的数据库依赖
npm install pg

修改 tsconfig.json

{
  "compilerOptions": {
    // 添加 typeorm 所需配置
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
  }
}

2. 创建实体类

@Entity('sys_user')
export class User {
  @PrimaryGeneratedColumn('uuid')
  id: number

  @Column('varchar', { length: 100 })
  name: string
}

3. 基本用法

import { Hono } from 'hono'
import { createTypeormMiddleware } from '@seepine/hono-typeorm'
import { User } from './entity/User'

const app = new Hono()
const { orm, typeormMiddleware } = createTypeormMiddleware({
  type: 'better-sqlite3',
  url: ':memory:',
  synchronize: true,
  entities: [User],
})
app.use(typeormMiddleware)
// 连接
await orm.initialize()

app.get('/users', async c => {
  const { manager } = c.var.orm
  const users = await manager.find(User)
  return c.json(users)
})

自定义字段

实际业务中,我们经常会需要字段自动生成,例如主键使用分布式雪花id,时间字段自动生成,创建者id字段自动从上下文获取当前用户id,本文以自定义主键场景为例,介绍如何自定义装饰器和生成逻辑

1. 创建装饰器

import { buildColumn } from '@seepine/hono-typeorm'

export function PrimarySnowflakeColumn(options?: ColumnOptions): PropertyDecorator {
  return buildColumn({
    // 指定仅在创建时生成,若需要新增和更新都生成值,可改为 createAndUpdate
    generateTrigger: 'create',
    // 实现生成逻辑,例如使用雪花算法生成id
    generate: (): any => {
      return `id_${id++}`
    },
    // 这里指定默认的字段配置
    columnOptions: {
      type: 'varchar',
      length: 50,
      primary: true, // 注意,若是主键装饰器,必须要此配置
      ...options,
    },
  })
}

2. 使用

在需要的字段上使用,则在新增时会按照 generate 方法自动生成id,当然若在新增时,手动赋值,则会以赋值为主

@Entity('sys_user')
export class User {
  // 使用自定义主键注解,例如雪花id生成策略
  @PrimarySnowflakeColumn()
  id: string
}

更多用法

1. 事务支持

app.post('/users', async c => {
  await c.var.orm.transaction(async manager => {
    // 在事务中进行数据库操作
    await manager.save(User, { name: 'Tom' })
    await manager.save(User, { name: 'Job' })
  })
  return c.text('ok')
})

2. 全局访问(可选)

declare global {
  var orm: DataSource
}
const { typeormMiddleware, orm } = createTypeormMiddleware({
  type: 'better-sqlite3',
  url: ':memory:',
  synchronize: true,
  entities: [User],
})
app.use(typeormMiddleware)
globalThis.orm = orm

// 在任何地方获取 EntityManager
const manager = globalThis.orm.manager

3. 更多

更多用法参考 typeorm 官方文档即可

配置选项

TypeormOpts

常用配置如下,更多请参考 TypeORM 官方文档

| 选项 | 类型 | 说明 | | ------------- | ----------------------------------------- | ----------------------------------------------------------------- | | type | string | 数据库类型,如 'postgres'、'mysql'、'sqlite'、'better-sqlite3' 等 | | url | string | 连接 URL 或文件路径 | | synchronize | boolean | 是否自动同步表结构 | | logLevel | LoggerOptions | 日志级别,默认 ['error', 'warn'] | | entities | MixedList<Function/string/EntitySchema> | 实体列表,如 src/entity/*.ts | | subscribers | Array<Function/string> | 订阅者列表 |

支持的环境变量

DATABASE_TYPE=postgres
DATABASE_URL=postgresql://myusername:mypassword@localhost:5432/mydatabase
DATABASE_SYNCHRONIZE=true

自定义 DataSource

若配置选项不满足,可直接自定义数据源传入

const datasource = new DataSource({
  type: 'postgres',
  url: 'postgresql://myusername:mypassword@localhost:5432/mydatabase',
  synchronize: true,
  logging: ['error', 'warn'],
  entities: [User],
  subscribers: [],
})
const { orm, typeormMiddleware } = createTypeormMiddleware(datasource)

示例

PostgreSQL

const { orm, typeormMiddleware } = createTypeormMiddleware({
  type: 'postgres',
  url: 'postgresql://myusername:mypassword@localhost:5432/mydatabase',
  synchronize: true,
  entities: [User],
})

MySQL

const { orm, typeormMiddleware } = createTypeormMiddleware({
  type: 'mysql',
  url: 'mysql://myusername:mypassword@localhost:3306/mydatabase',
  synchronize: true,
  entities: [User],
})

Better-Sqlite3

const { orm, typeormMiddleware } = createTypeormMiddleware({
  type: 'better-sqlite3',
  url: './data.db', // 文件路径或内存 ':memory:'
  synchronize: true,
  entities: [User],
})