import type { StateIncrementer } from '@sammo/server_util'; import SchemaSequence from './schema/SchemaSequence.js'; import { InvalidArgument } from '@sammo/util'; export function MongoSequenceFactory(collectionName: string): StateIncrementer{ return async (increase: number) => { if(increase <= 0){ throw new InvalidArgument('increase must be > 0'); } increase = Math.ceil(increase); const result = await SchemaSequence.findOneAndUpdate({ collectionName, }, { $inc: { nextSeq: increase, }, }, { upsert: true, new: true, }); return result.nextSeq; } }