Edit Page

.archive()

Archive ("soft-delete") records that match the specified criteria, saving them as new records in the built-in Archive model, then destroying the originals.

await Something.archive(criteria);

Usage

#
Argument Type Details
1 criteria Dictionary Records that match this Waterline criteria will be archived. Be warned, if you specify an empty dictionary ({}) as your criteria, all records will be destroyed!
Callback
#
Argument Type Details
1 err Error? The error that occurred, or null if there were no errors.
2 archivedRecords Array? of Dictionary For improved performance, the archived records are not provided to this callback by default. But if you chain .fetch(), then the recently archived records will be sent back. (Be aware that this requires an extra database query in some adapters.)

Example

#

To archive a particular user in the the database, use .archiveOne().

Or to archive multiple records in the the database:

await Pet.archive({ lastActiveAt: { '<': Date.now()-1000*60*60*24*365 } });

Accessing archived records

#

If you need to access archived records in the future, you can do so by searching the Archive model. For example, you might pass in the original record's primary key and model identity as constraints in a query.

For example, to retrieve the archive describing the user we got rid of above:

var archive = await Archive.findOne({
  fromModel: 'user',
  originalRecordId: 1
});

// The data from the original record is stored as `archive.originalRecord`.

Notes

#

This method is best used in situations where you would otherwise use .destroy(), but you still need to keep the deleted data somewhere (e.g. for compliance reasons). If you anticipate needing to access the data again in your app (e.g. if you allow un-deleting), you may want to consider using an isDeleted flag instead, since archived records are more difficult to work with programmatically. (There is no built-in "unarchive".)

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

Reference