MongoDB Starter
import { MongooseModule } from '@nestjs/mongoose';
import { config } from 'dotenv';
config();
...
@Module({
imports: [MongooseModule.forRoot(process.env.MONGO_URL!), BoardsModule],
...
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { IsString, IsNumber, IsOptional } from 'class-validator';
import { Document } from 'mongoose';
export type StockDocument = Stock & Document;
@Schema({ timestamps: true })
export class Stock {
@Prop({ required: true })
@IsString()
stockName: string;
@Prop()
@IsNumber()
yesterday_value: number;
@Prop()
@IsNumber()
current_value: number;
@Prop()
@IsNumber()
fluctuation_rate: number;
@Prop()
@IsNumber()
profit: number;
}
export const CreateStockSchema = SchemaFactory.createForClass(Stock);
import { MongooseModule } from '@nestjs/mongoose';
import { CreateStockSchema, Stock } from '../schema/boards.schema';
export const StockSchema = MongooseModule.forFeature([
{ name: Stock.name, schema: CreateStockSchema },
]);
import { StockSchema } from './model/boards.model';
...
@Module({
imports: [StockSchema],
...
...
import { Model } from 'mongoose';
import { StockDocument, Stock } from './schema/boards.schema';
@Injectable()
export class BoardsService {
constructor(
@InjectModel(Stock.name) private stockModel: Model<StockDocument>,
) {}
...