Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Feb 28, 2024
1 parent 1849280 commit 90e7345
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 65 deletions.
4 changes: 2 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Config {
this.getEnv(opts)
}

async getEnv (opts = {}) {
async getEnv(opts = {}) {
this.env = process.env.NODE_ENV ?? opts.env ?? ''
this.debug = Boolean(process.env.NODE_DEBUG)
if (this.debug) console.log(`debug: true, env: ${this.env}`)
Expand Down Expand Up @@ -55,4 +55,4 @@ function applyDefaults(cfg = {}, defaults = {}) {
return cfg
}

export default new Config()
export default new Config()
4 changes: 2 additions & 2 deletions lib/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ describe('config', () => {
})

it(`detects NODE_DEBUG env`, async () => {
process.env.NODE_DEBUG=1
process.env.NODE_DEBUG = 1
let cfg = await Config.get('mysql', 'test')
assert.equal(Config.debug, true)

process.env.NODE_DEBUG=''
process.env.NODE_DEBUG = ''
cfg = await Config.get('mysql', 'test')
assert.equal(Config.debug, false)

Expand Down
4 changes: 2 additions & 2 deletions lib/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { mapToDbColumn } from './util.js'
const groupDbMap = { id: 'nt_group_id', parent_gid: 'parent_group_id' }

class Group {
constructor(args = {}) {
constructor() {
this.mysql = Mysql
}

Expand Down Expand Up @@ -73,4 +73,4 @@ class Group {
}
}

export default new Group()
export default new Group()
4 changes: 1 addition & 3 deletions lib/group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ describe('group', function () {
parent_gid: 0,
},
])
assert.ok(
await Group.put({ id: testCase.id, name: testCase.name }),
)
assert.ok(await Group.put({ id: testCase.id, name: testCase.name }))
})

it('deletes a group', async () => {
Expand Down
11 changes: 5 additions & 6 deletions lib/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Mysql {
const skipExecute = params.skipExecute ?? false
delete params.skipExecute

const [ queryWhere, paramsArray ] = this.whereConditions(query, params)
const [queryWhere, paramsArray] = this.whereConditions(query, params)

if (skipExecute) return queryWhere
return await this.execute(queryWhere, paramsArray)
Expand Down Expand Up @@ -87,18 +87,17 @@ class Mysql {
first = false
}
}
return [ query, paramsArray ]
return [query, paramsArray]
}

async delete(query, params) {
const [ queryWhere, paramsArray ] = this.whereConditions(query, params)
const [queryWhere, paramsArray] = this.whereConditions(query, params)
return await this.execute(queryWhere, paramsArray)
}

async disconnect(dbh) {
const d = dbh || this.dbh
if (_debug)
console.log(`MySQL connection id ${d.connection.connectionId}`)
if (_debug) console.log(`MySQL connection id ${d.connection.connectionId}`)
if (d) await d.end()
}

Expand All @@ -108,4 +107,4 @@ class Mysql {
}
}

export default new Mysql()
export default new Mysql()
21 changes: 12 additions & 9 deletions lib/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ class Permission {
if (g.length) return g[0].id
}

return await this.mysql.insert(
return await Mysql.insert(
`INSERT INTO nt_perm`,
mapToDbColumn(args, permDbMap),
)
}

async get(args) {
const rows = await this.mysql.select(
const rows = await Mysql.select(
`SELECT nt_perm_id AS id
, nt_user_id AS uid
, nt_group_id AS gid
Expand All @@ -73,8 +73,8 @@ class Permission {
if (!args.id) return false
const id = args.id
delete args.id
// this.mysql.debug(1)
const r = await this.mysql.update(
// Mysql.debug(1)
const r = await Mysql.update(
`UPDATE nt_perm SET`,
`WHERE nt_perm_id=${id}`,
mapToDbColumn(args, permDbMap),
Expand All @@ -85,17 +85,20 @@ class Permission {
async delete(args, val) {
const g = await this.get(args)
if (g.length !== 1) return false
await this.mysql.execute(`UPDATE nt_perm SET deleted=? WHERE nt_perm_id=?`, [
val ?? 1,
g[0].id,
])
await Mysql.execute(
`UPDATE nt_perm SET deleted=? WHERE nt_perm_id=?`,
[val ?? 1, g[0].id],
)
return true
}

async destroy(args) {
const g = await this.get(args)
if (g.length === 1) {
await this.mysql.execute(`DELETE FROM nt_perm WHERE nt_perm_id=?`, [g[0].id])
await Mysql.delete(
`DELETE FROM nt_perm WHERE`,
mapToDbColumn(args, permDbMap),
)
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions lib/session.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import Mysql from './mysql.js'
import { mapToDbColumn } from './util.js'

const sessionDbMap = { id: 'nt_user_session_id', uid: 'nt_user_id', session: 'nt_user_session' }
const sessionDbMap = {
id: 'nt_user_session_id',
uid: 'nt_user_id',
session: 'nt_user_session',
}

class Session {
constructor(args = {}) {
constructor() {
this.mysql = Mysql
}

Expand All @@ -14,7 +18,7 @@ class Session {

const id = await Mysql.insert(
`INSERT INTO nt_user_session`,
mapToDbColumn(args, sessionDbMap)
mapToDbColumn(args, sessionDbMap),
)
return id
}
Expand Down Expand Up @@ -48,10 +52,10 @@ class Session {
async delete(args) {
const r = await Mysql.select(
`DELETE FROM nt_user_session WHERE`,
mapToDbColumn(args, sessionDbMap)
mapToDbColumn(args, sessionDbMap),
)
return r.affectedRows === 1
}
}

export default new Session()
export default new Session()
5 changes: 1 addition & 4 deletions lib/session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ describe('session', function () {
})

it('does not find a deleted session', async () => {
assert.equal(
await Session.get({ id: sessionId }),
undefined,
)
assert.equal(await Session.get({ id: sessionId }), undefined)
})
})
})
4 changes: 2 additions & 2 deletions lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class User {
if (u.length === 1) {
await Mysql.delete(
`DELETE FROM nt_user WHERE`,
mapToDbColumn({ id: u[0].id}, userDbMap)
mapToDbColumn({ id: u[0].id }, userDbMap),
)
}
}
Expand Down Expand Up @@ -183,4 +183,4 @@ class User {
}
}

export default new User()
export default new User()
2 changes: 1 addition & 1 deletion lib/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ after(async () => {
User.mysql.disconnect()
})

function sanitize (u) {
function sanitize(u) {
const r = JSON.parse(JSON.stringify(u))
for (const f of ['deleted', 'password', 'pass_salt']) {
delete r[f]
Expand Down
10 changes: 3 additions & 7 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import os from 'node:os'

import pkgJson from '../package.json' with { type: 'json' }

function setEnv () {
function setEnv() {
if (process.env.NODE_ENV !== undefined) return

/* c8 ignore next 9 */
Expand All @@ -23,7 +23,7 @@ const meta = {
},
}

function mapToDbColumn (args, maps) {
function mapToDbColumn(args, maps) {
// create an instance, so we don't mangle the original
const newArgs = JSON.parse(JSON.stringify(args))

Expand All @@ -36,8 +36,4 @@ function mapToDbColumn (args, maps) {
return newArgs
}

export {
setEnv,
meta,
mapToDbColumn,
}
export { setEnv, meta, mapToDbColumn }
2 changes: 1 addition & 1 deletion routes/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import validate from '@nictool/validate'
import Group from '../lib/group.js'
import { meta } from '../lib/util.js'

function GroupRoutes(server, mysql) {
function GroupRoutes(server) {
server.route([
{
method: 'GET',
Expand Down
1 change: 0 additions & 1 deletion routes/group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ after(async () => {
})

describe('group routes', () => {

it('POST /session establishes a session', async () => {
const res = await server.inject({
method: 'POST',
Expand Down
10 changes: 5 additions & 5 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ async function setup() {
},
routes: {
files: {
relativeTo: path.join(path.dirname(url.fileURLToPath(import.meta.url)), 'html'),
relativeTo: path.join(
path.dirname(url.fileURLToPath(import.meta.url)),
'html',
),
},
},
})
Expand Down Expand Up @@ -109,10 +112,7 @@ async function start() {
return server
}

export {
init,
start,
}
export { init, start }

process.on('unhandledRejection', (err) => {
console.error(err)
Expand Down
5 changes: 1 addition & 4 deletions routes/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,4 @@ function SessionRoutes(server) {

export default SessionRoutes

export {
Session,
SessionRoutes,
}
export { Session, SessionRoutes }
6 changes: 1 addition & 5 deletions routes/session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,7 @@ describe('session routes', () => {
assert.equal(res.statusCode, 200)
})

const routes = [
{ GET: '/' },
{ GET: '/session' },
{ DELETE: '/session' },
]
const routes = [{ GET: '/' }, { GET: '/session' }, { DELETE: '/session' }]

for (const r of routes) {
const key = Object.keys(r)[0]
Expand Down
5 changes: 1 addition & 4 deletions routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,4 @@ function UserRoutes(server) {

export default UserRoutes

export {
User,
UserRoutes,
}
export { User, UserRoutes }
3 changes: 1 addition & 2 deletions routes/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const parseCookie = (c) => {
}

describe('user routes', () => {

it('POST /session establishes a session', async () => {
const res = await server.inject({
method: 'POST',
Expand All @@ -36,7 +35,7 @@ describe('user routes', () => {
},
})
assert.ok(res.headers['set-cookie'][0])
sessionCookie = parseCookie(res.headers['set-cookie'][0])
sessionCookie = parseCookie(res.headers['set-cookie'][0])
})

it('GET /user', async () => {
Expand Down

0 comments on commit 90e7345

Please sign in to comment.