Skip to content

Instantly share code, notes, and snippets.

View alfasin's full-sized avatar
🖖
He bag production, he got walrus gumboot

Nir Alfasi alfasin

🖖
He bag production, he got walrus gumboot
View GitHub Profile
@alfasin
alfasin / backend.mdc
Created March 3, 2025 21:08 — forked from shilomagen/backend.mdc
Cursor Rules Example
---
description: Rules for backend (NestJS application)
globs: apps/**/*.*
---
# Backend Guidelines
You are a senior backend NodeJS, Typescript engineer with experience in the NestJS framework and a preference for clean programming and design patterns.
Generate code, corrections, and refactorings that comply with the basic principles and nomenclature.
## Basic Principles
@alfasin
alfasin / reset.sh
Last active February 16, 2025 21:22
Reset Prisma instead of resetting your DB
# before you start: go to prisma.schema and copy aside the sections: "generator client {...}" and "datasource db {...}"
# the may contain previewFeatures and extensions that won't be recovered automatically and you'll have to add them manually in a few steps!
# Go to your DB and drop the migrations table: `DROP TABLE _prisma_migrations`
# From the root of the project:
rm -rf prisma
npx prisma init
npx prisma db pull # This should recreate prisma.schema file from the DB
@alfasin
alfasin / maxSubArray.ts
Created March 6, 2024 23:33
Kadane's Algorithm
function maxSubArray(nums: number[]): number {
let max = nums[0];
let curSum = nums[0];
for (let i = 1; i < nums.length; i++) {
if (curSum < 0) curSum = nums[i];
else curSum += nums[i];
if (max < curSum) max = curSum;
}
return max;
};
@alfasin
alfasin / fastapi_server.py
Last active March 19, 2022 22:53
A small example of a fastAPI server with JWT token
import jwt
from fastapi import FastAPI, Header
from pydantic import BaseModel
from typing import Optional
JWT_SECRET = "secret" # IRL we should NEVER hardcode the secret: it should be an evironment variable!!!
JWT_ALGORITHM = "HS256"
app = FastAPI()
@alfasin
alfasin / colorful_winston_logger.js
Last active December 19, 2023 01:27
A Nodejs implementation of a console transport logger based on Winston 3.x which also prints the filename and line-number
const winston = require('winston');
const { format } = winston;
const { combine, colorize, timestamp, printf } = format;
/**
* /**
* Use CallSite to extract filename and number, for more info read: https://v8.dev/docs/stack-trace-api#customizing-stack-traces
* @param numberOfLinesToFetch - optional, when we want more than one line back from the stacktrace
* @returns {string|null} filename and line number separated by a colon, if numberOfLinesToFetch > 1 we'll return a string
@alfasin
alfasin / InstallingPython3OnOSX.sh
Last active February 19, 2020 14:24
Installing Python3 On OSX
# shamelessly copied from Jesse Myers (https://github.com/jessemyers)
# Install pyenv from homebrew:
brew install pyenv
pyenv install 3.7.4
pyenv global 3.7.4
# Install virtualenvwrapper globally:
$(pyenv which pip) install virtualenv virtualenvwrapper
@alfasin
alfasin / getFileNameAndLineNumber.js
Last active January 12, 2020 16:47
Use prepareStackTrace to get the filename & line number
/**
* Use CallSite to extract filename and number, for more info read: https://v8.dev/docs/stack-trace-api#customizing-stack-traces
* @returns {string} filename and line number separated by a colon
*/
const getFileNameAndLineNumber = () => {
const oldStackTrace = Error.prepareStackTrace;
try {
// eslint-disable-next-line handle-callback-err
Error.prepareStackTrace = (err, structuredStackTrace) => structuredStackTrace;
Error.captureStackTrace(this);
@alfasin
alfasin / better-nodejs-require-paths.md
Last active January 7, 2020 21:30 — forked from branneman/better-nodejs-require-paths.md
Better local require() paths for Node.js

Better local require() paths for Node.js

Problem

When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:

const Article = require('../../../../app/models/article');

Those suck for maintenance and they're ugly.

Possible solutions

@alfasin
alfasin / stringify.js
Created January 3, 2020 15:31 — forked from zmmbreeze/stringify.js
json stringify can deal with circular reference
// http://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json/11616993#11616993
var o = {};
o.o = o;
var cache = [];
JSON.stringify(o, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;