Skip to content

Instantly share code, notes, and snippets.

@lewiswalsh
lewiswalsh / upsert.js
Last active May 27, 2022 18:37
KnexJS 'upsert', update on duplicate key else insert #knex #mysql
knex(TABLE_NAME)
.insert({ data : data })
.onConflict(PRIMARY_KEY)
.merge()
.then((res) => {
...
})
.catch((err) => {
...
});
@lewiswalsh
lewiswalsh / select-and-copy-vue.js
Last active May 27, 2022 18:37
Select and copy the contents of an element in Vue #vue
<template>
<div id="app">
<input v-on:focus="$event.target.select()" ref="myelement" readonly v-model="mytext" />
<button v-on:click="copyElement">Copy</button>
</div>
</template>
<script>
export default {
data() {
@lewiswalsh
lewiswalsh / upload-file-with-vue.js
Last active May 27, 2022 18:38
Upload on file change with Vue #vue
<input type="file" @change="previewFiles" ref="imagefile">
methods: {
previewFiles(event) {
const fileToUpload = this.$refs.imagefile.files[0];
const formData = new FormData();
formData.append('fileToUpload', fileToUpload);
this.$http.post(`url/to/upload/script`, formData)
.then((res) => {
...
@lewiswalsh
lewiswalsh / s3bucketsize.sh
Last active May 27, 2022 18:49
List all s3 buckets with size #bash #s3
#!/bin/bash
BUCKETS=$(aws s3 ls)
while IFS= read -r line
do
BUCKET=$(echo $line | cut -d " " -f3)
SIZE=$(aws s3 ls --summarize --human-readable --request-payer --recursive s3://$BUCKET/ | grep "Total Size" | cut -d ":" -f2)
echo "$BUCKET $SIZE"
done <<< "$BUCKETS"
@lewiswalsh
lewiswalsh / export_keys.sql
Last active May 27, 2022 18:49
Export all keys from a database #mysql
SELECT CONCAT(
'ALTER TABLE ', fks.quotedSourceTableName, ' ',
'ADD CONSTRAINT ', fks.quotedConstraintName, ' ',
'FOREIGN KEY (', GROUP_CONCAT(fks.quotedSourceColumnName), ') ',
'REFERENCES ', fks.quotedTargetTableName, ' (',
GROUP_CONCAT(fks.quotedTargetColumnName),
') ',
'ON DELETE ', fks.deleteRule, ' ',
'ON UPDATE ', fks.updateRule, ';'
)
@lewiswalsh
lewiswalsh / export_indexes.sql
Last active May 27, 2022 18:50
Export indexes from a database #mysql
SELECT Concat('ALTER TABLE ', table_name, ' ', 'ADD ',
IF(non_unique = 1, CASE Upper(index_type)
WHEN 'FULLTEXT' THEN 'FULLTEXT INDEX'
WHEN 'SPATIAL' THEN 'SPATIAL INDEX'
ELSE Concat('INDEX ', index_name, ' USING ',
index_type)
end, IF(Upper(index_name) = 'PRIMARY', Concat(
'PRIMARY KEY USING ', index_type),
Concat('UNIQUE INDEX ',
index_name, ' USING ',
@lewiswalsh
lewiswalsh / blur-background.css
Last active May 27, 2022 18:51
CSS blur background image #css
backdrop-filter: blur(10px);
@lewiswalsh
lewiswalsh / certer-horizontally-and-vertically.css
Last active May 27, 2022 18:52
Center horizontally and vertically in CSS #css
display: grid;
place-items: center;
@lewiswalsh
lewiswalsh / gist:71a605c3c6f18d430466af57897b90f8
Created June 7, 2020 13:38
Cat a file with line numbers
cat -n <filename>
@lewiswalsh
lewiswalsh / unsplash.url
Last active May 27, 2022 18:57
Get a random image from Unsplash and set size #html
http://source.unsplash.com/random/1200x600