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

knex-supermodel

v0.3.0

Published

A thin Knex wrapper that provides a small base model that can be extended and act like a lite ORM.

Downloads

143

Readme

knex-supermodel

circle coverage npm MIT licensed

Description

knex-supermodel is meant to be a very lite but not quite an ORM for knex. This is accomplished by providing a base model that is simply an ES6 class that you extend in your own models. You can override the provided methods or even add to them to make your own. Each method will always return your model back to you, except in the obvious case of collection!

This package requires ES6 features only available in node 6.

Static Examples

Each subclass will have automatic access to static methods to create, fetch, collection, forge, update and destroy.

Create

When creating, the provided object becomes the properties. After inserting into the database, an instantation of your class is returned.

let user;

User.create({ foo: 'bar' })
  .then((u) => {
    user = u;
  });

Fetching

When fetching, the provided object becomes the where clause with a limit of 1. This results in an instantaion of your class whose properties are loaded from the database.

let user;

User.fetch({ id: '123' })
  .then((u) => {
    user = u;
  });

Forging

For convenience each model has access to a static forge method. Under the hood it is only calling the constructor and returing an instantiation of your class.

const user = User.forge({ foo: 'bar', bar: 'baz' });

console.log(user.foo); // bar
console.log(user.bar); // baz

Update

The static method update accepts new properties and a knex where clause object and returns to you instantiations of your class. It will return an array if there is more than one, otherwise it just give you the model updated. This is usefull if you are updating by ID.

User.update({ foo: 'baz' }, { foo: 'bar' })
  .then((users) => {
    console.log(user[0].foo); // baz
    console.log(user[1].foo); // baz
    console.log(user[2].foo); // baz
  });

Destroy

The static method destroy accepts a knex where clause object to delete records.

User.destroy({ foo: 'bar' }) // Deletes all users where foo is bar
  .then((res) => {
    console.log(res); // 1
  });

Collection

When getting a collection, the provided object becomes the where clause. Each member in the collection is an instantation of your class.

let users;

User.collection({ foo: 'bar' })
  .then((u) => {
    users = u;
  });

Instance Examples

Saving

Any added properties will become part of the resultant query. By default, the method of saving is insert but you may provide update as well.

class User extends require('knex-supermodel') {
  constructor(opts) {
    super(opts);
  }
}

User.knex = knex;
const user = new User({ foo: 'bar', bar: 'baz' });

console.log(user.foo); // bar
console.log(user.bar); // baz

user.save(); // performs insert
user.save({ method: 'insert' }); // performs insert
user.save({ method: 'update' }); // performs update

Destroy

When deleting, it assumes you want to delete by id unless you previously have set keys on the static model. These keys, if present, will be used to uniquely identify the model for deletion.

User.fetch({ id: '123' })
  .then((user) => user.destroy())
  .then((res) => {
    console.log(res); // 1
  });

User.fetch({ id: '123' })
  .then((user) => user.destroy())
  .then((res) => {
    console.log(res); // 1
  });

User.keys = [ 'foo' ];
User.fetch({ foo: 'bar' })
  .then((user) => user.destroy())
  .then((res) => {
    console.log(res); // 1
  });

Transacting

You may either provide a transacting knex to each method or chain it.

let user;

knex.transaction((trx) => {
  User.fetch({ id: '123' }, { trx })
    .then((u) => {
      user = u;

      return user.update({ foo: 'baz' });
    });
});

knex.transaction((trx) => {
  user.transaction(trx)
    .update({ trx })
    .then((user) => {
      return user.update({ foo: 'baz' });
    });
});

License

This software is licensed under the MIT license.