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

ah-bookshelf-plugin

v0.2.1

Published

Bookshelf plugin for actionhero

Downloads

18

Readme

ah-bookshelf-plugin

Bookshelf plugin for actionhero

Build Status NPM package Dependency Status devDependency Status License

Description

Features

Install

npm install --save ah-bookshelf-plugin
# Then add one of the following:
npm install --save pg
npm install --save mysql
npm install --save mariasql
npm install --save sqlite3

Be sure to enable the plugin within actionhero. Add the "ah-bookshelf-plugin" to config/plugins.js.

// config/plugins.js

"use strict";

module.exports = {
  "default": {
    general: function(api) {
      return {
        plugins: [
          "ah-bookshelf-plugin"
        ]
      };
    }
  },
  //...
}};

Be sure to enable the task for database. Add the require("ah-bookshelf-plugin/grunt")(grunt); to gruntfile.js.

// gruntfile.js

"use strict";

var grunt = require("grunt")
require("actionhero/grunt")(grunt);

// ah-bookshelf-plugin
require("ah-bookshelf-plugin/grunt")(grunt);

Configuration

After installation, bookshelf.js is copied to the config/plugins directory. Please correct.

// config/plugins/bookshelf.js

"use strict";

module.exports = {
  "default": {
    bookshelf: function(api) {
      return {
        // http://knexjs.org/#Installation-debug
        debug: true,

        // http://knexjs.org/#Installation-client
        client: "postgres",
        connection: {
          host: "127.0.0.1",
          port: 5432,
          user: "postgres",
          password: "postgres",
          database: "db_development",
          charset: "utf8"
        },

        // http://knexjs.org/#Installation-migrations
        migrations: {
          tableName: "knex_migrations",
          directory: api.projectRoot + "/database/migrations"
        },

        // http://knexjs.org/#Seeds-API
        seeds: {
          directory: api.projectRoot + "/database/seeds"
        },

        // models directory
        models: {
          directory: api.projectRoot + "/models"
        }
      };
    }
  },
  //...
};

Model

After installation, base.js is copied to the models directory. Please describe the basic class of models and collections to base.js. Model is allowed to inherit the base class, and add to api.models.

// models/base.js

"use strict";

module.exports = function(api) {

  // Base Model
  api.bookshelf.Model = api.bookshelf.Model.extend({

    hasTimestamps: true
    //...

  }, {

    find: function(id, options) {
      if (options == null) { options = {} }
      if (!options.require) { options.require = true }
      return this.forge({id: id}).fetch(options);
    }
    //...

  });


  // Base Collection
  api.bookshelf.Collection = api.bookshelf.Collection.extend({

    model: api.bookshelf.Model

  });

};
// models/user.js

"use strict";

module.exports = function(api) {

  // User Model
  api.models.User = api.bookshelf.Model.extend({

    tableName: "users"
    //...

  }, {

    findByUsername: function(username, options) {
      if (options == null) { options = {} }
      if (!options.require) { options.require = true }
      return this.forge({username: username}).fetch(options);
    }
    //...

  });


  // User Collection
  api.models.Users = api.bookshelf.Collection.extend({

    model: api.models.User

  });

};

Task

Create or delete a database, the execution of the migration, the creation of the initial data.

// grunt --help

              db:version  Show the current migration version
             db:rollback  Rollback the database
         db:migrate:make  Make migrate file (:name)
              db:migrate  Migrate the database
            db:seed:make  Make seed file (:name)
                 db:seed  Create the seed data
               db:create  Create the database
                 db:drop  Drop the database
        db:migrate:reset  Runs db:drop db:create db:migrate
                db:reset  Runs db:migrate:reset db:seed

Migration

Using the database task to create a migration file.

grunt db:migrate:make:create_users

Running "db:migrate:make:users" (db:migrate:make) task
>> Make /path/to/project/database/migrations/20150210164757_create_users.js

Please edit.

// database/migrations/20150210164757_create_users.js

"use strict";

exports.up = function(knex, Promise) {
  return knex.schema.createTable("users", function(t) {
    t.increments();
    t.string("username");
    t.timestamps();
  });
};

exports.down = function(knex, Promise) {
  return knex.schema.dropTable("users");
};

Please run migrate task.

grunt db:migrate

Seed

Using the database task to create a seed file.

grunt db:seed:make:users

Running "db:seed:make:users" (db:seed:make) task
>> Make /path/to/project/database/seeds/users.js

Please edit.

// database/seeds/users.js

"use strict";

exports.seed = function(knex, Promise) {
  return knex("users").insert({
    username: "ah-bookshelf-plugin",
    created_at: new Date(),
    updated_at: new Date()
  });
};

Please run seed task.

grunt db:seed

Usage

The api is exposed in api.bookshelf and api.models object. api.bookshelf is an instance of the bookshelf.

// actions/users.js

"use strict";

exports.index = {
  name: "users.index",
  description: "users.index",
  run: function(api, connection, next){
    api.models.User.fetchAll()
    .then(function(users) {
      connection.response.users = users;
      next(connection, true);
    })
    .catch(function(err) {
      connection.error = err;
      next(connection, true);
    });
  }
};