😺Tạo bảng và thêm dữ liệu vào mysql giống migate của Laravel (ok)
PreviousStudy MERN CRUD applications Todo App Back End , Front EndNextNodejs typescript mysql rest api Full (ok)
Last updated
Last updated
package.json
{
"name": "mysql-project",
"version": "0.0.1",
"description": "Awesome project developed with TypeORM.",
"devDependencies": {
"ts-node": "3.3.0",
"@types/node": "^8.0.29",
"typescript": "2.5.2"
},
"dependencies": {
"class-transformer": "^0.2.0",
"mysql": "^2.14.1",
"reflect-metadata": "^0.1.10",
"typeorm": "0.2.12"
},
"scripts": {
"start": "ts-node src/index.ts",
"build": "npx tsc",
"dev": "nodemon app.ts",
"product": "npx tsc && node build/app.js"
}
}
tsconfig.json
{
"compilerOptions": {
"lib": [
"es5",
"es6"
],
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./build",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
}
}
src\index.ts
import "reflect-metadata";
import { createConnection } from "typeorm";
import { Edition } from "./entity/Edition";
import { Section } from "./entity/Section";
import { serialize } from "class-transformer";
createConnection({
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "",
database: "test",
entities: [
__dirname + "/entity/*.ts"
],
synchronize: true,
logging: false
}).then(async connection => {
let edition1 = new Edition();
edition1.name = "January February 2019";
await connection.manager.save(edition1);
let edition2 = new Edition();
edition2.name = "November December 2018";
await connection.manager.save(edition2);
let section = new Section();
section.name = "Application Development";
section.editions = [edition1, edition2];
await connection.manager.save(section);
let section2 = new Section();
section2.name = "DBA";
section2.editions = [edition1];
await connection.manager.save(section2);
let section3 = new Section();
section3.name = "Database Developer";
section3.editions = [edition1];
await connection.manager.save(section3);
const loadedSection = await connection
.getRepository(Section)
.findOne(1, { relations: ["editions"] });
console.log(serialize(loadedSection));
}).catch(error => console.log(error));
src\entity\User.ts
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
firstName: string;
@Column()
lastName: string;
@Column()
age: number;
}
src\entity\Section.ts
import { Entity, PrimaryGeneratedColumn, Column, ManyToMany } from "typeorm";
import { Edition } from "./Edition";
@Entity()
export class Section {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@ManyToMany(type => Edition, edition => edition.sections)
editions: Edition[];
}
src\entity\Edition.ts
import { Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable } from "typeorm";
import { Section } from "./Section";
@Entity()
export class Edition {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@ManyToMany(type => Section, section => section.editions)
@JoinTable()
sections: Section[];
}
src\entity\CatalogTimestamp.ts
import { Entity, Column, PrimaryGeneratedColumn, OneToOne, JoinColumn } from "typeorm";
import { Catalog } from "./Catalog";
@Entity()
export class CatalogTimestamp {
@PrimaryGeneratedColumn()
id: number;
@Column()
firstAdded: string;
@Column()
firstUpdated: string;
@Column()
lastUpdated: string;
@OneToOne(type => Catalog, catalog => catalog.timestamp)
@JoinColumn()
catalog: Catalog;
}
src\entity\CatalogEntry.ts
import {Entity, Column, PrimaryGeneratedColumn, ManyToOne} from "typeorm";
import {CatalogEdition} from "./CatalogEdition";
@Entity()
export class CatalogEntry {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
author: string;
@Column()
isPublished: boolean;
@ManyToOne(type => CatalogEdition, catalogEdition => catalogEdition.catalogEntries)
catalogEdition: CatalogEdition;
}
src\entity\CatalogEdition.ts
import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from "typeorm";
import { CatalogEntry } from "./CatalogEntry";
@Entity()
export class CatalogEdition {
@PrimaryGeneratedColumn()
id: number;
@Column()
edition: string;
@Column()
isPublished: boolean;
@OneToMany(type => CatalogEntry, catalogEntry => catalogEntry.catalogEdition)
catalogEntries: CatalogEntry[];
}
src\entity\Catalog.ts
import { Entity, Column, PrimaryGeneratedColumn, OneToOne, JoinColumn } from "typeorm";
import { CatalogTimestamp } from "./CatalogTimestamp";
@Entity()
export class Catalog {
@PrimaryGeneratedColumn()
id: number;
@Column()
journal: string;
@Column()
publisher: string;
@Column({
length: 25
})
edition: string;
@Column("text")
title: string;
@Column()
author: string;
@Column()
isPublished: boolean;
@OneToOne(type => CatalogTimestamp, timestamp => timestamp.catalog, {
cascade: true,
})
timestamp: CatalogTimestamp;
}