Skip to content

Instantly share code, notes, and snippets.

@nicolaslopezj
Created April 25, 2019 18:56
Show Gist options
  • Save nicolaslopezj/f1b52ea90a60630de3a828090d022632 to your computer and use it in GitHub Desktop.
Save nicolaslopezj/f1b52ea90a60630de3a828090d022632 to your computer and use it in GitHub Desktop.
Autoincremente con Orionjs
import {Collection} from '@orion-js/app'
const Counters = new Collection({
name: 'autoIncrement_counters',
indexes: [
{
keys: {name: 1},
unique: true
}
]
})
// const nextNumber = getCount('counterId')
export default async function getCounter(name) {
const result = await Counters.findOneAndUpdate(
{name},
{$inc: {counter: 1}},
{upsert: true, new: true}
)
if (!result) return 1
return result.counter + 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment