@jitl/notion-api
Version:
The missing companion library for the official Notion public API.
54 lines • 2.5 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const __1 = require("..");
const notion_api_1 = require("../lib/notion-api");
const query_1 = require("../lib/query");
const exampleHelpers_1 = require("./exampleHelpers");
console.log('database schema');
(0, exampleHelpers_1.runExample)(module, 'Database schemas', async ({ notion, database_id, page_id }) => {
const mySchema = (0, __1.inferDatabaseSchema)({
Title: { type: 'title' },
SubTitle: { type: 'rich_text', name: 'Subtitle' },
PublishedDate: { type: 'date', name: 'Published Date' },
IsPublished: {
type: 'checkbox',
name: 'Show In Production',
id: 'asdf123',
},
});
// inferDatabaseSchema infers a concrete type with the same shape as the input,
// so you can reference properties easily. It also adds `name` to each [[PropertySchema]]
// based on the key name.
console.log(mySchema.Title.name); // "Title"
// You can use the properties in the inferred schema to access the corresponding
// property value on a Page.
for await (const page of (0, __1.iteratePaginatedAPI)(notion.databases.query, {
database_id,
})) {
if ((0, __1.isFullPage)(page)) {
const titleRichText = (0, __1.getPropertyValue)(page, mySchema.Title);
console.log('Title: ', (0, __1.richTextAsPlainText)(titleRichText));
const isPublished = (0, __1.getPropertyValue)(page, mySchema.IsPublished);
console.log('Is published: ', isPublished);
}
}
// Print schema differences between our literal and the API.
const database = await notion.databases.retrieve({ database_id });
const diffs = (0, __1.diffDatabaseSchemas)({ before: mySchema, after: database.properties });
for (const change of diffs) {
console.log((0, __1.databaseSchemaDiffToString)(change, { beforeName: 'mySchema', afterName: 'API database' }));
}
// Sketch
const db = (0, query_1.databaseFilterBuilder)(mySchema);
notion.databases.query({
database_id,
filter: db.or(db.IsPublished.equals(true), db.PublishedDate.after('2020-01-01')),
});
db.IsPublished.schema.id;
const page = await notion.pages.retrieve({ page_id });
if ((0, __1.isFullPage)(page)) {
const props = (0, notion_api_1.getAllProperties)(page, db.schema);
console.log(props.Title);
}
});
//# sourceMappingURL=databaseSchema.js.map
;