Added SQLite database with migrations

This commit is contained in:
unknown
2021-09-20 14:26:00 +02:00
parent fa6333a8fa
commit dd34f2ae02
7 changed files with 124 additions and 6 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
node_modules/
build/
build/
data/

47
src/db/index.ts Normal file
View File

@@ -0,0 +1,47 @@
require('ts-node/register');
import { Sequelize } from 'sequelize';
import { Umzug, SequelizeStorage } from 'umzug';
import { Logger } from '../utils';
const logger = new Logger();
// DB config
export const sequelize = new Sequelize({
dialect: 'sqlite',
storage: 'data/db.sqlite3',
logging: false
});
// Migrations config
const umzug = new Umzug({
migrations: { glob: '**/migrations/*.ts' },
context: sequelize.getQueryInterface(),
storage: new SequelizeStorage({ sequelize }),
logger: undefined
});
export type Migration = typeof umzug._types.migration;
export const connectDB = async () => {
try {
// Create & connect db
await sequelize.authenticate();
logger.log('Database connected');
// Check migrations
const pendingMigrations = await umzug.pending();
if (pendingMigrations.length > 0) {
logger.log(`Found pending migrations. Executing...`);
}
await umzug.up();
} catch (err) {
logger.log(`Database connection error`, 'ERROR');
if (process.env.NODE_ENV == 'development') {
console.log(err);
}
process.exit(1);
}
};

View File

@@ -0,0 +1,45 @@
import { DataTypes, Model } from 'sequelize';
import type { Migration } from '../';
import {
Snippet,
SnippetCreationAttributes
} from '../../typescript/interfaces';
const { INTEGER, STRING, DATE } = DataTypes;
export const up: Migration = async ({
context: queryInterface
}): Promise<void> => {
await queryInterface.createTable<Model<Snippet, SnippetCreationAttributes>>(
'snippets',
{
id: {
type: INTEGER,
allowNull: false,
primaryKey: true
},
title: {
type: STRING,
allowNull: false
},
language: {
type: STRING,
allowNull: false
},
createdAt: {
type: DATE,
allowNull: false
},
updatedAt: {
type: DATE,
allowNull: false
}
}
);
};
export const down: Migration = async ({
context: queryInterface
}): Promise<void> => {
await queryInterface.dropTable('snippets');
};

View File

@@ -1,6 +1,7 @@
import dotenv from 'dotenv';
import express from 'express';
import { Logger } from './utils';
import { connectDB } from './db';
// Env config
dotenv.config({ path: './src/config/.env' });
@@ -9,8 +10,15 @@ const app = express();
const logger = new Logger();
const PORT = process.env.PORT;
app.listen(PORT, () => {
logger.log(
`Server is working on port ${PORT} in ${process.env.NODE_ENV} mode`
);
});
// App config
app.use(express.json());
(async () => {
await connectDB();
app.listen(PORT, () => {
logger.log(
`Server is working on port ${PORT} in ${process.env.NODE_ENV} mode`
);
});
})();

View File

@@ -0,0 +1,5 @@
export interface Model {
id: number;
createdAt: Date;
updatedAt: Date;
}

View File

@@ -0,0 +1,10 @@
import { Model } from '.';
import { Optional } from 'sequelize';
export interface Snippet extends Model {
title: string;
language: string;
}
export interface SnippetCreationAttributes
extends Optional<Snippet, 'id' | 'createdAt' | 'updatedAt'> {}

View File

@@ -0,0 +1,2 @@
export * from './Model';
export * from './Snippet';