Node.js 如何使用UUID和Postgres pg promise库创建ExpressJS restful API

Node.js 如何使用UUID和Postgres pg promise库创建ExpressJS restful API,node.js,postgresql,uuid,Node.js,Postgresql,Uuid,我正在尝试为我的web应用程序中的用户创建CRUD。我使用uuid作为我的id,也作为主键,如下面的用户表所示。 我正在使用pg promise库连接到Postgres。用于创建用户和获取所有用户的路由代码正在工作。 但是,在创建id常量以获取一个用户、删除用户或更新用户(即 const user_id=parseIntreq.params.id 请参阅下面的示例代码。我也将感谢任何在线资源的例子 我的用户表 CRUD以获得一个用户 要编辑用户的CRUD 好的,关于UUID的一些注释@非常不正确

我正在尝试为我的web应用程序中的用户创建CRUD。我使用uuid作为我的id,也作为主键,如下面的用户表所示。 我正在使用pg promise库连接到Postgres。用于创建用户和获取所有用户的路由代码正在工作。 但是,在创建id常量以获取一个用户、删除用户或更新用户(即 const user_id=parseIntreq.params.id

请参阅下面的示例代码。我也将感谢任何在线资源的例子

我的用户表

CRUD以获得一个用户

要编辑用户的CRUD


好的,关于UUID的一些注释@非常不正确的uuid不仅仅是字符串。虽然它们在视觉上显示为字符串,您可以将它们作为字符串进行处理,但它们是良好的UUID。具有唯一属性的字符串缺少最值得注意的格式。下面是一个小演示。字符串不能在功能上执行此操作

create table uuid_demo(uuid_col uuid,  description text);

-- insert a generated systen function generated uuid
-- requires pgcrypto extension
insert into uuid_demo(uuid_col, description)
     values (gen_random_uuid(),'Systen Generated uuid') ;

-- show generated
select * from uuid_demo;
/*
uuid_col                            ;description
d41c9d09-0e21-4acf-b82c-41f009c84aeb;Systen Generated uuid
*/

-- query demo with generated uuid, both with and without formatting
select uuid_demo.*,'#1. Standard Form'  note       from uuid_demo where uuid_col = 'd41c9d09-0e21-4acf-b82c-41f009c84aeb'
union all
select uuid_demo.*,'#2. Standard, cast'            from uuid_demo where uuid_col = 'd41c9d09-0e21-4acf-b82c-41f009c84aeb'::uuid
union all
select uuid_demo.*,'#3 Non Standard Upper case'    from uuid_demo where uuid_col = 'D41C9D09-0E21-4ACF-B82C-41F009C84AEB'
union all
select uuid_demo.*,'#4 Non Standard Unformatted'   from uuid_demo where uuid_col = 'd41c9d090e214acfb82c41f009c84aeb'
union all
select uuid_demo.*,'#5 Non Std. Upper Unformatted' from uuid_demo where uuid_col = 'D41C9D090E214ACFB82C41F009C84AEB'
union all
select uuid_demo.*,'#7 Non Standard bracket'       from uuid_demo where uuid_col = '{d41c9d090e214acfb82c41f009c84aeb}';

/*
uuid_col                            ;description          ;note
d41c9d09-0e21-4acf-b82c-41f009c84aeb;Systen Generated uuid;#1. Standard Form
d41c9d09-0e21-4acf-b82c-41f009c84aeb;Systen Generated uuid;#2. Standard, cast
d41c9d09-0e21-4acf-b82c-41f009c84aeb;Systen Generated uuid;#3. Non Standard Upper case
d41c9d09-0e21-4acf-b82c-41f009c84aeb;Systen Generated uuid;#4. Non Standard Unformatted
d41c9d09-0e21-4acf-b82c-41f009c84aeb;Systen Generated uuid;#5. Non Std. Upper Unformatted
d41c9d09-0e21-4acf-b82c-41f009c84aeb;Systen Generated uuid;#6. Non Standard bracket
现在谈谈你的直接问题:你应该如何引用它。在这里,vitaly_t是正确的,因为在您的代码中,只需将其作为字符串处理即可

尝试1的标准。这是Postgres将始终作为输出提供的表单。 请尝试Postgres cast操作符::2。 请尝试上面未显示的Postgres CAST指令。 我将尝试将每种格式转换为ExpressJS格式。但我不能保证我只是从你的帖子中复制和修改了。再次查看我先前的免责声明

const showoneuserquery='SELECT*FROM user,其中id=$1' const showoneuserquery='SELECT*FROM user,其中id=$1::uuid' const showoneuserquery='SELECT*FROM user,其中id=CAST$1作为UUID' 对于更新,删除操作遵循相同的格式


如果这些都不起作用,你还有另一个选择。因为您对username有一个唯一的约束,所以只需将其用于访问users表,然后在加入users表时使用UUID,它应该是UUID列名到UUID列名。无需特别说明。

您能提供express路由器的代码吗?@Khauri I在控制器文件中将路由器业务逻辑与路由分开。我处理的代码是我问题中指出的业务逻辑。但可能我不理解您所指的代码。请在运行命名操作后使用您面临的错误/情况更新您的问题。您说您在使用req.params.id引用id时遇到问题?通常,您会在路由器中指定该值,如:router.get'/:id',getSingleUser。如果这不是你的意思,你能更具体地说明你所犯的错误吗?几条注释。首先,我不知道ExpressJS的免责声明。但是您声称正在将uuid用于表的主。但是,这不是在数据库表级别定义它的方式。您发布的表DDL将id定义为BIGSERIAL。该定义使其成为64位整数。但是,uuid是一个32字节的十六进制值。此外,用户是Postgres和SQL标准。如果Postgres开发团队决定更严格地遵循SQL标准,那么现在就可以不用使用它了。Thanks@Belayer,我已经使用用户名进行了测试,它在您的答案出现之前就可以工作了。我将按照您的建议,在加入表时使用uuid。我还将在应用程序的某些方面继续使用uuid。
function getSingleUser(req, res, next) {
  const showoneuserquery = 'SELECT * FROM user where id = $1'
  const user_id = parseInt(req.params.id);
  db.one(showoneuserquery, user_id)
    .then(function (data) {
      res.status(200)
        .json({
          status: 'success',
          data: data,
          message: 'Retrieved ONE user'
        });
    })

    .catch(function (err) {
      return next(err);
    });
}
function editUser(req, res, next) {
  const edituserquery = 'update user set username=$1, password=$2, confirm_password=$3  where id=$4'
  db.none(edituserquery, [req.body.username, req.body.password, req.body.confirm_password, (req.params.id)])
    .then(function () {
      res.status(200)
        .json({
          status: 'success',
          message: 'Updated user'
        });
    })
    .catch(function (err) {
      return next(err);
    });
}
create table uuid_demo(uuid_col uuid,  description text);

-- insert a generated systen function generated uuid
-- requires pgcrypto extension
insert into uuid_demo(uuid_col, description)
     values (gen_random_uuid(),'Systen Generated uuid') ;

-- show generated
select * from uuid_demo;
/*
uuid_col                            ;description
d41c9d09-0e21-4acf-b82c-41f009c84aeb;Systen Generated uuid
*/

-- query demo with generated uuid, both with and without formatting
select uuid_demo.*,'#1. Standard Form'  note       from uuid_demo where uuid_col = 'd41c9d09-0e21-4acf-b82c-41f009c84aeb'
union all
select uuid_demo.*,'#2. Standard, cast'            from uuid_demo where uuid_col = 'd41c9d09-0e21-4acf-b82c-41f009c84aeb'::uuid
union all
select uuid_demo.*,'#3 Non Standard Upper case'    from uuid_demo where uuid_col = 'D41C9D09-0E21-4ACF-B82C-41F009C84AEB'
union all
select uuid_demo.*,'#4 Non Standard Unformatted'   from uuid_demo where uuid_col = 'd41c9d090e214acfb82c41f009c84aeb'
union all
select uuid_demo.*,'#5 Non Std. Upper Unformatted' from uuid_demo where uuid_col = 'D41C9D090E214ACFB82C41F009C84AEB'
union all
select uuid_demo.*,'#7 Non Standard bracket'       from uuid_demo where uuid_col = '{d41c9d090e214acfb82c41f009c84aeb}';

/*
uuid_col                            ;description          ;note
d41c9d09-0e21-4acf-b82c-41f009c84aeb;Systen Generated uuid;#1. Standard Form
d41c9d09-0e21-4acf-b82c-41f009c84aeb;Systen Generated uuid;#2. Standard, cast
d41c9d09-0e21-4acf-b82c-41f009c84aeb;Systen Generated uuid;#3. Non Standard Upper case
d41c9d09-0e21-4acf-b82c-41f009c84aeb;Systen Generated uuid;#4. Non Standard Unformatted
d41c9d09-0e21-4acf-b82c-41f009c84aeb;Systen Generated uuid;#5. Non Std. Upper Unformatted
d41c9d09-0e21-4acf-b82c-41f009c84aeb;Systen Generated uuid;#6. Non Standard bracket