Skip to content

Instantly share code, notes, and snippets.

View fahmifan's full-sized avatar

fahmi irfan fahmifan

View GitHub Profile
@fahmifan
fahmifan / sql_gorm.go
Created March 16, 2024 22:24
Get SQL Tx from Gorm
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
func DBTxFromGorm(tx *gorm.DB) (DBTX, bool) {
dbtx, ok := tx.Statement.ConnPool.(*sql.Tx)
return dbtx, ok
@fahmifan
fahmifan / debug-slow-prisma-query.ts
Last active August 25, 2023 04:14
How to log slow prisma query, tested on Prisma 3
const prismaClient = new PrismaClient({
log: ['query'] // must enable this to log
});
// Log slow query with all parameters in place,
// so you can copy pasted it and do EXPLAIN query
prismaClient.$on('query' as any, (e: any) => {
const dur = Number.parseInt(e?.duration);
const maxQueryTime = 100; // in ms
if (dur <= maxQueryTime) return;
@fahmifan
fahmifan / setup.sh
Last active June 26, 2022 00:34
Setup for Accessing Go Private Repository
# add your ssh public key to github & gitlab
# set GOPRIVATE, for github & gitlab
GOPRIVATE="gitlab.com/<your-private-group>,github.com/<your-private-group>"
# if you use a private hosted gitlab
GOPRIVATE="gitlab.yoursite.com"
# set github to use ssh
git config --global url."git@github.com:".insteadOf "https://github.com/"
@fahmifan
fahmifan / aes.go
Last active June 24, 2022 03:44
simple AES encryption using cfb in Golang
package cryptor
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
)
@fahmifan
fahmifan / copy-to-clipboard.js
Created March 9, 2022 10:11
Copy To Clipboard Browser Bookmarklet
javascript:(function() {function copyToClipboard(text) { if (window.clipboardData && window.clipboardData.setData) { /*IE specific code path to prevent textarea being shown while dialog is visible.*/ return clipboardData.setData("Text", text); } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) { var textarea = document.createElement("textarea"); textarea.textContent = text; textarea.style.position = "fixed"; /* Prevent scrolling to bottom of page in MS Edge.*/ document.body.appendChild(textarea); textarea.select(); try { return document.execCommand("copy"); /* Security exception may be thrown by some browsers.*/ } catch (ex) { console.warn("Copy to clipboard failed.", ex); return false; } finally { document.body.removeChild(textarea); } }}var markdown = '[' + document.title + '](' + window.location.href + ')';var selection = window.getSelection(
@fahmifan
fahmifan / Dockerfile
Last active November 26, 2019 03:34
Deploy React using Docker + NGINX
FROM nginx:alpine
COPY ./build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
@fahmifan
fahmifan / upload_file_multer.js
Created October 1, 2019 05:10
Upload file using multer
const path = require('path')
const fs = require('fs')
const express = require('express')
const bodyParser = require('body-parser')
const multer = require('multer');
const crypto = require('crypto');
const db = require('./db/db.json')
@fahmifan
fahmifan / sqlmock_test.go
Created July 23, 2019 02:48
sqlmock for select query with and without regex
// with empty New(), you should use regex `(.+)` and escape the `$` using `\\`
db, mock, _ := sqlmock.New()
mock.ExpectQuery("SELECT (.+) FROM \"ARTICLES\" WHERE \"SLUG\" = \\$1").
WithArgs("foobar").
WillReturnRows(
sqlmock.NewRows(columns).AddRow(
"28e8a227-4ebd-43b8-9631-108392ed2ba8",
"foobar",
"Foobar",
"Body foobar",
@fahmifan
fahmifan / auth_handler.go
Created June 25, 2019 00:57
Inventory App
// AuthHandler handle user auth
type AuthHandler interface {
Authorize(w http.ResponseWriter, r *http.Request)
Login(w http.ResponseWriter, r *http.Request)
}
type authHandler struct {
userService app.UserService
}
@fahmifan
fahmifan / Prim.java
Created June 2, 2019 09:01
Prims Java by GOG
// A Java program for Prim's Minimum Spanning Tree (MST) algorithm.
// The program is for adjacency matrix representation of the graph
import java.util.*;
import java.lang.*;
import java.io.*;
public class Prim
{
// Number of vertices in the graph