Editing content

    Your CMS instance has methods to create and update content. During development these will update the file system directly, while in production the changes will result in a new git commit.

    Creating Entries

    New Entries can be created using the create function.

    import {Edit} from 'alinea'
    
    // Start a transaction to create a new entry of type BlogPost
    const post = Edit.create(BlogPost).set({
      title: 'A new blog post',
      body: 'Hello world'
    })
    
    // The new entry ID can be read before comitting
    console.log(`Creating post with id: ${post.id}`)
    
    // Save the changes
    await cms.commit(post)

    Creating child Entries

    To nest new Entries correctly it's possible to set the parent id, or construct directly from a parent update.

    import {Edit} from 'alinea'
    
    const blog = Edit.create(Blog).set({title: 'Blog'})
    const posts = postData.map(data =>
      blog.createChild(BlogPost).set({title: data.title})
    )
    // Or, if you're adding to an existing parent
    // blog.create(BlogPost).setParent(blogId).set({title: data.title})
    await cms.commit(blog, ...posts)

    Update Fields

    Entry fields can be edited using the update function. Optionally pass the entry Type as a second argument so the field values are typed correctly.

    import {Edit, Query} from 'alinea'
    
    // Select the first blog post
    const blogPostId = await cms.get(Query(BlogPost).select(Query.id))
    
    // Edit a field and save
    const update = Edit.update(blogPostId, BlogPost).set({
      body: 'New body text'
    })
    
    await cms.commit(update)

    Constructing field values

    Some fields contain values that are more complex than a string. The Edit namespace contains helper functions to construct these. In this example we construct the value of a List Field.

    const richTextField = richText('Item body text');
    const listField = list('My list field', {
      schema: {
        Text: type('Text', {
          title: text('Item title'),
          text: richText,
        })
      }
    })
    const rowText = Edit.richText(richTextField)
      .addHtml(`
        <h1>Main heading</h1>
        <p>A rich text value parsed from HTML.</p>
      `)
      .value()
    const listValue = Edit.list(listField)
      .add('Text', {
        title: 'The row title',
        text: rowText,
      })
      .value()
    const update = Edit.update(entryId, TypeWithList).set({
      list: listValue
    })

    File uploads

    Files can be uploaded using the upload function.

    import {Edit} from 'alinea'
    
    const file = new File(['content'], 'test.txt')
    const upload = Edit.upload(file)
    
    // The new entry ID can be read before comitting
    console.log(`Creating post with id: ${upload.id}`)
    
    // Upload file and save file metadata
    await cms.commit(upload)

    Creating image previews

    Alinea can create all the metadata for images (such as previews) by passing a createPreview function. On the server this will use the sharp package to read image data. The package will need to be installed separately.

    import {Edit} from 'alinea'
    import {createPreview} from 'alinea/core/media/CreatePreview'
    import fs from 'node:fs'
    
    const file = new File([
      fs.readFileSync('./test.png')
    ], 'test.png')
    const upload = Edit.upload(file, {createPreview})
    await cms.commit(upload)