Back to packages list

Vals using @supabase/supabase-js

Description from the NPM package:
Isomorphic Javascript client for Supabase
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
import { getOpenAiResponse } from "https://esm.town/v/thomasatflexos/getOpenAiResponse";
import { getRelevantContent } from "https://esm.town/v/thomasatflexos/getRelevantContent";
import process from "node:process";
export let createRelevantComment = async (
req: express.Request,
res: express.Response,
) => {
const content = req?.body?.content;
const url = req?.body?.url;
if (!content || !url) {
res.status(400)
.json({
message: "The content and url parameter is required for this end point",
});
}
const { createClient } = await import(
"https://esm.sh/@supabase/supabase-js@2"
);
const supabase = createClient(
process.env.supabaseURL,
process.env.supabaseKey,
);
// Query from the database to see if this link already exists
const { data, error } = await supabase
.from("linkedin_seedings")
.select()
.eq("link", url)
.limit(1);
if (error) {
throw error;
}
// If it does, then no more processing
if (data.length > 0) {
res.status(200).json({
message: "This link already exists in the database",
});
}
// Otherwise, start processing this new link
// First, retrieve documents in the database relevant to the post content
let relevantContent = await getRelevantContent(content);
let context = "";
let link = "";
// Prepare the context from relevant documents
for (let i = 0; i < relevantContent.length; i++) {
context += "\n" + relevantContent[i].pageContent;
link += "\n" + relevantContent[i].metadata.source;
}
// Inject relenvat context into our prompt
let PROMPT =
`You are a passionate advocate of remote work, and you're helping to make meaningful comments to a Linkedin post about remote work.
\n I will provide you with the text of a Linkedin post and some relevant context.
\n Here's the Linkedin post:
\n """${content}"""
\n Here's the context:
\n """${context}"""
\n IF you think that the Linkedin post is about new job opportunities, just respond with the text "N/A" and stop immediately. Do not respond any further.
\n ELSE IF you think the Linkdedin post is not about new job opportunities, please proceed to a meaningful comment to the provided Linkedin post based on the provided context and include ${link} in the post in a clever and relevant way, (don't be too
let finalResponse = await getOpenAiResponse(PROMPT);
const { data1, error1 } = await supabase
.from("linkedin_seedings")
.insert({ link: url, content: content, comment: finalResponse });
if (error1) {
throw error1;
}
res.status(200).json({
message: finalResponse,
});
};
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
import { fetch } from "https://esm.town/v/std/fetch";
import process from "node:process";
export let convertTodoItemsToGamePlans = async (
req: express.Request,
res: express.Response,
) => {
let task = req.body?.toDoItems;
if (!task) {
res.json({
status: 400,
message: "No to-do items were submitted",
});
}
let PROMPT = `
OK, let's plan my day!
My task remaining is:
"""
${task}
""""
Break the task into a journey like a game so that I can do all the task in an exciting manner and get reward after each small task is done and big reward for a huge task.
The response should be a JSON array
with each JSON object contains
a field called 'questName' that contains the title of the quest (a fun name for the adventure),
a field called 'quest' that contains a JSON array of the steps that I will have to go through the complete the quest. (break the quest down to at least 5 actionable tasks to get done),
and a field called 'reward' that contains the text of the reward (a reward should be something fun and tangible, like "dance 5 minutes" or "get a cup of coffee")
Always return in this format and do not deviate. Do not include any newline characters.`;
let rawBody = JSON.stringify({
"messages": [{ "role": "system", "content": PROMPT }],
"max_tokens": 1500,
"model": "gpt-3.5-turbo",
});
let openApiKey = process.env.OPEN_API_KEY;
var requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${openApiKey}`,
},
body: rawBody,
redirect: "follow",
};
let openApiResponse = await fetch(
"https://api.openai.com/v1/chat/completions",
requestOptions,
);
let jsonResponse = await openApiResponse.json();
let choice = jsonResponse?.choices[0]?.message.content;
if (!choice) {
res.json({
status: 400,
message: "Something went wrong!",
});
}
let finalResponse = choice.replace(/\n/g, "");
const { createClient } = await import(
"https://esm.sh/@supabase/supabase-js@2"
);
const supabase = createClient(
process.env.supabaseURL,
process.env.supabaseKey,
);
const { data, error } = await supabase
.from("daily_quests_usage")
.insert({ prompt: task, response: finalResponse });
if (error) {
throw error;
}
res.status(200).json(finalResponse);
};
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
import { fetch } from "https://esm.town/v/std/fetch";
import process from "node:process";
/**
This is a handler for when Notion redirects during OAuth 2.0 workflow
**/
export let redirect = async (req: express.Request, res: express.Response) => {
let code = req.query.code;
if (!code) {
res.json({ status: 400, Reason: "Invalid access token" });
}
// Retrieve Notion access token given a temporary code
const postData = {
"code": code,
"grant_type": "authorization_code",
"redirect_uri": process.env.NOTION_SMALL_STEPS_REDIRECT_URL,
};
// This endpoint uses Basic Authentication which requires a 64-encoded token
const token = process.env.OAUTH_CLIENT_ID + ":" +
process.env.OAUTH_CLIENT_SECRET;
const response = await fetch("https://api.notion.com/v1/oauth/token", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Notion-Version": "2022-06-28",
Authorization: `Basic ${btoa(token)}`,
},
body: JSON.stringify(postData),
});
const notionAuthRes = await response.json();
// Retrieve access token and other properties to save to database
let accessToken = notionAuthRes.access_token;
let email = notionAuthRes.owner.user.person.email;
let bot_id = notionAuthRes.bot_id;
// Using supabase to store out information
const { createClient } = await import(
"https://esm.sh/@supabase/supabase-js@2"
);
const supabase = createClient(
process.env.supabaseURL,
process.env.supabaseKey,
);
const { data, error } = await supabase
.from("small_steps_user")
.insert({ email: email, notion_auth_token: accessToken, bot_id: bot_id });
if (error) {
throw error;
}
const botIdResponse = JSON.stringify({ bot_id: bot_id });
res.send(`
<!DOCTYPE html>
<html>
<body>
<script>
window.opener.parent.postMessage(${botIdResponse}, "*");
window.close();
</script>
</body>
</html>
`);
};

Selecting from a Supabase table using the SDK.

Part of the Supabase guide on docs.val.town.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import process from "node:process";
export const supabaseSDKSelectMyFirstTable = (async () => {
const { createClient } = await import(
"https://esm.sh/@supabase/supabase-js@2"
);
const supabase = createClient(
process.env.supabaseURL,
process.env.supabaseKey,
);
const { data, error } = await supabase
.from("my_first_table")
.select("name, data")
.eq("name", "Alice")
.limit(1);
if (error) {
throw error;
}
return data;
})();

Inserting into a table using Supabase's SDK.

Part of the Supabase guide on docs.val.town.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import process from "node:process";
export const supabaseSDKInsertIntoMyFirstTable = (async () => {
const { createClient } = await import(
"https://esm.sh/@supabase/supabase-js@2"
);
const supabase = createClient(
process.env.supabaseURL,
process.env.supabaseKey,
);
const { data, error } = await supabase
.from("my_first_table")
.insert({ name: "Alice", data: { job: "software engineer" } });
if (error) {
throw error;
}
})();
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
import { email } from "https://esm.town/v/std/email?v=9";
import process from "node:process";
export const untitled_silverGoose = (async () => {
async function saveTickerToSupabase(request) {
const { createClient } = await import(
"https://esm.sh/@supabase/supabase-js@2"
);
const supabase = createClient(
process.env.marketMemoSupabaseApiUrl,
process.env.marketMemoSupabaseServiceRoleKey,
);
try {
const body = request.options.body;
const ticker = body.ticker;
const performanceId = body.performanceId;
const { data, error } = await supabase
.from("ms_ticker_lookup_table")
.insert({ ticker: ticker, performance_id: performanceId })
.select();
}
catch (error) {
await email({
text: error,
subject: "Error in saveTickerToSupabase",
});
}
}
})();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import process from "node:process";
export const getWallets = (async () => {
const { createClient } = await import("npm:@supabase/supabase-js");
const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_ANON_KEY,
);
const { data: wallets, error } = await supabase
.from("wallets")
.select("*");
if (error)
return error;
return wallets;
})();
1
Next