Browse Source

auto commit

pull/19/MERGE
yitter 2 years ago
parent
commit
eef43ebd79
2 changed files with 215 additions and 215 deletions
  1. +37
    -37
      SQL/README.md
  2. +178
    -178
      TypeScript/README.md

+ 37
- 37
SQL/README.md View File

@@ -1,37 +1,37 @@
# ❄ idgenerator-SQL
## 介绍
这里是在不同数据库中生成雪花ID的SQL脚本。
## SQL Server
设计说明:
```
1.这是SQL Server的一个内置方法,运行此脚本后,将在SQL Server的“可编程性-函数-标量值函数”中增加一个方法 Fn_NextSnowId
2.生成的ID = 时间差 + WokerId + 随机数
时间差 = 当前时间戳(毫秒单位) - 1582136402000
WorkerId = {配置值}
随机数 = 5 至 2^SeqBigLength-1 之间的整数
3.调用方法:
例如:select dbo.Fn_NextSnowId(rand())
说明:必须带 dbo. 前缀
4.自动赋值:
如果主键设置为雪花ID类型(bigint),可以将该主键的 “默认值或绑定” 设置为 ([dbo].[Fn_NextSnowId](rand())),以便在Insert记录时,能给主键自动赋值,而无需外部传入。
```
在执行函数之前,必须设置好以下3个参数:
```
set @WorkerId = 1 -- 最大值 2^@WorkerIdBigLength-1
set @WorkerIdBigLength = 4 -- 规则约束:@WorkerIdBigLength+@SeqBigLength<23
set @SeqBigLength = 8 -- 建议不小于6,在当前SQL版本中,@SeqBigLength 决定随机数的最大值(未采用自增数,这需要数据表记录Seq值)
```
## 其它思路
除了用SQL Server方法生成雪花ID之外,还可让SQL Server调用外部dll生成ID。参考:https://www.cnblogs.com/woxpp/p/3990277.html
# ❄ idgenerator-SQL
## 介绍
这里是在不同数据库中生成雪花ID的SQL脚本。
## SQL Server
设计说明:
```
1.这是SQL Server的一个内置方法,运行此脚本后,将在SQL Server的“可编程性-函数-标量值函数”中增加一个方法 Fn_NextSnowId
2.生成的ID = 时间差 + WokerId + 随机数
时间差 = 当前时间戳(毫秒单位) - 1582136402000
WorkerId = {配置值}
随机数 = 5 至 2^SeqBigLength-1 之间的整数
3.调用方法:
例如:select dbo.Fn_NextSnowId(rand())
说明:必须带 dbo. 前缀
4.自动赋值:
如果主键设置为雪花ID类型(bigint),可以将该主键的 “默认值或绑定” 设置为 ([dbo].[Fn_NextSnowId](rand())),以便在Insert记录时,能给主键自动赋值,而无需外部传入。
```
在执行函数之前,必须设置好以下3个参数:
```
set @WorkerId = 1 -- 最大值 2^@WorkerIdBigLength-1
set @WorkerIdBigLength = 4 -- 规则约束:@WorkerIdBigLength+@SeqBigLength<23
set @SeqBigLength = 8 -- 建议不小于6,在当前SQL版本中,@SeqBigLength 决定随机数的最大值(未采用自增数,这需要数据表记录Seq值)
```
## 其它思路
除了用SQL Server方法生成雪花ID之外,还可让SQL Server调用外部dll生成ID。参考:https://www.cnblogs.com/woxpp/p/3990277.html

+ 178
- 178
TypeScript/README.md View File

@@ -1,178 +1,178 @@
# ❄ idgenerator-TypeScript
## 介绍
项目更多介绍参照:https://github.com/yitter/idgenerator
代码贡献者:zhupengfei(在 bubao 布宝 的JS基础上改版,感谢bubao 布宝)
js Number 类型最大数值:9007199254740992(16位),
在JS中没有bigint类型,所以建议将ID控制在16位以内,统一使用number类型
## 使用
### 1.文件引用
```js
import { snowflakeIdv1 } from '../snowflakeIdv1'
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId})
let id1 = gen1.NextId()
console.log(id1, id1.toString().length)
```
### 2.npm库安装
```sh
npm i simple-flakeid
```
```js
const snowId = require('simple-flakeid')
let gen1 = new snowId.SnowflakeIdv1({ workerId: 1 })
for (let i = 0; i < 10; i++) {
let id1 = gen1.NextId()
console.log(`${i} ID:${id1} ${typeof id1} length:${id1.toString().length}`)
}
```
## function
### function NextId()
根据输出数值判断,小于number最大值时输出number类型,大于时输出bigint
### function NextNumber()
始终输出number类型,超过时throw error
### function NextBigId()
始终输出bigint类型
## 测试
```bash
ts-node test/test1.ts
```
## 使用
```js
import { snowflakeIdv1 } from '../snowflakeIdv1'
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId})
let id1 = gen1.NextId()
console.log(id1, id1.toString().length)
```
## 示例
```js
import { snowflakeIdv1 } from '../snowflakeIdv1'
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 6 })
for (let i = 0; i < 10; i++) {
let id1 = gen1.NextId()
console.log(`${i} ID:${id1} 长度:${id1.toString().length}`)
}
$ ts-node test/test4.ts
0 ID:234712552579141 长度:15
1 ID:234712552587333 长度:15
2 ID:234712552587334 长度:15
3 ID:234712552587335 长度:15
4 ID:234712552587336 长度:15
5 ID:234712552591429 长度:15
6 ID:234712552591430 长度:15
7 ID:234712552591431 长度:15
8 ID:234712552591432 长度:15
9 ID:234712552591433 长度:15
import { snowflakeIdv1 } from '../snowflakeIdv1'
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 11 })
for (let i = 0; i < 10; i++) {
let id1 = gen1.NextId()
console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
}
$ ts-node test/test4.ts
0 ID:7510958933018629 number 长度:16
1 ID:7510958933280773 number 长度:16
2 ID:7510958933280774 number 长度:16
3 ID:7510958933280775 number 长度:16
4 ID:7510958933411845 number 长度:16
5 ID:7510958933411846 number 长度:16
6 ID:7510958933542917 number 长度:16
7 ID:7510958933542918 number 长度:16
8 ID:7510958933542919 number 长度:16
9 ID:7510958933673989 number 长度:16
import { snowflakeIdv1 } from '../snowflakeIdv1'
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 12 })
for (let i = 0; i < 10; i++) {
let id1 = gen1.NextId()
console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
}
$ ts-node test/test4.ts
0 ID:15021931987734533 bigint 长度:17
1 ID:15021931987996677 bigint 长度:17
2 ID:15021931987996678 bigint 长度:17
3 ID:15021931987996679 bigint 长度:17
4 ID:15021931987996680 bigint 长度:17
5 ID:15021931988258821 bigint 长度:17
6 ID:15021931988258822 bigint 长度:17
7 ID:15021931988258823 bigint 长度:17
8 ID:15021931988258824 bigint 长度:17
9 ID:15021931988520965 bigint 长度:17
import { snowflakeIdv1 } from '../snowflakeIdv1'
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 13 })
for (let i = 0; i < 10; i++) {
let id1 = gen1.NextId()
console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
}
$ ts-node test/test4.ts
0 ID:30043877337997317 bigint 长度:17
1 ID:30043877338521605 bigint 长度:17
2 ID:30043877338521606 bigint 长度:17
3 ID:30043877339045893 bigint 长度:17
4 ID:30043877339045894 bigint 长度:17
5 ID:30043877339045895 bigint 长度:17
6 ID:30043877339045896 bigint 长度:17
7 ID:30043877339570181 bigint 长度:17
8 ID:30043877339570182 bigint 长度:17
9 ID:30043877339570183 bigint 长度:17
```
## 其他帮助
在mysql中int类型最大长度是10位数字,由于本算法默认生成的是15位,最短也是11位,所以在mysql中需要使用bigint数据类型
# ❄ idgenerator-TypeScript
## 介绍
项目更多介绍参照:https://github.com/yitter/idgenerator
代码贡献者:zhupengfei(在 bubao 布宝 的JS基础上改版,感谢bubao 布宝)
js Number 类型最大数值:9007199254740992(16位),
在JS中没有bigint类型,所以建议将ID控制在16位以内,统一使用number类型
## 使用
### 1.文件引用
```js
import { snowflakeIdv1 } from '../snowflakeIdv1'
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId})
let id1 = gen1.NextId()
console.log(id1, id1.toString().length)
```
### 2.npm库安装
```sh
npm i simple-flakeid
```
```js
const snowId = require('simple-flakeid')
let gen1 = new snowId.SnowflakeIdv1({ workerId: 1 })
for (let i = 0; i < 10; i++) {
let id1 = gen1.NextId()
console.log(`${i} ID:${id1} ${typeof id1} length:${id1.toString().length}`)
}
```
## function
### function NextId()
根据输出数值判断,小于number最大值时输出number类型,大于时输出bigint
### function NextNumber()
始终输出number类型,超过时throw error
### function NextBigId()
始终输出bigint类型
## 测试
```bash
ts-node test/test1.ts
```
## 使用
```js
import { snowflakeIdv1 } from '../snowflakeIdv1'
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId})
let id1 = gen1.NextId()
console.log(id1, id1.toString().length)
```
## 示例
```js
import { snowflakeIdv1 } from '../snowflakeIdv1'
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 6 })
for (let i = 0; i < 10; i++) {
let id1 = gen1.NextId()
console.log(`${i} ID:${id1} 长度:${id1.toString().length}`)
}
$ ts-node test/test4.ts
0 ID:234712552579141 长度:15
1 ID:234712552587333 长度:15
2 ID:234712552587334 长度:15
3 ID:234712552587335 长度:15
4 ID:234712552587336 长度:15
5 ID:234712552591429 长度:15
6 ID:234712552591430 长度:15
7 ID:234712552591431 长度:15
8 ID:234712552591432 长度:15
9 ID:234712552591433 长度:15
import { snowflakeIdv1 } from '../snowflakeIdv1'
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 11 })
for (let i = 0; i < 10; i++) {
let id1 = gen1.NextId()
console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
}
$ ts-node test/test4.ts
0 ID:7510958933018629 number 长度:16
1 ID:7510958933280773 number 长度:16
2 ID:7510958933280774 number 长度:16
3 ID:7510958933280775 number 长度:16
4 ID:7510958933411845 number 长度:16
5 ID:7510958933411846 number 长度:16
6 ID:7510958933542917 number 长度:16
7 ID:7510958933542918 number 长度:16
8 ID:7510958933542919 number 长度:16
9 ID:7510958933673989 number 长度:16
import { snowflakeIdv1 } from '../snowflakeIdv1'
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 12 })
for (let i = 0; i < 10; i++) {
let id1 = gen1.NextId()
console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
}
$ ts-node test/test4.ts
0 ID:15021931987734533 bigint 长度:17
1 ID:15021931987996677 bigint 长度:17
2 ID:15021931987996678 bigint 长度:17
3 ID:15021931987996679 bigint 长度:17
4 ID:15021931987996680 bigint 长度:17
5 ID:15021931988258821 bigint 长度:17
6 ID:15021931988258822 bigint 长度:17
7 ID:15021931988258823 bigint 长度:17
8 ID:15021931988258824 bigint 长度:17
9 ID:15021931988520965 bigint 长度:17
import { snowflakeIdv1 } from '../snowflakeIdv1'
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 13 })
for (let i = 0; i < 10; i++) {
let id1 = gen1.NextId()
console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
}
$ ts-node test/test4.ts
0 ID:30043877337997317 bigint 长度:17
1 ID:30043877338521605 bigint 长度:17
2 ID:30043877338521606 bigint 长度:17
3 ID:30043877339045893 bigint 长度:17
4 ID:30043877339045894 bigint 长度:17
5 ID:30043877339045895 bigint 长度:17
6 ID:30043877339045896 bigint 长度:17
7 ID:30043877339570181 bigint 长度:17
8 ID:30043877339570182 bigint 长度:17
9 ID:30043877339570183 bigint 长度:17
```
## 其他帮助
在mysql中int类型最大长度是10位数字,由于本算法默认生成的是15位,最短也是11位,所以在mysql中需要使用bigint数据类型

Loading…
Cancel
Save