Skip to content

Instantly share code, notes, and snippets.

View mickhansen's full-sized avatar

Mick Hansen mickhansen

  • Copenhagen, Denmark
View GitHub Profile
@mickhansen
mickhansen / optimize.js
Last active December 29, 2015 12:25 — forked from heijmerikx/optimize.js
router.get('/', function(req, res, next) {
models.Blog.findAll({
where: {
published: true
},
limit: 3,
order: [["createdAt","DESC"]],
include: [
{ model: models.Photo }
]
sequelize.getConnection(function (connection) {
// do stuff
return new Promise(); // connection automatically released back to pool when promise returned from callback is resolved or rejected
});
@mickhansen
mickhansen / user_task_association_example.js
Last active June 20, 2019 08:06
Sequelize Example with Users and Tasks
var User = sequelize.define('User')
, Task = sequelize.define('Task');
User.Tasks = User.hasMany(Task);
Task.User = Task.belongsTo(User);
/*
* Creation
*/
User.create({ /* user values */ }).then(function (user) {
@mickhansen
mickhansen / gist:966a67648c45dd5c25ee
Last active April 13, 2019 11:31
Sequelize JSONB indexing
indexes: [
{
using: 'GIN',
fields: ['field']
}
]
// CREATE INDEX field ON $modelTable USING gin (field);
indexes: [
@mickhansen
mickhansen / gist:7a3a85433557244e17a2
Created April 21, 2015 15:49
Sequelize JSONB Querying proposal
{
field: {
propa: 'valuea',
propb: {
propc: 'valueb'
}
}
}
// "field"#>>'{propa}' = 'valuea' AND "field"#>>'{propb, propc}' = 'valueb'
var deviceToSave = Device.build({});
Device.findOrCreate({
where: {device_id: deviceToSave.device_id},
defaults: deviceToSave.get()
}).spread(function(device, created) {
if (created) return device;
return device.updateAttributes(deviceToSave.get());
}).then(function (item) {
response.code(201).send(item.get());
var Promise = require('bluebird');
Promise.reduce(queries, function (results, query) {
return sequelize.query(query, null, {raw: true, plain: true}).then(function (result) {
return results.concat([result]);
});
}, []).then(function (results) {
console.log("results");
}).catch(function (err) {
console.error("Error": err);
});
SELECT "Comics".*,
"Favourites"."id" AS "Favourites.id",
"Favourites"."createdAt" AS "Favourites.createdAt",
"Favourites"."updatedAt" AS "Favourites.updatedAt",
"Favourites"."UserId" AS "Favourites.UserId",
"Favourites"."ComicId" AS "Favourites.ComicId"
FROM (
SELECT "Comics".* FROM "Comics" ORDER BY "number" DESC LIMIT 60
) AS "Comics"
LEFT OUTER JOIN "Favourites" AS "Favourites"
// Generated by CoffeeScript 1.7.1
(function() {
var AREAS, DAMAGE_MAX, Employee, Location, Report, Sequelize, arg, async, sequelize, start;
Sequelize = require('./index');
async = require('async');
AREAS = ['area1', 'area2', 'area3'];
var Sequelize = require('sequelize'),
DAO = require('sequelize/lib/dao'),
_increment = DAO.prototype.increment,
_decrement = DAO.prototype.decrement;
DAO.prototype.increment = function(field, options) {
var instance = this,
by = (options && options.by) || options || 1;
instance.set(field, instance.get(field) + by);