EzDevInfo.com

waterline

An adapter-based ORM for Node.js with support for mysql, mongo, postgres, redis, and more

How to show queries in console log using sails?

I am starting a project with sails and mysql, and I do'nt know how configurate it to show the queries executed in the console.


Source: (StackOverflow)

Waterline ORM (sails.js) conditions with NOT Equal in query

How can I write a NOT Equal condition in Waterline?

This code:

Users
  .count()
  .where({
     id: { '!=': req.params.id},
     lastname: req.body.lastname
  })

Does nothing... (disk adapter in sails.js)


Source: (StackOverflow)

Advertisements

Push values into array of mongodb database through (sails js) waterline

node js,sails js,waterline. I need to update(or push) values into the below schema after insert

I am using sailsjs with waterline and mongodb.

{
"countries": {
"states": [
{
"statename": "state",
"districts": [
{
"distname": "district",
"cities": [
{
"cityname": "Hyderabad",
"places": [
                {
                  "placename": "hitechcity"
                }
              ]

          }
        ]
      }
    ]
  }
]
}
}

I need to know how to update it i need something like this after update

{
"countries": {
"states": [
{
"statename": "state",
"districts": [
{
"distname": "district",
"cities": [
{
"cityname": "Hyderabad",

              "places": [
                {
                  "placename": "hitechcity"
                },
                {
                  "placename": "someother place"
                }
              ]

          }
        ]
      }
    ]
  }
]
}
}

please someone help me.


Source: (StackOverflow)

Sails.js Waterline query modifiers for dates with sails-mysql?

I just started using Sails.js with its ORM, Waterline, and absolutely love it, but I am not sure how to use query modifiers for dates. I am using sails-mysql. Specifically, I am trying to get rows that have a datetime field between two specific dates. I have tried doing this:

MyModel.find()
    .where({ datetime_field: { '>=': startDate } })
    .where({ datetime_field: { '<=': endDate } })
    .done(function(err, objects){
        // ...
    });

startDate and endDate are two Date objects. I have also tried converting them to strings with toString(). In both cases, I get every row from the database instead of rows between the two dates. Is there a way to do this or is this functionality not yet part of either Waterline or sails-mysql?


Source: (StackOverflow)

How to know which attribute called the waterline validation rule?

I'm doing my own custom validations on certain fields, so that only certain values are accepted (depending on the field) and the rest rejected. I would like to write a "filter" function that checks what attribute called the validation and from there decide what words the attribute is allowed to use. So the model would look something like this:

module.exports = {

    types: {

        filter: function(attribute) {

            if (attribute === 'number') {
                switch(attribute.value) {

                    case 'one':
                        return true;

                    case 'two':
                        return true;

                    default:
                        return false;

                }
            } else if (attribute === 'color') {
                switch(attribute.value) {

                    case 'red':
                        return true;

                    case 'blue':
                        return true;

                    default:
                        return false;

                }
           }

        },


    },

    attributes: {

        number: {
            type: 'string',
            required: true,
            filter: true
        },

        color: {
            type: 'string',
            required: true,
            filter: true
        }
    }
};

Of course, in normal Sails.js behaviour, "attribute" would not be the attribute, but the value of the attribute. (And attribute.value was just an example, meaning, I want the attribute value in there).

So, I want attribute to be the actual attribute that called the validation rule. Is this possible with Sails? I mean, I could write a function for each field in the model, but it would be nice to have a function that fits them all (I have many of them).

Thanks.


Source: (StackOverflow)

How to define instance methods for models with sails.js

How can I define functions/instance method for objects in Sails ?

In Waterline doc (https://github.com/balderdashy/waterline) they say:

var User = Waterline.Collection.extend({
...
  attributes: {
    ...
    // You can also define instance methods here
    fullName: function() {
      return this.firstName + ' ' + this.lastName
    }
  },
}

But when I try do define an instance method in attributes in a model in Sails, the function is not added to the object. Am I doing something wrong ?

Environment: Sails (v0.8.94), Node (v0.8.16)


Source: (StackOverflow)

How to perform a "$in" query with Waterline and MongoDB

I am trying to do a "$in" query with waterline I have an Array and I want to get a list of document with ids that are in the array. I don't know how to do that.

I tried:

User.find()
.where({id : {in : array}})
done(...)

But it doesn't seem to work as expected.

Any way I can do that?


Source: (StackOverflow)

How do I handle a Unique Field in sails?

I've defined a unique field in my model but when I tried to test it seems like it's not being checked by sails because I get a Error (E_UNKNOWN) :: Encountered an unexpected error: MongoError: E11000 duplicate key error index: instead a sails ValidationError.

What is the best way to handle a unique field in sails?

// model/User.js
module.exports{
attributes: {
  email: {required: true, unique: true, type: 'email' },
  ....
}
// in my controller
User.create({email: 'hello@gmail.com'}).then(...).fail(....)
User.create({email: 'hello@gmail.com'}).then(...).fail(// throws the mongo error ) 
// and same goes with update it throws error

Thanks in advance guys.


Source: (StackOverflow)

Sails.Js - How I do pagination in sails.Js

I want to create paginated table using sails.js, mongodb and waterline-ORM.

Is there a any specific way to do pagination in sails.js?


Source: (StackOverflow)

How to create a new instance of waterline model without saving it

This might be a very simple question but I don't find anything about it in the Waterline docs.

How is it possible to get an instance of a Waterline model without immediately saving it.

Model.create(data); // already written to database

I am looking for something like

var user = User.new(data); // new instance, not persistent so far
user.doSomething(withThis); // call method on instance
user.save(); // Now write it to database

Thanks


Source: (StackOverflow)

how to use the attribute type like 'array' and 'json' in sails

Sails have support very convenient model througth Waterline, and I have used the 'array' attribute type in the way storing many string, but now I want to store more complex object, Although I can store the raw data in mongo by 'array' type, I don't know if it is safe and I want define the object type in the array, like mongoose's style. for example, I need a model "Products" and I want define it as a 'array' but the object stored in the array only model "Book", what I can do like this, but I don't think it works.

module.exports = {
  products : {
    type : 'array',
    Book : {
      name : 'string',
      price : 'integer'
    }
  }
}

So, any suggestion about use of the 'array' and 'json' is very appreciated, thanks so much!


Source: (StackOverflow)

Transactional SQL with Sails.js

So I have been playing with NodeJS/Express for a little with now and I would really like to try to rewrite a relatively large side project using a full JavaScript stack just to see how it will work. Sails.js seems to be a pretty good choice for a NodeJS backend for a REST API with support for web sockets which is exactly what I am looking for however is one more issue I am looking to resolve and that is transactional SQL within NodeJS.

Most data layer/orms I have seen on the NodeJS side of things don't seem to support transactions when dealing with MySQL. The ORM provided with Sails.js (Waterline) also does not seem to support transactions which is weird because I have seen places where is mentioned it did though those comments are quite old. Knex.js has support for transactions so I was wondering if it is easy to replace the ORM is Sails.js with this (or if Sails.js assumes Waterline in the core framework).

I was also wondering if there is an ORM built on top of Knex.js besides Bookshelf as I am not a fan of Backbones Model/Collection system?


Source: (StackOverflow)

Is it possible to rename `createdAt` and `updatedAt` in sails.js / waterline

Using Waterline ORM from SailsJS, my defaults for autoCreatedAt and autoUpdatedAt are set to false, but I still need to implement to the functionality just using different field names (DBA request). Is there a way to either:

  1. specify different column names for the automatically generated field, or
  2. manually emulate the same behave in an attribute definition, or
  3. can I just leave custom fields in the schema like created_ts and updated_ts to be updated with triggers in the DB schema itself (but I still need Waterline to read them)?

Thanks in advance.


Source: (StackOverflow)

npm install not installing latest version on GitHub

I have a module called 'sails-mongo' and I want to update it to the newest version using the following command:

npm update sails-mongo --save

I also tried uninstall then install again. I tried sails-mongo@latest and sails-mongo@beta.

Problem: The current version (master) on GitHub the package.json (https://github.com/balderdashy/sails-mongo/blob/master/package.json) file has:

"dependencies": {
  "async": "~0.2.9",
  "lodash": "~2.4.1",
  "mongodb": "1.4.2",
  "waterline-errors": "~0.10.0"
},

And in the one being updated

"dependencies": {
  "async": "0.2.10",
  "underscore": "1.5.2",
  "underscore.string": "2.3.3",
  "mongodb": "~1.3.23"
},

The only way I get the master branch is using the command npm install git+https://github.com/balderdashy/sails-mongo

Why doesn't sails-mongo@latest install the master branch?


Source: (StackOverflow)

How to selectively populate waterline associations via query param in sails.js

By default, sails will populate all relationships within a model when it's corresponding API route is hit. Does anyone know if it's possible to toggle this functionality? If I'm working with a one-to-many association, I may not want to populate the association when doing a listing of all items for performance reasons. But when viewing a single item, it would be nice to complete the population.

For example, say one ticket can have many comments. I don't care about the comments when fetching a case listing but would be important when viewing a specific case. I took a guess at how it could function but it fails:

localhost:1337/tickets?populate=false

Update

I implemented the above functionality within balderdashy/sails#1695. The only change is that you selectively choose which associations to populate using:

localhost:1337/tickets?populate=[]          // Don't populate anything
localhost:1337/tickets?populate=[comments]  // Only populate comments

This would override whatever is defined for populate within your blueprint config.


Source: (StackOverflow)