Skip to content

Instantly share code, notes, and snippets.

@SamuelMarks
Last active November 23, 2017 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamuelMarks/533efddadc9514cdc22b1deed65e1c2b to your computer and use it in GitHub Desktop.
Save SamuelMarks/533efddadc9514cdc22b1deed65e1c2b to your computer and use it in GitHub Desktop.
TypeORM
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