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

cbwuti44  于 2023-06-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(148)

有人知道如何在数据库中使用plv8查看来自数据库函数的日志吗?我试着搜索文档,但似乎找不到任何答案来解决我所面临的问题。到目前为止,我只在supabase中看到错误消息,但它并不完全显示我的plv8.elog输出。现在错误是说'无法读取未定义的属性(阅读' 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);
  // --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('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, 'Update user info successful');

  // -- Select preference of specific user
  var result = plv8.execute('SELECT * FROM public.preferences WHERE user_id = $1', auth_user_id);

  var preference_id = result[0].id;

  // --Update skills
  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, 'Insert skills successful');

  // --Insert user selected categories
  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, 'Insert selected categories successful');

  // --Insert experiences of user
  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, 'Insert user experiences success');

  // --Insert accomplishments of user
  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, 'Insert user accomplishments success');

  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;
nwsw7zdq

nwsw7zdq1#

我设法解决了这个问题,确保在plv8.elog中使用LOG级别而不是INFO,然后您可以在supabase中的postgres日志中相应地过滤日志

相关问题