'Typeorm connection to postgres
I'am trying to connect typeorm to postgresql on serverless framework but i'm getting error.
"message": "No repository for "Blog" was found. Looks like this entity is not registered in current "default" connection?" }
ormconfig
export class Database {
private static connection: Connection
public getConnection = async () => {
if (!Database.connection) {
Database.connection = await createConnection({
name: 'default',
type: 'postgres',
host: process.env.DB_HOST,
port: 5432,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
entities: [
'src/entities/*.ts',
],
migrations: [
"src/migrations/*.ts"
],
cli: {
entitiesDir: "src/entities",
migrationsDir: "src/migrations",
},
synchronize: true,
logging: false
})
}
}
}
blog entity
import {BaseEntity, Entity, PrimaryGeneratedColumn, Column} from "typeorm";
@Entity()
export class Blog extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ type: 'varchar', nullable: true })
public name: string;
@Column({ type: 'varchar', nullable: true })
public description: string;
@Column({ type: 'varchar', nullable: true })
public author: string;
@Column({ type: 'varchar', nullable: true })
public image: string;
@Column({ type: 'timestamp', nullable: true })
public created_at: Date;
}
lambda
export const main = middyfy(async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
const db = new Database();
db.getConnection();
try {
const blog = new Blog();
blog.name = "Timber";
blog.description = "Testing";
await blog.save();
return formatJSONResponse({
blog
})
}catch (e) {
return formatJSONResponse({
status: 500,
message: e
});
}
})
I'm new in this and just trying to connect it i created migration and model blog now i want to save data to it. whats i'm doing wrong can someone please help me )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
