@elumixor/notion-orm 2.1.2 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -174152,7 +174152,10 @@ async function resolveDataSourceId(auth, id) {
174152
174152
  const dataSources = database.data_sources;
174153
174153
  if (dataSources && dataSources.length > 0)
174154
174154
  return dataSources[0].id;
174155
- } catch {}
174155
+ } catch (error2) {
174156
+ if (!(import_client.APIResponseError.isAPIResponseError(error2) && error2.code === import_client.APIErrorCode.ObjectNotFound))
174157
+ throw error2;
174158
+ }
174156
174159
  await probeClient.dataSources.retrieve({ data_source_id: id });
174157
174160
  return id;
174158
174161
  }
@@ -27,6 +27,10 @@ export declare function buildPropertyValueForAddPage(args: {
27
27
  phone_number: string;
28
28
  } | {
29
29
  url: string;
30
+ } | {
31
+ relation: {
32
+ id: string;
33
+ }[];
30
34
  } | {
31
35
  checkbox: boolean;
32
36
  } | {
@@ -1,5 +1,6 @@
1
1
  import type { ZodTypeAny } from "zod";
2
2
  import type { FindManyArgs, IconCoverResult, PaginateArgs, QueryFilter, SupportedNotionColumnType } from "./types";
3
+ export type ImageInput = string | Blob | Uint8Array;
3
4
  export type camelPropertyNameToNameAndTypeMapType = Record<string, {
4
5
  columnName: string;
5
6
  type: SupportedNotionColumnType;
@@ -107,16 +108,16 @@ export declare class DatabaseClient<DatabaseSchemaType extends Record<string, an
107
108
  /** Create a new record and return it. */
108
109
  create(args: {
109
110
  data: DatabaseSchemaType;
110
- $icon?: string | null;
111
- $cover?: string | null;
111
+ $icon?: ImageInput | null;
112
+ $cover?: ImageInput | null;
112
113
  }): Promise<Partial<DatabaseSchemaType> & {
113
114
  id: string;
114
115
  }>;
115
116
  /** Create multiple records and return them. */
116
117
  createMany(args: {
117
118
  data: DatabaseSchemaType[];
118
- $icon?: string | null;
119
- $cover?: string | null;
119
+ $icon?: ImageInput | null;
120
+ $cover?: ImageInput | null;
120
121
  }): Promise<(Partial<DatabaseSchemaType> & {
121
122
  id: string;
122
123
  })[]>;
@@ -126,15 +127,15 @@ export declare class DatabaseClient<DatabaseSchemaType extends Record<string, an
126
127
  id: string;
127
128
  };
128
129
  data: Partial<DatabaseSchemaType>;
129
- $icon?: string | null;
130
- $cover?: string | null;
130
+ $icon?: ImageInput | null;
131
+ $cover?: ImageInput | null;
131
132
  }): Promise<void>;
132
133
  /** Update all records matching the filter. Returns the count of updated records. */
133
134
  updateMany(args: {
134
135
  where?: QueryFilter<DatabaseSchemaType, ColumnNameToColumnType>;
135
136
  data: Partial<DatabaseSchemaType>;
136
- $icon?: string | null;
137
- $cover?: string | null;
137
+ $icon?: ImageInput | null;
138
+ $cover?: ImageInput | null;
138
139
  }): Promise<{
139
140
  count: number;
140
141
  }>;
@@ -143,8 +144,8 @@ export declare class DatabaseClient<DatabaseSchemaType extends Record<string, an
143
144
  where: QueryFilter<DatabaseSchemaType, ColumnNameToColumnType>;
144
145
  create: DatabaseSchemaType;
145
146
  update: Partial<DatabaseSchemaType>;
146
- $icon?: string | null;
147
- $cover?: string | null;
147
+ $icon?: ImageInput | null;
148
+ $cover?: ImageInput | null;
148
149
  }): Promise<{
149
150
  created: boolean;
150
151
  id: string;
@@ -166,6 +167,7 @@ export declare class DatabaseClient<DatabaseSchemaType extends Record<string, an
166
167
  where?: QueryFilter<DatabaseSchemaType, ColumnNameToColumnType>;
167
168
  }): Promise<number>;
168
169
  private buildCreateBody;
170
+ private resolveIconCover;
169
171
  private parsePage;
170
172
  private buildQueryCall;
171
173
  private fetchAllPages;
package/dist/index.js CHANGED
@@ -1388,6 +1388,8 @@ function buildPropertyValueForAddPage(args) {
1388
1388
  return phoneNumberCall({ value });
1389
1389
  } else if (type === "url" && typeof value === "string") {
1390
1390
  return urlCall({ url: value });
1391
+ } else if (type === "relation" && Array.isArray(value)) {
1392
+ return relationCall({ ids: value });
1391
1393
  } else if (type === "checkbox" && typeof value === "boolean") {
1392
1394
  return checkboxCall({ checked: value });
1393
1395
  } else if (type === "title" && typeof value === "string") {
@@ -1463,6 +1465,9 @@ var emailCall = (args) => {
1463
1465
  const { value } = args;
1464
1466
  return { email: value };
1465
1467
  };
1468
+ var relationCall = (args) => {
1469
+ return { relation: args.ids.map((id) => ({ id })) };
1470
+ };
1466
1471
 
1467
1472
  // src/db-client/query.ts
1468
1473
  function buildQueryResponse(res, camelPropertyNameToNameAndTypeMap, validateSchema, meta) {
@@ -1676,7 +1681,7 @@ class DatabaseClient {
1676
1681
  }
1677
1682
  }
1678
1683
  async create(args) {
1679
- const callBody = this.buildCreateBody(args.data, args.$icon, args.$cover);
1684
+ const callBody = await this.buildCreateBody(args.data, args.$icon, args.$cover);
1680
1685
  const page = await this.client.pages.create(callBody);
1681
1686
  return this.parsePage(page);
1682
1687
  }
@@ -1685,10 +1690,11 @@ class DatabaseClient {
1685
1690
  }
1686
1691
  async update(args) {
1687
1692
  const callBody = { page_id: args.where.id, properties: {} };
1688
- if (args.$icon !== undefined)
1689
- callBody.icon = args.$icon === null ? null : { type: "external", external: { url: args.$icon } };
1690
- if (args.$cover !== undefined)
1691
- callBody.cover = args.$cover === null ? null : { type: "external", external: { url: args.$cover } };
1693
+ const [icon, cover] = await Promise.all([this.resolveIconCover(args.$icon), this.resolveIconCover(args.$cover)]);
1694
+ if (icon !== undefined)
1695
+ callBody.icon = icon;
1696
+ if (cover !== undefined)
1697
+ callBody.cover = cover;
1692
1698
  for (const [propertyName, value] of Object.entries(args.data)) {
1693
1699
  const { type, columnName } = this.camelPropertyNameToNameAndTypeMap[propertyName];
1694
1700
  const columnObject = buildPropertyValueForAddPage({ type, value });
@@ -1728,15 +1734,16 @@ class DatabaseClient {
1728
1734
  const results = await this.fetchAllPages(queryCall, {});
1729
1735
  return results.length;
1730
1736
  }
1731
- buildCreateBody(data, $icon, $cover) {
1737
+ async buildCreateBody(data, $icon, $cover) {
1732
1738
  const callBody = {
1733
1739
  parent: { data_source_id: this.id, type: "data_source_id" },
1734
1740
  properties: {}
1735
1741
  };
1736
- if ($icon !== undefined)
1737
- callBody.icon = $icon === null ? null : { type: "external", external: { url: $icon } };
1738
- if ($cover !== undefined)
1739
- callBody.cover = $cover === null ? null : { type: "external", external: { url: $cover } };
1742
+ const [icon, cover] = await Promise.all([this.resolveIconCover($icon), this.resolveIconCover($cover)]);
1743
+ if (icon !== undefined)
1744
+ callBody.icon = icon;
1745
+ if (cover !== undefined)
1746
+ callBody.cover = cover;
1740
1747
  for (const [propertyName, value] of Object.entries(data)) {
1741
1748
  const { type, columnName } = this.camelPropertyNameToNameAndTypeMap[propertyName];
1742
1749
  const columnObject = buildPropertyValueForAddPage({ type, value });
@@ -1745,6 +1752,18 @@ class DatabaseClient {
1745
1752
  }
1746
1753
  return callBody;
1747
1754
  }
1755
+ async resolveIconCover(value) {
1756
+ if (value === undefined)
1757
+ return;
1758
+ if (value === null)
1759
+ return null;
1760
+ if (typeof value === "string")
1761
+ return { type: "external", external: { url: value } };
1762
+ const blob = value instanceof Blob ? value : new Blob([value]);
1763
+ const upload = await this.client.fileUploads.create({ filename: "upload" });
1764
+ await this.client.fileUploads.send({ file_upload_id: upload.id, file: { data: blob, filename: "upload" } });
1765
+ return { type: "file_upload", file_upload: { id: upload.id } };
1766
+ }
1748
1767
  parsePage(page, meta) {
1749
1768
  const result = { id: page.id };
1750
1769
  for (const [columnName, value] of Object.entries(page.properties)) {
@@ -3,5 +3,6 @@
3
3
  * External users' generated files import DatabaseClient and types from here.
4
4
  */
5
5
  export { DatabaseClient } from "./db-client/client";
6
+ export type { ImageInput } from "./db-client/client";
6
7
  export type { FindManyArgs, PaginateArgs, Query, QueryFilter, QueryResult, QueryResultType, OrderByInput, SupportedNotionColumnType, } from "./db-client/types";
7
8
  export type { NotionConfigType } from "./config/helpers";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elumixor/notion-orm",
3
- "version": "2.1.2",
3
+ "version": "2.2.0",
4
4
  "type": "module",
5
5
  "description": "Bring Notion database schemas/types to TypeScript",
6
6
  "main": "dist/index.js",