让sailsJS自动更新createdAt和updatedAt
Let sailsJS auto update createdAt and updatedAt
我正在尝试建立一个具有 createdAt 和 updatedAt 列的模型,并希望 sails 在条目更改时自动更新这些字段。目前我无法让它工作。试图查看文档,但他们没有提供有关如何设置的 "guide"。
我的文件如下:
Users.js(型号)
module.exports = {
tableName: 'users',
primaryKey: 'id',
attributes: {
id: {
type:'number',
unique:true,
autoIncrement:true
},
name: {
type:'string'
},
email: {
type:'string'
},
password: {
type:'string'
},
profilePicture: {
type:'longtext'
},
location: {
type:'string'
},
createdAt: {
type:'number'
},
updatedAt:{
type: 'number'
}
}
};
config.model.js(将迁移设置为安全,这样它不会在每次启动时清除 tables。)
migrate: 'safe',
attributes: {
createdAt: { type: 'number', autoCreatedAt: true, },
updatedAt: { type: 'number', autoUpdatedAt: true, },
id: { type: 'number', autoIncrement: true, },
},
在我的数据库 (MySQL) 中,我将 createdAt 和 updatedAt 列设置为 DATETIME。
然而,当我开始使用风帆时,table 已经存在,所以我不确定这些东西是否仅在您使用风帆创建 table 时才有效。
在 USER 模型中进行以下更改
module.exports = {
tableName: 'users',
primaryKey: 'id',
autocreatedAt : true,
autoupdatedAt : true,
attributes: {
id: {
type:'number',
unique:true,
autoIncrement:true
},
name: {
type:'string'
},
email: {
type:'string'
},
password: {
type:'string'
},
profilePicture: {
type:'longtext'
},
location: {
type:'string'
}
}
};
要将这些应用于您的所有表,您可以在 config/models.js
中修改 module.exports.models
并添加 autoUpdatedAt
和 autoCreatedAt
属性。此外,确保类型与其在数据库中的表示方式一致。在 Sails 中,createdAt
和 updatedAt
使用 datetime
类型:
module.exports.models = {
autoUpdatedAt: true,
autoCreatedAt: true,
attributes: {
createdAt: {
type: 'datetime',
}
updatedAt: {
type: 'datetime',
}
}
}
如果您决定希望列名称不同于 updatedAt
和 createdAt
的默认名称,而不是将值设置为 true
,您可以将它们设置为一个字符串值,它将是自定义列的名称。
文档:https://sailsjs.com/documentation/concepts/models-and-orm/model-settings#?autocreatedat
我正在尝试建立一个具有 createdAt 和 updatedAt 列的模型,并希望 sails 在条目更改时自动更新这些字段。目前我无法让它工作。试图查看文档,但他们没有提供有关如何设置的 "guide"。
我的文件如下:
Users.js(型号)
module.exports = {
tableName: 'users',
primaryKey: 'id',
attributes: {
id: {
type:'number',
unique:true,
autoIncrement:true
},
name: {
type:'string'
},
email: {
type:'string'
},
password: {
type:'string'
},
profilePicture: {
type:'longtext'
},
location: {
type:'string'
},
createdAt: {
type:'number'
},
updatedAt:{
type: 'number'
}
}
};
config.model.js(将迁移设置为安全,这样它不会在每次启动时清除 tables。)
migrate: 'safe',
attributes: {
createdAt: { type: 'number', autoCreatedAt: true, },
updatedAt: { type: 'number', autoUpdatedAt: true, },
id: { type: 'number', autoIncrement: true, },
},
在我的数据库 (MySQL) 中,我将 createdAt 和 updatedAt 列设置为 DATETIME。
然而,当我开始使用风帆时,table 已经存在,所以我不确定这些东西是否仅在您使用风帆创建 table 时才有效。
在 USER 模型中进行以下更改
module.exports = {
tableName: 'users',
primaryKey: 'id',
autocreatedAt : true,
autoupdatedAt : true,
attributes: {
id: {
type:'number',
unique:true,
autoIncrement:true
},
name: {
type:'string'
},
email: {
type:'string'
},
password: {
type:'string'
},
profilePicture: {
type:'longtext'
},
location: {
type:'string'
}
}
};
要将这些应用于您的所有表,您可以在 config/models.js
中修改 module.exports.models
并添加 autoUpdatedAt
和 autoCreatedAt
属性。此外,确保类型与其在数据库中的表示方式一致。在 Sails 中,createdAt
和 updatedAt
使用 datetime
类型:
module.exports.models = {
autoUpdatedAt: true,
autoCreatedAt: true,
attributes: {
createdAt: {
type: 'datetime',
}
updatedAt: {
type: 'datetime',
}
}
}
如果您决定希望列名称不同于 updatedAt
和 createdAt
的默认名称,而不是将值设置为 true
,您可以将它们设置为一个字符串值,它将是自定义列的名称。
文档:https://sailsjs.com/documentation/concepts/models-and-orm/model-settings#?autocreatedat