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) {
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 ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
@ -34,7 +34,7 @@ exports.up = 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 ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
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": {
"start": "nodemon src/server.ts",
"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",
"lint": "eslint --ext .ts",
"prettier": "prettier \"src/**/*.ts\" --write",
@ -16,6 +16,7 @@
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"cross-env": "^7.0.3",
"db-migrate": "^0.11.13",
"db-migrate-pg": "^1.2.2",
"dotenv": "^16.0.0",

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

@ -2,10 +2,8 @@ import { Client, Connection } from 'pg';
import client from '../database';
export type Product = {
id?: string;
title: string;
author: string;
pages: number;
id?: number;
name: string;
price: number;
}
@ -20,7 +18,7 @@ export class ProductStore {
conn.release()
return result.rows
} 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> {
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
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]
conn.release()
return product
} 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 {
const conn = await client.connect();
const result = await conn.query(
'UPDATE products SET title=$1, author=$2, pages=$3, price=$4 where id=$5 returning *;',
[p.title, p.author, p.pages, p.id]
'UPDATE products SET name=$1, price=$2 WHERE id=$3 returning *;',
[p.name, p.price, p.id]
);
conn.release()
return result.rows[0]
@ -72,7 +70,7 @@ export class ProductStore {
}
}
async delete(id: string): Promise<Product> {
async delete(id: number): Promise<Product> {
try {
const sql = 'DELETE FROM products WHERE id=(1$)'
// @ts-ignore

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

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