@elumixor/notion-orm 2.1.3 → 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
  }
@@ -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
@@ -1681,7 +1681,7 @@ class DatabaseClient {
1681
1681
  }
1682
1682
  }
1683
1683
  async create(args) {
1684
- const callBody = this.buildCreateBody(args.data, args.$icon, args.$cover);
1684
+ const callBody = await this.buildCreateBody(args.data, args.$icon, args.$cover);
1685
1685
  const page = await this.client.pages.create(callBody);
1686
1686
  return this.parsePage(page);
1687
1687
  }
@@ -1690,10 +1690,11 @@ class DatabaseClient {
1690
1690
  }
1691
1691
  async update(args) {
1692
1692
  const callBody = { page_id: args.where.id, properties: {} };
1693
- if (args.$icon !== undefined)
1694
- callBody.icon = args.$icon === null ? null : { type: "external", external: { url: args.$icon } };
1695
- if (args.$cover !== undefined)
1696
- 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;
1697
1698
  for (const [propertyName, value] of Object.entries(args.data)) {
1698
1699
  const { type, columnName } = this.camelPropertyNameToNameAndTypeMap[propertyName];
1699
1700
  const columnObject = buildPropertyValueForAddPage({ type, value });
@@ -1733,15 +1734,16 @@ class DatabaseClient {
1733
1734
  const results = await this.fetchAllPages(queryCall, {});
1734
1735
  return results.length;
1735
1736
  }
1736
- buildCreateBody(data, $icon, $cover) {
1737
+ async buildCreateBody(data, $icon, $cover) {
1737
1738
  const callBody = {
1738
1739
  parent: { data_source_id: this.id, type: "data_source_id" },
1739
1740
  properties: {}
1740
1741
  };
1741
- if ($icon !== undefined)
1742
- callBody.icon = $icon === null ? null : { type: "external", external: { url: $icon } };
1743
- if ($cover !== undefined)
1744
- 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;
1745
1747
  for (const [propertyName, value] of Object.entries(data)) {
1746
1748
  const { type, columnName } = this.camelPropertyNameToNameAndTypeMap[propertyName];
1747
1749
  const columnObject = buildPropertyValueForAddPage({ type, value });
@@ -1750,6 +1752,18 @@ class DatabaseClient {
1750
1752
  }
1751
1753
  return callBody;
1752
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
+ }
1753
1767
  parsePage(page, meta) {
1754
1768
  const result = { id: page.id };
1755
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.3",
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",