Application architecture using my own class - javascript

This is the first time I would like to implement MongoDB as a database within one of my applications and I’m quite confused...
My application has its own User class which is represented at the end of this post(UML class diagram).
Since my User object has its own properties and methods I feel that creating a schema usually used with mongoose would mean duplicating code.
Am I really well understanding how MongoDB works?
Is it not possible to save my User object inside my MongoDB database without having to use a schema?
If I want to follow the MVC pattern how could I implement the MongoDB model with the class represented on my diagram?
Thank you for your help!

Related

Is there a way to import prewritten JSON data into MongoDB when a new user is created but also have it tied to the new user?

I'm building a studying tool that essentially consists of multiple flashcards (in the thousands). I would like to know if there is a unique way or method where the prewritten flashcards can be duplicated and tied to every user who signs up? Or perhaps having the data prewritten inside a JSON file and then upon user creation having it imported into MongoDB with a userID field that the data becomes unique to every individual user. The reason for this is because the flashcards will be using a spaced repetition algorithm and as a result I can't have one users results be carried over globally for every user. It would need to be unique to that user.
Thanks in advance for any help!
So far I've built the flashcard model and tied it to the algorithm however it updates globally across all users. Im havign a bit of trouble with breaking down some of the logic to make it unique to each user.

Why mongoose.model() is required to get data from MongoDB?

I want to read data from the mongodb with mongoose, but every time it requires creating a model. Why?
I thought model are just like templates to insert data to MongoDB.
Can anyone describe what exactly mongoose.model() is and how it works?
I tried
const Model = mongoose.model(mongoose.Schima())
Without object in it
And it worked as well!!!
How does mongoose.model get data in background?
Thank you...
Mongoose models are much more than just templates on how to store data in the database: they perform type conversion, provide validation, have pre/post hooks, provide easy methods for population, and much more.
You don't need to use a full model to retrieve data from the database (in fact, you don't even need Mongoose at all), but you'll lose all the additional features.

SQL schema vs NoSQL namespace

I am pretty new to NoSQL and would like to fully understand the concept of namespace and how it compares to SQL schema.
I have seen plenty of useful analogies between tables, row, ... and their NoSQL counterparts.
Could you please help me understand the namespaces ?
In particular, I would like to know how I could leverage them to segregate the data of my dozen of customers ? I want to prevent accidental information leak between two of then, while still using a single database.
It really depends of the database engine you are using, it is hard to give a generic answer.
Ideally, if you really want to segregate the data, you can use multiple databases (in Redis, Redis Enterprise, MongoDB). In this case you are sure that data are separated. But you say you want to use a single DB. (why?)
If you want to stick with a single database you have various options, once again depending of the database engine you are using.
If you are using Redis:
you can use specific namespace based on key pattern, for example app:cust-001:orders, and you control the access to the data based on the key name/pattern. In Redis 6.0, the notion of ACL (Access Control List) has been added allowing you to limit the operations/access to the data based on a key pattern, for the connected user. This will allow you to have a good control of the data and who can see/manipulate them
If you are using MongoDB:
you can use multiple collections (tables), for example, prefixing the collection name with a context.
or you can use a composite key, where one of the fields will be your context
In both cases, for Redis and MongoDB, you are kind of creating using business logic the concept of "database".
If you provide more details/examples, the community can probably give you a more detailed answer.

Spring-mvc + Thymeleaf: dealing with complex form

I'm working on an internal tool using spring-mvc and thymeleaf.
A section of this tool is used to create an entity we save in the database.
This entity is quite complex; it contains many properties and relations. Some of these relations contain list and other properties.
I have 2 constraints:
Single page. No "wizard".
To only save a completed object in the database.
Now, I'm not really asking for a specific issue. I know my way around thymeleaf, spring #ModelAttribute, etc.
My question is mostly which strategy are you choosing or how to deal with really complex object creation.
Now I can see 3 ways to do it :
Rendering page with thymeleaf. Every time a new element need to be added to a list, I use Ajax to add the new element on the server and rerender the specific fragment. So doing back and forth to the server with my #ModelAttribute and only save at the end.
Rendering a basic page with thymeleaf. Using JavaScript to create html elements and instead of submitting to a #ModelAttribute, I'm serializing my form to JSON and submit this JSON to the server. (kind of client side model)
Rendering a basic page with thymeleaf. Create the html element dynamically with JavaScript when I need to add list item (being sure I'm putting proper name="" to fit with my Java form object) and submit the whole thing at the end.
I'm personally unsure between 1 or 2.
I feel dealing with complex object is much more easier using JSON than form submission. Also, the input value/field with sub object and property can be quite nasty. Having this kind of syntax
does not sound great to me...
3 can probably work but the way spring data binding is done with sub property is lacking some detail in my humble opinion (section 7.4.1 - http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html).
What do you think ?
Personally I use Thymeleaf's own dynamic field management to ensure clean addition of objects and fields to object.
So I will recommend option 4: Dynamic Field management by Thymeleaf.
Have a read of http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields.
I use that for both single field additions as well as addition of nested forms. Does the trick no questions asked.
Hope that helps.

MongoDB create a new collection from the client side?

Intro:
I have a collection of players in my app, this is built using a schema and then passing some stuff into my node.js configuration, perhaps it's a bit more complex than that but I mean this is the basic idea of how mongoDB for nodejs works with models and collections.
So now in my database I have my DB name then collections then a collection of players, great now I can build an api and start making requests to GET and PUT from that collection.
Question:
My problem is in my stats app after week one's game was tracked I need to clear all the player attributes so that in week 2 it's fresh, but I would like to save all the stats for the players from week 1.
So due to my basic knowledge of how a collection is built I am thinking in order to build a new collection I need to define a schema, but perhaps there is another way, if not then the schema would have to be dynamic right? Well how do I build a dynamic schema to suit this problem?
Ideas:
Idea: The user clicks a button saying complete week 1, then the server takes all the data from the players collection and builds a
new collection named week 1 players?
Question: If the above is a good idea, how do I build a function to create a new mongoDB collection and save players_week1 into its
own branch?
Final Word:
So hopefully someone has an optimal solution and understands what I am talking about, in the mean time I am going to try and follow my idea and see if I can search the docs to find some answers. Thanks guys.

Categories