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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@nelts/typeorm

v1.0.13

Published

The description of this project

Readme

@nelts/typeorm

基于ts:typeorm的nelts插件。

Usage

npm i @nelts/typeorm

Example

我们需要在package.json上写入依赖:

{
  "plugin": {
    "@nelts/typeorm": {
      "enable": true,
      "runAt": [
        "master",
        "worker",
        "agent"
      ]
    }
  }
}

我们需要定义一些全局type或者interface类型[src/index.ts]

import { Connection } from 'typeorm';
import { 
  LocalAgentFactory as OrmLocalAgentFactory, 
  LocalMasterFactory as OrmLocalMasterFactory, 
  LocalAgentPlugin as OrmLocalAgentPlugin, 
  LocalMasterPlugin as OrmLocalMasterPlugin, 
  LocalWorkerContext as OrmLocalWorkerContext, 
  LocalWorkerFactory as OrmLocalWorkerFactory, 
  LocalWorkerPlugin as OrmLocalWorkerPlugin,
} from '@nelts/typeorm';

import Package from './modal/package';

// worker interfaces
export interface LocalWorkerContext extends OrmLocalWorkerContext {
  conn: Connection;
}
export interface LocalWorkerPlugin extends OrmLocalWorkerPlugin {
  getConnection(id: string): Connection;
}
export interface LocalWorkerFactory extends OrmLocalWorkerFactory {}

// agent interfaces
export interface LocalAgentPlugin extends OrmLocalAgentPlugin {}
export interface LocalAgentFactory extends OrmLocalAgentFactory {
  conn: Connection,
}

// master interfaces
export interface LocalMasterFactory extends OrmLocalMasterFactory {}
export interface LocalMasterPlugin extends OrmLocalMasterPlugin {}

export const Modals = [Package];

在master进程上,需要预设置数据库信息用来初始化数据库 [src/master.ts]

import { LocalMasterPlugin, Modals } from './index';

export default (plu: LocalMasterPlugin) => {
  plu.on('props', async configs => {
    const ormComponent = plu.getComponent<LocalMasterPlugin>('@nelts/typeorm');
    ormComponent.typeorm.preset(configs.mysql, Modals);
  });
}

在agent进程上,我们需要设置conn对象获取方式

import { LocalAgentPlugin, Modals, LocalAgentFactory } from './index';

export default (plu: LocalAgentPlugin) => {
  let id: string;
  plu.on('props', async configs => {
    const ormComponent = plu.getComponent<LocalAgentPlugin>('@nelts/typeorm');
    id = ormComponent.typeorm.preset(configs.mysql, Modals);
  });
  plu.on('ServerStarted', async () => {
    const app = plu.app as LocalAgentFactory;
    const ormComponent = plu.getComponent<LocalAgentPlugin>('@nelts/typeorm');
    if (!id) throw new Error('you cannot use getConnection method before recevie configs');
    app.conn = ormComponent.typeorm.get(id);
  })
}

在worker进程上,我们需要对Context对象进程设置 [src/worker.ts]

import { LocalWorkerPlugin, Modals, LocalWorkerContext } from './index';

export default (plu: LocalWorkerPlugin) => {
  let id: string;
  plu.on('props', async configs => {
    const ormComponent = plu.getComponent<LocalWorkerPlugin>('@nelts/typeorm');
    id = ormComponent.typeorm.preset(configs.mysql, Modals);
  });
  plu.app.on('ContextStart', async (ctx: LocalWorkerContext) => ctx.conn = plu.getConnection(id));
  plu.getConnection = (id: string) => {
    if (!id) throw new Error('you cannot use getConnection method before recevie configs');
    const ormComponent = plu.getComponent<LocalWorkerPlugin>('@nelts/typeorm');
    return ormComponent.typeorm.get(id);
  }
}

至此,全部设置完成。当服务启动后,首先会在master进程上初始化数据库,然后在agent和worker进程上获取到对象的conn对象。我们可以在Controller层上这样使用

import { Controller, Prefix, Get } from '@nelts/http';
import { LocalWorkerContext, LocalWorkerPlugin } from '../index';
import Package from '../modal/package';

@Prefix()
export default class IndexController extends Controller<LocalWorkerPlugin> {
  @Get()
  async Home(ctx: LocalWorkerContext) {
    const packageRepo = ctx.conn.getRepository(Package);
    ctx.body = await packageRepo.find();
  }
}

在agent进程上可以这样使用:

import { AgentComponent, AgentComponentImplements, Ipc, Auto } from '@nelts/agent';

@Auto
export default class IndexAgent extends AgentComponent implements AgentComponentImplements {
  @Ipc(true)
  async test() {
    const packageRepo = this.app.conn.getRepository(Package);
    return await packageRepo.find();
  }
}

License

MIT

Copyright (c) 2019-present, yunjie (Evio) shen