waterline

sails.js + waterline One-To-Many model association, what should happen when deleting the Many?

社会主义新天地 提交于 2019-12-01 02:41:29
问题 I have a one to many relation between Teacher(One) and Children(Many). If I do: Teacher.destroy(teacherId).exec(function(err){}); The children are not automatically removed. Is it a bug or should I delete them manually? If that's not a bug, what is the explanation for not deleting children? 回答1: Waterline currently doesn't support cascading deletes. It may be a configuration option in future versions, but it will probably never be the default. In most production-ready apps you probably should

sails.js - how can I acess session data in Model hook beforeCreate

匆匆过客 提交于 2019-11-30 21:02:27
I want to update a field 'owner' of a Model. The owner needs to be fetched from the session which contains the user who is currently logged in and is creating the Model. I want something like this: Model = { attributes: { }, beforeCreate(values,next) { var owner_user_id = req.session.user_id; values.owner = owner_user_id; next(); } } Are you sure a lifecycle callback is the right place to do it? Because it's really not. What if tomorrow you'll need to use your model in a CLI task or something else sessionless? Besides, with the associations API coming, there will be, probably, a more elegant

Sails.js Model: create 2 association to self failed

僤鯓⒐⒋嵵緔 提交于 2019-11-30 20:27:14
I'm pretty new on Nodejs and sails. I'm implementing a server which is similiar to Twitter. In user model, there should be 2 fields: follower and following, and the 2 fields are association of the model 'user' itself. My question is when the model have only 1 association, either follower or following, it works. However, when both follower and following included, there would be en error. The code is something like this: module.exports = { attributes: { alias: { type:'string', required: true, primaryKey: true }, pwd: { type: 'string', required: true }, follower: { collection: 'user', via: 'alias

I am confused with Sails.js waterline one-to-one association logic

风格不统一 提交于 2019-11-30 15:39:19
So the reason why im confused, because I am a PHP developer and used Laravel and FuelPHP alot What i dont really understand is the association it self. What i mean, i wanted to create a basic hasOne / BelongsTo logic, with the following User has one profile Profile belongs to an user I am used to the following build up (Laravel style) Users table id | username | email | password --------------------------------------- 1 | My Username | My email | 1234568 Users_profile table user_id | first_name | last_name ---------------------------------------- 1 | My First name | My Last name Then i just

Specify returned fields in Node.js / Waterline?

自闭症网瘾萝莉.ら 提交于 2019-11-30 13:43:57
I want to make a request like: User.find().exec(function(){}); I know I can use toJSON in the model however I don't like this approach since sometimes I need different parameters. For instance if it's the logged in user I will return their email and other parameters. However if it the request fort he same data is made by a different user it would not include the email and a smaller subset of parameters. I've also tried using: User.find({}, {username:1}) ... User.find({}, {fields: {username:1}}); But not having any luck. How can I specify the fields I need returned? So actually found a weird

Best way to migrate table changes to production sailsjs tables

走远了吗. 提交于 2019-11-30 09:26:15
I just lost 11,000 records from my database just running the command for sailsjs without the --prod part in it, So I thought I should ask whats the best way to change the tables on production server when the Model.js has been changed ? Thanks Automated migration should never be done in production. This a common-sense practice that applies to any production system with important data. There are a few solutions available for migrating a sails.js database. sails-db-migrate : db-migrate integration for sails.js db-migrate integration for Sails.js. This is a fairly simple wrapper, which provides

Inherit attributes and lifecycle functions of Sails.js models

狂风中的少年 提交于 2019-11-30 08:34:41
问题 I want to create a custom set of attributes and lifecycle methods that are shared between all my Sails.js models. Sails.js automatically creates and registers the model objects by calling the Waterline.Collection.extend() method and providing the model definition found inside the /api/models directory. What would be the best way to extend my model definition from a parent? I already tried using _.extend(sails.config.model.parentModel, childModel) but sadly the sails object is not exposed

Change field name for CreatedAt / UpdateAt Attributes

拜拜、爱过 提交于 2019-11-30 06:48:01
I am attempting to model an existing MSSQL table in SailsJS. Unsurprisingly the tables in the existing database have a createdAt and updatedAt column similar to what is generated by the SailsJS framework. Is there a way to assign the value of the property generated by the SailsJS framework to an attribute that I have defined? For example: attributes: { ... creationDate:{ columnName:'cre_dt', type:'datetime', defaultsTo: this.createdAt } ... } sgress454 No, but you can turn off the auto-generated property entirely and use your own: autoCreatedAt: false, autoUpdatedAt: false, attributes: {

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

喜欢而已 提交于 2019-11-30 06:40:46
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

sails.js - how can I acess session data in Model hook beforeCreate

爷,独闯天下 提交于 2019-11-30 05:32:32
问题 I want to update a field 'owner' of a Model. The owner needs to be fetched from the session which contains the user who is currently logged in and is creating the Model. I want something like this: Model = { attributes: { }, beforeCreate(values,next) { var owner_user_id = req.session.user_id; values.owner = owner_user_id; next(); } } 回答1: Are you sure a lifecycle callback is the right place to do it? Because it's really not. What if tomorrow you'll need to use your model in a CLI task or