bwreid / oop-orm

Who needs Bookshelf?

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

OOP ORM

This is a project designed to get you to write your own simple ORM.

Installation

  1. Fork/clone

  2. npm install

  3. createdb oop_orm_dev && createdb oop_orm_test

  4. cp .env.sample .env

  5. npm run migrate

  6. npm run seed

  7. npm run test

Description

For this exercise you will only be creating the Model for a Theatre. Theatres have a name and address field outside of all the usual stuff. You will be writing your model so that it has the following static and instance methods.

GET ALL

const theatres = await Theatre.all()
// or, Theatre.all().then(theatres => ...)
// [ { ... }, { ... }, { ... } ]

GET ONE

const theatres = await Theatre.find(1)
// or, Theatre.find(1).then(theatres => ...)
// { ... }

CREATE

const theatre = new Theatre({ name: 'My Theatre', address: '123 Main Street' })
await theatre.save()
// or, theatre.save().then(theatre => ...)
// { id: xx, name: 'My Theatre', address: '123 Main Street' }

UPDATE

const theatre = await Theatre.find(1)
theatre.name = 'My New Name'
await theatre.save()
// or, theatre.save().then(theatre => ...)
// { id: 1, name: 'My New Name', ... }

DESTROY

const theatre = await Theatre.find(1)
await theatre.destroy()
// or, theatre.destroy().then(theatre => ...)
// { id: 1, ... }

Getters and Setters

To help you out, you'll also be creating the following properties on individual instances of each theatre. These should allow you to create the above methods.

GET & SET .id

You should be able to retrieve the ID from a theatre but not change it after the fact.

const theatre = new Theatre({ id: 99, name: 'Name', address: 'Address' })
theatre.id // 99
const theatre = new Theatre({ id: 99, name: 'Name', address: 'Address' })
theatre.id = 100 // ERROR!

GET & SET .removed

This property will tell you whether or not the current instance has been removed (i.e. deleted). This will allow you to keep track of which instances you've deleted and which are still active in your database.

const theatre = new Theatre({ id: 99, name: 'Name', address: 'Address' })
theatre.removed // false
const theatre = new Theatre({ id: 99, name: 'Name', address: 'Address' })
theatre.removed = true // ERROR!
const theatre = Theatre.find(99)
theatre.removed // false

theatre.destroy()
theatre.removed // true

BONUS: .valid

You may find this property helpful in other parts of your code! It is designed to both give you and the user a way to determine whether or not the instance is valid.

const theatre = new Theatre({ name: 'My Theatre' })
theatre.valid // false
theatre.address = '123 Main Street'
theatre.valid // true

About

Who needs Bookshelf?


Languages

Language:JavaScript 100.0%