SFT/mock-server/utils/id.js

17 lines
665 B
JavaScript

// 全局数字自增ID生成器,按实体类型区分独立序列,与 swagger 中 id 字段 format:int64 的
// "数字主键"约定保持一致(区别于旧mock使用字符串id如 u001 的做法)。
const counters = new Map()
// 各 seed 文件在写入固定初始数据后,调用本函数把序列起点设置到"最大已用id + 1",
// 避免后续新增数据与种子数据id冲突。
export function initCounter(type, startValue) {
const current = counters.get(type) || 1
counters.set(type, Math.max(current, startValue))
}
export function nextId(type) {
const current = counters.get(type) || 1
counters.set(type, current + 1)
return current
}