Skip to content

Instantly share code, notes, and snippets.

@mitchwongho
mitchwongho / dynamodb.java
Created May 8, 2021 09:48
#DynamoDB #JavaSDK #AWS
private Result getPeopleByGSI(@NotNull final String indexedReference, final String... attributes) {
// make reference the index
final DynamoDbIndex<People> index = DynamoUtil.peopleTable.index("someIndexedAttribute-index");
// Apply value to the indexed attribute (as the PartitionKey)
final QueryConditional queryConditional = QueryConditional.keyEqualTo(
Key.builder().partitionValue(indexedReference).build());
// Compose Filter expressions
final List<String> expressions = new ArrayList<>();
final Expression.Builder expressionBuilder = Expression.builder();
@susimsek
susimsek / ExecutionTimeAdvice.java
Last active January 26, 2024 22:27
Spring Boot Measure Execution Time using Spring AOP
@Aspect
@Component
@Slf4j
@ConditionalOnExpression("${aspect.enabled:true}")
public class ExecutionTimeAdvice {
@Around("@annotation(TrackExecutionTime)")
public Object executionTime(ProceedingJoinPoint point) throws Throwable {
long startTime = System.currentTimeMillis();
Object object = point.proceed();
@soulmachine
soulmachine / jwt-expiration.md
Last active April 9, 2024 04:12
How to deal with JWT expiration?

First of all, please note that token expiration and revoking are two different things.

  1. Expiration only happens for web apps, not for native mobile apps, because native apps never expire.
  2. Revoking only happens when (1) uses click the logout button on the website or native Apps;(2) users reset their passwords; (3) users revoke their tokens explicitly in the administration panel.

1. How to hadle JWT expiration

A JWT token that never expires is dangerous if the token is stolen then someone can always access the user's data.

Quoted from JWT RFC:

@lucasscariot
lucasscariot / model-user.js
Last active June 22, 2023 17:08
Composite Primary Key in Sequelize
/*
* Migration
*/
'use strict';
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface.createTable('Users', {
firstName: {
type: Sequelize.STRING
},
@beaucharman
beaucharman / throttle.js
Last active November 12, 2022 15:39
An ES6 implementation of the throttle function. "Throttling enforces a maximum number of times a function can be called over time. As in 'execute this function at most once every 100 milliseconds.'" - CSS-Tricks (https://css-tricks.com/the-difference-between-throttling-and-debouncing/)
function throttle(callback, wait, immediate = false) {
let timeout = null
let initialCall = true
return function() {
const callNow = immediate && initialCall
const next = () => {
callback.apply(this, arguments)
timeout = null
}
@lagner
lagner / percentile.js
Created May 16, 2016 18:23 — forked from IceCreamYou/percentile.js
Utility functions to calculate percentiles and percent ranks in a JavaScript array.
// Returns the value at a given percentile in a sorted numeric array.
// "Linear interpolation between closest ranks" method
function percentile(arr, p) {
if (arr.length === 0) return 0;
if (typeof p !== 'number') throw new TypeError('p must be a number');
if (p <= 0) return arr[0];
if (p >= 1) return arr[arr.length - 1];
var index = arr.length * p,
lower = Math.floor(index),
@msbaek
msbaek / ConvertFromOneObjectToAnotherTest.java
Created November 14, 2015 09:01
convert one object to another object by using Function in guava
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import lombok.Data;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.List;
@Data
@joshnuss
joshnuss / app.js
Last active March 4, 2024 00:01
Express.js role-based permissions middleware
// the main app file
import express from "express";
import loadDb from "./loadDb"; // dummy middleware to load db (sets request.db)
import authenticate from "./authentication"; // middleware for doing authentication
import permit from "./authorization"; // middleware for checking if user's role is permitted to make request
const app = express(),
api = express.Router();
// first middleware will setup db connection
@henriquebf
henriquebf / Dockerfile
Last active February 12, 2021 08:33
Dockerfile with Node.js, Imagemagick (with native support) and Gulp build tasks
FROM node:0.12.7-wheezy
# Install tools & libs to compile everything
RUN apt-get update && apt-get install -y build-essential libssl-dev libreadline-dev wget && apt-get clean
# Install imagemagick with support to native library
RUN apt-get install -y imagemagick libmagick++-dev libmagic-dev && apt-get clean
ENV PATH=$PATH:~/opt/bin:~/opt/node/bin:/usr/lib/x86_64-linux-gnu/ImageMagick-6.8.9/bin-Q16
# Install global CLIs
@IceCreamYou
IceCreamYou / percentile.js
Last active November 17, 2022 01:54
Utility functions to calculate percentiles and percent ranks in a JavaScript array.
// Returns the value at a given percentile in a sorted numeric array.
// "Linear interpolation between closest ranks" method
function percentile(arr, p) {
if (arr.length === 0) return 0;
if (typeof p !== 'number') throw new TypeError('p must be a number');
if (p <= 0) return arr[0];
if (p >= 1) return arr[arr.length - 1];
var index = (arr.length - 1) * p,
lower = Math.floor(index),