Skip to content

Instantly share code, notes, and snippets.

View neerajkumar161's full-sized avatar
💭
Looking for Node.js Backend TypeScript Role

Neeraj Kumar neerajkumar161

💭
Looking for Node.js Backend TypeScript Role
View GitHub Profile
@neerajkumar161
neerajkumar161 / user.ts
Created December 27, 2021 16:00
src/models/user.ts
import {model, Schema} from 'mongoose'
const user = new Schema(
{
firstname: {type: String},
lastname: {type: String},
age: {type: Number},
email: {type: String},
password: {type: String}
},
@neerajkumar161
neerajkumar161 / server.ts
Last active December 29, 2021 14:15
server.ts
import {ApolloServerPluginDrainHttpServer} from 'apollo-server-core'
import {ApolloServer} from 'apollo-server-express'
import config from 'config'
import express from 'express'
import {GraphQLError} from 'graphql'
import http from 'http'
import {connectMongoDB} from './db/mongodb'
import {mainSchema} from './src/schema-loader' // will create in next steps, skip for now
const port = config.get('port') || 5000
@neerajkumar161
neerajkumar161 / mongodb.ts
Last active December 25, 2021 07:57
db/mongodb.ts
import config from 'config'
import {connect} from 'mongoose' // import connect method from mongoose
const MONGO_URI: string = config.get('db_url') // This will get the db_url, that we've declared in config/default.json file
export const connectMongoDB = async () => {
try {
const res = await connect(MONGO_URI)
if (res) console.log('Connected to MongoDB')
} catch (error) {
@neerajkumar161
neerajkumar161 / default.json
Last active January 5, 2022 04:38
config/default.json
{
"port": 8000,
"db_url": "mongodb+srv://<username>:<password>@sandbox.ejy8i.mongodb.net/graphqlDatabase?retryWrites=true&w=majority"
}
@neerajkumar161
neerajkumar161 / package.json
Last active January 15, 2022 15:56
Package.json after installing dependencies and adding start script
{
"name": "node-graphql-curd",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "pm2 start server.js --watch"
},
"keywords": [],
--config
--db
--src
--models
--resolvers
--schemas
server.ts