Skip to content

Instantly share code, notes, and snippets.

@eddieajau
eddieajau / createSequelizeStream.js
Created March 20, 2016 22:14
Module to allow a Sequelize select query to be converted into a stream.
"use strict";
var Readable = require('stream').Readable;
/**
* Create a stream from batching a Sequelize select query.
*
* @param {Function} query - Function that returns a promise.
* @param {object} options
* @param {number} options.limit - The limit for the number of rows to return in each "batch"
class SafeObserver {
constructor(private _next: (value: any) => void = () => {},
public error: (errorValue: any) => void = (e: any) => { throw e; },
private _complete: (completeValue?: any) => void = () => {}) {}
public next(value: any) {
try {
this._next(value);
}
catch (e) {
@eddieajau
eddieajau / typeorm_model.ts
Last active May 9, 2019 02:49
Example of automatic translation of a JSON property in a TypeORM model.
import { PrimaryGeneratedColumn, Entity, Column } from 'typeorm';
@Entity('foo')
export class Foo {
@PrimaryGeneratedColumn()
public id: number;
@Column({ default: '' })
public get link(): string {
let value = this._link;
@eddieajau
eddieajau / csv-parse-async.mjs
Last active April 11, 2023 01:09
Parsing a CSV using a stream pipeline (async)
import { parse } from 'csv-parse'
import fs from 'fs'
import { pipeline } from 'stream/promises'
/**
* Do something async with a row.
*
* @param {*} row A row of the CSV as an object.
*/
async function handleRow(row) {