fix: gen random id for article

This commit is contained in:
Sergey Yarkov 2022-06-17 15:08:21 +03:00
parent 95b32bf672
commit dc89a9eb00
2 changed files with 4 additions and 2 deletions

View File

@ -1,4 +1,5 @@
import db from '#app/db'; import db from '#app/db';
import crypto from 'node:crypto';
/** /**
* ArticleModel typedef * ArticleModel typedef
@ -57,13 +58,14 @@ class ArticleModel {
try { try {
const count = await db.client.query('SELECT COUNT(*) FROM "articles"'); const count = await db.client.query('SELECT COUNT(*) FROM "articles"');
const values = Object.values({ const values = Object.values({
id: String(parseInt(count.rows[0].count, 10) + 1), id: String(parseInt(count.rows[0].count, 10) + crypto.randomInt(100)),
title: data.title, title: data.title,
description: data.description, description: data.description,
body: data.body, body: data.body,
created_at: new Date().toISOString(), created_at: new Date().toISOString(),
updated_at: new Date().toISOString(), updated_at: new Date().toISOString(),
}); });
const result = await db.client.query( const result = await db.client.query(
`INSERT INTO "articles" ("id", "title", "description", "body", "created_at", "updated_at") VALUES($1, $2, $3, $4, $5, $6) RETURNING *`, `INSERT INTO "articles" ("id", "title", "description", "body", "created_at", "updated_at") VALUES($1, $2, $3, $4, $5, $6) RETURNING *`,
values values

2
package-lock.json generated
View File

@ -7,7 +7,7 @@
"": { "": {
"name": "node-docker-example", "name": "node-docker-example",
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "MIT",
"dependencies": { "dependencies": {
"dotenv": "^16.0.0", "dotenv": "^16.0.0",
"handlebars": "^4.7.7", "handlebars": "^4.7.7",