Skip to content

Instantly share code, notes, and snippets.

View bearzk's full-sized avatar

Kai Zhang bearzk

View GitHub Profile
@bearzk
bearzk / .eslintrc.js
Last active February 20, 2024 21:03
eslint example
module.exports = {
'env': {
'browser': true,
'es2021': true
},
'extends': [
// "standard-with-typescript",
'eslint:recommended',
'plugin:react/recommended',
'plugin:import/errors',
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install fzf node raycast obsidian bitwarden zsh iterm2 localstack slack orbstack autojump bat the_silver_searcher jq go python vim netnewswire keycastr awscli redisinsight keybase obs insomnium grammarly-desktop vscodium git neteasemusic fortune httpie iterm2 zed marta vlc starship
# hold keys should repeat it in the app
# defaults domains | grep codium
defaults write com.vscodium ApplePressAndHoldEnabled -bool false

Idempotent array push

db.myRecords.findAndModify(
    {
      _id: recordId, 
      itemArray: {$not: { $elemMatch: { itemId: newItem.id } }}
    },
    { 
      $push: {itemArray: newItem}

Single Table Design for Dashboards

basic concept to read: https://www.dynamodbguide.com/

Also, as MongoDB is much more flexible about indices than DDB, it's less dangerous to try out STD in MongoDB.

Table Design

basic

@bearzk
bearzk / async-await-non-blocking.md
Last active April 11, 2023 09:27
async await non-blocking example

async await non-blocking

In the context of the node process that handles multiple http requests, async await is non-blocking. Though in each request, async await will block.

See this example, calling the 2 endpoints /fast and /slow, /slow will respond after 3 seconds, you can call the /fast

@bearzk
bearzk / StreamToString.js
Created March 15, 2023 21:12
Stream to string
async function streamToString(stream) {
return await new Promise((resolve, reject) => {
const chunks = [];
stream.on('data', (chunk) => chunks.push(chunk));
stream.on('error', reject);
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf-8')));
});
}
@bearzk
bearzk / docker.md
Last active March 6, 2023 12:56
tiny crash course to get containers running

docker cli crash course

TL;DR

you have docker installed on your system, you want to install the services we use for API project

$ docker run -d --name mongo4.4 -p 27017:27017 mongo:4.4

$ docker run -d --name rabbitmq -p 4369:4369 -p 5671-5672:5671-5672 -p 15671-15672:15671-15672 -p 15691-15692:15691-15692 -p 25672:25672 rabbitmq:3.8-management-alpine
@bearzk
bearzk / setup.sh
Created October 9, 2022 18:02
setup .env from .env.example
# put setup script here
# - create default config file
# - etc
echo "########################"
echo "## setting up project ##"
echo "########################"
echo
echo
@bearzk
bearzk / peek-stream.js
Created October 6, 2022 23:49
peek into stream
const { pipeline: pipelineAsCallback, Transform, Readable } = require('stream');
const { promisify } = require('util');
const pipeline = promisify(pipelineAsCallback);
const debugStream = new Transform({
objectMode: true,
transform(chunk, encoding, callback) {
console.log('chunk');
console.log(JSON.stringify(chunk, null, 2));
@bearzk
bearzk / js-function-expression-declaration.js
Last active October 4, 2022 08:57
js function expression & declaration
plus(1, 2) // this won't complain, as add() is hoisted
minus(2, 1) // this will complain, as minus is defined later
// expression
function plus(x, y) {
return x + y;
}
// declaration
const minus = function (x, y) {