I'm working on a Flutter "Collections" app where I want a user to be able to define a schema for their list, then create several items that conform to that schema.
{
"name": "My Book Collection",
"schema": [
{
"id": "uuid-title",
"type": "text",
"name": "Title",
"unique": true
},
{
"id": "uuid-pages",
"type": "number",
"name": "Pages",
"min": 0,
"max": null,
"format": "int"
}
],
"items": {
"abc-123": {
"uuid-title": "The Hobbit",
"uuid-pages": 304
},
"def-456": {
"uuid-title": "Harry Potter and the Philosopher's Stone",
"uuid-pages": 223
}
}
}
What strategies can I use to update Specific Items or Schema Items, preferable immutably? I'm thinking I can make a Collection which has a List<SchemaItem> and Map<String, CollectionItem>. I think I can use a Provider or ProxyProvider to provide the List<SchemaItem> and a "Selected" CollectionItem to downstream widgets.
What strategies to I use to update items:
Is it enough to implement a CollectionItem.copyWith() function with some ChangeNotifier?
Do I need to go higher, with a Collection.updateItem(id: "abc-123", { "uuid-pages": 365 }) and wait for a Stream somewhere to provide an updated object?
Do I jump all the way to the top with a Service? CollectionService.selectedCollection.updateItem(id: "abc-123", { "uuid-pages": 365 }) and wait for a sqflite or Firebase update to trigger a rebuild?
I'm open to any starting points. All the tutorials, guides, and videos I've seen work with very simple data. Counters (Ints), ToDos (Strings & Bools), Pizza Toppings (List), but nothing with these nested maps. Do I just need to spend more time getting comfortable with OOP?
Finally, would I be crazy to work with the data as maps or should I definitely be converting them to Classes?
just activate the robo pojo generator in your android studio and past this json schema and get the corresponding model class and use it as you want
Related
I have an endpoint that returns a list of questions. Each question has the following properties
Input field name eg: Select from the following list
Validation eg: IsRequired
controlType eg: (dropdown, text, image, file)
values eg (dart, javascript, python)
I want to build input forms on the fly on mobile when i get this endpoint. How do i go about this on flutter. I already have the api.
Think of it like a quiz app but with different controls. eg. dropdown, textfield, checkbox.
Here is an example of the api response
[
{
"question": "what is your name ?",
"cotrollType": "textField",
"values": [],
"validations": [
{
"validation": "IsRequired"
}
]
},
{
"question": "select from the dropdown:",
"cotrollType": "dropdown",
"values": ["one", "two", "three"],
"validations": [
{
"validation": "IsRequired"
}
]
},
]
yeah more code is needed but probably you could just use BloC to get the API response and create the Widgets there and then just emit them as new state, that way you can show some sort of progress indidcator in the meantime, also are you using some kind of model to parse the response and make it a bit easier?
Here is my json file which I want to convert to dart model
{
"data": {
"catalog_id": "615ac5699a3c9f2ea3a65180",
"catalog_images": {
"l_large": {
"url": ""
},
"l_medium": {
"url": ""
},
"p_small": {
"url": "https://s3-ap-southeast-1.amazonaws.com/ott-as-service/ott_default_images/default.png"
}
},
"videolist_tags2": [],
"items": [
{
"title": "HERE AND NOW",
"content_id": "615acc129a3c9f2ea3a6518c",
"status": "published",
"sequence_no": 1,
"catalog_id": "615ac5699a3c9f2ea3a65180",
"catalog_object": {
"friendly_id": "movies",
"layout_type": "t_16_9_big_meta",
"id": "615ac5699a3c9f2ea3a65180",
"plan_category_type": "",
"layout_scheme": "",
"catalog_id": "615ac5699a3c9f2ea3a65180"
},
"play_url": {
"saranyu": {
"url": "http://52.77.63.32//v2/smart_urls/61c5c5868530b8bb03e2b625"
}
},
....
}
}
I would like a tool which can auto generate a model for me, because it is very time consuming manually.
You can use this tool to convert your json to dart. It also supports null safety and complex lists.
just go to browser and type quick type that is the best for converting json to any model class.copy your json code and paste it over there it will generate your model class.
There is no way to do that perfectly. JSON does not contain type information of the kind you need for null safety.
For example, there is no way to know, whether there are fields that are nullable, because it would be perfectly fine to just not have them in the JSON data at all. There also is no way to know whether a field is nullable, that right now is in there and has a value.
So, you can use a generator, but to actually create a good model, you will either need your own knowledge or you need a description like Swagger/OpenAPI that is not just example data, but actual interface definition.
I am trying to implement a query or something similar where I can retrieve data from Firebase where I can get cars that have at least one matching tag with the currently displayed car.
Here is my Object and the way it is structured on Firebase
"cars": {
"carID1": {
"name": "car1",
"id": "carID1",
"tags": {
"0": "racing",
"1": "sport"
},
},
}
For example I have a car with tags: 'sport' and 'automate' and would like to display all the other cars that have either 'sport' or 'automate' tag.
What would be the best way to implement a query of this sorts ?
I want to store website menus in Mongo for the navigation of my CMS, but since I'm new to Mongo and the concept of documents, I'm trying to figure out what would be best:
a) Should I store menu documents, containing children and those having more children, or
b) Should I store menu item documents with parent_id and child_ids ?
Both would appear to have benefits, since in case A it's normal to load an entire menu at once as you'll need everything to display, but B might be easier to update single items?
I'm using Spring data mongo.
PS: If I asked this question in a wrong way, please let me know. I'm sure this question can be expanded to any general parent-child relationship, but I was having trouble finding the right words.
Since menus are typically going to be very small (under 16MB I hope) then the embedded form should give you the best performance:
{
"topItem1": [
{ "name": "item1", "link": "linkValue" },
{ "name": "item2", "link": "linkValue" }
],
"topItem2": [
{ "name": "item1", "link": "linkValue" },
{ "name": "item2", "link": "linkValue" }
{
"name": "sub-menu",
"type": "sub",
"items": [
{ "name": "item1", "link": "linkValue" },
{ "name": "item2", "link": "linkValue" }
}
}
]
}
The only possible issue there is with updating the content inside nested arrays, as MngoDB can only "match" the first found array index. See the positional $ operator documentation for this.
But as long as you know the positions then this should not be a problem, using "dot notation" concepts:
db.menu.update({}, {
"$set": {
"topItem2.2.items.1": { "name": "item3", "link": "linkValue" }
}
})
But general adding should be simple:
db.menu.update(
{ "topItem2.name": "sub-menu" },
{
"$push": {
"topItem2.2.items": { "name": "item4", "link": "linkValue" }
}
}
)
So that is a perspective on how to use the inherrent embedded structure rather than associate "parent" and "child" items.
After long hard thinking I believe I would use:
{
_id: {},
submenu1: [
{label: "Whatever", url: "http://localhost/whatever"}
]
}
I thought about using related documents with IDs all sitting in a collection but then you would have to shoot off multiple queries to get the parent and its range, possibly even sub-sub ranges too. With this structure you have only one query for all.
This structure is not infallible however, if you change your menu items regularly you might start to notice fragmentation. You can remedy this a little with powerof2sizes allocation: http://docs.mongodb.org/manual/reference/command/collMod/#usePowerOf2Sizes
But yes, with careful planning you should be able to use one single document for every parent menu item
Is this example of nesting generally accepted as good or bad practice (and why)?
A collection called users:
user
basic
name : value
url : value
contact
email
primary : value
secondary : value
address
en-gb
address : value
city : value
state : value
postalcode : value
country : value
es
address : value
city : value
state : value
postalcode : value
country : value
Edit: From the answers in this post I've updated the schema applying the following rules (the data is slightly different from above):
Nest, but only one level deep
Remove unneccesary keys
Make use of arrays to make objects more flexible
{
"_id": ObjectId("4d67965255541fa164000001"),
"name": {
"0": {
"name": "Joe Bloggs",
"il8n": "en"
}
},
"type": "musician",
"url": {
"0": {
"name": "joebloggs",
"il8n": "en"
}
},
"tags": {
"0": {
"name": "guitar",
"points": 3,
"il8n": "en"
}
},
"email": {
"0": {
"address": "joe.bloggs#example.com",
"name": "default",
"primary": 1,
"il8n": "en"
}
},
"updates": {
"0": {
"type": "news",
"il8n": "en"
}
},
"address": {
"0": {
"address": "1 Some street",
"city": "Somecity",
"state": "Somestate",
"postalcode": "SOM STR",
"country": "UK",
"lat": 49.4257641,
"lng": -0.0698241,
"primary": 1,
"il8n": "en"
}
},
"phone": {
"0": {
"number": "+44 (0)123 4567 890",
"name": "Home",
"primary": 1,
"il8n": "en"
},
"1": {
"number": "+44 (0)098 7654 321",
"name": "Mobile",
"il8n": "en"
}
}
}
Thanks!
In my opinion above schema not 'generally accepted', but looks like great. But i suggest some improvements thats will help you to query on your document in future:
User
Name
Url
Emails {email, emailType(primary, secondary)}
Addresses{address, city, state, postalcode, country, language}
Nesting is always good, but two or three level nesting deep can create additional troubles in quering/updating.
Hope my suggestions will help you make right choice of schema design.
You may want to take a look at schema design in MongoDB, and specifically the advice on embedding vs. references.
Embedding is preferred as "Data is then colocated on disk; client-server turnarounds to the database are eliminated". If the parent object is in RAM, then access to the nested objects will always be fast.
In my experience, I've never found any "best practices" for what a MongoDB record actually looks like. The question to really answer is, "Does this MongoDB schema allow me to do what I need to do?"
For example, if you had a list of addresses and needed to update one of them, it'd be a pain since you'd need to iterate through all of them or know which position a particular address was located. You're safe from that since there is a key-value for each address.
However, I'd say nix the basic and contact keys. What do these really give you? If you index name, it'd be basic.name rather than just name. AFAIK, there are some performance impacts to long vs. short key names.
Keep it simple enough to do what you need to do. Try something out and iterate on it...you won't get it right the first time, but the nice thing about mongo is that it's relatively easy to rework your schema as you go.
That is acceptable practice. There are some problems with nesting an array inside of an array. See SERVER-831 for one example. However, you don't seem to be using arrays in your collection at all.
Conversely, if you were to break this up into multiple collections, you would have to deal with a lack of transactions and the resulting race conditions in your data access code.