Skip to content

Commit 53d72b9

Browse files
committed
Add scripts to create
1 parent 272b22f commit 53d72b9

7 files changed

Lines changed: 14035 additions & 8 deletions

File tree

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"test": "jest"
99
},
1010
"devDependencies": {
11+
"@types/faker": "^4.1.12",
1112
"@types/jest": "^25.1.4",
13+
"@types/mongoose": "^5.7.28",
1214
"@types/node": "^13.9.3",
1315
"jest": "^25.1.0",
1416
"nodemon": "^2.0.2",
@@ -20,5 +22,9 @@
2022
},
2123
"jest": {
2224
"preset": "ts-jest"
25+
},
26+
"dependencies": {
27+
"faker": "^4.1.0",
28+
"mongoose": "^5.9.20"
2329
}
2430
}

src/mongoose.connection.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import mongoose from 'mongoose'
2+
3+
export const executeScript = async (baby: () => Promise<unknown>) => {
4+
try {
5+
await mongoose.connect('mongodb://dante:SoNEfaxEMPEr4@ds111138.mlab.com:11138/temp-fix-gm', {
6+
useNewUrlParser: true
7+
})
8+
9+
await baby()
10+
} catch (error) {
11+
console.error(error)
12+
process.exit(1)
13+
} finally {
14+
await mongoose.connection.close()
15+
}
16+
}

src/post.schema.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import mongoose, { Schema } from 'mongoose'
2+
3+
const Image = new Schema({
4+
src: String,
5+
alt: String
6+
})
7+
8+
const Comments = new Schema({
9+
message: String,
10+
images: [Image]
11+
})
12+
13+
const PostSchema = new Schema({
14+
name: String,
15+
firstLevelImagePath: String,
16+
images: [Image],
17+
comments: [Comments],
18+
secondLevelImage: Image
19+
})
20+
21+
export const Post = mongoose.model('Post', PostSchema)

src/scripts/data/all.json

Lines changed: 13751 additions & 0 deletions
Large diffs are not rendered by default.

src/scripts/insert-data.script.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { readFileSync } from 'fs'
2+
import path from 'path'
3+
import { executeScript } from '../mongoose.connection'
4+
import { Post } from '../post.schema'
5+
6+
executeScript(main)
7+
8+
const allDataFilePath = path.join(path.dirname(__filename), './data/all.json')
9+
console.log('Dante: allDataFilePath', allDataFilePath)
10+
11+
async function main() {
12+
const file = await readFileSync(allDataFilePath)
13+
const data = JSON.parse(file.toString())
14+
15+
await Post.insertMany(data)
16+
17+
console.log('All data inserted successfully')
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { executeScript } from '../mongoose.connection'
2+
import { Post } from '../post.schema'
3+
4+
executeScript(main)
5+
6+
async function main() {
7+
const posts = await Post.find({})
8+
console.log('Dante: main -> posts', posts)
9+
}

0 commit comments

Comments
 (0)