Skip to content

Instantly share code, notes, and snippets.

View kirklewis's full-sized avatar
🙂
try { Positivity } catch { Negativity } finally { laugh() }

Kirk Lewis kirklewis

🙂
try { Positivity } catch { Negativity } finally { laugh() }
View GitHub Profile

Sequelize updateWithIncludes

A quick solution I made to update associated models dynamically. This can be taken a step further and made more generic so DirectorKey and director are based on any parent model. I am using a transaction to rollback everything when an error occurs. Just a bit of fun and it works!

Note: createdAt might be updated if supplied in the updates but this doesn't tend to happen in my case as Postman to controller validation do not allow default date fields.

The Setup

The following works fine with the exceptions:

  • it is not generic but can easily be made to be, this is from a use case I was proving.
  • it will fill missing field data with null. This can be fixed by replacing bulkCreate with a custom method to generate the [key-values] of each associate for use in VALUES but using ON DUPLICATE KEY UPDATE. This works well.
@kirklewis
kirklewis / cubelet-group.mtl
Last active March 22, 2020 14:11
Rubik's Cube Cubelet OBJ File
# Blender MTL File: 'cubelet.blend'
# Material Count: 1
newmtl Material
Ns 323.999994
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
@kirklewis
kirklewis / build-docker-compose-yaml.groovy
Created February 24, 2020 21:12
Groovy 3.0.0 YamlBuilder Example
import groovy.yaml.YamlBuilder
class DockerService { List ports; List volumes; Boolean tty; }
def ordersApiService = new DockerService(
ports: ['3040:3040'],
volumes: ['./:/opt'],
tty: true,
)
@kirklewis
kirklewis / perl-module-regex.pl
Created February 24, 2020 11:01
A Perl module Regular Expression.
use v5.24;
use Data::Dump qw(pp);
use constant PERL_MODULE_REGEX => qr/\w+:{2}[\w:]+/;
use constant PERL_USE_MODULE_REGEX => qr/use\s+(${\PERL_MODULE_REGEX})/;
use constant PERL_PACAKGE_MODULE_REGEX => qr/package\s+(${\PERL_MODULE_REGEX})/;
my $str = <<'MODULES';
use My::ACME;
use My::ACME::Imported;
@kirklewis
kirklewis / list-to-yaml.groovy
Created February 16, 2020 19:09
Groovy 3.0 YamlBuilder
import groovy.yaml.YamlBuilder
def counts = [ 1, 2, 3 ]
def yaml = new YamlBuilder()
yaml(counts)
assert yaml.toString() == '''\
---
- 1
- 2
@kirklewis
kirklewis / yaml-to-list.groovy
Created February 16, 2020 19:08
Groovy 3.0 YamlSlurper
import groovy.yaml.YamlSlurper
def yamlSlurper = new YamlSlurper()
def yaml = yamlSlurper.parseText('''\
- 100
- 50
- 1
''')
assert yaml instanceof List
@kirklewis
kirklewis / process-file.go
Created August 29, 2019 10:02
Template File Processing in Go
package main
import (
"os"
"text/template"
)
func main() {
// variables
vars := make(map[string]interface{})
@kirklewis
kirklewis / send-statsd-test-metrics.sh
Last active March 22, 2021 14:48
Generate and send StatsD test metrics
#!/bin/bash
DEFAULT='\033[0m'
HI_CYAN='\033[0;96m'
HI_WHITE='\033[0;93m'
STATSD_HOST=127.0.0.1
STATSD_PORT=8125
api_names=(orders invoice user)
#!/usr/bin/env perl
use v5.20.3;
use strict;
use warnings;
# Basic String Interpolation with Scalars and Lists
my $PI = 3.14;
my @nums = (3, 2, 1);
say "scalar: $PI - list: @nums";

Keybase proof

I hereby claim:

  • I am kirklewis on github.
  • I am kirklewis (https://keybase.io/kirklewis) on keybase.
  • I have a public key whose fingerprint is D9FF 94AB C80F A0DA 0940 E7A8 6E02 7D52 9BAF A6BF

To claim this, I am signing this object: