Last active
November 23, 2017 08:09
-
-
Save SamuelMarks/533efddadc9514cdc22b1deed65e1c2b to your computer and use it in GitHub Desktop.
TypeORM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'reflect-metadata'; | |
import { createConnection } from 'typeorm'; | |
@Entity('user_tbl') | |
export class User { | |
@PrimaryColumn({ type: 'varchar' }) | |
public username: string; | |
@Column({ type: 'varchar', nullable: false, select: false }) | |
public password: string; | |
} | |
createConnection({ | |
type: "mysql", | |
host: "localhost", | |
port: 3306, | |
username: "root", | |
password: "admin", | |
database: "test", | |
entities: [ User ], | |
synchronize: true, | |
logging: false | |
}).then(connection => { | |
const user0 = new User(); | |
user0.username = 'foo'; | |
user0.password = Math.random().toString(); | |
connection.manager | |
.getRepository(User) | |
.save(user0) | |
.then(() => { | |
const user1 = new User(); | |
user1.username = 'foo'; | |
user1.password = Math.random().toString(); | |
connection.manager | |
.getRepository(User) | |
.save(user1) | |
.then(() => console.info('inserted:', user1, 'over', user0, ';')) | |
.catch(console.error.bind(console)); | |
}) | |
.catch(console.error.bind(console)); | |
}).catch(error => console.log(error)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment