We use Makefile
to easily have command.
psql --host=postgres --username=me --dbname=testDb
First time:
make local
Regular:
make local-dev
After you start the app, you can run the following command to watch your app.
make log
If you want to use psql
, with password: 1234
.
make database
If you want to go into app container with shell.
make application
localhost:8000
- account: 1234@admin.com
- password: 1234
Notice that when using docker, need to change database host to postgres container. (in docker-compose.yml
)
# create simple app with typeorm
typeorm init --name server --database postgres
# create entity
typeorm entity:create -n User
// Photo
@Entity()
export class Photo {
@PrimaryGeneratedColumn()
id: number;
@Column()
url: string;
@ManyToOne(
type => User,
user => user.photos
)
user: User;
}
// User
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@OneToMany(
type => Photo,
photo => photo.user
)
photos: Photo[];
}
- create:
const photo1 = new Photo();
photo1.url = "me.jpg";
await connection.manager.save(photo1);
const photo2 = new Photo();
photo2.url = "me-and-bears.jpg";
await connection.manager.save(photo2);
const user = new User();
user.name = "John";
user.photos = [photo1, photo2];
await connection.manager.save(user);
///
const user = new User();
user.name = "Leo";
await connection.manager.save(user);
const photo1 = new Photo();
photo1.url = "me.jpg";
photo1.user = user;
await connection.manager.save(photo1);
const photo2 = new Photo();
photo2.url = "me-and-bears.jpg";
photo2.user = user;
await connection.manager.save(photo2);
- read
const userRepository = connection.getRepository(User);
const users = await userRepository.find({ relations: ["photos"] });
// or from inverse side
const photoRepository = connection.getRepository(Photo);
const photos = await photoRepository.find({ relations: ["user"] });
create file nodemon.json
{
"watch": ["src"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts"],
"exec": "ts-node ./src/index.ts"
}
in package.json
"scripts": {
"dev": "nodemon"
}