JavaScript 字符串id生成器
发布日期:2021-07-25 11:32:16
浏览次数:1
分类:技术文章
本文共 618 字,大约阅读时间需要 2 分钟。
可以作为数据库主键默认值使用
1、shortid
创建了非常短的非顺序 url 友好的唯一 id。非常适合 url 缩短器、MongoDB 和 Redis id,以及用户可能看到的任何其他 id。
- 适用于 Node、io.js 和 Web 浏览器。
- 可以生成任意数量的 id,没有重复,甚至每天数百万。
- 默认情况下 7-14 个 url 友好的字符:
A-Z
,a-z
,0-9
,_-
用法
const shortid = require('shortid');
console.log(shortid.generate());
Mongoose Unique Id(与数据库主键生成类似)
_id: {
'type': String,
'default': shortid.generate
},
2、Nano ID
一个小巧的、快速、安全的、URL 友好的、独特的 JavaScript 字符串 ID 生成器。
import { nanoid } from 'nanoid' model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
在 Node.js 中,您可以使用 CommonJS 导入:
const { nanoid } = require('nanoid')
如果要减小 ID 大小(并增加冲突概率),可以将大小作为参数传递。
nanoid(10) //=> "IRFa-VaY2b"
转载地址:https://blog.csdn.net/qq_32615575/article/details/118186850 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月23日 10时25分57秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
bash学习笔记
2019-04-27
sqlite学习
2019-04-27
手把手教你实现Unity与Android的交互
2019-04-27
手把手教你使用Unity的Behavior Designer
2019-04-27
Unity3D摄像机裁剪——NGUI篇
2019-04-27
lua深拷贝一个table
2019-04-27
app运行提示Unable to Initialize Unity Engine
2019-04-27
spring boot 与 Ant Design of Vue 实现修改按钮(十七)
2019-04-27
spring boot 与 Ant Design of Vue 实现删除按钮(十八)
2019-04-27
spring boot 与 Ant Design of Vue 实现新增角色(二十)
2019-04-27
spring boot 与 Ant Design of Vue 实现修改角色(二十一)
2019-04-27
spring boot 与 Ant Design of Vue 实现删除角色(补二十一)
2019-04-27
spring boot 与 Ant Design of Vue 实现左侧组织树(二十三)
2019-04-27
spring boot 与 Ant Design of Vue 实现新增组织(二十四)
2019-04-27
spring boot 与 Ant Design of Vue 实现修改组织(二十五)
2019-04-27
spring boot 与 Ant Design of Vue 实现删除组织(二十六)
2019-04-27
spring boot 与 Ant Design of Vue 实现删除用户(三十)
2019-04-27