2017-11-22 21:10:34 +00:00
|
|
|
// @flow
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as db from '../common/database';
|
2018-06-25 17:42:50 +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;
|
2019-04-18 00:50:03 +00:00
|
|
|
export const canSync = 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,
|
2018-12-12 17:36:11 +00:00
|
|
|
launches: number,
|
2017-11-22 21:10:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type Stats = BaseModel & BaseStats;
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
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,
|
2018-12-12 17:36:11 +00:00
|
|
|
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
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function migrate(doc: Stats): Stats {
|
2016-11-22 19:42:10 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +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
|
|
|
|
2018-06-25 17:42:50 +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
|
|
|
|
2018-06-25 17:42:50 +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];
|
|
|
|
}
|
|
|
|
}
|