From 83259822271e5952fb48df459dcfa8b9a579d359 Mon Sep 17 00:00:00 2001 From: Vic Date: Sun, 15 May 2022 01:51:43 +0200 Subject: [PATCH] book.ts fix + migration fix + added book_spec --- database.json | 4 +- migrations/20220514223735-books-table.js | 53 ++++++++++++++ .../sql/20220222050518-books-table-down | 1 - .../sqls/20220514223735-books-table-down.sql | 2 + .../20220514223735-books-table-up.sql} | 7 +- package.json | 2 +- src/database.ts | 46 +++++++----- src/models/book.ts | 18 ++--- src/models/tests/book_spec.ts | 70 +++++++++++++++++++ 9 files changed, 168 insertions(+), 35 deletions(-) create mode 100644 migrations/20220514223735-books-table.js delete mode 100644 migrations/sql/20220222050518-books-table-down create mode 100644 migrations/sqls/20220514223735-books-table-down.sql rename migrations/{sql/20220222050518-books-table-up => sqls/20220514223735-books-table-up.sql} (51%) create mode 100644 src/models/tests/book_spec.ts diff --git a/database.json b/database.json index 8f8ee12..a2bf251 100644 --- a/database.json +++ b/database.json @@ -2,7 +2,7 @@ "dev": { "driver": "pg", "host": "127.0.0.1", - "database": "shelf_dev", + "database": "shelf", "user": "postgres", "password": "fredy123" }, @@ -10,7 +10,7 @@ "driver": "pg", "host": "127.0.0.1", "database": "shelf_test", - "user": "postgres_test", + "user": "postgres", "password": "fredy123" } } \ No newline at end of file diff --git a/migrations/20220514223735-books-table.js b/migrations/20220514223735-books-table.js new file mode 100644 index 0000000..42b85d6 --- /dev/null +++ b/migrations/20220514223735-books-table.js @@ -0,0 +1,53 @@ +'use strict'; + +var dbm; +var type; +var seed; +var fs = require('fs'); +var path = require('path'); +var Promise; + +/** + * We receive the dbmigrate dependency from dbmigrate initially. + * This enables us to not have to rely on NODE_PATH. + */ +exports.setup = function(options, seedLink) { + dbm = options.dbmigrate; + type = dbm.dataType; + seed = seedLink; + Promise = options.Promise; +}; + +exports.up = function(db) { + var filePath = path.join(__dirname, 'sqls', '20220514223735-books-table-up.sql'); + return new Promise( function( resolve, reject ) { + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return reject(err); + console.log('received data: ' + data); + + resolve(data); + }); + }) + .then(function(data) { + return db.runSql(data); + }); +}; + +exports.down = function(db) { + var filePath = path.join(__dirname, 'sqls', '20220514223735-books-table-down.sql'); + return new Promise( function( resolve, reject ) { + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return reject(err); + console.log('received data: ' + data); + + resolve(data); + }); + }) + .then(function(data) { + return db.runSql(data); + }); +}; + +exports._meta = { + "version": 1 +}; diff --git a/migrations/sql/20220222050518-books-table-down b/migrations/sql/20220222050518-books-table-down deleted file mode 100644 index 736dbb3..0000000 --- a/migrations/sql/20220222050518-books-table-down +++ /dev/null @@ -1 +0,0 @@ -drop TABLE books \ No newline at end of file diff --git a/migrations/sqls/20220514223735-books-table-down.sql b/migrations/sqls/20220514223735-books-table-down.sql new file mode 100644 index 0000000..42b45ed --- /dev/null +++ b/migrations/sqls/20220514223735-books-table-down.sql @@ -0,0 +1,2 @@ +/* Replace with your SQL commands */ +drop TABLE books \ No newline at end of file diff --git a/migrations/sql/20220222050518-books-table-up b/migrations/sqls/20220514223735-books-table-up.sql similarity index 51% rename from migrations/sql/20220222050518-books-table-up rename to migrations/sqls/20220514223735-books-table-up.sql index bf184b1..1ec5520 100644 --- a/migrations/sql/20220222050518-books-table-up +++ b/migrations/sqls/20220514223735-books-table-up.sql @@ -1,7 +1,8 @@ +/* Replace with your SQL commands */ CREATE TABLE books ( id SERIAL PRIMARY KEY, title VARCHAR(150), author VARCHAR(255), - price integer not null, - summary text -); + pages integer not null, + price integer not null +); \ No newline at end of file diff --git a/package.json b/package.json index e11fbc5..1b96b5b 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "start": "nodemon src/server.ts", "watch": "tsc-watch --esModuleInterop src/server.ts --outDir ./dist --onSuccess \"node ./dist/server.js\"", - "test": "jasmine-ts", + "test": "ENV=test db-migrate --env test up && jasmine-ts && db-migrate db:drop test", "build": "npx tsc", "lint": "eslint --ext .ts", "prettier": "prettier \"src/**/*.ts\" --write", diff --git a/src/database.ts b/src/database.ts index 393aaa8..67189ab 100644 --- a/src/database.ts +++ b/src/database.ts @@ -1,24 +1,36 @@ -import { Pool } from 'pg'; -import dotenv from 'dotenv'; +import dotenv from 'dotenv' +import { Pool } from 'pg' -dotenv.config(); +dotenv.config() const { - ENV, - POSTGRES_HOST, - POSTGRES_DB, - POSTGRES_USER, - POSTGRES_PASSWORD -} = process.env; + POSTGRES_HOST, + POSTGRES_DB, + POSTGRES_USER, + POSTGRES_PASSWORD, + POSTGRES_TEST_DB, + ENV, +} = process.env let client = new Pool(); -if (ENV === 'dev') { - client = new Pool({ - host: POSTGRES_HOST, - database: POSTGRES_DB, - user: POSTGRES_USER, - password: POSTGRES_PASSWORD - }); +console.log(ENV) + +if(ENV === 'test') { + client = new Pool({ + host: POSTGRES_HOST, + database: POSTGRES_TEST_DB, + user: POSTGRES_USER, + password: POSTGRES_PASSWORD, + }) +} + +if(ENV === 'dev') { + client = new Pool({ + host: POSTGRES_HOST, + database: POSTGRES_DB, + user: POSTGRES_USER, + password: POSTGRES_PASSWORD, + }) } -export default client \ No newline at end of file +export default client; \ No newline at end of file diff --git a/src/models/book.ts b/src/models/book.ts index 37812fb..988cb29 100644 --- a/src/models/book.ts +++ b/src/models/book.ts @@ -1,13 +1,13 @@ import { Client } from 'pg'; import client from '../database'; -//import { Books } from '../types'; +//import { Book } from '../types'; export type Book = { - id: number; + id?: string; title: string; author: string; + pages: number; price: number; - summary: string; } export class BookStore { @@ -27,7 +27,7 @@ export class BookStore { async show(id: string): Promise { try { - const sql = 'SELECT * FROM books where id =($1)' + const sql = 'SELECT * FROM books where id=($1)' // @ts-ignore const conn = await client.connect() @@ -43,18 +43,18 @@ export class BookStore { async create(b: Book): Promise { try { - const sql = 'INSERT INTO books (title, author, price, summary) VALUES ($1, $2, $3, $4) RETURNING *' + const sql = 'INSERT INTO books (title, author, pages, price) VALUES ($1, $2, $3, $4) RETURNING *' // @ts-ignore const conn = await client.connect() - const result = await conn.query(sql, [b.title, b.author, b.price, b.summary]) + const result = await conn.query(sql, [b.title, b.author, b.pages, b.price]) const book = result.rows[0] conn.release() return book } catch (err) { - throw new Error(`Could not add new book ${title}. Error: ${err}`) + throw new Error(`Could not add new book ${b.title}. Error: ${err}`) } } @@ -76,8 +76,4 @@ export class BookStore { throw new Error(`Could not delete book ${id}. Error: ${err}`) } } - - - - } diff --git a/src/models/tests/book_spec.ts b/src/models/tests/book_spec.ts new file mode 100644 index 0000000..79c7dca --- /dev/null +++ b/src/models/tests/book_spec.ts @@ -0,0 +1,70 @@ +import { Book, BookStore } from '../book' + +const store = new BookStore() + +describe("Book store Model", () => { + it('should have an index method', () => { + expect(store.index).toBeDefined(); + }); + + it('index menthod should return a list of products', async () => { + const result = await store.index(); + expect(result).toEqual([]); + }); + + it('should have a create method', () => { + expect(store.index).toBeDefined; + }); + + it('should have a update method', () => { + expect(store.index).toBeDefined; + }); + + it('create method should add a book', async () => { + const result = await store.create({ + title: '1984', + author: 'George Orwell', + pages: 42, + price: 10 + + }); + expect(result).toEqual({ + id: "1", + title: '1984', + author: 'George Orwell', + pages: 42, + price: 10 + }); + }); + + it('index method should return a list of books', async () => { + const result = await store.index(); + expect(result).toEqual([{ + id: "1", + title: '1984', + author: 'George Orwell', + pages: 42, + price: 10 + + }]); + }); + + it('show method should return a book', async () => { + const result = await store.show("1"); + expect(result).toEqual({ + id: "1", + title: '1984', + author: 'George Orwell', + pages: 42, + price: 10 + }); + }); + + it('delete method should remove the book', async () => { + store.delete("1"); + const result = await store.index() + + expect(result).toEqual([]); + }); +}); +