Skip to content

Instantly share code, notes, and snippets.

@DaveMacNeil
Last active July 4, 2017 20:30
Show Gist options
  • Save DaveMacNeil/85dc23b7d9c9a3a97806c1f772152d1e to your computer and use it in GitHub Desktop.
Save DaveMacNeil/85dc23b7d9c9a3a97806c1f772152d1e to your computer and use it in GitHub Desktop.
FK example
import { Entity, Column, PrimaryColumn, PrimaryGeneratedColumn, OneToMany, ManyToMany, JoinTable } from 'typeorm';
import Location from './Location';
/**
* Customer model
*
* @class Customer
*/
@Entity()
class Customer {
@PrimaryColumn('nvarchar', { length: 25 })
CustomerID: string;
@Column('nvarchar', { length: 50, nullable: false })
CustomerName: string;
@Column('nvarchar', { length: 254 })
Email: string;
@OneToMany(type => Location, location => location.customer)
locations: Location[] | null;
}
export default Customer;
import { Entity, PrimaryColumn, PrimaryGeneratedColumn, Column, ManyToOne, ManyToMany, JoinTable } from 'typeorm';
import Customer from './Customer';
/**
* Location model
*
* @class Location
*/
@Entity() // Set timstamps to true, to enable createdAt and updatedAt fields
class Location {
@PrimaryColumn('nvarchar', { length: 25 })
LocationID: string;
@Column('nvarchar', { length: 255 })
LocationAddress: string;
@Column('nvarchar', { length: 25 })
CustomerID: string;
// One customer can have many locations, waiting on fix
@ManyToOne(type => Customer, customer => customer.locations)
customer: Customer;
}
export default Location;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment