Code Monkey home page Code Monkey logo

lia-mysql's Introduction

Mysql Installer

Simplify the use of MySQL

Installer

Using npm:

$ npm install lia-mysql

How to Use MysqlInstaller

1.1 Mysql Initial (single mode)

import {MysqlInstaller,MysqlConfig} from "core-mysql";
const TARGET = {SQL: {}};
const config = {
    host: "127.0.0.1",
    port: 3306,
    user: "root",
    password: "root12345",
    database: "test"
}
const installer = new MysqlInstaller(config as MysqlConfig,TARGET,'sys|info');
await installer.load();

1.2 Mysql Use (single mode)

query

const result = await TARGET.SQL.query('select * from users where id=?', [100])
console.log(result);

Insert

const row = {
  name: 'fengmk2',
  otherField: 'other field value',
};
const result = await TARGET.SQL.insert('table-name', row);
console.log(result);
  • Insert multi rows
const rows = [{
    name: 'fengmk2',
    otherField: 'other field value',
}, {
    name: 'fengmk3',
    otherField: 'other field value',
}];
const result = await TARGET.SQL.insert('table-name', rows);
console.log(result);

Update

  • Update a row with primary key: id
const row = {
  id: 123,
  name: 'fengmk2',
  otherField: 'other field value',
};
const result = await TARGET.SQL.update('table-name', row);
console.log(result);
  • Update a row with options.where and options.columns
const row = {
  name: 'fengmk2',
  otherField: 'other field value',
};
const result = await TARGET.SQL.update('table-name', row, {
  where: { name: row.name },
  columns: [ 'otherField' ]
});
console.log(result);

Update multiple rows

  • Update multiple rows with primary key: id
const options = [{
  id: 123,
  name: 'fengmk2',
  email: '[email protected]',
  otherField: 'other field value',
}, {
   id: 124,
  name: 'fengmk2_2',
  email: 'm@fengmk2_2.com',
  otherField: 'other field value 2',
}]
const result = await TARGET.SQL.updateRows('table-name', options);
console.log(result);
  • Update multiple rows with row and where properties
const options = [{
  row: {
    email: '[email protected]',
    otherField: 'other field value',
  },
  where: {
    id: 123,
    name: 'fengmk2',
  }
}, {
  row: {
    email: 'm@fengmk2_2.com',
    otherField: 'other field value2',
  }, 
  where: {
    id: 124,
    name: 'fengmk2_2',
  }
}]
const result = await TARGET.SQL.updateRows('table-name', options);
console.log(result);

Get

  • Get a row
const row = await TARGET.SQL.get('table-name', { name: 'fengmk2' });

=> SELECT * FROM `table-name` WHERE `name` = 'fengmk2'

2.1 Mysql Initial (multi mode)

import {MysqlInstaller,MysqlConfig,MysqlConfigs} from "core-installer";
const TARGET = {SQL: {}};
const config = {
    APP1:{
        host: "127.0.0.1",
        port: 3306,
        user: "root",
        password: "root12345",
        database: "test"
    }
}
const installer = new MysqlInstaller(config as MysqlConfigs<MysqlConfig>,TARGET,'sys|info');
await installer.load();

2.2 Mysql Use (multi mode)

The multi-instance mode is basically the same as the single-instance mode, except that the database instance needs to be specified on the SQL instance, for example:

query

const result = await TARGET.SQL.APP1.query('select * from users where id=?', [100])
console.log(result);

For other operations, please refer to the single mode

lia-mysql's People

Contributors

sunlley avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.