Skip to content

Commit

Permalink
Refactor(): write a script to add data to database
Browse files Browse the repository at this point in the history
- delete old data from the database
- added a new Post and comment data
- change the type of _id in Comment Model
  • Loading branch information
purnimagupta committed Sep 9, 2019
1 parent 2927835 commit cb9069a
Show file tree
Hide file tree
Showing 10 changed files with 543 additions and 12 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach by Process ID",
"processId": "${command:PickProcess}"
},

]
}
6 changes: 1 addition & 5 deletions src/app1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ class App {
this.app.use(express.static(path.join(__dirname, 'public')));
this.app.use(bodyParser.json());





let sessionStore = new RedisStore({
port: 6379,
db: 2,
Expand Down Expand Up @@ -101,7 +97,7 @@ class App {
})
}

//Connect to database
//Connect to database
private connectToTheDatabase() {
let dbURL = 'mongodb://localhost:27017/reduxtagram-server';
const { MONGODB_URL } = process.env
Expand Down
11 changes: 7 additions & 4 deletions src/database/db-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export {};
const User = require('../models/user-model');
const Post = require('../models/posts-model');
const Comment = require('../models/comment-model');
const ObjectId = require('mongoose').Types.ObjectId;

exports.fetchUserByEmail = function(email: string) {
return User.findOne({
Expand Down Expand Up @@ -32,13 +33,15 @@ exports.getPosts = function() {
.catch((err: any) => console.log("err occured in fetchUserByEmail", err))
}

exports.fetchSinglePost = function(code: string) {
return Post.findOne({code: code}).exec()
exports.fetchSinglePost = function(id: string) {
return Post.findOne({_id: id}).exec()

}

// Fetch Comments

exports.fetchCommentsForPost = function(postCode: string) {
return Comment.findOne({postCode: postCode}).exec()
exports.fetchCommentsForPost = function(postId: string) {
return Comment.findOne({
postId: new ObjectId(postId)
}).exec()
}
53 changes: 53 additions & 0 deletions src/database/insert-sample-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const Comment = require('../models/comment-model');
const Post = require('../models/posts-model');
var mongoose = require('mongoose');
import Post from '../interfaces/post.interface';
import Comments from '../interfaces/comment.interface';
const samplePosts = require("./sample-posts");
const sampleComments = require("./sample-comments");

function insertPostsAndComments() {

let dbURL = 'mongodb://localhost:27017/reduxtagram-server';

mongoose.connect(dbURL, { useNewUrlParser: true}, function(err: any){
if(err) {
console.log('Error connecting to: ', dbURL)
}
else {
console.log('Connected to : ' + dbURL);

Post.collection.insert(samplePosts.default, async function (err: any, docs: any) {
if (err){
return console.error(err);
} else {
const posts = await Post.find({})
.exec()
.catch((err: any) => console.log("err occured in fetching posts", err));

const comments: Comments[] = []
posts.map((post:any, i: any) => {
comments.push({
...sampleComments.default[i],
postId: post._id
})
});

Comment.collection.insert(comments, async function(err: any, docs: any){
if(err) {
return console.error(err)
} else {
const comments = await Comment.find({})
.exec()
.catch((err: any) => console.log("err occured in fetching comments", err))
console.log("comments");
console.log(comments);
}
})
}
});
}
});
}

insertPostsAndComments();
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
}
{
"postCode": "BAPIPRjQce9",
"commnets": [
"comments": [
{
"text":"Those are cute! They're like silver dollar pancakes.",
"user": "rrsimonsen"
Expand Down
File renamed without changes.
Loading

0 comments on commit cb9069a

Please sign in to comment.