Skip to content

Commit e989388

Browse files
authored
feat: add in support for 0.3 typeorm (#43)(@asheliahut)
Update adapter to support new Typeorm BREAKING CHANGE: The adapter has been updated to work with Typeorm of version ~0.3.0 so it will no longer work with older versions of Typeorm. - Change `Connection` inputsto `DataSource` for referencing database - Update all find methods to findBy
1 parent 25cee7e commit e989388

18 files changed

+4025
-5033
lines changed

.github/workflows/push.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
- name: Lint
6363
run: yarn lint
6464
- name: Migrate
65-
run: yarn typeorm migration:run
65+
run: yarn run migrate
6666
- name: Test
6767
run: yarn test
6868

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ Resource.validate = validate
2525
import {
2626
BaseEntity,
2727
Entity, PrimaryGeneratedColumn, Column,
28-
createConnection,
2928
ManyToOne,
3029
RelationId
3130
} from 'typeorm'
31+
import { MyDataSource } from './my-data-source'
3232
import * as express from 'express'
3333
import { Database, Resource } from '@adminjs/typeorm'
3434
import { validate } from 'class-validator'
@@ -67,13 +67,10 @@ export class Person extends BaseEntity
6767

6868
( async () =>
6969
{
70-
const connection = await createConnection({/* ... */})
71-
72-
// Applying connection to model
73-
Person.useConnection(connection)
70+
await MyDataSource.initialize();
7471

7572
const adminJs = new AdminJS({
76-
// databases: [connection],
73+
// databases: [MyDataSource],
7774
resources: [
7875
{ resource: Person, options: { parent: { name: 'foobar' } } }
7976
],

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"test": "mocha -r ts-node/register ./spec/**/*.spec.ts",
2020
"ts-node": "ts-node",
2121
"lint": "eslint './src/**/*.{ts,js}' './spec/**/*.{ts,js}' './example-app/**/*.{ts,js}' --ignore-pattern 'build' --ignore-pattern 'yarn.lock'",
22+
"migrate": "ts-node ./node_modules/typeorm/cli.js migration:run -d spec/utils/test-data-source.ts",
2223
"check:all": "yarn lint && yarn test && yarn build",
2324
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js",
2425
"release": "semantic-release"
@@ -36,7 +37,7 @@
3637
"license": "MIT",
3738
"peerDependencies": {
3839
"adminjs": "^5.0.0",
39-
"typeorm": "~0.2.45"
40+
"typeorm": "~0.3.0"
4041
},
4142
"optionalDependencies": {},
4243
"devDependencies": {
@@ -59,12 +60,12 @@
5960
"eslint-plugin-react-hooks": "^4.0.8",
6061
"husky": "^4.2.5",
6162
"mocha": "^6.2.2",
62-
"pg": "^7.12.1",
63+
"pg": "^8.7.3",
6364
"semantic-release": "^17.0.7",
6465
"semantic-release-slack-bot": "^1.6.2",
65-
"ts-node": "^8.4.1",
66-
"tsconfig-paths": "^3.9.0",
67-
"typeorm": "~0.2.45",
68-
"typescript": "^4.0.3"
66+
"ts-node": "^10.7.0",
67+
"tsconfig-paths": "^3.14.1",
68+
"typeorm": "~0.3.0",
69+
"typescript": "^4.6.3"
6970
}
7071
}

spec/Database.spec.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
import { expect } from 'chai'
22

3-
import { connection, connect, close } from './utils/testConnection'
3+
import { dataSource } from './utils/test-data-source'
44
import { Database } from '../src/Database'
55

66
describe('Database', () => {
77
before(async () => {
8-
await connect()
8+
await dataSource.initialize()
99
})
1010

11-
after(() => { close() })
11+
after(() => { dataSource.destroy() })
1212

1313
describe('.isAdapterFor', () => {
14-
it('returns true when typeorm connection is given', () => {
15-
expect(Database.isAdapterFor(connection)).to.equal(true)
16-
})
17-
it('returns false for any other data', () => {
18-
expect(Database.isAdapterFor({})).to.equal(false)
14+
it('returns true when typeorm DataSource is given', () => {
15+
expect(Database.isAdapterFor(dataSource)).to.equal(true)
1916
})
17+
// Test is irrelevent because isAdapterFor is typed
18+
// it('returns false for any other data', () => {
19+
// expect(Database.isAdapterFor()).to.equal(false)
20+
// })
2021
})
2122

2223
describe('#resources', () => {
2324
it('returns all entities', async () => {
24-
expect(new Database(connection).resources()).to.have.lengthOf(3)
25+
expect(new Database(dataSource).resources()).to.have.lengthOf(3)
2526
})
2627
})
2728
})

spec/Property.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { expect } from 'chai'
33
import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata'
44
import { Property } from '../src/Property'
55
import { Car } from './entities/Car'
6-
import { connect, close } from './utils/testConnection'
6+
import { dataSource } from './utils/test-data-source'
77

88
describe('Property', () => {
99
let columns: Array<ColumnMetadata>
1010

1111
before(async () => {
12-
await connect()
12+
await dataSource.initialize()
1313
})
1414

1515
beforeEach(() => {
@@ -18,7 +18,7 @@ describe('Property', () => {
1818

1919
after(async () => {
2020
await Car.delete({})
21-
close()
21+
dataSource.destroy()
2222
})
2323

2424
describe('#name', () => {

spec/Resource.spec.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { validate } from 'class-validator'
44

55
import { Car } from './entities/Car'
66
import { CarDealer } from './entities/CarDealer'
7-
import { connect, close } from './utils/testConnection'
7+
import { dataSource } from './utils/test-data-source'
88

99
import { Resource } from '../src/Resource'
1010
import { CarBuyer } from './entities/CarBuyer'
@@ -22,7 +22,7 @@ describe('Resource', () => {
2222
}
2323

2424
before(async () => {
25-
await connect()
25+
await dataSource.initialize()
2626
})
2727

2828
beforeEach(async () => {
@@ -33,7 +33,7 @@ describe('Resource', () => {
3333
})
3434

3535
after(async () => {
36-
close()
36+
dataSource.destroy()
3737
})
3838

3939
describe('.isAdapterFor', () => {
@@ -68,12 +68,12 @@ describe('Resource', () => {
6868

6969
describe('#properties', () => {
7070
it('returns all the properties', () => {
71-
expect(resource.properties()).to.have.lengthOf(13)
71+
expect(resource.properties()).to.have.lengthOf(12)
7272
})
7373

7474
it('returns all properties with the correct position', () => {
7575
expect(resource.properties().map((property) => property.position())).to.deep.equal([
76-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
76+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
7777
])
7878
})
7979
})
@@ -138,23 +138,29 @@ describe('Resource', () => {
138138

139139
it('stores Column with defined name property', async () => {
140140
const params = await resource.create(data)
141-
const storedRecord = await Car.findOne(params.carId) as Car
141+
const reference: any = {}
142+
reference[resource.idName()] = params.carId
143+
const storedRecord: Car|null = await Car.findOneBy(reference)
142144

143-
expect(storedRecord.streetNumber).to.equal(data.streetNumber)
145+
expect(storedRecord?.streetNumber).to.equal(data.streetNumber)
144146
})
145147

146148
it('stores number Column with property as string', async () => {
147149
const params = await resource.create(data)
148-
const storedRecord = await Car.findOne(params.carId) as Car
150+
const reference: any = {}
151+
reference[resource.idName()] = params.carId
152+
const storedRecord: Car|null = await Car.findOneBy(reference)
149153

150-
expect(storedRecord.stringAge).to.equal(4)
154+
expect(storedRecord?.stringAge).to.equal(4)
151155
})
152156

153157
it('stores mixed type properties', async () => {
154158
const params = await resource.create(data)
155-
const storedRecord = await Car.findOne(params.carId) as Car
159+
const reference: any = {}
160+
reference[resource.idName()] = params.carId
161+
const storedRecord: Car|null = await Car.findOneBy(reference)
156162

157-
expect(storedRecord.meta).to.deep.equal({
163+
expect(storedRecord?.meta).to.deep.equal({
158164
title: data['meta.title'],
159165
description: data['meta.description'],
160166
})
@@ -218,7 +224,7 @@ describe('Resource', () => {
218224
})
219225
const recordInDb = await resource.findOne((record && record.id()) as string)
220226

221-
expect(recordInDb && recordInDb.param('name')).to.equal(ford)
227+
expect(recordInDb && recordInDb.get('name')).to.equal(ford)
222228
})
223229

224230
it('throws error when wrong name is given', async () => {
@@ -274,7 +280,7 @@ describe('Resource', () => {
274280

275281
afterEach(async () => {
276282
await Car.delete(carParams.carId)
277-
await CarDealer.delete(carDealer)
283+
await CarDealer.delete(carDealer.id)
278284
})
279285

280286
it('deletes the resource', async () => {

spec/entities/Car.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ export class Car extends BaseEntity {
6060
public carDealerId: number;
6161

6262
@ManyToOne(() => CarBuyer, (carBuyer) => carBuyer.cars)
63-
// @JoinColumn({
64-
// name: 'car_buyer_id',
65-
// })
63+
@JoinColumn({
64+
name: 'car_buyer_id',
65+
})
6666
public carBuyer: CarBuyer;
6767

6868
@Column({ name: 'car_buyer_id', type: 'uuid', nullable: true })

spec/entities/CarBuyer.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import { Car } from './Car'
44

55
@Entity()
66
export class CarBuyer extends BaseEntity {
7-
@PrimaryGeneratedColumn('uuid')
7+
@PrimaryGeneratedColumn('uuid', {
8+
name: 'car_buyer_id',
9+
})
810
public carBuyerId: string;
911

1012
@Column()
1113
@IsDefined()
1214
public name: string;
1315

14-
@OneToMany(() => Car, (car) => car.carDealer)
16+
@OneToMany(() => Car, (car) => car.carDealer, {
17+
cascade: true,
18+
})
1519
public cars: Array<Car>;
1620
}

spec/entities/CarDealer.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import { Car } from './Car'
44

55
@Entity()
66
export class CarDealer extends BaseEntity {
7-
@PrimaryGeneratedColumn()
7+
@PrimaryGeneratedColumn({
8+
name: 'car_dealer_id',
9+
})
810
public id: string;
911

1012
@Column()
1113
@IsDefined()
1214
public name: string;
1315

14-
@OneToMany(() => Car, (car) => car.carDealer)
16+
@OneToMany(() => Car, (car) => car.carDealer, {
17+
cascade: true,
18+
})
1519
public cars: Array<Car>;
1620
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
module.exports = {
1+
/* eslint-disable no-unused-expressions */
2+
// eslint-disable-next-line import/no-extraneous-dependencies
3+
import 'reflect-metadata'
4+
import { DataSource } from 'typeorm'
5+
6+
export const dataSource = new DataSource({
27
type: 'postgres',
38
host: process.env.POSTGRES_HOST || 'localhost',
49
port: +(process.env.POSTGRES_PORT || 5432),
510
username: process.env.POSTGRES_USER || '',
6-
password: process.env.POSTGRES_PASSWORD || '',
11+
password: process.env.POSTGRES_PASSWORD || 'mysecretpassword',
712
database: process.env.POSTGRES_DATABASE || 'database_test',
813
entities: ['spec/entities/**/*.ts'],
914
migrations: ['spec/migrations/**/*.ts'],
1015
subscribers: ['spec/subscribers/**/*.ts'],
1116
synchronize: true,
1217
logging: false,
13-
cli: {
14-
migrationsDir: 'spec/migrations',
15-
entitiesDir: 'spec/entities',
16-
subscribersDir: 'spec/subscribers',
17-
},
18-
}
18+
})

0 commit comments

Comments
 (0)