Sunday, 19 March 2017

Node.js - generate ugly class names

Is there a way to generate ugly class names? I mean if there's a plugin that I could use to replace .top-header class with .a9ev in my css. Also, would be great if class name in my html was also changed

How big sites like facebook or google do this?



via delco

Accessing nested object in nested array using a variable (MongoDB, Node, Inc)

I am aware of the myriad of related topics to this question but none seem to answer my problem. It is related to Node and Mongo but I think this is a general Javascript issue.

In MongoDB I need to access the options array with a variable giving the index number and then increment it's votes property. An example would be:

{
    "_id": {
        "$oid": "58ce7c6c39aff90a7c66b0d4"
    },
    "question": "Test",
    "options": [
        {
            "answer": "answer1",
            "option": "Bob Dylan",
            "votes": 2
        },
        {
            "answer": "answer2",
            "option": "Bob Geldof",
            "votes": 0
        }
    ]
}

I then get the index of the array I need to update like so:

var votedFor = "answer2";
votedFor = votedFor.match(/\d+/)[0];
var index = parseInt(votedFor) - 1;

Which I then assume the path to the integer to increment would be:

var inc = {};
inc["options[" + index + "].votes"] = 1;

Which I would like to pass in like so:

db.collection('questions')
.findOneAndUpdate({"_id": questionId}, {$inc : inc }, (err, result) => {
  if (err) return res.send(err)
  res.send(result)
})

Which updates the db like so:

    {
        "_id": {
            "$oid": "58ce7c6c39aff90a7c66b0d4"
        },
        "question": "Test",
        "options": [
            {
                "answer": "answer1",
                "option": "Bob Dylan",
                "votes": 2
            },
            {
                "answer": "answer2",
                "option": "Bob Geldof",
                "votes": 0
            }
    ],
    "options[1]": {
        "votes": 1
    }
}

Which, as you can see, as opposed to incrementing options[1].votes it has created a new property instead.

Is there any way of accessing the array index with a variable like this?



via swhizzle

Conditional testing withing a NightWatchJS test script

I am trying to write a nightwatch test script with conditions in the test script. My code so far

module.exports = {
tags: ['getting-started'],
set_url: function (browser) {
browser.url('http://www.google.com');
browser.pause(5000);
browser.assert.title('Google');
if (browser.expect.element('#main').to.be.present) {
    browser.pause(5000);
    browser.setValue('input[type=text]', ['Night Watcher', browser.Keys.ENTER]);
    browser.pause(5000);
    if(browser.assert.containsText('#main', 'The Night Watch')){
    console.log('search has the right result'); // for example
    }else{
    console.log('No result found');
    }
}
browser.end();
}

} But the browser.expect.element('#main').to.be.present and browser.assert.containsText('#main', 'The Night Watch') returns an object and is not actually the result I am interested with. But the browser.expect.element('#main').to.be.present and browser.assert.containsText('#main', 'The Night Watch') returns an object and is not actually the result I am interested with.



via Chum H

Is reproducible benchmarking possible?

I need to test some node frameworks, or at least their routing part. That means from the request arrives at the node process for processing until a route has been decided and a function/class with the business logic is called, e.g. just before calling it. I have looked hard and long for a suitable approach, but concluded that it must be done directly in the code and not using an external benchmark tool. I fear measuring the wrong attributes. I tried artillery and ab but they measure a lot more attributes then I want to measure, like RTT, bad OS scheduling, random tasks executing in the OS and so on. My initial benchmarks for my custom routing code using process.hrtime() shows approx. 0.220 ms (220 microseconds) execution time but the external measure shows 0.700 (700 microseconds) which is not an acceptable difference, since it's 3.18x additional time. Sometimes execution time jumps to 1.x seconds due to GC or system tasks. Now I wonder how a reproducible approach would look like? Maybe like this:

  • Use Docker with Scientific Linux to get a somewhat controlled environment.
  • A minimal docker container install, node enabled container only, no extras.
  • Store time results in global scope until test is done and then save to disk.
  • Terminate all applications with high/moderate diskIO and/or CPU on host OS.
  • Measure time as explained before and crossing my fingers.

Any other recommendations to take into consideration?



via Gillsoft AB

Sequelize CLI how to create migrations from models?

I have two models with relations one to many.

I don't understand how to create migration files. Does each model have its own migration file or one migration file can create several tables from models and relations between them (for example as in rails migrations)?

I had a look at many examples including Sequelize docs, and there are primitive examples of models creating and its migration.

//User model
module.exports = function (sequelize, Sequelize) {
    var User = sequelize.define('users', {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true,
        },
        email: {
            type: Sequelize.STRING,
            allowNull: false,
            unique: true,
        },
        password: {
            type: Sequelize.STRING,
            allowNull: false,
        },
    });

    return User;
}

//Order model
module.exports = function (sequelize, Sequelize) {
    var Order = sequelize.define('orders', {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true,
        },
        price: {
            type: Sequelize.INTEGER,
            allowNull: false,
        },
        totalPrice: {
            type: Sequelize.INTEGER,
            allowNull: false,
        },
    });

    return Order;
}

//db.js
//Relations
db.orders.belongsTo(db.users);
db.users.hasMany(db.orders);



via Sergei R

Is there something like sync-gateway to MongoDb?

Hello everyone I'm trying to do a proyect, this proyect had an android aplication, a simply Rest web service in PHP or Nodejs and a Couchbase like database with its own server. But I'm having a lot of problems with couchbase, I can't connect with PHP neither NodeJs, so I tried to connect NodeJs with MongoDB and It Works!, but I wanted to use Couchbase because I liked sync-gateway for android aplications, so my question is if is there a sync-gateway for mongodb?.

Sorry for my english. Thank you so much.



via code lost

Upload file with MEAN stack (angular2, node.js, express, mongodb)

I want to be able to store files according to the user of the application. The client will send me the file to be stored, as well as the token to identify the user, and also the name that the user gave to his file, the year of the file... This information will be sent to me through a multipart HTTP request. With Node.js, I want to be able to retrieve the file, token and other information sent to store it and update the database.

I saw that I can use Multer but I don't understand how to recover both the file and also the token and other information sent. I'm using Node.JS, Express and MongoDB.

PS : Excuse me for misspellings i am french and Node.js is new for me.



via Lea