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

@comodinx/sequelize

v0.1.1

Published

@comodinx/sequelize is an adapter promise-based Node.js ORM autodiscover. Based on sequelize 5

Downloads

19

Readme

Sequelize

@comodinx/sequelize is an adapter promise-based Node.js ORM autodiscover. Based on sequelize 5

Index

Download & Install

NPM

$ npm install @comodinx/sequelize

Source code

$ git clone https://gitlab.com/comodinx/sequelize.git
$ cd sequelize
$ npm install

How is it used?

Create new Database instance.

const { Database } = require('@comodinx/sequelize');
// For more information of Database connections. See: https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-constructor-constructor
const db = new Database({
  host: 'localhost',
  username: 'myuser',
  password: 'mypass',
  database: 'mydbname'
});

// Autodiscover models on database
db.discover().then(() => {
  return db.models.users.getById(1);
})

Simple query Execution

db.query(/* YOUR SQL QUERY */).then(/* array */ result => /*...*/).catch(/*...*/);

Query for one result

db.queryOne(/* YOUR SQL QUERY */).then(/* object|undefined */ result => /*...*/).catch(/*...*/);

Handle transaction

This function is automatically responsible for commit or rollback (as appropriate). The commit will be performed once the callback function received as an argument is finished. In case the callback function returns a promise, the commit will be made at the end of this promise. In case of any failure, a rollback will be performed automatically (even if the commit fails).

db.transaction(/* callback */ transaction => {
  return db
    .query(/* FIRST SQL QUERY */, { transaction })
    .then(/* array */ result => {
      return db.query(/* SECOND SQL QUERY */, { transaction }).then(/*...*/);
    });
})
.catch(/*...*/);

Check if database connection found

db.isAlive().then(/* boolean */ alive => /*...*/).catch(/*...*/);

// OR

db.ping().then(/* boolean */ alive => /*...*/).catch(/*...*/);

End connection

db.end().then(/*...*/).catch(/*...*/);

Create new Model instance.

const { DataTypes } = require('sequelize');
const { Model } = require('@comodinx/sequelize');
const model = new Model(/* model name */ 'users', /* model definition */ {
  columnA: {
      type: DataTypes.BOOLEAN,
      validate: {
        is: ['[a-z]','i'],
        max: 23,
        isIn: {
          args: [['en', 'zh']],
          msg: 'Must be English or Chinese'
        }
      },
      field: 'column_a'
  },
  columnB: DataTypes.STRING,
  columnC: 'MY VERY OWN COLUMN TYPE'
} /* , sequelize options */);

Find models

const options = {
  where: {
    id: 1,
    deleted_at: {
      $is: null
    }
  },
  order: [['id', 'DESC'], ['created_at', 'ASC']],
  offset: 10,
  limit: 10
};

model.find(options).then(/* array */ models => /*...*/).catch(/*...*/);

// ------------------------------------------------------------------------------------

const options = {
  fields: ['id', 'active'],
  where: {
    OR: {
      deleted_at: {
        $is: null,
        $gt: '2019-06-01 00:00:00'
      }
    }
  }
};

model.find(options).then(/* array */ models => /*...*/).catch(/*...*/);

Get by ID

model.getById(1).then(/* object|undefined */ model => /*...*/).catch(/*...*/);

Get one

const options = {
  where: {
    id: {
      $in: [1, 2, 3]
    }
  }
};

model.getOne(options).then(/* object|undefined */ model => /*...*/).catch(/*...*/);

Exist

const options = {
  where: {
    id: 1
  }
};

model.exist(options).then(/* boolean */ exist => /*...*/).catch(/*...*/);

Count

const options = {
  where: {
    active: 1
  }
};

model.count(options).then(/* number */ total => /*...*/).catch(/*...*/);

Insert new model

const data = {
  id: null,
  active: 1,
  created_at: model.literal('NOW()'),
  updated_at: null
};

model.create(data).then(/* object */ model => /*...*/).catch(/*...*/);

Update existing model

const data = {
  active: 0,
  updated_at: model.literal('NOW()')
};

model.update(data, /* ID value */ 1).then(/* object */ model => /*...*/).catch(/*...*/);

// Or update more rows

const data = {
  active: 1
};

model.update(data, /* All disactive rows */ 0, /* Reference field name */ 'active').then(/* array */ models => /*...*/).catch(/*...*/);

Delete model

model.destroy(/* ID value */ 1).then(/* number */ result => /*...*/).catch(/*...*/);

Handle transaction

This function is automatically responsible for commit or rollback (as appropriate). The commit will be performed once the callback function received as an argument is finished. In case the callback function returns a promise, the commit will be made at the end of this promise. In case of any failure, a rollback will be performed automatically (even if the commit fails).

model.transaction(/* callback */ transaction => {
  const options = {
    transaction,
    where: {
      username: 'myusername'
    }
  };

  return model
    .exist(options)
    .then(exist => {
      if (exist) return model.update(data, 'myusername', 'username', { transaction });
      return model.create(data, { transaction });
    })
    .then(result => model.getById(result.id, { transaction }));
})
.catch(/*...*/);

Tests

In order to see more concrete examples, I INVITE YOU TO LOOK AT THE TESTS :)

Run the unit tests

npm install
npm test