NestJS Mongodb Compoud Index - mongodb

I am currently writing mongodb schema in nest js.
My schema is below
{
_id: {
filed1: string,
filed2: string,
field3: string,
field4: {
filed5: int
}
}
filed1: ~
...
...
Is there a way to create nestJs schema for the collection?
Is there a way to set the index for the _id field?
I've tried the following, but it didn't work.
import { DateTimeScalar } from '#/common/scalars/dateTime.scalar';
import { Field, Float, ID, Int, ObjectType } from '#nestjs/graphql';
import { Prop, raw, Schema, SchemaFactory } from '#nestjs/mongoose';
import { Document } from 'mongoose';
export type Document = MongooseClass & Document;
#ObjectType()
export class filed4 {
#Field(() => Int)
filed5: number;
}
#ObjectType()
export class compundId {
#Field(() => String)
filed1: string;
#Field(() => String)
filed2: string;
#Field(() => String)
filed3: string;
#Field(() => field4)
field4: field5;
}
#ObjectType()
#Schema({ collection: 'collection' })
export class MongooseClass {
#Field(() => ID, { nullable: false })
#Prop(
raw({
filed1: { type: String },
filed2: { type: String },
filed3: { type: string },
}),
)
_id: compundId;
filed ...
}
const MongooseSchema = SchemaFactory.createForClass(MongooseClass);
MongooseSchema.index({ _id: { filed1: 1, filed5: 1 } });
MongooseSchema.index({ filed2: 1, filed3: 1 });
MongooseSchema.index({ filed5: 1 });
export default ExcuteRateInfoMongooseSchema;
After checking the documentation, I couldn't find an answer to that question. please help me

Related

TypeORM: when do a findOneAndUpdate with option insert:true on MongoDB repository, the createdAt and updatedAt are never inserted, how to fix it?

I noticed that when I do a findOneAndUpdate with option insert:true on MongoDB repository which auto createAt and updateAt are enabled, the createdAt and updatedAt are never inserted/updated, is that normal? how to fix it?
do a findOne, if not found then create it else update the data. all that with the less DB requests sent
here an code example:
import { ObjectID } from 'mongodb';
import { container, instanceCachingFactory } from 'tsyringe';
import {
Column,
Entity,
EntityRepository,
getCustomRepository,
MongoRepository,
} from 'typeorm';
import { CreateDateColumn, ObjectIdColumn, UpdateDateColumn } from 'typeorm';
export abstract class TypeOrmAbstractEntity {
#ObjectIdColumn() _id: ObjectID;
#CreateDateColumn() createdAt: Date;
#UpdateDateColumn() updatedAt: Date;
get id() {
return this._id.toHexString();
}
}
#Entity('subscriptions')
export class Subscription extends TypeOrmAbstractEntity {
#Column({ nullable: false }) subscriptionID: string;
#Column({ nullable: false }) customerID: string;
}
#EntityRepository(Subscription)
export class SubscriptionRepository extends MongoRepository<Subscription> {}
container.register<SubscriptionRepository>(SubscriptionRepository, {
useFactory: instanceCachingFactory(() =>
getCustomRepository(SubscriptionRepository)
),
});
....
const stripeSubscriptionRepository = container.resolve(SubscriptionRepository);
subscriptionRepository.findOneAndUpdate(
{ customerID },
{
$set: {
subscriptionID: subscription.id,
customerID: customerID,
},
},
{ upsert: true }
);

#nestjs/mongoose, virtual populate with 2 databases

I'm trying to populate userId field which exists in database 1 from user document which exists in database 2.
I've already defined connectionName parameter in MongooseModule.ForRootAsync() I can't find out where the problem is. Also it works if I request info from db1 and db2 individually.
Actually on console.log(commentPopulated) userId field is just the objectId without populated fields from User schema and also sometimes with certain #Prop() and .populate() parameters app throw me this error:
MissingSchemaError: Schema hasn't been registered for model "User".
Using #nestjs/mongoose decorators how do I achieve this?
app.module.ts
MongooseModule.forRootAsync({
connectionName: 'db1',
useFactory: () => ({
uri: process.env.DB1,
connectionFactory: (connection: { plugin: (arg0: unknown) => void }) => {
connection.plugin(_)
connection.plugin(autoPopulate)
return connection
},
}),
}),
MongooseModule.forRootAsync({
connectionName: 'db2',
useFactory: () => ({
uri: process.env.DB2,
connectionFactory: (connection: { plugin: (arg0: unknown) => void }) => {
connection.plugin(_)
connection.plugin(autoPopulate)
return connection
},
}),
}),
comment.module.ts
const commentModule: DynamicModule = MongooseModule.forFeatureAsync([
{
name: Comment.name,
useFactory: () => {
return CommentSchema
}
}
], 'db1')
#Module({
imports: [commentModule],
providers: [CommentService, CommentResolver]
})
export class CommentModule { }
comment.schema.ts
#Schema({ toJSON: { virtuals: true, getters: true }, toObject: { virtuals: true, getters: true } })
#ObjectType()
export class Comment extends Document {
#Prop()
#Field(() => String)
readonly _id: MongooseSchema.Types.ObjectId
#Prop({ required: true })
#Field(() => String)
text: string
//TODO: Reference User document from DB2, Comment document exists in DB1
#Prop({ type: MongooseSchema.Types.ObjectId, ref: User.name})
#Field(() => User, { nullable: true })
userId: MongooseSchema.Types.ObjectId
#Prop({ type: String, enum: UserType, required: true, default: UserType.Regular })
#Field(() => UserType, { defaultValue: UserType.Regular })
userType: UserType
#Prop({ default: Date.now })
#Field(() => Date)
created: Date
}
export const CommentSchema = SchemaFactory.createForClass(Comment)
user.module.ts
const userModule: DynamicModule = MongooseModule.forFeatureAsync([
{
name: User.name,
useFactory: () => {
return UserSchema
},
},
], 'db2')
#Module({
imports: [userModule],
providers: [UserService, UserResolver]
})
export class UserModule { }
user.schema.ts
#Schema()
#ObjectType()
export class User extends Document {
#Prop()
#Field(() => String)
readonly _id: MongooseSchema.Types.ObjectId
#Prop({ required: true })
#Field(() => String)
firstName: string
#Prop({ required: true })
#Field(() => String)
lastName: string
#Prop({ required: true })
#Field(() => String)
email: string
}
export const UserSchema = SchemaFactory.createForClass(User)
comment.service.ts
#Injectable()
export class CommentService {
constructor(#InjectModel(Comment.name, 'db1') private readonly model: Model<Comment>) { }
async getComments() {
const commentPopulated = await this.model.findById('63b8608c7d4f880cba028bfe').populate('userId')
console.log(commentPopulated)
return commentPopulated
}
}
I have tried randomly playing with parameters on #Prop() decorator with no success, I think there is the problem, also played with .populate() function parameters.

How to accept multiple objects into an array NestJS

I have a feedbackQuestion schema which takes (title: string, subtitle: string, types: enum, values: enum)
import { Prop, Schema, SchemaFactory } from '#nestjs/mongoose'
import { Document } from 'mongoose'
import { Types, Value } from 'src/common/enum/types.enum'
export type FeedbackQuestionDocument = FeedbackQuestion & Document
#Schema({ timestamps: true, id: true })
export class FeedbackQuestion {
#Prop()
title: string
#Prop()
subtitle: string
#Prop()
types: Types
#Prop()
value: Value
}
export const FeedbackQuestionSchema =
SchemaFactory.createForClass(FeedbackQuestion)
The feedbackQuestion schema serves as a subdocument in my feedback schema for the key question
import { Prop, Schema, SchemaFactory } from '#nestjs/mongoose'
import mongoose, { Document, ObjectId } from 'mongoose'
import { User } from './user.schema'
import { Transform, Type } from 'class-transformer'
import { FeedbackQuestion } from './feedback-question.schema'
import { distributionChannels } from 'src/common/enum/distributionChannels.enum'
export type FeedbackDocument = Feedback & Document
#Schema({ timestamps: true, id: true })
export class Feedback {
#Transform(({ value }) => value.toString())
_id: ObjectId
#Prop()
label: string
#Prop({ default: false })
status: boolean
#Prop()
question: [FeedbackQuestion]
#Prop()
comment: string
#Prop()
thankYouMessage: string
#Prop()
distributionChannels: distributionChannels
#Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User' })
#Type(() => User)
user: User
}
export const FeedbackSchema = SchemaFactory.createForClass(Feedback)
when creating my create-feedbackDto, I assigned question to be an array of type feedbackQuestion
import { Type } from 'class-transformer'
import { FeedbackQuestion } from '../../schemas/feedback-question.schema'
import { IsArray, IsEnum, IsNotEmpty, ValidateNested } from 'class-validator'
import { Types, Value } from '../enum/types.enum'
import { distributionChannels } from '../enum/distributionChannels.enum'
export class CreateFeedbackDto {
#IsNotEmpty()
label: string
status: boolean
#IsArray()
#ValidateNested({ each: true })
#Type(() => FeedbackQuestion)
question: FeedbackQuestion[]
#IsNotEmpty()
comment: string
#IsNotEmpty()
thankYouMessage: string
#IsNotEmpty()
title: string
#IsNotEmpty()
subtitle: string
#IsEnum(Types)
#IsNotEmpty()
types: Types
#IsEnum(Value)
#IsNotEmpty()
value: Value
#IsEnum(distributionChannels)
#IsNotEmpty()
distributionChannels: distributionChannels
}
In my feedback services, I want to work on questions such that I can pass in multiple objects of feedbackQuestion into the question array when creating a feedback. Please How can I do that?
The current code only takes one FeedbackQuestion object in the array
import { Injectable } from '#nestjs/common'
import { InjectModel } from '#nestjs/mongoose'
import { Model } from 'mongoose'
import { Feedback, FeedbackDocument } from '../../schemas/feedback.schema'
import {
FeedbackQuestion,
FeedbackQuestionDocument,
} from '../../schemas/feedback-question.schema'
import { IServiceResponse } from '../../common/interfaces/service.interface'
import { CreateFeedbackDto } from 'src/common/dto/create-feedback.dto'
#Inject#5406able()
export class FeedbackService {
constructor(
#Inject#5406Model(Feedback.name)
private feedbackDocumentModel: Model<FeedbackDocument>,
#Inject#5406Model(FeedbackQuestion.name)
private feedbackQuestionDocumentModel: Model<FeedbackQuestionDocument>,
) {}
async createFeedback(payload: CreateFeedbackDto): Promise<IServiceResponse> {
const feedbackQuestion = await this.feedbackQuestionDocumentModel.create({
title: payload.title,
subtitle: payload.subtitle,
type: payload.types,
value: payload.value,
})
const feedback = await this.feedbackDocumentModel.create({
label: payload.label,
status: payload.status,
question: [feedbackQuestion],
comment: payload.comment,
thankYouMesage: payload.thankYouMessage,
distributionChannels: payload.distributionChannels,
})
// feedback.question.push(feedbackQuestion)
await feedback.save()
return {
data: {
user: feedback,
},
}
}
}
This is the current response I get
"label": "Yearly Feedback",
"status": false,
"question": [
{
"title": "Yearly Feedback",
"subtitle": "Rating the Yearly performance of the organization",
"value": 1,
"_id": "627fa9b915d31bbbc0fe6908",
"createdAt": "2022-05-14T13:08:09.180Z",
"updatedAt": "2022-05-14T13:08:09.180Z",
"__v": 0
}
],
Thanks

How can I use the point type coordinate information of postgis in graphql using mutations?

I created a mutation to insert new data into the postgresql called location. The column coordinate must receive and store data, for example, ST_GeomFromGeoJSON ('{ "type": "Point", "coordinates": [-48.23456,20.12345]}').
However, graphql is not working, so I don't know where to modify it. I think it's because the scalar called GeoJSONPoint that I made is not working properly. Could you tell me how to create a scalar if graphql puts the data above?
GeoJSONPoint Scalar
import { GraphQLScalarType, Kind } from 'graphql';
export const GeoJSONPoint = new GraphQLScalarType({
name: 'GeoJSONPoint',
description: 'Geometry scalar type',
parseValue(value) {
return value;
},
serialize(value) {
return value;
},
parseLiteral(ast) {
if (ast.kind === Kind.OBJECT) {
console.log(ast);
return new Object(ast);
}
return null;
}
});
location.entity
import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn
} from 'typeorm';
import { Location_Group } from './location_group.entity';
import { Geometry } from 'geojson';
import { Field, ID, Int, ObjectType } from '#nestjs/graphql';
import { GeoJSONPoint } from 'src/scalar/geoJSONPoint.scalar';
#ObjectType()
#Entity('location')
export class Location {
#Field(() => ID)
#PrimaryGeneratedColumn('increment')
id: number;
#Field(() => String)
#Column({ type: 'varchar' })
name: string;
#Field(() => GeoJSONPoint)
#Column({
type: 'geometry',
nullable: true,
spatialFeatureType: 'Point',
srid: 4326
})
coordinate: Geometry;
#Field(() => Int)
#Column({ type: 'int' })
order_number: number;
#Field()
#CreateDateColumn({ type: 'timestamptz' })
created_at: Date;
#Field(() => Location_Group)
#ManyToOne(
() => Location_Group,
(location_group) => location_group.location
)
#JoinColumn([{ name: 'location_group_id', referencedColumnName: 'id' }])
location_group: Location_Group;
}
resolver
#Mutation(() => Location)
async createLocation(
#Args('data') data: LocationDataInput
): Promise<Location> {
console.log(data);
return await this.locationService.setLocation(data);
}
I solved this problem. First of all, we divided the values entered by parseLiteral in scalar into
{type: '', coordinates: []}
and removed the foreign key column.

typeorm geometry type Undefined type error

everyone. I am creating a project that uses geometry data using postgresql postgis this time. So I want to declare geometry in the column and use it, but there's an error. Could you tell me why there is an error?
Multiple official documents were checked, but no method was found.
Commenting the coordinate column will create the code normally.
import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn
} from 'typeorm';
import { Location_Group } from './location_group.entity';
import { Geometry } from 'geojson';
import { Field, ID, Int, ObjectType } from '#nestjs/graphql';
#ObjectType()
#Entity()
export class Location {
#Field(() => ID)
#PrimaryGeneratedColumn('increment')
id: number;
#Field(() => String)
#Column({ type: 'varchar' })
name: string;
#Field()
#Column({
type: 'geometry',
nullable: true,
spatialFeatureType: 'Point',
srid: 4326
})
coordinate: Geometry;
#Field(() => Int)
#Column({ type: 'int' })
order_number: number;
#Field()
#CreateDateColumn({ type: 'timestamptz' })
created_at: Date;
#Field(() => Location_Group)
#ManyToOne(
() => Location_Group,
(location_group) => location_group.location
)
#JoinColumn([{ name: 'location_group_id', referencedColumnName: 'id' }])
location_group: Location_Group;
}
There is someone who wants me to share the scalar I made, so I write it here. I hope this code helps you.
import { GraphQLScalarType } from 'graphql';
export const GeoJSONPoint = new GraphQLScalarType({
name: 'GeoJSONPoint',
description: 'Geometry scalar type',
parseValue(value) {
return value;
},
serialize(value) {
return value;
},
parseLiteral(ast) {
const geometryData = {
type: '',
coordinates: []
};
for (const i in ast['fields']) {
if (ast['fields'][i]['name']['value'] == 'type') {
if (ast['fields'][i]['value']['value'] != 'point') {
return null;
}
geometryData.type = ast['fields'][i]['value']['value'];
}
if (ast['fields'][i]['name']['value'] == 'coordinate') {
for (const j in ast['fields'][i]['value']['values']) {
geometryData.coordinates.push(
parseFloat(
ast['fields'][i]['value']['values'][j]['value']
)
);
}
}
}
return geometryData;
}
});