Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Simple and intuitive ORM for MySQL

![](https://img.shields.io/npm/v/like-mysql.svg) ![](https://img.shields.io/npm/dt/like-mysql.svg) ![](https://img.shields.io/badge/tested_with-tape-e683ff.svg) ![](https://img.shields.io/github/license/LuKks/like-mysql.svg)
![](https://img.shields.io/npm/v/like-mysql.svg) ![](https://img.shields.io/npm/dt/like-mysql.svg) ![](https://img.shields.io/github/license/LuKks/like-mysql.svg)

```javascript
const mysql = require('like-mysql')
Expand Down
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
"mysql2": "^2.3.3"
},
"devDependencies": {
"standard": "^16.0.4",
"tape": "^5.5.0"
"brittle": "^3.5.0",
"standard": "^16.0.4"
},
"scripts": {
"test": "standard && tape test.js",
"tape": "tape test.js",
"lint": "standard"
"test": "standard && brittle test.js"
},
"repository": {
"type": "git",
Expand Down
92 changes: 46 additions & 46 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const tape = require('tape')
const test = require('brittle')
const mysql = require('./')

const cfg = ['127.0.0.1:3305', 'root', 'secret', 'sys']

tape('able to connect', async function (t) {
test('able to connect', async function (t) {
const db = mysql(...cfg)

await db.ready()

await db.end()
})

tape('ready timeouts', async function (t) {
test('ready timeouts', async function (t) {
const db = mysql('127.0.0.1:1234', cfg[1], cfg[2], cfg[3])

try {
Expand All @@ -24,7 +24,7 @@ tape('ready timeouts', async function (t) {
await db.end()
})

tape('should not connect due unexisting database', async function (t) {
test('should not connect due unexisting database', async function (t) {
const db = mysql(cfg[0], cfg[1], cfg[2], 'database-not-exists')

try {
Expand All @@ -37,7 +37,7 @@ tape('should not connect due unexisting database', async function (t) {
await db.end()
})

tape('should not connect due wrong authentication', async function (t) {
test('should not connect due wrong authentication', async function (t) {
const db = mysql(cfg[0], cfg[1], 'wrong-password', cfg[3])

try {
Expand All @@ -50,7 +50,7 @@ tape('should not connect due wrong authentication', async function (t) {
await db.end()
})

tape('limited connections', async function (t) {
test('limited connections', async function (t) {
const db = mysql(...cfg, { connectionLimit: 1, waitForConnections: false })

await db.ready()
Expand All @@ -69,21 +69,21 @@ tape('limited connections', async function (t) {
await db.end()
})

tape('createDatabase() and dropDatabase()', async function (t) {
test('createDatabase() and dropDatabase()', async function (t) {
const db = mysql(...cfg)

await db.ready()

t.ok(await db.createDatabase('forex'))
t.notOk(await db.createDatabase('forex'))
t.absent(await db.createDatabase('forex'))

t.ok(await db.dropDatabase('forex'))
t.notOk(await db.dropDatabase('forex'))
t.absent(await db.dropDatabase('forex'))

await db.end()
})

tape('createDatabase() without any previous database', async function (t) {
test('createDatabase() without any previous database', async function (t) {
const db = mysql(cfg[0], cfg[1], cfg[2], '')

await db.ready()
Expand All @@ -94,7 +94,7 @@ tape('createDatabase() without any previous database', async function (t) {
await db.end()
})

tape('createTable() and dropTable()', async function (t) {
test('createTable() and dropTable()', async function (t) {
const db = mysql(...cfg)

await db.ready()
Expand All @@ -104,17 +104,17 @@ tape('createTable() and dropTable()', async function (t) {
}

t.ok(await db.createTable('users', columns))
t.notOk(await db.createTable('users', columns))
t.absent(await db.createTable('users', columns))

t.ok(await db.dropTable('users'))
t.notOk(await db.dropTable('users'))
t.absent(await db.dropTable('users'))

await db.end()
})

// + create complex tables

tape('insert() without autoincrement id', async function (t) {
test('insert() without autoincrement id', async function (t) {
const db = mysql(...cfg)

await db.ready()
Expand All @@ -132,7 +132,7 @@ tape('insert() without autoincrement id', async function (t) {
await db.end()
})

tape('insert() with autoincrement id', async function (t) {
test('insert() with autoincrement id', async function (t) {
const db = mysql(...cfg)

await db.ready()
Expand All @@ -150,7 +150,7 @@ tape('insert() with autoincrement id', async function (t) {
await db.end()
})

tape('insert()', async function (t) {
test('insert()', async function (t) {
const db = mysql(...cfg)

await db.ready()
Expand Down Expand Up @@ -184,7 +184,7 @@ tape('insert()', async function (t) {
await db.end()
})

tape('select()', async function (t) {
test('select()', async function (t) {
const db = mysql(...cfg)

await db.ready()
Expand All @@ -198,22 +198,22 @@ tape('select()', async function (t) {

const rows = await db.select('users')
t.ok(rows.length === 2)
t.deepEqual(rows[0], { username: 'joe', password: '123' })
t.deepEqual(rows[1], { username: 'bob', password: '456' })
t.alike(rows[0], { username: 'joe', password: '123' })
t.alike(rows[1], { username: 'bob', password: '456' })

const rows2 = await db.select('users', ['username'])
t.ok(rows2.length === 2)
t.deepEqual(rows2[0], { username: 'joe' })
t.deepEqual(rows2[1], { username: 'bob' })
t.alike(rows2[0], { username: 'joe' })
t.alike(rows2[1], { username: 'bob' })

const rows3 = await db.select('users', ['username'], 'LIMIT 1')
t.ok(rows3.length === 1)
t.deepEqual(rows3[0], { username: 'joe' })
t.alike(rows3[0], { username: 'joe' })

const findUsername = 'joe'
const rows4 = await db.select('users', ['password'], 'username = ?', findUsername)
t.ok(rows4.length === 1)
t.deepEqual(rows4[0], { password: '123' })
t.alike(rows4[0], { password: '123' })

const rows5 = await db.select('users', ['*'], 'ORDER BY username ASC')
t.ok(rows5.length === 2)
Expand All @@ -239,7 +239,7 @@ tape('select()', async function (t) {
await db.end()
})

tape('selectOne()', async function (t) {
test('selectOne()', async function (t) {
const db = mysql(...cfg)

await db.ready()
Expand All @@ -252,23 +252,23 @@ tape('selectOne()', async function (t) {
await db.insert('users', { username: 'bob', password: '456' })

const row = await db.selectOne('users', ['*'], 'username = ?', 'joe')
t.deepEqual(row, { username: 'joe', password: '123' })
t.alike(row, { username: 'joe', password: '123' })

const row2 = await db.selectOne('users', ['*'], 'ORDER BY username ASC')
t.deepEqual(row2, { username: 'bob', password: '456' })
t.alike(row2, { username: 'bob', password: '456' })

const row3 = await db.selectOne('users', ['*'], 'username = ? ORDER BY username ASC', 'joe')
t.deepEqual(row3, { username: 'joe', password: '123' })
t.alike(row3, { username: 'joe', password: '123' })

const row4 = await db.selectOne('users', ['*'], 'username = ?', 'random-username')
t.deepEqual(row4, undefined)
t.alike(row4, undefined)

await db.dropTable('users')

await db.end()
})

tape('exists()', async function (t) {
test('exists()', async function (t) {
const db = mysql(...cfg)

await db.ready()
Expand All @@ -284,14 +284,14 @@ tape('exists()', async function (t) {
t.ok(exists)

const exists2 = await db.exists('users', 'username = ?', 'random-username')
t.notOk(exists2)
t.absent(exists2)

await db.dropTable('users')

await db.end()
})

tape('count()', async function (t) {
test('count()', async function (t) {
const db = mysql(...cfg)

await db.ready()
Expand All @@ -317,7 +317,7 @@ tape('count()', async function (t) {
await db.end()
})

tape('update()', async function (t) {
test('update()', async function (t) {
const db = mysql(...cfg)

await db.ready()
Expand All @@ -343,7 +343,7 @@ tape('update()', async function (t) {
await db.end()
})

tape('update() with arithmetic', async function (t) {
test('update() with arithmetic', async function (t) {
const db = mysql(...cfg)

await db.ready()
Expand All @@ -355,16 +355,16 @@ tape('update() with arithmetic', async function (t) {
await db.insert('users', { username: 'joe', count: 0 })
await db.insert('users', { username: 'bob', count: 0 })

t.deepEqual({ count: 0 }, await db.selectOne('users', ['count'], 'username = ?', 'bob'))
t.alike({ count: 0 }, await db.selectOne('users', ['count'], 'username = ?', 'bob'))
t.ok(await db.update('users', [{ count: 'count + ?' }, 1], 'username = ?', 'bob') === 1)
t.deepEqual({ count: 1 }, await db.selectOne('users', ['count'], 'username = ?', 'bob'))
t.alike({ count: 1 }, await db.selectOne('users', ['count'], 'username = ?', 'bob'))

await db.dropTable('users')

await db.end()
})

tape('delete()', async function (t) {
test('delete()', async function (t) {
const db = mysql(...cfg)

await db.ready()
Expand Down Expand Up @@ -395,7 +395,7 @@ tape('delete()', async function (t) {
await db.end()
})

tape('transaction()', async function (t) {
test('transaction()', async function (t) {
const db = mysql(...cfg)

await db.ready()
Expand All @@ -420,16 +420,16 @@ tape('transaction()', async function (t) {
const user = await db.selectOne('users', ['*'], 'username = ?', 'alice')
const profile = await db.selectOne('profiles', ['*'], 'owner = ?', user.id)
t.ok(user.id === resultId)
t.deepEqual(user, { id: 3, username: 'alice' })
t.deepEqual(profile, { owner: 3, name: 'Alice' })
t.alike(user, { id: 3, username: 'alice' })
t.alike(profile, { owner: 3, name: 'Alice' })

await db.dropTable('users')
await db.dropTable('profiles')

await db.end()
})

tape('transaction() with error', async function (t) {
test('transaction() with error', async function (t) {
const db = mysql(...cfg)

await db.ready()
Expand Down Expand Up @@ -464,7 +464,7 @@ tape('transaction() with error', async function (t) {
await db.end()
})

tape('execute()', async function (t) {
test('execute()', async function (t) {
const db = mysql(...cfg)

await db.ready()
Expand All @@ -477,7 +477,7 @@ tape('execute()', async function (t) {
await db.insert('users', { username: 'bob' })

const [rows, fields] = await db.execute('SELECT * FROM `users` WHERE `username` = ?', ['joe'])
t.deepEqual(rows, [{ id: 1, username: 'joe' }])
t.alike(rows, [{ id: 1, username: 'joe' }])
t.ok(fields[0].name === 'id')
t.ok(fields[1].name === 'username')

Expand All @@ -486,7 +486,7 @@ tape('execute()', async function (t) {
await db.end()
})

tape('query()', async function (t) {
test('query()', async function (t) {
const db = mysql(...cfg)

await db.ready()
Expand All @@ -499,7 +499,7 @@ tape('query()', async function (t) {
await db.insert('users', { username: 'bob' })

const [rows, fields] = await db.query('SELECT * FROM `users` WHERE `username` = "joe"')
t.deepEqual(rows, [{ id: 1, username: 'joe' }])
t.alike(rows, [{ id: 1, username: 'joe' }])
t.ok(fields[0].name === 'id')
t.ok(fields[1].name === 'username')

Expand All @@ -508,7 +508,7 @@ tape('query()', async function (t) {
await db.end()
})

tape('without ready()', async function (t) {
test('without ready()', async function (t) {
const db = mysql(...cfg)

await db.createTable('users', {
Expand All @@ -519,7 +519,7 @@ tape('without ready()', async function (t) {
await db.insert('users', { username: 'bob' })

const [rows, fields] = await db.execute('SELECT * FROM `users` WHERE `username` = ?', ['joe'])
t.deepEqual(rows, [{ id: 1, username: 'joe' }])
t.alike(rows, [{ id: 1, username: 'joe' }])
t.ok(fields[0].name === 'id')
t.ok(fields[1].name === 'username')

Expand Down