Skip to content

Instantly share code, notes, and snippets.

View jordanell's full-sized avatar

Jordan Ell jordanell

View GitHub Profile
@jordanell
jordanell / addSearchToTables.js
Created April 9, 2018 15:35
Adding TSVectors and indexes to PostgreSQL tables using Sequelize CLI.
const vectorName = '_search';
const searchObjects = {
authors: ['name', 'biography'],
posts: ['name', 'summary'],
};
module.exports = {
up: (queryInterface) => (
queryInterface.sequelize.transaction((t) =>
@jordanell
jordanell / search.js
Last active April 9, 2018 16:18
Chaining Sequelize full text searchi queries
import { map } from 'lodash';
import models from 'src/models';
const search = async function search() {
const results = await models.sequelize.query(`
SELECT *
FROM ${models.Author.tableName}
WHERE _search @@ plainto_tsquery('english', :query);
`, {
import Enzyme from 'enzyme';
import { JSDOM } from 'jsdom';
import initialize from 'src/initialize';
// Initialize our project
initialize();
// Force the test environment
process.env.NODE_ENV = 'test';
import { mount as enzymeMount } from 'enzyme';
import createHistory from 'history/createMemoryHistory';
import React from 'react';
import { Provider } from 'react-redux';
import { ConnectedRouter } from 'react-router-redux';
import Immutable from 'seamless-immutable';
import initialize from 'src/store/initialize';
/**
@jordanell
jordanell / counterCache.js
Created September 5, 2018 17:51
Sequelize counter cache
import {
filter,
} from 'lodash';
/**
* A global hook which is applied to every model's create, update and destroy lifecycle
* methods. Once one of those lifecycle event's occurs, this function iterates over
* every other model definition looking for cache columns which point to the triggering
* model. If any models are found, their cache counter coulmns are then updated.
*
@jordanell
jordanell / polymorphism.js
Created March 22, 2019 15:47
Sequelize polymorphism example
this.Comment = this.sequelize.define('comment', {
title: Sequelize.STRING,
commentable: Sequelize.STRING,
commentable_id: Sequelize.INTEGER
});
this.Comment.prototype.getItem = function() {
return this['get' + this.get('commentable').substr(0, 1).toUpperCase() + this.get('commentable').substr(1)]();
};
@jordanell
jordanell / polymorphism-eagar-loading.js
Created March 22, 2019 15:53
Sequelize polymorphism eager loading
/**
* Given a comment which has polymorphic associations of post and photo.
*/
const comments = await models.Comment.findAll({
include: [
{ model: models.Photo },
{ model: models.Post },
],
});
@jordanell
jordanell / models.js
Last active July 8, 2021 19:48
Sequelize paranoid delete cascade
import paranoidDeleteCascade from './helpers/paranoidDeleteCascade';
// Patch the paranoid delete functionality of Sequelize
sequelize.addHook('afterDestroy', paranoidDeleteCascade(db));
@jordanell
jordanell / authorize.js
Last active April 30, 2019 20:34
Express authorization middleware
import { isEmpty } from 'lodash';
import {
ForbiddenError,
NotFoundError,
} from 'src/errors';
import models from 'src/models';
const defaultBlacklist = [];
@jordanell
jordanell / withPaywall.jsx
Last active August 11, 2021 11:11
React Paywall
import get from 'lodash/get';
import PropTypes from 'prop-types';
import React from 'react';
import { connect } from 'react-redux';
/**
* Higher-order component that passes state and props into a predicate that
* checks whether the current user has permission to view the page due to paywall restrictions.
*
* If the predicate function returns true, the component is rendered.