Skip to content

Commit c4e574d

Browse files
committed
add cors - clean DB on startup - itemName unique
1 parent ab3da18 commit c4e574d

5 files changed

Lines changed: 47 additions & 0 deletions

File tree

examples_new/microservices/items/package-lock.json

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples_new/microservices/items/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@chronosrx/common": "^1.0.4",
2222
"axios": "^1.6.2",
2323
"cookie-parser": "^1.4.6",
24+
"cors": "^2.8.5",
2425
"dotenv": "^16.3.1",
2526
"express": "^4.18.2",
2627
"express-async-errors": "^3.1.1",
@@ -29,6 +30,7 @@
2930
},
3031
"devDependencies": {
3132
"@types/cookie-parser": "^1.4.6",
33+
"@types/cors": "^2.8.17",
3234
"@types/express": "^4.17.21",
3335
"@types/jest": "^29.5.11",
3436
"@types/jsonwebtoken": "^9.0.5",

examples_new/microservices/items/src/app.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ import express from 'express';
22
import 'express-async-errors';
33
import dotenv from 'dotenv';
44
dotenv.config();
5+
import cors from 'cors'
56
import { NotFoundError, errorHandler } from '@chronosrx/common';
67
import itemsRouter from './routes/item-router';
78
import eventRouter from './routes/event-router';
89
import cookieParser from 'cookie-parser';
910

1011
const app = express();
1112

13+
app.use(
14+
cors({
15+
credentials: true,
16+
origin: 'http://localhost:8080',
17+
})
18+
);
1219
app.use(express.json());
1320
app.use(cookieParser());
1421

examples_new/microservices/items/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { DbConnectionError } from '@chronosrx/common';
22
import { app } from './app';
33
import mongoose from 'mongoose';
4+
import { Item } from './models/items';
5+
import { User } from './models/users';
46

57
const PORT = process.env.PORT || 3001;
68

@@ -11,6 +13,10 @@ const start = async () => {
1113
try {
1214
await mongoose.connect(process.env.MONGO_URI, {});
1315
console.log('🍃 Connected to MongoDB');
16+
17+
// reset DB's
18+
await User.deleteMany();
19+
await Item.deleteMany();
1420
} catch (err) {
1521
throw new DbConnectionError();
1622
}

examples_new/microservices/items/src/models/items.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const itemSchema = new mongoose.Schema(
2020
itemName: {
2121
type: String,
2222
required: true,
23+
unique: true,
2324
},
2425
},
2526
{

0 commit comments

Comments
 (0)