|
| 1 | +describe('sqlx-promise', function() { |
| 2 | + |
| 3 | +it('mysql async/await', async () => { |
| 4 | + child_process.execSync('mysql -uroot < test/mysql_initdb.txt') |
| 5 | + const client = sqlx.createClient() |
| 6 | + client.define('*', MYSQL_CONFIG_1) |
| 7 | + |
| 8 | + const conn = client.getConnection(OPERATER_INFO) |
| 9 | + let result |
| 10 | + // insert |
| 11 | + await conn.insert( |
| 12 | + 'table1', |
| 13 | + [ |
| 14 | + {a:1, b:21}, |
| 15 | + {a:2, b:22}, |
| 16 | + {a:3, b:23}, |
| 17 | + {a:3, b:123}, |
| 18 | + ]) |
| 19 | + // select |
| 20 | + result = await conn.select('table1', '*', { $and: { a: 3, b: 123 } }) |
| 21 | + assert.equal(result.rows.length, 1) |
| 22 | + // update |
| 23 | + result = await conn.update('table1', {b:1}, {a:3}) |
| 24 | + assert.equal(result.rows.affected_rows, 2) |
| 25 | + assert.equal(result.rows.changed_rows, 2) |
| 26 | + // selectEx |
| 27 | + result = await conn.selectEx('table1', 'select * from table1 where a = ?', [2]) |
| 28 | + assert.equal(result.rows.length, 1) |
| 29 | + assert.equal(result.rows[0].b, 22) |
| 30 | + // delete |
| 31 | + result = await conn.delete('table1', {a:3}) |
| 32 | + assert.equal(result.rows.affected_rows, 2) |
| 33 | + conn.release() |
| 34 | +}) |
| 35 | + |
| 36 | +it('redis async/await', async () => { |
| 37 | + const client = sqlx.createClient() |
| 38 | + client.define('budget', {type: 'redis', config: REDIS_CONIFG}) |
| 39 | + |
| 40 | + const conn = client.getConnection(OPERATER_INFO) |
| 41 | + let result |
| 42 | + // insert |
| 43 | + result = await conn.insert('budget', {key1: 'value1'}) |
| 44 | + assert.equal(result.rows, 'OK') |
| 45 | + // select |
| 46 | + result = await conn.select('budget', '*', {key1: 1}) |
| 47 | + assert.equal(result.rows, 'value1') |
| 48 | + // update |
| 49 | + result = await conn.update('budget', {key1: 'value2'}, {key1: 1}) |
| 50 | + assert.equal(result.rows, 'OK') |
| 51 | + result = await conn.select('budget', '*', {key1: 1}) |
| 52 | + assert.equal(result.rows, 'value2') |
| 53 | + // delete |
| 54 | + result = await conn.delete('budget', {key1: 1}) |
| 55 | + assert.equal(result.rows, 1) |
| 56 | + result = await conn.select('budget', '*', {key1: 1}) |
| 57 | + assert.equal(result.rows, null) |
| 58 | + conn.release() |
| 59 | +}) |
| 60 | +}) |
| 61 | + |
| 62 | + |
| 63 | +const assert = require('assert') |
| 64 | +const async = require('async') |
| 65 | +const sqlx = require('..') |
| 66 | +const child_process = require('child_process') |
| 67 | +const _ = require('lodash') |
| 68 | + |
| 69 | +// mysql |
| 70 | +const MYSQL_CONFIG_1 = { |
| 71 | + type: 'mysql', |
| 72 | + config: { |
| 73 | + connectionLimit: 1, |
| 74 | + host: '127.0.0.1', |
| 75 | + user: 'root', |
| 76 | + password: '', |
| 77 | + //debug: ['ComQueryPacket'], |
| 78 | + database: 'sqlx_mysql', |
| 79 | + } } |
| 80 | + |
| 81 | +// redis |
| 82 | +const REDIS_CONIFG = { |
| 83 | + host: '127.0.0.1', |
| 84 | + port: '6379', |
| 85 | +} |
| 86 | + |
| 87 | +// sqlx operater |
| 88 | +const OPERATER_INFO = { |
| 89 | + user: '101,23', |
| 90 | + actions: '*', |
| 91 | +} |
0 commit comments