From the Diesel documentation I can see that it is possible to retrieve all the children of a selected parent using the belonging_to method:
https://docs.diesel.rs/diesel/associations/index.html
In the example, the first Post of the User is retrieved using belonging_to:
let user = users.find(2).get_result::<User>(&connection)?;
let users_post = Post::belonging_to(&user)
.first(&connection)?;
I would like to know if Diesel offers the possibility to obtain the parent from the child (in the example, the User from the Post).
It would even better to insert the User in a variable of the Post struct.
Related
I have a single collection into which I am inserting documents of different types. I use the type parameter to distinguish between different datatypes in the collection. When I am inserting a document, I have created an Id field for every document, but Cosmosdb has a built-in id field.
How can I insert a new document and retrieve the id of the created Document all in one query?
The CreateDocumentAsync method returns the created document so you should be able to get the document id.
Document created = await client.CreateDocumentAsync(collectionLink, order);
I think you just need to .getResource() method to get the create document obj.
Please refer to the java code:
DocumentClient documentClient = new DocumentClient(END_POINT,
MASTER_KEY, ConnectionPolicy.GetDefault(),
ConsistencyLevel.Session);
Document document = new Document();
document.set("name","aaa");
document = documentClient.createDocument("dbs/db/colls/coll",document,null,false).getResource();
System.out.println(document.toString());
//then do your business logic with the document.....
C# code:
Parent p = new Parent
{
FamilyName = "Andersen.1",
FirstName = "Andersen",
};
Document doc = client.CreateDocumentAsync("dbs/db/colls/coll",p,null).Result.Resource;
Console.WriteLine(doc);
Hope it helps you.
Sure, you could always fetch the id from creation method response in your favorite API as already shown in other answers. You may have reasons why you want to delegate key-assigning to DocumentDB, but to be frank, I don't see any good ones.
If inserted document would have no id set DocumentDB would generate a GUID for you. There wouldn't be any notable difference compared to simply generating a new GUID yourself and assign it into id-field before save. Self-assigning the identity would let you simplify your code a bit and also let you use the identity not only after persisting but also BEFORE. Which could simplify a lot of scenarios you may have or run into in future.
Also, note that you don't have to use GUIDs as as id and could use any unique value you already have. Since you mentioned you have and Id field (which by name, I assume to be a primary key) then you should consider reusing this instead introducing another set of keys.
Self-assigned non-Guid key is usually a better choice since it can be designed to match your data and application needs better than a GUID. For example, in addition to being just unique, it may also be a natural key, narrower, human-readable, ordered, etc.
Describe relationship in maximo 7.5 with all detail including backend database process.
Suppose I create a relationship in maximo 7.5 asset management between owner_group and person_group "owner_group=:person_group"
then what is the actual meaning of this query. I am quite confused about it.
Can you please explain what is the process in database?
Maximo "relationships" are simply the "where clause" snippet of an SQL statement. The "child object" that you define is the table that "where clause" will be run against.
Maximo's backend always starts out with "select * from ". It will then append your child object's table name (which is almost always the object name itself). Then it appends " where " and your relationship text. In the end Maximo ends up running the query "select * from <object> where <relationship>" against the database. It will then load the results into an MBOSet of <object>s.
It isn't so much a join as it is a jump/switch to another table. Because it isn't a join, the query has no context of the object data you started from. To be able to use data from your originating object to filter down your destination object set, you can use field binds, those "colon fieldname" pieces of the where clause snippet. Maximo will do a find and replace on all of those binds it finds, replacing them with data from the appropriate fieldnames of your starting object. This is where the "parent object" setting of the relationship comes into play. That means Maximo will only run this query if you start from a record of a type equal to the "parent object" so that it knows where those field binds come from.
It might be worth mentioning that a relationship must start from a single object (MBO), not a set of objects (MBOSet) in order to allow this work. However, since a query returns one or more records in its result, you will always get a set of objects back (MBOSet) from the relationship. Also, because this is a find and replace like this before the query is run, Maximo does not take advantage of any database query plan caching feature; each query looks different to the database and is reparsed before being executed.
For example, if you want to create a relationship from Asset to Workorder to get all of the open workorders for an asset, you would do the following:
Create a relationship with a child object of WORKORDER because that is the record type you want to get back. You want all open workorders, so you start your relationship text with "status = 'OPEN'". You also only want workorders for the asset you are currently looking at, that means you want to use data from your current object so you need to use a field bind. In this case you want to filter the workorder records whose assetnum is the same your current record's assetnum, that means you add " and assetnum = :assetnum" to your relationship text/where clause (giving you "status = 'OPEN' and assetnum = :assetnum" for your total relationship string). The first assetnum without the colon means it is normal query text and will be run as is. The second assetnum with the colon tells Maximo this is a bind that needs to be replaced. The order doesn't matter, just the colon matters. Maximo will find the assetnum field of your current object (ASSET) and put the contents of that field (say assetnum "Blower0082") into the query in place of the ":assetnum". The end result of this defined relationship when run on this particular asset is that Maximo runs the following query against the database and loads any results into a WORKORDER MBOSet:
select * from WORKORDER where status = 'OPEN' and assetnum = 'Blower0082';
Your particular example of owner_group = :person_group doesn't make sense on its own; a parent object (so Maximo knows where to pull bind data from and when it is allowed to execute this query) and a child object (so that it knows what table it is selecting from) are needed too. I'm going to assume this has a parent object of PERSONGROUP and a child object of WORKORDER. Since "person_group" starts with a colon, Maximo is going to replace that string with the contents of the person_group field of your parent object, so the person_group field must exist on your parent object. Since owner_group does not have a colon in front of it, it is normal SQL syntax and therefore that field must exist on your child/destination object. This relationship would get you all workorders that are owned by the persongroup record you have loaded at the moment. Assuming that persongroup is "plumbers", it would run the following query and return all workorders currently owned by plumbers: select * from WORKORDER where owner_group = 'plumbers';
You can think of Maximo relationships in the same terms as a SQL join.
In System Configuration > Platform Configuration > Database Configuration -- every object can have relationship associated with a child object.
For example, if your Parent Object is Work Order, I will create a relationship called OWNERPERSON where the Child Object is PERSON.
The Where Clause personid = :owner means when the Child Object (PERSON) brings a result from the where clause personid = :owner then the related data can be displayed. :owner is the variable that is in WORKORDER.
If the :owner value in WORKORDER is Chip_Drapeau then you can display any from the PERSON Child Object where personid = Chip_Drepeau
Hope this helps.
I have a Mongoose model that holds Places. Each place has a lat/lng. I can define a Mongoose virtual attribute called distance that would be used to sort Places in ascending order. What's the best way to refer to the user's location information (let's assume it's stored in a session variable for now) from inside the distance virtual attribute?
For anything involving external data, adding a method to the schema would be a better choice than a virtual property.
I'm solving a similar issue. The problem is that methods are fine if you want perform an operation on a single value but I'm retrieving a list and want to inject a new virtual field into every record in the list - but use session data to generate the field. to do this safely (avoiding globals), I think I'll need to use a QueryStream and inject the new field using an ArrayFormatter that takes the session variables as constructor parameters.
This also looks like a job for LINQ so another approach might be to use one of the ports of LINQ to JS.
If you sill prefer to use virtuals, you can store user location info in NodeJs globals. For example this code may be set after user login:
global.user_location = user.location;
I really like the document-based approach of storing data like blog posts as a whole document with all information needed saved inside of it. Therefore the authorĀ“s username is stored as plain text. The author himself has his own document with personal information attached to it. What happens when the author decides to change his username? Do I have to update every document the contains a blog post by that author or is this just one of the drawbacks using a document-based database?
Thanks for any suggestions!
If you need to write a query(view) with content from the blogpost and the name of the author, then the name must be included in the blog content, and therefore all blogposts must be updated.
if the name is only for information ( i mean you do not query a blogpost-content like keywords AND name of author), you can add the id into the blog document (and of course now can Query blog content AND author-id) and emit {'_id':doc.author_id} as a Value.
include_docs=true then gives you the doc of the Author (and no longer the blogpost-doc.. you have to call it explicit with the id thats in the result rows). No Need to update the blogposts.
Example:
Case 1:
Use Author by Name, you have to include the name, and therefore update ALL docs.
{
"_id":"blogpost1",
"author":"Oliver",
"keyword":"couchDB"
}
to look for all couchdb posts from oliver:
emit ([doc.author,doc.keyword],1)
call:
&key=["Oliver","couchDB"]
Case 2:
No need to query by name
{
"_id":"blogpost1",
"author_id":"author-123",
"keyword":"couchDB"
}
emit (doc.keyword,{'_id':doc.author_id})
and the authors doc:
{
"_id":"author-123",
"name":"Oliver"
}
call:
?key=["couchDB"]&include_docs=true
result:
...
{"id":"blogpost1","key":"couchDB","value":{"_id":"author-123"},"doc":{"_id":"author-123","_rev":"xxx","name":"Oliver,....
I've been exploring Sub Sonic 3's SimpleRepository and have been pretty happy with it but have a question regarding transactions. I am aware that using methods like 'AddMany' and 'DeleteMany' will automatically perform all of those operations within a single transaction, but was wondering if it's possible to force the SimpleRepository to perform the add or update of two different object types within the same transaction. For example, let's say I have the notion of two different but related entities in my application: a User and a Profile. Every user has to have a Profile and every Profile belongs to one and only one user. When a new user registers with my application I want them to provide the basic user information (credentials, name, e-mail) but also want some additional "profile" information (about me, gender, zip code, etc.)
I'd like to be able to perform the add of the User and the Profile object within a single transaction, but since it requires two distinct calls to the 'Add' method with different type parameters I'm not sure how to make this work.
You can do this using a transaction as follows:
using (TransactionScope transactionScope = new TransactionScope())
{
using (SharedDbConnectionScope connectionScope = new SharedDbConnectionScope())
{
// Add your user
// Add your profile
transactionScope.Complete();
}
}
adam - i think the code above is correct but is actually the wrong way around, it should be:
using (SharedDbConnectionScope connectionScope = new SharedDbConnectionScope())
{
using (TransactionScope transactionScope = new TransactionScope())
{
// Add your user
// Add your profile
transactionScope.Complete();
}
}
cheers
jimi