Skip to content

Instantly share code, notes, and snippets.

@rw3iss
rw3iss / dropDb.js
Created April 12, 2021 02:29
dropDb.js
require('dotenv').config()
const { DataMapper, DbHelper } = require('api-data-tools');
console.log('dropDb', process.env.DATABASE_URL);
async function main() {
DbHelper.initialize();
let r = [];
@rw3iss
rw3iss / IDB.ts
Last active April 11, 2021 17:14
IndexedDB
/* Usage:
// init:
this.db = new IDB();
this.db.open(DBNAME);
this.db.addStore(DBNAME, { autoIncrement: true }, [
{ name: 'id', key: 'id', params: { unique: true } },
{ name: 'word, language', key: [ 'word', 'language' ], params: { unique: true } },
]);
@rw3iss
rw3iss / Store.ts
Created April 11, 2021 14:35
Store.ts
import LocalStorage from 'lib/utils/LocalStorage';
export default class Store {
id: string;
items: any;
autoSave = true;
constructor(storeId, items = {}, autoSave = true) {
this.id = storeId;
@rw3iss
rw3iss / awsStorage.js
Last active April 11, 2021 14:18
File Uploads - with GCP or AWS
// Client:
onImagesAdded = (e) => {
try {
console.log('added', e)
Array.from(e.target.files).forEach(async (f: any) => {
let sUrl = await S3Service.getSignedUrl(f.name, f.type);
console.log('got signed url', sUrl)
let r = await this.sendFile(f, sUrl);
})
@rw3iss
rw3iss / Response.js
Created March 30, 2021 17:16
Node Server Utils
export default class Response {
static success(res, data) {
const r = {
success: true
}
if (data) {
r.data = data
}
@rw3iss
rw3iss / Cache.js
Last active March 30, 2021 16:59
redis server Cache.js
const { getRedisClient } = require('./redis/redis')
redis = getRedisClient()
const EXPIRE_DEFAULT = 60*60; // seconds = 1hr
// App-wide cache. Just a wrapper for redis.
class Cache {
async get(id) {
@rw3iss
rw3iss / dateUtils.js
Last active September 14, 2023 16:33
JS Utilities
export class DateUtils {
public static dateToIsoString(date: Date | string): string | null {
return date.toISOString();
}
public static getDateFromMysql(isoDateStr: string): Date {
var dateParts = isoDateStr.split("-");
return new Date(dateParts[0], dateParts[1] - 1, dateParts[2].substr(0, 2));
}
@rw3iss
rw3iss / restart.sh
Last active April 14, 2021 16:51
Safe kill previous server and restart node app
"setup": "npm install && rm -rf scripts/migrations && rm config/.curr.schema.json && npm run migrate",
"dev": "concurrently --kill-others \"npm run build-watcher\" \"npm run build-reload\"",
"build-watcher": "nodemon --watch './src/**/*' -e ts,tsx,js,jsx,scss,html,json,env --exec \"npm run build\"",
"build-reload": "nodemon --signal SIGTERM build/index.js",
@rw3iss
rw3iss / readfile.js
Created March 3, 2021 21:29
Read a file input
// trigger upload dialog:
this.importFile.current.click();
onFileImportChange = (e) => {
try {
const reader = new FileReader();
reader.addEventListener('load', () => {
let data = JSON.parse(reader.result);
this.setState(data);
});
@rw3iss
rw3iss / Validation.ts
Last active April 13, 2021 02:38
Simple validation library
/*
Usage example:
import Validation from 'Validation.ts';
let data = {
username: "aUsername",
password: "aPassword",
confirmPassword: "aPassword"
}