Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [ 18, 20 ]
node-version: [ 22, 24 ]
steps:
- name: Checkout Source code
uses: actions/checkout@v6
Expand All @@ -24,11 +24,10 @@ jobs:
with:
node-version: ${{ matrix.node-version }}

- name: Cache node_modules
uses: actions/cache@v5
with:
path: '**/node_modules'
key: ${{ runner.os }}-${{ matrix.node-version }}-modules-${{ hashFiles('**/yarn.lock') }}
- name: Enable corepack
run: |
corepack enable
corepack prepare yarn@stable --activate

- name: Install
run: yarn
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
temp
node_modules
.idea
.yarn
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
63 changes: 19 additions & 44 deletions app/entities/Author.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
'use strict';

import { Collection, EntitySchema } from '@mikro-orm/core';
import { defineEntity, p } from '@mikro-orm/core';
import { BaseEntitySchema } from './BaseEntity.js';
import { Book } from './Book.js';
import { BaseEntity } from './BaseEntity.js';

/**
* @property {string} name
* @property {string} email
* @property {number} age
* @property {boolean} termsAccepted
* @property {string[]} identities
* @property {Date} born
* @property {Collection<Book>} books
* @property {Book} favouriteBook
* @property {number} version
* @property {string} versionAsString
*/
export class Author extends BaseEntity {
export const AuthorSchema = defineEntity({
extends: BaseEntitySchema,
name: 'Author',
properties: {
name: p.string(),
email: p.string(),
age: p.integer().nullable(),
termsAccepted: p.boolean(),
identities: p.array().nullable(),
born: p.datetime().nullable(),
books: () => p.oneToMany(Book).mappedBy('author'),
favouriteBook: () => p.manyToOne(Book).nullable(),
},
});

export class Author extends AuthorSchema.class {

/**
* @param {string} name
* @param {string} email
*/
constructor(name, email) {
super();
this.name = name;
Expand All @@ -31,28 +30,4 @@ export class Author extends BaseEntity {

}

Author.beforeDestroyCalled = 0;
Author.afterDestroyCalled = 0;

export const schema = new EntitySchema({
class: Author,
extends: 'BaseEntity',
properties: {
name: { type: 'string' },
email: { type: 'string' },
age: { type: 'number', nullable: true },
termsAccepted: { type: 'boolean' },
identities: { type: 'string[]', nullable: true },
born: { type: 'Date', nullable: true },
books: {
kind: '1:m',
mappedBy: 'author',
type: 'Book',
},
favouriteBook: {
kind: 'm:1',
type: 'Book',
nullable: true,
},
},
});
AuthorSchema.setClass(Author);
32 changes: 6 additions & 26 deletions app/entities/BaseEntity.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
'use strict';

import { Collection, ReferenceKind, EntitySchema, wrap } from '@mikro-orm/core';
import { defineEntity, p } from '@mikro-orm/core';

/**
* @property {number} id
* @property {Date} createdAt
* @property {Date} updatedAt
*/
export class BaseEntity {

constructor() {
this.createdAt = new Date();
this.updatedAt = new Date();
const props = wrap(this).__meta.properties;

Object.keys(props).forEach(prop => {
if ([ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(props[prop].reference)) {
this[prop] = new Collection(this);
}
});
}

}

export const schema = new EntitySchema({
export const BaseEntitySchema = defineEntity({
name: 'BaseEntity',
abstract: true,
properties: {
id: { primary: true, type: 'number' },
createdAt: { type: 'Date' },
updatedAt: { type: 'Date', onUpdate: () => new Date() },
id: p.integer().primary(),
createdAt: p.datetime().onCreate(() => new Date()),
updatedAt: p.datetime().onCreate(() => new Date()).onUpdate(() => new Date()),
},
});
52 changes: 16 additions & 36 deletions app/entities/Book.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
'use strict';

import { Collection, EntitySchema } from '@mikro-orm/core';
import { Publisher } from './Publisher.js';
import { defineEntity, p } from '@mikro-orm/core';
import { BaseEntitySchema } from './BaseEntity.js';
import { Author } from './Author.js';
import { Publisher } from './Publisher.js';
import { BookTag } from './BookTag.js';
import { BaseEntity } from './BaseEntity.js';

/**
* @property {string} title
* @property {Author} author
* @property {Publisher} publisher
* @property {Collection<BookTag>} tags
*/
export class Book extends BaseEntity {
export const BookSchema = defineEntity({
extends: BaseEntitySchema,
name: 'Book',
properties: {
title: p.string(),
author: () => p.manyToOne(Author),
publisher: () => p.manyToOne(Publisher).nullable(),
tags: () => p.manyToMany(BookTag).owner().inversedBy('books'),
},
});

export class Book extends BookSchema.class {

/**
* @param {string} title
* @param {Author} author
*/
constructor(title, author) {
super();
this.title = title;
Expand All @@ -26,25 +27,4 @@ export class Book extends BaseEntity {

}

export const schema = new EntitySchema({
class: Book,
extends: 'BaseEntity',
properties: {
title: { type: 'string' },
author: {
kind: 'm:1',
type: 'Author',
},
publisher: {
kind: 'm:1',
type: 'Publisher',
nullable: true,
},
tags: {
kind: 'm:n',
owner: true,
inversedBy: 'books',
type: 'BookTag',
},
},
});
BookSchema.setClass(Book);
37 changes: 13 additions & 24 deletions app/entities/BookTag.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
'use strict';

import { Collection, EntitySchema } from '@mikro-orm/core';
import { defineEntity, p } from '@mikro-orm/core';
import { BaseEntitySchema } from './BaseEntity.js';
import { Book } from './Book.js';
import { BaseEntity } from './BaseEntity.js';

/**
* @property {number} id
* @property {string} name
* @property {Collection<Book>} books
*/
export class BookTag extends BaseEntity {
export const BookTagSchema = defineEntity({
extends: BaseEntitySchema,
name: 'BookTag',
properties: {
name: p.string(),
books: () => p.manyToMany(Book).mappedBy('tags'),
},
});

export class BookTag extends BookTagSchema.class {

/**
* @param {string} name
*/
constructor(name) {
super();
this.name = name;
}

}

export const schema = new EntitySchema({
class: BookTag,
extends: 'BaseEntity',
properties: {
name: { type: 'string' },
books: {
kind: 'm:n',
owner: false,
mappedBy: 'tags',
type: 'Book',
},
},
});
BookTagSchema.setClass(BookTag);
39 changes: 14 additions & 25 deletions app/entities/Publisher.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
'use strict';

import { Collection, EntitySchema } from '@mikro-orm/core';
import { defineEntity, p } from '@mikro-orm/core';
import { BaseEntitySchema } from './BaseEntity.js';
import { Book } from './Book.js';
import { BaseEntity } from './BaseEntity.js';

/**
* @property {string} name
* @property {string} type
* @property {Collection<Book>} books
*/
export class Publisher extends BaseEntity {
export const PublisherSchema = defineEntity({
extends: BaseEntitySchema,
name: 'Publisher',
properties: {
name: p.string(),
type: p.string(),
books: () => p.oneToMany(Book).mappedBy('publisher'),
},
});

export class Publisher extends PublisherSchema.class {

constructor(name = 'asd', type = 'local') {
super();
Expand All @@ -19,20 +24,4 @@ export class Publisher extends BaseEntity {

}

export const schema = new EntitySchema({
class: Publisher,
extends: 'BaseEntity',
properties: {
name: {
type: 'string',
},
books: {
kind: '1:m',
mappedBy: 'publisher',
type: 'Book',
},
type: {
type: 'string',
},
},
});
PublisherSchema.setClass(Publisher);
6 changes: 6 additions & 0 deletions app/mikro-orm.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { defineConfig } from '@mikro-orm/sqlite';
import { SqlHighlighter } from '@mikro-orm/sql-highlighter';
import { BaseEntitySchema } from './entities/BaseEntity.js';
import { AuthorSchema } from './entities/Author.js';
import { BookSchema } from './entities/Book.js';
import { BookTagSchema } from './entities/BookTag.js';
import { PublisherSchema } from './entities/Publisher.js';

export default defineConfig({
dbName: 'mikro-orm-express-js',
entities: [BaseEntitySchema, AuthorSchema, BookSchema, BookTagSchema, PublisherSchema],
highlighter: new SqlHighlighter(),
dynamicImportProvider: id => import(id),
});
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
"test": "mocha **/*.spec.js"
},
"dependencies": {
"express": "^5.0.0",
"express-promise-router": "^4.1.1",
"@mikro-orm/core": "^7.0.1",
"@mikro-orm/sql-highlighter": "^1.0.0",
"@mikro-orm/sqlite": "^7.0.1",
"@mikro-orm/sql-highlighter": "^1.0.0"
"express": "^5.0.0",
"express-promise-router": "^4.1.1"
},
"devDependencies": {
"@mikro-orm/cli": "^7.0.1",
Expand All @@ -31,5 +31,6 @@
"configPaths": [
"./app/mikro-orm.config.js"
]
}
},
"packageManager": "yarn@4.13.0"
}
Loading