2017-12-17 21:22:09 -05:00
|
|
|
import { Model, Sequelize } from "sequelize-typescript";
|
|
|
|
import config from "../config";
|
|
|
|
import { LogService } from "matrix-js-snippets";
|
|
|
|
import User from "./models/User";
|
|
|
|
import UserScalarToken from "./models/UserScalarToken";
|
|
|
|
import Upstream from "./models/Upstream";
|
|
|
|
import * as Promise from "bluebird";
|
2017-12-18 23:44:01 -05:00
|
|
|
import WidgetRecord from "./models/WidgetRecord";
|
|
|
|
import * as path from "path";
|
|
|
|
import * as Umzug from "umzug";
|
2017-12-24 04:02:57 -05:00
|
|
|
import AppService from "./models/AppService";
|
|
|
|
import AppServiceUser from "./models/AppServiceUser";
|
2017-12-17 21:22:09 -05:00
|
|
|
|
|
|
|
class _DimensionStore {
|
|
|
|
private sequelize: Sequelize;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.sequelize = new Sequelize({
|
|
|
|
dialect: 'sqlite',
|
|
|
|
database: "dimension",
|
|
|
|
storage: config.database.file,
|
|
|
|
username: "",
|
|
|
|
password: "",
|
|
|
|
logging: i => LogService.verbose("DimensionStore [SQL]", i)
|
|
|
|
});
|
|
|
|
this.sequelize.addModels(<Array<typeof Model>>[
|
|
|
|
User,
|
|
|
|
UserScalarToken,
|
|
|
|
Upstream,
|
2017-12-18 23:44:01 -05:00
|
|
|
WidgetRecord,
|
2017-12-24 04:02:57 -05:00
|
|
|
AppService,
|
|
|
|
AppServiceUser,
|
2017-12-17 21:22:09 -05:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public updateSchema(): Promise<any> {
|
|
|
|
LogService.info("DimensionStore", "Updating schema...");
|
2017-12-18 23:44:01 -05:00
|
|
|
|
|
|
|
const migrator = new Umzug({
|
|
|
|
storage: "sequelize",
|
|
|
|
storageOptions: {sequelize: this.sequelize},
|
|
|
|
migrations: {
|
|
|
|
params: [this.sequelize.getQueryInterface()],
|
|
|
|
path: path.join(__dirname, "migrations"),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return migrator.up();
|
2017-12-17 21:22:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-24 04:02:57 -05:00
|
|
|
export const DimensionStore = new _DimensionStore();
|
|
|
|
|
|
|
|
export function resolveIfExists<T>(record: T): Promise<T> {
|
|
|
|
if (!record) return Promise.reject("Record not found");
|
|
|
|
return Promise.resolve(record);
|
|
|
|
}
|