Skip to content

Instantly share code, notes, and snippets.

View kevcodez's full-sized avatar
🚀

Kevin Grüneberg kevcodez

🚀
View GitHub Profile
function doYourStuff() {
const yourString = "foobar"
const uniqueString = findUniqueString(yourString)
// continue with your logic with unique string
}
function findUniqueString(baseString) {
let uniqueString = baseString
// number of iterations should be super small, probably unnecessary to have a max threshold
@kevcodez
kevcodez / hook.sql
Created April 30, 2022 15:18
Supabase Before insert hook
-- Create a new trigger
CREATE OR REPLACE FUNCTION public.notify_parqet_api_signup()
RETURNS TRIGGER AS $$
DECLARE
parqet_user_id varchar;
social_provider_id varchar;
BEGIN
-- Extract ID from social provider, can be null
social_provider_id := NEW.raw_user_meta_data->>'provider_id';
@kevcodez
kevcodez / auth0-rule.js
Created June 27, 2022 10:03
Auth0 notify backend on social auth "registration"
async function (user, context, callback) {
const axios = require('axios');
const isSocial = context.connectionStrategy === context.connection;
// if it is the first login (hence the `signup`) and it is a social login
if (context.stats.loginsCount === 1 && isSocial) {
let loginMethod = '';
const connectionName = user.identities[0].provider.toLowerCase();
@kevcodez
kevcodez / lock.js
Last active August 28, 2022 15:31
Simple Distributed Lock with Mongo
async function acquireLock(name) {
// Open connection to database
const collection = await db.collection('locks');
try {
// Insert an entry to the database, using the _id field, which already has a unique key constraint
await collection.insertOne({ _id: name });
// No exception was thrown, so we successfully acquired a lock
return true;
@kevcodez
kevcodez / lock-advanced.js
Last active August 28, 2022 15:34
Advanced Distributed Lock with Mongo
/**
* Create the MongoDB collection and an expiring index on a field named "expiresAt".
*
* db.createCollection('locks');
* db.locks.createIndex( { "expiresAt": 1 }, { expireAfterSeconds: 0 } )
**/
async function acquireLock(name, ttlSeconds) {
const collection = await db.collection('locks');
@kevcodez
kevcodez / my-cron.js
Created August 28, 2022 15:28
Use distributed lock
// Scheduled job running at 4:30 am
cron.scheduleJob('30 4 * * *', async () => {
// Acquire distributed lock
await lockService.acquireAndExecute({ name: 'translateAssets', ttlSeconds: 60 * 60 }, async () => {
// code to translate assets
})
@kevcodez
kevcodez / sample_asset.json
Created October 7, 2022 14:45
medium_mongodb_performance_sample_document
{
"_id": "1232312323",
"name": "Apple",
"isin": "US123456890",
"marketCap": 1233413233.123,
"ipoDate": "2000-01-01",
"lastPrice": 123.23,
"dividends": [
{
"date": "2022-01-01",
@kevcodez
kevcodez / query.sh
Created October 7, 2022 14:46
medium_mongodb_performance_asset_by_ipoDate_dividends_date
db.assets.find({
ipoDate: { $gt: ISODate('2015-01-01')},
"dividends.date": { $gt: ISODate('2022-07-01') }
})
@kevcodez
kevcodez / query.sh
Created October 7, 2022 14:46
medium_mongodb_performance_array_exists
db.assets.find({
"myArray.0": { $exists: true }
})
@kevcodez
kevcodez / query.sh
Created October 7, 2022 14:47
medium_mongodb_performance_array_elem_match
db.assets.find({
myArray: {
$elemMatch: {
myProperty: 'val'
}
}
})