@elumixor/notion-orm 0.1.1 → 2.1.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.
@@ -1,10 +1,12 @@
1
1
  export type NotionConfigType = {
2
2
  auth: string;
3
3
  databases: Record<string, string>;
4
+ /** Output directory for generated files, relative to project root. Defaults to "generated/notion-orm" */
5
+ outputDir?: string;
4
6
  };
5
7
  export declare function validateConfig(): Promise<void>;
6
8
  export declare function findConfigFile(): {
7
9
  path: string;
8
10
  isTS: true;
9
11
  } | undefined;
10
- export declare function initializeNotionConfigFile(): Promise<void>;
12
+ export declare function initializeNotionConfigFile(): Promise<"created" | "exists">;
@@ -1,4 +1,4 @@
1
- import { SupportedNotionColumnType } from "./queryTypes";
1
+ import { SupportedNotionColumnType } from "./types";
2
2
  export declare function buildPropertyValueForAddPage(args: {
3
3
  type: SupportedNotionColumnType;
4
4
  value: string | number | boolean;
@@ -0,0 +1,175 @@
1
+ import type { ZodTypeAny } from "zod";
2
+ import type { FindManyArgs, IconCoverResult, PaginateArgs, QueryFilter, SupportedNotionColumnType } from "./types";
3
+ export type camelPropertyNameToNameAndTypeMapType = Record<string, {
4
+ columnName: string;
5
+ type: SupportedNotionColumnType;
6
+ }>;
7
+ export declare class DatabaseClient<DatabaseSchemaType extends Record<string, any>, ColumnNameToColumnType extends Record<keyof DatabaseSchemaType, SupportedNotionColumnType>> {
8
+ private client;
9
+ private id;
10
+ private camelPropertyNameToNameAndTypeMap;
11
+ private schema;
12
+ name: string;
13
+ private loggedSchemaValidationIssues;
14
+ constructor(args: {
15
+ id: string;
16
+ camelPropertyNameToNameAndTypeMap: camelPropertyNameToNameAndTypeMapType;
17
+ auth: string;
18
+ name: string;
19
+ schema: ZodTypeAny;
20
+ });
21
+ /** Find all matching records. When `stream` is set, returns an AsyncIterable fetching in batches of that size. */
22
+ findMany<Args extends FindManyArgs<DatabaseSchemaType, ColumnNameToColumnType> & {
23
+ stream: number;
24
+ }>(args: Args): AsyncIterable<Partial<DatabaseSchemaType> & {
25
+ id: string;
26
+ } & IconCoverResult<Args>>;
27
+ findMany<S extends Partial<Record<keyof DatabaseSchemaType, true>>, Args extends Omit<FindManyArgs<DatabaseSchemaType, ColumnNameToColumnType>, "stream" | "select"> & {
28
+ select: S;
29
+ }>(args: Args): Promise<({
30
+ [K in Extract<keyof S, keyof DatabaseSchemaType>]?: DatabaseSchemaType[K];
31
+ } & {
32
+ id: string;
33
+ } & IconCoverResult<Args>)[]>;
34
+ findMany<O extends Partial<Record<keyof DatabaseSchemaType, true>>, Args extends Omit<FindManyArgs<DatabaseSchemaType, ColumnNameToColumnType>, "stream" | "omit"> & {
35
+ omit: O;
36
+ }>(args: Args): Promise<({
37
+ [K in Exclude<keyof DatabaseSchemaType, keyof O>]?: DatabaseSchemaType[K];
38
+ } & {
39
+ id: string;
40
+ } & IconCoverResult<Args>)[]>;
41
+ findMany<Args extends Omit<FindManyArgs<DatabaseSchemaType, ColumnNameToColumnType>, "stream">>(args?: Args): Promise<(Partial<DatabaseSchemaType> & {
42
+ id: string;
43
+ } & IconCoverResult<Args>)[]>;
44
+ /**
45
+ * Fetch one page of results using Notion's native cursor.
46
+ * Pass the returned `nextCursor` as `after` on the next call to get the next page.
47
+ */
48
+ paginate<S extends Partial<Record<keyof DatabaseSchemaType, true>>, Args extends Omit<PaginateArgs<DatabaseSchemaType, ColumnNameToColumnType>, "select"> & {
49
+ select: S;
50
+ }>(args: Args): Promise<{
51
+ data: ({
52
+ [K in Extract<keyof S, keyof DatabaseSchemaType>]?: DatabaseSchemaType[K];
53
+ } & {
54
+ id: string;
55
+ } & IconCoverResult<Args>)[];
56
+ nextCursor: string | null;
57
+ hasMore: boolean;
58
+ }>;
59
+ paginate<O extends Partial<Record<keyof DatabaseSchemaType, true>>, Args extends Omit<PaginateArgs<DatabaseSchemaType, ColumnNameToColumnType>, "omit"> & {
60
+ omit: O;
61
+ }>(args: Args): Promise<{
62
+ data: ({
63
+ [K in Exclude<keyof DatabaseSchemaType, keyof O>]?: DatabaseSchemaType[K];
64
+ } & {
65
+ id: string;
66
+ } & IconCoverResult<Args>)[];
67
+ nextCursor: string | null;
68
+ hasMore: boolean;
69
+ }>;
70
+ paginate<Args extends PaginateArgs<DatabaseSchemaType, ColumnNameToColumnType>>(args?: Args): Promise<{
71
+ data: (Partial<DatabaseSchemaType> & {
72
+ id: string;
73
+ } & IconCoverResult<Args>)[];
74
+ nextCursor: string | null;
75
+ hasMore: boolean;
76
+ }>;
77
+ /** Find the first matching record, or null if none found. */
78
+ findFirst<S extends Partial<Record<keyof DatabaseSchemaType, true>>, Args extends Omit<FindManyArgs<DatabaseSchemaType, ColumnNameToColumnType>, "stream" | "select"> & {
79
+ select: S;
80
+ }>(args: Args): Promise<({
81
+ [K in Extract<keyof S, keyof DatabaseSchemaType>]?: DatabaseSchemaType[K];
82
+ } & {
83
+ id: string;
84
+ } & IconCoverResult<Args>) | null>;
85
+ findFirst<O extends Partial<Record<keyof DatabaseSchemaType, true>>, Args extends Omit<FindManyArgs<DatabaseSchemaType, ColumnNameToColumnType>, "stream" | "omit"> & {
86
+ omit: O;
87
+ }>(args: Args): Promise<({
88
+ [K in Exclude<keyof DatabaseSchemaType, keyof O>]?: DatabaseSchemaType[K];
89
+ } & {
90
+ id: string;
91
+ } & IconCoverResult<Args>) | null>;
92
+ findFirst<Args extends Omit<FindManyArgs<DatabaseSchemaType, ColumnNameToColumnType>, "stream">>(args?: Args): Promise<(Partial<DatabaseSchemaType> & {
93
+ id: string;
94
+ } & IconCoverResult<Args>) | null>;
95
+ /** Find a record by its Notion page ID. Returns null if not found. */
96
+ findUnique<Args extends {
97
+ where: {
98
+ id: string;
99
+ };
100
+ select?: Partial<Record<keyof DatabaseSchemaType, true>>;
101
+ omit?: Partial<Record<keyof DatabaseSchemaType, true>>;
102
+ $icon?: true;
103
+ $cover?: true;
104
+ }>(args: Args): Promise<(Partial<DatabaseSchemaType> & {
105
+ id: string;
106
+ } & IconCoverResult<Args>) | null>;
107
+ /** Create a new record and return it. */
108
+ create(args: {
109
+ data: DatabaseSchemaType;
110
+ $icon?: string;
111
+ $cover?: string;
112
+ }): Promise<Partial<DatabaseSchemaType> & {
113
+ id: string;
114
+ }>;
115
+ /** Create multiple records and return them. */
116
+ createMany(args: {
117
+ data: DatabaseSchemaType[];
118
+ $icon?: string;
119
+ $cover?: string;
120
+ }): Promise<(Partial<DatabaseSchemaType> & {
121
+ id: string;
122
+ })[]>;
123
+ /** Update a record by its Notion page ID. */
124
+ update(args: {
125
+ where: {
126
+ id: string;
127
+ };
128
+ data: Partial<DatabaseSchemaType>;
129
+ $icon?: string;
130
+ $cover?: string;
131
+ }): Promise<void>;
132
+ /** Update all records matching the filter. Returns the count of updated records. */
133
+ updateMany(args: {
134
+ where?: QueryFilter<DatabaseSchemaType, ColumnNameToColumnType>;
135
+ data: Partial<DatabaseSchemaType>;
136
+ $icon?: string;
137
+ $cover?: string;
138
+ }): Promise<{
139
+ count: number;
140
+ }>;
141
+ /** Create or update a record. If a record matches `where`, updates it; otherwise creates it. */
142
+ upsert(args: {
143
+ where: QueryFilter<DatabaseSchemaType, ColumnNameToColumnType>;
144
+ create: DatabaseSchemaType;
145
+ update: Partial<DatabaseSchemaType>;
146
+ $icon?: string;
147
+ $cover?: string;
148
+ }): Promise<{
149
+ created: boolean;
150
+ id: string;
151
+ }>;
152
+ /** Archive (soft-delete) a record by its Notion page ID. */
153
+ delete(args: {
154
+ where: {
155
+ id: string;
156
+ };
157
+ }): Promise<void>;
158
+ /** Archive all records matching the filter. Returns the count of deleted records. */
159
+ deleteMany(args?: {
160
+ where?: QueryFilter<DatabaseSchemaType, ColumnNameToColumnType>;
161
+ }): Promise<{
162
+ count: number;
163
+ }>;
164
+ /** Count records matching the filter. */
165
+ count(args?: {
166
+ where?: QueryFilter<DatabaseSchemaType, ColumnNameToColumnType>;
167
+ }): Promise<number>;
168
+ private buildCreateBody;
169
+ private parsePage;
170
+ private buildQueryCall;
171
+ private fetchAllPages;
172
+ private streamingIterable;
173
+ private applySelectOmit;
174
+ private validateDatabaseSchema;
175
+ }
@@ -1,10 +1,13 @@
1
1
  import type { QueryDataSourceResponse } from "@notionhq/client/build/src/api-endpoints";
2
- import type { apiFilterType, QueryFilter, SupportedNotionColumnType } from "./queryTypes";
3
- import type { camelPropertyNameToNameAndTypeMapType } from "./DatabaseClient";
2
+ import type { apiFilterType, QueryFilter, SupportedNotionColumnType } from "./types";
3
+ import type { camelPropertyNameToNameAndTypeMapType } from "./client";
4
4
  /**
5
5
  * Transforms Notion API query response into simplified format
6
6
  */
7
- export declare function buildQueryResponse<DatabaseSchemaType extends Record<string, any>>(res: QueryDataSourceResponse, camelPropertyNameToNameAndTypeMap: camelPropertyNameToNameAndTypeMapType, validateSchema: (result: Partial<DatabaseSchemaType>) => void): (Partial<DatabaseSchemaType> & {
7
+ export declare function buildQueryResponse<DatabaseSchemaType extends Record<string, any>>(res: QueryDataSourceResponse, camelPropertyNameToNameAndTypeMap: camelPropertyNameToNameAndTypeMapType, validateSchema: (result: Partial<DatabaseSchemaType>) => void, meta?: {
8
+ $icon?: true;
9
+ $cover?: true;
10
+ }): (Partial<DatabaseSchemaType> & {
8
11
  id: string;
9
12
  })[];
10
13
  /**
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Column types' for all query options
3
3
  */
4
- import type { DataSourceObjectResponse, QueryDataSourceParameters } from "@notionhq/client/build/src/api-endpoints";
4
+ import type { DataSourceObjectResponse } from "@notionhq/client/build/src/api-endpoints";
5
5
  type NotionPropertyTypeToConfigMap = DataSourceObjectResponse["properties"];
6
6
  export type DatabasePropertyType = NotionPropertyTypeToConfigMap[keyof NotionPropertyTypeToConfigMap]["type"];
7
7
  export declare const SUPPORTED_PROPERTY_TYPES: {
@@ -111,16 +111,37 @@ export type CompoundFilters<Y extends Record<string, any>, T extends Record<keyo
111
111
  or: Array<SingleFilter<Y, T> | CompoundFilters<Y, T>>;
112
112
  };
113
113
  export type QueryFilter<Y extends Record<string, any>, T extends Record<keyof Y, SupportedNotionColumnType>> = SingleFilter<Y, T> | CompoundFilters<Y, T>;
114
- export type Query<Y extends Record<string, any>, T extends Record<keyof Y, SupportedNotionColumnType>> = {
115
- filter?: QueryFilter<Y, T>;
116
- sort?: QueryDataSourceParameters["sorts"];
114
+ /** Prisma-style orderBy: `{ fieldName: 'asc' | 'desc' }` or an array of those */
115
+ export type OrderByInput<Y> = {
116
+ [K in keyof Y]?: "asc" | "desc";
117
+ } | {
118
+ [K in keyof Y]?: "asc" | "desc";
119
+ }[];
120
+ export type FindManyArgs<Y extends Record<string, any>, T extends Record<keyof Y, SupportedNotionColumnType>> = {
121
+ where?: QueryFilter<Y, T>;
122
+ orderBy?: OrderByInput<Y>;
117
123
  select?: Partial<Record<keyof Y, true>>;
118
124
  omit?: Partial<Record<keyof Y, true>>;
119
- limit?: number;
120
- pagination?: {
121
- pageSize: number;
122
- };
123
- };
125
+ take?: number;
126
+ skip?: number;
127
+ /** When set, findMany returns an AsyncIterable, fetching results in batches of this size */
128
+ stream?: number;
129
+ $icon?: true;
130
+ $cover?: true;
131
+ };
132
+ export type PaginateArgs<Y extends Record<string, any>, T extends Record<keyof Y, SupportedNotionColumnType>> = {
133
+ where?: QueryFilter<Y, T>;
134
+ orderBy?: OrderByInput<Y>;
135
+ select?: Partial<Record<keyof Y, true>>;
136
+ omit?: Partial<Record<keyof Y, true>>;
137
+ take?: number;
138
+ /** Opaque cursor token returned by a previous paginate() call */
139
+ after?: string;
140
+ $icon?: true;
141
+ $cover?: true;
142
+ };
143
+ /** @deprecated Use FindManyArgs */
144
+ export type Query<Y extends Record<string, any>, T extends Record<keyof Y, SupportedNotionColumnType>> = FindManyArgs<Y, T>;
124
145
  export type apiFilterQuery = {
125
146
  filter?: apiSingleFilter | apiAndFilter | apiOrFilter;
126
147
  };
@@ -144,6 +165,15 @@ type apiOrFilter = {
144
165
  export type QueryResult<DatabaseSchema> = Partial<DatabaseSchema> & {
145
166
  id: string;
146
167
  };
168
+ export type IconCoverResult<Args> = (Args extends {
169
+ $icon: true;
170
+ } ? {
171
+ $icon: string | null;
172
+ } : unknown) & (Args extends {
173
+ $cover: true;
174
+ } ? {
175
+ $cover: string | null;
176
+ } : unknown);
147
177
  export type QueryResultType<Y, Args> = Args extends {
148
178
  select: infer S extends Record<string, unknown>;
149
179
  } ? {