【问题标题】:How to create an ExpressJS restful API with UUID and Postgres pg-promise library如何使用 UUID 和 Postgres pg-promise 库创建 ExpressJS RESTful API
【发布时间】:2020-04-07 15:39:59
【问题描述】:

我正在尝试在我的网络应用程序中为用户创建 CRUD。我使用 uuid 作为我的 id(也是下面用户表中显示的主键)。 我正在使用 pg-promise 库连接到 Postgres。创建用户和获取所有用户的路由代码正在工作。 但是我在创建 id const 以获取一个用户、删除用户或更新用户时如何引用 uuid 遇到了挑战,即 const user_id = parseInt(req.params.id);

请在下面查看我的示例代码。我也会感谢任何在线资源的例子。

我的用户表

CREATE TABLE m_user (
id uuid DEFAULT uuid_generate_v4() NOT NULL,
username text NOT NULL UNIQUE,
password text NOT NULL,
confirm_password text NOT NULL,
created_at timestamp NOT NULL default now(),
last_login timestamp NOT NULL default now(),
PRIMARY KEY (id)
);

CRUD 获得一个用户

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);
    });
}

CRUD 编辑用户

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);
    });
}

【问题讨论】:

  • 你能提供你的快速路由器的代码吗?
  • @Khauri 我将路由器业务逻辑与控制器文件中的路由分开。我处理的代码是我的问题中指出的业务逻辑。但也许我不明白你所指的代码。
  • 请用您在运行指定操作后遇到的错误/情况更新您的问题。
  • 您说您在使用 req.params.id 引用 ID 时遇到问题?通常您会在路由器中指定该值,如下所示:router.get('/:id', getSingleUser)。如果这不是您的意思,您能否更具体地说明您遇到的错误?
  • 一些笔记。首先是我不知道 ExpressJS 的免责声明。但是......你声称你正在使用 uuid 作为你的表的主要。但是,这不是在数据库表级别定义的方式。您发布的表 DDL 将 id 定义为 BIGSERIAL。该定义使其成为 64 位整数。但是,uuid 是一个 32 字节的十六进制值。此外,user 是 Postgres 和 SQL 标准 reserved word。虽然你现在可以侥幸使用它,但如果 Postgres 开发团队决定更接近 SQL 标准,它可能会消失。

标签: node.js postgresql uuid


【解决方案1】:

好的,关于 UUID 的一些说明。 @vitaly_t 不太正确 uuid 不仅仅是字符串。虽然在视觉上它们显示为字符串,并且您可以将它们作为字符串处理,但它们是 UUID。具有独特属性的字符串最值得注意的是several representations 格式。下面的一个小演示展示了这一点。字符串不能在功能上做到这一点。

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. 尝试 (#1) 的标准。这是 Postgres 将始终作为输出提供的形式。
  2. 尝试 Postgres 强制转换运算符 (::) (#2)。
  3. 试试 Postgres CAST 指令(上面没有显示)。

我会尝试将这些都放入 ExpressJS 格式。但我不保证(我只是从您的帖子中复制和修改)。再次查看我之前的免责声明。

  1. const showoneuserquery = 'SELECT * FROM user where id = $1'
  2. const showoneuserquery = 'SELECT * FROM user where id = $1::uuid'
  3. const showoneuserquery = 'SELECT * FROM user where id = CAST ($1 as UUID)'

对于更新,删除操作遵循相同的格式。

如果这些都不起作用,您还有其他选择。由于您对用户名有唯一约束,只需将其用于访问用户表,那么在加入用户表时只需使用 UUID,它应该是 UUID 列名到 UUID 列名。没有什么特别需要的。

【讨论】:

  • 谢谢@Belayer,我已经使用用户名进行了测试,并且在您的答案出现之前它可以工作。我会按照您的建议并在加入表格时使用 uuid。我还有应用程序的某些方面仍然会使用 uuid。
猜你喜欢
  • 2019-11-12
  • 2018-01-07
  • 2015-11-23
  • 2018-08-19
  • 2021-08-11
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
  • 2018-03-15
相关资源
最近更新 更多