Skip to content

Instantly share code, notes, and snippets.

@agreatfool
Last active February 28, 2018 05:32
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 agreatfool/0aaa839545e87205a129b41e66678bb2 to your computer and use it in GitHub Desktop.
Save agreatfool/0aaa839545e87205a129b41e66678bb2 to your computer and use it in GitHub Desktop.
import { createConnection } from 'typeorm';
import { TestEntity } from './entities/TestEntity';
async function main(): Promise<any> {
try {
const connection = await createConnection({
"type": "mysql",
"host": "x.x.x.x",
"port": 3306,
"username": "root",
"password": "123",
"database": "test_entity",
"logging": ["error", "schema", "query", "info", "log"],
"entities": [TestEntity]
});
console.log('DB Connected, isConnected:', connection.isConnected);
const repository = connection.getRepository(TestEntity);
let queryBuilder = repository.createQueryBuilder();
let startTime = Date.now();
await queryBuilder
.andWhere(`column_b = 1`)
.andWhere(`column_d = 1`)
.getMany();
let endTime = Date.now();
console.log('Ms consumed:', endTime - startTime);
} catch (error) {
console.log('Error:', error);
}
}
main().then(_ => _);
{
"name": "samples",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "tsc && node build/app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"debug": "3.1.0",
"fs-copy-file": "1.0.2",
"glob": "7.1.2",
"hashring": "3.2.0",
"mysql": "^2.15.0",
"mz": "2.7.0",
"typeorm": "0.1.13"
}
}
DROP TABLE IF EXISTS `test_entity`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test_entity` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`column_b` int(11) NOT NULL,
`column_c` varchar(60) NOT NULL,
`column_d` tinyint(4) NOT NULL DEFAULT '1',
`column_e` varchar(60) DEFAULT NULL,
`column_f` int(11) DEFAULT NULL,
`column_g` int(11) NOT NULL,
`column_h` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_index_column_b_c` (`column_b`,`column_c`),
KEY `ind_115f97b4b3cac9417c9b31806d` (`column_c`)
) ENGINE=InnoDB AUTO_INCREMENT=47475 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
import { Entity, Column, PrimaryGeneratedColumn, Index, BaseEntity } from 'typeorm';
@Entity('test_entity')
@Index('test_entity_index', ['columnB', 'columnC'], { unique: true })
export class TestEntity extends BaseEntity {
@PrimaryGeneratedColumn({ type: 'int', name: 'id' })
id: number;
@Column({ type: 'int', name: 'column_b' })
columnB: number;
@Index()
@Column({ type: 'varchar', length: 60, name: 'column_c' })
columnC: string;
@Column({ type: 'tinyint', name: 'column_d', default: 1 })
columnD: number;
@Column({ type: 'varchar', length: 60, name: 'column_e', nullable: true })
columnE: string;
@Column({ type: 'int', length: 11, name: 'column_f', nullable: true })
columnF: number;
@Column({ type: 'int', length: 11, name: 'column_g' })
columnG: number;
@Column({ type: 'int', length: 11, name: 'column_h' })
columnH: number;
}
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"sourceMap": false,
"rootDir": "src",
"outDir": "build",
"allowJs": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment