diff --git a/compose.yaml b/compose.yaml index dc016bd..ac0a51c 100644 --- a/compose.yaml +++ b/compose.yaml @@ -5,6 +5,6 @@ services: - 5432:5432 environment: - DEBUG=true - - POSTGRES_USER=admin - - POSTGRES_PASSWORD=password - - POSTGRES_DB=sequelize-sandbox + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + - POSTGRES_DB=sandbox-db diff --git a/example-migration.ts b/example-migration.ts new file mode 100644 index 0000000..77e1a23 --- /dev/null +++ b/example-migration.ts @@ -0,0 +1,14 @@ +import { QueryInterface } from 'sequelize'; +import { MigrationParams } from 'umzug' +import { DataType } from 'sequelize-typescript'; + +type Migration = (params: MigrationParams) => Promise; + +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(); +}; diff --git a/migration.ts b/migration.ts new file mode 100644 index 0000000..d42c8f0 --- /dev/null +++ b/migration.ts @@ -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()); +} diff --git a/package-lock.json b/package-lock.json index 5449bac..cd2d627 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,8 @@ "reflect-metadata": "^0.2.2", "sequelize": "^6.37.5", "sequelize-typescript": "^2.1.6", - "typescript": "^5.6.3" + "typescript": "^5.6.3", + "umzug": "^3.8.2" }, "devDependencies": { "@faker-js/faker": "^9.1.0", @@ -1123,6 +1124,188 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@nodelib/fs.scandir": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", + "integrity": "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==", + "license": "MIT", + "dependencies": { + "@nodelib/fs.stat": "2.0.5", + "run-parallel": "^1.1.9" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/@nodelib/fs.stat": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.5.tgz", + "integrity": "sha512-RkhPPp2zrqDAQA/2jNhnztcPAlv64XdhIp7a7454A5ovI7Bukxgt7MX7udwAu3zg1DcpPU0rz3VV1SeaqvY4+A==", + "license": "MIT", + "engines": { + "node": ">= 8" + } + }, + "node_modules/@nodelib/fs.walk": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@nodelib/fs.walk/-/fs.walk-1.2.8.tgz", + "integrity": "sha512-oGB+UxlgWcgQkgwo8GcEGwemoTFt3FIO9ababBmaGwXIoBKZ+GTy0pP185beGg7Llih/NSHSV2XAs1lnznocSg==", + "license": "MIT", + "dependencies": { + "@nodelib/fs.scandir": "2.1.5", + "fastq": "^1.6.0" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/@rushstack/node-core-library": { + "version": "5.9.0", + "resolved": "https://registry.npmjs.org/@rushstack/node-core-library/-/node-core-library-5.9.0.tgz", + "integrity": "sha512-MMsshEWkTbXqxqFxD4gcIUWQOCeBChlGczdZbHfqmNZQFLHB3yWxDFSMHFUdu2/OB9NUk7Awn5qRL+rws4HQNg==", + "license": "MIT", + "dependencies": { + "ajv": "~8.13.0", + "ajv-draft-04": "~1.0.0", + "ajv-formats": "~3.0.1", + "fs-extra": "~7.0.1", + "import-lazy": "~4.0.0", + "jju": "~1.4.0", + "resolve": "~1.22.1", + "semver": "~7.5.4" + }, + "peerDependencies": { + "@types/node": "*" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@rushstack/node-core-library/node_modules/ajv": { + "version": "8.13.0", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.13.0.tgz", + "integrity": "sha512-PRA911Blj99jR5RMeTunVbNXMF6Lp4vZXnk5GQjcnUWUTsrXtekg/pnmFFI2u/I36Y/2bITGS30GZCXei6uNkA==", + "license": "MIT", + "dependencies": { + "fast-deep-equal": "^3.1.3", + "json-schema-traverse": "^1.0.0", + "require-from-string": "^2.0.2", + "uri-js": "^4.4.1" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/epoberezkin" + } + }, + "node_modules/@rushstack/node-core-library/node_modules/ajv-draft-04": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/ajv-draft-04/-/ajv-draft-04-1.0.0.tgz", + "integrity": "sha512-mv00Te6nmYbRp5DCwclxtt7yV/joXJPGS7nM+97GdxvuttCOfgI3K4U25zboyeX0O+myI8ERluxQe5wljMmVIw==", + "license": "MIT", + "peerDependencies": { + "ajv": "^8.5.0" + }, + "peerDependenciesMeta": { + "ajv": { + "optional": true + } + } + }, + "node_modules/@rushstack/node-core-library/node_modules/json-schema-traverse": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", + "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==", + "license": "MIT" + }, + "node_modules/@rushstack/node-core-library/node_modules/lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "license": "ISC", + "dependencies": { + "yallist": "^4.0.0" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@rushstack/node-core-library/node_modules/semver": { + "version": "7.5.4", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", + "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", + "license": "ISC", + "dependencies": { + "lru-cache": "^6.0.0" + }, + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@rushstack/node-core-library/node_modules/yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", + "license": "ISC" + }, + "node_modules/@rushstack/terminal": { + "version": "0.14.2", + "resolved": "https://registry.npmjs.org/@rushstack/terminal/-/terminal-0.14.2.tgz", + "integrity": "sha512-2fC1wqu1VCExKC0/L+0noVcFQEXEnoBOtCIex1TOjBzEDWcw8KzJjjj7aTP6mLxepG0XIyn9OufeFb6SFsa+sg==", + "license": "MIT", + "dependencies": { + "@rushstack/node-core-library": "5.9.0", + "supports-color": "~8.1.1" + }, + "peerDependencies": { + "@types/node": "*" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@rushstack/terminal/node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/@rushstack/terminal/node_modules/supports-color": { + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-8.1.1.tgz", + "integrity": "sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==", + "license": "MIT", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/supports-color?sponsor=1" + } + }, + "node_modules/@rushstack/ts-command-line": { + "version": "4.23.0", + "resolved": "https://registry.npmjs.org/@rushstack/ts-command-line/-/ts-command-line-4.23.0.tgz", + "integrity": "sha512-jYREBtsxduPV6ptNq8jOKp9+yx0ld1Tb/Tkdnlj8gTjazl1sF3DwX2VbluyYrNd0meWIL0bNeer7WDf5tKFjaQ==", + "license": "MIT", + "dependencies": { + "@rushstack/terminal": "0.14.2", + "@types/argparse": "1.0.38", + "argparse": "~1.0.9", + "string-argv": "~0.3.1" + } + }, "node_modules/@sinclair/typebox": { "version": "0.27.8", "resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.27.8.tgz", @@ -1178,6 +1361,12 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/argparse": { + "version": "1.0.38", + "resolved": "https://registry.npmjs.org/@types/argparse/-/argparse-1.0.38.tgz", + "integrity": "sha512-ebDJ9b0e702Yr7pWgB0jzm+CX4Srzz8RcXtLJDJB+BSccqMa36uyH/zUsSYao5+BD1ytv3k3rPYCq4mAE1hsXA==", + "license": "MIT" + }, "node_modules/@types/babel__core": { "version": "7.20.5", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.5.tgz", @@ -1530,6 +1719,45 @@ "url": "https://github.com/sponsors/epoberezkin" } }, + "node_modules/ajv-formats": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-3.0.1.tgz", + "integrity": "sha512-8iUql50EUR+uUcdRQ3HDqa6EVyo3docL8g5WJ3FNcWmu62IbkGUue/pEyLBW8VGKKucTPgqeks4fIU1DA4yowQ==", + "license": "MIT", + "dependencies": { + "ajv": "^8.0.0" + }, + "peerDependencies": { + "ajv": "^8.0.0" + }, + "peerDependenciesMeta": { + "ajv": { + "optional": true + } + } + }, + "node_modules/ajv-formats/node_modules/ajv": { + "version": "8.17.1", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.17.1.tgz", + "integrity": "sha512-B/gBuNg5SiMTrPkC+A2+cW0RszwxYmn6VYxB/inlBStS5nx6xHIt/ehKRhIMhqusl7a8LjQoZnjCs5vhwxOQ1g==", + "license": "MIT", + "dependencies": { + "fast-deep-equal": "^3.1.3", + "fast-uri": "^3.0.1", + "json-schema-traverse": "^1.0.0", + "require-from-string": "^2.0.2" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/epoberezkin" + } + }, + "node_modules/ajv-formats/node_modules/json-schema-traverse": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", + "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==", + "license": "MIT" + }, "node_modules/ansi-escapes": { "version": "4.3.2", "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-4.3.2.tgz", @@ -1595,7 +1823,6 @@ "version": "1.0.10", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", - "dev": true, "dependencies": { "sprintf-js": "~1.0.2" } @@ -1784,7 +2011,6 @@ "version": "3.0.3", "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", - "dev": true, "license": "MIT", "dependencies": { "fill-range": "^7.1.1" @@ -2357,7 +2583,6 @@ "version": "0.13.1", "resolved": "https://registry.npmjs.org/emittery/-/emittery-0.13.1.tgz", "integrity": "sha512-DeWwawk6r5yR9jFgnDKYt4sLS0LmHJJi3ZOnb5/JdbYwj3nW+FxQnHIjhBKz8YLC7oRNPVM9NQ47I3CVx34eqQ==", - "dev": true, "license": "MIT", "engines": { "node": ">=12" @@ -2798,8 +3023,23 @@ "node_modules/fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", - "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", - "dev": true + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" + }, + "node_modules/fast-glob": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.3.2.tgz", + "integrity": "sha512-oX2ruAFQwf/Orj8m737Y5adxDQO0LAB7/S5MnxCdTNDd4p6BsyIVsv9JQsATbTSq8KHRpLwIHbVlUNatxd+1Ow==", + "license": "MIT", + "dependencies": { + "@nodelib/fs.stat": "^2.0.2", + "@nodelib/fs.walk": "^1.2.3", + "glob-parent": "^5.1.2", + "merge2": "^1.3.0", + "micromatch": "^4.0.4" + }, + "engines": { + "node": ">=8.6.0" + } }, "node_modules/fast-json-stable-stringify": { "version": "2.1.0", @@ -2820,6 +3060,21 @@ "dev": true, "license": "MIT" }, + "node_modules/fast-uri": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.0.3.tgz", + "integrity": "sha512-aLrHthzCjH5He4Z2H9YZ+v6Ujb9ocRuW6ZzkJQOrTxleEijANq4v1TsaPaVG1PZcuurEzrLcWRyYBYXD5cEiaw==", + "license": "BSD-3-Clause" + }, + "node_modules/fastq": { + "version": "1.17.1", + "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.17.1.tgz", + "integrity": "sha512-sRVD3lWVIXWg6By68ZN7vho9a1pQcN/WBFaAAsDDFzlJjvoGx0P8z7V1t72grFJfJhu3YPZBuu25f7Kaw2jN1w==", + "license": "ISC", + "dependencies": { + "reusify": "^1.0.4" + } + }, "node_modules/fb-watchman": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/fb-watchman/-/fb-watchman-2.0.2.tgz", @@ -2879,7 +3134,6 @@ "version": "7.1.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", - "dev": true, "license": "MIT", "dependencies": { "to-regex-range": "^5.0.1" @@ -3000,6 +3254,20 @@ "node": ">= 0.6" } }, + "node_modules/fs-extra": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-7.0.1.tgz", + "integrity": "sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw==", + "license": "MIT", + "dependencies": { + "graceful-fs": "^4.1.2", + "jsonfile": "^4.0.0", + "universalify": "^0.1.0" + }, + "engines": { + "node": ">=6 <7 || >=8" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -3111,7 +3379,6 @@ "version": "5.1.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", - "dev": true, "dependencies": { "is-glob": "^4.0.1" }, @@ -3144,8 +3411,7 @@ "node_modules/graceful-fs": { "version": "4.2.10", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.10.tgz", - "integrity": "sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA==", - "dev": true + "integrity": "sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA==" }, "node_modules/has-flag": { "version": "3.0.0", @@ -3298,6 +3564,15 @@ "node": ">=4" } }, + "node_modules/import-lazy": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/import-lazy/-/import-lazy-4.0.0.tgz", + "integrity": "sha512-rKtvo6a868b5Hu3heneU+L4yEQ4jYKLtjpnPeUdK7h0yzXGmyBTypknlkCvHFBqfX9YlorEiMM6Dnq/5atfHkw==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, "node_modules/import-local": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/import-local/-/import-local-3.2.0.tgz", @@ -3381,7 +3656,6 @@ "version": "2.15.1", "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.15.1.tgz", "integrity": "sha512-z0vtXSwucUJtANQWldhbtbt7BnL0vxiFjIdDLAatwhDYty2bad6s+rijD6Ri4YuYJubLzIJLUidCh09e1djEVQ==", - "dev": true, "license": "MIT", "dependencies": { "hasown": "^2.0.2" @@ -3397,7 +3671,6 @@ "version": "2.1.1", "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", - "dev": true, "engines": { "node": ">=0.10.0" } @@ -3426,7 +3699,6 @@ "version": "4.0.3", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", - "dev": true, "dependencies": { "is-extglob": "^2.1.1" }, @@ -3438,7 +3710,6 @@ "version": "7.0.0", "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", - "dev": true, "license": "MIT", "engines": { "node": ">=0.12.0" @@ -4177,6 +4448,12 @@ "url": "https://github.com/chalk/supports-color?sponsor=1" } }, + "node_modules/jju": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/jju/-/jju-1.4.0.tgz", + "integrity": "sha512-8wb9Yw966OSxApiCt0K3yNJL8pnNeIv+OEq2YMidz4FKP6nonSRoOXc80iXY4JaN2FC11B9qsNmDsm+ZOfMROA==", + "license": "MIT" + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -4247,6 +4524,15 @@ "node": ">=6" } }, + "node_modules/jsonfile": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", + "integrity": "sha512-m6F1R3z8jjlf2imQHS2Qez5sjKWQzbuuhuJ/FKYFRZvPE3PuHcSMVZzfsLhGVOkfd20obL5SWEBew5ShlquNxg==", + "license": "MIT", + "optionalDependencies": { + "graceful-fs": "^4.1.6" + } + }, "node_modules/keyv": { "version": "4.5.4", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", @@ -4392,6 +4678,15 @@ "dev": true, "license": "MIT" }, + "node_modules/merge2": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz", + "integrity": "sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==", + "license": "MIT", + "engines": { + "node": ">= 8" + } + }, "node_modules/methods": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", @@ -4404,7 +4699,6 @@ "version": "4.0.8", "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", - "dev": true, "license": "MIT", "dependencies": { "braces": "^3.0.3", @@ -4771,7 +5065,6 @@ "version": "1.0.7", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", - "dev": true, "license": "MIT" }, "node_modules/path-to-regexp": { @@ -4880,7 +5173,6 @@ "version": "2.3.1", "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", - "dev": true, "engines": { "node": ">=8.6" }, @@ -4910,6 +5202,15 @@ "node": ">=8" } }, + "node_modules/pony-cause": { + "version": "2.1.11", + "resolved": "https://registry.npmjs.org/pony-cause/-/pony-cause-2.1.11.tgz", + "integrity": "sha512-M7LhCsdNbNgiLYiP4WjsfLUuFmCfnjdF6jKe2R9NKl4WFN+HZPGHJZ9lnLP7f9ZnKe3U9nuWD0szirmj+migUg==", + "license": "0BSD", + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/postgres-array": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", @@ -5036,7 +5337,6 @@ "version": "2.3.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", - "dev": true, "engines": { "node": ">=6" } @@ -5073,6 +5373,26 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/queue-microtask": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", + "integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, "node_modules/range-parser": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", @@ -5131,11 +5451,19 @@ "node": ">=0.10.0" } }, + "node_modules/require-from-string": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", + "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/resolve": { "version": "1.22.8", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.8.tgz", "integrity": "sha512-oKWePCxqpd6FlLvGV1VU0x7bkPmmCNolxzjMf4NczoDnQcIWrAF+cPtZn5i6n+RfD2d9i0tzpKnG6Yk168yIyw==", - "dev": true, "license": "MIT", "dependencies": { "is-core-module": "^2.13.0", @@ -5187,6 +5515,39 @@ "integrity": "sha512-XgmCoxKWkDofwH8WddD0w85ZfqYz+ZHlr5yo+3YUCfycWawU56T5ckWXsScsj5B8tqUcIG67DxXByo3VUgiAdA==", "license": "MIT" }, + "node_modules/reusify": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/reusify/-/reusify-1.0.4.tgz", + "integrity": "sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==", + "license": "MIT", + "engines": { + "iojs": ">=1.0.0", + "node": ">=0.10.0" + } + }, + "node_modules/run-parallel": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz", + "integrity": "sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT", + "dependencies": { + "queue-microtask": "^1.2.2" + } + }, "node_modules/safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", @@ -5511,8 +5872,7 @@ "node_modules/sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", - "integrity": "sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==", - "dev": true + "integrity": "sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==" }, "node_modules/stack-utils": { "version": "2.0.6", @@ -5534,6 +5894,15 @@ "node": ">= 0.8" } }, + "node_modules/string-argv": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/string-argv/-/string-argv-0.3.2.tgz", + "integrity": "sha512-aqD2Q0144Z+/RqG52NeHEkZauTAUWJO8c6yTftGJKO3Tja5tUgIfmIl6kExvhtxSDP7fXB6DvzkfMpCd/F3G+Q==", + "license": "MIT", + "engines": { + "node": ">=0.6.19" + } + }, "node_modules/string-length": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/string-length/-/string-length-4.0.2.tgz", @@ -5672,7 +6041,6 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz", "integrity": "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==", - "dev": true, "license": "MIT", "engines": { "node": ">= 0.4" @@ -5712,7 +6080,6 @@ "version": "5.0.1", "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", - "dev": true, "license": "MIT", "dependencies": { "is-number": "^7.0.0" @@ -5900,6 +6267,34 @@ "node": ">=14.17" } }, + "node_modules/umzug": { + "version": "3.8.2", + "resolved": "https://registry.npmjs.org/umzug/-/umzug-3.8.2.tgz", + "integrity": "sha512-BEWEF8OJjTYVC56GjELeHl/1XjFejrD7aHzn+HldRJTx+pL1siBrKHZC8n4K/xL3bEzVA9o++qD1tK2CpZu4KA==", + "license": "MIT", + "dependencies": { + "@rushstack/ts-command-line": "^4.12.2", + "emittery": "^0.13.0", + "fast-glob": "^3.3.2", + "pony-cause": "^2.1.4", + "type-fest": "^4.0.0" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/umzug/node_modules/type-fest": { + "version": "4.26.1", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-4.26.1.tgz", + "integrity": "sha512-yOGpmOAL7CkKe/91I5O3gPICmJNLJ1G4zFYVAsRHg7M64biSnPtRj0WNQt++bRkjYOqjWXrhnUw1utzmVErAdg==", + "license": "(MIT OR CC0-1.0)", + "engines": { + "node": ">=16" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/undefsafe": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.5.tgz", @@ -5912,6 +6307,15 @@ "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==", "license": "MIT" }, + "node_modules/universalify": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", + "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==", + "license": "MIT", + "engines": { + "node": ">= 4.0.0" + } + }, "node_modules/unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", @@ -5955,7 +6359,6 @@ "version": "4.4.1", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==", - "dev": true, "dependencies": { "punycode": "^2.1.0" } diff --git a/package.json b/package.json index 63a13ae..cf303f0 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,10 @@ "check": "tsc --noEmit", "lint": "npx prettier --check .", "format": "npx prettier --write .", - "db:start": "docker compose up -d", - "db:stop": "docker compose down" + "db": "docker compose up -d && npm run db:up", + "db:up": "ts-node migration up", + "db:stop": "docker compose down", + "migrate": "ts-node migration create --name " }, "author": "nealwp", "license": "ISC", @@ -36,6 +38,7 @@ "reflect-metadata": "^0.2.2", "sequelize": "^6.37.5", "sequelize-typescript": "^2.1.6", - "typescript": "^5.6.3" + "typescript": "^5.6.3", + "umzug": "^3.8.2" } } diff --git a/src/app.ts b/src/app.ts index 484270d..5fbc958 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,11 +1,17 @@ import * as db from './db'; import router from './routes'; -import { createServer } from './server'; +import express from 'express'; const PORT = 3000; -const server = createServer(router); + +const server = express(); + +server.use(express.json()); +server.use(express.urlencoded({ extended: true })); +server.use(router); db.initialize().then(async () => { + await db.runMigrations(); server.listen(PORT, () => { console.log(`server listening on port ${PORT}`); }); diff --git a/src/db.ts b/src/db.ts index b6cdf18..6a6dbef 100644 --- a/src/db.ts +++ b/src/db.ts @@ -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 = Record; + +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 }; diff --git a/src/migrations/2024.10.26T20.56.44.create-character-table.ts b/src/migrations/2024.10.26T20.56.44.create-character-table.ts new file mode 100644 index 0000000..2f79e2f --- /dev/null +++ b/src/migrations/2024.10.26T20.56.44.create-character-table.ts @@ -0,0 +1,13 @@ +import { QueryInterface } from 'sequelize'; +import { MigrationParams } from 'umzug'; +import { DataType } from 'sequelize-typescript'; + +type Migration = (params: MigrationParams) => Promise; + +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 }, + }); +}; diff --git a/src/migrations/2024.10.26T21.11.33.create-inventory-table.ts b/src/migrations/2024.10.26T21.11.33.create-inventory-table.ts new file mode 100644 index 0000000..43f251e --- /dev/null +++ b/src/migrations/2024.10.26T21.11.33.create-inventory-table.ts @@ -0,0 +1,16 @@ +import { QueryInterface } from 'sequelize'; +import { MigrationParams } from 'umzug' +import { DataType } from 'sequelize-typescript'; + +type Migration = (params: MigrationParams) => Promise; + +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' }, + }, + }); +}; diff --git a/src/migrations/2024.10.26T21.52.09.create-weapon-table.ts b/src/migrations/2024.10.26T21.52.09.create-weapon-table.ts new file mode 100644 index 0000000..a9dd103 --- /dev/null +++ b/src/migrations/2024.10.26T21.52.09.create-weapon-table.ts @@ -0,0 +1,15 @@ +import { QueryInterface } from 'sequelize'; +import { MigrationParams } from 'umzug' +import { DataType } from 'sequelize-typescript'; + +type Migration = (params: MigrationParams) => Promise; + +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 }, + }); +}; diff --git a/src/models.ts b/src/models.ts new file mode 100644 index 0000000..9eecdbe --- /dev/null +++ b/src/models.ts @@ -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'; diff --git a/src/models/character.model.ts b/src/models/character.model.ts index 626fe39..14283f0 100644 --- a/src/models/character.model.ts +++ b/src/models/character.model.ts @@ -1,18 +1,31 @@ -import { Table, Column, Model, PrimaryKey, AutoIncrement, HasMany } from 'sequelize-typescript'; -import { CharacterAttributes, CharacterCreationAttributes } from '../@types/character.types'; +import { ColumnOptions } from '../db'; +import { Table, Column, Model, HasMany, DataType } from 'sequelize-typescript'; import { Inventory } from './inventory.model'; -@Table({ tableName: 'characters' }) -class Character extends Model implements CharacterAttributes { - @PrimaryKey - @AutoIncrement - @Column +interface CreationAttributes { + name: string; + age: number; +} + +interface Attributes extends CreationAttributes { + id: number; +} + +const columns: ColumnOptions = { + id: { field: 'id', type: DataType.INTEGER, autoIncrementIdentity: true, primaryKey: true }, + name: { field: 'name', type: DataType.STRING }, + age: { field: 'age', type: DataType.INTEGER }, +} + +@Table({ tableName: 'character' }) +class Character extends Model implements Attributes { + @Column(columns.id) override id!: number; - @Column + @Column(columns.name) name!: string; - @Column + @Column(columns.age) age!: number; @HasMany(() => Inventory) diff --git a/src/models/index.ts b/src/models/index.ts deleted file mode 100644 index c0d504a..0000000 --- a/src/models/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './character.model'; -export * from './weapon.model'; -export * from './inventory.model'; diff --git a/src/models/inventory.model.ts b/src/models/inventory.model.ts index 4cfa274..98d9c9a 100644 --- a/src/models/inventory.model.ts +++ b/src/models/inventory.model.ts @@ -1,22 +1,34 @@ -import { Table, Column, Model, PrimaryKey, AutoIncrement, ForeignKey, BelongsTo, HasMany } from 'sequelize-typescript'; -import { InventoryAttributes, InventoryCreationAttributes } from '../@types/inventory.types'; +import { Table, Column, Model, ForeignKey, HasMany, DataType } from 'sequelize-typescript'; +import { ColumnOptions } from '../db'; import { Character } from './character.model'; import { Weapon } from './weapon.model'; +interface CreationAttributes { + characterId: number; +} + +interface Attributes extends CreationAttributes { + id: number; +} + +const columns: ColumnOptions = { + id: { field: 'id', type: DataType.INTEGER, autoIncrementIdentity: true, primaryKey: true }, + characterId: { + field: 'character_id', + type: DataType.INTEGER, + references: { model: 'character', key: 'id' }, + }, +} + @Table({ tableName: 'inventory' }) -class Inventory extends Model implements InventoryAttributes { - @PrimaryKey - @AutoIncrement - @Column +class Inventory extends Model implements Attributes { + @Column(columns.id) override id!: number; @ForeignKey(() => Character) - @Column + @Column(columns.characterId) characterId!: number; - @BelongsTo(() => Character) - character!: Character; - @HasMany(() => Weapon) weapons!: Weapon[]; } diff --git a/src/models/weapon.model.ts b/src/models/weapon.model.ts index b70231b..a72b1dd 100644 --- a/src/models/weapon.model.ts +++ b/src/models/weapon.model.ts @@ -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 implements WeaponAttributes { - @PrimaryKey - @AutoIncrement - @Column +interface CreationAttributes { + inventoryId: number; + name: string; + damage: number; + type: string; +} + +interface Attributes extends CreationAttributes { + id: number; +} + +const columns: ColumnOptions = { + 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 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 }; diff --git a/src/server.ts b/src/server.ts deleted file mode 100644 index 24c69dd..0000000 --- a/src/server.ts +++ /dev/null @@ -1,14 +0,0 @@ -import express, { Router } from 'express'; -import bodyParser from 'body-parser'; - -const createServer = (router: Router) => { - const server = express(); - - server.use(bodyParser.json()); - server.use(bodyParser.urlencoded({ extended: true })); - server.use(router); - - return server; -}; - -export { createServer };