WhyTF am I putting my Node in The Intern?
-
This is basic JS stuff but fuck if I remember:getLists: function(callback) { if (!db) { console.log("Creating database..."); this.createDB(this.file, function() { console.log("Database created. Retrying..."); getLists(callback); //<--- This errors }); return; };
How do I access the containing scope from that deeply nested?Nevermind, I have to close over this:
var self = this; this.createDB(this.file, function() { console.log("Database created. Retrying..."); self.getLists(callback); });
-
From the topic title, I expected at least one Bill Clinton joke in here. I am disappoint.
-
-
Trust you'd be the first to moan about the Super Mega Ultra Hyper Omega Ultimate Facepalm
-
I reserve the right to moan about anything
-
-
According to the documentation, foreign keys constraints need to be explicitely enabled when opening the database:
Yeah -- they didn't go with on-by-default for backwards-compat reasons, I suspect. Fortunately, once they're on for a database, they're on.
-
OK So.
Logically, I have three models: lists, items, and users. I have currently combined lists and items into the same DAO, but ideally, users should have its own DAO probably.
BUT. The createDB method bootstraps the WHOLE database. So ideally, the code for that method would be shared between the two DAOs so that any of them can bootstrap the database correctly without being WET.
I would normally use inheritance: create a BaseDAO and the specific ones add CRUD for their given thing on top of it. What is the "Nodey" solution to this problem?
-
...wat.
yeah... it's stupid, it's for backwards compatibility with old versions of sqlite that let you define FKs but had absolutely no way to actually enforce them.....
now i think they should have gone the other way with their backwards compatibility, but they didn't and we're stuck with it.
-
Apparently in version 4 it'll be on by default, but version 3 is giving people time to switch
-
now i think they should have gone the other way with their backwards compatibility, but they didn't and we're stuck with it.
They should have taken a hint from MySQL and defined a
SQLITE_REAL_FOREIGN_KEY
keyword that would provide actual foreign keys...
-
isn't that PHP not MySQL you're poking fun at?
-
isn't that PHP not MySQL you're poking fun at?
I know that that function name is often used to poke fun at PHP, but AFAIK the original sin was committed by the MySQL developers. From
/usr/include/mysql/mysql.h
:unsigned long STDCALL mysql_real_escape_string(MYSQL *mysql, char *to,const char *from, unsigned long length);
So the PHP developers weren’t directly responsable for this. That doesn’t excuse them for directly exporting this function to PHP code without thinking, though.
-
hu-uh... TIL.
-
Another question:
A typical execution that runs all tests and outputs results to the console would look like this:
intern-client config=tests/intern
The commands shown above rely on npm being installed and configured properly. If your environment PATH is not set properly, you may need to run node_modules/.bin/intern-client instead of intern-client.
What does "configured properly" mean? I have npm installed, that's how I installed intern-client.
-
And another!
server.route({ method: "POST", path: "/list/add", handler: TodoController.createList }); server.route({ method: "GET", path: "/list/{id}", handler: TodoController.fetchList });
Sending a post request works as expected, but when I echo the newly-created ID back into a get request, I never make it to the TodoController.fetchList method, as proven by a console.log and the test timing out.
Any obvious mistakes here?
-
but that's when I ended up with serialization issues
Doesn't db.serialize handle serialization for you?
-
Yeah -- they didn't go with on-by-default for backwards-compat reasons, I suspect. Fortunately, once they're on for a database, they're on.
Exactly that. You need to use
PRAGMA foreign_keys = ON
once per connection. You're advised to do just that.I think that drh ought to flip the default state on that, but it's not my call.
-
I have now two files worth of unit tests and one integration test. My latest unit test uses Sinon to mock out the DAO object.
Why does this break the Integration test by.... having it suddenly return foreign key constraint violations?! If I comment out the instruction for The Intern to run the new unit test, it passes. If I run all three files, it fails. Dafuq.
Answer: Sinon.js doesn't clean up after itself and leaks abstractions. But I never wrote a test with a foreign key constraint failure in it. So.... magic.
-
@accalia Does this look right for using async.js?
-
looks good. ;-)
did have one suggestion though. left it on github for you.
-
I already did what you suggested, though I missed one spot :)
-
it's pretty solid and really shows what async.js is good for (reducing callback hell, and making async control flow)
-
I've been asked to compare it with Promises, so i'm doing another rewrite as well, but yeah, it looks a lot tidier than doing nothing
-
Promises,
in my professional opinion: ick!
it's not that promises are a bad idea, properly implemented they're great! but there's too many non compatible implementations of promises for Node.JS... they all do slightly different things with slightly different syntax! and goddess preserve you when you start using libraries that use a different form of promise than you use.
-
I'm using Bluebird, and I'm actually kind of stumped on how this doesn't nest infinitely.
Okay, so I can do
dao.createDB(":memory:").then( );
But then is going to take a function, so I'm going to want to start nesting:
dao.createDB(":memory:").then( dao.getLists().then( dao.createList("insertTest") ) );
This isn't better :/
Upon reading more, I can see several places where promises would be better, but not for this use case I think. The exception handling is downright nice with Bluebird.
-
Ok, I'm testing the controller. You can call the reply object like
reply("some response")
or likereply.view("viewname", info)
. How do I mock that with sinon? In one test I'm doingvar fakeReply = { view: sandbox.spy() };
And in the other I'm doing
var fakeReply = sandbox.spy();
But it's complaining it's the wrong type of spy at a given time.
-
-
I know nobody cares anymore but I'm stuck again.
This time the database, which I let the code generate if it's not at the expected location, is not generating. It generated it the first dozen or so times, but when I deleted the file last time I made a schema change, it hasn't recreated it. It doesn't throw an error either. It just hangs forever.
createDB: function(filename, callback) { this.file = filename; this.db = new sqlite3.Database(this.file, sqlite3.OPEN_READWRITE || sqlite3.OPEN_CREATE, function(err) { if (err) callback(err); }); this.db.serialize(); this.db.run("DROP TABLE IF EXISTS TodoItems",errorprint); this.db.run("DROP TABLE IF EXISTS TodoLists",errorprint); this.db.run("DROP TABLE IF EXISTS Users",errorprint); this.db.run("CREATE TABLE Users (userID INTEGER PRIMARY KEY, username TEXT, password TEXT)",errorprint); this.db.run("CREATE TABLE TodoLists (listID INTEGER PRIMARY KEY, listName TEXT, owner INTEGER, FOREIGN KEY(owner) REFERENCES Users(userID))",errorprint); this.db.run("CREATE TABLE TodoItems (itemID INTEGER PRIMARY KEY, listID INTEGER NOT NULL, itemName TEXT, itemText TEXT, state INTEGER, FOREIGN KEY(listID) REFERENCES TodoLists(listID))",errorprint); this.db.run("PRAGMA foreign_keys = ON", errorprint); this.db.parallelize(); if (callback) callback(); },
-
...wtf syntax highlighting?
-
It ignored the
js
hint (apparently the hint is supposed to bejavascript
, not sure if that's a Discourse thing) and guessed SQL. As to why the "SQL" highlighting is wonky on that, who knows.
-
Thanks, fixed.
So I manually created the DB and it still doesn't work. WTF.DISREGARD I SUCK COCKSI forgot I changed the name of the file in case it was in some weird psuedo-deleted state. It works now that I manually made the DB
-
As to why the "SQL" highlighting is wonky on that, who knows.
Because the SQL is in quotes and stuff in quotes in SQL isn't actually SQL even if it looks like SQL
select "select column from randomtable", anothercolumn from randomtable;
-
No, the highlighting when it thought the code was SQL wasn't matching the quotes. At least, it didn't get the first quote, but it did the rest.
createDB: function(filename, callback) { this.file = filename; this.db = new sqlite3.Database(this.file, sqlite3.OPEN_READWRITE || sqlite3.OPEN_CREATE, function(err) { if (err) callback(err); }); this.db.serialize(); this.db.run("DROP TABLE IF EXISTS TodoItems",errorprint); this.db.run("DROP TABLE IF EXISTS TodoLists",errorprint); this.db.run("DROP TABLE IF EXISTS Users",errorprint); this.db.run("CREATE TABLE Users (userID INTEGER PRIMARY KEY, username TEXT, password TEXT)",errorprint); this.db.run("CREATE TABLE TodoLists (listID INTEGER PRIMARY KEY, listName TEXT, owner INTEGER, FOREIGN KEY(owner) REFERENCES Users(userID))",errorprint); this.db.run("CREATE TABLE TodoItems (itemID INTEGER PRIMARY KEY, listID INTEGER NOT NULL, itemName TEXT, itemText TEXT, state INTEGER, FOREIGN KEY(listID) REFERENCES TodoLists(listID))",errorprint); this.db.run("PRAGMA foreign_keys = ON", errorprint); this.db.parallelize(); if (callback) callback(); },
-
Ã’_o
-
-
What SQL dialect are they even using? Both
'
and"
are used to quote strings in MySQL, withbacktick
quoting table and column names. Meanwhile, Postgres uses'
for strings and"
for table and column names.I'm sure other dialects do their own weird stuff as well.
-
I'm sure other dialects do their own weird stuff as well.
Single quotes are used to indicate the beginning and end of a string in SQL. Double quotes generally aren't used in SQL, but that can vary from database to database.
-
Well, yes, I'm aware that there is some canonical spec floating about, but at this point I don't expect any conformity, anywhere. Postgres is case-sensitive when it comes to column and table names. Oracle and MSSQL aren't, AFAIK. I don't think MySQL is either. Postgres and MySQL use
LIMIT
, MSSQL usesTOP
. Getting the last inserted ID in any kind of semi-reliable way varies wildly for each RMDBS. Some SQL dialects can just do arithmetics, in Oracle you select fromdual
.It's a fucking mess, that's what it is.
-
-
Oracle is case-sensitive if you wrap the name in double-quotes, and also allows more characters in names that way.
-
Oracle is case-sensitive if you wrap the name in double-quotes
Well... you can say Postgres is the same, kinda? If unquoted it will be converted to lowercase no matter what you give it, and if the column / table name is lowercase it will match (so
... FROM someTable ...
will matchsometable
). The difference being Oracle will either uppercase / lowercase both, so... FROM someTable ...
would match bothsometable
andSOMETABLE
, while in Postgres... from someTable...
will match onlysometable
...and also allows more characters in names that way
-
Nope,
someTable
only matches"SOMETABLE"
, not"someTable"
or"sometable"
-
Ah, so it's
toupper
, gotcha. Other way around in Postgres then. Didn't work that much on Oracle DBs, guess they were all using uppercase names so I got that misconception stuck in my head.
-
Meanwhile, MSSQL is case sensitive. Though I think you might be able to turn that off.
-
-
Meanwhile, MSSQL is case sensitive. Though I think you might be able to turn that off.
So again, either got things mixed up or working in the environment where it was turned off.
But this kinda makes my point for me, really. This shit is so confusing I got half of this shit wrong and I'm only really sure about Postgres because I actively use it. Everything else would require googling / reading books.
-
For MSSQL at least, it seems to be linked to the DB's default collation whether object names are case sensitive or not; well, it used to be anyway. Things may have changed.
-
@PleegWat said:
and also allows more characters in names that way
Not a WTF. It means that you can use characters not normally allowed in names, such as punctuation, or names that are the same as SQL keywords. That the names are names of a table instead of a column or index or other entity, that's merely coincidental.
-
I interpreted that as "longer string", not "a larger set of characters allowed", hence the
But yes, given your interpretation it's completely reasonable.