Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for entity schema #70

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
23 changes: 0 additions & 23 deletions example-app/ormconfig.js

This file was deleted.

18 changes: 18 additions & 0 deletions example-app/ormconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import dotenv from 'dotenv'
import { DataSourceOptions } from 'typeorm'
import { Car, Seller, User } from './src/entities/index.js'
import { BrandSchema } from './src/schemas/brand.schema.js'

dotenv.config()

export const dataSourceOptions: DataSourceOptions = {
type: 'postgres',
host: process.env.POSTGRES_HOST || 'localhost',
port: +(process.env.POSTGRES_PORT || 5432),
username: process.env.POSTGRES_USER || 'postgres',
password: process.env.POSTGRES_PASSWORD || '',
database: process.env.POSTGRES_DATABASE || 'database_test',
entities: [User, Car, Seller, BrandSchema],
synchronize: true,
logging: true,
}
14 changes: 8 additions & 6 deletions example-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"type": "module",
"scripts": {
"build": "tsc",
"start": "node build/src/index.js",
Expand All @@ -16,18 +17,19 @@
"concurrently": "^5.2.0",
"cypress": "^4.11.0",
"nodemon": "^2.0.4",
"ts-node": "3.3.0",
"typescript": "3.9.7"
"ts-node": "10.9.2",
"typescript": "5.5.3"
},
"dependencies": {
"adminjs": "^3.3.1",
"@adminjs/express": "^3.0.0",
"@adminjs/express": "^6.1.0",
"@adminjs/typeorm": "^1.0.1",
"adminjs": "^7.8.7",
"dotenv": "^16.4.5",
"express": "^4.17.1",
"express-formidable": "^1.2.0",
"express-session": "^1.17.1",
"pg": "^8.3.0",
"pg": "^8.12.0",
"reflect-metadata": "^0.1.10",
"typeorm": "0.2.28"
"typeorm": "0.3.20"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, RelationId, ManyToOne } from 'typeorm'
import { User } from './User'
import { Seller } from './Seller'
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, RelationId, ManyToOne, Relation } from 'typeorm'
import { User } from './User.js'
import { Seller } from './Seller.js'

@Entity()
export class Car extends BaseEntity {
Expand All @@ -17,10 +17,10 @@ export class Car extends BaseEntity {
meta: any

@ManyToOne((type) => User, (user) => user.cars)
owner: User
owner: Relation<User>

@ManyToOne((type) => Seller, (seller) => seller.cars)
seller: User
seller: Relation<User>

// in order be able to fetch resources in adminjs - we have to have id available
@RelationId((car: Car) => car.owner)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm'
import { Car } from './Car'

export enum UserRoles {
DESIGNER = 'designer',
CLIENT = 'client'
}
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, OneToMany, Relation } from 'typeorm'
import { Car } from './Car.js'

@Entity()
export class Seller extends BaseEntity {
Expand All @@ -15,5 +10,5 @@ export class Seller extends BaseEntity {
name: string

@OneToMany((type) => Car, (car) => car.seller)
cars: Array<Car>
cars: Array<Relation<Car>>
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm'
import { Car } from './Car'
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, OneToMany, Relation } from 'typeorm'
import { Car } from './Car.js'

export enum UserRoles {
enum UserRoles {
DESIGNER = 'designer',
CLIENT = 'client'
}
Expand All @@ -27,5 +27,5 @@ export class User extends BaseEntity {
role: UserRoles

@OneToMany((type) => Car, (car) => car.owner)
cars: Array<Car>
cars: Array<Relation<Car>>
}
5 changes: 5 additions & 0 deletions example-app/src/entities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Car } from './Car.js'
import { Seller } from './Seller.js'
import { User } from './User.js'

export { Car, Seller, User }
27 changes: 16 additions & 11 deletions example-app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import 'reflect-metadata'
import { createConnection } from 'typeorm'
import express from 'express'
import AdminJS from 'adminjs'
import { buildRouter } from '@adminjs/express'
import * as TypeormAdapter from '@adminjs/typeorm'
import { User } from './entity/User'
import { Car } from './entity/Car'
import { Seller } from './entity/Seller'
import { Database, Resource } from '@adminjs/typeorm'
import AdminJS from 'adminjs'
import express from 'express'
import 'reflect-metadata'
import { DataSource } from 'typeorm'
import { dataSourceOptions } from '../ormconfig.js'
import { Car } from './entities/Car.js'
import { Seller } from './entities/Seller.js'
import { User } from './entities/User.js'
import { BrandSchema } from './schemas/brand.schema.js'

AdminJS.registerAdapter(TypeormAdapter)
AdminJS.registerAdapter({ Database, Resource })

const PORT = 3000

const run = async () => {
await createConnection()
const dataSource = await new DataSource(dataSourceOptions).initialize()
const app = express()
const admin = new AdminJS({
resources: [{
Expand All @@ -37,7 +39,10 @@ const run = async () => {
},
},
},
}, Seller],
}, Seller, {
schema: BrandSchema,
dataSource,
}],
})
const router = buildRouter(admin)

Expand Down
5 changes: 5 additions & 0 deletions example-app/src/interfaces/brand.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Brand {
id: number;
name: string;
imgUrl: string;
}
20 changes: 20 additions & 0 deletions example-app/src/schemas/brand.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { EntitySchema } from 'typeorm'
import { Brand } from '../interfaces/brand.interface.js'

export const BrandSchema = new EntitySchema<Brand>({
name: 'Brand',
tableName: 'brand',
columns: {
id: {
primary: true,
generated: 'uuid',
type: 'uuid',
},
name: {
type: 'varchar',
},
imgUrl: {
type: 'varchar',
},
},
})
8 changes: 5 additions & 3 deletions example-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
"compilerOptions": {
"baseUrl": ".",
"outDir": "./build",
"target": "es6",
"target": "esnext",
"esModuleInterop": true,
"jsx": "react",
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"moduleResolution": "node",
"module": "commonjs",
"moduleResolution": "nodenext",
"module": "nodenext",
"types": ["cypress"],
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"sourceMap": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"paths": {
"react": ["node_modules/@types/react"],
"adminjs": ["node_modules/adminjs"]
Expand Down
Loading