mirror of
https://github.com/pawelmalak/snippet-box.git
synced 2025-12-21 13:23:05 +01:00
Added SQLite database with migrations
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
build/
|
build/
|
||||||
|
data/
|
||||||
47
src/db/index.ts
Normal file
47
src/db/index.ts
Normal 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);
|
||||||
|
}
|
||||||
|
};
|
||||||
45
src/db/migrations/00_initial.ts
Normal file
45
src/db/migrations/00_initial.ts
Normal 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');
|
||||||
|
};
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { Logger } from './utils';
|
import { Logger } from './utils';
|
||||||
|
import { connectDB } from './db';
|
||||||
|
|
||||||
// Env config
|
// Env config
|
||||||
dotenv.config({ path: './src/config/.env' });
|
dotenv.config({ path: './src/config/.env' });
|
||||||
@@ -9,8 +10,15 @@ const app = express();
|
|||||||
const logger = new Logger();
|
const logger = new Logger();
|
||||||
const PORT = process.env.PORT;
|
const PORT = process.env.PORT;
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
// App config
|
||||||
logger.log(
|
app.use(express.json());
|
||||||
`Server is working on port ${PORT} in ${process.env.NODE_ENV} mode`
|
|
||||||
);
|
(async () => {
|
||||||
});
|
await connectDB();
|
||||||
|
|
||||||
|
app.listen(PORT, () => {
|
||||||
|
logger.log(
|
||||||
|
`Server is working on port ${PORT} in ${process.env.NODE_ENV} mode`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
|||||||
5
src/typescript/interfaces/Model.ts
Normal file
5
src/typescript/interfaces/Model.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
export interface Model {
|
||||||
|
id: number;
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
|
}
|
||||||
10
src/typescript/interfaces/Snippet.ts
Normal file
10
src/typescript/interfaces/Snippet.ts
Normal 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'> {}
|
||||||
2
src/typescript/interfaces/index.ts
Normal file
2
src/typescript/interfaces/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export * from './Model';
|
||||||
|
export * from './Snippet';
|
||||||
Reference in New Issue
Block a user