Skip to content

Commit

Permalink
Add post model example
Browse files Browse the repository at this point in the history
  • Loading branch information
cyri113 committed May 20, 2023
1 parent 82bda1f commit 5a9f794
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
18 changes: 18 additions & 0 deletions __test__/models/post.model.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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()
})
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build": "npx tsc",
"start": "node dist/index.js",
"dev": "env-cmd nodemon ./src/index.ts",
"test": "docker compose -f docker-compose.test.yml up --exit-code-from app"
"test": "jest"
},
"repository": {
"type": "git",
Expand Down
16 changes: 16 additions & 0 deletions src/models/post.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import mongoose from 'mongoose'

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

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

export default model
3 changes: 0 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"paths": {
"@/*": ["./*"]
}
},
"include": [
"./src/**/*",
Expand Down

0 comments on commit 5a9f794

Please sign in to comment.