1.7.3 • Published 5 years ago

dbexecutors v1.7.3

Weekly downloads
13
License
MIT
Repository
github
Last release
5 years ago

Introduction

What is this?

Database manipulations can be very boring, you need to create a connection, do CRUD by sending commands to database server, close connection, re-establish the broken connection... And with Node.js, you also need to listen to those network events and deal with a lot of callbacks.

A connection pool could make it easier, but most Node.js connection pool packages didn't give us the async/await interface, and you still need to release the connection after you use it. Sometimes I just want a function that helps me execute a sql string, or a redis command. I don't want to care about the network connection stuffs, or listen to any network events.

With this package, you don't need to do those boring things anymore, dbexecutors give you a simple Promise-based interface, and do all those necessary things underground.

Basic Usage

For example, with MysqlExecutor, you just need to create a executor first(tell it which server to connect):

const executor = dbexecutors.getMysqlExecutor({
  host: "localhost",
  port: 3306,
  password: "asdf",
  //...
})

Then you can use it like this:

(async function() {

//...
await executor.insert("t1", { name: "x", age: 6, gender: 1 })
// You can also write:
await executor.execute('INSERT INTO t1(name, age) VALUES("x", 6)')

//...
const r = await executor.select("t1", { name: "x" })
//...


})().catch(console.error)

RedisExecutor works the same way.

const executor = dbexecutors.getRedisExecutor({
  host: "localhost",
  port: 6379,
  password: "asdf",
})
(async function() {

//...
await executor.execute([ "set", "test_string", "hello, redisexecutor" ])
//...
const r = await executor.execute([ "get", "test_string" ])
//...


})().catch(console.error)

Transaction

For redis, you don't need transaction, you can and should use redis script instead. These are some contents you can find in https://redis.io/topics/transactions

A Redis script is transactional by definition, so everything you can do with a Redis transaction, you can also do with a script, and usually the script will be both simpler and faster.

Actually Redis may remove transaction in the future and use redis script only.

However it is not impossible that in a non immediate future we'll see that the whole user base is just using scripts. If this happens we may deprecate and finally remove transactions.

For mysql, you do need transaction. In this case, you need to get connection from the connection pool and release it after you finishing your query.

(async function() {

const conn = await executor.getConnection()

await conn.transactionStart()

try {
  await conn.execute(`update t1 set age=26 where name="A"`)
  await conn.execute(`update t1 set age=27 where name="B"`)
  await conn.transactionCommit()
} catch (e) {
  await conn.transactionRollback()
}

conn.release()

})().catch(console.error)
1.7.3

5 years ago

1.7.2

5 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.6.2

6 years ago

1.6.1

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.3

6 years ago

1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.8

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.9.10

6 years ago

0.9.9

6 years ago

0.9.8

7 years ago

0.9.7

7 years ago

0.9.6

7 years ago

0.9.5

7 years ago

0.9.4

7 years ago

0.9.3

7 years ago

0.9.2

7 years ago

0.9.1

7 years ago

0.9.0

7 years ago

0.8.5

7 years ago

0.8.4

7 years ago

0.8.3

7 years ago

0.8.2

7 years ago

0.8.0

7 years ago

0.7.0

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago