couchdb views tied between two databases? - javascript

Say I have several databases on my couch instance (in my case a user account database and login session database) The sessions have fields corresponding to a field in the user database. Is there a way to create a view, or make a call that encompasses this correlation, or would it just have to be done with an external script? To be more clear, here's an example.
Account db:
{
"_id": "78555fdfdd345debf427373f9dfaeca4",
...
"username" : "bob"
}
Sessions db:
{
"_id": "78555fdfdd345debf427373f9dfcd7ae",
..
"accountId": "78555fdfdd345debf427373f9dfaeca4",
"username": "bob"
}
Can I use emit or something like that to bundle together all this information in one call?

No, however a common workaround is to have a "type" attribute for documents.
For example...
Application db:
{
"_id": "account.78555fdfdd345debf427373f9dfaeca4",
"type": "account",
...
"username" : "bob"
}
{
"_id": "session.78555fdfdd345debf427373f9dfcd7ae",
"type": "session",
..
"accountId": "account.78555fdfdd345debf427373f9dfaeca4",
"username": "bob"
}
And then in your views:
map:
function (doc) {
if (doc.type=='account') {
# ...
}
}

No, unfortunately there is no way to connect data from multiple databases.
Each view is supposed to be self-contained, otherwise updating any document in 1 database will immediately need to trigger views indexes in every other database to be recalculated in all cases.

Related

Limit length array-type of property in Loopback 4 query?

I've been trying this new framework Loopback 4 and its awesome but I dont know to which point is flexible,I'm having the following model on the database:
{
"id": "string",
"lastUpdate": "2020-10-01T18:10:46.306Z",
"name": "string",
"logo": "string",
"data": [
{}
]
}
And what I'm trying there is to make a query that returns me the data, but as is an array, it has a lot of data and I would like to paginate it, so I thought on limiting the query. I've achieved a query to look like the following:
{
"offset": 0,
"limit": 10,
"skip": 0,
"where": {
"name": {"eq":"BengalaSpain"}
},
"fields": {
"data": true
}
}
I'm trying to limit the data property to 10, but of course, this dosnt affects the property itself, just the wrapper object around it. Is there any way to achieve what im trying?
Thanks in advance guys!
LoopBack 4 filters apply at a Repository level as these constraints are passed to the ORM datasource connectors to be converted into their respective native queries (e.g. TOP 10 for SQL Server).
A possible solution is to link the data field into a Relation. Relations essentially create nested Repositories (e.g. hasManyRepository), hence are able to meet the requirement of isolating data into its own Repository.
To quickly create a relation, remove the property from the Model and re-create it using lb4 relation command.
From there, it would be possible to take advantage of the now-enabled InclusionResolver and write use query:
{
"where": {
"name": {"eq":"BengalaSpain"}
},
"fields": {
"data": true
},
"include": [
{
"relation": "<relation name here>",
"scope": {
"limit": 10
}
}
]
}
A side-effect is the separation of data into its own table. However, this should be done irregardless due to database normalization.

Omit fields when performing Firebase query

Is it possible to omit particular fields when performing a Firebase query?
The following query:
getReferredUsers(referralId) {
this.logger.debug(`Getting referred users by referral id ${referralId}`);
if (referralId) {
return this.afs.collection('users', ref => ref.where('referredBy', '==', referralId).limit(3)).valueChanges();
}
}
Returns the following:
{
"displayName": null,
"email": "theshizy#hotmail.co.uk",
"emailVerified": true,
"firstName": "James",
"lastName": "Burton",
"photoURL": "https://i.imgur.com/LHhAQBH.jpg",
"referralId": "6O5qGmRQ",
"referredBy": "cpRcYiov",
"selectedCurrency": "GBP",
"selectedTimezone": "(UTC) Edinburgh, London",
"uid": "LjZ8TAb4ZGhgw2DGubbVosw8dkd2"
}
]
Can I cut it down further so that it only returns the following:
{
"emailVerified": true,
"firstName": "James",
"lastName": "Burton",
"photoURL": "https://i.imgur.com/LHhAQBH.jpg",
"referralId": "6O5qGmRQ",
}
]
The Firestore client-side SDKs always returns complete documents. There is no way to request only a part of the document with the client-side SDK, although the option does exist in the server-side SDKs' select methods.
If you only need a part of the information, consider creating an additional collection where each document just contains the data you need. This will also allow you to more easily secure access to the different types of data.
Also see:
How to allow only particular fields of a firestore document to be accessed publicly
How to get a list of keys in Firestore?

Meteor: how to sort users by most following?

welcom ...
I have in my Meteor project 2 collections
1 - followers
"_id": "_id",
"follower": "username1",
"following": "username2"
}
2- users
"_id": "_id",
"username": "username",
[...]
}
I would like to sort users by the most following how I can do this
can anyone help me ?....
I would suggest putting the 'followers' collection as an object inside each 'users' document. There's no point in putting them in separate collections and then having to reference back and forth based on a user's ID. It's just taking up more space in your DB. Just make an object inside each user called 'follows' or something similar with the same structure (though make 'follower' and 'following' arrays). Something like this:
"users":{
"_id":_id,
"username":"username",
"follows":{
"followers":["username 1","username 2"],
"following":["username 3", "username 4"],
}
}
Once you have it so each user document has it's own 'follows' object, you can sort users by using the 'aggregate' functionality MongoDB Aggregate. This following code sorts based on the length of the followers array. You'd do a separate one for 'following' swapping out '$followers' with "$following".
db.users.aggregate(
[
{
$project: {
"length": { $size: "$followers" }
},
{ "$sort": { "length": -1 } },
}
]
)
This will probably require some tweaking, of course. just helping lead you in the right direction.

Facebook Graph API friends' names and ids

I know that if I simply try to get a list of my friends, it will only return app friends AKA friends who also have given permissions to Facebook
So, rightfully so, when I do a GET 'me/friends', I only see 5 friends.
However, when I do a GET me/posts/?fields=likes.fields(name), it obviously returns data with a list of friends who have liked my posts:
{
"id": "post1id",
"likes": {
"data": [
{
"name": "name1",
"id": "id1"
},
{
"name": "name2",
"id": "id2"
}....
]
}
It shows all friends, so non-app and app friends.
My question is, if all I want to use is my friends' names and id's from this result, would it be allowed, considering that some of these friends are not app-friends?
If you work the API with the user token, you may receive all likes (including friends who don't use the app, non-friends, friends who use the app, pages) but this is at all no indication for a users friends. Anyways you can utilize ALL data provided by the GraphAPI as long as you don't work around the limitations with something like exploiting the game invite feature.

Firebase linking users across application with unique identifier

I'm using firebase and my users are set up like this:
{
"firebase-account-123": {
"users": {
"simplelogin:1": {
"properties"{ "name": "john doe", "email": "user#email.com" }
"children": {
"simplelogin:2":{ "name": "user 2", "email": "user2#email.com" },
}
},
"simplelogin:2": {
"properties"{ "name": "user 2", "email": "user2#email.com", "disabled": false }
}
}
}
I use simplelogin:X and the users email across many objects in my database. I know using this to identify my users across different objects is probably a bad move.
At some point I am trying to implement the ability to change emails which means that their simplelogin ID is going to have to change along with every email property that ever linked to their email.
Does anyone have any suggestions on how I can link users to one another without have to change their data if it changes across the entire platform? I'm thinking somewhere along the lines of a unique identifier for each users. I just don't know how I would implement this.

Categories

Resources