Skip to content

Commit

Permalink
edited for no cache or load balancing
Browse files Browse the repository at this point in the history
  • Loading branch information
cesartheroman committed Jun 3, 2023
1 parent 593eb90 commit 3bba403
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 103 deletions.
21 changes: 14 additions & 7 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
*/node_modules*
*/data*
# Ignore everything
**

.git
.vscode
.gitignore
# Allow files and directories
!/server
!/dist
!/.env
!/tsconfig.json
!/package*.json

docker-compose.yml
Dockerfile*
# Ignore unnecessary files inside allowed directories
# This should go after the allowed directories
**/*~
**/*.log
**/.DS_Store
**/Thumbs.db
10 changes: 2 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
FROM node:18
FROM node:18-alpine

WORKDIR /service

COPY package*.json ./

RUN npm install

COPY /server ./

COPY .env ./

COPY tsconfig.json ./
RUN npm install
26 changes: 13 additions & 13 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
build:
context: .
restart: on-failure
command: npm run start
command: npm start
# command: npm run dev
volumes:
- ./:/service
Expand All @@ -25,19 +25,19 @@ services:
POSTGRES_USER: ${PGUSER}
POSTGRES_DB: ${PGDATABASE}

redis:
image: redis:latest
ports:
- 6379:6379
# redis:
# image: redis:latest
# ports:
# - 6379:6379

nginx:
image: nginx:latest
ports:
- 3000:3000
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- api
# nginx:
# image: nginx:latest
# ports:
# - 3000:3000
# volumes:
# - ./nginx.conf:/etc/nginx/nginx.conf
# depends_on:
# - api

volumes:
pg_data:
9 changes: 4 additions & 5 deletions server/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ export const getOneProduct = async (
const { product_id } = req.params;

try {
// const [JsonBuildObject] = await readProductById(parseInt(product_id));
const [response] = await readProductById(parseInt(product_id));
// const { jsonb_build_object: product } = JsonBuildObject;
const [JsonBuildObject] = await readProductById(parseInt(product_id));
const { jsonb_build_object: product } = JsonBuildObject;

if (response) {
res.status(200).send(response);
if (product) {
res.status(200).send(product);
} else {
res.status(404).send(`Product ${product_id} does not exist`);
}
Expand Down
108 changes: 40 additions & 68 deletions server/models/definitions/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,26 @@ export const pgQueries = {
queryProductsList:
'SELECT * FROM products WHERE product_id > $1 ORDER BY product_id LIMIT $2',

// queryProductById: `SELECT jsonb_build_object(
// 'product_id', product_id,
// 'name', name,
// 'slogan', slogan,
// 'description', description,
// 'category', category,
// 'default_price', default_price,
// 'features', (
// SELECT ARRAY(
// SELECT jsonb_build_object(
// 'feature', features.feature,
// 'value', features.value
// )
// FROM features
// WHERE features.product_id = $1
// )
// )
// )
// FROM products
// WHERE product_id = $1;`,

queryProductById: `SELECT *,
(
SELECT array_to_json(array_agg(feature_cols)
)
FROM
(
SELECT feature, value
FROM features
WHERE features.product_id = $1) feature_cols
)
AS features
FROM products
WHERE products.product_id = $1;`,
queryProductById: `SELECT jsonb_build_object(
'product_id', product_id,
'name', name,
'slogan', slogan,
'description', description,
'category', category,
'default_price', default_price,
'features', (
SELECT ARRAY(
SELECT jsonb_build_object(
'feature', features.feature,
'value', features.value
)
FROM features
WHERE features.product_id = $1
)
)
)
FROM products
WHERE product_id = $1;`,

queryProductStyles: `SELECT jsonb_build_object(
'product_id', products.product_id,
Expand Down Expand Up @@ -88,40 +74,26 @@ export const redisQueries = {
queryProductsList: (product_id: number, limit: number) =>
`SELECT * FROM products WHERE product_id > ${product_id} ORDER BY product_id LIMIT ${limit}`,

// queryProductById: (product_id: number) => `SELECT jsonb_build_object(
// 'product_id', product_id,
// 'name', name,
// 'slogan', slogan,
// 'description', description,
// 'category', category,
// 'default_price', default_price,
// 'features', (
// SELECT ARRAY(
// SELECT jsonb_build_object(
// 'feature', features.feature,
// 'value', features.value
// )
// FROM features
// WHERE features.product_id = ${product_id}
// )
// )
// )
// FROM products
// WHERE product_id = ${product_id};`,

queryProductById: (product_id: number) => `SELECT *,
(
SELECT array_to_json(array_agg(feature_cols)
)
FROM
(
SELECT feature, value
FROM features
WHERE features.product_id = ${product_id}) feature_cols
)
AS features
FROM products
WHERE products.product_id = ${product_id};`,
queryProductById: (product_id: number) => `SELECT jsonb_build_object(
'product_id', product_id,
'name', name,
'slogan', slogan,
'description', description,
'category', category,
'default_price', default_price,
'features', (
SELECT ARRAY(
SELECT jsonb_build_object(
'feature', features.feature,
'value', features.value
)
FROM features
WHERE features.product_id = ${product_id}
)
)
)
FROM products
WHERE product_id = ${product_id};`,

queryProductStyles: (product_id: number) => `SELECT jsonb_build_object(
'product_id', products.product_id,
Expand Down
4 changes: 2 additions & 2 deletions server/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const readProductsList = async (
/* Read One Product */
export const readProductById = async (
product_id: number
): Promise<Product[]> => {
): Promise<JsonBuildObjectProduct[]> => {
const client = await db.connect();

try {
Expand All @@ -81,7 +81,7 @@ export const readProductById = async (
// return rows;
// }

const { rows }: { rows: Product[] } = await db.query(query);
const { rows }: { rows: JsonBuildObjectProduct[] } = await db.query(query);
return rows;
} catch (err) {
console.log('Error executing query: readProductById', err);
Expand Down

0 comments on commit 3bba403

Please sign in to comment.