waterline

Service that returns data from an asynchronous method

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 12:45:37
I am using Sails' ORM (Waterline). I have written a geturl service that should return the url of several models/actions in my app. I am currently calling this service inside my templates. (As I am alone to develop this, don't hesitate to warn me if this design pattern is wrong) Now it occurs that Waterline's .find() method is asynchronous (as it should). I always use callbacks to do things when inserting or fetching things in database. Now I have seen everywhere that I cannot return any data from asynchronous methods. As a consequence I am puzzled because I want to create this [damned] service

How can I select different databases for a model based on request parameters in Sails.js?

孤人 提交于 2019-12-06 11:39:35
问题 I have an application that I am considering porting to Sails.JS. One of the big issues I am having is supporting the per-request database selection that we have right now. Our app creates/uses a different database for each customer that we have and we select the database based on the request. How could I achieve that in Sails.JS? Could I write a Express middleware that would do this on a per request basis? 来源: https://stackoverflow.com/questions/26930432/how-can-i-select-different-databases

sails.js nested models

限于喜欢 提交于 2019-12-06 05:19:17
问题 In sails.js 0.10 I'm trying to do the following // user.js module.exports = { attributes: { uuid: { type: 'string', primaryKey: true, required: true } , profile: { firstname: 'string', lastname: 'string', birthdate: 'date', required: true } } }; I'm getting an error when trying to create a user and sailsJS doesn't recognize the "profile" attribute. I'm not sure if sails supports the nested JSON structure, and if it does I'm not sure how to structure it. error: Sent 500 ("Server Error")

How to extract distinct values from a mongo database using Waterline and Sails.js (version 0.10)?

好久不见. 提交于 2019-12-06 04:25:50
Using Sails.js version 0.10.x , assume I have a model Dog , populated as follows (writtenout in yaml format for convenience, but in my case it's actually in a mongo database.) dogs: - breed: "wolf" name: "Fido" - breed: "wolf" name: "Roger" - breed: "dingo" name: "Nipper" - breed: "dingo" name: "Ernie" - breed: "corgi" name: "Bernadi" - breed: "corgi" name: "Queenie" - breed: "poodle" name: "Christopher" - breed: "poodle" name: "Tony" etc So now I want to create a list of the available breeds. Dog.find().exec(err, dogs) { breeds = []; dogs.each(function(dog)) { if (breeds.indexOf(dog.breed) ==

How to access request object in sails.js lifecycle callbacks?

让人想犯罪 __ 提交于 2019-12-06 02:47:31
问题 Suppose I have this model: module.exports = { attributes: { title: { type: 'string', required: true }, content: { type: 'string', required: true }, createdBy: { type: 'string', required: true } } } I need to set the current user id to the model's createdBy attribute. I thought I could do that with the beforeValidate lifecycle callback, but I can't access the request object where the current user is stored. Is there a way to access it, or should I solve this somehow else? I tried this with no

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

本小妞迷上赌 提交于 2019-12-05 15:09:10
问题 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: specify different column names for the automatically generated field, or manually emulate the same behave in an attribute definition, or 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

Using sails.js with an existing postgres database

筅森魡賤 提交于 2019-12-05 14:48:38
问题 I was looking at using Sails for an app that we are developing. I'm using the sails-postgresql adapter which uses the waterline orm. I have an existing database that I want to connect to. If I create a model using generate something and then in my model I have attributes:{ title:{type:'String'} } If I browse to localhost/something the orm deletes all the columns in the something table except title. Is there a way to stop it from doing this? This app should not delete columns on this database.

Can't find records in Waterline by date/time

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 13:28:29
how compare a datetime in sails.js Model? here it is what i did but without luck. var _date = moment().format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z'; Game.find({ where:{ active: true, start: { '>=' : _date } }, limit: 1, sort: 'start asc' }, function(err, game) { console.log('ERROR: ' + err); console.log('Game OBJ' + game.toString()); }); The datetime type is transformed into an actual Javascript date in Waterline (the Sails ORM). So it needs to be compared against a date object, not a string. You can keep your code exactly as it is, but change the first line to: var _date = new Date(moment().format

Sails.js + Waterline: Many-to-Many through association

强颜欢笑 提交于 2019-12-05 06:21:13
I'm new to Sails.js (v0.10.5) and Waterline ORM. I have 3 tables in database: users (id, name), roles(id, alias) and join table users_roles(user_id, role_id). It's important not to change table names and field names in database. I want Policy entity to be a join entity between User and Role. Here is some mapping code: //User.js module.exports = { tableName: 'users', autoCreatedAt: false, autoUpdatedAt: false, attributes: { id: { type: 'integer', required: true }, name: { type: 'string' }, roles: { collection: 'role', via: 'users', through: 'policy' }, } } //Role.js module.exports = { tableName

How can I add an instance method to all Models in sails.js?

好久不见. 提交于 2019-12-05 01:31:41
I'd like to add a default toDisplay function to all models which will use metadata, not unlike attribute/association definitions, to perform manipulations on the instance's attributes/associations making them suitable for display in the UI. for example: Foo.findOne(someId) .exec(function(err, foo) { ... res.view({ foo: foo.toDisplay(), }); }); So, I'd like to add this function too all models. I can imagine a Model.prototype.toDisplay = ... solution, but I'm not sure where to get Model from (some long require('waterline/..../model') path?), and if I had Model, where to put that snip-it. Please