Skip to content

Instantly share code, notes, and snippets.

View bagus2x's full-sized avatar
🎯
Focusing

tubagus saifulloh bagus2x

🎯
Focusing
View GitHub Profile
When designing schemas in Mongo DB, expecially the first times, a quick useful checklist to choose whether or not to use embedded documents in one-to-many relation.
So here is the checklist:
Type of query: ask yourself what could be the best model representation for the most frequent queries you'll need to do. Everytime I'll get the parent document I'll always (or really often) need all the child documents. Answer: Nested.
Data model lifecycle: think about the life cycle of the container document and its content: make sense that child documents will still have to exist when the parent document is deleted? If the answer is "no" nested is the way.
Snapshots: another reason that should affect your choice is the data you're representing and if the nested item is a snapshot of something happened at a given time. Suppose you're working with a "Receipt" object that contains a list of buyed products these will be copied as a nested documents in the receipt. You're storing an information related to a s
@bagus2x
bagus2x / html.tag.regex.js
Created December 29, 2020 12:51
simple regular expression to select html tag
<.*?(>)|<\/[\w]+>
import React, { createContext, Dispatch, useContext, useMemo } from 'react';
interface ActionsT {
INCREMENT: string;
DECREMENT: string;
}
export const Actions: ActionsT = {
INCREMENT: 'INCREMENT',
DECREMENT: 'DECREMENT'
@bagus2x
bagus2x / gosqlnull_test.go
Last active August 7, 2021 02:29
golang sql null to default value, and vice versa. json...
package gosqlnull
import (
"database/sql"
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse, CancelToken } from 'axios';
import { history } from '../App';
const BASE_URL = 'http://localhost:8080';
export const publicClient = axios.create({
baseURL: BASE_URL
});
export const privateClient = axios.create({
baseURL: BASE_URL
@bagus2x
bagus2x / react-query-when-button-is-clicked.ts
Created August 5, 2021 06:59
react query example when button is clicked
import React from 'react';
import { useQuery } from 'react-query';
function Playground() {
const { data, refetch } = useQuery(
'RANDOM',
async () => {
const res = await new Promise<number>((resolve) => setTimeout(() => resolve(Math.random()), 1000));
return res;
},
@bagus2x
bagus2x / useIntersectionObserver.ts
Created August 5, 2021 18:10
simple Intersection Observer hook
import { MutableRefObject, useEffect, useRef } from 'react';
interface Options<Target> {
root?: MutableRefObject<any>;
enabled?: boolean;
rootMargin?: string;
threshold: number;
onIntersect: (entry?: IntersectionObserverEntry, target?: Target) => void;
}
@bagus2x
bagus2x / cursor-based-pagination-example.txt
Created August 9, 2021 04:33
cursor based pagination example + non unique column
// Keunggulannya bisa untuk nonunique column timestamp
CREATE TABLE cek (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
created_at INT NOT NULL
);
INSERT INTO cek VALUES (DEFAULT, 'satu', 1);
INSERT INTO cek VALUES (DEFAULT, 'dua', 2);
mkdir ~/.go
echo "GOPATH=$HOME/.go" >> ~/.bashrc
echo "export GOPATH" >> ~/.bashrc
echo "PATH=\$PATH:\$GOPATH/bin # Add GOPATH/bin to PATH for scripting" >> ~/.bashrc
source ~/.bashrc
@bagus2x
bagus2x / print-hello-word-syscall.go
Created August 31, 2021 03:18
hello world stdout golang syscall
package main
import (
"syscall"
"unsafe"
)
func main() {
str := []byte("Hello World")
syscall.Syscall(syscall.SYS_WRITE, uintptr(syscall.Stdout), uintptr(unsafe.Pointer(&str[0])), uintptr(len(str)))