From what I read on their website you just need to create a class for migrations and use it in a cli command. Technically you can inject/create the options object in the same way and reuse it in that class.
Unfortunately I can't provide yet some boilerplate for that. In my current project I switched rather early on from Typeorm to MikroOrm, but in this aspect they seem rather similar so this approach should work.
The only noticeable difference is that for MikroOrm I managed to find a third party executable that is able to run for a specific project, for this you might need to either have a global config or a custom script to run from a different path.
Looking forward into your posts about migration, i'm still trying to grasp the concepts of monorepo and all that jazz witnin this series of posts worked well for me, I also tried your approach into mirkoorm and it worked, still my concerns are on migrations, what third party tool did you use?
I can actually provide some snippets for this., they might be useful for after you use the instructions from the package's page.
./mikro-orm.base.ts - at the root of the nx project I have a config that can be propagated for any number of projects
import{LoadStrategy,MikroORMOptions}from'@mikro-orm/core';import{TSMigrationGenerator}from'@mikro-orm/migrations';import{PostgreSqlDriver}from'@mikro-orm/postgresql';import{SqlHighlighter}from'@mikro-orm/sql-highlighter';exportfunctionmikroOrmConfigBase(path=__dirname):Partial<MikroORMOptions<PostgreSqlDriver>>{constundocumentedConfig:any={ignoreSchema:['columnar'],// schema generated by and for citus};return{entities:[path+'/src/**/*.entity{.ts,.js}'],entitiesTs:[path+'/src/**/*.entity.ts'],type:'postgresql',persistOnCreate:false,// we have to manually call persistAndFlushallowGlobalContext:true,forceUtcTimezone:true,strict:true,// disable automatic type castingloadStrategy:LoadStrategy.JOINED,highlighter:newSqlHighlighter(),// default values:schemaGenerator:{disableForeignKeys:true,// wrap statements with `set foreign_key_checks = 0` or equivalentcreateForeignKeyConstraints:true,// whether to generate FK constraints...undocumentedConfig,},migrations:{tableName:'mikro_orm_migrations',// name of database table with log of executed transactionspath:path+'/src/**/data-access/migrations',// path to the folder with migrationspathTs:path+'/src/**/data-access/migrations',// path to the folder with TS migrations (if used, we should put path to compiled files in `path`)glob:'!(*.d).{js,ts}',// how to match migration files (all .js and .ts files, but not .d.ts)transactional:true,// wrap each migration in a transactiondisableForeignKeys:true,// wrap statements with `set foreign_key_checks = 0` or equivalentallOrNothing:true,// wrap all migrations in master transactiondropTables:true,// allow to disable table droppingsafe:true,// allow to disable table and column droppingsnapshot:true,// save snapshot when creating new migrationsemit:'ts',// migration generation modegenerator:TSMigrationGenerator,// migration generator, e.g. to allow custom formatting},seeder:{path:path+'/src/**/data-access/seeder',// path to the folder with seederspathTs:path+'/src/**/data-access/seeder',// path to the folder with TS seeders (if used, we should put path to compiled files in `path`)defaultSeeder:'DatabaseSeeder',// default seeder class nameglob:'!(*.d).{js,ts}',// how to match seeder files (all .js and .ts files, but not .d.ts)emit:'ts',// seeder generation modefileName:(className:string)=>className,// seeder file naming convention},};}
Please change any paths that might be different for your project.
Then in my backend project I have
/apps/backend/project.json -> under the "target" options
There must be something wrong with webpack i tried your approach it wont detect entities on runtime thats just sad, i just made another class to do my migrations i use this approach on runtime i use your previous service inject approach, but i manually override entity locations. Thank you!
Yes. Webpack packs everything together in a single bundle and i suppose the paths are ruined in that case.
For that Nest has the "autoLoadEntories" option vut you need to import every entity in the ORM's module for it to work.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Doing migrations has changed since type orm 0.3 old cli into typeormoptions are not there anymore. am i right?
From what I read on their website you just need to create a class for migrations and use it in a cli command. Technically you can inject/create the options object in the same way and reuse it in that class.
Unfortunately I can't provide yet some boilerplate for that. In my current project I switched rather early on from Typeorm to MikroOrm, but in this aspect they seem rather similar so this approach should work.
The only noticeable difference is that for MikroOrm I managed to find a third party executable that is able to run for a specific project, for this you might need to either have a global config or a custom script to run from a different path.
Looking forward into your posts about migration, i'm still trying to grasp the concepts of monorepo and all that jazz witnin this series of posts worked well for me, I also tried your approach into mirkoorm and it worked, still my concerns are on migrations, what third party tool did you use?
I used @alexy4744/nx-mikro-orm-cli
I can actually provide some snippets for this., they might be useful for after you use the instructions from the package's page.
./mikro-orm.base.ts - at the root of the nx project I have a config that can be propagated for any number of projects
Please change any paths that might be different for your project.
Then in my
backend
project I have/apps/backend/project.json -> under the "target" options
/apps/backend/mikro-orm.ts
As you can see I just reused the functions from the previous articles to get the env variables from the env file or from the environment.
Also in the
data acces
folder which for me hosts the entities i have aDatabaseSeeder.ts
file that gets the repositories and populates the data.I cannot post that one as it is 100% custom made for my project's needs and it wouldn't be useful at all, but I can post a small snippet
I hope this helps!
There must be something wrong with webpack i tried your approach it wont detect entities on runtime thats just sad, i just made another class to do my migrations i use this approach on runtime i use your previous service inject approach, but i manually override entity locations. Thank you!
Yes. Webpack packs everything together in a single bundle and i suppose the paths are ruined in that case.
For that Nest has the "autoLoadEntories" option vut you need to import every entity in the ORM's module for it to work.