forked from triggerdotdev/trigger.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
165 lines (151 loc) · 4.48 KB
/
Copy pathindex.ts
File metadata and controls
165 lines (151 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { SupabaseClient, SupabaseClientOptions, createClient } from "@supabase/supabase-js";
import {
ConnectionAuth,
IO,
IOTask,
IntegrationTaskKey,
Json,
RunTaskErrorCallback,
RunTaskOptions,
TriggerIntegration,
retry,
} from "@trigger.dev/sdk";
import { GenericSchema } from "./types";
export type SupabaseIntegrationOptions<TSchema extends string> =
| {
/** The unique ID for this integration */
id: string;
/** The Supabase project url (e.g. "https://<project-id>.supabase.co") */
supabaseUrl: string;
/** The Supabase service account API Key (found in your Supabase Project Settings -> API -> service_role) */
supabaseKey: string;
/** Options that are passed through to the call the createClient */
options?: SupabaseClientOptions<TSchema>;
}
| {
id: string;
projectId: string;
supabaseKey: string;
options?: SupabaseClientOptions<TSchema>;
};
/**
* A Trigger Integration for Supabase
*
* @example
* ```ts
* import { Supabase } from "@trigger.dev/supabase";
* import { Database } from "@/supabase.types";
*
* const supabase = new Supabase<Database>({
* id: "my-supabase",
* projectId: process.env.SUPABASE_ID!,
* supabaseKey: process.env.SUPABASE_API_KEY!,
* });
* ```
*/
export class Supabase<
Database = any,
SchemaName extends string & keyof Database = "public" extends keyof Database
? "public"
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any,
> implements TriggerIntegration
{
private _options: SupabaseIntegrationOptions<SchemaName>;
private _client?: SupabaseClient<Database, SchemaName, Schema>;
private _io?: IO;
private _connectionKey?: string;
/**
* The native Supabase client. This is exposed for use outside of Trigger.dev jobs
*
* @example
* ```ts
* import { Supabase } from "@trigger.dev/supabase";
* import { Database } from "@/supabase.types";
*
* const supabase = new Supabase<Database>({
* id: "my-supabase",
* projectId: process.env.SUPABASE_ID!,
* supabaseKey: process.env.SUPABASE_API_KEY!,
* });
*
* const { data, error } = await supabase.native.from("users").select("*");
* ```
*/
public readonly native: SupabaseClient<Database, SchemaName, Schema>;
constructor(private options: SupabaseIntegrationOptions<SchemaName>) {
this._options = options;
const supabaseOptions = options.options || {};
const supabaseUrl =
"projectId" in options ? `https://${options.projectId}.supabase.co` : options.supabaseUrl;
this.native = createClient(supabaseUrl, options.supabaseKey, {
...supabaseOptions,
auth: {
...supabaseOptions.auth,
persistSession: false,
},
});
}
get authSource() {
return "LOCAL" as const;
}
get id() {
return this.options.id;
}
get metadata() {
return { id: "supabase", name: "Supabase" };
}
get client() {
if (!this._client) {
throw new Error("Supabase client not initialized");
}
return this._client;
}
cloneForRun(io: IO, connectionKey: string, auth?: ConnectionAuth) {
const supabase = new Supabase<Database, SchemaName, Schema>(this._options);
supabase._io = io;
supabase._connectionKey = connectionKey;
const supabaseOptions = this._options.options || {};
const supabaseUrl =
"projectId" in this._options
? `https://${this._options.projectId}.supabase.co`
: this._options.supabaseUrl;
supabase._client = createClient(supabaseUrl, this._options.supabaseKey, {
...supabaseOptions,
auth: {
...supabaseOptions.auth,
persistSession: false,
},
});
return supabase;
}
runTask<T, TResult extends Json<T> | void>(
key: IntegrationTaskKey,
callback: (
client: SupabaseClient<Database, SchemaName, Schema>,
task: IOTask,
io: IO
) => Promise<TResult>,
options?: RunTaskOptions,
errorCallback?: RunTaskErrorCallback
): Promise<TResult> {
if (!this._io) throw new Error("No IO");
if (!this._connectionKey) throw new Error("No connection key");
return this._io.runTask(
key,
(task, io) => {
if (!this._client) throw new Error("No client");
return callback(this._client, task, io);
},
{
icon: "supabase",
retry: retry.standardBackoff,
...(options ?? {}),
connectionKey: this._connectionKey,
},
errorCallback
);
}
}