From 8d48d2f6aefdf73d7b8fe3912c288f473908bbb6 Mon Sep 17 00:00:00 2001 From: Thorsten Schaeff Date: Tue, 11 Aug 2020 03:00:19 +0800 Subject: [PATCH 01/11] [WIP] Add type definitions. --- src/index.d.ts | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/index.d.ts diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 00000000..5f87f4da --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,80 @@ +declare module '@supabase/supabase-js' { + interface User { + app_metadata: { + provider: 'email' + [key: string]: any + } + user_metadata: { + [key: string]: any + } + aud: string + created_at: string + confirmed_at: string + email: string + id: string + last_sign_in_at: string + role: string + updated_at: string + } + + interface AuthResponse { + status: number + body: { + user: User + access_token: string + refresh_token: string + expires_in: number + unauthorized: boolean + } + } + + interface Auth { + /** + * Allow your users to sign up and create a new account. + * After they have signed up, all interactions using the Supabase JS client will be performed as "that user". + */ + signup: (email: string, password: string) => Promise + /** + * If an account is created, users can login to your app. + * After they have logged in, all interactions using the Supabase JS client will be performed as "that user". + */ + login: (email: string, password: string) => Promise + /** + * Get the JSON data for the logged in user. + */ + user: () => Promise + /** + * After calling log out, all interactions using the Supabase JS client will be "anonymous". + */ + logout: () => Promise + } + + interface SupabaseClient { + /** + * Supabase Auth allows you to create and manage user sessions for access to data that is secured by access policies. + */ + auth: Auth + } + + const createClient: ( + /** + * The unique Supabase URL which is supplied when you create a new project in your project dashboard. + */ + supabaseUrl: string, + /** + * The unique Supabase Key which is supplied when you create a new project in your project dashboard. + */ + supabaseKey: string, + options?: { + autoRefreshToken: boolean + /** + * You can switch in between schemas. + * The schema however would need to be on the list of exposed schemas. + * Defaults to the 'public' schema. + * If there is the need to use more than one schema, + * another instance of .createClient() would need to be instantiated. + */ + schema: string + } + ) => SupabaseClient +} From 81f6578f8e700475c1b067b528a854bcad9a5bfd Mon Sep 17 00:00:00 2001 From: Thorsten Schaeff Date: Tue, 11 Aug 2020 22:55:17 +0800 Subject: [PATCH 02/11] Extended SupabaseClient --- src/index.d.ts | 102 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/src/index.d.ts b/src/index.d.ts index 5f87f4da..c4f3709b 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -49,11 +49,111 @@ declare module '@supabase/supabase-js' { logout: () => Promise } + enum FilterOperator { + Equal = 'eq', + NotEqual = 'neq', + GreaterThan = 'gt', + LessThan = 'lt', + GreaterThanOrEqual = 'gte', + LessThanOrEqual = 'lte', + /** Finds all rows whose value in the stated columnName matches the supplied pattern. */ + Like = 'like', + /** A case-sensitive version of `Like`. */ + ILike = 'ilike', + /** A check for exact equality (null, true, false) */ + Is = 'is', + /** ('name', 'in', ['Rio de Janeiro', 'San Francisco']) */ + In = 'in', + /** Finds all rows whose json, array, or range value on the stated columnName contains the items specified in the filterObject */ + Contains = 'cs', + Contained = 'cd', + OverlapsArray = 'ova', + OverlapsRange = 'ovr', + StrictlyLeft = 'sl', + StrictlyRight = 'sr', + NotExtendLeft = 'nxl', + NotExtendRight = 'nxr', + Adjacent = 'adj', + } + const FilterOperatorString: + | 'eq' + | 'neq' + | 'gt' + | 'lt' + | 'gte' + | 'lte' + | 'like' + | 'ilike' + | 'is' + | 'in' + | 'cs' + | 'cd' + | 'ova' + | 'ovr' + | 'sl' + | 'sr' + | 'nxr' + | 'nxl' + | 'adj' + + interface PostgrestClient {} interface SupabaseClient { /** * Supabase Auth allows you to create and manage user sessions for access to data that is secured by access policies. */ auth: Auth + /** + * Name of the database table that will be read from. + */ + from(tableName: string): SupabaseClient + /** + * This allows you to apply various filters on your query. Filters can also be chained together. + * Example: `.filter('name', 'eq', 'Paris')` + */ + filter( + /** Name of the database column. */ + columnName: string, + /** Name of filter operator to be utilised. */ + operator: FilterOperator, + /** Value to compare to. Exact data type of criteria depends on the operator used. */ + criteria: any + ): SupabaseClient + /** + * Reverse of .filter(). Returns rows that do not meet the criteria specified using the columnName and operator provided. + * Example: `.not('name', 'eq', 'Paris')` + */ + not( + /** Name of the database column. */ + columnName: string, + /** Name of filter operator to be utilised. */ + operator: FilterOperator, + /** Value to compare to. Exact data type of criteria depends on the operator used. */ + criteria: any + ): SupabaseClient + /** + * Finds rows that exactly match the specified filterObject. Equivalent of multiple `filter('columnName', 'eq', criteria)`. + */ + match( + /** Example: `.match({name: 'Beijing', country_id: 156})` */ + filterObject: { [columnName: string]: any } + ): SupabaseClient + /** + * Orders your data before fetching. + */ + order( + /** Name of chosen column to base the order on. */ + columnName: string, + /** Specifies whether the order will be ascending or descending. Default is false */ + sortAscending?: boolean = false, + /** Specifies whether null values will be displayed first. Default is false */ + nullsFirst?: boolean = false + ): SupabaseClient + /** + * Finds all rows whose value on the stated columnName exactly matches the specified filterValue. Equivalent of filter(columnName, 'eq', criteria). + * + * Example: `.eq('name', 'San Francisco')` + */ + eq(columnName: string, filterValue: string | integer | boolean): SupabaseClient } const createClient: ( @@ -66,7 +166,7 @@ declare module '@supabase/supabase-js' { */ supabaseKey: string, options?: { - autoRefreshToken: boolean + autoRefreshToken?: boolean = true /** * You can switch in between schemas. * The schema however would need to be on the list of exposed schemas. From 67983367f0d389eb61993b4389f0161b28ca6bc6 Mon Sep 17 00:00:00 2001 From: Thorsten Schaeff Date: Wed, 12 Aug 2020 02:16:49 +0800 Subject: [PATCH 03/11] Add PostgrestClient --- src/index.d.ts | 52 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index c4f3709b..8c78feb7 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -96,8 +96,30 @@ declare module '@supabase/supabase-js' { | 'nxl' | 'adj' - interface PostgrestClient {} - interface SupabaseClient { + interface PostgrestResponse { + body: T + status: number + statusCode: number + statusText: string + } + + interface PostgrestClient extends Promise> { + /** A comma separated list of columns. For example select('id, name') */ + select(columnQuery: string = '*'): PostgrestClient + /** Result must be single object, otherwise returns `406 Not Acceptable` */ + single(): PostgrestClient // TODO make chaining order independent + /** + * Limit the amount of records to be returned. + */ + limit( + /** Specifies number of items to be returned at most. */ + criteria: number, + /** Name of chosen foreignTable to apply the limit on. Used if foreign tables are present. */ + foreignTableName?: string | null + ): PostgrestResponse + } + + interface SupabaseClient extends PostgrestClient { /** * Supabase Auth allows you to create and manage user sessions for access to data that is secured by access policies. */ @@ -105,55 +127,55 @@ declare module '@supabase/supabase-js' { /** * Name of the database table that will be read from. */ - from(tableName: string): SupabaseClient + from(tableName: string): SupabaseClient /** * This allows you to apply various filters on your query. Filters can also be chained together. * Example: `.filter('name', 'eq', 'Paris')` */ filter( /** Name of the database column. */ - columnName: string, + columnName: keyof T, /** Name of filter operator to be utilised. */ operator: FilterOperator, /** Value to compare to. Exact data type of criteria depends on the operator used. */ - criteria: any - ): SupabaseClient + criteria: T[keyof T] + ): SupabaseClient /** * Reverse of .filter(). Returns rows that do not meet the criteria specified using the columnName and operator provided. * Example: `.not('name', 'eq', 'Paris')` */ not( /** Name of the database column. */ - columnName: string, + columnName: keyof T, /** Name of filter operator to be utilised. */ operator: FilterOperator, /** Value to compare to. Exact data type of criteria depends on the operator used. */ - criteria: any - ): SupabaseClient + criteria: T[keyof T] + ): SupabaseClient /** * Finds rows that exactly match the specified filterObject. Equivalent of multiple `filter('columnName', 'eq', criteria)`. */ match( /** Example: `.match({name: 'Beijing', country_id: 156})` */ - filterObject: { [columnName: string]: any } - ): SupabaseClient + filterObject: { [columnName: keyof T]: T[keyof T] } + ): SupabaseClient /** * Orders your data before fetching. */ order( /** Name of chosen column to base the order on. */ - columnName: string, + columnName: keyof T, /** Specifies whether the order will be ascending or descending. Default is false */ sortAscending?: boolean = false, /** Specifies whether null values will be displayed first. Default is false */ nullsFirst?: boolean = false - ): SupabaseClient + ): SupabaseClient /** * Finds all rows whose value on the stated columnName exactly matches the specified filterValue. Equivalent of filter(columnName, 'eq', criteria). * * Example: `.eq('name', 'San Francisco')` */ - eq(columnName: string, filterValue: string | integer | boolean): SupabaseClient + eq(columnName: keyof T, filterValue: T[keyof T]): SupabaseClient } const createClient: ( @@ -176,5 +198,5 @@ declare module '@supabase/supabase-js' { */ schema: string } - ) => SupabaseClient + ) => SupabaseClient } From ce9dc1103517abd57b56bb831000f04b94cf0a44 Mon Sep 17 00:00:00 2001 From: Thorsten Schaeff Date: Wed, 12 Aug 2020 18:12:02 +0800 Subject: [PATCH 04/11] Query functions. --- src/index.d.ts | 127 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 93 insertions(+), 34 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 8c78feb7..284277a7 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -75,7 +75,7 @@ declare module '@supabase/supabase-js' { NotExtendRight = 'nxr', Adjacent = 'adj', } - const FilterOperatorString: + type FilterOperatorString = | 'eq' | 'neq' | 'gt' @@ -97,37 +97,15 @@ declare module '@supabase/supabase-js' { | 'adj' interface PostgrestResponse { - body: T + body: T[] | null status: number statusCode: number statusText: string } - interface PostgrestClient extends Promise> { - /** A comma separated list of columns. For example select('id, name') */ - select(columnQuery: string = '*'): PostgrestClient - /** Result must be single object, otherwise returns `406 Not Acceptable` */ - single(): PostgrestClient // TODO make chaining order independent - /** - * Limit the amount of records to be returned. - */ - limit( - /** Specifies number of items to be returned at most. */ - criteria: number, - /** Name of chosen foreignTable to apply the limit on. Used if foreign tables are present. */ - foreignTableName?: string | null - ): PostgrestResponse - } + interface PostgrestClient extends Promise> {} - interface SupabaseClient extends PostgrestClient { - /** - * Supabase Auth allows you to create and manage user sessions for access to data that is secured by access policies. - */ - auth: Auth - /** - * Name of the database table that will be read from. - */ - from(tableName: string): SupabaseClient + interface PostgrestFilterClient extends PostgrestClient { /** * This allows you to apply various filters on your query. Filters can also be chained together. * Example: `.filter('name', 'eq', 'Paris')` @@ -136,10 +114,10 @@ declare module '@supabase/supabase-js' { /** Name of the database column. */ columnName: keyof T, /** Name of filter operator to be utilised. */ - operator: FilterOperator, + operator: FilterOperator | FilterOperatorString, /** Value to compare to. Exact data type of criteria depends on the operator used. */ criteria: T[keyof T] - ): SupabaseClient + ): PostgrestFilterClient /** * Reverse of .filter(). Returns rows that do not meet the criteria specified using the columnName and operator provided. * Example: `.not('name', 'eq', 'Paris')` @@ -151,14 +129,42 @@ declare module '@supabase/supabase-js' { operator: FilterOperator, /** Value to compare to. Exact data type of criteria depends on the operator used. */ criteria: T[keyof T] - ): SupabaseClient + ): PostgrestFilterClient /** * Finds rows that exactly match the specified filterObject. Equivalent of multiple `filter('columnName', 'eq', criteria)`. */ match( /** Example: `.match({name: 'Beijing', country_id: 156})` */ filterObject: { [columnName: keyof T]: T[keyof T] } - ): SupabaseClient + ): PostgrestFilterClient + /** + * Finds all rows whose value on the stated columnName exactly matches the specified filterValue. Equivalent of filter(columnName, 'eq', criteria). + * + * Example: `.eq('name', 'San Francisco')` + */ + eq(columnName: keyof T, filterValue: T[keyof T]): PostgrestFilterClient + } + + interface PostgrestReadClient extends PostgrestFilterClient { + /** Result must be single object, otherwise returns `406 Not Acceptable` */ + single(): PostgrestReadClient + /** + * Limit the amount of records to be returned. + */ + limit( + /** Specifies number of items to be returned at most. */ + criteria: number, + /** Name of chosen foreignTable to apply the limit on. Used if foreign tables are present. */ + foreignTableName?: string | null + ): PostgrestReadClient + /** + * Skip a number of rows before returning rows. + */ + offset( + /** Index or position of the start of the specified range. */ + skipCount: number, + foreignTableName?: string | null + ): PostgrestReadClient /** * Orders your data before fetching. */ @@ -169,13 +175,66 @@ declare module '@supabase/supabase-js' { sortAscending?: boolean = false, /** Specifies whether null values will be displayed first. Default is false */ nullsFirst?: boolean = false - ): SupabaseClient + ): PostgrestReadClient /** - * Finds all rows whose value on the stated columnName exactly matches the specified filterValue. Equivalent of filter(columnName, 'eq', criteria). + * Paginates your request. + */ + range( + /** Index or position of the start of the specified range. */ + fromIndex: number, + /** Index or position of the end of the specified range. If not stated, all remaining rows after the starting index will be returned. */ + toIndex?: number + ): PostgrestReadClient + } + + interface SupabaseQueryClient { + /** + * Read data. + */ + select( + /** A comma separated list of columns. For example `.select('id, name')` */ + columnQuery: string = '*' + ): PostgrestReadClient + /** + * Insert or upsert data. + */ + insert( + /** + * A single object or an array of rows of type object which contain information to be saved into the selected table. + */ + data: T | T[], + /** + * For upsert, if set to true, primary key columns would need to be included in the data parameter in order for an update to properly happen. Also, primary keys used must be natural, not surrogate. + */ + options?: { upsert: bollean = false } + ): PostgrestClient + /** + * Update data. * - * Example: `.eq('name', 'San Francisco')` + * It is to note that it is required to apply filters when using `.update()`. Not using filters would result in an error. + * + * Example: `supabase.from('cities').update({ name: 'Middle Earth' }).match({ name: 'Auckland' })` + */ + update(data: T): PostgrestFilterClient + /** + * Delete data. + * + * It is to note that it is required to apply filters when using `.delete()`. Not using filters would result in an error. + * + * Example: `supabase.from('cities').delete().match({ name: 'Bielefeld' })` + */ + delete(): PostgrestFilterClient + } + + interface SupabaseClient { + /** + * Supabase Auth allows you to create and manage user sessions for access to data that is secured by access policies. + */ + auth: Auth + /** + * Name of the database table to perform an operation on. */ - eq(columnName: keyof T, filterValue: T[keyof T]): SupabaseClient + from(tableName: string): SupabaseQueryClient } const createClient: ( From 3d6b5ae0999d59e56a8736c56604f60c7ba06395 Mon Sep 17 00:00:00 2001 From: Thorsten Schaeff Date: Wed, 12 Aug 2020 19:24:43 +0800 Subject: [PATCH 05/11] Add rpc. --- src/index.d.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/index.d.ts b/src/index.d.ts index 284277a7..8f73d9aa 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -235,6 +235,15 @@ declare module '@supabase/supabase-js' { * Name of the database table to perform an operation on. */ from(tableName: string): SupabaseQueryClient + /** + * Stored procedures. + */ + rpc( + /** Name of stored function in the database. */ + functionName: string, + /** Parameters to be passed to the stored function. */ + functionParameters?: object | object[] + ): PostgrestFilterClient } const createClient: ( From 6cd1ee360e467ae27f6c3c81f031adc8a16f6f35 Mon Sep 17 00:00:00 2001 From: Thorsten Schaeff Date: Wed, 12 Aug 2020 20:35:02 +0800 Subject: [PATCH 06/11] Fix .single response type. --- src/index.d.ts | 73 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 19 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 8f73d9aa..b89f47fe 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -103,9 +103,14 @@ declare module '@supabase/supabase-js' { statusText: string } - interface PostgrestClient extends Promise> {} + interface PostgrestSingleResponse { + body: T | null + status: number + statusCode: number + statusText: string + } - interface PostgrestFilterClient extends PostgrestClient { + interface PostgrestClient extends Promise> { /** * This allows you to apply various filters on your query. Filters can also be chained together. * Example: `.filter('name', 'eq', 'Paris')` @@ -117,7 +122,7 @@ declare module '@supabase/supabase-js' { operator: FilterOperator | FilterOperatorString, /** Value to compare to. Exact data type of criteria depends on the operator used. */ criteria: T[keyof T] - ): PostgrestFilterClient + ): PostgrestClient /** * Reverse of .filter(). Returns rows that do not meet the criteria specified using the columnName and operator provided. * Example: `.not('name', 'eq', 'Paris')` @@ -129,25 +134,26 @@ declare module '@supabase/supabase-js' { operator: FilterOperator, /** Value to compare to. Exact data type of criteria depends on the operator used. */ criteria: T[keyof T] - ): PostgrestFilterClient + ): PostgrestClient /** * Finds rows that exactly match the specified filterObject. Equivalent of multiple `filter('columnName', 'eq', criteria)`. */ match( /** Example: `.match({name: 'Beijing', country_id: 156})` */ filterObject: { [columnName: keyof T]: T[keyof T] } - ): PostgrestFilterClient + ): PostgrestClient /** * Finds all rows whose value on the stated columnName exactly matches the specified filterValue. Equivalent of filter(columnName, 'eq', criteria). * * Example: `.eq('name', 'San Francisco')` */ - eq(columnName: keyof T, filterValue: T[keyof T]): PostgrestFilterClient - } - - interface PostgrestReadClient extends PostgrestFilterClient { - /** Result must be single object, otherwise returns `406 Not Acceptable` */ - single(): PostgrestReadClient + eq(columnName: keyof T, filterValue: T[keyof T]): PostgrestClient + /** + * Return a single object as response body. Result must be single object, otherwise returns `406 Not Acceptable`. + * + * Note: this must be called at the end of your query chain! + */ + single(): Promise> /** * Limit the amount of records to be returned. */ @@ -156,7 +162,7 @@ declare module '@supabase/supabase-js' { criteria: number, /** Name of chosen foreignTable to apply the limit on. Used if foreign tables are present. */ foreignTableName?: string | null - ): PostgrestReadClient + ): PostgrestClient /** * Skip a number of rows before returning rows. */ @@ -164,7 +170,7 @@ declare module '@supabase/supabase-js' { /** Index or position of the start of the specified range. */ skipCount: number, foreignTableName?: string | null - ): PostgrestReadClient + ): PostgrestClient /** * Orders your data before fetching. */ @@ -175,7 +181,7 @@ declare module '@supabase/supabase-js' { sortAscending?: boolean = false, /** Specifies whether null values will be displayed first. Default is false */ nullsFirst?: boolean = false - ): PostgrestReadClient + ): PostgrestClient /** * Paginates your request. */ @@ -184,7 +190,24 @@ declare module '@supabase/supabase-js' { fromIndex: number, /** Index or position of the end of the specified range. If not stated, all remaining rows after the starting index will be returned. */ toIndex?: number - ): PostgrestReadClient + ): PostgrestClient + } + + interface SupabaseRealtimeClient { + subscribe(): SupabaseRealtimeClient + unsubscribe(): SupabaseRealtimeClient + schema: string + tableName: string + uuid: string + } + + interface SupabaseRealtimePayload { + commit_timestamp: string + eventType: 'INSERT' | 'UPDATE' | 'DELETE' + new: T + old: T + schema: string + table: string } interface SupabaseQueryClient { @@ -194,7 +217,7 @@ declare module '@supabase/supabase-js' { select( /** A comma separated list of columns. For example `.select('id, name')` */ columnQuery: string = '*' - ): PostgrestReadClient + ): PostgrestClient /** * Insert or upsert data. */ @@ -215,7 +238,7 @@ declare module '@supabase/supabase-js' { * * Example: `supabase.from('cities').update({ name: 'Middle Earth' }).match({ name: 'Auckland' })` */ - update(data: T): PostgrestFilterClient + update(data: T): PostgrestClient /** * Delete data. * @@ -223,7 +246,15 @@ declare module '@supabase/supabase-js' { * * Example: `supabase.from('cities').delete().match({ name: 'Bielefeld' })` */ - delete(): PostgrestFilterClient + delete(): PostgrestClient + /** + * Subscribe to realtime changes in your databse. + */ + on( + /** The database event which you would like to receive updates for, or you can use the special wildcard `*` to listen to all changes. */ + eventType: 'INSERT' | 'UPDATE' | 'DELETE' | '*', + callbackFunction: (payload: SupabaseRealtimePayload) => void + ): SupabaseRealtimeClient } interface SupabaseClient { @@ -243,7 +274,11 @@ declare module '@supabase/supabase-js' { functionName: string, /** Parameters to be passed to the stored function. */ functionParameters?: object | object[] - ): PostgrestFilterClient + ): PostgrestClient + /** Remove a subscription. */ + removeSubscription(reference: SupabaseRealtimeClient): void + /** List of all subscriptions. */ + getSubscriptions(): SupabaseRealtimeClient[] } const createClient: ( From ce2b1263e22a16a5f12b953cec8b4d977b0c2260 Mon Sep 17 00:00:00 2001 From: Thorsten Schaeff Date: Wed, 12 Aug 2020 21:02:07 +0800 Subject: [PATCH 07/11] Fix problems --- src/index.d.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index b89f47fe..2eb4808d 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -140,7 +140,7 @@ declare module '@supabase/supabase-js' { */ match( /** Example: `.match({name: 'Beijing', country_id: 156})` */ - filterObject: { [columnName: keyof T]: T[keyof T] } + filterObject: { [columnName: string]: T[keyof T] } ): PostgrestClient /** * Finds all rows whose value on the stated columnName exactly matches the specified filterValue. Equivalent of filter(columnName, 'eq', criteria). @@ -178,9 +178,9 @@ declare module '@supabase/supabase-js' { /** Name of chosen column to base the order on. */ columnName: keyof T, /** Specifies whether the order will be ascending or descending. Default is false */ - sortAscending?: boolean = false, + sortAscending?: boolean, /** Specifies whether null values will be displayed first. Default is false */ - nullsFirst?: boolean = false + nullsFirst?: boolean ): PostgrestClient /** * Paginates your request. @@ -215,8 +215,12 @@ declare module '@supabase/supabase-js' { * Read data. */ select( - /** A comma separated list of columns. For example `.select('id, name')` */ - columnQuery: string = '*' + /** + * A comma separated list of columns. For example `.select('id, name')`. + * + * Omitting `columnQuery` is equal to `.select('*'). + */ + columnQuery?: string ): PostgrestClient /** * Insert or upsert data. @@ -229,7 +233,7 @@ declare module '@supabase/supabase-js' { /** * For upsert, if set to true, primary key columns would need to be included in the data parameter in order for an update to properly happen. Also, primary keys used must be natural, not surrogate. */ - options?: { upsert: bollean = false } + options?: { upsert?: boolean } ): PostgrestClient /** * Update data. @@ -257,7 +261,7 @@ declare module '@supabase/supabase-js' { ): SupabaseRealtimeClient } - interface SupabaseClient { + interface SupabaseClient { /** * Supabase Auth allows you to create and manage user sessions for access to data that is secured by access policies. */ @@ -291,7 +295,7 @@ declare module '@supabase/supabase-js' { */ supabaseKey: string, options?: { - autoRefreshToken?: boolean = true + autoRefreshToken?: boolean /** * You can switch in between schemas. * The schema however would need to be on the list of exposed schemas. @@ -301,5 +305,5 @@ declare module '@supabase/supabase-js' { */ schema: string } - ) => SupabaseClient + ) => SupabaseClient } From 6091b9eda2523aa6a3a5f07e879ee2291bc675f3 Mon Sep 17 00:00:00 2001 From: Thorsten Schaeff Date: Wed, 12 Aug 2020 23:35:40 +0800 Subject: [PATCH 08/11] Polish --- src/index.d.ts | 290 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 209 insertions(+), 81 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 2eb4808d..eb6ba65e 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,4 +1,65 @@ declare module '@supabase/supabase-js' { + enum FilterOperator { + /** Finds all rows whose value on the stated columnName exactly matches the specified filterValue. */ + Equal = 'eq', + /** Finds all rows whose value on the stated columnName does not match the specified filterValue. */ + NotEqual = 'neq', + /** Finds all rows whose value on the stated columnName is greater than the specified filterValue. */ + GreaterThan = 'gt', + /** Finds all rows whose value on the stated columnName is less than the specified filterValue. */ + LessThan = 'lt', + /** Finds all rows whose value on the stated columnName is greater than or equal to the specified filterValue. */ + GreaterThanOrEqual = 'gte', + /** Finds all rows whose value on the stated columnName is less than or equal to the specified filterValue. */ + LessThanOrEqual = 'lte', + /** Finds all rows whose value in the stated columnName matches the supplied pattern. */ + Like = 'like', + /** A case-sensitive version of `Like`. */ + ILike = 'ilike', + /** A check for exact equality (null, true, false) */ + Is = 'is', + /** Finds all rows whose value on the stated columnName is found on the specified filterArray. */ + In = 'in', + /** Finds all rows whose json, array, or range value on the stated columnName contains the items specified in the filterObject. */ + Contains = 'cs', + /** Finds all rows whose json, array, or range value on the stated columnName is contained by the specific filterObject. */ + Contained = 'cd', + /** Finds all rows whose array value on the stated columnName overlaps with the specified filterArray. */ + OverlapsArray = 'ova', + /** Finds all rows whose range value on the stated columnName overlaps with the specified filterRange. */ + OverlapsRange = 'ovr', + /** Finds all rows whose range value on the stated columnName is strictly on the left hand side of the specified filterRange. */ + StrictlyLeft = 'sl', + /** Finds all rows whose range value on the stated columnName is strictly on the right hand side of the specified filterRange. */ + StrictlyRight = 'sr', + /** Finds all rows whose range value on the stated columnName does not extend to the left of the specified filterRange. */ + NotExtendLeft = 'nxl', + /** Finds all rows whose range value on the stated columnName does not extend to the right of the specified filterRange. */ + NotExtendRight = 'nxr', + /** Finds all rows whose range value on the stated columnName is adjacent to the specified filterRange. */ + Adjacent = 'adj', + } + type FilterOperatorString = + | 'eq' + | 'neq' + | 'gt' + | 'lt' + | 'gte' + | 'lte' + | 'like' + | 'ilike' + | 'is' + | 'in' + | 'cs' + | 'cd' + | 'ova' + | 'ovr' + | 'sl' + | 'sr' + | 'nxr' + | 'nxl' + | 'adj' + interface User { app_metadata: { provider: 'email' @@ -49,53 +110,6 @@ declare module '@supabase/supabase-js' { logout: () => Promise } - enum FilterOperator { - Equal = 'eq', - NotEqual = 'neq', - GreaterThan = 'gt', - LessThan = 'lt', - GreaterThanOrEqual = 'gte', - LessThanOrEqual = 'lte', - /** Finds all rows whose value in the stated columnName matches the supplied pattern. */ - Like = 'like', - /** A case-sensitive version of `Like`. */ - ILike = 'ilike', - /** A check for exact equality (null, true, false) */ - Is = 'is', - /** ('name', 'in', ['Rio de Janeiro', 'San Francisco']) */ - In = 'in', - /** Finds all rows whose json, array, or range value on the stated columnName contains the items specified in the filterObject */ - Contains = 'cs', - Contained = 'cd', - OverlapsArray = 'ova', - OverlapsRange = 'ovr', - StrictlyLeft = 'sl', - StrictlyRight = 'sr', - NotExtendLeft = 'nxl', - NotExtendRight = 'nxr', - Adjacent = 'adj', - } - type FilterOperatorString = - | 'eq' - | 'neq' - | 'gt' - | 'lt' - | 'gte' - | 'lte' - | 'like' - | 'ilike' - | 'is' - | 'in' - | 'cs' - | 'cd' - | 'ova' - | 'ovr' - | 'sl' - | 'sr' - | 'nxr' - | 'nxl' - | 'adj' - interface PostgrestResponse { body: T[] | null status: number @@ -111,6 +125,49 @@ declare module '@supabase/supabase-js' { } interface PostgrestClient extends Promise> { + /** + * Return a single object as response body. Result must be single object, otherwise returns `406 Not Acceptable`. + * + * Note: this must be called at the end of your query chain! + */ + single(): Promise> + /** + * Limit the amount of records to be returned. + */ + limit( + /** Specifies number of items to be returned at most. */ + criteria: number, + /** Name of chosen foreignTable to apply the limit on. Used if foreign tables are present. */ + foreignTableName?: string | null + ): PostgrestClient + /** + * Skip a number of rows before returning rows. + */ + offset( + /** Index or position of the start of the specified range. */ + skipCount: number, + foreignTableName?: string | null + ): PostgrestClient + /** + * Orders your data before fetching. + */ + order( + /** Name of chosen column to base the order on. */ + columnName: keyof T, + /** Specifies whether the order will be ascending or descending. Default is false */ + sortAscending?: boolean, + /** Specifies whether null values will be displayed first. Default is false */ + nullsFirst?: boolean + ): PostgrestClient + /** + * Paginates your request. + */ + range( + /** Index or position of the start of the specified range. */ + fromIndex: number, + /** Index or position of the end of the specified range. If not stated, all remaining rows after the starting index will be returned. */ + toIndex?: number + ): PostgrestClient /** * This allows you to apply various filters on your query. Filters can also be chained together. * Example: `.filter('name', 'eq', 'Paris')` @@ -121,7 +178,7 @@ declare module '@supabase/supabase-js' { /** Name of filter operator to be utilised. */ operator: FilterOperator | FilterOperatorString, /** Value to compare to. Exact data type of criteria depends on the operator used. */ - criteria: T[keyof T] + criteria: any ): PostgrestClient /** * Reverse of .filter(). Returns rows that do not meet the criteria specified using the columnName and operator provided. @@ -135,6 +192,12 @@ declare module '@supabase/supabase-js' { /** Value to compare to. Exact data type of criteria depends on the operator used. */ criteria: T[keyof T] ): PostgrestClient + /** + * To write append an OR filter, which should be made up of several other filters. + * + * Example: `.or('id.gt.20,and(name.eq.New Zealand,name.eq.France)')` + */ + or(criteria: string): PostgrestClient /** * Finds rows that exactly match the specified filterObject. Equivalent of multiple `filter('columnName', 'eq', criteria)`. */ @@ -143,54 +206,119 @@ declare module '@supabase/supabase-js' { filterObject: { [columnName: string]: T[keyof T] } ): PostgrestClient /** - * Finds all rows whose value on the stated columnName exactly matches the specified filterValue. Equivalent of filter(columnName, 'eq', criteria). + * Finds all rows whose value on the stated columnName exactly matches the specified filterValue. Equivalent of `filter(columnName, 'eq', criteria)`. * * Example: `.eq('name', 'San Francisco')` */ eq(columnName: keyof T, filterValue: T[keyof T]): PostgrestClient /** - * Return a single object as response body. Result must be single object, otherwise returns `406 Not Acceptable`. + * Finds all rows whose value on the stated columnName does not match the specified filterValue. Equivalent of `filter(columnName, 'neq', criteria)`. * - * Note: this must be called at the end of your query chain! + * Example: `.neq('name', 'San Francisco')` */ - single(): Promise> + neq(columnName: keyof T, filterValue: T[keyof T]): PostgrestClient /** - * Limit the amount of records to be returned. + * Finds all rows whose value on the stated columnName is greater than the specified filterValue. Eqiuvalent of `filter(columnName, 'gt', criteria)`. + * + * Example: `.gt('level', 9000)` */ - limit( - /** Specifies number of items to be returned at most. */ - criteria: number, - /** Name of chosen foreignTable to apply the limit on. Used if foreign tables are present. */ - foreignTableName?: string | null - ): PostgrestClient + gt(columnName: keyof T, filterValue: T[keyof T]): PostgrestClient /** - * Skip a number of rows before returning rows. + * Finds all rows whose value on the stated columnName is less than the specified filterValue. Eqiuvalent of `filter(columnName, 'lt', criteria)`. + * + * Example: `.lt('level', 9000)` */ - offset( - /** Index or position of the start of the specified range. */ - skipCount: number, - foreignTableName?: string | null - ): PostgrestClient + lt(columnName: keyof T, filterValue: T[keyof T]): PostgrestClient /** - * Orders your data before fetching. + * Finds all rows whose value on the stated columnName is greater than or equal to the specified filterValue. Eqiuvalent of `filter(columnName, 'gte', criteria)`. + * + * Example: `.gte('level', 9000)` */ - order( - /** Name of chosen column to base the order on. */ - columnName: keyof T, - /** Specifies whether the order will be ascending or descending. Default is false */ - sortAscending?: boolean, - /** Specifies whether null values will be displayed first. Default is false */ - nullsFirst?: boolean - ): PostgrestClient + gte(columnName: keyof T, filterValue: T[keyof T]): PostgrestClient /** - * Paginates your request. + * Finds all rows whose value on the stated columnName is less than or equal to the specified filterValue. Eqiuvalent of `filter(columnName, 'lte', criteria)`. + * + * Example: `.lte('level', 9000)` */ - range( - /** Index or position of the start of the specified range. */ - fromIndex: number, - /** Index or position of the end of the specified range. If not stated, all remaining rows after the starting index will be returned. */ - toIndex?: number - ): PostgrestClient + lte(columnName: keyof T, filterValue: T[keyof T]): PostgrestClient + /** + * Finds all rows whose value in the stated columnName matches the supplied pattern. Equivalent of `filter(columnName, 'like', stringPattern)`. + * + * Example: `.like('name', '%la%')` + */ + like(columnName: keyof T, stringPattern: string): PostgrestClient + /** + * A case-sensitive version of like(). Equivalent of `filter(columnName, 'ilike', stringPattern)`. + * + * Example: `.ilike('name', '%LA%')` + */ + ilike(columnName: keyof T, stringPattern: string): PostgrestClient + /** + * A check for exact equality (null, true, false), finds all rows whose value on the state columnName exactly match the specified filterValue. Equivalent of `filter(columnName, 'is', filterValue)`. + * + * Example: `.is('name', null)` + */ + is(columnName: keyof T, filterValue: null | boolean): PostgrestClient + /** + * Finds all rows whose value on the stated columnName is found on the specified filterArray. Equivalent of `filter(columnName, 'in', criteria)`. + * + * Example: `.in('name', ['Rio de Janeiro', 'San Francisco'])` + */ + in(columnName: keyof T, filterArray: Array): PostgrestClient + /** + * Finds all rows whose json, array, or range value on the stated columnName contains the items specified in the filterObject. Equivalent of `filter(columName, 'cs', criteria)`. + * + * Example: `.cs('main_exports', ['oil'])` + */ + cs(columnName: keyof T, filterObject: object | Array): PostgrestClient + /** + * Finds all rows whose json, array, or range value on the stated columnName is contained by the specific filterObject. Equivalent of `filter(columName, 'cd', criteria)`. + * + * Example: `.cd('main_exports', ['cars', 'food', 'machine'])` + */ + cd(columnName: keyof T, filterObject: object | Array): PostgrestClient + /** + * Finds all rows whose array value on the stated columnName overlaps with the specified filterArray. Equivalent of `filter(columnName, 'ova', criteria)`. + * + * Example: `.ova('main_exports', ['computers', 'minerals'])` + */ + ova(columnName: keyof T, filterArray: Array): PostgrestClient + /** + * Finds all rows whose range value on the stated columnName overlaps with the specified filterRange. Equivalent of `filter(columnName, 'ovr', criteria)`. + * + * Example: `.ovr('population_range_millions', [150, 250])` + */ + ovr(columnName: keyof T, filterRange: Array): PostgrestClient + /** + * Finds all rows whose range value on the stated columnName is strictly on the left hand side of the specified filterRange. Equivalent of `filter(columnName, 'sl', criteria)`. + * + * Example: `.sl('population_range_millions', [150, 250])` + */ + sl(columnName: keyof T, filterRange: Array): PostgrestClient + /** + * Finds all rows whose range value on the stated columnName is strictly on the right hand side of the specified filterRange. Equivalent of `filter(columnName, 'sl', criteria)`. + * + * Example: `.sr('population_range_millions', [150, 250])` + */ + sr(columnName: keyof T, filterRange: Array): PostgrestClient + /** + * Finds all rows whose range value on the stated columnName does not extend to the left of the specified filterRange. Equivalent of `filter(columnName, 'nxl', criteria)`. + * + * Example: `.nxl('population_range_millions', [150, 250])` + */ + nxl(columnName: keyof T, filterRange: Array): PostgrestClient + /** + * Finds all rows whose range value on the stated columnName does not extend to the right of the specified filterRange. Equivalent of `filter(columnName, 'nxl', criteria)`. + * + * Example: `.nxr('population_range_millions', [150, 250])` + */ + nxr(columnName: keyof T, filterRange: Array): PostgrestClient + /** + * Finds all rows whose range value on the stated columnName is adjacent to the specified filterRange. Equivalent of `filter(columnName, 'adj', criteria)`. + * + * Example: `.adj('population_range_millions', [70, 185])` + */ + adj(columnName: keyof T, filterRange: Array): PostgrestClient } interface SupabaseRealtimeClient { From f5fe670e0285d8ee94fe3e35ed1faa1a92575bb7 Mon Sep 17 00:00:00 2001 From: Thorsten Schaeff Date: Thu, 13 Aug 2020 00:18:13 +0800 Subject: [PATCH 09/11] Add --copy-files to .babelrc. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9efe1c26..52afa911 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "test:watch": "npm test -- --watch", "test:examples": "node examples/", "cover": "nyc --check-coverage && npm test", - "build": "BABEL_ENV=production babel src --out-dir lib", + "build": "BABEL_ENV=production babel src --out-dir lib --copy-files", "prepublish": "npm run clean && npm run build", "deploy:minor": "npm version minor && npm publish --access=public", "deploy:patch": "npm version patch && npm publish --access=public" From ec05167e95581b3c24c2f2ad744470e68ddabafe Mon Sep 17 00:00:00 2001 From: Thorsten Schaeff Date: Thu, 13 Aug 2020 18:45:15 +0800 Subject: [PATCH 10/11] More polish. --- src/index.d.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index eb6ba65e..bb278385 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -60,9 +60,9 @@ declare module '@supabase/supabase-js' { | 'nxl' | 'adj' - interface User { + interface SupabaseAuthUser { app_metadata: { - provider: 'email' + provider?: string [key: string]: any } user_metadata: { @@ -78,10 +78,10 @@ declare module '@supabase/supabase-js' { updated_at: string } - interface AuthResponse { + interface SupabaseAuthResponse { status: number body: { - user: User + user: SupabaseAuthUser access_token: string refresh_token: string expires_in: number @@ -94,16 +94,16 @@ declare module '@supabase/supabase-js' { * Allow your users to sign up and create a new account. * After they have signed up, all interactions using the Supabase JS client will be performed as "that user". */ - signup: (email: string, password: string) => Promise + signup: (email: string, password: string) => Promise /** * If an account is created, users can login to your app. * After they have logged in, all interactions using the Supabase JS client will be performed as "that user". */ - login: (email: string, password: string) => Promise + login: (email: string, password: string) => Promise /** * Get the JSON data for the logged in user. */ - user: () => Promise + user: () => Promise /** * After calling log out, all interactions using the Supabase JS client will be "anonymous". */ @@ -181,7 +181,7 @@ declare module '@supabase/supabase-js' { criteria: any ): PostgrestClient /** - * Reverse of .filter(). Returns rows that do not meet the criteria specified using the columnName and operator provided. + * Reverse of `.filter()`. Returns rows that do not meet the criteria specified using the columnName and operator provided. * Example: `.not('name', 'eq', 'Paris')` */ not( @@ -248,7 +248,7 @@ declare module '@supabase/supabase-js' { */ like(columnName: keyof T, stringPattern: string): PostgrestClient /** - * A case-sensitive version of like(). Equivalent of `filter(columnName, 'ilike', stringPattern)`. + * A case-sensitive version of `like()`. Equivalent of `filter(columnName, 'ilike', stringPattern)`. * * Example: `.ilike('name', '%LA%')` */ @@ -322,7 +322,13 @@ declare module '@supabase/supabase-js' { } interface SupabaseRealtimeClient { + /** + * Subscribes to a specific table for changes in realtime. + * + * Note: If you want to receive the "previous" data for updates and deletes, you will need to set `REPLICA IDENTITY` to `FULL`, like this: `ALTER TABLE your_table REPLICA IDENTITY FULL;`. + */ subscribe(): SupabaseRealtimeClient + /** Unsubscribes from a specific subscription. */ unsubscribe(): SupabaseRealtimeClient schema: string tableName: string @@ -332,7 +338,9 @@ declare module '@supabase/supabase-js' { interface SupabaseRealtimePayload { commit_timestamp: string eventType: 'INSERT' | 'UPDATE' | 'DELETE' + /** The new record. Present for 'INSERT' and 'UPDATE' events. */ new: T + /** The previous record. Present for 'UPDATE' and 'DELETE' events. */ old: T schema: string table: string @@ -346,7 +354,7 @@ declare module '@supabase/supabase-js' { /** * A comma separated list of columns. For example `.select('id, name')`. * - * Omitting `columnQuery` is equal to `.select('*'). + * Omitting `columnQuery` is equal to `.select('*')`. */ columnQuery?: string ): PostgrestClient From 2ecb447c04148691b63eb298702f1760b069c56a Mon Sep 17 00:00:00 2001 From: Thorsten Schaeff Date: Thu, 13 Aug 2020 20:29:18 +0800 Subject: [PATCH 11/11] Ilike case-insensitive. --- src/index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index bb278385..ca1d9a5b 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -14,7 +14,7 @@ declare module '@supabase/supabase-js' { LessThanOrEqual = 'lte', /** Finds all rows whose value in the stated columnName matches the supplied pattern. */ Like = 'like', - /** A case-sensitive version of `Like`. */ + /** A case-insensitive version of `like`. */ ILike = 'ilike', /** A check for exact equality (null, true, false) */ Is = 'is', @@ -244,13 +244,13 @@ declare module '@supabase/supabase-js' { /** * Finds all rows whose value in the stated columnName matches the supplied pattern. Equivalent of `filter(columnName, 'like', stringPattern)`. * - * Example: `.like('name', '%la%')` + * Example: `.like('name', '%LA%')` */ like(columnName: keyof T, stringPattern: string): PostgrestClient /** - * A case-sensitive version of `like()`. Equivalent of `filter(columnName, 'ilike', stringPattern)`. + * A case-insensitive version of `like()`. Equivalent of `filter(columnName, 'ilike', stringPattern)`. * - * Example: `.ilike('name', '%LA%')` + * Example: `.ilike('name', '%la%')` */ ilike(columnName: keyof T, stringPattern: string): PostgrestClient /**