Created
April 25, 2019 18:56
-
-
Save nicolaslopezj/f1b52ea90a60630de3a828090d022632 to your computer and use it in GitHub Desktop.
Autoincremente con Orionjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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