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

henhouse-store-sequelize

v1.0.16

Published

a store plugin for henhouse, access db by sequelize

Downloads

4

Readme

henhouse-store-sequelize

a store plugin for henhouse, access data by sequelize

Installation

Install using npm:

npm install henhouse-store-sequelize

API Reference

constructor

const sequelizeStore = new SequelizeStore(options)

options

| option | Type | Description | | ------------- | ---------------------------- | ----------------------------------- | | tableNameMode | SequelizeStore.TableNameMode | CAMELCASE、 UNDERLINE、 TUNDERLINE | | fieldNameMode | SequelizeStore.FieldNameMode | CAMELCASE、 UNDERLINE、 TUNDERLINE | | ... | | other Sequelize constructor options |

example:

const sequelizeStore = new SequelizeStore({
  dialect: 'mysql', // connect to mysql database
  host: 'localhost',
  port: 3306,
  database: 'noname',
  username: 'root',
  password: '999999',
  timezone: '+08:00',
  dialectOptions: { // if fields got a big number value
    supportBigNumbers: true,
    bigNumberStrings: true
  },
  tableNameMode: SequelizeStore.TableNameMode.TUNDERLINE, // table name as "t_my_table"
  fieldNameMode: SequelizeStore.FieldNameMode.FUNDERLINE // field name as "f_my_field"
})

define model

const store = new SequelizeStore(options)
const myService = new Henhouse(options)
const Model = myService.define(store, modelName, attributes, options)

Model

| member | Type | Description | | -------------- | --------------- | ----------------------------------- | | sequelizeModel | Sequelize.Model | raw sequelize model | | associations | [] | associated models | | methods | {} | group of functions via http request |

methods:

  • query(sequelizeOptions, id)
  • create(modelData)
  • update(modelData, sequelizeOptions, id)
  • remove(sequelizeOptions, id)

define sample

const Types = Henhouse.DataTypes

const sequelizeStore = new SequelizeStore({
  ...
})

const myService = new Henhouse({
  servicePath: 'my-service'
})
const User = myService.define(
  sequelizeStore, // store, like a data source
  'user', // model name
  { // fields section
    id: Types.ID,
    phoneNumber: Types.INT,
    loginId: Types.STRING,
    name: Types.STRING,
    isRemoved: {
      type: Types.BOOLEAN,
      queryByDefault: false // do not query this field by default
    },
    createdAt: { // sequelize createdAt feature
      type: Types.DATE,
      queryByDefault: false
    },
    updatedAt: { // sequelize At feature
      type: Types.DATE,
      queryByDefault: false
    }
  },
  {
    path: 'users', // model name + 's' by default
    idGenerator: idGen, // generate id value by a external asynchronous function
    readonly: false, // false by default, without create / update function when true
    removable: false, //false by default, without remove function when false
    httpMethods: { // auto bind routers to default access procedures of the model
      get: true, // bind get method, access by get->http://localhost/my-service/user
      post: true, // bind post method
      put: true, // bind put method
      patch: true // bind patch method
      delete: true, // bind delete method
    }
  }
)

with associations

const TenantUser = myService.define(
  sequelizeStore,
  'tenantUser',
  {
    id: Types.ID,
    tenant: {
      model: tenant
      // foreignKey: 'tenantId' by default
      // required: true by default, query table by inner join
    },
    person: {
      model: user,
      foreignKey: 'userId',
      required: false // query table by left outter join
    },
    userAlias: Types.STRING,
    isAdministrator: Types.BOOLEAN,
    isDisabled: Types.BOOLEAN,
    isRemoved: Types.BOOLEAN,
    createdAt: {
      type: Types.DATE,
      queryByDefault: false
    },
    updatedAt: {
      type: Types.DATE,
      queryByDefault: false
    }
  },
  {
    idGenerator: idGen,
    httpMethods: {
      get: true,
      post: true,
      patch: true
    }
  }
)

customized methods

const User = myService.define(sequelizeStore,
  'user',
  {
    ...
  },
  {
    httpMethods: {
      get: function (ctx, next, model, id) {
        // put your code here
        // id should exist when request '/users/:id'
      },
      post: {
        default: true, // access default create procedure when post through '/users'
        default: function (ctx, next, model, id) {
          // '/users/'
        },
        myPreset1: function (ctx, next, model, id) {
          // '/users?_preset=myPreset1'
        },
        myPreset2: function (ctx, next, model, id) {
          // '/users?_preset=myPreset2'
        },
      },
      
    }
  }

http request

query

  • query specified fields

    http://localhost/my-service/users?_fields=id,name
    http://localhost/my-service/users

    equals:

    http://localhost/my-service/users?_fields=*
  • query by conditions

    http://localhost/my-service/users?loginId=foo&name=*bar*
    select id, phone_number, login_id, name from user
    where login_id='foo' and name like '%bar%'
  • query by order

    http://localhost/my-service/users?_order=-id,name
    select id, phone_number, login_id, name from user
    where order by id desc, convert(name using gbk)
  • associations

    http://localhost/my-service/tenant-users?_fields=id,person.name,tenant.name&tenant.name=foobar&_order=-id,user.name
    select id,user.name,tenant.name from tenant_user
    inner join tenant on tenant_id=tenant.id
    left outer join user on user_id=user.id
    where tenant.name='foobar'
    order by id desc,convert(user.name using gbk)
  • query by preset options

    http://localhost/my-service/tenant-users?_preset=myPreset1

create

  • post (code by request)

    request({
      method: 'POST',
      url: 'http://localhost:3000/my-service/users',
      form: {
        name: 'sunzz',
        phoneNumber: 13051761351
      }
    })
  • batch post (code by request)

    request({
      method: 'POST',
      url: 'http://localhost:3000/my-service/users',
      form: [
        {
          name: 'sunzz',
          phoneNumber: 13051761351
        },
        {
          name: 'sunyy',
          phoneNumber: 13051761351
        }
      ]
    })

update

  • put / patch (code by request)

    request({
      method: 'put', // or 'patch'
      url: 'http://localhost:3000/my-service/users/1',
      form: {
        name: 'sunxx'
      }
    })
  • batch put/patch (code by request)

    request({
      method: 'put', // or 'patch'
      url: 'http://localhost:3000/my-service/users',
      form: [
        {
          id: 1,
          name: 'sunxx'
        },
        {
          id: 2,
          name: 'sunyy'
        }
      ]
    })

remove

  • delete (code by request)

    request({
      method: 'delete',
      url: 'http://localhost:3000/my-service/users/1'
    })