Skip to content

Instantly share code, notes, and snippets.

@qfox
Created July 7, 2014 07:48
Show Gist options
  • Save qfox/19662d3ec545a149924c to your computer and use it in GitHub Desktop.
Save qfox/19662d3ec545a149924c to your computer and use it in GitHub Desktop.
proposed junglingdb test for model field names in db
DROP TABLE IF EXISTS "forum_topic" 1404719138606
DROP TABLE IF EXISTS "forum_comment" 1404719138610
CREATE TABLE "forum_topic" (
"id" SERIAL PRIMARY KEY,
"status" boolean ,
"createdAt" timestamp with time zone ,
"updatedAt" timestamp with time zone ,
"title" varchar
) 1404719138624
CREATE TABLE "forum_comment" (
"id" SERIAL PRIMARY KEY,
"status" boolean ,
"createdAt" timestamp with time zone ,
"updatedAt" timestamp with time zone ,
"title" varchar ,
"topicId" integer ,
"parent" integer
) 1404719138629
DELETE FROM "forum_topic" 1404719138786
INSERT INTO "forum_topic" ("status","title") VALUES (NULL,E'John Lennon') RETURNING id 1404719138791
z 9
INSERT INTO "forum_topic" ("status","title") VALUES (NULL,E'Paul McCartney') RETURNING id 1404719138794
z 11
INSERT INTO "forum_topic" ("status","title") VALUES (NULL,E'George Harrison') RETURNING id 1404719138795
z 15
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'make the tor',1) RETURNING id 1404719138812
z 14
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'fire the hipe',1) RETURNING id 1404719138813
z 13
INSERT INTO "forum_topic" ("status","title") VALUES (NULL,E'Ringo Starr') RETURNING id 1404719138796
z 17
INSERT INTO "forum_topic" ("status","title") VALUES (NULL,E'Pete Best') RETURNING id 1404719138797
z 21
INSERT INTO "forum_topic" ("status","title") VALUES (NULL,E'Stuart Sutcliffe') RETURNING id 1404719138798
z 21
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'fire the hipe',1) RETURNING id 1404719138812
z 20
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'clean the nose',1) RETURNING id 1404719138813
z 19
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'rake the pie',2) RETURNING id 1404719138815
z 18
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'make the swarm',2) RETURNING id 1404719138815
z 17
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'clean the tor',2) RETURNING id 1404719138815
z 16
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'clean the pepper',3) RETURNING id 1404719138826
z 15
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'rake the nose',3) RETURNING id 1404719138826
z 14
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'clean the hipe',3) RETURNING id 1404719138827
z 13
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'make the pie',3) RETURNING id 1404719138828
z 12
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'make the hipe',3) RETURNING id 1404719138828
z 11
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'take the hipe',4) RETURNING id 1404719138842
z 10
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'take the hipe',4) RETURNING id 1404719138843
z 9
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'bake the swarm',4) RETURNING id 1404719138844
z 8
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'fire the nose',4) RETURNING id 1404719138844
z 7
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'take the pie',4) RETURNING id 1404719138844
z 6
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'fire the pepper',5) RETURNING id 1404719138846
z 5
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'clean the hipe',5) RETURNING id 1404719138846
z 4
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'bake the swarm',5) RETURNING id 1404719138846
z 3
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'fire the pepper',5) RETURNING id 1404719138846
z 2
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'rake the pepper',5) RETURNING id 1404719138847
z 1
INSERT INTO "forum_comment" ("status","title","parent") VALUES (NULL,E'fire the nose',6) RETURNING id 1404719138848
z 0
qwe
qwe
SELECT "status", "createdAt", "updatedAt", "title", "id" FROM "forum_topic" 1404719138885
SELECT "status", "createdAt", "updatedAt", "title", "topicId", "id", "parent" FROM "forum_comment" WHERE "parent" IN (E'1',E'2',E'3',E'4',E'5',E'6') 1404719138890
/home/alex/repos/jugglingdb/lib/include.js:138
for (var j = 0; j < objectsFrom.length; j++) {
^
TypeError: Cannot read property 'length' of undefined
at done (/home/alex/repos/jugglingdb/lib/include.js:138:44)
at /home/alex/repos/jugglingdb/lib/include.js:205:21
at /home/alex/repos/jugglingdb/lib/model.js:525:13
at Function.AbstractClass.include (/home/alex/repos/jugglingdb/lib/include.js:36:9)
at PG.<anonymous> (/home/alex/repos/jugglingdb-postgres/lib/postgres.js:261:39)
at null.callback (/home/alex/repos/jugglingdb-postgres/lib/postgres.js:71:13)
at Query.handleReadyForQuery (/home/alex/repos/jugglingdb-postgres/node_modules/pg/lib/query.js:84:10)
at null.<anonymous> (/home/alex/repos/jugglingdb-postgres/node_modules/pg/lib/client.js:159:19)
at EventEmitter.emit (events.js:117:20)
at null.<anonymous> (/home/alex/repos/jugglingdb-postgres/node_modules/pg/lib/connection.js:97:12)
'use strict';
// test
var JDB = require('jugglingdb'),
URL = require('url');
// db
var dbConf = parseConnString('postgres://test:@localhost/test?debug');
dbConf.adapter = require('jugglingdb-postgres');
var pg = new JDB.Schema(dbConf.adapter, dbConf);
pg.log = console.log.bind(console);
/**
* models
*/
var Topic = pg.define('ForumTopic', {
//id : { type: Number, dataType: 'integer', autoIncrement: true, primaryKey: true },
status : Boolean,
createdAt : { type: Date, default: Date.now, name: 'sys_created' },
updatedAt : { type: Date, default: Date.now, name: 'sys_modified' },
title : { type: String, length: 255 }
}, {
table : 'forum_topic'
});
var Comment = pg.define('ForumComment', {
//id : { type: Number, dataType: 'integer', autoIncrement: true, primaryKey: true },
status : Boolean,
createdAt : { type: Date, default: Date.now, name: 'sys_created' },
updatedAt : { type: Date, default: Date.now, name: 'sys_modified' },
title : { type: String, length: 255 },
topicId : { type: Number, dataType: 'integer', name: 'parent' }
}, {
table : 'forum_comment'
});
/**
* references
*/
//Comment.belongsTo('topic', { model : Topic, foreignKey : 'parent' });
Topic.hasMany('comments', { model : Comment, foreignKey : 'parent' });
/**
* action
*/
pg.automigrate(function (err, data) {
seed(function (err, done) {
console.log('qwe');
// this is where we continue ...
Topic.all({ include: 'comments' }, function (err, topicsWithComment) {
console.log('asd');
//console.log(JSON.stringify(topicsWithComment));
pg.disconnect();
});
console.log('qwe');
});
});
// utils
function parseConnString (str) {
var r = Object.create(null),
c = URL.parse(str, true);
if (!c.protocol || !c.pathname.replace(/\//g, '')) {
throw new Error('Invalid db conn string ' + JSON.stringify(str));
}
r.adapter = c.protocol.replace(/:$/, '');
r.database = c.pathname.replace(/^\//, '').replace(/\//, '_');
var auth = c.auth.split(/:/);
if (auth) {
if (auth[0]) { r.username = auth[0]; }
if (auth[1]) { r.password = auth[1]; }
}
if (c.port) { r.port = c.port; }
if (c.hostname) { r.host = c.hostname; }
r.debug = c.debug !== undefined;
r.ssl = c.ssl !== undefined;
return r;
}
function seed (done) {
var topics = [
{ title: 'John Lennon' },
{ title: 'Paul McCartney' },
{ title: 'George Harrison' },
{ title: 'Ringo Starr' },
{ title: 'Pete Best' },
{ title: 'Stuart Sutcliffe' }
];
var count = Number(topics.length);
Topic.destroyAll(function () {
topics.forEach(function (topicData) {
Topic.create(topicData, function (err, topic) {
if (err) { throw err; }
for (var i = 0, l = Math.random() * 5; i < l; i += 1) {
count += 1;
topic.comments.create({ title: r() }, ok);
}
ok();
});
});
});
function ok (err, ok) {
if (err) { throw err; }
count -= 1;
console.log('z', count);
if (!count) {
done();
}
}
function r () {
var q = ['take', 'make', 'bake', 'rake', 'clean', 'fire'],
w = ['tor', 'pepper', 'pie', 'nose', 'hipe', 'swarm'];
return [
q[Math.floor(Math.random() * q.length)],
'the',
w[Math.floor(Math.random() * w.length)]
].join(' ');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment