1.0.3-beta-3 • Published 2 years ago

zihorm v1.0.3-beta-3

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

zihorm

zihorm is a tiny promise-based Node.js ORM tool for MySQL. This is an extremely experimental project, so expect things to break!

Installation

npm install zihorm

then

const { Zihorm } = require('zihorm');

Usage

Connect Database

const zihorm = new Zihorm('host', 'database', 'user', 'password');

Define Models

const { DataTypes } = require('zihorm');

const User = zihorm.define('user', {
  id: {
    field: 'id',
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
  },
  name: {
    field: 'name',
    type: DataTypes.STRING(10),
  },
  phone: {
    field: 'phone',
    type: DataTypes.STRING(13),
  },
});

Persist And Query

const result = await User.create({
  name: 'jiho',
  phone: '01012345678',
});

const createUser = async () => {
  await User.create({
    name: 'jiho',
  });
};

const getUser = async () => {
  const users = await User.findAll({
    where: {
      name: 'jiho',
    },
    order: ['id', 'DESC'],
  });
};

const updateUser = async () => {
  await User.update(
    { name: 'jiho2' },
    {
      where: {
        id: 2,
      },
    },
  );
};

const deleteUser = async () => {
  const response = await User.deleteOne({
    where: {
      id: 4,
    },
  });
};

Raw Query (Recommend)

const [data, field] = await zihorm.query('select * from user');

DataTypes

DataTypes.STRING(Number);
DataTypes.INTEGER;
DataTypes.DATE;
DataTypes.BOOLEAN;

Op

Op.eq : '='
Op.lt : '<'
Op.lte : '<='
Op.gt : '>'
Op.gte : '>='

options

define Model option

optiontyperequireddescription
fieldstringtruefield name (column name)
typeDataTypestruefield's type
uniquebooleanfalse(Optional)field unique option
notNullbooleanfalse(Optional) field not null option
autoIncrementbooleanfalse(Optional) field auto_increment option
primaryKeybooleanfalse(Optional) field primary key option

find option

optiontypedefaultdescription
attributesarray*select fields' name (column name)
whereObjectnullfilter record option
order[string] or [string, 'ASC' or 'DESC][string , 'ASC]order records
include{pk: string, joinPk: string, model: string}nullinner join
groupBystringnullgroup by field
1.0.3-beta-3

2 years ago

1.0.3-beta-2

2 years ago

1.0.3-beta-1

2 years ago

1.0.3-beta

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago