From d367787158e09ba8fff02e3bed6331ea13b049cf Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Sun, 25 Feb 2024 15:56:49 -0800 Subject: [PATCH] feat: mysql.update added - group.put added - group.put tests - user.put added --- lib/group.js | 14 +++++++++++++ lib/group.test.js | 11 ++++++++++ lib/mysql.js | 19 +++++++++++++++++ lib/mysql.test.js | 53 +++++++++++++++++++++++++++++++++++++++++++++-- lib/user.js | 22 +++++++++++++++----- lib/user.test.js | 4 ++++ 6 files changed, 116 insertions(+), 7 deletions(-) diff --git a/lib/group.js b/lib/group.js index d29c823..6aec64d 100644 --- a/lib/group.js +++ b/lib/group.js @@ -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 diff --git a/lib/group.test.js b/lib/group.test.js index 14abab2..0946f1d 100644 --- a/lib/group.test.js +++ b/lib/group.test.js @@ -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 }) diff --git a/lib/mysql.js b/lib/mysql.js index 35516b4..81d6e00 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -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] @@ -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) diff --git a/lib/mysql.test.js b/lib/mysql.test.js index 3edebe2..6ff9186 100644 --- a/lib/mysql.test.js +++ b/lib/mysql.test.js @@ -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) diff --git a/lib/user.js b/lib/user.js index d0c7b3f..b0a7f9e 100644 --- a/lib/user.js +++ b/lib/user.js @@ -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 + } + 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) { diff --git a/lib/user.test.js b/lib/user.test.js index 9e927e2..65d47f4 100644 --- a/lib/user.test.js +++ b/lib/user.test.js @@ -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 })