fluree / ledger

Fluree ledger server source
GNU Affero General Public License v3.0
77 stars 8 forks source link

Adding data to my flureeDB #187

Closed luisenriqueramos1977 closed 2 years ago

luisenriqueramos1977 commented 2 years ago

I was following this tutorial for adding data to my flureedb: https://developers.flur.ee/docs/overview/fluree_basics/ First, I created this collection: [{ "_id": "_collection", "name": "Attention", "doc": "Text italicized by the note author" }]

Second, I added some instances of Attention: [ { "_id": "Attention$attention1", "textContent":"attention 1 of note 1" }, { "_id": "Attention$attention2", "textContent":"attention 2 of note 1" } ]

But, when I tried to link another instance to these new ones:

[ { "_id": "Note$note2", "contains_attention":[ "Attention$attention1", "Attention$attention2"] } ]

Fluree tells me Attention$attention1, and Attention$attention2 do not exist. I checked they were created, but Fluree is right because references attention1, attention2 are not there. But, in the mentioned example, the references were taken by fluree.

However, when I tried to add more data in the given example, I obtained the same error, verifying the reference of the example was missed as well (artist$2), so I wonder how to work with the custom reference (artist$2 in example, Attention$attention1 in my app)?, and avoid the need to use fluree reference (e.g "_id": 404620279021570). Because, in the end, when I substitute attention1 with its reference, the new object is accepted.

Luis Ramos

fdmmarshall commented 2 years ago

Hello @luisenriqueramos1977, when transacting new data to a predicate collection the _id requires a temporary value to successfully transact, after the successful transaction the temporary id no longer exists (this is why you can use the temporary id again which creates another new record)and the _id is assigned a fluree specific _id number.

There are two options depending on your specific use case:

If the identifier you are attaching to every new record will be unique for every transaction you could just create a predicate in your schema that is of type: string and unique: true . You will be able to query using that specific identifier to reach a specific existing record.

[{
"_id": "_predicate",
"name": "identifier",
"type": "string",
"doc": "unique string or set of characters that attached to every record"
}],
[{
"_id": "Attention$1",
"textContent":"attention 1 of note 1"
"identifier": "Attention$1"
 },
{
"_id": "Attention$2",
"textContent":"attention 2 of note 1"
"identifier": "Attention$2"
 }]

If you want multiple records to have the same string value, but you don't want to add new string object data every single time you can leverage the tag type. So if you have hundreds of thousands of records like:

{
    "_id": "user",
    "type": "STUDENT",
    ...
},
{
    "_id": "user",
    "type": "ADMIN",
    ...
},
{
    "_id": "user",
    "type": "STUDENT",
    ...
}

and the type is always going to be one of a small enumerated list of options, then tag is perfect because you don't persist a new string every single time, you just end up referencing the single tag for each value.

Hope that helps!