2017-11-22 21:10:34 +00:00
|
|
|
// @flow
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as db from '../common/database';
|
2017-11-22 21:10:34 +00:00
|
|
|
import type {BaseModel} from './index';
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export const name = 'Stats';
|
2016-10-02 20:57:00 +00:00
|
|
|
export const type = 'Stats';
|
|
|
|
export const prefix = 'sta';
|
2017-03-23 22:10:42 +00:00
|
|
|
export const canDuplicate = false;
|
2016-11-22 19:42:10 +00:00
|
|
|
|
2017-11-22 21:10:34 +00:00
|
|
|
type BaseStats = {
|
|
|
|
currentLaunch: number | null,
|
|
|
|
lastLaunch: number | null,
|
|
|
|
currentVersion: string | null,
|
|
|
|
lastVersion: string | null,
|
|
|
|
launches: number
|
|
|
|
};
|
|
|
|
|
|
|
|
export type Stats = BaseModel & BaseStats;
|
|
|
|
|
|
|
|
export function init (): BaseStats {
|
2016-11-10 01:15:27 +00:00
|
|
|
return {
|
2017-11-22 21:10:34 +00:00
|
|
|
currentLaunch: null,
|
|
|
|
lastLaunch: null,
|
|
|
|
currentVersion: null,
|
2016-10-02 20:57:00 +00:00
|
|
|
lastVersion: null,
|
|
|
|
launches: 0
|
2016-11-10 01:15:27 +00:00
|
|
|
};
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2017-11-22 21:10:34 +00:00
|
|
|
export function migrate (doc: Stats): Stats {
|
2016-11-22 19:42:10 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2017-11-22 21:10:34 +00:00
|
|
|
export function create (patch: Object = {}): Promise<Stats> {
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.docCreate(type, patch);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2017-11-22 21:10:34 +00:00
|
|
|
export async function update (patch: Object): Promise<Stats> {
|
2016-10-02 20:57:00 +00:00
|
|
|
const stats = await get();
|
|
|
|
return db.docUpdate(stats, patch);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2017-11-22 21:10:34 +00:00
|
|
|
export async function get (): Promise<Stats> {
|
2016-10-02 20:57:00 +00:00
|
|
|
const results = await db.all(type);
|
|
|
|
if (results.length === 0) {
|
2017-11-20 16:07:36 +00:00
|
|
|
return create();
|
2016-10-02 20:57:00 +00:00
|
|
|
} else {
|
|
|
|
return results[0];
|
|
|
|
}
|
|
|
|
}
|