Skip to content

Commit 1d94b01

Browse files
committed
update to v6
1 parent 20a2347 commit 1d94b01

17 files changed

+842
-1263
lines changed

.env

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
MIKRO_ORM_DYNAMIC_IMPORTS=1
2-
MIKRO_ORM_TYPE=mongo
2+
MIKRO_ORM_TYPE=better-sqlite
33
MIKRO_ORM_ENTITIES=./app/entities/*.js
4-
MIKRO_ORM_DB_NAME=mikro-orm-express-js
54
MIKRO_ORM_DEBUG=true

.github/workflows/tests.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
fail-fast: false
1616
matrix:
17-
node-version: [ 14, 16 ]
17+
node-version: [ 18, 20 ]
1818
steps:
1919
- name: Checkout Source code
2020
uses: actions/checkout@v4
@@ -25,19 +25,13 @@ jobs:
2525
node-version: ${{ matrix.node-version }}
2626

2727
- name: Cache node_modules
28-
uses: actions/cache@v3
28+
uses: actions/cache@v4
2929
with:
3030
path: '**/node_modules'
3131
key: ${{ runner.os }}-${{ matrix.node-version }}-modules-${{ hashFiles('**/yarn.lock') }}
3232

33-
- name: Init docker
34-
run: docker-compose up -d
35-
3633
- name: Install
3734
run: yarn
3835

3936
- name: Test
4037
run: yarn test
41-
42-
- name: Teardown docker
43-
run: docker-compose down

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
# Express + MongoDB + JavaScript example integration
1+
# Express + SQLite + JavaScript example integration
22

33
1. Install dependencies via `yarn` or `npm install`
4-
2. Run `docker-compose up -d` to start mongodb
5-
3. Run via `yarn start` or `yarn start:dev` (nodemon)
6-
4. Example API is running on localhost:3000
4+
2. Run via `yarn start` or `yarn start:dev` (nodemon)
5+
3. Example API is running on localhost:3000
76

87
Available routes:
98

app/controllers/author.controller.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Author } from '../entities/Author.js';
88
const router = Router();
99

1010
router.get('/', async (req, res) => {
11-
const authors = await DI.authorRepository.findAll({
11+
const authors = await DI.authors.findAll({
1212
populate: ['books'],
1313
orderBy: { name: QueryOrder.DESC },
1414
limit: 20,
@@ -18,7 +18,7 @@ router.get('/', async (req, res) => {
1818

1919
router.get('/:id', async (req, res) => {
2020
try {
21-
const author = await DI.authorRepository.findOne(req.params.id, {
21+
const author = await DI.authors.findOne(+req.params.id, {
2222
populate: ['books'],
2323
});
2424

@@ -40,7 +40,7 @@ router.post('/', async (req, res) => {
4040

4141
try {
4242
const author = DI.em.create(Author, req.body);
43-
await DI.em.persist(author).flush();
43+
await DI.em.flush();
4444

4545
res.json(author);
4646
} catch (e) {
@@ -50,9 +50,9 @@ router.post('/', async (req, res) => {
5050

5151
router.put('/:id', async (req, res) => {
5252
try {
53-
const author = await DI.authorRepository.findOneOrFail(req.params.id);
53+
const author = await DI.authors.findOneOrFail(+req.params.id);
5454
wrap(author).assign(req.body);
55-
await DI.authorRepository.flush();
55+
await DI.em.flush();
5656

5757
res.json(author);
5858
} catch (e) {

app/controllers/author.controller.spec.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import request from 'supertest';
2-
import expect from 'expect';
2+
import { expect } from 'expect';
33
import { app, DI, server } from '../server.js';
44

55
describe('author controller', () => {
66

77
before(async () => {
8-
DI.orm.config.set('dbName', 'express-test-db');
9-
DI.orm.config.getLogger().setDebugMode(false);
10-
await DI.orm.config.getDriver().reconnect();
11-
await DI.orm.getSchemaGenerator().clearDatabase();
8+
await DI.orm.reconnect({ dbName: ':memory:', debug: false });
9+
await DI.orm.schema.createSchema();
1210
});
1311

1412
after(async () => {

app/controllers/book.controller.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { DI } from '../server.js';
88
const router = Router();
99

1010
router.get('/', async (req, res) => {
11-
const books = await DI.bookRepository.findAll({
11+
const books = await DI.books.findAll({
1212
populate: ['author'],
1313
orderBy: { title: QueryOrder.DESC },
1414
limit: 20,
@@ -18,7 +18,7 @@ router.get('/', async (req, res) => {
1818

1919
router.get('/:id', async (req, res) => {
2020
try {
21-
const book = await DI.bookRepository.findOneOrFail(req.params.id, {
21+
const book = await DI.books.findOneOrFail(+req.params.id, {
2222
populate: ['author'],
2323
});
2424
res.json(book);
@@ -35,8 +35,7 @@ router.post('/', async (req, res) => {
3535

3636
try {
3737
const book = DI.em.create(Book, req.body);
38-
wrap(book.author, true).__initialized = true;
39-
await DI.em.persist(book).flush();
38+
await DI.em.flush();
4039

4140
res.json(book);
4241
} catch (e) {
@@ -46,9 +45,9 @@ router.post('/', async (req, res) => {
4645

4746
router.put('/:id', async (req, res) => {
4847
try {
49-
const book = await DI.bookRepository.findOneOrFail(req.params.id);
48+
const book = await DI.books.findOneOrFail(+req.params.id);
5049
wrap(book).assign(req.body);
51-
await DI.bookRepository.flush();
50+
await DI.em.flush();
5251

5352
res.json(book);
5453
} catch (e) {

app/controllers/book.controller.spec.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import request from 'supertest';
2-
import expect from 'expect';
2+
import { expect } from 'expect';
33
import { app, DI, server } from '../server.js';
44

5-
describe('author controller', () => {
5+
describe('book controller', () => {
66

77
before(async () => {
8-
DI.orm.config.set('dbName', 'express-test-db');
9-
DI.orm.config.getLogger().setDebugMode(false);
10-
await DI.orm.config.getDriver().reconnect();
11-
await DI.orm.getSchemaGenerator().clearDatabase();
8+
await DI.orm.reconnect({ dbName: ':memory:', debug: false });
9+
await DI.orm.schema.createSchema();
1210
});
1311

1412
after(async () => {

app/entities/Author.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ export const schema = new EntitySchema({
4545
identities: { type: 'string[]', nullable: true },
4646
born: { type: 'Date', nullable: true },
4747
books: {
48-
reference: '1:m',
48+
kind: '1:m',
4949
mappedBy: 'author',
5050
type: 'Book',
5151
},
5252
favouriteBook: {
53-
reference: 'm:1',
53+
kind: 'm:1',
5454
type: 'Book',
5555
nullable: true,
5656
},

app/entities/BaseEntity.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22

3-
import { Collection, ReferenceType, EntitySchema, wrap } from '@mikro-orm/core';
3+
import { Collection, ReferenceKind, EntitySchema, wrap } from '@mikro-orm/core';
44

55
/**
6-
* @property {ObjectID} _id
6+
* @property {number} id
77
* @property {Date} createdAt
88
* @property {Date} updatedAt
99
*/
@@ -15,7 +15,7 @@ export class BaseEntity {
1515
const props = wrap(this).__meta.properties;
1616

1717
Object.keys(props).forEach(prop => {
18-
if ([ReferenceType.ONE_TO_MANY, ReferenceType.MANY_TO_MANY].includes(props[prop].reference)) {
18+
if ([ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(props[prop].reference)) {
1919
this[prop] = new Collection(this);
2020
}
2121
});
@@ -26,7 +26,7 @@ export class BaseEntity {
2626
export const schema = new EntitySchema({
2727
name: 'BaseEntity',
2828
properties: {
29-
_id: { primary: true, type: 'ObjectID' },
29+
id: { primary: true, type: 'number' },
3030
createdAt: { type: 'Date' },
3131
updatedAt: { type: 'Date', onUpdate: () => new Date() },
3232
},

app/entities/Book.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ export const schema = new EntitySchema({
3232
properties: {
3333
title: { type: 'string' },
3434
author: {
35-
reference: 'm:1',
35+
kind: 'm:1',
3636
type: 'Author',
3737
},
3838
publisher: {
39-
reference: 'm:1',
39+
kind: 'm:1',
4040
type: 'Publisher',
4141
nullable: true,
4242
},
4343
tags: {
44-
reference: 'm:n',
44+
kind: 'm:n',
4545
owner: true,
4646
inversedBy: 'books',
4747
type: 'BookTag',

0 commit comments

Comments
 (0)