Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

MySQL driver for Deno

License

NotificationsYou must be signed in to change notification settings

nowzoo/mysql

 
 

Repository files navigation

Build StatusGitHubGitHub release(Deno)

MySQL and MariaDB database driver for Deno.

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

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

API

connect

import{Client}from"https://deno.land/x/mysql/mod.ts";constclient=awaitnewClient().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 topoolSize

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

create database

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

create table

awaitclient.execute(`DROP TABLE IF EXISTS users`);awaitclient.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

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

update

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

delete

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

query

constusername="manyuanrong";constusers=awaitclient.query(`select * from users`);constqueryWithParams=awaitclient.query("select ??,name from ?? where id = ?",["id","users",1],);console.log(users,queryWithParams);

execute

There are two ways to execute an SQL statement.

First and default one will return you anrows key containing an array of rows:

const{rows:users}=awaitclient.execute(`select * from users`);console.log(users);

The second one will return you aniterator key containing an[Symbol.asyncIterator] property:

awaitclient.useConnection(async(conn)=>{// note the third parameter of execute() method.const{iterator:users}=awaitconn.execute(`select * from users`,/* params: */[],/* iterator: */true,);forawait(constuserofusers){console.log(user);}});

The second method is recommended only for SELECT queries that might contain manyresults (e.g. 100k rows).

transaction

constusers=awaitclient.transaction(async(conn)=>{awaitconn.execute(`insert into users(name) values(?)`,["test"]);returnawaitconn.query(`select ?? from ??`,["name","users"]);});console.log(users.length);

close

awaitclient.close();

Logging

The driver logs to the console by default.

To disable logging:

import{configLogger}from"https://deno.land/x/mysql/mod.ts";awaitconfigLogger({enable:false});

Test

The tests require a database to run against.

docker container run --rm -d -p 3306:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=true docker.io/mariadb:latestdenotest --allow-env --allow-net=127.0.0.1:3306 ./test.ts

Use different docker images to test against different versions of MySQL andMariaDB. Please seeci.yml for examples.

About

MySQL driver for Deno

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript100.0%

[8]ページ先頭

©2009-2025 Movatter.jp