-
What is is best way to create create an auto generate filed in Collections like updated so each time a content items it updated it will update this field updated: last_edit_time can anyone give some hint. thanks. |
Beta Was this translation helpful? Give feedback.
Answered by
zce
Aug 4, 2024
Replies: 1 comment 3 replies
-
new recipe in docs: https://velite.js.org/guide/last-modified Last Modified SchemaBased on file statconst timestamp = () =>
s.custom<string | undefined>(i => i === undefined || typeof i === 'string').transform<string>(async (value, { meta, addIssue }) => {
if (value != null) {
addIssue({ fatal: false, code: 'custom', message: '`s.timestamp()` schema will resolve the file modified timestamp' })
}
const stats = await stat(meta.path)
return stats.mtime.toISOString()
})
// use it in your schema
const posts = defineCollection({
// ...
schema: {
// ...
lastModified: timestamp()
}
}) Based on git timestampconst execAsync = promisify(exec)
const timestamp = () =>
s.custom<string | undefined>(i => i === undefined || typeof i === 'string').transform<string>(async (value, { meta, addIssue }) => {
if (value != null) {
addIssue({ fatal: false, code: 'custom', message: '`s.timestamp()` schema will resolve the value from `git log -1 --format=%cd`' })
}
const { stdout } = await execAsync(`git log -1 --format=%cd ${meta.path}`)
return new Date(stdout).toISOString()
})
// use it in your schema
const posts = defineCollection({
// ...
schema: {
// ...
lastModified: timestamp()
}
}) |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
StevePhuc
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
new recipe in docs: https://velite.js.org/guide/last-modified
Last Modified Schema
Based on file stat
Based on git timestamp