0.10.4 • Published 13 days ago

@axiosleo/orm-mysql v0.10.4

Weekly downloads
-
License
MIT
Repository
github
Last release
13 days ago

@axiosleo/orm-mysql

NPM version npm download node version License FOSSA Status

Installation

npm install @axiosleo/orm-mysql

Usage

Create MySQL client

const { createClient } = require("@axiosleo/orm-mysql");

const client = createClient({
  host: process.env.MYSQL_HOST,
  port: process.env.MYSQL_PORT,
  user: process.env.MYSQL_USER,
  password: process.env.MYSQL_PASS,
  database: process.env.MYSQL_DB,
});

Initialize database handler

const { QueryHandler } = require("@axiosleo/orm-mysql");

const db = new QueryHandler(client);

Initialize query

const query = db.table('<table-name>');

query.attr("id", "name", "age"); // set attributes
query.where("name", "Joe");      // set where condition
query.orWhere("age", ">", 18);   // set or where condition
query.andWhere("age", "<", 30);  // set and where condition
query.orderBy("age", "desc");    // set order by
query.limit(10);                 // set limit
query.offset(0);                 // set offset

let rows = await query.select(); // select

Some Query Examples

const { createClient, QueryHandler, Query } = require("@axiosleo/orm-mysql");

const conn = createClient({
  host: process.env.MYSQL_HOST,
  port: process.env.MYSQL_PORT,
  user: process.env.MYSQL_USER,
  password: process.env.MYSQL_PASS,
  database: process.env.MYSQL_DB,
});

const hanlder = new QueryHandler(conn);

async function selectExample() {
  const query = handler.table("users"); // init QueryOperator by table name

  query.attr("id", "name", "age"); // set attributes
  query.where("name", "Joe");      // set where condition
  query.orWhere("age", ">", 18);   // set or where condition
  query.andWhere("age", "<", 30);  // set and where condition
  query.orderBy("age", "desc");    // set order by
  query.limit(10);                 // set limit
  query.offset(0);                 // set offset

  let rows = await query.select(); // select
}

async function findExample() {
  const query = handler.table("users"); // init QueryOperator by table name

  query.attr("id", "name", "age");      // set attributes
  query.where("name", "Joe");           // set where condition
  query.orWhere("age", ">", 18);        // set or where condition
  query.andWhere("age", "<", 30);       // set and where condition
  query.orderBy("age", "desc");         // set order by
  // query.limit(10);                   // not supported set limit
  // query.offset(10);                  // not supported set offset

  let row = await query.find();         // find single row
}

async function insertExample() {
  const query = handler.table("users");

  // insert
  let row = await query.insert({
    name: "Joe",
    age: 18,
  });

  // The insert operation will be changed to the update operation if the uuid already exists
  row = await query.keys('uuid').insert({
    uuid: 'uuid-string', // uuid is unique index
    name: "Joe",
    age: 18,
  })
}

async function updateExample() {
  const query = handler.table("users");

  // update
  let row = await query.where("name", "Joe").update({
    name: "Joe",
    age: 18,
  });
}

async function deleteExample() {
  const query = handler.table("users");

  // delete with conditions
  let result = await query.where("name", "Joe").delete();

  // delete by id
  result = await query.delete(1);
}

async function subqueryExample() {
  const query = handler.table("users", "u");
  const subQuery = new Query("select");
  subQuery.table("users").having("COUNT(*)", ">", 1);

  const sql = query.where("u.name", subQuery, "IN").buildSql("select").sql;
  // SELECT * FROM `users` AS `u` WHERE `u`.`name` IN (SELECT * FROM `users` GROUP BY `u`.`name` HAVING COUNT(*) > ?)
}

Hook

const { Hook } = require("@axiosleo/orm-mysql");

// opt: 'select' | 'find' | 'insert' | 'update' | 'delete' | 'count'

Hook.pre(async (options) => {
  debug.log('options', options);
}, { table: 'table_name', opt: 'insert'});

Hook.post(async (options, result) => {
  throw new Error('some error');
}, { table: 'table_name', opt: 'insert' });

Transaction

const { TransactionHandler, createPromiseClient } = require("@axiosleo/orm-mysql");

const conn = await createPromiseClient({
  host: process.env.MYSQL_HOST,
  port: process.env.MYSQL_PORT,
  user: process.env.MYSQL_USER,
  password: process.env.MYSQL_PASS,
  database: process.env.MYSQL_DB,
});

const transaction = new TransactionHandler(conn, {
  /*
  level = 'READ UNCOMMITTED' | 'RU'
        | 'READ COMMITTED' | 'RC'
        | 'REPEATABLE READ' | 'RR'
        | 'SERIALIZABLE' | 'S'
  */
  level: "SERIALIZABLE", // 'SERIALIZABLE' as default value
});
await transaction.begin();

try {
  // insert user info
  // will not really create a record.
  let row = await transaction.table("users").insert({
    name: "Joe",
    age: 18,
  });
  const lastInsertId = row[0].insertId;

  // insert student info
  await transaction.table("students").insert({
    user_id: lastInsertId,
  });
  await transaction.commit();
} catch (e) {
  await transaction.rollback();
  throw e;
}

Migration

Migration examples.

  • Migration script example
'use strict';

/**
 * @param {import('@axiosleo/orm-mysql').MigrationInterface} migration
 */
function up(migration) {
  migration.createTable('table1', {
    field1: {
      type: 'varchar',
      length: 64,
      allowNull: false,
      uniqIndex: true
    },
    field2: {
      type: 'VARCHAR',
      allowNull: false
    },
    field3: {
      type: 'VARCHAR',
      comment: 'comment',
      allowNull: false
    },
  });
}

/**
 * @param {import('@axiosleo/orm-mysql').MigrationInterface} migration
 */
function down(migration) {
  migration.dropTable('table1');
}

module.exports = {
  up,
  down
};
  • Generate migration script
orm-mysql generate -h

Usage:

  generate [--] [name] <dir>
  gen

Arguments:

 *name    Migration name
  dir     Migration scripts directory
  • Run migration
orm-mysql migrate -h

Description:

  Migrate database

Usage:

  migrate [options] [--] [action] <dir>

Options:

  -d, --debug    [false] debug mode
  --host         [localhost] mysql host
  --port         [3306] port number to connect to the database
  --user         [root] username for connect to the database
  --pass         password to connect to the database
  --db           database name

Arguments:

 *action         up or down
  dir            migration directory

Custom query driver

const { 
  createClient, 
  QueryHandler, 
  Query,
  Builder 
} = require("@axiosleo/orm-mysql");

const conn = createClient({
  host: process.env.MYSQL_HOST,
  port: process.env.MYSQL_PORT,
  user: process.env.MYSQL_USER,
  password: process.env.MYSQL_PASS,
  database: process.env.MYSQL_DB,
});

const hanlder = new QueryHandler(conn, {
  driver: 'custom',
  queryHandler: (con, options) => {
    const builder = new Builder(options);
    return new Promise((resolve, reject) => {
      if (options.operator === 'select') {
        resolve([{ a: 1, b: 2 }]);
      } else {
        reject(new Error('some error'));
      }
    });
  }
});

License

This project is open-sourced software licensed under MIT.

FOSSA Status

0.10.4

13 days ago

0.10.3

24 days ago

0.10.1

1 month ago

0.10.2

1 month ago

0.10.0

1 month ago

0.10.0-alpha

1 month ago

0.9.16

2 months ago

0.9.17

2 months ago

0.9.14

2 months ago

0.9.15

2 months ago

0.9.13

2 months ago

0.9.12

2 months ago

0.9.11

2 months ago

0.9.11-alpha.3

2 months ago

0.9.11-alpha.2

2 months ago

0.9.11-alpha.4

2 months ago

0.9.11-alpha.1

2 months ago

0.9.10

2 months ago

0.9.9

2 months ago

0.9.8

3 months ago

0.9.7

3 months ago

0.9.6

3 months ago

0.9.4

3 months ago

0.9.3

3 months ago

0.9.5

3 months ago

0.9.2

3 months ago

0.9.1

3 months ago

0.9.0

3 months ago

0.8.6

5 months ago

0.8.5

5 months ago

0.8.4

5 months ago

0.8.3

5 months ago

0.8.1

6 months ago

0.6.3

8 months ago

0.8.0

7 months ago

0.6.2

8 months ago

0.5.6

10 months ago

0.7.0

8 months ago

0.6.1

8 months ago

0.6.0

8 months ago

0.5.5

1 year ago

0.5.4

1 year ago

0.5.3

1 year ago

0.5.2

1 year ago

0.5.0

1 year ago

0.5.1

1 year ago

0.4.0

2 years ago

0.3.0

2 years ago

0.2.1

2 years ago

0.2.3

2 years ago

0.3.1

2 years ago

0.2.2

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.1.0-alpha.4

2 years ago

0.1.0-alpha.3

2 years ago

0.1.0-alpha.2

2 years ago

0.1.0-alpha.1

2 years ago

0.0.1

2 years ago