0

I am new to SailsJS and using it for an application and facing some issues with queries while deleting a Project Model
Project Model:

name: {
        type: 'string',
        required: true
      },
deletedAt: {
             type: 'datetime'
           },  
deletedBy: {
        model: 'User',
    }  

User Model:

name: {
        type: 'string',
        required: true
      },
email: {
        type: 'string',
        required: true
      }  

On creating a project, Sails automatically fills in data for createdAt and updatedAt and I am filling in data for Project Name
On deleting a project, I need to do a soft-delete, where In I will be setting deletedAt value and deletedBy values.
On getting list of all projects, It should not get deleted projects.
One way I thought of was to get all projects using query:

Project.find(function (err, projList) {
..
}  

and then for each project do a check as follows:

if (project.hasOwnProperty('deletedBy') && project.deletedBy != null) {
   //Dont include this in list 
  }  

Is there any way, Instead of I manually checking for deleted projects, Can it be done using queries? i.e. Omitting all deleted projects.
Thanks in Advance.

Abhishek
  • 1,999
  • 5
  • 26
  • 52
  • Is your questions complete? You list the user model here, but your question does not ask anything about the user model. – Meeker Nov 07 '14 at 22:40

1 Answers1

1
Project
    .find()
    .where({deletedAt : null})
    .exec(function(err, projectList){
..
});

Depending on your model / db you may have to setup deletedAt with defaultsTo: null

name: {
    type: 'string',
    required: true
},
deletedAt: {
    type: 'datetime',
    defaultsTo: null,
},  
deletedBy: {
    model: 'User',
} 
Meeker
  • 5,979
  • 2
  • 20
  • 38