Skip to content

Instantly share code, notes, and snippets.

View ali-master's full-sized avatar
🎯
Focusing on Something new!

Ali Torki ali-master

🎯
Focusing on Something new!
View GitHub Profile
@ali-master
ali-master / README.md
Created July 15, 2024 19:49
UUIDv7 in typescript

UUIDv7 is a 128-bit unique identifier like it's older siblings, such as the widely used UUIDv4. But unlike v4, UUIDv7 is time-sortable with 1 ms precision. By combining the timestamp and the random parts, UUIDv7 becomes an excellent choice for record identifiers in databases, including distributed ones.

Let's briefly explore the UUIDv7 structure and move on to the zero-dependency implementations in 33 languages (as ranked by the Stack Overflow survey).

These implementations may not be the fastest or most idiomatic, but they are concise and easy to understand. Many of the code examples are interactive (but the results are cached, so you won't see different UUIDs very often).

@ali-master
ali-master / hot-keys.hook.ts
Created July 11, 2024 13:26
React Keyboard hot keys Hooks
import { useEffect } from "react";
import hotkeys from "hotkeys-js";
// Types
import type { KeyHandler } from "hotkeys-js";
/**
* Filter function to ignore hotkeys when typing in an input, textarea or select
* @param {KeyboardEvent} event
* @returns {boolean}
*/
@ali-master
ali-master / nestjs-retry.ts
Created June 20, 2024 04:05
Retry Pattern in NestJS
import { Injectable } from '@nestjs/common';
import { AxiosResponse } from 'axios';
export type AxiosMethod = () => Promise<AxiosResponse>;
@Injectable()
export class Retry {
constructor() {}
async retry(
@ali-master
ali-master / ubuntu-docker-install.sh
Created April 28, 2024 08:15 — forked from kolosek/ubuntu-docker-install.sh
Ubuntu 22.04 docker and docker-compose install
sudo sysctl -w vm.max_map_count=262144
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu `lsb_release -cs` stable"
sudo apt update -y
sudo apt install docker-ce -y
# Docker-compose
mkdir -p ~/.docker/cli-plugins/
curl -SL https://github.com/docker/compose/releases/download/v2.3.3/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
@ali-master
ali-master / fixed-point.ts
Created April 9, 2024 21:37
Javscript Number fixed point
export function formatFixedPoint(val: bigint, decimals = 18): string {
const l = val / 10n ** BigInt(decimals);
const r = val % 10n ** BigInt(decimals);
if (r === 0n) {
return l.toString();
}
return `${l}.${r.toString().padStart(decimals, "0").replace(/0*$/, "")}`;
}
@ali-master
ali-master / pick.interceptor.ts
Created April 9, 2024 21:36
Nestjs Pick Interceptor
import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
} from "@nestjs/common";
import { Observable, map } from "rxjs";
export class PickOptions {
type?: any;
@ali-master
ali-master / lock.module.ts
Last active April 9, 2024 21:35
Nestjs Redis Lock
import { Global, Module } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
@Global()
@Module({
providers: [
{
provide: LockService,
inject: [ConfigService],
@ali-master
ali-master / docker-compose
Created April 5, 2024 20:44 — forked from hexfusion/docker-compose
etcd cluster v3 docker-compose example
version: '2'
services:
etcd-1:
image: gcr.io/etcd-development/etcd:v3.3.9
restart: always
ports:
- 2379
- 2380
volumes:
@ali-master
ali-master / useBroadcastLeader.ts
Created April 4, 2024 04:05 — forked from tannerlinsley/useBroadcastLeader.ts
A React Hook to determine if a tab of your application is the "leader" using BroadcastChannel and leader election
import { BroadcastChannel, createLeaderElection } from 'broadcast-channel'
import React from 'react'
const channels = {}
export function useBroadcastLeader(id = 'default') {
const [isBroadcastLeader, setIsBroadcastLeader] = React.useState(false)
React.useEffect(() => {
if (!channels[id]) {
@ali-master
ali-master / main.ts
Created April 3, 2024 21:29
Simple Multi Tenant Manager
import { Controller, Get, Inject, Injectable, Module, Scope } from "@nestjs/common";
import {
ContextId, ContextIdFactory, ContextIdResolver, ContextIdResolverFn, ContextIdStrategy,
HostComponentInfo, ModuleRef, NestFactory, REQUEST
} from "@nestjs/core";
import { NestExpressApplication } from "@nestjs/platform-express";
// Context strategy
const tenants = new Map<string, ContextId>();
export class TenantContextIdStrategy implements ContextIdStrategy {