Skip to content

create an auto generate filed in Collections like updated: last_edit_time when a item is updated #199

Answered by zce
StevePhuc asked this question in Q&A
Discussion options

You must be logged in to vote

new recipe in docs: https://velite.js.org/guide/last-modified

Last Modified Schema

Based on file stat

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 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 timestamp

const execAsync = promisify(exec)

const times…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@StevePhuc
Comment options

@mupinnn
Comment options

@zce
Comment options

zce Aug 27, 2024
Maintainer

Answer selected by StevePhuc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants