Skip to content

Instantly share code, notes, and snippets.

View mpellegrini's full-sized avatar

Michael Pellegrini mpellegrini

View GitHub Profile
@mpellegrini
mpellegrini / drizzle-utils.ts
Created August 28, 2024 21:27
Drizzle Utility Functions
import { type AnyColumn, type SQL, sql } from 'drizzle-orm'
/**
* The Coalesce function evaluates the arguments in the specified order
* and always returns the first non-null value from the argument list.
*
* @template T - The type of the value being coalesced.
* @param args - The value to be coalesced.
* @returns - The SQL expression representing the coalesced value.
*/
@mpellegrini
mpellegrini / update_audit_metadata_function.sql
Last active August 9, 2024 12:22
Postgres Function and Trigger to Update Audit Metadata Fields
CREATE OR REPLACE FUNCTION public.update_audit_metadata()
RETURNS trigger
LANGUAGE 'plpgsql'
AS
$function$
BEGIN
NEW.created_at := OLD.created_at;
NEW.created_by := OLD.created_by;
NEW.version := OLD.version;
IF row (NEW.*) IS DISTINCT FROM row (OLD.*) THEN

Keybase proof

I hereby claim:

  • I am mpellegrini on github.
  • I am mpellegrini (https://keybase.io/mpellegrini) on keybase.
  • I have a public key ASCJzHB0u-QGRLctjx9M5lAJx3bsi5zOGQMbNmH6GWJt-go

To claim this, I am signing this object:

@mpellegrini
mpellegrini / FunctionalVisitor.java
Created May 9, 2019 19:17
Example of Functional Visitor Pattern using Vavr
package com.michaelpellegrini.example.functional;
import io.vavr.Function1;
import io.vavr.collection.List;
import static io.vavr.API.$;
import static io.vavr.API.Case;
import static io.vavr.API.Match;
import static io.vavr.Predicates.instanceOf;
@mpellegrini
mpellegrini / RestEasyLogger.java
Last active May 19, 2020 07:55
JAX-RS Provider and RestEasy Interceptor to Log request / response messages
import org.jboss.resteasy.annotations.interception.ServerInterceptor;
import org.jboss.resteasy.core.ResourceMethod;
import org.jboss.resteasy.core.ServerResponse;
import org.jboss.resteasy.spi.Failure;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.interception.MessageBodyWriterContext;
import org.jboss.resteasy.spi.interception.MessageBodyWriterInterceptor;
import org.jboss.resteasy.spi.interception.PreProcessInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@mpellegrini
mpellegrini / LocalDateBinder.java
Created May 27, 2017 09:33
JDBI v2 Bind Java LocalDate
public class LocalDateBinder implements Binder<Bind, LocalDate> {
@Override
public void bind(SQLStatement<?> stmt, Bind bind, LocalDate localDate) {
stmt.bind(bind.value(), localDate.format(DateTimeFormatter.ISO_LOCAL_DATE));
}
}
@mpellegrini
mpellegrini / DiscoverLdapServers.java
Created November 13, 2014 04:50
Get LDAP Servers Programmatically
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.spi.NamingManager;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
@mpellegrini
mpellegrini / programatic-ehcache.java
Created July 1, 2014 02:49
Creating Ehcache Programmatically
// Create a configuration
Configuration configuration = new Configuration();
//Create a CacheManager using defaults
CacheManager manager = CacheManager.create(configuration);
//Create a Cache specifying its configuration.
Cache testCache = new Cache(
new CacheConfiguration("test", maxElements)
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
@mpellegrini
mpellegrini / web-fragment.xml
Last active August 29, 2015 13:57
Java EE Servlet 3.0 Web Fragments Descriptor
<?xml version="1.0" encoding="UTF-8"?>
<web-fragment version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd">
<display-name>display-name</display-name>
<description>description</description>
</web-fragment>
// Better
public static boolean isEqual(byte[] a, byte[] b) {
if (a.length != b.length) {
return false;
}
int result = 0;
for (int i = 0; i < a.length; i++) {
result |= a[i] ^ b[i]
}