2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2022-11-22 04:43:28 -08:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeListSearchItems,
|
|
|
|
INodeListSearchResult,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
import { notionApiRequestAllItems } from './GenericFunctions';
|
2022-11-11 04:37:52 -08:00
|
|
|
|
|
|
|
export async function getDatabases(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
filter?: string,
|
|
|
|
): Promise<INodeListSearchResult> {
|
|
|
|
const returnData: INodeListSearchItems[] = [];
|
|
|
|
const body: IDataObject = {
|
|
|
|
page_size: 100,
|
|
|
|
query: filter,
|
|
|
|
filter: { property: 'object', value: 'database' },
|
|
|
|
};
|
2022-12-29 03:20:43 -08:00
|
|
|
const databases = await notionApiRequestAllItems.call(this, 'results', 'POST', '/search', body);
|
2022-11-11 04:37:52 -08:00
|
|
|
for (const database of databases) {
|
|
|
|
returnData.push({
|
|
|
|
name: database.title[0]?.plain_text || database.id,
|
|
|
|
value: database.id,
|
|
|
|
url: database.url,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
returnData.sort((a, b) => {
|
|
|
|
if (a.name.toLocaleLowerCase() < b.name.toLocaleLowerCase()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.name.toLocaleLowerCase() > b.name.toLocaleLowerCase()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
return { results: returnData };
|
|
|
|
}
|