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 🙏

© 2024 – Pkg Stats / Ryan Hefner

egg-knex-model

v0.0.2

Published

a knex plugin for egg.js

Downloads

3

Readme

egg-knex-model

a knex plugin for egg.js

Usage

  1. Run npm i egg-knex-model
  2. Enable egg-knex-model
{
  knex: {
    enable: true,
    package: 'egg-knex-model',
  },
}
  1. Configure egg-knex-model in config/config.default.ts
{
  knex: {
    client: {
      // please put knex config here
      // https://knexjs.org/#Installation-client
      client: 'pg',
      version: '7.2',
      connection: {
        host : '127.0.0.1',
        user : 'your_database_user',
        password : 'your_database_password',
        database : 'myapp_test'
      }
    },
    app: true,
    agent: false,
  },
}
  1. Configure egg-ts-helper in package.json (TypeScript ONLY)
{
  "egg": {
    "typescript": true,
    "tsHelper": {
      "watchDirs": {
        "model": {
          "pattern": "**/*.(ts|js)",
          "path": "app/model",
          "generator": "class",
          "interface": "IModel",
          "caseStyle": "lower",
          "trigger": [
            "add",
            "unlink"
          ],
          "interfaceHandle": false
        }
      }
    }
  }
}
  1. Add model in app/model
// app/model/User.ts
import { BaseModel, BaseEntity } from 'egg-knex-model';

// or if you don't want `id`, `created_at` and `updated_at`,
// you can directly define the interface of entity without `extends`
// or you can use PartialBaseEntity<'id'> to get { id:number } and `extends` it
interface E extends BaseEntity {
  username:string;
  points:number;
}

export default class extends BaseModel<E> {
  protected tableName = 'user';

  // you can add some method like this,
  // or just use methods in BaseModel
  getByUsername(username:string) {
    return this.getOne({username});
  }
}
  1. Use model
 // app/service/Test.ts
 export default class Test extends Service {
   public async test() {
     // transaction is easy to use
     // let's use it to add a new user and increment it's points
     await this.app.knex.trx(async model => {
       // must use `model` instead of `this.app.model` in a transaction
       await model.user.insert({
         username: '23333',
         points: 100,
       });
       await model.user.increment({ username: '23333'}, 'points');
     });

     // use getOne to get the user
     console.log(await this.app.model.user.getOne({
       username: '23333',
     }));
     /*
     {
       id: 5,
       username: '23333',
       points: 101,
       created_at: 2018-10-27T19:18:54.703Z,
       updated_at: 2018-10-27T19:18:54.703Z
     }
     */

     // it should auto rollback when an error occurred
     try {
       await this.app.knex.trx(async model => {
         await model.user.update({}, { username: '555555' });
         throw new Error('this transaction should be rolled back');
       });
     } catch (e) {
       console.error(e);
       /*
       Error: this transaction should be rolled back
           at app.knex.trx (/Users/erona/projects/bhdh/app/service/Test.ts:27:15)
           at <anonymous>
       From previous event:
           at /Users/erona/projects/egg-knex-model/node_modules/knex/lib/transaction.js:91:14
           at runCallback (timers.js:794:20)
       From previous event:
           at new Transaction (/Users/erona/projects/egg-knex-model/node_modules/knex/lib/transaction.js:63:41)
           at Client_PG.transaction (/Users/erona/projects/egg-knex-model/node_modules/knex/lib/client.js:152:12)
           at Function.transaction (/Users/erona/projects/egg-knex-model/node_modules/knex/lib/util/make-knex.js:65:21)
           at Object.createTrx (/Users/erona/projects/egg-knex-model/dist/lib/transaction.js:57:17)
           at Function.knexEx.trx (/Users/erona/projects/egg-knex-model/dist/lib/loader.js:90:60)
           at Test.test (/Users/erona/projects/bhdh/app/service/Test.ts:25:27)
           at <anonymous>
       */
     }
   }
 }