- Notifications
You must be signed in to change notification settings - Fork0
nowzoo/mysql
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
MySQL and MariaDB database driver for Deno.
On this basis, there is also an ORM library:Deno Simple Orm
欢迎国内的小伙伴加我专门建的 Deno QQ 交流群:698469316
import{Client}from"https://deno.land/x/mysql/mod.ts";constclient=awaitnewClient().connect({hostname:"127.0.0.1",username:"root",db:"dbname",password:"password",});
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",});
awaitclient.execute(`CREATE DATABASE IF NOT EXISTS enok`);awaitclient.execute(`USE enok`);
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;`);
letresult=awaitclient.execute(`INSERT INTO users(name) values(?)`,["manyuanrong",]);console.log(result);// { affectedRows: 1, lastInsertId: 1 }
letresult=awaitclient.execute(`update users set ?? = ?`,["name","MYR"]);console.log(result);// { affectedRows: 1, lastInsertId: 0 }
letresult=awaitclient.execute(`delete from users where ?? = ?`,["id",1]);console.log(result);// { affectedRows: 1, lastInsertId: 0 }
constusername="manyuanrong";constusers=awaitclient.query(`select * from users`);constqueryWithParams=awaitclient.query("select ??,name from ?? where id = ?",["id","users",1],);console.log(users,queryWithParams);
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).
constusers=awaitclient.transaction(async(conn)=>{awaitconn.execute(`insert into users(name) values(?)`,["test"]);returnawaitconn.query(`select ?? from ??`,["name","users"]);});console.log(users.length);
awaitclient.close();
The driver logs to the console by default.
To disable logging:
import{configLogger}from"https://deno.land/x/mysql/mod.ts";awaitconfigLogger({enable:false});
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
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Languages
- TypeScript100.0%