Giter VIP home page Giter VIP logo

deno_mysql's Introduction

deno_mysql

Build Status GitHub GitHub release (Deno)

MySQL and MariaDB (5.5 and 10.2+) database driver for Deno.

MariaDB 10.0 and 10.1 are not supported at the moment

On this basis, there is also an ORM library: Deno Simple Orm

欢迎国内的小伙伴加我专门建的 Deno QQ 交流群:698469316

TODO

  • Connecting to database
    • Retring
    • Timeout
  • Basic login authentication
  • Simple queries (no arguments)
  • Parsing data types
  • Queries with parameters
  • Close connection
  • Connection pool
  • Transaction
  • Test case
  • Support MariaDB 10.0
  • Support MariaDB 10.1
  • Support caching_sha2_password auth plugin (mysql8 default)

API

connect

import { Client } from "https://deno.land/x/mysql/mod.ts";
const client = await new Client().connect({
  hostname: "127.0.0.1",
  username: "root",
  db: "dbname",
  password: "password",
});

connect pool

Create client with connection pool.

pool size is auto increment from 0 to poolSize

import { Client } from "https://deno.land/x/mysql/mod.ts";
const client = await new Client().connect({
  hostname: "127.0.0.1",
  username: "root",
  db: "dbname",
  poolSize: 3, // connection limit
  password: "password",
});

create database

await client.execute(`CREATE DATABASE IF NOT EXISTS enok`);
await client.execute(`USE enok`);

create table

await client.execute(`DROP TABLE IF EXISTS users`);
await client.execute(`
    CREATE TABLE users (
        id int(11) NOT NULL AUTO_INCREMENT,
        name varchar(100) NOT NULL,
        created_at timestamp not null default current_timestamp,
        PRIMARY KEY (id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`);

insert

let result = await client.execute(`INSERT INTO users(name) values(?)`, [
  "manyuanrong",
]);
console.log(result);
// { affectedRows: 1, lastInsertId: 1 }

update

let result = await client.execute(`update users set ?? = ?`, ["name", "MYR"]);
console.log(result);
// { affectedRows: 1, lastInsertId: 0 }

delete

let result = await client.execute(`delete from users where ?? = ?`, ["id", 1]);
console.log(result);
// { affectedRows: 1, lastInsertId: 0 }

query

const username = "manyuanrong";
const users = await client.query(`select * from users`);
const queryWithParams = await client.query(
  "select ??,name from ?? where id = ?",
  ["id", "users", 1]
);
console.log(users, queryWithParams);

transaction

const users = await client.transaction(async (conn) => {
  await conn.execute(`insert into users(name) values(?)`, ["test"]);
  return await conn.query(`select ?? from ??`, ["name", "users"]);
});
console.log(users.length);

close

await client.close();

deno_mysql's People

Contributors

0xflotus avatar alexsorokoletov avatar delwiv avatar halvardssm avatar jovidecroock avatar magichim avatar manyuanrong avatar mariusvatasoiu avatar t-yng avatar uki00a avatar xvrzhao avatar zhmushan 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.