Skip to content

Instantly share code, notes, and snippets.

View Vectormike's full-sized avatar
🏠
Working from home

Victor Jonah Vectormike

🏠
Working from home
View GitHub Profile
class Database {
constructor() {
this.connectionURL = {
name: "",
options: {}
}
}
// Our connect method taking in two arguments
connect(name, options) {
class Database {
constructor() {
this.connectionURL = {
name: "",
options: {}
}
// This disallows modifying the instance we created
Object.freeze(this);
}
class Database {
constructor() {
// Check if our first instance has already been created
if (Database.instance instanceof Database) {
return Database.instance;
}
this.connectionURL = {
name: "",
options: {}
}
class Database {
constructor() {
if (Database.instance instanceof Database) {
return Database.instance;
}
this.connectionURL = {
name: "",
options: {}
}
import { first, middle, last } from "random-name";
class randomName {
generateFirstName() {
return first();
}
generateMiddleName() {
return middle();
}
import name from "./random-name";
class PlugComponent {
constructor() {
this.firstName = name.generateFirstName();
this.middleName = name.generateMiddleName();
this.lastName = name.generateLastName();
}
generateFullName() {
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;
}
}
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',
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,
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;
}
}