Skip to content
8 changes: 6 additions & 2 deletions examples/data-sources/retrieve/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
const { notion, yargs } = require('../../shared');
const { log } = require('../../shared/utils');

const dataSourceId = 'acce37c4c4ee4b78aa786a944c2577cf';
const argv = yargs.default({ dataSourceId }).argv;
const dataSourceId = '45808fcd2698412a97df10e19c36cc21';
const argv = yargs.option('dataSourceId', {
alias: 'd',
describe: 'The ID of the data source to create the page in',
default: dataSourceId,
}).argv;

(async () => {
const ds = await notion.dataSources.retrieve({
Expand Down
17 changes: 13 additions & 4 deletions examples/properties/retrieve.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
/**
* Retrieve a property _value_ from a database.
* Retrieve a property _value_ from a page.
*
* Required if you want to get accurate data from Rollups via the API.
*
* Arguments:
*
* --page-id: ID of the page to fetch property from
* --prop-id: ID of the property to fetch
*/

const { notion, yargs } = require('../shared');
const { log } = require('../shared/utils');

const pageId = 'a2d67b9cb48e4b2aaca6026d8d577dfd';
const propId = 'uy%7Db';
const argv = yargs.default({ pageId, propId }).argv;
const pageId = '3291c1cce3f380b7b2b0d382d589ef78';
const propId = '}Ulu';
const argv = yargs
.option('pageId', {
alias: 'd',
default: pageId,
})
.option('propId', {
alias: 'p',
default: propId,
}).argv;

(async () => {
const prop = await notion.pages.properties.retrieve({
Expand Down
30 changes: 29 additions & 1 deletion examples/shared/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function date(start = new Date(), end = null, time_zone = null) {
};

if (end) {
date.end = date;
date.end = end;
}

return { date };
Expand Down Expand Up @@ -111,11 +111,39 @@ function omitProps(properties, keys = 'id') {
return omit(properties);
}

/**
* Merge property configurations for views.
* Takes all properties from a data source and merges them with custom
* property configurations, maintaining the order of customProps.
*
* @param {Object} properties - Properties object from data source
* @param {Array} customProps - Array of custom property configurations
* @returns {Array} Merged array with customProps first, then remaining properties
*/
function mergeViewProperties(properties, customProps = []) {
// Convert all properties to view property format with visible: false
const allProps = Object.values(properties).map((prop) => ({
property_id: decodeURIComponent(prop.id),
visible: false,
}));

// Start with customProps to maintain their order, then add remaining props
const propsMap = new Map(customProps.map((p) => [p.property_id, p]));
allProps.forEach((p) => {
if (!propsMap.has(p.property_id)) {
propsMap.set(p.property_id, p);
}
});

return Array.from(propsMap.values());
}

module.exports = {
date,
emoji,
getPropertySchema,
icon,
mergeViewProperties,
number,
pageTitle,
richText,
Expand Down
134 changes: 134 additions & 0 deletions examples/views/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* Creates a new table view for a data source with custom properties,
* filters, sorts, and grouping configuration.
*
* Arguments:
*
* --data-source-id, -d: ID of the data source to create a view for
* --database-id, -b: ID of the database to create a top-level view in (optional)
*/

const { notion, yargs } = require('../shared');
const { log } = require('../shared/utils');

const dataSourceId = '45808fcd2698412a97df10e19c36cc21';
const databaseId = 'a48377a8442142f19d7ef89211fce07d';

const argv = yargs
.option('dataSourceId', {
alias: 'd',
default: dataSourceId,
})
.option('databaseId', {
alias: 'b',
default: databaseId,
}).argv;

(async () => {
const properties = [
{
property_id: '}Ulu',
visible: true,
status_show_as: 'checkbox',
width: 32,
},
{
property_id: '~IY^',
visible: true,
width: 32,
},
{
property_id: 'title',
visible: true,
wrap: true,
},
{
property_id: 'k\\Sg',
visible: true,
},
{
property_id: '4]f9',
visible: true,
width: 300,
},
];

const params = {
data_source_id: argv.dataSourceId,
name: 'My New View',
type: 'table',
position: {
type: 'start',
},
filter: {
// FIXME: this does not work, creates three separate filter groups
and: [
// TODO: it'd be great if list filters supported arrays!
{
property: 'Status',
status: {
does_not_equal: 'Done',
},
},
{
property: 'Status',
status: {
does_not_equal: 'Archive',
},
},
{
property: 'Owner',
people: { contains: 'me' },
},
{
property: 'Completed by',
people: { does_not_contain: 'me' },
},
],
},
quick_filters: {
Date: {
date: {
on_or_before: 'today',
},
},
// TODO: would be great if you could create blank quick filters
// Owner: {
// people: { contains: '' },
// },
},
sorts: [
{
property: 'Date',
direction: 'ascending',
},
],
configuration: {
type: 'table',
properties,
group_by: {
type: 'relation',
property_id: '4]f9',
sort: {
type: 'ascending',
},
hide_empty_groups: true,
},
subtasks: {
display_mode: 'flattened',
filter_scope: 'parents_and_subitems',
toggle_column_id: 'title',
},
show_vertical_lines: false,
},
};

// Add database_id if provided for top-level views
if (argv.databaseId) {
params.database_id = argv.databaseId;
}

const view = await notion.views.create(params);

log(view);
})();
25 changes: 25 additions & 0 deletions examples/views/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Deletes a specified view by its ID.
*
* Arguments:
*
* --view-id: ID of the view to delete
*/

const { notion, yargs } = require('../shared');
const { log } = require('../shared/utils');

const viewId = '3101c1cce3f3804d8481000c37e852fa';

const argv = yargs.option('viewId', {
alias: 'v',
default: viewId,
}).argv;

(async () => {
const view = await notion.views.delete({
view_id: argv.viewId,
});

log(view);
})();
25 changes: 25 additions & 0 deletions examples/views/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Lists all views for a specified database.
*
* Arguments:
*
* --database-id: ID of the database to list views of
*/

const { notion, yargs } = require('../shared');
const { log } = require('../shared/utils');

const databaseId = 'a48377a8442142f19d7ef89211fce07d';

const argv = yargs.option('databaseId', {
alias: 'd',
default: databaseId,
}).argv;

(async () => {
const views = await notion.views.list({
database_id: argv.databaseId,
});

log(views);
})();
40 changes: 40 additions & 0 deletions examples/views/query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Queries a view by ID.
*
* Arguments:
*
* --view-id: ID of the view to query.
*/

const { log } = require('../shared/utils');
const { notion, yargs } = require('../shared');

const viewId = 'e40b1aba33a04d0095731e38f7f41c4f';

const argv = yargs.option('viewId', {
alias: 'v',
default: viewId,
}).argv;

(async () => {
// FIXME: total_count always null?
const query = await notion.views.queries.create({
view_id: argv.viewId,
});

log(query);

// TODO: paginate like fetch-pages.js (to abstract)
// const results = await notion.views.queries.results({
// view_id: argv.viewId,
// query_id: query.id,
// start_cursor: query.next_cursor,
// page_size: 50,
// });

// This endpoint is idempotent — calling it on an already-deleted or expired query still returns success.
await notion.views.queries.delete({
view_id: argv.viewId,
query_id: query.id,
});
})();
25 changes: 25 additions & 0 deletions examples/views/retrieve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Retrieves details of a specific view by its ID.
*
* Arguments:
*
* --view-id: ID of the view to retrieve
*/

const { log } = require('../shared/utils');
const { notion, yargs } = require('../shared');

const viewId = 'e40b1aba33a04d0095731e38f7f41c4f';

const argv = yargs.option('viewId', {
alias: 'v',
default: viewId,
}).argv;

(async () => {
const view = await notion.views.retrieve({
view_id: argv.viewId,
});

log(view);
})();
42 changes: 42 additions & 0 deletions examples/views/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Updates a view's name, sorts, and grouping configuration.
*
* Arguments:
*
* --view-id: ID of the view to update
*/

const { notion, yargs } = require('../shared');
const { log } = require('../shared/utils');

const viewId = 'e40b1aba33a04d0095731e38f7f41c4f';

const argv = yargs.option('viewId', {
alias: 'v',
default: viewId,
}).argv;

(async () => {
const view = await notion.views.update({
view_id: argv.viewId,
name: 'Updated View',
sorts: [
{
property: 'Date',
direction: 'ascending',
},
],
configuration: {
type: 'table',
group_by: {
type: 'person',
property_id: '<ji~',
sort: {
type: 'ascending',
},
},
},
});

log(view);
})();
Loading