typeorm 0.3, Dockerfile optmizations

This commit is contained in:
Charlie Laabs
2022-03-25 22:38:20 -05:00
parent f89bed9e5d
commit 4215b5ae13
7 changed files with 142 additions and 167 deletions

View File

@@ -6,7 +6,7 @@ import Markov, {
MarkovConstructorOptions,
AddDataProps,
} from 'markov-strings-db';
import { createConnection } from 'typeorm';
import { DataSource } from 'typeorm';
import { MarkovInputData } from 'markov-strings-db/dist/src/entity/MarkovInputData';
import type { PackageJsonPerson } from 'types-package-json';
import makeEta from 'simple-eta';
@@ -27,6 +27,7 @@ import {
trainCommand,
} from './deploy-commands';
import { getRandomElement, getVersion, packageJson } from './util';
import ormconfig from './ormconfig';
interface MarkovDataCustom {
attachments: string[];
@@ -87,7 +88,7 @@ function getGuildChannelId(channel: Discord.TextBasedChannel): string | null {
async function isValidChannel(channel: Discord.TextBasedChannel): Promise<boolean> {
const channelId = getGuildChannelId(channel);
if (!channelId) return false;
const dbChannel = await Channel.findOne(channelId);
const dbChannel = await Channel.findOneBy({ id: channelId });
return dbChannel?.listen || false;
}
@@ -97,7 +98,7 @@ function isHumanAuthoredMessage(message: Discord.Message | Discord.PartialMessag
async function getValidChannels(guild: Discord.Guild): Promise<Discord.TextChannel[]> {
L.trace('Getting valid channels from database');
const dbChannels = await Channel.find({ guild: Guild.create({ id: guild.id }), listen: true });
const dbChannels = await Channel.findBy({ guild: { id: guild.id }, listen: true });
L.trace({ dbChannels: dbChannels.map((c) => c.id) }, 'Valid channels from database');
const channels = (
await Promise.all(
@@ -864,8 +865,9 @@ client.on('interactionCreate', async (interaction) => {
* Loads the config settings from disk
*/
async function main(): Promise<void> {
const connection = await Markov.extendConnectionOptions();
await createConnection(connection);
const dataSourceOptions = Markov.extendDataSourceOptions(ormconfig);
const dataSource = new DataSource(dataSourceOptions);
await dataSource.initialize();
await client.login(config.token);
}

38
src/ormconfig.ts Normal file
View File

@@ -0,0 +1,38 @@
import { DataSourceOptions } from 'typeorm';
import { Channel } from './entity/Channel';
import { Guild } from './entity/Guild';
import { CreateTables1640838214672 } from './migration/1640838214672-CreateTables';
const ENTITIES = [Channel, Guild];
const MIGRATIONS = [CreateTables1640838214672];
// const SUBSCRIBERS = [];
const devConfig: DataSourceOptions = {
type: 'better-sqlite3',
database: process.env.CONFIG_DIR
? `${process.env.CONFIG_DIR}/db/db.sqlite3`
: 'config/db/db.sqlite3',
synchronize: true,
migrationsRun: false,
// logging: 'all',
entities: ENTITIES,
migrations: MIGRATIONS,
// subscribers: SUBSCRIBERS,
};
const prodConfig: DataSourceOptions = {
type: 'better-sqlite3',
database: process.env.CONFIG_DIR
? `${process.env.CONFIG_DIR}/db/db.sqlite3`
: 'config/db/db.sqlite3',
synchronize: false,
logging: false,
entities: ENTITIES,
migrations: MIGRATIONS,
migrationsRun: true,
// subscribers: SUBSCRIBERS,
};
const finalConfig = process.env.NODE_ENV !== 'production' ? devConfig : prodConfig;
export default finalConfig;