Skip to content

Instantly share code, notes, and snippets.

@qfox
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qfox/683e6b23196fbff368b5 to your computer and use it in GitHub Desktop.
Save qfox/683e6b23196fbff368b5 to your computer and use it in GitHub Desktop.
sequelize-promises
Executing (default): INSERT INTO "forum_topic" ("title","id","createdAt","updatedAt") VALUES ('a',DEFAULT,'2014-07-11 08:06:10.130 +00:00','2014-07-11 08:06:10.130 +00:00'),('b',DEFAULT,'2014-07-11 08:06:10.130 +00:00','2014-07-11 08:06:10.130 +00:00'),('c',DEFAULT,'2014-07-11 08:06:10.130 +00:00','2014-07-11 08:06:10.130 +00:00'),('d',DEFAULT,'2014-07-11 08:06:10.130 +00:00','2014-07-11 08:06:10.130 +00:00') RETURNING *;
Executing (default): SELECT * FROM "forum_topic";
[ '{"id":1,"title":"a","createdAt":"2014-07-11T08:06:10.130Z","updatedAt":"2014-07-11T08:06:10.130Z"}',
'{"id":2,"title":"b","createdAt":"2014-07-11T08:06:10.130Z","updatedAt":"2014-07-11T08:06:10.130Z"}',
'{"id":3,"title":"c","createdAt":"2014-07-11T08:06:10.130Z","updatedAt":"2014-07-11T08:06:10.130Z"}',
'{"id":4,"title":"d","createdAt":"2014-07-11T08:06:10.130Z","updatedAt":"2014-07-11T08:06:10.130Z"}' ]
Executing (default): SELECT * FROM "forum_topic" WHERE title <> 'a';
[ '{"id":2,"title":"b","createdAt":"2014-07-11T08:06:10.130Z","updatedAt":"2014-07-11T08:06:10.130Z"}',
'{"id":3,"title":"c","createdAt":"2014-07-11T08:06:10.130Z","updatedAt":"2014-07-11T08:06:10.130Z"}',
'{"id":4,"title":"d","createdAt":"2014-07-11T08:06:10.130Z","updatedAt":"2014-07-11T08:06:10.130Z"}' ]
done
'use strict';
var SEQUELIZE = require('sequelize');
var sequelize = new SEQUELIZE('postgres://test:@localhost:5432/test', { logging : console.log.bind(console) });
var chainer = new SEQUELIZE.Utils.QueryChainer();
var Topic = sequelize.define('ForumTopic', {
title : SEQUELIZE.STRING
}, { tableName: 'forum_topic' });
sequelize.sync({force : true})
.then(function () {
return Topic.bulkCreate([
{title: 'a'},
{title: 'b'},
{title: 'c'},
{title: 'd'}
]);
})
.then(function () {
return Topic.findAll();
})
.then(function (data) {
console.log(data.map(function (topic) { return JSON.stringify(topic.dataValues); }));
return sequelize.query('SELECT * FROM "forum_topic" WHERE title <> ?;', null, {raw: true}, ['a']);
})
.then(function (data) {
console.log(data.map(function (topic) { return JSON.stringify(topic); }));
})
.error(function (err) {
console.error(err)
})
.done(function () {
console.log('done');
});
@qfox
Copy link
Author

qfox commented Jul 11, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment