Skip to main content

notion.notion.createdatabase

Home > @runlightyear/notion > Notion > createDatabase

Notion.createDatabase() method

Create a database

Signature:
createDatabase(props: CreateDatabaseProps): Promise<CreateDatabaseResponse>;

Parameters

ParameterTypeDescription
propsCreateDatabaseProps
Returns:

Promise<CreateDatabaseResponse>

Example 1

Create a database

import { defineAction } from "@runlightyear/lightyear";
import { Notion } from "@runlightyear/notion";

defineAction({
name: "createDatabase",
title: "Create Database",
apps: ["notion"],
variables: ["parentPageId"],
run: async ({ auths, variables }) => {
const notion = new Notion({
auth: auths.notion,
});
const result = await notion.createDatabase({
parent: {
pageId: variables.parentPageId!,
},
title: [
{
text: {
content: "Grocery List",
},
},
],
properties: {
Name: {
title: {},
},
Description: {
richText: {},
},
"In Stock": {
checkbox: {},
},
"Food Group": {
select: {
options: [
{
name: "🥦 Vegetable",
color: "green",
},
{
name: "🍎 Fruit",
color: "red",
},
{
name: "🍞 Carbs",
color: "yellow",
},
],
},
},
Price: {
number: {
format: "dollar",
},
},
"Last Ordered": {
date: {},
},
"Store Availability": {
multiSelect: {
options: [
{
name: "Duc Loi Market",
color: "blue",
},
{
name: "Rainbow Grocery",
color: "gray",
},
{
name: "Nijiya Market",
color: "purple",
},
{
name: "Gus's Community Market",
color: "yellow",
},
],
},
},
"+1": {
people: {},
},
Photo: {
files: {},
},
},
});
console.log("Database: ", result.data);
},
});

Example 2

Create a database with items

import { defineAction } from "@runlightyear/lightyear";
import { Notion } from "@runlightyear/notion";

defineAction({
name: "createDatabaseItems",
title: "Create Database Items",
apps: ["notion"],
variables: ["parentPageId"],
run: async ({ auths, variables }) => {
const notion = new Notion({ auth: auths.notion });
const response = await notion.createDatabase({
parent: {
pageId: variables.parentPageId!,
},
title: [
{
text: {
content: "Shopping List",
},
},
],
properties: {
Name: {
title: {},
},
Description: {
richText: {},
},
Quantity: {
number: {
format: "number",
},
},
},
});

const newDatabaseId = response.data.id;

await notion.createPage({
parent: {
databaseId: newDatabaseId,
},
properties: {
Name: {
title: [
{
text: {
content: "🥦 Broccoli",
},
},
],
},
Description: {
richText: [
{
text: {
content: "A green vegetable",
},
},
],
},
Quantity: {
number: 1,
},
},
});

await notion.createPage({
parent: {
databaseId: newDatabaseId,
},
properties: {
Name: {
title: [
{
text: {
content: "🍎 Apple",
},
},
],
},
Description: {
richText: [
{
text: {
content: "A red fruit",
},
},
],
},
Quantity: {
number: 2,
},
},
});

await notion.createPage({
parent: {
databaseId: newDatabaseId,
},
properties: {
Name: {
title: [
{
text: {
content: "🍞 Bread",
},
},
],
},
Description: {
richText: [
{
text: {
content: "A yellow carb",
},
},
],
},
Quantity: {
number: 3,
},
},
});
},
});