Skip to content

Commit fe7ebc8

Browse files
committed
Updates most examples to data sources
1 parent d94f8b6 commit fe7ebc8

File tree

20 files changed

+145
-69
lines changed

20 files changed

+145
-69
lines changed

examples/architect-os/update-dates.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ function validDateConfig(part, defaultValue = 1) {
7373
console.log(`Updating ${dbTitle} dates (${id})...`);
7474

7575
const database = await notion.databases.retrieve({ database_id: id });
76+
const dataSource = database.data_sources.at(0);
7677
const query = filter ? { filter } : null;
77-
const pages = await fetchAllPages(id, query);
78+
const pages = await fetchAllPages(dataSource.id, query);
7879

7980
const sortedPages = pages.sort((a, b) => {
8081
return getDate(a, dateProp) - getDate(b, dateProp);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Update a data source.
3+
*
4+
* This example updates a formula, renames the data source, and switches an icon.
5+
*
6+
* Arguments:
7+
*
8+
* --data-source-id: ID of the database to update
9+
* --prop-id: ID of property to update
10+
* --title: Title of the data source to change to
11+
*/
12+
13+
const { notion, yargs } = require('../../shared');
14+
const props = require('../../shared/props');
15+
const { log } = require('../../shared/utils');
16+
17+
const dataSourceId = 'bb23cc5b-70e1-4b11-ad4c-d92a97c43717';
18+
const title = 'Updated Data Source Name';
19+
const propId = 'f%5Csj';
20+
const argv = yargs.default({ dataSourceId, propId, title }).argv;
21+
22+
const properties = {
23+
[argv.propId]: {
24+
name: 'X * Y',
25+
formula: {
26+
expression: 'prop("X") * prop("Y")',
27+
},
28+
},
29+
};
30+
31+
const params = {
32+
data_source_id: argv.dataSourceId,
33+
icon: props.emoji('🐞'),
34+
title: [props.text(argv.title)],
35+
properties,
36+
};
37+
38+
(async () => {
39+
const dataSource = await notion.dataSources.update(params);
40+
41+
log(dataSource);
42+
})();

examples/databases/bulk-edit/changelog.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ async function editPage(page) {
5151
}
5252

5353
(async () => {
54-
const pages = await fetchAllPages(databaseId);
54+
const database = await notion.databases.retrieve({ database_id: databaseId });
55+
const dataSource = database.data_sources.at(0);
56+
const pages = await fetchAllPages(dataSource.id);
5557

5658
await performWithAll(pages, editPage);
5759
})();

examples/databases/bulk-edit/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ async function editPage(page) {
6464
}
6565

6666
(async () => {
67-
const pages = await fetchAllPages(argv.databaseId, {
67+
const database = await notion.databases.retrieve({ database_id: argv.databaseId });
68+
const dataSource = database.data_sources.at(0);
69+
const pages = await fetchAllPages(dataSource.id, {
6870
filter: {
6971
or: [
7072
{

examples/databases/bulk-edit/spends-and-trends.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/**
2+
* Note: this uses an older version of the Notion API which used Databases
3+
* APIs instead of the newer Data Sources APIs.
4+
*
25
* Example bulk-edit script. In this script, there are two databases. One is
36
* Spends which has a bunch of data for advertising spends in it. The other
47
* is Trends, which has a bunch of data for traffic/metrics for websites. There
@@ -32,7 +35,6 @@ async function editPage(page) {
3235
const dateTitle = format(parsed, 'MMM d, yyyy');
3336
const spendsTitle = `Spends: ${dateTitle}`;
3437

35-
// FIXME: use data sources
3638
const {
3739
results: [spends],
3840
} = await notion.databases.query({

examples/databases/search/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
const { notion, yargs } = require('../../shared');
1212
const { log } = require('../../shared/utils');
1313

14-
const databaseId = '7354557becb34d72b6140bb541ac529a';
14+
const dataSourceId = '668bd2e3-9307-4438-83b5-75b11376e8a3';
1515

1616
const argv = yargs.default({
17-
databaseId,
17+
dataSourceId,
1818
query: '',
1919
pageSize: 100,
2020
}).argv;
@@ -23,9 +23,8 @@ const argv = yargs.default({
2323
// that contains the text "Hall". This is a non-exact match, so would find:
2424
// "Hall of Fame" and "Hall 1 Lighting".
2525
(async () => {
26-
// FIXME: use data sources
27-
const response = await notion.databases.query({
28-
database_id: argv.databaseId,
26+
const response = await notion.dataSources.query({
27+
data_source_id: argv.dataSourceId,
2928
query: argv.query,
3029
filter: {
3130
property: 'Task',
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/**
22
* Sort a multi-select's options alphabetically.
33
*
4+
* Note: you can only sort up to 100 options like this!
5+
*
46
* Arguments:
57
*
6-
* --database-id: ID of the database to create in
8+
* --data-source-id: ID of the database to create in
79
* --sort-prop: name of multi-select to sort
810
* --[no-]case-sensitive: whether to sort ABCabc (default or --case-sensitive) or AaBbCc (--no-case-sensitive)
911
*/
@@ -12,30 +14,30 @@ const { notion, yargs } = require('../../shared');
1214
const { log } = require('../../shared/utils');
1315
const _ = require('lodash');
1416

15-
const databaseId = 'f91e66f29d63457894be7c91b132f345';
17+
const dataSourceId = '1cf71d94-0107-4560-a702-a41cb5d90aea';
1618
const sortProp = 'Feeling';
1719
const argv = yargs
1820
.boolean('case-sensitive')
19-
.default({ databaseId, sortProp, caseSensitive: true }).argv;
21+
.default({ dataSourceId, sortProp, caseSensitive: true }).argv;
2022

2123
(async () => {
22-
let database = await notion.databases.retrieve({
23-
database_id: argv.databaseId,
24+
let dataSource = await notion.dataSources.retrieve({
25+
data_source_id: argv.dataSourceId,
2426
});
2527

26-
const propId = database.properties[argv.sortProp].id;
28+
const propId = dataSource.properties[argv.sortProp].id;
2729
const iteratee = argv.caseSensitive ? 'name' : [(option) => option.name.toLowerCase()];
2830

2931
// Sort the options and remove color since it cannot be updated via API
3032
const sortedOptions = _.map(
31-
_.orderBy(database.properties[argv.sortProp].multi_select.options, iteratee),
33+
_.orderBy(dataSource.properties[argv.sortProp].multi_select.options, iteratee),
3234
(option) => {
3335
return _.omit(option, 'color');
3436
}
3537
);
3638

3739
const properties = {
38-
database_id: argv.databaseId,
40+
data_source_id: argv.dataSourceId,
3941
properties: {
4042
[propId]: {
4143
multi_select: {
@@ -45,8 +47,7 @@ const argv = yargs
4547
},
4648
};
4749

48-
// FIXME: use data sources
49-
database = await notion.databases.update(properties);
50+
dataSource = await notion.dataSources.update(properties);
5051

51-
log(database.properties[argv.sortProp]);
52+
log(dataSource.properties[argv.sortProp]);
5253
})();

examples/databases/update/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Update a database.
33
*
4-
* This example updates a formula, renames the database, and switches an icon.
4+
* This example renames the database and switches an icon.
55
*
66
* Works great when used with the database created with the ../create script!
77
*
@@ -34,11 +34,9 @@ const params = {
3434
database_id: argv.databaseId,
3535
icon: props.emoji('💽'),
3636
title: [props.text(argv.title)],
37-
properties,
3837
};
3938

4039
(async () => {
41-
// FIXME: use data sources
4240
const database = await notion.databases.update(params);
4341

4442
log(database);

examples/formula1/circuits.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// FIXME: Data Sources
21
const orderBy = require('lodash/orderBy');
32
const { RateLimit } = require('async-sema');
43

@@ -15,6 +14,10 @@ const limit = RateLimit(1, { timeUnit: 2000, uniformDistribution: true });
1514
async function createCircuitsDatabase() {
1615
const response = await notion.search({
1716
query: DB_TITLE,
17+
filter: {
18+
value: 'data_source',
19+
property: 'object',
20+
},
1821
});
1922

2023
if (response.results.length) {
@@ -57,7 +60,7 @@ async function fetchCircuits() {
5760
return orderBy(circuits, 'circuitName', 'desc');
5861
}
5962

60-
async function createCircuit(database, circuit) {
63+
async function createCircuit(dataSource, circuit) {
6164
process.stdout.write('.');
6265

6366
const {
@@ -76,7 +79,7 @@ async function createCircuit(database, circuit) {
7679

7780
return await notion.pages.create({
7881
parent: {
79-
database_id: database.id,
82+
data_source_id: dataSource.id,
8083
},
8184
properties,
8285
});
@@ -85,10 +88,12 @@ async function createCircuit(database, circuit) {
8588
async function createCircuits(database, circuits) {
8689
process.stdout.write('Creating circuits...');
8790

91+
const dataSource = database.data_sources.at(0);
92+
8893
return await Promise.all(
8994
circuits.map(async (circuit) => {
9095
await limit();
91-
return await createCircuit(database, circuit);
96+
return await createCircuit(dataSource, circuit);
9297
})
9398
);
9499
}

examples/nm/align-member-ids.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ async function updateStudent(page, users) {
5050

5151
(async () => {
5252
const users = await getCache('members');
53+
const database = await notion.databases.retrieve({ database_id: studentsDbId });
54+
const dataSource = database.data_sources.at(0);
5355

54-
const pages = await fetchAllPages(studentsDbId, {
56+
const pages = await fetchAllPages(dataSource.id, {
5557
filter: {
5658
and: [
5759
{

0 commit comments

Comments
 (0)