Skip to content

Commit

Permalink
npx prettier --write .
Browse files Browse the repository at this point in the history
  • Loading branch information
cyri113 committed May 20, 2023
1 parent 5a9f794 commit cbe8b68
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 48 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ docker compose -f docker-compose.dev.yml up
#### Supported Services

- MongoDB
- Redis
- Redis
40 changes: 23 additions & 17 deletions __test__/models/post.model.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import Post from '../../src/models/post.model'
import Post from "../../src/models/post.model";

describe('Post model', () => {
describe('validation', () => {
test('should validate a post', async function () {
await expect(new Post({
title: 'Lorem ipsum',
content: 'Lorem ipsum'
}).validate()).resolves.toBeUndefined()
})
test('should have a title', async () => {
await expect(new Post({ title: undefined, content: '' }).validate()).rejects.toThrow()
})
test('should have content', async () => {
await expect(new Post({ title: '', content: undefined }).validate()).rejects.toThrow()
})
})
})
describe("Post model", () => {
describe("validation", () => {
test("should validate a post", async function () {
await expect(
new Post({
title: "Lorem ipsum",
content: "Lorem ipsum",
}).validate()
).resolves.toBeUndefined();
});
test("should have a title", async () => {
await expect(
new Post({ title: undefined, content: "" }).validate()
).rejects.toThrow();
});
test("should have content", async () => {
await expect(
new Post({ title: "", content: undefined }).validate()
).rejects.toThrow();
});
});
});
35 changes: 20 additions & 15 deletions src/config/env.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
import Joi from 'joi'
import Joi from "joi";

const schema = Joi
.object()
const schema = Joi.object()
.keys({
PORT: Joi.number().required().default(3000),
MONGODB_HOST: Joi.string().required().example('localhost'),
MONGODB_HOST: Joi.string().required().example("localhost"),
MONGODB_PORT: Joi.number().required().example(27017),
MONGODB_USER: Joi.string().required().example('root'),
MONGODB_PASS: Joi.string().required().example('pass'),
MONGODB_NAME: Joi.string().optional().example('db'),
REDIS_QUEUE_HOST: Joi.string().optional().example('localhost'),
REDIS_QUEUE_PORT: Joi.number().optional().example(6379)
MONGODB_USER: Joi.string().required().example("root"),
MONGODB_PASS: Joi.string().required().example("pass"),
MONGODB_NAME: Joi.string().optional().example("db"),
REDIS_QUEUE_HOST: Joi.string().optional().example("localhost"),
REDIS_QUEUE_PORT: Joi.number().optional().example(6379),
})
.unknown()
.unknown();

const { value: env, error } = schema.prefs({ errors: { label: 'key' } }).validate(process.env)
const { value: env, error } = schema
.prefs({ errors: { label: "key" } })
.validate(process.env);

if (error != null) {
throw new Error(`Config validation error: ${error.message}`)
throw new Error(`Config validation error: ${error.message}`);
}

// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const MONGODB_URL = `mongodb://${env.MONGODB_USER}:${env.MONGODB_PASS}@${env.MONGODB_HOST}:${env.MONGODB_PORT}/${env.MONGODB_NAME !== undefined ? env.MONGODB_NAME : ''}`
const MONGODB_URL = `mongodb://${env.MONGODB_USER}:${env.MONGODB_PASS}@${
env.MONGODB_HOST
}:${env.MONGODB_PORT}/${
env.MONGODB_NAME !== undefined ? env.MONGODB_NAME : ""
}`;

export default {
...env,
MONGODB_URL
}
MONGODB_URL,
};
18 changes: 11 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import mongoose from "mongoose";
import app from "./app";
import { env } from "./config";

mongoose.set('strictQuery', true)
mongoose.set("strictQuery", true);

main().catch(err => { console.error(err) })
main().catch((err) => {
console.error(err);
});

async function main (): Promise<void> {
await mongoose.connect(env.MONGODB_URL)
async function main(): Promise<void> {
await mongoose.connect(env.MONGODB_URL);
app.listen(env.PORT, () => {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
console.log(`⚡️[server]: Server is running at http://localhost:${env.PORT}`)
})
}
console.log(
`⚡️[server]: Server is running at http://localhost:${env.PORT}`
);
});
}
14 changes: 7 additions & 7 deletions src/models/post.model.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import mongoose from 'mongoose'
import mongoose from "mongoose";

const schema = new mongoose.Schema({
title: {
type: String,
required: true
required: true,
},
content: {
type: String,
required: true
}
})
required: true,
},
});

const model = mongoose.model('Post', schema)
const model = mongoose.model("Post", schema);

export default model
export default model;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"skipLibCheck": true
},
"include": [
"./src/**/*",
Expand Down

0 comments on commit cbe8b68

Please sign in to comment.