book.ts fix + migration fix + added book_spec
parent
7fa01964cd
commit
8325982227
@ -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
|
||||||
|
};
|
@ -1 +0,0 @@
|
|||||||
drop TABLE books
|
|
@ -0,0 +1,2 @@
|
|||||||
|
/* Replace with your SQL commands */
|
||||||
|
drop TABLE books
|
@ -1,7 +1,8 @@
|
|||||||
|
/* Replace with your SQL commands */
|
||||||
CREATE TABLE books (
|
CREATE TABLE books (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
title VARCHAR(150),
|
title VARCHAR(150),
|
||||||
author VARCHAR(255),
|
author VARCHAR(255),
|
||||||
price integer not null,
|
pages integer not null,
|
||||||
summary text
|
price integer not null
|
||||||
);
|
);
|
@ -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 {
|
const {
|
||||||
ENV,
|
POSTGRES_HOST,
|
||||||
POSTGRES_HOST,
|
POSTGRES_DB,
|
||||||
POSTGRES_DB,
|
POSTGRES_USER,
|
||||||
POSTGRES_USER,
|
POSTGRES_PASSWORD,
|
||||||
POSTGRES_PASSWORD
|
POSTGRES_TEST_DB,
|
||||||
} = process.env;
|
ENV,
|
||||||
|
} = process.env
|
||||||
|
|
||||||
let client = new Pool();
|
let client = new Pool();
|
||||||
if (ENV === 'dev') {
|
console.log(ENV)
|
||||||
client = new Pool({
|
|
||||||
host: POSTGRES_HOST,
|
if(ENV === 'test') {
|
||||||
database: POSTGRES_DB,
|
client = new Pool({
|
||||||
user: POSTGRES_USER,
|
host: POSTGRES_HOST,
|
||||||
password: POSTGRES_PASSWORD
|
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;
|
@ -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…
Reference in New Issue