Library to mock nodejs object base on flow typing - node.js

I'm looking for a JS library to mock object base of my type declaration.
For example this is my user type:
export type UserType = {
id: string,
userId: string,
platform: ?string,
gender: ?string,
timezone: ?number,
picture_url: ?string,
first_name: string,
last_name: string,
locale: ?string,
created_on: ?Date
};
I want to be able to do that: let userMock = mock(UserType)
do you know something that allow to do that?

For typescript, you can use Factory to generate factories for test data for Typescript based on the interface provided.
For a normal js, you can utilize Faker which provides a good tool to generate random test data.

Related

How to define a constant in the swagger definition of a NestJS Class?

I have a bunch of Exceptions that use the same interface, so it is easier for the frontend to distinct between them from the same endpoint.
I want to be able to document in swagger that MyWeirdExpection has a property type that has the value my-weird-exception (it comes from an string ENUM if that is relevant).
Is this possible to do?
Something like:
enum MyEnum {
MyWeirdExpection: 'my-weird-exception',
RegularExpection: 'my-regular-expection'
... more
}
#ApiProperty({
description: 'Frontend can react on this property',
type: String, // <-----------WHAT HERE?
})
type: MyEnum.MyWeirdExpection;
Again, my goal is that in the swagger definition, says that MyWeirdExpection.type is the constant 'my-weird-exception'
The following will provide a type of MyEnum.MyWeirdExpection with the value populated as 'my-weird-exception':
#ApiProperty({
description: "Search by asset type",
required: false,
default: MyEnum.MyWeirdExpection,
type: MyEnum.MyWeirdExpection
})
assetType4: string | undefined;

Type sharing between frontend and backend without bundling the ORM

In a fullstack typescript project i wish to share the interface/types that i use on the backend project in the frontend project to avoid code duplication and keep consistency.
From search i found that this can be done by having a third "shared" project with these types and then using typescript's referenced projects to include it.
So i would have a Frontend project, Backend project and a Shared project that would be referenced by the first two.
Although my types extend the mongoose's Document type, witch would cause the need for mongoose to be bundled in the frontend project in order to use the same types in both the frontend and backend.
What is the proper way to share these interfaces/types without bundling the extra backend packages to the frontend? (and keeping the _id property that comes from the Document extension as well while we are at it).
RoadBook.ts (IRoadBook is the interface/type to be shared and mongoose schema,model declaration)
import mongoose, {Schema,Model,Document} from 'mongoose';
export interface IRoadBook extends Document {
Name : string;
Description : string;
}
const RoadBookSchema = new Schema({
Name : {type : String, required : true, unique: true},
Description : {type : String }
});
export default mongoose.model<IRoadBook>('RoadBook',RoadBookSchema);
Might be important to reference that the backend is a simple typescript/express/mongoose app and the frontend a react webapp.
Thank you in advance.
What you could do is define the IRoadBook interface in your ORM which doesn't extend Document except for the members that you need in both the front end and the back end. Then, in the back end, define the schema.
ORM
export interface IRoadBook {
_id: string; // or ObjectID or whatever type mongoose.Document['_id'] is
Name: string;
Description: string;
}
Back end
import mongoose, { Schema, Model, Document } from 'mongoose';
import { IRoadBook } from 'my-orm';
const RoadBookSchema = new Schema({
Name: { type: String, required: true, unique: true },
Description: { type: String }
});
export default mongoose.model<IRoadBook & Document>('RoadBook', RoadBookSchema);

How would i make the mongoose Schema dynamically according to the other fields value in node.js?

i want to define the schema dynamically according to the condition using mongoose
Schema
new mongoose.Schema({
type: String, //BASIC OR ADVANCE
// only for type = BASIC
name: String,
age: Number
/*
want these fields too but only if type = ADVANCE
email: String,
password: Number
PhoneNumber: String
*/
});
how would i achieve this kind of schema using mongoose.
it depends on what your approach to your database is. you can simply create two types with identifiers as Advance and Basic and use middleware to maintain the flow. Now to answer your question:
something like:
new mongoose.Schema({
type: String, //BASIC OR ADVANCE
// only for type = BASIC
name: String,
age: Number,
advance: []
And now you can check if advance is empty or not. Seriously it all depends on your approach, how you deal with the problem. Once a Schema is declared you can without the advance field, you can still save data like:
const MyModel = mongoose.model('Test', new Schema({ name: String }));
const doc = new MyModel();
doc.advance = {
email: "test#test.com",
password: 1234,
PhoneNumber: 1234
}
doc.save();
But with this structure if you want to know the Schema, you'll think that in your file it is only name and age and later when you start exploring, you'll find out that you are doing something like this and using a proper structure.
Think of moongoose documents as JavaScript Objects, there is a reason it is known as non-Structured data. Hope this explanation helps.

how to dynamically query in mongoose?

so I'm new to Programming and Mongoose, just learning the basics.
and now I get the task to make a dynamic query with criteria such as
query => gets all the data I input into the query in the parameter
example:
query: [ios, android] => i get all ios data, and android
query_else => gets all the data I input other than the parameter in the query
example:
query_else: [ios, android] => I got all the data OTHER ios and android
if I try to use .find, I can only get 1 specific data I'm looking for, but it can't if I enter 2 queries in it the results are []
maybe it's not the answer I want to look for, but how to think about solving this case, because of my lack of knowledge about mongoose and my lack of knowledge about coding that makes me deadlock thinking
Thank you in advance
account log schema in activity-log collection:
const actLogSchema = new Schema(
{
account_id: {
type: Schema.Types.ObjectId,
ref: 'account'
},
date_created: { type: String, default: Date.now() },
ip: String,
location: String,
device: String,
type: String,
label: String,
action: String,
description: String
},
{ versionKey: false }
);
i assume query is part of {date_created, ip, location, device, type, action, label, description}
and query_else is equal to query but the different it is values.. like the example above

node.js+mongoose - Is single rest endpoint enough to update a document with nested arrays?

I'm developing a REST api using node.js + mongoose. I have a model which has few nested arrays within it. I have a put endpoint to update the document. However to add an object into the sub-array, do i need a separate endpoint or is there a way to use the same put endpoint? `
companyName: String,
city: String,
pincode: Number,
managers: [{
name: String,
dob: Date,
gender: String,
highestEducation: String,
email: String,
phoneNumbers: [{phoneNumber: Number}],
}],`
I have an endpoint ../api/customer for updating the document. It replaces the existing document with the json that im supplying. So, if i want to add a manager (not replace the existing manager), do i need a seperate endpoint just for this? What is the optimized solution?

Resources