This is a simple Weather App.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** App.js **/ | |
import { HEROES, COMICS } from './custom/data'; | |
import { shuffle, GAME_STATE } from './custom/utils'; | |
const initialState = { | |
// we initialize the state by populating the bench with a shuffled collection of heroes | |
bench: shuffle(HEROES), | |
[COMICS.DC]: [], | |
[COMICS.MARVEL]: [], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** custom/data.js **/ | |
export const COMICS = { | |
DC: 'dc', | |
MARVEL: 'marvel', | |
}; | |
export const HEROES = [ | |
{ | |
name: 'Superman', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { runInTransaction } = require('mongoose-transact-utils'); | |
const { User } = require('./models'); | |
async function example() { | |
await runInTransaction(async session => { | |
// all the query methods listed here - https://mongoosejs.com/docs/queries.html | |
// session works with query methods as follows - | |
const user = await User.findOne({ }).session(session); | |
// as mentioned earlier, if you use save |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { runInTransaction } = require('mongoose-transact-utils'); | |
const { User } = require('./models/'); | |
async function someOperationsWithTransaction() { | |
let john; | |
await runInTransaction(async session => { | |
john = await User.findOne({ name: 'John' }).session(session); | |
// the same session | |
console.log(john.$session() === session); // returns true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function updateUserAge(userName, session) { | |
// update the user | |
} | |
// SENARIO - 1 | |
// this will throw a WriteConflict error because of Concurrency Lock | |
await Promise.all([ | |
runInTransaction(async session => { await updateUserAge('John', session) }), | |
runInTransaction(async session => { await updateUserAge('John', session) }), | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose'); | |
const personSchema = require('./models/person'); | |
const dogSchema = require('./models/dog'); | |
const catSchema = require('./models/cat'); | |
const models = { | |
Person: mongoose.model('Person', personSchema), | |
Dog: mongoose.model('Dog', dogSchema), | |
Cat: mongoose.model('Cat', catSchema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { runInTransaction } = require('mongoose-transact-utils'); | |
const { User } = require('./models'); | |
// any queries or write you want to do using transaction | |
(async () => { | |
// runInTransction catches any error in the callback to abort the transaction session | |
// and then rethrows the error for you to handle the reporting | |
await runInTransaction(async session => { | |
// run any queries here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose'); | |
// the run-rs command will by default start the replica sets on the following ports | |
const dbUri = 'mongodb://localhost:27017,localhost:27018,localhost:27019/example'; | |
async function init() { | |
// connecting the DB | |
await mongoose.connect(dbUri, { replicaSet: 'rs' }); | |
// a simple mongoose model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function rate(rating) { | |
const submit = document.querySelector(`input[type="submit"]`); | |
submit.removeAttribute('onclick'); | |
if (![0, 1, 2, 3, 4].includes(rating)) { | |
console.log("rate(0) for Strongly Agree, rate(1) for Agree, rate(2) for Neutral, rate(4) for Strongly Disagree"); | |
} else { | |
const inputs = [...document.querySelectorAll(`input[type="radio"]`)]; | |
const ratings = inputs.filter( x => x.id.endsWith(`Rating_${rating}`) ); | |
const questions = inputs.filter( x => x.id.includes(`Question`)); | |
const text = document.querySelector('textarea'); |