Skip to content

Commit 821f56a

Browse files
committed
mv ./lib/session ./lib/user/session
1 parent 0ebfc25 commit 821f56a

5 files changed

Lines changed: 165 additions & 1 deletion

File tree

lib/session/store/mysql.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
<<<<<<<< HEAD:lib/session/store/mysql.js
12
import Mysql from '../../mysql.js'
23
import { mapToDbColumn } from '../../util.js'
4+
========
5+
import Mysql from '../mysql.js'
6+
import { mapToDbColumn } from '../util.js'
7+
>>>>>>>> a2746b1 (mv ./lib/session ./lib/user/session):lib/user/session.js
38

49
const sessionDbMap = {
510
id: 'nt_user_session_id',

lib/session/test/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import assert from 'node:assert/strict'
22
import { describe, it, after, before } from 'node:test'
33

4+
<<<<<<<< HEAD:lib/session/test/index.js
45
import User from '../../user/index.js'
56
import Session from '../index.js'
67
import userCase from '../../user/test/user.json' with { type: 'json' }
8+
========
9+
import User from '../index.js'
10+
import Session from '../session.js'
11+
import userCase from './user.json' with { type: 'json' }
12+
>>>>>>>> a2746b1 (mv ./lib/session ./lib/user/session):lib/user/test/session.js
713

814
const sessionUser = {
915
...userCase,

lib/user/session.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<<<<<<<< HEAD:lib/session/store/mysql.js
2+
import Mysql from '../../mysql.js'
3+
import { mapToDbColumn } from '../../util.js'
4+
========
5+
import Mysql from '../mysql.js'
6+
import { mapToDbColumn } from '../util.js'
7+
>>>>>>>> a2746b1 (mv ./lib/session ./lib/user/session):lib/user/session.js
8+
9+
const sessionDbMap = {
10+
id: 'nt_user_session_id',
11+
uid: 'nt_user_id',
12+
session: 'nt_user_session',
13+
}
14+
15+
class SessionRepoMySQL {
16+
constructor() {
17+
this.mysql = Mysql
18+
}
19+
20+
async create(args) {
21+
const r = await this.get(args)
22+
if (r) return r.id
23+
24+
const id = await Mysql.execute(...Mysql.insert(`nt_user_session`, mapToDbColumn(args, sessionDbMap)))
25+
return id
26+
}
27+
28+
async get(args) {
29+
let query = `SELECT s.nt_user_session_id AS id
30+
, s.nt_user_id AS uid
31+
, s.nt_user_session AS session
32+
FROM nt_user_session s
33+
LEFT JOIN nt_user u ON s.nt_user_id = u.nt_user_id
34+
WHERE u.deleted=0`
35+
36+
const params = []
37+
for (const f of ['nt_user_session_id', 'nt_user_id', 'nt_user_session']) {
38+
if (args[f] !== undefined) {
39+
query += ` AND s.${f} = ?`
40+
params.push(args[f])
41+
}
42+
}
43+
for (const g of ['id', 'uid', 'session']) {
44+
if (args[g] !== undefined) {
45+
query += ` AND s.${sessionDbMap[g]} = ?`
46+
params.push(args[g])
47+
}
48+
}
49+
50+
const sessions = await Mysql.execute(query, params)
51+
return sessions[0]
52+
}
53+
54+
async put(args) {
55+
if (!args.id) return false
56+
57+
if (args.last_access) {
58+
const p = await this.get({ id: args.id })
59+
if (!p) return false
60+
61+
// update only when +1 minute old (save DB writes)
62+
const now = parseInt(Date.now() / 1000, 10)
63+
const oneMinuteAgo = now - 60
64+
if (p.last_access > oneMinuteAgo) return true
65+
args.last_access = now
66+
}
67+
68+
const id = args.id
69+
delete args.id
70+
const r = await Mysql.execute(
71+
...Mysql.update(`nt_user_session`, `nt_user_session_id=${id}`, mapToDbColumn(args, sessionDbMap)),
72+
)
73+
return r.changedRows === 1
74+
}
75+
76+
async delete(args) {
77+
const r = await Mysql.execute(...Mysql.delete(`nt_user_session`, mapToDbColumn(args, sessionDbMap)))
78+
return r.affectedRows === 1
79+
}
80+
}
81+
82+
export default SessionRepoMySQL

lib/user/test/session.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import assert from 'node:assert/strict'
2+
import { describe, it, after, before } from 'node:test'
3+
4+
<<<<<<<< HEAD:lib/session/test/index.js
5+
import User from '../../user/index.js'
6+
import Session from '../index.js'
7+
import userCase from '../../user/test/user.json' with { type: 'json' }
8+
========
9+
import User from '../index.js'
10+
import Session from '../session.js'
11+
import userCase from './user.json' with { type: 'json' }
12+
>>>>>>>> a2746b1 (mv ./lib/session ./lib/user/session):lib/user/test/session.js
13+
14+
const sessionUser = {
15+
...userCase,
16+
id: userCase.id + 100,
17+
username: `${userCase.username}-session`,
18+
email: `session-${userCase.email}`,
19+
}
20+
21+
before(async () => {
22+
await User.create(sessionUser)
23+
})
24+
25+
after(async () => {
26+
await Session.delete({ uid: sessionUser.id })
27+
await User.destroy({ id: sessionUser.id })
28+
await User.mysql.disconnect()
29+
})
30+
31+
describe('session', function () {
32+
let sessionId
33+
34+
describe('create', () => {
35+
it('creates a login session', async () => {
36+
sessionId = await Session.create({
37+
nt_user_id: sessionUser.id,
38+
session: '3.0.0',
39+
last_access: parseInt(Date.now() / 1000, 10),
40+
})
41+
assert.ok(sessionId)
42+
})
43+
})
44+
45+
describe('get', () => {
46+
it('finds a session by id', async () => {
47+
const s = await Session.get({ id: sessionId })
48+
assert.ok(s?.id)
49+
})
50+
51+
it('finds a session by nt_user_session_id', async () => {
52+
const s = await Session.get({ nt_user_session_id: sessionId })
53+
assert.ok(s?.id)
54+
})
55+
56+
it('finds a session by session', async () => {
57+
const s = await Session.get({ nt_user_session: '3.0.0' })
58+
assert.ok(s?.id)
59+
})
60+
})
61+
62+
describe('delete', () => {
63+
it('deletes a session by ID', async () => {
64+
assert.ok(await Session.delete({ id: sessionId }))
65+
})
66+
67+
it('does not find a deleted session', async () => {
68+
assert.equal(await Session.get({ id: sessionId }), undefined)
69+
})
70+
})
71+
})

test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ else
3030
# npm i --no-save node-test-github-reporter
3131
# $NODE --test --test-reporter=node-test-github-reporter
3232
# fi
33-
$NODE --test --test-reporter=spec lib/*/test/index.js lib/*.test.js routes/*.test.js
33+
$NODE --test --test-reporter=spec lib/*/test/*.js lib/*.test.js routes/*.test.js
3434
fi

0 commit comments

Comments
 (0)