Skip to content

Commit

Permalink
feat: add support for MikroORM v6
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Metadata structure's changed in MikroORM v6. The adapter setup remains the same, but access to some information's changed so this version will only work with MikroORM v6+.
  • Loading branch information
dziraf committed Mar 4, 2024
1 parent df12c09 commit d245bc7
Show file tree
Hide file tree
Showing 20 changed files with 5,181 additions and 3,887 deletions.
29 changes: 29 additions & 0 deletions example-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Local setup

Prepare database service:
```bash
$ docker-compose up -d
$ cp example.env .env
```

Default env vars should work if you used docker-compose to setup Postgres.

Build app, setup database schema, run app:

```bash
$ yarn
$ yarn build
$ yarn db:setup
$ yarn start
```

Optionally: you can link local `@adminjs/mikroorm` if you plan to make changes.

```bash
$ cd .. # return to root directory
$ yarn link # create a link to local package
$ yarn clean && yarn build # clean workdir and build local package
$ cd example-app # return to example app
$ yarn link "@adminjs/mikroorm"
```
You should now see your local changes in `@adminjs/mikroorm`. Make sure to re-run the example app whenever you make changes, or setup nodemon.
20 changes: 10 additions & 10 deletions example-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
"license": "MIT",
"scripts": {
"build": "tsc",
"db:setup": "ts-node ./src/scripts/setup-db.ts",
"start": "node build/index.js",
"dev": "yarn build && concurrently \"yarn build --watch\" \"nodemon --ext '.js' --watch ../lib --watch ./build node build/index.js\""
"db:setup": "dotenv -c '.env' -- node build/scripts/setup-db.js",
"start": "dotenv -c '.env' -- node build/index.js"
},
"devDependencies": {
"@types/express": "^4.17.17",
"@types/node": "^18",
"@types/uuid": "^9.0.1",
"dotenv-cli": "^7.3.0",
"nodemon": "^2.0.21",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"dependencies": {
"@adminjs/express": "^5.1.0",
"@adminjs/mikroorm": "^2.0.0",
"@mikro-orm/core": "^5.6.13",
"@mikro-orm/postgresql": "^5.6.13",
"adminjs": "^6.8.7",
"@adminjs/express": "^6.1.0",
"@adminjs/mikroorm": "^3.0.0",
"@mikro-orm/core": "^6.1.7",
"@mikro-orm/postgresql": "^6.1.7",
"adminjs": "^7.7.0",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-formidable": "^1.2.0",
Expand Down
10 changes: 5 additions & 5 deletions example-app/src/entities/Car.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
Property,
} from '@mikro-orm/core';

import { Seller } from './Seller.js';
import { User } from './User.js';
import type { Seller } from './Seller.js';
import type { User } from './User.js';

@Entity({ tableName: 'cars' })
export class Car extends BaseEntity<Car, 'id'> {
export class Car extends BaseEntity {
@PrimaryKey()
id: number;

Expand All @@ -31,9 +31,9 @@ export class Car extends BaseEntity<Car, 'id'> {
})
updatedAt: Date = new Date();

@ManyToOne(() => User, { mapToPk: true })
@ManyToOne(() => 'User', { mapToPk: true })
owner: User;

@ManyToOne(() => Seller, { mapToPk: true })
@ManyToOne(() => 'Seller', { mapToPk: true })
seller: Seller;
}
6 changes: 3 additions & 3 deletions example-app/src/entities/Seller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
import { BaseEntity, Entity, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { v4 } from 'uuid';

import { Car } from './Car.js';
import type { Car } from './Car.js';

export enum UserRoles {
DESIGNER = 'designer',
CLIENT = 'client',
}

@Entity()
export class Seller extends BaseEntity<Seller, 'id'> {
export class Seller extends BaseEntity {
@PrimaryKey({ columnType: 'uuid' })
id = v4();

@Property({ fieldName: 'name', columnType: 'text' })
name: string;

@OneToMany(() => Car, (car) => car.seller)
@OneToMany(() => 'Car', (car: Car) => car.seller)
cars: Car[];
}
6 changes: 3 additions & 3 deletions example-app/src/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import { v4 } from 'uuid';
import { BaseEntity, Entity, Enum, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';

import { Car } from './Car.js';
import type { Car } from './Car.js';

enum UserRole {
ADMIN = 'admin',
CLIENT = 'client',
}

@Entity({ tableName: 'users' })
export class User extends BaseEntity<User, 'id'> {
export class User extends BaseEntity {
@PrimaryKey({ columnType: 'uuid' })
id = v4();

Expand All @@ -32,6 +32,6 @@ export class User extends BaseEntity<User, 'id'> {
@Property({ fieldName: 'updated_at', columnType: 'timestamptz', onUpdate: () => new Date() })
updatedAt: Date = new Date();

@OneToMany(() => Car, (car) => car.owner)
@OneToMany(() => 'Car', (car: Car) => car.owner)
cars: Car[];
}
6 changes: 2 additions & 4 deletions example-app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import 'reflect-metadata';
import path from 'path';
import dotenv from 'dotenv';

dotenv.config({ path: path.join(__dirname, '../.env') });
/* eslint-disable import/first */
import express from 'express';
import AdminJS from 'adminjs';
import AdminJSExpress from '@adminjs/express';
import { Database, Resource } from '@adminjs/mikroorm';
import { MikroORM } from '@mikro-orm/core';

import { PostgreSqlDriver } from '@mikro-orm/postgresql';
import { User, Car, Seller } from './entities/index.js';
/* eslint-enable import/first */

Expand All @@ -19,7 +17,7 @@ const run = async () => {
const orm = await MikroORM.init({
entities: [User, Car, Seller],
dbName: process.env.DATABASE_NAME,
type: 'postgresql',
driver: PostgreSqlDriver,
clientUrl: process.env.DATABASE_URL,
debug: true,
});
Expand Down
19 changes: 2 additions & 17 deletions example-app/src/scripts/setup-db.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,21 @@
/* eslint-disable no-console */
import 'reflect-metadata';
import path from 'path';
import dotenv from 'dotenv';

dotenv.config({ path: path.join(__dirname, '../../.env') });
/* eslint-disable import/first */
import { MikroORM } from '@mikro-orm/core';
import { PostgreSqlDriver } from '@mikro-orm/postgresql';
import { Car, Seller, User } from '../entities/index.js';
/* eslint-enable import/first */

(async () => {
const orm = await MikroORM.init({
entities: [User, Car, Seller],
dbName: process.env.DATABASE_NAME,
type: 'postgresql',
driver: PostgreSqlDriver,
clientUrl: process.env.DATABASE_URL,
});
const generator = orm.getSchemaGenerator();

const dropDump = await generator.getDropSchemaSQL();
console.log(dropDump);

const createDump = await generator.getCreateSchemaSQL();
console.log(createDump);

const updateDump = await generator.getUpdateSchemaSQL();
console.log(updateDump);

// there is also `generate()` method that returns drop + create queries
const dropAndCreateDump = await generator.generate();
console.log(dropAndCreateDump);

// or you can run those queries directly, but be sure to check them first!
await generator.dropSchema();
await generator.createSchema();
Expand Down
4 changes: 3 additions & 1 deletion example-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"sourceMap": true
"sourceMap": false,
"skipDefaultLibCheck": true,
"skipLibCheck": true
},
"include": [
"./src/**/*"
Expand Down
Loading

0 comments on commit d245bc7

Please sign in to comment.