How to generate a unique invoice id in Sails.js

时间:2022-11-25 13:26:42

I need to make Sails generate an unique ID for my invoices with the current format:

我需要让Sails为我的发票生成一个唯一的ID,使用当前格式:

 <auto-incremented-number>/<current-year>

like: 8/2016, 122/2099, 122/2100

喜欢:8 / 2016,122 / 199,122 / 2100

the idea was to follow the example in the documentation

我的想法是遵循文档中的示例

 function getLatestId() {
   //stuck here
   return 42;
 }

 const current_year = new Date().getFullYear()
 const calculated_id = getLatestId() +"/"+ current_year

 Invoice.create({ data:'...', id: calculated_id }).exec(function (err, item){
   if (err) { return res.serverError(err); }

   sails.log('Invoice created:', item.id);
   return res.ok();
 });

The problem is that even with a working getLatestId function the code is ugly and it code smells from a mile.

问题是,即使使用了一个有效的getLatestId函数,代码也很丑陋而且代码闻起来有一英里的气味。

Even trying to update the id in the exec callback of the create function looks odd, because an item with a specific id is already created.

甚至尝试更新create函数的exec回调中的id看起来很奇怪,因为已经创建了具有特定id的项。

I cannot believe that sails doesn't have something to address a similar scenario, but I can't find anything in the docs either.

我无法相信帆没有解决类似情况的问题,但我也无法在文档中找到任何内容。

1 个解决方案

#1


1  

1.You need to check if this generated ID exist

1.您需要检查此生成的ID是否存在

2.To generate uID under node use crypto

2.在节点使用加密下生成uID

const crypto = require('crypto');
const uID = crypto.randomBytes(2).toString('hex');

Example:

'use strict';
const crypto = require('crypto');

function generateID(callback) {
  let uID = crypto.randomBytes(2).toString('hex'); //eg.uID = 3d4f
  let current_year = new Date().getFullYear();
  let uID = uID +"/"+ current_year;

  Invoice.findOne({id: uID}).exec(function(err, invoice) {
    if(err) {
      //catch Error
    } else {
      if(invoice) {
        // nope... uID exist
        return generateID(callback);
      } else {
        return callback(uID)
      }
    }
  });
}

generateID(function (uID) {
  Invoice.create({ data:'...', id: uID }).exec(function (err, item) {
    if (err) {
      // return res.serverError(err);  <-- It's bad idea sending whole err to client. It's may expose your app architecture
      sails.log.error(err);
      return res.serverError({errCode: 001, message: '<Your custom message>'});
    } else {
      sails.log('Invoice created:', item.id);
      // return res.ok(); <-- It's OK, but if u use new version of sailsJS you should have created response (201). You can see code in responses/created.js
      return res.created();
    }

  });
});

Here is few another methods to generate random: https://blog.tompawlak.org/generate-random-values-nodejs-javascript

这里有几个生成随机的方法:https://blog.tompawlak.org/generate-random-values-nodejs-javascript

#1


1  

1.You need to check if this generated ID exist

1.您需要检查此生成的ID是否存在

2.To generate uID under node use crypto

2.在节点使用加密下生成uID

const crypto = require('crypto');
const uID = crypto.randomBytes(2).toString('hex');

Example:

'use strict';
const crypto = require('crypto');

function generateID(callback) {
  let uID = crypto.randomBytes(2).toString('hex'); //eg.uID = 3d4f
  let current_year = new Date().getFullYear();
  let uID = uID +"/"+ current_year;

  Invoice.findOne({id: uID}).exec(function(err, invoice) {
    if(err) {
      //catch Error
    } else {
      if(invoice) {
        // nope... uID exist
        return generateID(callback);
      } else {
        return callback(uID)
      }
    }
  });
}

generateID(function (uID) {
  Invoice.create({ data:'...', id: uID }).exec(function (err, item) {
    if (err) {
      // return res.serverError(err);  <-- It's bad idea sending whole err to client. It's may expose your app architecture
      sails.log.error(err);
      return res.serverError({errCode: 001, message: '<Your custom message>'});
    } else {
      sails.log('Invoice created:', item.id);
      // return res.ok(); <-- It's OK, but if u use new version of sailsJS you should have created response (201). You can see code in responses/created.js
      return res.created();
    }

  });
});

Here is few another methods to generate random: https://blog.tompawlak.org/generate-random-values-nodejs-javascript

这里有几个生成随机的方法:https://blog.tompawlak.org/generate-random-values-nodejs-javascript