当我为模型设置 ttl 时,它无法在环回中工作
ttl not working in loopback when I set it for a Model
我正在尝试为环回模型设置 ttl
,以便在指定时间后自动删除文档。
这是我添加的属性:
"ttl": {
"type": "number",
"required": true
}
这不是 AccessToken
模型,而是一个单独的模型,我想在指定的时间间隔后删除其文档。
AccessTokens
在 ttl
启动后不会被删除,它们只是为了登录目的使令牌无效。 我不确定任何 database/ORM 是否会在它们存在一段时间后删除行 我错了 mongodb 这样做,然而环回实际上并没有使用这个特性。此脚本将创建一个作业,根据 ttl
列删除所有已过期的行。
server/boot/job-delete-expired.js
module.exports = (server) => {
const myModel = server.models.myModel;
if (!myModel) {
throw new Error("My model not found!");
}
const deleteExpiredModels = async () => {
const now = new Date();
const all = await myModel.find();
// If the created time + the ttl is paste now then it can be deleted
const expired = all.filter(m => (m.created + m.ttl) > now);
// Delete them all
await Promise.all(expired.map(e => myModel.destroyById(e.id)));
};
// Execute this every 10 minutes
setInterval(() => deleteExpiredModels(), 60000)
};
免责声明:此代码没有错误处理,并且 setInterval 不会等待承诺解决,如果您在生产中使用它,请考虑使用 async/await 的 while 循环以确保只有一个实例deleteExpiredModels
曾经执行过。
我能够按如下方式解决这个问题:
MyCollection.getDataSource().connector.connect(function(err, db) {
if(!err){
var collection = db.collection('MyCollection');
collection.createIndex( { "expireAt": 1 }, { expireAfterSeconds: 0 } );
}
});
然后对于每个文档,我插入 expireAt
对应于文档应该过期的时间。
MongoDB 在文档的 expireAt
时间自动从集合中删除文档。
我使用 model.json 文件解决了这个问题
"indexes":{
"expireAt_1":{
"keys": {"createdOn": 1},
"options":{"expireAfterSeconds": 2592000}
}
}
我为索引使用了一个名称。
定义的键具有具有日期值的对象 属性。
需要在选项中设置 expireAfterSeconds 值 property.In 这种情况我将其设置为 createdOn date
后 30 天
我正在尝试为环回模型设置 ttl
,以便在指定时间后自动删除文档。
这是我添加的属性:
"ttl": {
"type": "number",
"required": true
}
这不是 AccessToken
模型,而是一个单独的模型,我想在指定的时间间隔后删除其文档。
AccessTokens
在 ttl
启动后不会被删除,它们只是为了登录目的使令牌无效。 我不确定任何 database/ORM 是否会在它们存在一段时间后删除行 我错了 mongodb 这样做,然而环回实际上并没有使用这个特性。此脚本将创建一个作业,根据 ttl
列删除所有已过期的行。
server/boot/job-delete-expired.js
module.exports = (server) => {
const myModel = server.models.myModel;
if (!myModel) {
throw new Error("My model not found!");
}
const deleteExpiredModels = async () => {
const now = new Date();
const all = await myModel.find();
// If the created time + the ttl is paste now then it can be deleted
const expired = all.filter(m => (m.created + m.ttl) > now);
// Delete them all
await Promise.all(expired.map(e => myModel.destroyById(e.id)));
};
// Execute this every 10 minutes
setInterval(() => deleteExpiredModels(), 60000)
};
免责声明:此代码没有错误处理,并且 setInterval 不会等待承诺解决,如果您在生产中使用它,请考虑使用 async/await 的 while 循环以确保只有一个实例deleteExpiredModels
曾经执行过。
我能够按如下方式解决这个问题:
MyCollection.getDataSource().connector.connect(function(err, db) {
if(!err){
var collection = db.collection('MyCollection');
collection.createIndex( { "expireAt": 1 }, { expireAfterSeconds: 0 } );
}
});
然后对于每个文档,我插入 expireAt
对应于文档应该过期的时间。
MongoDB 在文档的 expireAt
时间自动从集合中删除文档。
我使用 model.json 文件解决了这个问题
"indexes":{
"expireAt_1":{
"keys": {"createdOn": 1},
"options":{"expireAfterSeconds": 2592000}
}
}
我为索引使用了一个名称。 定义的键具有具有日期值的对象 属性。 需要在选项中设置 expireAfterSeconds 值 property.In 这种情况我将其设置为 createdOn date
后 30 天