book.ts fix + migration fix + added book_spec

Vic
Vic 2 years ago
parent 7fa01964cd
commit 8325982227

@ -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"
}
}

@ -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
};

@ -0,0 +1,2 @@
/* Replace with your SQL commands */
drop TABLE books

@ -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
);

@ -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",

@ -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
export default client;

@ -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<Book> {
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<Book> {
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}`)
}
}
}

@ -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([]);
});
});
Loading…
Cancel
Save