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({
      type: 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 children

    Set the _parentId property to nest children under a parent.

    import {Edit} from 'alinea'
    
    const blog = Edit.create({
      type: Blog,
      set: {title: 'Blog'}
    })
    await cms.commit(blog)
    
    const posts = postData.map(data =>
      Edit.create({
        type: BlogPost,
        parentId: blog.id, 
        set: {
          title: data.title
        }
      })
    )
    await cms.commit(...posts)

    Update Fields

    Entry fields can be edited using the update function. Optionally pass the entry Type if you want to update its fields.

    import {Edit, Query} from 'alinea'
    
    // Select the first blog post
    const post = await cms.get({
      type: BlogPost
    })
    
    // Edit a field and save
    const update = Edit.update({
      id: post._id,
      type: 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({
      id: entryId,
      type: 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)