This app is like the features of an common ORM, but only the findAll and create methods. The app uses decorations in the declarations of models .
David Nascimento
- ts-node-dev;
- dotenv;
- typescript;
- reflect-metadat;
- knex;
- mysql;
- The reflect api;
- Environment vars;
- Typescript decorations;
- How doing an ORM 😅.
With decoration
you can declarate the model like this:
@Table("users")
class User extends Repository{
@Column("id", true)
public id!: number
@Column("name")
public name!: string
@Column("email")
public email!: string
@Column("description")
public description!: string
public toJSON(): Object {
return {
id: this.id,
name: this.name,
email: this.email,
description: this.description
}
}
}
User
.findAll()
.then(console.log)
You can be view the full example into src/index.ts
.
- Config you environment;
- Git clone
git clone https://github.com/david32145/nodejs-reflect-metadata
; - Run
yarn install
ornpm install
for install dependencies; - Copy .env.example to .env and fill with your config. Below an example of
.env
;
DB_HOST=localhost
DB_PORT=3306
DB_NAME=reflect_matadata
DB_USER=root
DB_PASS=root
- Run
yarn migration:up
ornpm run migration:up
for up database migrations; - Run
yarn dev
ornpm run dev
- Edit
src/index.ts
and start the game.