diff --git a/common/config/rush/browser-approved-packages.json b/common/config/rush/browser-approved-packages.json index f85863aa..c1c28e61 100644 --- a/common/config/rush/browser-approved-packages.json +++ b/common/config/rush/browser-approved-packages.json @@ -554,6 +554,10 @@ "name": "migrate", "allowedCategories": [ "apis", "libraries" ] }, + { + "name": "mongo-migrate-ts", + "allowedCategories": [ "apis" ] + }, { "name": "mongodb", "allowedCategories": [ "apis", "libraries" ] diff --git a/common/config/rush/pnpm-lock.yaml b/common/config/rush/pnpm-lock.yaml index 8730899d..930bb127 100644 --- a/common/config/rush/pnpm-lock.yaml +++ b/common/config/rush/pnpm-lock.yaml @@ -2791,9 +2791,9 @@ importers: express: specifier: 4.18.2 version: 4.18.2 - migrate: - specifier: ~2.0.1 - version: 2.0.1 + mongo-migrate-ts: + specifier: ~1.3.1 + version: 1.3.1(mongodb@6.0.0) mongodb: specifier: 6.0.0 version: 6.0.0 @@ -2812,9 +2812,6 @@ importers: source-map-support: specifier: ~0.5.21 version: 0.5.21 - tsconfig-paths: - specifier: ~4.2.0 - version: 4.2.0 uuid: specifier: ~9.0.1 version: 9.0.1 @@ -17388,14 +17385,9 @@ packages: engines: {node: '>=14.20.1'} dev: true - /bson@6.1.0: - resolution: {integrity: sha512-yiQ3KxvpVoRpx1oD1uPz4Jit9tAVTJgjdmjDKtUErkOoL9VNoF8Dd58qtAOL5E40exx2jvAT9sqdRSK/r+SHlA==} - engines: {node: '>=16.20.1'} - /bson@6.2.0: resolution: {integrity: sha512-ID1cI+7bazPDyL9wYy9GaQ8gEEohWvcUl/Yf0dIdutJxnmInEEyCsb4awy/OiBfall7zBA179Pahi3vCdFze3Q==} engines: {node: '>=16.20.1'} - dev: false /buffer-crc32@0.2.13: resolution: {integrity: sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==} @@ -18604,10 +18596,6 @@ packages: whatwg-url: 11.0.0 dev: true - /dateformat@4.6.3: - resolution: {integrity: sha512-2P0p0pFGzHS5EMnhdxQi7aJN+iMheud0UhG4dlE1DLAlvL8JHjJJTX/CSm4JXwV0Ka5nGk3zC5mcb5bUQUxxMA==} - dev: false - /debug@2.6.9: resolution: {integrity: sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==} dependencies: @@ -24334,21 +24322,6 @@ packages: braces: 3.0.2 picomatch: 2.3.1 - /migrate@2.0.1: - resolution: {integrity: sha512-t9yzYd4kPdx+soNL/Cc4YQoVYOgPK28LNEmkW7kn0x9duVBNsV+cDaETby8nZ9Dm+LsQTB4SPJ586i8WQgjM5g==} - engines: {node: '>= 14.0.0'} - hasBin: true - dependencies: - chalk: 4.1.2 - commander: 2.20.3 - dateformat: 4.6.3 - dotenv: 16.0.3 - inherits: 2.0.4 - minimatch: 9.0.3 - mkdirp: 3.0.1 - slug: 8.2.3 - dev: false - /mime-db@1.52.0: resolution: {integrity: sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==} engines: {node: '>= 0.6'} @@ -24543,10 +24516,17 @@ packages: engines: {node: '>=10'} hasBin: true - /mkdirp@3.0.1: - resolution: {integrity: sha512-+NsyUUAZDmo6YVHzL/stxSu3t9YS1iljliy3BSDrXJ/dkn1KYdmtZODGGjLcc9XLgVVpH4KshHB8XmZgMhaBXg==} - engines: {node: '>=10'} + /mongo-migrate-ts@1.3.1(mongodb@6.0.0): + resolution: {integrity: sha512-5dvOTG/UA0IW7WE98kSh+n3usUhvSrqbfZX15kWrrKLjwqi7IRANhRtlQ8o4by6UZoYCKVCfveuOeyKtilLyBQ==} hasBin: true + peerDependencies: + mongodb: 4.x.x || 5.x.x + dependencies: + cli-table: 0.3.11 + commander: 9.5.0 + connection-string: 4.3.6 + mongodb: 6.0.0 + ora: 5.4.1 dev: false /mongodb-connection-string-url@2.6.0: @@ -24707,7 +24687,7 @@ packages: optional: true dependencies: '@mongodb-js/saslprep': 1.1.0 - bson: 6.1.0 + bson: 6.2.0 mongodb-connection-string-url: 2.6.0 /mongoose@6.9.1: @@ -28303,11 +28283,6 @@ packages: resolution: {integrity: sha512-NwrtjCg+lZoqhFU8fOwl4ay2ei8PaqCBOUV3/ektPY9trO1yQ1oXEfmHAhKArUVUr/hOHvy5f6AdP17dCM0zMw==} dev: true - /slug@8.2.3: - resolution: {integrity: sha512-fXjhAZszNecz855GUNIwW0+sFPi9WV4bMiEKDOCA4wcq1ts1UnUVNy/F78B0Aat7/W3rA+se//33ILKNMrbeYQ==} - hasBin: true - dev: false - /smart-buffer@4.2.0: resolution: {integrity: sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==} engines: {node: '>= 6.0.0', npm: '>= 3.0.0'} @@ -29575,15 +29550,6 @@ packages: minimist: 1.2.8 strip-bom: 3.0.0 - /tsconfig-paths@4.2.0: - resolution: {integrity: sha512-NoZ4roiN7LnbKn9QqE1amc9DJfzvZXxF4xDavcOWt1BPkdx+m+0gJuPM+S0vCe7zTJMYUP0R8pO2XMr+Y8oLIg==} - engines: {node: '>=6'} - dependencies: - json5: 2.2.3 - minimist: 1.2.8 - strip-bom: 3.0.0 - dev: false - /tsdx@0.14.1(@types/node@20.9.0): resolution: {integrity: sha512-keHmFdCL2kx5nYFlBdbE3639HQ2v9iGedAFAajobrUTH2wfX0nLPdDhbHv+GHLQZqf0c5ur1XteE8ek/+Eyj5w==} engines: {node: '>=10'} diff --git a/common/config/rush/repo-state.json b/common/config/rush/repo-state.json index 0ef53f21..5544b8bc 100644 --- a/common/config/rush/repo-state.json +++ b/common/config/rush/repo-state.json @@ -1,5 +1,5 @@ // DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush. { - "pnpmShrinkwrapHash": "226112290342a77c7f773aeecbc45e260e27e9bc", + "pnpmShrinkwrapHash": "789c7de00d79d30378a52f3bcfad002fc81af8d2", "preferredVersionsHash": "7c6836c4ff2ee31a263e87ea93a487fc752ca3f6" } diff --git a/platform/rigs/base-nodejs/profiles/nestjs-app/tsconfig-base.json b/platform/rigs/base-nodejs/profiles/nestjs-app/tsconfig-base.json index 3788ae7f..2b0fe61b 100644 --- a/platform/rigs/base-nodejs/profiles/nestjs-app/tsconfig-base.json +++ b/platform/rigs/base-nodejs/profiles/nestjs-app/tsconfig-base.json @@ -35,6 +35,7 @@ }, "sourceMap": true, "include": [ + "../../../../../migrations/*.ts", "../../../../../package.json", "../../../../../src/*.{ts|tsx}", "../../../../../src/**/*.{ts|tsx}", diff --git a/services/player-achievements/.migrate b/services/player-achievements/.migrate deleted file mode 100644 index bae11660..00000000 --- a/services/player-achievements/.migrate +++ /dev/null @@ -1,9 +0,0 @@ -{ - "lastRun": "1701140634237-trophy.ts", - "migrations": [ - { - "title": "1701140634237-trophy.ts", - "timestamp": 1701151686419 - } - ] -} \ No newline at end of file diff --git a/services/player-achievements/migrations/1701265134891_Migration.ts b/services/player-achievements/migrations/1701265134891_Migration.ts new file mode 100644 index 00000000..a9f34ad5 --- /dev/null +++ b/services/player-achievements/migrations/1701265134891_Migration.ts @@ -0,0 +1,22 @@ +import { Db } from 'mongodb'; +import { MigrationInterface } from 'mongo-migrate-ts'; +import { asyncForEach } from '@cats-cradle/nestjs-modules'; +import { ObjectId, UUID } from 'bson'; +import { v4 } from 'uuid'; +import { Achievements } from '../src/models/achievements'; + +export class Migration1701265134891 implements MigrationInterface { + public async up(db: Db): Promise { + await asyncForEach(Achievements, async (trophy: object) => { + const record = { _id: new UUID(v4()) as any as ObjectId }; + + await db.collection('trophies').insertOne({ ...record, ...trophy }); + }); + } + + public async down(db: Db): Promise { + await asyncForEach(Achievements, async (trophy) => { + await db.collection('trophies').deleteOne(trophy); + }); + } +} diff --git a/services/player-achievements/migrations/index.ts b/services/player-achievements/migrations/index.ts new file mode 100644 index 00000000..08b92487 --- /dev/null +++ b/services/player-achievements/migrations/index.ts @@ -0,0 +1,18 @@ +import { mongoMigrateCli } from 'mongo-migrate-ts'; +import dotenv from 'dotenv'; + +dotenv.config(); + +const username = process.env.MONGO_DATABASE_USER; +const password = process.env.MONGO_DATABASE_PASSWORD; +const MONGO_URI = `${process.env.MONGO_DATABASE_URI}`; + +const db = `${process.env.STAGE}-${process.env.APP_NAME}`; +const uri = MONGO_URI.replace('//', `//${username}:${password}@`); + +mongoMigrateCli({ + uri, + database: db, + migrationsDir: __dirname, + migrationsCollection: 'migration_versions', +}); diff --git a/services/player-achievements/package.json b/services/player-achievements/package.json index b2e8c7ce..f76b3052 100644 --- a/services/player-achievements/package.json +++ b/services/player-achievements/package.json @@ -12,9 +12,10 @@ "test:cov": "jest --coverage", "lint": "eslint --format visualstudio \"./{stacks,src}/**/*.ts\" --fix", "lint:ci": "eslint --format visualstudio \"./{stacks,src}/*.ts\" --fix-dry-run", - "migrate:create": "migrate create --template-file ./src/migrations-utils/template.ts --migrations-dir=\"./src/migrations\" --compiler=\"ts:./src/migrations-utils/ts-compiler.js\"", - "migrate:up": "migrate --migrations-dir=\"./src/migrations\" --compiler=\"ts:./src/migrations-utils/ts-compiler.js\" up", - "migrate:down": "migrate --migrations-dir=\"./src/migrations\" --compiler=\"ts:./src/migrations-utils/ts-compiler.js\" down", + "migrate:status": "ts-node migrations/index.ts status", + "migrate:new": "ts-node migrations/index.ts new", + "migrate:up": "ts-node migrations/index.ts up", + "migrate:down": "ts-node migrations/index.ts down --all", "docs": "typedoc --entryPointStrategy expand ./src", "cdk": "cdk", "cdk:bootstrap": "cdk bootstrap", @@ -51,11 +52,10 @@ "@cats-cradle/create-bundle": "1.0.7", "@nestjs/mongoose": "9.2.1", "mongoose": "6.9.1", - "migrate": "~2.0.1", "mongodb": "6.0.0", - "tsconfig-paths": "~4.2.0", "dotenv": "~16.0.3", - "bson": "~6.2.0" + "bson": "~6.2.0", + "mongo-migrate-ts": "~1.3.1" }, "devDependencies": { "@shelf/jest-mongodb": "~4.1.7", diff --git a/services/player-achievements/src/migrations-utils/db.ts b/services/player-achievements/src/migrations-utils/db.ts deleted file mode 100644 index 6b7aeeab..00000000 --- a/services/player-achievements/src/migrations-utils/db.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { MongoClient, MongoClientOptions, Db } from 'mongodb'; -import dotenv from 'dotenv'; - -export const getDb = async (): Promise => { - dotenv.config(); - - const options: MongoClientOptions = { - auth: { - username: process.env.MONGO_DATABASE_USER, - password: process.env.MONGO_DATABASE_PASSWORD, - }, - }; - - const MONGO_URL = `${process.env.MONGO_DATABASE_URI}/` - + `${process.env.STAGE}-${process.env.APP_NAME}`; - console.log(MONGO_URL); - - const client: MongoClient = await MongoClient.connect(MONGO_URL, options); - const db = client.db(); - - return db; -}; diff --git a/services/player-achievements/src/migrations-utils/template.ts b/services/player-achievements/src/migrations-utils/template.ts deleted file mode 100644 index b675a01e..00000000 --- a/services/player-achievements/src/migrations-utils/template.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { asyncForEach } from '@cats-cradle/nestjs-modules'; -import { getDb } from './db'; - -const items = [{ a: 'b' }]; - -export const up = async () => { - const db = await getDb(); - - await asyncForEach(items, async (item: any) => { - db.collection('item').insertOne(item); - }); -}; - -export const down = async () => { - const db = await getDb(); - - await asyncForEach(items, async (item: any) => { - db.collection('item').deleteOne(item); - }); -}; diff --git a/services/player-achievements/src/migrations-utils/ts-compiler.js b/services/player-achievements/src/migrations-utils/ts-compiler.js deleted file mode 100644 index 4397ba74..00000000 --- a/services/player-achievements/src/migrations-utils/ts-compiler.js +++ /dev/null @@ -1,7 +0,0 @@ -const tsNode = require('ts-node'); -const tsConfigPaths = require('tsconfig-paths'); - -module.exports = function (opts) { - tsConfigPaths.register(); - return tsNode.register(opts); -}; diff --git a/services/player-achievements/src/migrations/1701140634237-trophy.ts b/services/player-achievements/src/migrations/1701140634237-trophy.ts deleted file mode 100644 index 6d79c4e4..00000000 --- a/services/player-achievements/src/migrations/1701140634237-trophy.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { asyncForEach } from '@cats-cradle/nestjs-modules'; -import { ObjectId, UUID } from 'bson'; -import { v4 } from 'uuid'; -import { getDb } from '../migrations-utils/db'; -import { Achievements } from '../models/achievements'; - -export const up = async () => { - const db = await getDb(); - - await asyncForEach(Achievements, async (trophy: object) => { - const record = { _id: new UUID(v4()) as any as ObjectId }; - - await db.collection('trophies').insertOne({ ...record, ...trophy }); - }); -}; - -export const down = async () => { - const db = await getDb(); - - await asyncForEach(Achievements, async (trophy) => { - await db.collection('trophies').deleteOne(trophy); - }); -};