This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Database { | |
constructor() { | |
this.connectionURL = { | |
name: "", | |
options: {} | |
} | |
} | |
// Our connect method taking in two arguments | |
connect(name, options) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Database { | |
constructor() { | |
this.connectionURL = { | |
name: "", | |
options: {} | |
} | |
// This disallows modifying the instance we created | |
Object.freeze(this); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Database { | |
constructor() { | |
// Check if our first instance has already been created | |
if (Database.instance instanceof Database) { | |
return Database.instance; | |
} | |
this.connectionURL = { | |
name: "", | |
options: {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Database { | |
constructor() { | |
if (Database.instance instanceof Database) { | |
return Database.instance; | |
} | |
this.connectionURL = { | |
name: "", | |
options: {} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { first, middle, last } from "random-name"; | |
class randomName { | |
generateFirstName() { | |
return first(); | |
} | |
generateMiddleName() { | |
return middle(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import name from "./random-name"; | |
class PlugComponent { | |
constructor() { | |
this.firstName = name.generateFirstName(); | |
this.middleName = name.generateMiddleName(); | |
this.lastName = name.generateLastName(); | |
} | |
generateFullName() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
async getPersonById(personId) { | |
const person = await DBInstance.User.find(personId); | |
// person might return a null value which our client might not want | |
return person; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { LogicalException } = require('@adonisjs/generic-exceptions') // Nevermind about this, it is how it is a generic exception for Adonisjs. | |
class NotFoundException extends LogicalException { | |
handle (error, response , resource) { | |
response | |
.status(404) | |
.json({ | |
status: 'Not Found', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BaseExceptionHandler = use('BaseExceptionHandler') | |
class ExceptionHandler extends BaseExceptionHandler { | |
async handle(error, { request, response }) { | |
if (error.name == 'NotFoundException') { | |
return new NotFoundException().handle(error, response, error.message) | |
} | |
return response.status(500).send({ | |
status_code: 500, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
async getPersonById(personId) { | |
const person = await DBInstance.User.find(personId); | |
if (!person) { | |
// Here we don't have to return null to the client anymore instead 'Person not found' is. | |
throw new NotFoundException('Person') | |
} | |
return person; | |
} | |
} |