UNPKG

@pagenote/notion-database

Version:

make notion as a real-database for server

384 lines 13.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const notionType_1 = require("../types/notionType"); const util_1 = require("./util"); const ConvertNotionTypeMap = { [notionType_1.SupportNotionTypes.checkbox]: { initPropertySchema: function (key) { return { type: notionType_1.SupportNotionTypes.checkbox, checkbox: {}, name: key }; }, objectToProperty: function (input) { return { type: notionType_1.SupportNotionTypes.checkbox, checkbox: !!input }; }, objectToPropertyFilter: function (key, input, operator = 'equals') { const filter = { [operator]: input === true }; return { checkbox: filter, property: key, type: 'checkbox' }; }, propertyToObject: function (input) { return input.type === 'checkbox' ? input === null || input === void 0 ? void 0 : input.checkbox : false; } }, [notionType_1.SupportNotionTypes.number]: { initPropertySchema: function (key) { return { type: notionType_1.SupportNotionTypes.number, number: { format: 'number' }, // id: key, name: key }; }, objectToProperty: function (input) { return { type: notionType_1.SupportNotionTypes.number, number: typeof input === 'number' ? input : 0 }; }, propertyToObject: function (input) { return input.type === 'number' ? input === null || input === void 0 ? void 0 : input.number : 0; }, objectToPropertyFilter: function (key, input, operator = 'equals') { const filter = { [operator]: input }; return { number: filter, property: key, type: notionType_1.SupportNotionTypes.number }; } }, // notion 每一行数据必须字段 title [notionType_1.SupportNotionTypes.title]: { initPropertySchema: function (filedKey) { return { type: notionType_1.SupportNotionTypes.title, title: {}, // id: filedKey, name: filedKey }; }, objectToProperty: function (input = '') { const content = input ? input.toString() : ''; return { type: notionType_1.SupportNotionTypes.title, title: [ { type: 'text', text: { content: content, link: null }, annotations: { bold: false, italic: false, strikethrough: false, underline: false, code: false, color: 'default' }, plain_text: content, href: '' } ] }; }, propertyToObject: function (input) { const inputs = input.type === notionType_1.SupportNotionTypes.title ? input.title : []; return (0, util_1.resolveNotionRichTextItemRequest)(inputs); }, objectToPropertyFilter: function (key, input, operator = 'equals') { const filter = { [operator]: input }; return { title: filter, property: key, type: notionType_1.SupportNotionTypes.title }; } }, [notionType_1.SupportNotionTypes.rich_text]: { initPropertySchema: function (filedKey) { return { type: notionType_1.SupportNotionTypes.rich_text, rich_text: {}, name: filedKey }; }, objectToProperty: function (input = '') { const content = input ? input.toString() : ''; return { type: notionType_1.SupportNotionTypes.rich_text, rich_text: [ { type: 'text', text: { content: content, link: null }, annotations: { bold: false, italic: false, strikethrough: false, underline: false, code: false, color: 'default' }, plain_text: content, href: '' } ] }; }, propertyToObject: function (input) { const inputs = input.type === notionType_1.SupportNotionTypes.rich_text ? input.rich_text : []; return (0, util_1.resolveNotionRichTextItemRequest)(inputs); }, objectToPropertyFilter: function (key, input, operator = 'equals') { const filter = { [operator]: input }; return { rich_text: filter, property: key, type: notionType_1.SupportNotionTypes.rich_text }; } }, [notionType_1.SupportNotionTypes.select]: { propertyToObject: function (input) { var _a; return input.type === 'select' ? (_a = input.select) === null || _a === void 0 ? void 0 : _a.name : ''; }, initPropertySchema: function (filedKey) { return { type: notionType_1.SupportNotionTypes.select, select: { options: [] }, name: filedKey || '' }; }, objectToProperty: function (input) { const name = input ? input.toString() : ''; return { type: notionType_1.SupportNotionTypes.select, select: { name: name // id: item.toString(), // color: 'default' } }; }, objectToPropertyFilter: function (key, input, operator = 'equals') { const content = input ? input.toString() : ''; const filter = { equals: '', [operator]: content }; return { select: filter, property: key, type: notionType_1.SupportNotionTypes.select }; } }, [notionType_1.SupportNotionTypes.status]: { propertyToObject: function (input) { var _a; return input.type === 'status' ? (_a = input === null || input === void 0 ? void 0 : input.status) === null || _a === void 0 ? void 0 : _a.name : ''; }, initPropertySchema: function (filedKey) { return { type: notionType_1.SupportNotionTypes.status, status: {}, name: filedKey }; }, objectToProperty: function (input = '') { const name = input ? input.toString() : ''; return { type: notionType_1.SupportNotionTypes.status, status: { name: name // color: 'default' } }; }, objectToPropertyFilter: function (key, input, operator = 'equals') { const content = input ? input.toString() : ''; const filter = { [operator]: content }; return { status: filter, property: key, type: notionType_1.SupportNotionTypes.status }; } }, [notionType_1.SupportNotionTypes.multi_select]: { initPropertySchema: function (filedKey) { return { type: notionType_1.SupportNotionTypes.multi_select, multi_select: { options: [] }, name: filedKey }; }, objectToProperty: function (input) { const arrays = Array.isArray(input) ? input : [(input || '').toString()]; // @ts-ignore const select = arrays.map(function (item) { return { name: (item || '').toString() // id: item.toString(), // color: 'default' }; }); return { type: notionType_1.SupportNotionTypes.multi_select, multi_select: select }; }, propertyToObject: function (input) { const selectes = input.type === notionType_1.SupportNotionTypes.multi_select ? input.multi_select : []; return selectes.map(function (item) { return item.name; }); }, objectToPropertyFilter: function (key, data, operator = 'contains') { const input = data; const items = Array.isArray(input) ? input : [input]; const and = items.map(function (item) { const filter = { [operator]: item }; const filterItem = { multi_select: filter, property: key, type: notionType_1.SupportNotionTypes.multi_select }; return filterItem; }); const result = { and: and }; return result; } }, [notionType_1.SupportNotionTypes.date]: { initPropertySchema: function (filedKey) { return { type: notionType_1.SupportNotionTypes.date, date: {}, name: filedKey }; }, objectToProperty: function (data) { const input = data; const start = Array.isArray(input) ? input[0] : input; const end = Array.isArray(input) ? input[1] : null; return { type: notionType_1.SupportNotionTypes.date, date: { start: start ? new Date(start).toISOString() : '', end: end ? new Date(end).toISOString() : null, time_zone: null } }; }, propertyToObject: function (input) { const data = input.type === notionType_1.SupportNotionTypes.date ? input.date : { end: '', start: '' }; if (!data || !data.start) { return null; } return data.end ? [new Date(data.start), new Date(data.end)] : new Date(data.start); }, objectToPropertyFilter: function (key, data, operator = 'on_or_before') { const input = data; const date = Array.isArray(input) ? input[0] : input; const filter = { [operator]: date.toISOString() }; return { date: filter, property: key, type: notionType_1.SupportNotionTypes.date }; } }, [notionType_1.SupportNotionTypes.url]: { initPropertySchema: function (filedKey) { return { type: notionType_1.SupportNotionTypes.url, url: {}, name: filedKey }; }, objectToProperty: function (input) { const url = input ? input.toString() : ''; return { type: notionType_1.SupportNotionTypes.url, url: url }; }, propertyToObject: function (input) { const url = input.type === notionType_1.SupportNotionTypes.url ? input.url : ''; return url; }, objectToPropertyFilter: function (key, input, operator = 'equals') { const filter = { [operator]: input }; return { url: filter, property: key, type: notionType_1.SupportNotionTypes.url }; } }, [notionType_1.UnSupportNotionTypes.files]: { propertyToObject: function (input) { return input.type === 'files' ? input.files.map(function (item) { switch (item.type) { case 'file': return item.file.url; case 'external': return item.external.url; default: return ''; } }) : []; } }, [notionType_1.UnSupportNotionTypes.email]: { propertyToObject: function (input) { return input.type === 'email' ? input.email : ''; } } }; exports.default = ConvertNotionTypeMap; //# sourceMappingURL=index.js.map