nuxt-typeorm
v0.0.8
Published
A Nuxt module to make it easy to use TypeORM in your project
Downloads
12
Readme
⚠️ Under Active Development ⚠️
Nuxt TypeORM
A Nuxt module to make it easy to use TypeORM in your project
Usage
Installation
- Add
nuxt-typeorm
dependency to your project
npm install nuxt-typeorm
- Add
nuxt-typeorm
to themodules
section ofnuxt.config.ts
export default defineNuxtConfig({
modules: ["nuxt-typeorm"],
});
- Add your TypeORM and TypeScript configuration to
nuxt.config.ts
typeorm: {
type: "sqlite",
database: "db.sqlite",
synchronize: true,
logging: false,
},
nitro: {
esbuild: {
options: {
tsconfigRaw: {
compilerOptions: {
emitDecoratorMetadata: true,
experimentalDecorators: true,
},
},
},
},
typescript: {
tsConfig: {
compilerOptions: {
emitDecoratorMetadata: true,
experimentalDecorators: true,
strictPropertyInitialization: false,
},
},
},
}
- Create an entity
// server/entities/user.entity.ts
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
@Entity("user")
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ type: "varchar" })
firstName: string;
@Column({ type: "varchar" })
lastName: string;
}
- Export the entities
// server/entities/entities.ts
import { User } from "./user.entity";
export const entities = [User];
- You're done!
nuxt-typeorm
provides agetRepository
helper function
// server/api/users.get.ts
import { getRepository } from "#typeorm";
export default defineEventHandler(async (event) => {
const userRepository = await getRepository(User);
const users = await userRepository.find();
return {
users,
};
});