.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);
Argument | Type | Details | |
---|---|---|---|
1 | criteria | 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! |
Argument | Type | Details | |
---|---|---|---|
1 | err | The error that occurred, or null if there were no errors. |
|
2 | archivedRecords | 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.) |
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 } });
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`.
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 anisDeleted
flag instead, since archived records are more difficult to work with programmatically. (There is no built-in "unarchive".)