Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1import pkg from 'sequelize';
2
3/* Bank models */
4import LocationBankLocation from './banks/LocationBankLocationModel.js';
5import LocationBank from './banks/LocationBankModel.js';
6/* High level models */
7import Org from './OrgModel.js';
8import Policy from './PolicyModel.js';
9/* Rules models */
10import Action from './rules/ActionModel.js';
11import Backtest from './rules/BacktestModel.js';
12import ItemType from './rules/ItemTypeModel.js';
13import RuleLatestVersion from './rules/RuleLatestVersionModel.js';
14import Rule from './rules/RuleModel.js';
15/* Other */
16import { makeSequelize, maketransactionWithRetry } from './sequelizeSetup.js';
17import User from './UserModel.js';
18
19const { Sequelize } = pkg;
20
21// NB: this type includes a bunch of exports that are not the DataType constructors,
22// but at least it also includes the DataTypes, so that we get autocomplete.
23// I don't think the DataTypes type is actually exported on its own.
24// eslint-disable-next-line @typescript-eslint/consistent-type-imports
25export type DataTypes = typeof import('sequelize');
26
27/* eslint-disable @typescript-eslint/no-explicit-any */
28const makeDb = () => {
29 const sequelize = makeSequelize();
30 const db = {
31 sequelize,
32 Sequelize,
33 Action: Action(sequelize, Sequelize as any),
34 Backtest: Backtest(sequelize, Sequelize as any),
35 ItemType: ItemType(sequelize, Sequelize as any),
36 LocationBank: LocationBank(sequelize, Sequelize as any),
37 LocationBankLocation: LocationBankLocation(sequelize, Sequelize as any),
38 Org: Org(sequelize, Sequelize as any),
39 Policy: Policy(sequelize, Sequelize as any),
40 Rule: Rule(sequelize, Sequelize as any),
41 RuleLatestVersion: RuleLatestVersion(sequelize, Sequelize as any),
42 User: User(sequelize, Sequelize as any),
43 transactionWithRetry: maketransactionWithRetry(sequelize),
44 async close() {
45 await sequelize.close();
46 },
47 };
48 Object.values(db).forEach((model) => {
49 if ('associate' in model) {
50 model.associate(db);
51 }
52 });
53 return db;
54};
55
56export default makeDb;