Skip to content

Commit

Permalink
feat: mysql.update added
Browse files Browse the repository at this point in the history
- group.put added
- group.put tests
- user.put added
  • Loading branch information
msimerson committed Feb 25, 2024
1 parent 8d55c8a commit d367787
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 7 deletions.
14 changes: 14 additions & 0 deletions lib/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ class Group {
)
}

async put(args) {
if (!args.id) return false
const id = args.id
delete args.id
// Mysql.debug(1)
const r = await Mysql.update(
`UPDATE nt_group SET`,
`WHERE nt_group_id=${id}`,
Util.mapToDbColumn(args, groupDbMap),
)
// console.log(r)
return r.changedRows === 1
}

async delete(args, val) {
const g = await this.getAdmin(args)
if (g.length !== 1) return false
Expand Down
11 changes: 11 additions & 0 deletions lib/group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ describe('group', function () {
})
})

it('changes a group', async () => {
assert.ok(await group.put({ id: 4096, name: 'example.net' }))
assert.deepEqual(await group.get({ id: 4096 }), [
{
id: 4096,
name: 'example.net',
},
])
assert.ok(await group.put({ id: 4096, name: 'example.com' }))
})

it('delete a group', async () => {
assert.ok(await group.delete({ id: 4096 }))
let u = await group.getAdmin({ id: 4096 })
Expand Down
19 changes: 19 additions & 0 deletions lib/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ class MySQL {
}

async insert(query, params = {}) {
const skipExecute = params.skipExecute ?? false
delete params.skipExecute

query += `(${Object.keys(params).join(',')}) VALUES(${Object.keys(params).map(() => '?')})`

if (skipExecute) return query
return await this.execute(query, Object.values(params))
}

async select(query, params = {}) {
const skipExecute = params.skipExecute ?? false
delete params.skipExecute

let paramsArray = []
if (Array.isArray(params)) {
paramsArray = [...params]
Expand All @@ -61,9 +69,20 @@ class MySQL {
}
}

if (skipExecute) return query
return await this.execute(query, paramsArray)
}

async update(query, where, params = {}) {
const skipExecute = params.skipExecute ?? false
delete params.skipExecute

query += ` ${Object.keys(params).join('=?,')}=? ${where}`

if (skipExecute) return { q: query, p: Object.values(params) }
return await this.execute(query, Object.values(params))
}

async disconnect(dbh) {
const d = dbh || this.dbh
if (this._debug)
Expand Down
53 changes: 51 additions & 2 deletions lib/mysql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,58 @@ describe('mysql', () => {
})
}

it.todo('SQL: formats SELECT queries', async () => {})
it('SQL: formats SELECT queries', async () => {
const r = await mysql.select(`SELECT * FROM nt_user WHERE`, {
last_name: 'Test',
skipExecute: true,
})
assert.equal(r, `SELECT * FROM nt_user WHERE last_name=?`)
})

it('SQL: formats INSERT queries', async () => {
const r = await mysql.select(`INSERT INTO nt_user SET`, {
first_name: 'uNite',
last_name: 'Test',
skipExecute: true,
})
assert.equal(r, `INSERT INTO nt_user SET first_name=? AND last_name=?`)
})

it.todo('SQL: formats INSERT queries', async () => {})
it('SQL: formats UPDATE queries, 1', async () => {
const { q, p } = await mysql.update(
`UPDATE nt_user SET`,
`WHERE nt_user_id=4096`,
{ first_name: 'uNite', skipExecute: true },
)
assert.equal(q, `UPDATE nt_user SET first_name=? WHERE nt_user_id=4096`)
assert.deepEqual(p, ['uNite'])
})

it('SQL: formats UPDATE queries, 2', async () => {
const { q, p } = await mysql.update(
`UPDATE nt_user SET`,
`WHERE nt_user_id=4096`,
{ last_name: 'Teste', is_admin: 1, skipExecute: true },
)
assert.equal(
q,
`UPDATE nt_user SET last_name=?,is_admin=? WHERE nt_user_id=4096`,
)
assert.deepEqual(p, ['Teste', 1])
})

it('SQL: formats UPDATE queries, 3', async () => {
const { q, p } = await mysql.update(
`UPDATE nt_user SET`,
`WHERE nt_user_id=4096`,
{ first_name: 'Unit', last_name: 'Test', is_admin: 0, skipExecute: true },
)
assert.equal(
q,
`UPDATE nt_user SET first_name=?,last_name=?,is_admin=? WHERE nt_user_id=4096`,
)
assert.deepEqual(p, ['Unit', 'Test', 0])
})

it('disconnects', async () => {
assert.ok(this.dbh.connection.connectionId)
Expand Down
22 changes: 17 additions & 5 deletions lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,26 @@ class User {
)
}

async put(args) {
if (!args.id) return false
const id = args.id
delete args.id
const r = await Mysql.update(
`UPDATE nt_user SET`,
`WHERE nt_user_id=${id}`,
Util.mapToDbColumn(args, userDbMap),
)
return r.changedRows === 1
}

Check warning on line 121 in lib/user.js

View check run for this annotation

Codecov / codecov/patch

lib/user.js#L112-L121

Added lines #L112 - L121 were not covered by tests

async delete(args, val) {
const u = await this.getAdmin(args)
if (u.length !== 1) return false
await Mysql.execute(`UPDATE nt_user SET deleted=? WHERE nt_user_id=?`, [
val ?? 1,
u[0].id,
])
return true
const r = await Mysql.execute(
`UPDATE nt_user SET deleted=? WHERE nt_user_id=?`,
[val ?? 1, u[0].id],
)
return r.changedRows === 1
}

async destroy(args) {
Expand Down
4 changes: 4 additions & 0 deletions lib/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ describe('user', function () {
})
})

it.todo('modifies existing user', async () => {
//
})

it('deletes a user', async () => {
assert.ok(await user.delete({ id: 4096 }))
let u = await user.getAdmin({ id: 4096 })
Expand Down

0 comments on commit d367787

Please sign in to comment.