-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set up session table (return requirements) #594
Changes from 9 commits
9196a5f
c14dd9c
f25fbd2
c59b226
f37017a
4e08147
4198e0c
4f38961
b4f79d4
10ec62d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict' | ||
|
||
/** | ||
* Model for sessions | ||
* @module SessionModel | ||
*/ | ||
|
||
const BaseModel = require('./base.model.js') | ||
|
||
class SessionModel extends BaseModel { | ||
static get tableName () { | ||
return 'sessions' | ||
} | ||
|
||
// Defining which fields contain json allows us to insert an object without needing to stringify it first | ||
static get jsonAttributes () { | ||
return [ | ||
'data' | ||
] | ||
} | ||
} | ||
|
||
module.exports = SessionModel |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict' | ||
|
||
const tableName = 'sessions' | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
exports.up = function (knex) { | ||
return knex | ||
.schema | ||
.createTable(tableName, (table) => { | ||
// Primary Key | ||
table.uuid('id').primary().defaultTo(knex.raw('gen_random_uuid()')) | ||
// Data | ||
table.jsonb('data').defaultTo({}) | ||
|
||
// Automatic timestamps | ||
table.timestamps(false, true) | ||
}) | ||
.then(() => { | ||
knex.raw(` | ||
CREATE TRIGGER update_timestamp | ||
BEFORE UPDATE | ||
ON ${tableName} | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE update_timestamp(); | ||
`) | ||
}) | ||
} | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
exports.down = function (knex) { | ||
return knex | ||
.schema | ||
.dropTableIfExists(tableName) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict' | ||
|
||
// Test framework dependencies | ||
const Lab = require('@hapi/lab') | ||
const Code = require('@hapi/code') | ||
|
||
const { describe, it, beforeEach } = exports.lab = Lab.script() | ||
const { expect } = Code | ||
|
||
// Test helpers | ||
const DatabaseHelper = require('../support/helpers/database.helper.js') | ||
const SessionHelper = require('../support/helpers/session.helper.js') | ||
|
||
// Thing under test | ||
const SessionModel = require('../../app/models/session.model.js') | ||
|
||
describe('Sessions model', () => { | ||
let testRecord | ||
|
||
beforeEach(async () => { | ||
await DatabaseHelper.clean() | ||
}) | ||
|
||
describe('Basic query', () => { | ||
beforeEach(async () => { | ||
testRecord = await SessionHelper.add() | ||
}) | ||
|
||
it('can successfully run a basic query', async () => { | ||
const result = await SessionModel.query().findById(testRecord.id) | ||
|
||
expect(result).to.be.an.instanceOf(SessionModel) | ||
expect(result.id).to.equal(testRecord.id) | ||
expect(result.data).to.equal({}) | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict' | ||
|
||
/** | ||
* @module SessionHelper | ||
*/ | ||
|
||
const { generateUUID } = require('../../../app/lib/general.lib.js') | ||
const SessionModel = require('../../../app/models/session.model.js') | ||
|
||
/** | ||
* Add a new session | ||
* | ||
* If no `data` is provided, default values will be used. These are | ||
* | ||
* - `id` - [random UUID] | ||
* - `data` - [empty object {}] | ||
* | ||
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database | ||
* | ||
* @returns {module:SessionModel} The instance of the newly created record | ||
*/ | ||
function add (data = {}) { | ||
const insertData = defaults(data) | ||
|
||
return SessionModel.query() | ||
.insert({ ...insertData }) | ||
.returning('*') | ||
} | ||
|
||
/** | ||
* Returns the defaults used | ||
* | ||
* It will override or append to them any data provided. Mainly used by the `add()` method, we make it available | ||
* for use in tests to avoid having to duplicate values. | ||
* | ||
* @param {Object} [data] Any data you want to use instead of the defaults used here in the database | ||
*/ | ||
function defaults (data = {}) { | ||
const defaults = { | ||
id: generateUUID(), | ||
data: {} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where a table automatically generates an ID we don't tend to set one in the helper. If you've seen us do this it should normally come with a note saying the previous team failed to implement an automatic UUID on insert in the underlying table, hence the need to do it. So, we just focus on setting meaningful defaults to reflect what an instantiated record would typically look like. In this particular case const defaults = {
data: { licenceId: '01/128' }
}
or const defaults = {} |
||
|
||
return { | ||
...defaults, | ||
...data | ||
} | ||
} | ||
|
||
module.exports = { | ||
add, | ||
defaults | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.