Skip to content

Instantly share code, notes, and snippets.

@heroic
heroic / node-mysql
Created February 5, 2011 10:07
node-mysql query issue
var sys = require('sys');
var io = require('socket.io');
var http = require('http');
var db_client = require("mysql").Client;
var db = new db_client();
db.user = "root";
db.password = "hello"
db.connect();
db.query("USE development_test");
@heroic
heroic / table schema
Created February 5, 2011 10:09
node-mysql issue table schema
CREATE TABLE IF NOT EXISTS `friends` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`to_id` int(11) NOT NULL,
`relation_id` text,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `index_friends_on_user_id` (`user_id`),
@heroic
heroic / chat_test.html
Created February 5, 2011 10:11
node-mysql chat test file
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Page Title</title>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
socket = new io.Socket('localhost',{
port: 8080
});
@heroic
heroic / inflections
Created February 28, 2011 03:03
some inflections
var inflect = require('./inflector.js');
inflect.plural(/$/, 's')
inflect.plural(/s$/i, 's')
function Inflector() {
var plurals = singulars = uncountables = humans = [];
}
exports.inflector = Inflector;
Inflector.prototype = {
plural: function(rule, replacement) {
if(rule instanceof String) {
this.uncountables.splice(this.uncountables.indexof(rule), 1)
@heroic
heroic / gist:939366
Created April 24, 2011 06:06
bcrypt
var bcrypt = require('bcrypt')
// function encrypt_password(password, salt, cb) {
// var encrypted = password + salt
// for (var i=10;i>0;i--) {
// encrypted = sha512(encrypted)
// }
// bcrypt.encrypt(encrypted, salt, function(err, hash) {
// cb(hash)
// })
// }
@heroic
heroic / gist:961551
Created May 8, 2011 18:12
node-mongo
var mongo = require("mongodb"), db = new mongo.Db("test", new mongo.Server("localhost", mongo.Connection.DEFAULT_PORT), {})
db.open(function(err, db) {
if(err) {
throw new Error(err)
} else {
db.collection("testing", function(err, collection) {
if(err) {
console.log(err.message)
} else {
@heroic
heroic / gist:962290
Created May 9, 2011 09:40
upsert issue
the data
{ _id: '4db932f1d50e96fda3c84b1b',
email: 'email@email.com',
encrypted_password: 'password',
friends:
[ '4db96b973d01205364000006',
'4db94a1948a683a176000001',
'4dc77b24c5ba38be14000002' ],
location: [ 72.4930088, 23.0431957 ],
var mongo = require("mongodb"), db = new mongo.Db("test", new mongo.Server("localhost", mongo.Connection.DEFAULT_PORT), {strict: true})
db.open(function(err, db) {
if(err) {
throw new Error(err)
} else {
db.createCollection("testing", function(err, collection) {
collection.save({
email: 'email@email.com',
encrypted_password: 'password',
var mongo = require("mongodb"), db = new mongo.Db("test", new mongo.Server("localhost", mongo.Connection.DEFAULT_PORT), {strict: true})
db.open(function(err, db) {
if(err) {
throw new Error(err)
} else {
db.collection("testing", function(err, collection) {
collection.find({}).limit(1).toArray(function(err, users){
user = users[0]
if(err) {