summaryrefslogtreecommitdiff
path: root/src/misc/gen-id.ts
blob: 5ee65e817716c459c02b68de411ee2a9dc4c2f9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { ulid } from 'ulid';
import { genAid } from './id/aid';
import { genMeid } from './id/meid';
import { genObjectId } from './id/object-id';
import config from '../config';

const metohd = config.id.toLowerCase();

export function genId(date?: Date): string {
	if (!date || (date > new Date())) date = new Date();

	switch (metohd) {
		case 'aid': return genAid(date);
		case 'meid': return genMeid(date);
		case 'ulid': return ulid(date.getTime());
		case 'objectid': return genObjectId(date);
		default: throw 'unknown id generation method';
	}
}