Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Soumyajit Pathak drenther

🏠
Working from home
View GitHub Profile
@drenther
drenther / data.js
Last active Mar 5, 2019
Data for "Line up the Heroes"
View data.js
/** custom/data.js **/
export const COMICS = {
DC: 'dc',
MARVEL: 'marvel',
};
export const HEROES = [
{
name: 'Superman',
@drenther
drenther / example-queries-and-writes.js
Last active Jan 26, 2022
Example of using session with various mongoose methods
View example-queries-and-writes.js
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 Mar 13, 2019
An example of $session getter/setter example
View session-getter-example.js
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 Mar 13, 2019
Example of a WriteConflict and concurrency lock based errors
View writeConflict.js
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 Mar 13, 2019
Export all the models
View model.js
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 Mar 13, 2019
Handling mongoose transactions using runInTransaction helper function
View mongoose-transact-utils.js
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 Sep 21, 2021
A gettings started example for mongoose transactions
View init.js
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
View Simple Weather App.markdown
@drenther
drenther / autoFeedback.js
Created Sep 15, 2018
A tiny script to simplify Amizone feedback
View autoFeedback.js
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');
@drenther
drenther / VisibilityManager.js
Created Aug 22, 2018
VisibilityManager.js for react-page-visibility-example
View VisibilityManager.js
import { Component } from "react";
import { pageVisibilityApi } from "../pageVisibilityUtils";
const { hidden, visibilityChange } = pageVisibilityApi();
class VisibilityManager extends Component {
state = {
isVisible: true
};