Skip to content

Instantly share code, notes, and snippets.

View drenther's full-sized avatar
🏠
Working from home

Soumyajit Pathak drenther

🏠
Working from home
View GitHub Profile
@drenther
drenther / initialState.js
Last active March 5, 2019 17:59
Initial State for Line up the Heroes and the shuffle algorithm
/** 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]: [],
@drenther
drenther / data.js
Last active March 5, 2019 17:55
Data for "Line up the Heroes"
/** custom/data.js **/
export const COMICS = {
DC: 'dc',
MARVEL: 'marvel',
};
export const HEROES = [
{
name: 'Superman',
@drenther
drenther / example-queries-and-writes.js
Last active January 13, 2023 00:21
Example of using session with various mongoose methods
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
@drenther
drenther / session-getter-example.js
Last active March 13, 2019 11:41
An example of $session getter/setter example
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
@drenther
drenther / writeConflict.js
Last active March 13, 2019 11:01
Example of a WriteConflict and concurrency lock based errors
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) }),
]);
@drenther
drenther / model.js
Last active March 13, 2019 10:30
Export all the models
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)
@drenther
drenther / mongoose-transact-utils.js
Last active March 13, 2019 11:42
Handling mongoose transactions using runInTransaction helper function
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
@drenther
drenther / init.js
Last active September 21, 2021 13:49
A gettings started example for mongoose transactions
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
@drenther
drenther / autoFeedback.js
Created September 15, 2018 14:00
A tiny script to simplify Amizone feedback
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');