Skip to content

Instantly share code, notes, and snippets.

@davetapley
davetapley / validate-and-upsert-2.js
Created April 29, 2013 16:49
Validate a Mongoose model and then upsert if valid (the other way)
mySchema.statics.upsert = function (conditions, update, callback) {
this.findOne(conditions, function (err, existingDoc) {
if (err) {
callback(err);
} else if (existingDoc) {
// I don't think this is valid
existingDoc.update(update);
existingDoc.save(callback);
} else {
return this.create(update, callback);
@davetapley
davetapley / validate-and-upsert.js
Created April 29, 2013 16:29
Validate a Mongoose model and then upsert if valid
mySchema.statics.validateAndUpsert = function (conditions, update, callback) {
var updateModel = new this(update);
updateModel.validate(function (err) {
if (err) {
callback(err);
} else {
this.findOneAndUpdate(conditions, update, { upsert: true}, callback);
}
}
};
@davetapley
davetapley / fail.sh
Created April 24, 2013 01:05
PostgreSQL roles issue on Ubuntu 10.04
vagrant@heroku:~$ sudo su postgres
$ psql
psql (9.1.9)
Type "help" for help.
postgres=# SELECT rolname FROM pg_roles;
rolname
----------
postgres
vagrant
@davetapley
davetapley / lat_hist.js
Created April 16, 2013 17:29
Pull entire Latitude history for a user
// https://developers.google.com/latitude/v1/using#ListingHistory
var minTimeMs = 0, // Earliest location to pull, 1st Jan 1070 seems far back enough.
maxTimeMs = Date.new().getTime(), // Latest location to pull, now.
lastResponse = [];
while {
// it is assumed this returns a max of 1000 locations in reverse chronological order,
// most recent location before maxTime first,
// then 999 more locations going back in time.
@davetapley
davetapley / mongojs_count.js
Last active December 16, 2015 02:59
Count for mongojs cursor is returned incorrectly.
var testCollection = require('mongojs')('test').collection('testCollection'),
shouldBeOneDoc = testCollection.find({}).sort({ myField : 1 }).limit(1);
shouldBeOneDoc.count(function (err, count) { console.log(count) });
@davetapley
davetapley / events.js
Created April 10, 2013 17:45
Trouble using for in loop with mocha
/* Mocha test
to use:
npm install mocha
mocha <filename>
or
npm test
*/
var assert = require('assert');
var tzwhere = require('../lib/index');
exports.routes = function() {
this.get('/', function() {
var users = db.collection('users');
this.res.setHeader("Content-Type", 'text/html');
this.res.writeHead(200, {});
var that = this;
var stream = users.find().forEach(function(err, userDoc) {
if(userDoc) {
@davetapley
davetapley / lat_lngs.txt
Created March 26, 2013 01:45
Generated with: > r = Random.new; 50.times { puts r.location(34.393, -113.475, 10000).join ',' }
34.442688693689824,-113.53830101755034
34.33576843364106,-113.42973400738283
34.44648524918964,-113.52317003164038
34.41614244624507,-113.42110221380793
34.443154610235155,-113.49498783458732
34.43219600146056,-113.43444706591943
34.36054463655045,-113.53565715125131
34.427160925658754,-113.47398007995909
34.43880963756491,-113.43854457484196
34.37758904544772,-113.51267759234292
@davetapley
davetapley / error.xml
Created March 21, 2013 18:18
pgbackups download error
<!-- Running command:
curl -o latest.dump `heroku pgbackups:url --app xxxxx`
At 100% we get: -->
<Error><Code>AccessDenied</Code><Message>Request has expired</Message><RequestId>xxxxxxx</RequestId><Expires>2013-03-21T16:02:26Z</Expires><HostId>xxxxxxx</HostId><ServerTime>2013-03-21T16:50:38Z</ServerTime></Error>
@davetapley
davetapley / 304-problem.rb
Last active December 14, 2015 13:18
When hitting the `index` action at `/places.json` I'm always getting a `200` and the expected JSON. But hitting the `map` action at `/places/map.json` always gives `304`.
# config/routes.rb
resources :places, only: [:index, :edit, :update], constraints: {format: :json} do
collection do
get 'map'
end
# app/controllers/places_controller.rb