-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
645 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { QueryInterface } from 'sequelize'; | ||
import { MigrationParams } from 'umzug' | ||
import { DataType } from 'sequelize-typescript'; | ||
|
||
type Migration = (params: MigrationParams<QueryInterface>) => Promise<unknown>; | ||
|
||
export const up: Migration = async ({ context: queryInterface }) => { | ||
//await queryInterface.createSchema() | ||
//await queryInterface.createTable(); | ||
//await queryInterface.addColumn(); | ||
//await queryInterface.changeColumn(); | ||
//await queryInterface.removeColumn(); | ||
//await queryInterface.dropTable(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Umzug, SequelizeStorage } from 'umzug'; | ||
import fs from 'node:fs'; | ||
import { client } from './src/db'; | ||
|
||
const umzug = new Umzug({ | ||
migrations: { glob: './src/migrations/*.ts' }, | ||
context: client.getQueryInterface(), | ||
storage: new SequelizeStorage({ sequelize: client }), | ||
logger: console, // log generated queries to console | ||
create: { | ||
template: filepath => [ | ||
[filepath, fs.readFileSync('example-migration.ts').toString()] | ||
], | ||
folder: './src/migrations', | ||
} | ||
}); | ||
|
||
if (require.main === module) { | ||
umzug.runAsCLI().then(() => process.exit()); | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,46 @@ | ||
import { Sequelize, SequelizeOptions } from 'sequelize-typescript'; | ||
import * as models from './models'; | ||
|
||
const config = { | ||
DB_URL: 'localhost', | ||
DB_USER: 'postgres', | ||
DB_PASS: 'postgres', | ||
DB_PORT: 5432, | ||
DB_NAME: 'scratch', | ||
}; | ||
|
||
const dbConfig: SequelizeOptions = { | ||
dialect: 'postgres', | ||
host: config.DB_URL, | ||
username: config.DB_USER, | ||
password: config.DB_PASS, | ||
port: config.DB_PORT, | ||
database: config.DB_NAME, | ||
logging: false, | ||
models: Object.values(models), | ||
}; | ||
|
||
const sequelize = new Sequelize(dbConfig); | ||
|
||
const initialize = async () => { | ||
try { | ||
await sequelize.authenticate(); | ||
await sequelize.sync({ alter: true }); | ||
console.log('All models were synchronized successfully.'); | ||
} catch (error) { | ||
console.error(`Error initializing database: ${error}`); | ||
} | ||
}; | ||
|
||
export { initialize, sequelize as client }; | ||
import { ModelAttributeColumnOptions } from 'sequelize'; | ||
import { Sequelize, SequelizeOptions } from 'sequelize-typescript'; | ||
import { Umzug, SequelizeStorage } from 'umzug'; | ||
import * as models from './models'; | ||
|
||
// this is to make explicit column names required, instead of | ||
// allowing Sequelize to auto-generate them | ||
export type ColumnOptions<T> = Record<keyof T, ModelAttributeColumnOptions & { field: string}>; | ||
|
||
const DB_HOST = 'localhost'; | ||
const DB_USER = 'postgres'; | ||
const DB_PASS = 'postgres'; | ||
const DB_PORT = 5432; | ||
const DB_NAME = 'sandbox-db'; | ||
|
||
const dbConfig: SequelizeOptions = { | ||
dialect: 'postgres', | ||
host: DB_HOST, | ||
username: DB_USER, | ||
password: DB_PASS, | ||
port: DB_PORT, | ||
database: DB_NAME, | ||
logging: false, | ||
models: Object.values(models), | ||
}; | ||
|
||
const sequelize = new Sequelize(dbConfig); | ||
|
||
async function initialize() { | ||
return sequelize.authenticate(); | ||
}; | ||
|
||
const MIGRATION_GLOB = process.env['NODE_ENV'] == 'production' ? `./dist/migrations/*.js` : `./src/migrations/*.ts`; | ||
|
||
const umzug = new Umzug({ | ||
migrations: { glob: MIGRATION_GLOB }, | ||
context: sequelize.getQueryInterface(), | ||
storage: new SequelizeStorage({ sequelize }), | ||
logger: console, // log generated queries to console | ||
}); | ||
|
||
async function runMigrations() { | ||
return umzug.up(); | ||
} | ||
|
||
export { initialize, runMigrations, sequelize as client }; |
13 changes: 13 additions & 0 deletions
13
src/migrations/2024.10.26T20.56.44.create-character-table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { QueryInterface } from 'sequelize'; | ||
import { MigrationParams } from 'umzug'; | ||
import { DataType } from 'sequelize-typescript'; | ||
|
||
type Migration = (params: MigrationParams<QueryInterface>) => Promise<unknown>; | ||
|
||
export const up: Migration = async ({ context: queryInterface }) => { | ||
await queryInterface.createTable('character', { | ||
id: { field: 'id', type: DataType.INTEGER, autoIncrementIdentity: true, primaryKey: true }, | ||
name: { field: 'name', type: DataType.STRING }, | ||
age: { field: 'age', type: DataType.INTEGER }, | ||
}); | ||
}; |
16 changes: 16 additions & 0 deletions
16
src/migrations/2024.10.26T21.11.33.create-inventory-table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { QueryInterface } from 'sequelize'; | ||
import { MigrationParams } from 'umzug' | ||
import { DataType } from 'sequelize-typescript'; | ||
|
||
type Migration = (params: MigrationParams<QueryInterface>) => Promise<unknown>; | ||
|
||
export const up: Migration = async ({ context: queryInterface }) => { | ||
await queryInterface.createTable('inventory', { | ||
id: { field: 'id', type: DataType.INTEGER, autoIncrementIdentity: true, primaryKey: true }, | ||
characterId: { | ||
field: 'character_id', | ||
type: DataType.INTEGER, | ||
references: { model: 'character', key: 'id' }, | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { QueryInterface } from 'sequelize'; | ||
import { MigrationParams } from 'umzug' | ||
import { DataType } from 'sequelize-typescript'; | ||
|
||
type Migration = (params: MigrationParams<QueryInterface>) => Promise<unknown>; | ||
|
||
export const up: Migration = async ({ context: queryInterface }) => { | ||
await queryInterface.createTable('weapon', { | ||
id: { field: 'id', type: DataType.INTEGER, autoIncrementIdentity: true, primaryKey: true }, | ||
inventoryId: { field: 'inventory_id', type: DataType.INTEGER, references: { model: 'inventory', key: 'id' }}, | ||
name: { field: 'name', type: DataType.STRING }, | ||
damage: { field: 'damage', type: DataType.FLOAT }, | ||
type: { field: 'type', type: DataType.STRING }, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// use this file to "register" models, and to avoid having a huge stack of imports in db.ts | ||
export { Weapon } from './models/weapon.model'; | ||
export { Character } from './models/character.model'; | ||
export { Inventory } from './models/inventory.model'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,43 @@ | ||
import { Table, Column, Model, PrimaryKey, AutoIncrement, ForeignKey, BelongsTo } from 'sequelize-typescript'; | ||
import { WeaponAttributes, WeaponCreationAttributes, WeaponType } from '../@types/weapon.types'; | ||
import { Table, Column, Model, ForeignKey, DataType } from 'sequelize-typescript'; | ||
import { ColumnOptions } from '../db'; | ||
import { Inventory } from './inventory.model'; | ||
|
||
@Table({ tableName: 'weapons' }) | ||
class Weapon extends Model<WeaponAttributes, WeaponCreationAttributes> implements WeaponAttributes { | ||
@PrimaryKey | ||
@AutoIncrement | ||
@Column | ||
interface CreationAttributes { | ||
inventoryId: number; | ||
name: string; | ||
damage: number; | ||
type: string; | ||
} | ||
|
||
interface Attributes extends CreationAttributes { | ||
id: number; | ||
} | ||
|
||
const columns: ColumnOptions<Attributes> = { | ||
id: { field: 'id', type: DataType.INTEGER, autoIncrementIdentity: true, primaryKey: true }, | ||
inventoryId: { field: 'inventory_id', type: DataType.INTEGER, references: { model: 'inventory', key: 'id' }}, | ||
name: { field: 'name', type: DataType.STRING }, | ||
damage: { field: 'damage', type: DataType.FLOAT }, | ||
type: { field: 'type', type: DataType.STRING }, | ||
} | ||
|
||
@Table({ tableName: 'weapon' }) | ||
class Weapon extends Model<Attributes, CreationAttributes> implements Attributes { | ||
@Column(columns.id) | ||
override id!: number; | ||
|
||
@ForeignKey(() => Inventory) | ||
@Column | ||
@Column(columns.inventoryId) | ||
inventoryId!: number; | ||
|
||
@BelongsTo(() => Inventory) | ||
inventory!: Inventory; | ||
|
||
@Column | ||
@Column(columns.name) | ||
name!: string; | ||
|
||
@Column | ||
@Column(columns.damage) | ||
damage!: number; | ||
|
||
@Column | ||
type!: WeaponType; | ||
@Column(columns.type) | ||
type!: string; | ||
} | ||
|
||
export { Weapon }; |
This file was deleted.
Oops, something went wrong.