A
A
Albert Ushakov2022-01-06 10:59:21
Node.js
Albert Ushakov, 2022-01-06 10:59:21

How to generate bonus card id using Sequelize on node.js?

Need to generate a 9-digit number such as 000000001 to save (or generate) in the database when adding a record, and then when adding new ones, generate a unique value of the autoIncrement type?
Is it possible to implement this in Sequelize on the side of the database itself (postgresql)?
In theory, you can do this: (0.00000001 <- Last value from the base (Somehow converted to float) + 0.00000001) * 10 but how to do this when adding without an extra request to get the last value.

I found pure sql on the net:

CREATE TABLE cpl(
  id serial,
  incre character varying default concat(substring('00000000',1,8 - length(currval('cpl_id_seq')::text)),currval('cpl_id_seq')),
  name text
);

How to turn this into a Sequelize option:
const User = sequelize.define('user', {
   pointCard: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true}
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Kudryavtsev, 2022-01-06
@Deissh

Sequelize can use Raw Queries if you don’t want to make 1 request in JS to get the latest record from the database and 1 update request with the generated bonus card ID.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question