Console using postgres+sequelize with Node.js? - node.js

I am fairly new to Node but I am loving the tool. My only problem is when I want to have direct access to the database. I have a good experience with ruby on rails+postgres. Using rails console was very helpful when I was developing rails.
Is there some kind of equivalent I can use to have direct access to my database? I have uploaded my app to heroku so I would like something that I can run on heroku as well.
(I prefer not to use SQL, I am wondering if there is a sequelize console?)

Here is the way to do it:
node --experimental-repl-await
> models = require('./models');
> User = models.User; //however you load the model in your actual app this may vary
> await User.findAll(); //use await to avoid promise errors
TLDR
This gives you access to all of the models you have created and you can use the sequelize ORM commands like findAll, create etc.. just as you would in Rails active record.
Sequelize uses promises, so to run these properly in REPL you will want to use the --experimental-repl-await flag

Related

MongoDB database cleaner for Nightwatch.js

Is there any way of wiping a mongo database in between Nightwatch e2e tests?
I'm coming from Ruby, where you can configure RSpec to use a package called database cleaner and wipe your db after each test, and I'm wondering if a similar setup exists in the javascript ecosystem.
I did some research and found a promising-looking package called node-database-cleaner, but at present it is throwing an error.
Code:
require('mongodb');
const DatabaseCleaner = require('database-cleaner');
const databaseCleaner = new DatabaseCleaner('mongodb');
...test assertions
databaseCleaner.clean('mongodb://localhost:27017/my-db')
Error:
TypeError: db.collections is not a function
I'm not bound to using node-database-cleaner—I'd be interested in any solution no matter what library it uses.

socket.io authentication with socketio-auth

I've a server in node.js & Express and i use socket.io for real-time messaging and socketio-auth module for authentication,I setup everything for using this module but i found nothing about db object and findUser that the auther used in examples(not even an single line of comment about them) to work with,should i implement them myself,right?
if anyone have a working example of using or implementing them with redis i'll be grateful to see it.
the db and findUser object is from MongoDb and not related to socket.io. I would recommend you look at http://www.tutorialspoint.com/mongodb/ to gain a better understanding as to why they are used

Sails/Bookshelf Running a script that uses the Sails/Bookshelf environment

I would like to create a script to be run in the background of my server as a cron task.
I would like the script to have access to the sails environment (ie, loading all the modules, especially bookshelf and knex, and the database connection).
so that I could create a file myscript.js that looks something like
var environment = require("sails_environment")
// code that uses bookshelf etc exactly as if it were written
// inside a controller action
I actually only need the bookshelf module and db connection for this script, so it could be that bookshelf has a way to do this, but I imagine it is something built in to Sails.
There are lots of ways to do what you want. Here are a few.
https://github.com/balderdashy/sails/issues/2092#issuecomment-56043637
https://www.npmjs.com/package/sails-hook-schedule
http://www.worldnucleus.com/2014/12/run-cron-job-in-sailsjs.html

MarkLogic - node.js - How to require cts for search

I have the node db connection working okay I think:
var marklogic = require('marklogic');
var db = marklogic.createDatabaseClient({ ...
And now I want to do cts.search
How do I require cts?
You don't. The cts functions are for running queries within the database. Working with the Node.js Client API, you'll use the query builder. There's an example in the feature introduction, with more detail in the Getting Started tutorial and the Node.js Application Developer's Guide.
You may be mixing up the Node.js Client API with Server-side JavaScript. The former is used to have middle-tier Node.js code interact with MarkLogic. The latter is for writing REST API extensions or transformations that run within the database (and can be called from the Node API).

How do I unit test keystonejs models?

Is there any way to run tests for keystonejs that also hit a test or real mongodb instance?
It would be nice if similar to the way Django does it.
There aren't any official examples of implementing unit testing for KeystoneJS sites yet, but there wouldn't be anything stopping you from writing tests with a framework like mocha, the way you would in any other node.js app.
You'd want to initialise Keystone, register your models, then connect to the database and execute tests without starting the web server. Something like this:
./tests.js
var keystone = require('keystone');
keystone.init({
'name': 'Your Project'
});
keystone.import('models');
keystone.mongoose.connect('localhost', 'your-database');
keystone.mongoose.connection.on('open', function() {
// Run tests here
// Use keystone.list('Key') to access Lists and execute queries
// as you would in your main application
});
then run tests.js, or make it an npm / grunt / etc. script.
Keep an eye on issue #216 for an integrated testing framework.

Resources