YAML

YAML operators

Introduction

Custom operators for converting YAML

Usage

Configuration options

Nabu

When using the Nabu storage engine, the YAML plugin is included by default. If you want to pass configuration options you can do it like this

const store = Nabu
  .configure({
    yaml: { indent: 4 }
  })
  .bootstrap();

Memory storage engine

If you are using the memory storage engine the YAML plugin must be configured

import Memory from '@tashmet/memory';
import mingo from '@tashmet/mingo';
import yaml from '@tashmet/yaml';

const store = Memory
  .configure({})
  .use(mingo())
  .use(yaml({ /* configuration options */}))
  .bootstrap();

Operators

$yamlToObject

{ $yamlToObject: <expression> }

Convert a YAML string to an object

const data = dedent`
  title: foo
  list:
    - item1
    - item2
`;
const pipeline: Document[] = [
  { $documents: [{ data }] },
  { $set: { data: { $yamlToObject: '$data' } } }
];

const doc = await tashmet.db('test').aggregate(pipeline).next();
{ data: { title: 'foo', list: ['item1', 'item2'] } }

To convert YAML as front matter we need to specify some extra parameters

const data = dedent`
  ---
  title: foo
  ---
  Content goes here
`;
const pipeline: Document[] = [
  { $documents: [{ data }] },
  {
    $set: {
      data: {
        $yamlToObject: {
          data: '$data',
          frontMatter: true,
          contentKey: 'body'
        }
      } 
    }
  }
];
const doc = await tashmet.db('test').aggregate(pipeline).next();
{ data: { title: 'foo', body: 'Content goes here' } }

$objectToYaml

{ $objectToYaml: <expression> }

Convert an object to a YAML string

const input = [
  { data: { foo: 'bar' } }
];
const pipeline: Document[] = [
  { $documents: input },
  { $set: { data: { $objectToYaml: '$data' } } }
];

const doc = await tashmet.db('test').aggregate(pipeline).next();

Last updated