Skip to content

Commit

Permalink
Add get chats rpc function
Browse files Browse the repository at this point in the history
  • Loading branch information
0xinhua committed Jun 19, 2024
1 parent 73da2c6 commit b46df36
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
24 changes: 8 additions & 16 deletions app/api/chats/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { auth } from '@/auth'
import { NextResponse } from 'next/server'
import { pgPool } from '@/lib/pg'
import { supabase } from '@/lib/supabase'

export async function GET(req: Request) {
Expand All @@ -18,32 +17,25 @@ export async function GET(req: Request) {

try {

const query = `
SELECT *
FROM chat_dataset.chats
WHERE user_id = $1
ORDER BY updated_at DESC;
`
const { rows } = await pgPool.query(query, [
userId
])
const { data: rows, error } = await supabase.rpc('get_user_chats', { p_user_id: userId })

// console.log('rows data =>', rows)
console.log('rows data =>', rows, error)

if (rows.length === 0) {
if (error) {
console.error('get_user_chats rpc error:', error)
return NextResponse.json({
data: [],
code: 0
code: 0,
})
}

return NextResponse.json({
data: rows,
data: rows ? rows : [],
code: 0
})

} catch (error) {
console.log('error', error)
console.log('get_user_chats rpc error', error)
return NextResponse.json({
data: [],
code: 0
Expand All @@ -65,7 +57,7 @@ export async function DELETE(req: Request) {

try {

const { data, error } = await supabase.rpc('delete_user_chats', { p_user_id: userId });
const { data, error } = await supabase.rpc('delete_user_chats', { p_user_id: userId })

console.log('delete rows data', error)

Expand Down
1 change: 1 addition & 0 deletions components/empty-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export function EmptyScreen({ setInput }: Pick<UseChatHelpers, 'setInput'>) {
<div className="mt-4 grid gap-3 lg:grid-cols-3 md:grid-cols-2 lg:gap-5">
{ quickstartMessages.map((message, index) => (
<button
title={message.message}
key={index}
className="rounded-md border bg-gray-50 p-2.5 px-3 text-gray-600 hover:bg-gray-100 max-xl:text-sm dark:border-gray-800 dark:bg-neutral-800 dark:text-gray-300 dark:hover:bg-neutral-700 text-left"
onClick={() => setInput(message.message)}
Expand Down
7 changes: 1 addition & 6 deletions lib/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,32 @@ export const quickstartMessages = [
{
heading: '💡 Data analysis',
message: `Summarize the key characteristics of this dataset. Include information on data types, missing values, and basic statistics. The following dataset:
\n`
},
{
heading: `📖 Summarize content`,
message: `Summarize content you are provided with for a second-grade student:
\n`
},
{
heading: '📧 Format and correct email',
message: `Proofread and format [email]. Also, give suggestions for getting the point across effectively.
[email] following:
\n`
},
{
heading: '🔠 Translate English',
message: `You will be provided with statements, and your task is to convert them to standard English.
[statements] following:
[statements] following:
\n`
},
{
heading: '🈳 Translate Chinese',
message: `You will be provided with statements, and your task is to convert them to simplified Chinese, In the translated text, English words and numbers should be preceded and followed by a space.
[statements] following:
\n`
},
{
Expand Down
35 changes: 35 additions & 0 deletions supabase/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,41 @@ END;
$$;
```

- **Function to get all Chat history**

```sql
-- Execute the following SQL statement in the Supabase SQL Editor to create the function
create or replace function get_user_chats(p_user_id uuid)
returns table(
id bigint,
chat_id text,
user_id uuid,
title text,
path text,
created_at bigint,
messages jsonb,
share_path text,
updated_at bigint
) as $$
begin
return query
select
c.id,
c.chat_id,
c.user_id,
c.title,
c.path,
c.created_at,
c.messages,
c.share_path,
c.updated_at
from chat_dataset.chats c
where c.user_id = p_user_id
order by c.updated_at desc;
end;
$$ language plpgsql;
```

- **Function to Delete all Chat data**

```sql
Expand Down

0 comments on commit b46df36

Please sign in to comment.