- Did the unthinkable and actually achieved Peak ORM

This commit is contained in:
Alexis
2020-06-06 00:17:01 +02:00
parent cabbf68f7a
commit 369d53d509
9 changed files with 381 additions and 394 deletions

View File

@@ -0,0 +1,27 @@
'use strict'
// Bookshelf
const bookshelf = require('../database/connection').bookshelf
const Spells = require('./spell-repository')
class IngredientRepository {
constructor() {
this.model = bookshelf.Model.extend({
tableName: 'ingredient',
spells() {
return this.belongsToMany( Spells._model, 'spell_ingredient', 'ingredient_id', 'spell_id')
}
})
}
set model(model) {
this._model = model
}
get model() {
return this._model
}
}
module.exports = IngredientRepository

View File

@@ -0,0 +1,27 @@
'use strict'
// Bookshelf
const bookshelf = require('../database/connection').bookshelf
const Schools = require('./school-repository')
class MetaSchoolRepository {
constructor() {
this.model = bookshelf.Model.extend({
tableName: 'meta_school',
schools() {
return this.hasMany( Schools._model )
}
})
}
set model(model) {
this._model = model
}
get model() {
return this._model
}
}
module.exports = MetaSchoolRepository

View File

@@ -0,0 +1,33 @@
'use strict'
// Bookshelf
const bookshelf = require('../database/connection').bookshelf
const Spells = require('./spell-repository')
const MetaSchoolRepository = require('./meta-school-repository')
const MetaSchools = new MetaSchoolRepository()
class SchoolRepository {
constructor() {
this.model = bookshelf.Model.extend({
tableName: 'school',
spells() {
return this.belongsToMany( Spells._model, 'spell_school', 'school_id', 'spell_id')
},
meta_schools() {
return this.belongsTo( MetaSchools._model, 'meta_school_id')
}
})
}
set model(model) {
this._model = model
}
get model() {
return this._model
}
}
module.exports = SchoolRepository

View File

@@ -0,0 +1,160 @@
'use strict'
// Bookshelf
const bookshelf = require('../database/connection').bookshelf
const SchoolRepository = require('./school-repository')
const Schools = new SchoolRepository()
const IngredientRepository = require('./ingredient-repository')
const Ingredients = new IngredientRepository()
const VariableRepository = require('./variable-repository')
const Variables = new VariableRepository()
// Model validation
const Validator = require('jsonschema').Validator
const v = new Validator()
const SpellModel = require("../models/SpellValidation")
v.addSchema(SpellModel, "/SpellModel")
// Validations
const regexXSS = RegExp(/<[^>]*script/)
// Error handling
const { HttpError } = require('../models/Errors')
class SpellRepository {
constructor() {
this.model = bookshelf.Model.extend({
tableName: 'spell',
schools() {
return this.belongsToMany( Schools._model, 'spell_school', 'spell_id', 'school_id' )
},
variables() {
return this.belongsToMany( Variables._model, 'spell_variable', 'spell_id', 'variable_id' )
},
ingredients() {
return this.belongsToMany( Ingredients._model, 'spell_ingredient', 'spell_id', 'ingredient_id' )
}
})
}
set model(model) {
this._model = model
}
get model() {
return this._model
}
getAll() {
return new Promise((resolve, reject) => {
this._model.forge()
.fetchAll({ withRelated: ['schools.meta_schools', 'variables', 'ingredients'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err)
reject(new HttpError(500, "Couldn't get spells"))
})
})
}
getOne(id) {
return new Promise((resolve, reject) => {
this._model.forge()
.where({ 'id' : id })
.fetch({ withRelated: ['schools.meta_schools', 'variables', 'ingredients'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err)
reject(new HttpError(500, "Couldn't get spell"))
})
})
}
addOne(s) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(s)) {
reject(new HttpError(403, "Error: Spell cannot be nothing !"))
} else if (!v.validate(s, SpellModel).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SpellModel).errors))
} else if (this.isXSSAttempt(s.name) || this.isXSSAttempt(s.description) || this.isXSSAttempt(s.cost)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else {
bookshelf.transaction(t => {
return this._model.forge({
'name': s.name,
'description': s.description,
'level': s.level,
'charge' : s.charge,
'cost' : s.cost,
'is_ritual' : s.is_ritual
}).save(null, {
transacting: t
})
.tap(spell => {
return spell
.schools()
.attach(s.schools, {
transacting: t
});
})
.tap(spell => {
return spell
.variables()
.attach(s.variables, {
transacting: t
});
})
.tap(spell => {
return spell
.ingredients()
.attach(s.ingredients, {
transacting: t
});
})
.catch(err => {
throw err
})
})
.then(v => {
return v.load(['schools.meta_schools', 'variables', 'ingredients'])
})
.then(v => {
resolve(this.getOne(v.id))
})
.catch(err => {
console.log(err)
reject(new HttpError(500, "Insert error"))
})
}
})
}
// Check if object is null
isEmptyObject = (obj) => {
if (Object.keys(obj).length === 0 && obj.constructor === Object) {
return true
} else {
return false
}
}
// Check if script injection attempt
isXSSAttempt = (string) => {
if (regexXSS.test(string)) {
return true
} else {
return false
}
}
}
module.exports = SpellRepository

View File

@@ -0,0 +1,85 @@
'use strict'
// Bookshelf
const bookshelf = require('../database/connection').bookshelf
// Hashing and passwords
const bcrypt = require('bcrypt')
// Model validation
const Validator = require('jsonschema').Validator
const v = new Validator()
const UserModel = require("../models/UserValidation")
v.addSchema(UserModel, "/UserModel")
// Validations
const regexInt = RegExp(/^[1-9]\d*$/)
const regexXSS = RegExp(/<[^>]*script/)
// Error handling
const { HttpError } = require('../models/Errors')
class UserRepository {
constructor() {
this.model = bookshelf.Model.extend({
tableName: 'user',
})
}
set model(model) {
this._model = model
}
get model() {
return this._model
}
getAll() {
return new Promise((resolve, reject) => {
this._model.forge()
.fetchAll()
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err)
reject(new HttpError(500, "Couldn't get users"))
})
})
}
getOne(id) {
return new Promise((resolve, reject) => {
this._model.forge()
.where({ 'id' : id })
.fetch()
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err)
reject(new HttpError(500, "Couldn't get user"))
})
})
}
// Check if object is null
isEmptyObject = (obj) => {
if (Object.keys(obj).length === 0 && obj.constructor === Object) {
return true
} else {
return false
}
}
// Check if script injection attempt
isXSSAttempt = (string) => {
if (regexXSS.test(string)) {
return true
} else {
return false
}
}
}
module.exports = UserRepository

View File

@@ -0,0 +1,27 @@
'use strict'
// Bookshelf
const bookshelf = require('../database/connection').bookshelf
const Spells = require('./spell-repository')
class VariableRepository {
constructor() {
this.model = bookshelf.Model.extend({
tableName: 'variable',
spells() {
return this.belongsToMany( Spells._model, 'spell_variable', 'variable_id', 'spell_id')
}
})
}
set model(model) {
this._model = model
}
get model() {
return this._model
}
}
module.exports = VariableRepository