defineDatabase defines how Contentlayer should generate your schema for one particular database.

Database definitions are used within the options for makeSource, which is how the definition is passed onto Contentlayer.

Usage

import { defineDatabase } from 'contentlayer-source-notion'

const Post = defineDatabase(() => ({
  name: 'Post',
  databaseId: '<database_id>',
}))

Options

name

boolean (required)

Name of the document. This defines the types and functions that are generated for documents of this type.

const Doc = defineDatabase(() => ({
  name: "Doc",
  // ...
})

In the usage example, the name was Doc, which would generate a data object allDocs representing the collection of pages of this database (see databaseId). And it would add a type definition for Doc, representing the shape of the data object created from your content.

databaseId

string (required)

The ID of the database used to generate the schema.

You can find the ID of the database in the URL:
notion.so/my-org/fe26b972ec3f4b32a1882230915fe111

properties

Options to configure how properties should be used to generate the data shape for the document type. See the properties types reference for more information

computedFields

Computed fields can be calculated on the fly rather than being read directly from the content source.

Here's an example that transforms the page title to a slug.

export const Post = defineDatabase(() => ({
  // ...
  computedFields: {
    slug: {
      type: 'string',
      resolve: (doc) => slugify(doc.title),
    },
  },
}))

description

string

Provides a description for the generated type definition.

automaticImport

boolean

The Notion source automatically import all properties of your database.

If thoses properties can contain sensitive information you can set this option to true and only properties defined in properties will be in your schema.

importContent

boolean

Notion requires a request for each pages to query their content. (That can lead to other requests)

As Notion can be slow, you can disable this feature if you only use the page properties.

query

This option allows you to sort and filter your database.

Example:

In this example only published posts will be queried ordered by name.

const Post = defineDatabase(() => ({
  name: 'Post',
  databaseId: '<database_id>',
  query: {
    filter: {
      property: 'Status',
      status: {
        equals: 'Published',
      },
    },
    sort: [
      {
        property: 'Name',
        direction: 'ascending',
      },
    ],
  },
}))

You can find more information on the Notion API Documentation.


Was this article helpful to you?
Provide feedback

Last edited on April 01, 2024.
Edit this page