Skip to content

Instantly share code, notes, and snippets.

View jordanell's full-sized avatar

Jordan Ell jordanell

View GitHub Profile
@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 / 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.
*
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';
/**
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';
@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);
`, {
@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) =>