Supabase Postgre / 数据库函数如何记录和调试?

huangapple go评论87阅读模式
英文:

Supabase Postgre / Database Function how to log and debug?

问题

有人知道如何使用Supabase中的PLV8查看来自我的数据库函数的日志吗?我尝试查阅文档,但似乎找不到解决我面临问题的答案。到目前为止,我只能在Supabase中看到错误消息,但它并没有准确显示我的PLV8.elog输出。当前的错误消息是 'Cannot read properties of undefined (reading 'id')',所以我想记录接收到的 request_data 以进行调试。

-- FUNCTION: public.handle_onboard_user(json)

-- DROP FUNCTION IF EXISTS public.handle_onboard_user(json);

CREATE OR REPLACE FUNCTION public.handle_onboard_user(
    request_data json)
    RETURNS void
    LANGUAGE 'plv8'
    COST 100
    VOLATILE PARALLEL UNSAFE
AS $BODY$
try {
  plv8.elog(INFO, request_data);
  // -- 提取 JSON 参数
  var user = request_data.user; 
  var selected_skills = request_data.selected_skills;
  var selected_categories = request_data.selected_categories;
  var experiences = request_data.experiences;
  var accomplishments = request_data.accomplishments;

  var auth_user_id = user.id;

  // -- 更新用户数据的引导
  plv8.execute('UPDATE public.users SET first_name = $1, last_name = $2, title = $3, resume = $4, degree_programme_id = $5, avatar = $6, about = $7 ,is_onboarded = true, updated_at = now() WHERE id = $8', user.first_name, user.last_name, user.title, user.resume, user.degree_programme_id, user.avatar, user.about, auth_user_id);
  plv8.elog(INFO, '更新用户信息成功');

  // -- 选择特定用户的首选项
  var result = plv8.execute('SELECT * FROM public.preferences WHERE user_id = $1', auth_user_id);

  var preference_id = result[0].id;

  // -- 更新技能
  for (var i = 0; i < selected_skills.length; i++) {
    var skill = selected_skills[i];

    plv8.execute('INSERT INTO public.selected_skills (emsi_id, name) VALUES ($1, $2) ON CONFLICT (emsi_id) DO NOTHING', skill.emsi_id, skill.name);

    var result = plv8.execute('SELECT id FROM public.selected_skills WHERE emsi_id = $1 LIMIT 1', skill.emsi_id);

    if (result.length == 0) {
      continue;
    }

    var skill_id = result[0].id;

    plv8.execute('INSERT INTO public.skills_preferences ' +
  '(selected_skill_id, preference_id) VALUES ($1, $2)', skill_id, preference_id);
  }
  plv8.elog(INFO, '插入技能成功');

  // -- 插入用户选择的类别
  for (var i = 0; i < selected_categories.length; i++) {
    var category = selected_categories[i];

    plv8.execute('INSERT INTO public.categories_preferences (category_id, preference_id) VALUES ($1, $2)', category.id, preference_id);
  }
  plv8.elog(INFO, '插入选定的类别成功');

  // -- 插入用户的经验
  for (var i = 0; i < experiences.length; i++) {
    var experience = experiences[i];

    plv8.execute('INSERT INTO public.experiences (title, company_name, is_current, start_date, end_date, description, user_id) VALUES ($1, $2, $3, $4, $5, $6, $7)', experience.title, experience.company_name, experience.is_current, experience.start_date, experience.end_date, experience.description, auth_user_id);
  }
  plv8.elog(INFO, '插入用户经验成功');

  // -- 插入用户的成就
  for (var i = 0; i < accomplishments.length; i++) {
    var accomplishment = accomplishments[i];

    plv8.execute('INSERT INTO public.accomplishments (title, issuer, is_active, start_date, end_date, description, user_id) VALUES ($1, $2, $3, $4, $5, $6, $7)', accomplishment.title, accomplishment.issuer, accomplishment.is_active, accomplishment.start_date, accomplishment.end_date, accomplishment.description, auth_user_id);
  }
  plv8.elog(INFO, '插入用户成就成功');

  return;

} catch (error) {

  plv8.elog(ERROR, error.message);

}
$BODY$;

ALTER FUNCTION public.handle_onboard_user(json)
    OWNER TO postgres;

GRANT EXECUTE ON FUNCTION public.handle_onboard_user(json) TO PUBLIC;

GRANT EXECUTE ON FUNCTION public.handle_onboard_user(json) TO anon;

GRANT EXECUTE ON FUNCTION public.handle_onboard_user(json) TO authenticated;

GRANT EXECUTE ON FUNCTION public.handle_onboard_user(json) TO postgres;

GRANT EXECUTE ON FUNCTION public.handle_onboard_user(json) TO service_role;
英文:

Does someone know how to see my logs coming from my database function using plv8 in supabase? I tried searching the docs but could not seem to find any answers to solve the issue I am facing. So far i am only seeing Error messages within supabase but it is not exactly showing my plv8.elog outputs. Right now error is saying 'Cannot read properties of undefined (reading 'id')' so i wanted to log the received request_data to debug

-- FUNCTION: public.handle_onboard_user(json)
-- DROP FUNCTION IF EXISTS public.handle_onboard_user(json);
CREATE OR REPLACE FUNCTION public.handle_onboard_user(
request_data json)
RETURNS void
LANGUAGE &#39;plv8&#39;
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
try {
plv8.elog(INFO, request_data);
// --Extract JSON params
var user = request_data.user; 
var selected_skills = request_data.selected_skills;
var selected_categories = request_data.selected_categories;
var experiences = request_data.experiences;
var accomplishments = request_data.accomplishments;
var auth_user_id = user.id;
// --Update user data onboarding
plv8.execute(&#39;UPDATE public.users SET first_name = $1, last_name = $2, title = $3, resume = $4, degree_programme_id = $5, avatar = $6, about = $7 ,is_onboarded = true, updated_at = now() WHERE id = $8&#39;, user.first_name, user.last_name, user.title, user.resume, user.degree_programme_id, user.avatar, user.about, auth_user_id);
plv8.elog(INFO, &#39;Update user info successful&#39;);
// -- Select preference of specific user
var result = plv8.execute(&#39;SELECT * FROM public.preferences WHERE user_id = $1&#39;, auth_user_id);
var preference_id = result[0].id;
// --Update skills
for (var i = 0; i &lt; selected_skills.length; i++) {
var skill = selected_skills[i];
plv8.execute(&#39;INSERT INTO public.selected_skills (emsi_id, name) VALUES ($1, $2) ON CONFLICT (emsi_id) DO NOTHING&#39;, skill.emsi_id, skill.name);
var result = plv8.execute(&#39;SELECT id FROM public.selected_skills WHERE emsi_id = $1 LIMIT 1&#39;, skill.emsi_id);
if (result.length == 0) {
continue;
}
var skill_id = result[0].id;
plv8.execute(&#39;INSERT INTO public.skills_preferences &#39; +
&#39;(selected_skill_id, preference_id) VALUES ($1, $2)&#39;, skill_id, preference_id);
}
plv8.elog(INFO, &#39;Insert skills successful&#39;);
// --Insert user selected categories
for (var i = 0; i &lt; selected_categories.length; i++) {
var category = selected_categories[i];
plv8.execute(&#39;INSERT INTO public.categories_preferences (category_id, preference_id) VALUES ($1, $2)&#39;, category.id, preference_id);
}
plv8.elog(INFO, &#39;Insert selected categories successful&#39;);
// --Insert experiences of user
for (var i = 0; i &lt; experiences.length; i++) {
var experience = experiences[i];
plv8.execute(&#39;INSERT INTO public.experiences (title, company_name, is_current, start_date, end_date, description, user_id) VALUES ($1, $2, $3, $4, $5, $6, $7)&#39;, experience.title, experience.company_name, experience.is_current, experience.start_date, experience.end_date, experience.description, auth_user_id);
}
plv8.elog(INFO, &#39;Insert user experiences success&#39;);
// --Insert accomplishments of user
for (var i = 0; i &lt; accomplishments.length; i++) {
var accomplishment = accomplishments[i];
plv8.execute(&#39;INSERT INTO public.accomplishments (title, issuer, is_active, start_date, end_date, description, user_id) VALUES ($1, $2, $3, $4, $5, $6, $7)&#39;, accomplishment.title, accomplishment.issuer, accomplishment.is_active, accomplishment.start_date, accomplishment.end_date, accomplishment.description, auth_user_id);
}
plv8.elog(INFO, &#39;Insert user accomplishments success&#39;);
return;
} catch (error) {
plv8.elog(ERROR, error.message);
}
$BODY$;
ALTER FUNCTION public.handle_onboard_user(json)
OWNER TO postgres;
GRANT EXECUTE ON FUNCTION public.handle_onboard_user(json) TO PUBLIC;
GRANT EXECUTE ON FUNCTION public.handle_onboard_user(json) TO anon;
GRANT EXECUTE ON FUNCTION public.handle_onboard_user(json) TO authenticated;
GRANT EXECUTE ON FUNCTION public.handle_onboard_user(json) TO postgres;
GRANT EXECUTE ON FUNCTION public.handle_onboard_user(json) TO service_role;

答案1

得分: 0

我成功解决了,确保在plv8.elog中使用LOG级别而不是INFO,然后你可以在Supabase中相应地筛选PostgreSQL日志。

英文:

I manage to solve it make sure to use LOG level instead of INFO in plv8.elog and then you could filter the logs accordingly in postgres logs within supabase

huangapple
  • 本文由 发表于 2023年6月19日 15:23:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定