Skip to content

Instantly share code, notes, and snippets.

View taxilian's full-sized avatar

Richard Bateman taxilian

View GitHub Profile
@taxilian
taxilian / mongo-mirror.ts
Created September 4, 2022 04:21
Simple node.js typescript script to use change streams to mirror all CRUD operations from one mongodb connection to another
import {MongoClient, ResumeToken, Timestamp, Logger} from 'mongodb';
import path from 'path';
import fs from 'fs';
import {serialize as bsonSerialize, deserialize as bsonDeserialize} from 'bson';
const MONGO_URL_SRC = process.env.MONGO_URL_SRC || 'mongodb://localhost:27017';
const MONGO_DB_SRC = process.env.MONGO_DB_SRC || void 0;
const MONGO_URL_DEST = process.env.MONGO_URL_DEST || 'mongodb://localhost:27018';
const MONGO_DB_DEST = process.env.MONGO_DB_DEST || void 0;
@taxilian
taxilian / FireWyrm.md
Last active August 18, 2022 23:02
FireWyrm documentation

FireWyrm message protocol

FireWyrm messages are structured as Arrays. Note that the colonyId will usually just be 0.

cmdId should be unique for any currently outstanding command; the response will be sent with a matching cmdId to allow it to be mapped back to the original command.

Supported argument types

These types are permitted as arguments or return values for any property or function

  • int
@taxilian
taxilian / consumer.schema.json
Created August 2, 2022 19:09
Example use of monGobb
{
"type": "object",
"title": "Consumer",
"properties": {
"_id": {
"bsonType": "objectId",
"tsType": "ObjectId"
},
"uniqueSlug": { "type": "string" },
"name": { "type": "string" },
@taxilian
taxilian / stringTpl.ts
Created August 2, 2022 17:08
Typescript string template literal function creator
/**
* Use this with a template literal in order to create reusable string template;
* use interpolation to add strings for each variable you want to use in the template.
*
* e.g.:
*
* const reUsableStringTemplate = stringTpl`${'name'} and ${'otherName'} are going to ${'place'}`;
*
* You can then call it with:
*
@taxilian
taxilian / MongoProxy_CollectionConfig.ts
Last active August 1, 2022 21:59
Tool to watch for .schema.json files and compile them as mongodb interfaces using json-schema-to-typescript, also proxy wrapper to use this for creating doc/model thingies
// Collection wrapper, used as an easy way to configure things and a place to put simple helpers,
// also the basis for making a "Model" object
import * as mongodb from 'mongodb';
import { JSONSchema4 } from 'json-schema';
import camelCase from 'lodash/camelCase';
interface IndexSpecification {
[key: string]: mongodb.IndexDirection;
}
@taxilian
taxilian / config.ts
Created February 3, 2022 20:05
Env overrides for config
import _ from 'lodash';
const ENV_PREFIX = `APP_CONFIG`;
function setEnvOverrides(cfg: Config) {
const foundVars = Object.keys(process.env).filter(name => name.startsWith(ENV_PREFIX));
for (const v of foundVars) {
const actualName = v.substr(ENV_PREFIX.length);
// console.log(`Processing ENV var ${actualName}`);
@taxilian
taxilian / backup_cluster.sh
Created September 8, 2021 21:12
Bash script to export all kubernetes resources as YAML files in a directory structure
#!/bin/bash
allCRDs=$(kubectl get crd -o name | cut -d '/' -f 2)
nsCRD=""
clusterCRD=""
for crd in $allCRDs; do
scope=$(kubectl describe crd $crd | grep "Scope" | cut -d ':' -f 2 | sed 's/^ *//g')
if [ "$scope" == "Namespaced" ]; then
@taxilian
taxilian / testCall.sh
Created August 4, 2021 16:33
Tool for testing how often a command succeeds
#!/bin/bash
CMD="$@"
declare -i COUNT
declare -i SUCCESS
SUCCESS=0
COUNT=0
for i in {1..100}; do
@taxilian
taxilian / ExampleSourceComponent.vue
Last active July 9, 2021 19:28
Dialog abstraction example
<script lang="ts">
import {showDialog} from './dialogMgr';
@Component()
export default class MyOtherComponent extends Vue {
markdown = '(some loaded markdown here)';
async openDialog() {
@taxilian
taxilian / app.ts
Last active April 7, 2021 00:34
My attempt at implementing a node.js server with kubernetes best practices for closing gracefully
const dbConnections: Array<MongoClient> = [];
export type startupMiddleware = ReturnType<typeof initMiddleware>;
let failCase: Error;
export function setUnrecoverableError(err: Error) {
failCase = err;
console.warn(`Unrecoverable error set:`, err, err.stack);
}