reworked product data shape

Vic
Vic 2 years ago
parent 7e29c42080
commit 16b733beab

@ -19,7 +19,7 @@ exports.setup = function(options, seedLink) {
}; };
exports.up = function(db) { exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20220514223735-books-table-up.sql'); var filePath = path.join(__dirname, 'sqls', '20220518182209-shelf-up.sql');
return new Promise( function( resolve, reject ) { return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err); if (err) return reject(err);
@ -34,7 +34,7 @@ exports.up = function(db) {
}; };
exports.down = function(db) { exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20220514223735-books-table-down.sql'); var filePath = path.join(__dirname, 'sqls', '20220518182209-shelf-down.sql');
return new Promise( function( resolve, reject ) { return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err); if (err) return reject(err);

@ -1,8 +0,0 @@
/* Replace with your SQL commands */
CREATE TABLE products (
id SERIAL PRIMARY KEY,
title VARCHAR(150),
author VARCHAR(255),
pages integer not null,
price integer not null
);

@ -0,0 +1,6 @@
/* Replace with your SQL commands */
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name VARCHAR(250) NOT NULL,
price INTEGER NOT NULL
);

@ -6,7 +6,7 @@
"scripts": { "scripts": {
"start": "nodemon src/server.ts", "start": "nodemon src/server.ts",
"watch": "tsc-watch --esModuleInterop src/server.ts --outDir ./dist --onSuccess \"node ./dist/server.js\"", "watch": "tsc-watch --esModuleInterop src/server.ts --outDir ./dist --onSuccess \"node ./dist/server.js\"",
"test": "ENV=test db-migrate --env test up && jasmine-ts && db-migrate db:drop test", "test": "cross-env ENV=test db-migrate --env test up && jasmine-ts && db-migrate db:drop test",
"build": "npx tsc", "build": "npx tsc",
"lint": "eslint --ext .ts", "lint": "eslint --ext .ts",
"prettier": "prettier \"src/**/*.ts\" --write", "prettier": "prettier \"src/**/*.ts\" --write",
@ -16,6 +16,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"cross-env": "^7.0.3",
"db-migrate": "^0.11.13", "db-migrate": "^0.11.13",
"db-migrate-pg": "^1.2.2", "db-migrate-pg": "^1.2.2",
"dotenv": "^16.0.0", "dotenv": "^16.0.0",

@ -4,30 +4,30 @@ import { Pool } from 'pg'
dotenv.config() dotenv.config()
const { const {
ENV,
POSTGRES_HOST, POSTGRES_HOST,
POSTGRES_DB, POSTGRES_DB,
POSTGRES_USER,
POSTGRES_PASSWORD,
POSTGRES_TEST_DB, POSTGRES_TEST_DB,
ENV, POSTGRES_USER,
POSTGRES_PASSWORD
} = process.env } = process.env
let client = new Pool(); let client = new Pool();
console.log(ENV) console.log(ENV)
if(ENV === 'test') { if(ENV === 'dev') {
client = new Pool({ client = new Pool({
host: POSTGRES_HOST, host: POSTGRES_HOST,
database: POSTGRES_TEST_DB, database: POSTGRES_DB,
user: POSTGRES_USER, user: POSTGRES_USER,
password: POSTGRES_PASSWORD, password: POSTGRES_PASSWORD,
}) })
} }
if(ENV === 'dev') { if(ENV === 'test') {
client = new Pool({ client = new Pool({
host: POSTGRES_HOST, host: POSTGRES_HOST,
database: POSTGRES_DB, database: POSTGRES_TEST_DB,
user: POSTGRES_USER, user: POSTGRES_USER,
password: POSTGRES_PASSWORD, password: POSTGRES_PASSWORD,
}) })

@ -2,10 +2,8 @@ import { Client, Connection } from 'pg';
import client from '../database'; import client from '../database';
export type Product = { export type Product = {
id?: string; id?: number;
title: string; name: string;
author: string;
pages: number;
price: number; price: number;
} }
@ -20,7 +18,7 @@ export class ProductStore {
conn.release() conn.release()
return result.rows return result.rows
} catch (err) { } catch (err) {
throw new Error(`Cannot get any product ${err}`) throw new Error(`Cannot get products ${err}`)
} }
} }
@ -42,18 +40,18 @@ export class ProductStore {
async create(p: Product): Promise<Product> { async create(p: Product): Promise<Product> {
try { try {
const sql = 'INSERT INTO products (title, author, pages, price) VALUES ($1, $2, $3, $4) RETURNING *;' const sql = 'INSERT INTO products (name, price) VALUES ($1, $2) RETURNING *;'
// @ts-ignore // @ts-ignore
const conn = await client.connect() const conn = await client.connect()
const result = await conn.query(sql, [p.title, p.author, p.pages, p.price]) const result = await conn.query(sql, [p.name, p.price])
const product = result.rows[0] const product = result.rows[0]
conn.release() conn.release()
return product return product
} catch (err) { } catch (err) {
throw new Error(`Could not add new product ${p.title}. Error: ${err}`) throw new Error(`Could not add new product ${p.name}. Error: ${err}`)
} }
} }
@ -62,8 +60,8 @@ export class ProductStore {
try { try {
const conn = await client.connect(); const conn = await client.connect();
const result = await conn.query( const result = await conn.query(
'UPDATE products SET title=$1, author=$2, pages=$3, price=$4 where id=$5 returning *;', 'UPDATE products SET name=$1, price=$2 WHERE id=$3 returning *;',
[p.title, p.author, p.pages, p.id] [p.name, p.price, p.id]
); );
conn.release() conn.release()
return result.rows[0] return result.rows[0]
@ -72,7 +70,7 @@ export class ProductStore {
} }
} }
async delete(id: string): Promise<Product> { async delete(id: number): Promise<Product> {
try { try {
const sql = 'DELETE FROM products WHERE id=(1$)' const sql = 'DELETE FROM products WHERE id=(1$)'
// @ts-ignore // @ts-ignore

@ -2,7 +2,7 @@ import { Product, ProductStore } from '../product'
const store = new ProductStore() const store = new ProductStore()
describe("Book store Model", () => { describe("Products store Model", () => {
it('should have an index method', () => { it('should have an index method', () => {
expect(store.index).toBeDefined(); expect(store.index).toBeDefined();
}); });
@ -22,29 +22,23 @@ describe("Book store Model", () => {
it('create method should add a product', async () => { it('create method should add a product', async () => {
const result = await store.create({ const result = await store.create({
title: '1984', name: '1984',
author: 'George Orwell', price: 5,
pages: 42,
price: 10
}); });
expect(result).toEqual({ expect(result).toEqual({
id: "1", id: 1,
title: '1984', name: '1984',
author: 'George Orwell', price: 5,
pages: 42,
price: 10
}); });
}); });
it('index method should return a list of products', async () => { it('index method should return a list of products', async () => {
const result = await store.index(); const result = await store.index();
expect(result).toEqual([{ expect(result).toEqual([{
id: "1", id: 1,
title: '1984', name: '1984',
author: 'George Orwell', price: 5,
pages: 42,
price: 10
}]); }]);
}); });
@ -52,16 +46,14 @@ describe("Book store Model", () => {
it('show method should return a product', async () => { it('show method should return a product', async () => {
const result = await store.show("1"); const result = await store.show("1");
expect(result).toEqual({ expect(result).toEqual({
id: "1", id: 1,
title: '1984', name: '1984',
author: 'George Orwell', price: 5,
pages: 42,
price: 10
}); });
}); });
it('delete method should remove the product', async () => { it('delete method should remove the product', async () => {
store.delete("1"); store.delete(1);
const result = await store.index() const result = await store.index()
expect(result).toEqual([]); expect(result).toEqual([]);

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save