@elumixor/notion-orm 2.1.3 → 2.2.1

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
@@ -1619,6 +1619,26 @@ function recursivelyBuildFilter(queryFilter, camelPropertyNameToNameAndTypeMap)
1619
1619
  }
1620
1620
 
1621
1621
  // src/db-client/client.ts
1622
+ var MIME_TO_EXT = {
1623
+ "image/jpeg": "jpg",
1624
+ "image/png": "png",
1625
+ "image/gif": "gif",
1626
+ "image/webp": "webp",
1627
+ "image/svg+xml": "svg",
1628
+ "image/avif": "avif"
1629
+ };
1630
+ function sniffMimeType(bytes) {
1631
+ if (bytes[0] === 255 && bytes[1] === 216 && bytes[2] === 255)
1632
+ return "image/jpeg";
1633
+ if (bytes[0] === 137 && bytes[1] === 80 && bytes[2] === 78 && bytes[3] === 71)
1634
+ return "image/png";
1635
+ if (bytes[0] === 71 && bytes[1] === 73 && bytes[2] === 70)
1636
+ return "image/gif";
1637
+ if (bytes[8] === 87 && bytes[9] === 69 && bytes[10] === 66 && bytes[11] === 80)
1638
+ return "image/webp";
1639
+ return "application/octet-stream";
1640
+ }
1641
+
1622
1642
  class DatabaseClient {
1623
1643
  client;
1624
1644
  id;
@@ -1681,7 +1701,7 @@ class DatabaseClient {
1681
1701
  }
1682
1702
  }
1683
1703
  async create(args) {
1684
- const callBody = this.buildCreateBody(args.data, args.$icon, args.$cover);
1704
+ const callBody = await this.buildCreateBody(args.data, args.$icon, args.$cover);
1685
1705
  const page = await this.client.pages.create(callBody);
1686
1706
  return this.parsePage(page);
1687
1707
  }
@@ -1690,10 +1710,11 @@ class DatabaseClient {
1690
1710
  }
1691
1711
  async update(args) {
1692
1712
  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 } };
1713
+ const [icon, cover] = await Promise.all([this.resolveIconCover(args.$icon), this.resolveIconCover(args.$cover)]);
1714
+ if (icon !== undefined)
1715
+ callBody.icon = icon;
1716
+ if (cover !== undefined)
1717
+ callBody.cover = cover;
1697
1718
  for (const [propertyName, value] of Object.entries(args.data)) {
1698
1719
  const { type, columnName } = this.camelPropertyNameToNameAndTypeMap[propertyName];
1699
1720
  const columnObject = buildPropertyValueForAddPage({ type, value });
@@ -1733,15 +1754,16 @@ class DatabaseClient {
1733
1754
  const results = await this.fetchAllPages(queryCall, {});
1734
1755
  return results.length;
1735
1756
  }
1736
- buildCreateBody(data, $icon, $cover) {
1757
+ async buildCreateBody(data, $icon, $cover) {
1737
1758
  const callBody = {
1738
1759
  parent: { data_source_id: this.id, type: "data_source_id" },
1739
1760
  properties: {}
1740
1761
  };
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 } };
1762
+ const [icon, cover] = await Promise.all([this.resolveIconCover($icon), this.resolveIconCover($cover)]);
1763
+ if (icon !== undefined)
1764
+ callBody.icon = icon;
1765
+ if (cover !== undefined)
1766
+ callBody.cover = cover;
1745
1767
  for (const [propertyName, value] of Object.entries(data)) {
1746
1768
  const { type, columnName } = this.camelPropertyNameToNameAndTypeMap[propertyName];
1747
1769
  const columnObject = buildPropertyValueForAddPage({ type, value });
@@ -1750,6 +1772,22 @@ class DatabaseClient {
1750
1772
  }
1751
1773
  return callBody;
1752
1774
  }
1775
+ async resolveIconCover(value) {
1776
+ if (value === undefined)
1777
+ return;
1778
+ if (value === null)
1779
+ return null;
1780
+ if (typeof value === "string")
1781
+ return { type: "external", external: { url: value } };
1782
+ const bytes = value instanceof Blob ? new Uint8Array(await value.arrayBuffer()) : value;
1783
+ const contentType = value instanceof Blob && value.type ? value.type : sniffMimeType(bytes);
1784
+ const ext = MIME_TO_EXT[contentType] ?? "bin";
1785
+ const filename = `upload.${ext}`;
1786
+ const blob = new Blob([bytes], { type: contentType });
1787
+ const upload = await this.client.fileUploads.create({ filename, content_type: contentType });
1788
+ await this.client.fileUploads.send({ file_upload_id: upload.id, file: { data: blob, filename } });
1789
+ return { type: "file_upload", file_upload: { id: upload.id } };
1790
+ }
1753
1791
  parsePage(page, meta) {
1754
1792
  const result = { id: page.id };
1755
1793
  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.1",
4
4
  "type": "module",
5
5
  "description": "Bring Notion database schemas/types to TypeScript",
6
6
  "main": "dist/index.js",