Skip to content

Instantly share code, notes, and snippets.

View fl0wo's full-sized avatar
🎯
Focusing

Florian Sabani fl0wo

🎯
Focusing
View GitHub Profile
@fl0wo
fl0wo / MomentoFetcher.ts
Created May 28, 2024 21:31
Momento fetcher client for backend
export class MomentoFetcher {
private readonly apiKey: string;
private readonly baseurl: string;
constructor(key: string, endpoint: string) {
this.apiKey = key;
if (!endpoint.startsWith('https://')) {
this.baseurl = `https://${endpoint}`;
} else {
this.baseurl = `${endpoint}`;
}
@fl0wo
fl0wo / example-consumer.tsx
Created May 23, 2024 07:58
Just created a TopicProvider in NextJs! Looks a bit as a Flutter + Angular style. Combined: Signletone, Context Provider, BehaviorSubject Obtained: 1 single connection per topic, spread across the website for every component that needs it.
/**
This component actually needs the messages from Momento, so it uses useTopic() to subscribe to the stream.
*/
"use client";
import {FC, useEffect, useState} from 'react';
import { TopicItem } from '@gomomento/sdk-web';
import { Subscription } from 'rxjs';
import {publishToTopic} from "../src/momento/client";
const MOMENTO_CACHE_DB_TRIGGERS_API_KEY=''
const MOMENTO_CACHE_DB_TRIGGERS_CACHE_NAME=''
const MOMENTO_CACHE_DB_TRIGGERS_TOPIC_BASE_URL=''
const MOMENTO_CACHE_DB_TRIGGERS_TOPIC_NAME=''
const msg = {
query_type: 'test',
table_name: 'no-table',
@fl0wo
fl0wo / UpdateRowBeforeAndAfterPluginWithDiffs.ts
Last active April 17, 2024 15:31
UpdateRowBeforeAndAfterPlugin but with diffs
import {LibSQLPlugin} from "libsql-client-hooks";
import {Client, InStatement, ResultSet} from '@libsql/core/api';
import {
deepDiffPartials,
isUpdateForTable,
removeDoubleQuotes,
replaceAllSequencesFromQuery
} from "./utils";
import {TableNamesTurso} from "../index";
@fl0wo
fl0wo / UpdateRowBeforeAndAfterPlugin.ts
Created April 17, 2024 15:04
UpdateRowBeforeAndAfterPlugin using LibSQLPlugin
import {LibSQLPlugin} from "libsql-client-hooks";
import { InStatement, Client, ResultSet } from '@libsql/core/api';
import {isUpdateForTable, removeSpaces} from "./utils";
import {TableNamesTurso} from "../index";
export class UpdateRowBeforeAndAfterPlugin<T> implements LibSQLPlugin {
private rowBefore: T | undefined;
constructor(
@fl0wo
fl0wo / turso-hooks-regexps.ts
Created April 13, 2024 10:55
Regexps to check SQL queries patterns for turso hooks
export function isUpdateForTable(query: any, tableName: string): boolean {
const escapedTableName = tableName.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
const regexp = new RegExp(`update "?${escapedTableName}"? set`, 'i');
return regexp.test(typeof query === "string" ? query : query.sql);
}
export function isInsertForTable(query: any, tableName: string): boolean {
const escapedTableName = tableName.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
const regexp = new RegExp(`insert into "?${escapedTableName}"?`, 'i');
return regexp.test(typeof query === "string" ? query : query.sql);
@fl0wo
fl0wo / query-candles-from-timestream.ts
Created September 23, 2023 19:14
Utility code on how to build and query Timestream db containing candlesticks
import {QueryCommand, QueryCommandInput} from "@aws-sdk/client-timestream-query";
export function minDifferenceDates(d1:Date, d2:Date) {
const differenceMs = d1.getTime() - d2.getTime();
return Math.abs(differenceMs / (1000 * 60));
}
export function minutesAgo(agoMinutes:number) {
const now = new Date().getTime();
@fl0wo
fl0wo / last-known-candles-dictonary-builder.ts
Created September 23, 2023 18:58
LastKnownCandlesDictonary builder
const fromRowToDictonary = (rows: any[]) => {
const dictonary: LastKnownCandlesDictonary = {};
rows.forEach((row) => {
dictonary[row.symbol] = row.lastaction;
});
return dictonary;
}
const reformatRowToObj = (res: QueryCommandOutput) => {
@fl0wo
fl0wo / candle-write-operations.ts
Created September 22, 2023 22:17
Simple way to save candlesticks with batch-write multi-type into Timestream
import { OHLCV } from 'ccxt';
import { getInsertRecordParameterToTimeStream, writeBatchToTimestream } from './write';
import { SymbolAndCandles } from '../lambda/candle-fetcher/models';
import {
_Record,
Dimension,
MeasureValue,
MeasureValueType, TimestreamWriteClient,
WriteRecordsCommand, WriteRecordsCommandOutput,
WriteRecordsRequest,
@fl0wo
fl0wo / timestream-write-batch.ts
Created September 22, 2023 21:30
A way to batch-write records to an AWS Timestream Database
import {
_Record,
Dimension,
MeasureValue,
MeasureValueType, TimestreamWriteClient,
WriteRecordsCommand, WriteRecordsCommandOutput,
WriteRecordsRequest,
} from '@aws-sdk/client-timestream-write';
export const getInsertRecordParameterToTimeStream = (