Command Shift

CRUD Readers

Now that we have used Sequelize to create a Reader in our database, we need to add the rest of the CRUD operations.

Replace the code in your reader.test.js file with this:

// tests/reader.test.js
const { expect } = require('chai');
const request = require('supertest');
const { Reader } = require('../src/models');
const app = require('../src/app');
 
describe('/readers', () => {
  before(async () => Reader.sequelize.sync());
 
  beforeEach(async () => {
    await Reader.destroy({ where: {} });
  });
 
  describe('with no records in the database', () => {
    describe('POST /readers', () => {
      it('creates a new reader in the database', async () => {
        const response = await request(app).post('/readers').send({
          name: 'Elizabeth Bennet',
          email: 'future_ms_darcy@gmail.com',
        });
        const newReaderRecord = await Reader.findByPk(response.body.id, {
          raw: true,
        });
 
        expect(response.status).to.equal(201);
        expect(response.body.name).to.equal('Elizabeth Bennet');
        expect(newReaderRecord.name).to.equal('Elizabeth Bennet');
        expect(newReaderRecord.email).to.equal('future_ms_darcy@gmail.com');
      });
    });
  });
 
  describe('with records in the database', () => {
    let readers;
 
    beforeEach(async () => {
      readers = await Promise.all([
        Reader.create({
          name: 'Elizabeth Bennet',
          email: 'future_ms_darcy@gmail.com',
        }),
        Reader.create({ name: 'Arya Stark', email: 'vmorgul@me.com' }),
        Reader.create({ name: 'Lyra Belacqua', email: 'darknorth123@msn.org' }),
      ]);
    });
 
    describe('GET /readers', () => {
      it('gets all readers records', async () => {
        const response = await request(app).get('/readers');
 
        expect(response.status).to.equal(200);
        expect(response.body.length).to.equal(3);
 
        response.body.forEach((reader) => {
          const expected = readers.find((a) => a.id === reader.id);
 
          expect(reader.name).to.equal(expected.name);
          expect(reader.email).to.equal(expected.email);
        });
      });
    });
 
    describe('GET /readers/:id', () => {
      it('gets readers record by id', async () => {
        const reader = readers[0];
        const response = await request(app).get(`/readers/${reader.id}`);
 
        expect(response.status).to.equal(200);
        expect(response.body.name).to.equal(reader.name);
        expect(response.body.email).to.equal(reader.email);
      });
 
      it('returns a 404 if the reader does not exist', async () => {
        const response = await request(app).get('/readers/12345');
 
        expect(response.status).to.equal(404);
        expect(response.body.error).to.equal('The reader could not be found.');
      });
    });
 
    describe('PATCH /readers/:id', () => {
      it('updates readers email by id', async () => {
        const reader = readers[0];
        const response = await request(app)
          .patch(`/readers/${reader.id}`)
          .send({ email: 'miss_e_bennet@gmail.com' });
        const updatedReaderRecord = await Reader.findByPk(reader.id, {
          raw: true,
        });
 
        expect(response.status).to.equal(200);
        expect(updatedReaderRecord.email).to.equal('miss_e_bennet@gmail.com');
      });
 
      it('returns a 404 if the reader does not exist', async () => {
        const response = await request(app)
          .patch('/readers/12345')
          .send({ email: 'some_new_email@gmail.com' });
 
        expect(response.status).to.equal(404);
        expect(response.body.error).to.equal('The reader could not be found.');
      });
    });
 
    describe('DELETE /readers/:id', () => {
      it('deletes reader record by id', async () => {
        const reader = readers[0];
        const response = await request(app).delete(`/readers/${reader.id}`);
        const deletedReader = await Reader.findByPk(reader.id, { raw: true });
 
        expect(response.status).to.equal(204);
        expect(deletedReader).to.equal(null);
      });
 
      it('returns a 404 if the reader does not exist', async () => {
        const response = await request(app).delete('/readers/12345');
        expect(response.status).to.equal(404);
        expect(response.body.error).to.equal('The reader could not be found.');
      });
    });
  });
});

These tests will confirm that your app is able to create, read, update and delete Readers.

Passing these tests is surprisingly similar to the the Music Library, the only real difference is we are using Sequelize methods now:

Create

Reader.create() - to create new entries in the database.

const data = {
  name: 'Stephenie Meyer',
  email: 'sparkle@vampire.com'
};
 
const newReader = await Reader.create(data);

Read

Reader.findAll() - to find and return a list of entries.

const readers = await Reader.findAll();

Reader.findByPk() - to find and return a single entry by its id

const readerId = 3;
const reader = await Reader.findByPK(readerId);

Update

Reader.update() - to update entries in the database.

const readerId = 3;
const updateData = {
  email: `shiny-vampire@rainy-forest.com`
};
 
const [ updatedRows ] = await Reader.update(updateData, { where: {} });

Delete

Reader.destroy() - to delete entries from the database.

const readerId = 3;
 
const deletedRows = await Reader.destroy({ where: { id: readerId } });

Challenge

Use the code snippets and documentation above to pass these tests. Move on to the next page when you are done.

On this page