New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@northernv/supermodel

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@northernv/supermodel

A Base model for Knex

  • 1.4.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@northernv/supermodel

npm MIT licensed

Completely inspired by knex-supermodel, just altered with some personal modifications to meet my needs

Usage

npm install -S @northernv/supermodel;
yarn add @northernv/supermodel;

In your models, that are Javascript classes, just extend with the main class

const { default: Base } = require('@northernv/supermodel')

class User extends Base {
}

Description

@northernv/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!

This package requires ES6 features only available in node 6+.

Benefits

  • Converts camelCase into snake_case for inserts / updates
  • Converts snake_case to camelCase automatigically

Static Examples

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

const { default: Base } = require('@northernv/supermodel')
const knex = require('knex')
const db = knex({ ...knexOptions })

class User extends Base {
  constructor(opts) {
    super(opts);
  }
}

// Note: It converts properties to snake_case
const user = User.create({ fooBar: 'hello', barBaz: 'baz' }, { db });

console.log(user.fooBar); // hello
console.log(user.foo_bar); // hello
console.log(user.barBaz); // baz

Transacting

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

let user;

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

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

License

This software is licensed under the MIT license.

FAQs

Package last updated on 11 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc