ADD: Seeder and User Seeder

ADD: Chalk package
This commit is contained in:
2025-01-02 16:56:18 +03:30
parent 928a87564a
commit d96cb36bdf
6 changed files with 140 additions and 6 deletions
+27
View File
@@ -0,0 +1,27 @@
const User = require('./userModel')
const chalk = require('chalk')
const runSeeder = async () => {
const users = [
{
_id: '66ddb52302f30f105de39e61',
name: 'main admin',
email: 'superadmin@chayamarket.ir',
password: '$2a$10$pabxmRjm6klipAC6vBvgI.J.GJpnG//L1MeNcdbtxuYQRYHLI/I3O',
phoneNumber: '+989123456789',
favorites: [],
isVerified: false,
isCompleted: false,
shippingAddress: [],
bankAccount: [],
role: '66a2da5b9b989a8909948848',
},
]
await User.deleteMany({})
console.log(chalk.red('Deleted all user records.'))
await User.insertMany(users, null)
console.log(chalk.green.bold(`USER: Created ${users.length} user(s)`))
}
module.exports = { runSeeder }
+58 -5
View File
@@ -11,7 +11,7 @@
"dependencies": { "dependencies": {
"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"body-parser": "^1.20.3", "body-parser": "^1.20.3",
"chalk": "^5.3.0", "chalk": "^4.1.2",
"compression": "^1.7.5", "compression": "^1.7.5",
"cookie-parser": "^1.4.7", "cookie-parser": "^1.4.7",
"cors": "^2.8.5", "cors": "^2.8.5",
@@ -121,6 +121,20 @@
"node": ">= 0.6" "node": ">= 0.6"
} }
}, },
"node_modules/ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
"dependencies": {
"color-convert": "^2.0.1"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
}
},
"node_modules/anymatch": { "node_modules/anymatch": {
"version": "3.1.3", "version": "3.1.3",
"resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz",
@@ -261,16 +275,39 @@
} }
}, },
"node_modules/chalk": { "node_modules/chalk": {
"version": "5.3.0", "version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-5.3.0.tgz", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==", "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"dependencies": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
},
"engines": { "engines": {
"node": "^12.17.0 || ^14.13 || >=16.0.0" "node": ">=10"
}, },
"funding": { "funding": {
"url": "https://github.com/chalk/chalk?sponsor=1" "url": "https://github.com/chalk/chalk?sponsor=1"
} }
}, },
"node_modules/chalk/node_modules/has-flag": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
"engines": {
"node": ">=8"
}
},
"node_modules/chalk/node_modules/supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"dependencies": {
"has-flag": "^4.0.0"
},
"engines": {
"node": ">=8"
}
},
"node_modules/chokidar": { "node_modules/chokidar": {
"version": "3.6.0", "version": "3.6.0",
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz", "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz",
@@ -303,6 +340,22 @@
"node": ">=0.10.0" "node": ">=0.10.0"
} }
}, },
"node_modules/color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"dependencies": {
"color-name": "~1.1.4"
},
"engines": {
"node": ">=7.0.0"
}
},
"node_modules/color-name": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA=="
},
"node_modules/compressible": { "node_modules/compressible": {
"version": "2.0.18", "version": "2.0.18",
"resolved": "https://registry.npmjs.org/compressible/-/compressible-2.0.18.tgz", "resolved": "https://registry.npmjs.org/compressible/-/compressible-2.0.18.tgz",
+2 -1
View File
@@ -3,6 +3,7 @@
"version": "1.0.0", "version": "1.0.0",
"main": "app.js", "main": "app.js",
"scripts": { "scripts": {
"seed": "node ./seeder/seed.js",
"start": "node app.js", "start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
@@ -13,7 +14,7 @@
"dependencies": { "dependencies": {
"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"body-parser": "^1.20.3", "body-parser": "^1.20.3",
"chalk": "^5.3.0", "chalk": "^4.1.2",
"compression": "^1.7.5", "compression": "^1.7.5",
"cookie-parser": "^1.4.7", "cookie-parser": "^1.4.7",
"cors": "^2.8.5", "cors": "^2.8.5",
+6
View File
@@ -0,0 +1,6 @@
require('dotenv').config({ path: './config/.env' })
const express = require('express')
const seederDBConnection = require('./seederDBConnection.js')
const app = express()
seederDBConnection(app)
+42
View File
@@ -0,0 +1,42 @@
const mongoose = require('mongoose')
const port = process.env.PORT || 3000
const env = process.env.NODE_ENV || 'development'
const redisClient = require('../setup/redis')
const seederRunner = require('./seederRunner')
const chalk = require('chalk')
module.exports = (app) => {
let connectionString
if (env === 'development') {
connectionString = process.env.MONGODB_URL
} else if (env === 'test') {
connectionString = process.env.MONGODB_URL
} else if (env === 'production') {
connectionString = process.env.MONGODB_URL
}
console.log(env)
mongoose
.connect(connectionString, {})
.then(function () {
console.log('MongoDB Database Connected')
redisClient.connect().then((r) => {
console.log('Redis connection Established')
app.listen(port, function () {
console.log('Server Started On Port ' + port)
console.log(
chalk.red.bold(
'-------- Running Seeders ---------'
)
)
seederRunner.run().then((r) => {
console.log(chalk.blue.bold('Seeding is done.'))
process.exit(0)
})
})
})
})
.catch(function (err) {
console.log(err)
})
}
+5
View File
@@ -0,0 +1,5 @@
const userSeeder = require('../components/user/userSeeder')
module.exports.run = async () => {
await userSeeder.runSeeder()
}