Node.js 如何使用模块节点pg migrate为postgresql中的函数编写迁移脚本

Node.js 如何使用模块节点pg migrate为postgresql中的函数编写迁移脚本,node.js,postgresql,pg,dbmigrate,Node.js,Postgresql,Pg,Dbmigrate,我试图使用node pg migrate module编写一个迁移脚本,但未能成功,因为我遇到了以下错误 Error: Can't get migration files: //dbMigration/migrations/1534615332847_new_test_function.js:11 },'DECLARE ^^^^^^^^^ SyntaxError: Invalid or unexpected token 我的迁移脚本如下所示: exports.shorthands = unde

我试图使用node pg migrate module编写一个迁移脚本,但未能成功,因为我遇到了以下错误

Error: Can't get migration files: //dbMigration/migrations/1534615332847_new_test_function.js:11
},'DECLARE
^^^^^^^^^
SyntaxError: Invalid or unexpected token
我的迁移脚本如下所示:

exports.shorthands = undefined
exports.up = (pgm) => {
pgm.createFunction('mew_test_function', [
{ mode: 'IN', name: 'soldtocode', type: 'text', default: null }
],
{
  return: 'json',
  language: 'plpgsql',
  replace: true
},'DECLARE 
customer_list json;
final_response json;
 begin
  if not exists(select 1 from company_info where supplier_location_id = soldtocode) then
      final_response:=json_build_object("status","fail","message","No Record Found","customer_list", customer_list);
  else 
      SELECT array_to_json(array_agg(row_to_json(t))) FROM 
      (select distinct ci."b2x_registration_id",ci.supplier_location_id,ci."name",mo.id,mo.name "customerName"
      from public.company_info ci 
      join public.company_oem_mapping com on ci.id = com.company_info_id
      join mst_oem mo on mo.id = com.oem_id
      and ci.supplier_location_id = soldtocode) t 
      INTO customer_list;
      final_response:=json_build_object("status","pass","message","Record Found Successfully","customer_list", customer_list);
  end if;
RETURN final_response;

end 
')
} 

exports.down = (pgm) => {
pgm.dropFunction('mew_test_function', [
    { mode: 'IN', name: 'soldtocode', type: 'text', default: null }
  ],{
    ifExists : true,
    cascade : false
  })
}
exports.shorthands = undefined
exports.up = (pgm) => {
  pgm.sql(`CREATE OR REPLACE FUNCTION public.get_customer_name(soldtocode text)
  RETURNS json
  LANGUAGE plpgsql
  AS $function$
  DECLARE 
  customer_list json;
  final_response json;
  begin
  if not exists(select 1 from company_info where supplier_location_id = soldtocode) then
    final_response:=json_build_object("status","fail","message","No Record Found","customer_list", customer_list);
  else 
    SELECT array_to_json(array_agg(row_to_json(t))) FROM 
    (select distinct ci."b2x_registration_id",ci.supplier_location_id,ci."name",mo.id,mo.name "customerName"
    from public.company_info ci 
    join public.company_oem_mapping com on ci.id = com.company_info_id
    join mst_oem mo on mo.id = com.oem_id
    and ci.supplier_location_id = soldtocode) t 
    INTO customer_list;
    final_response:=json_build_object("status","pass","message","Record Found Successfully","customer_list", customer_list);
  end if;
  RETURN final_response;
  end 
  $function$`)
}
exports.down = (pgm) => {
  pgm.sql(`DROP FUNCTION IF EXISTS public.get_customer_name(soldtocode text)`)
}
&下面是我实际使用的postgresql函数:

CREATE OR REPLACE FUNCTION public.get_customer_name(soldtocode text)
RETURNS json
LANGUAGE plpgsql
AS $function$
DECLARE 
customer_list json;
final_response json;
begin
    if not exists(select 1 from company_info where supplier_location_id = soldtocode) then
        final_response:=json_build_object("status","fail","message","No Record Found","customer_list", customer_list);
    else 
        SELECT array_to_json(array_agg(row_to_json(t))) FROM 
        (select distinct ci."b2x_registration_id",ci.supplier_location_id,ci."name",mo.id,mo.name "customerName"
        from public.company_info ci 
        join public.company_oem_mapping com on ci.id = com.company_info_id
        join mst_oem mo on mo.id = com.oem_id
        and ci.supplier_location_id = soldtocode) t 
        INTO customer_list;
        final_response:=json_build_object("status","pass","message","Record Found Successfully","customer_list", customer_list);
    end if;
RETURN final_response;

end 
$function$

谁能给我一个在node pg migrate模块中编写的函数的适当示例。我能够编写createtable和其他脚本,但它会给添加函数迁移带来问题。提前感谢。

找到了解决此问题的解决方法。pgm还提供了将原始sql查询直接添加到迁移文件中的方法

查找以下代码,如下所示:

exports.shorthands = undefined
exports.up = (pgm) => {
pgm.createFunction('mew_test_function', [
{ mode: 'IN', name: 'soldtocode', type: 'text', default: null }
],
{
  return: 'json',
  language: 'plpgsql',
  replace: true
},'DECLARE 
customer_list json;
final_response json;
 begin
  if not exists(select 1 from company_info where supplier_location_id = soldtocode) then
      final_response:=json_build_object("status","fail","message","No Record Found","customer_list", customer_list);
  else 
      SELECT array_to_json(array_agg(row_to_json(t))) FROM 
      (select distinct ci."b2x_registration_id",ci.supplier_location_id,ci."name",mo.id,mo.name "customerName"
      from public.company_info ci 
      join public.company_oem_mapping com on ci.id = com.company_info_id
      join mst_oem mo on mo.id = com.oem_id
      and ci.supplier_location_id = soldtocode) t 
      INTO customer_list;
      final_response:=json_build_object("status","pass","message","Record Found Successfully","customer_list", customer_list);
  end if;
RETURN final_response;

end 
')
} 

exports.down = (pgm) => {
pgm.dropFunction('mew_test_function', [
    { mode: 'IN', name: 'soldtocode', type: 'text', default: null }
  ],{
    ifExists : true,
    cascade : false
  })
}
exports.shorthands = undefined
exports.up = (pgm) => {
  pgm.sql(`CREATE OR REPLACE FUNCTION public.get_customer_name(soldtocode text)
  RETURNS json
  LANGUAGE plpgsql
  AS $function$
  DECLARE 
  customer_list json;
  final_response json;
  begin
  if not exists(select 1 from company_info where supplier_location_id = soldtocode) then
    final_response:=json_build_object("status","fail","message","No Record Found","customer_list", customer_list);
  else 
    SELECT array_to_json(array_agg(row_to_json(t))) FROM 
    (select distinct ci."b2x_registration_id",ci.supplier_location_id,ci."name",mo.id,mo.name "customerName"
    from public.company_info ci 
    join public.company_oem_mapping com on ci.id = com.company_info_id
    join mst_oem mo on mo.id = com.oem_id
    and ci.supplier_location_id = soldtocode) t 
    INTO customer_list;
    final_response:=json_build_object("status","pass","message","Record Found Successfully","customer_list", customer_list);
  end if;
  RETURN final_response;
  end 
  $function$`)
}
exports.down = (pgm) => {
  pgm.sql(`DROP FUNCTION IF EXISTS public.get_customer_name(soldtocode text)`)
}