TypeORM how to create entity with relation to other entities without loading the other entities into memory - typeorm

System Synopsis
I have a system where I have users and addresses.
Each user can have many addresses.
There is an endpoint in which a user can save a new address.
At this endpoint I have the users phone number and the new address they want to add.
Problem
At the moment I need to read the user from the database into memory to create the address object. I find the user in the database using the phone number. This seems like a lot of needless overhead and wouldn't exist if I was using raw sql.
Question
Is there someway to create the address object and say that it should belong to the user with this phone number without loading the user into memory first?

Related

Firebase -Can Shard Data be shared between multiple Realtime Databases

Using the Realtime Database it says here that if you want to scale beyond 200,000 simultaneous connections that you can create/shard another database. It also says:
Each query only runs against a single database instance. Realtime
Database doesn't support queries across database instances.
No sharing or duplication of data across database instances (or
minimal sharing or duplication).
Each app instance only connects to one database at any given moment.
Let's say in first database I have a Posts ref, a Users ref, and a Search Posts ref with 100K user objects, 200K post objects, and 200K search objects. I now decide to create/shard another database with the same exact refs.
When the next x amount of users signs up, if their User, Post, and Search Posts refs are in the new shard database, does that mean that they won't be able to access users or search those user's posts from the first database? Also vice versa the users from the first database won't be able to have access to users or their posts in the second database?
The point of sharding is to load balance your connections and is not related to quantity of data.
The RTDB makes no decisions about where data is stored, you do. One server would contain users, another would contain posts.
You would run user queries against the server with users and posts queries against the serve with posts.
All you're doing is pointing your app toward the server you want to query before running the query.
In other words, there would be no reason to add users to server 1 and then add more users to server 2 as the quantity of data doesn't matter.

Designing a Core Data model to remember previous object states

Let's say we have a simple Core Data model which keeps track of transactions a bunch customer makes, but for some reason, when we look at transactions, we want to know the customers address at the time the transaction was made.
If the customer changes their address, what is the best way to store the old state of the customer?
One way might be to store the customer's address on the transaction, but that wouldn't work with to-many relationships, e.g. if prices of multiple products in a previous transaction changed, how would we record that?
Another option would be to just duplicate the objects, but then we need some way of knowing which is the current one. Would It be better to create intermediary entities which are related to both the transaction and the customer/product?
From your description I'd probably do this:
Every Customer has an Address.
Every Transaction has an Address.
If the Customer gets a new Address, this doesn't affect any existing Transaction's Address.
You can always find all transactions for an address, or the address for any transaction, even if the customer address changes.
If necessary you can get all addresses for a customer by looking at the customer's transactions, and getting the addresses for those. You could also add a pastAddresses relationship from Customer to Address if you need it.

Firebase: How to identify registered contacts?

I am writing an app, where a signed up user should be able to see which of his contacts have signed up, too. What is the most elegant way to do this?
I was planning to create an array of all locally saved email addresses extracted from the user's local iOS addressbook and create a query for those. Is there any better way to do this?
Edit: Is this actually possible without downloading the whole user list? I could use a for loop with queryStartingAtValue(emailAddress) and queryEndingAtValue(emailAddress). But this could possibly lead to hundreds of queries at the same time.
In NoSQL databases you'll often end up modeling the data in ways that your application wants to consume it.
In this case it seems your app needs to look up whether a user exists, based on their email address. For that purpose I'd add a list of email-to-uid data:
emailToUid
"test#mail,com": "P0...wklsh1"
"MJQZ1347": "Aj1278a..."
This is essentially a self-created index that allows you to check whether an email address is used without having to run a query.
Now you can loop over the contact and look whether there is a user for that email address with a:
ref.child("emailToUid").child(email).observeSingleEventOfType(.Value
This is going to be very fast. Because of the way Firebase communicates with the back-end, there's going to be very little difference between a single request with 100 email addresses or 100 requests with a single email address. See my answer here for more on that: Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly
You could have something like this
user
-$user_id
-email
-username
-contacts
-contact_uid1:email1,
-contact_uid2:email2,
-contact_uid3:email3,
And then do:
Download the contact child from firebase and save it to a var
Create the loop to check every contact in the address book
If the email is in the contact child don't do anything (it means you already verified the user once)
If the email is not in the contact child launch a single event query to find the uid of an specific email
in the callback of the query if it is nsnull the user is not in the app
if the user exist, add the user to your contacts child node in firebase
This way you only launch the queries of the contacts you haven't checked

iOS Address book records existing outside of address book database

I was reviewing Address Book Programming Guide for iOS and came across this comment:
Even though records are usually part of the Address Book database, they can also exist outside of it. This makes them a useful way to store contact information your application is working with.
What does this mean? Can I create contacts that are viewable by contacts app but not stored in the shared database?
Thanks!
A Record is just an object, so you can create one and do whatever you want with it. That statement means you can create and use an ABRecord for your own purposes without putting it into the Address Book database.
It means the opposite of what you asked - if you want a contact viewable in the contacts app, you have to put it in the Address Book database. However, if you're going to make your own contacts app (or add your own internal address book functionality), you could create and use ABRecords in your implementation.
ABRecordRef aRecord = ABPersonCreate(); will create a new record, and you can fill it out with contact information and use it internally in your app. Thus, as the snippet you found said, you can use them as a way to store contact information within your app without putting those contacts into the address book database.

Finding a user by email address

I was looking at the Users API on the D2L API reference site:
http://docs.valence.desire2learn.com/res/user.html
And there does not appear to be a way to find a user by email address.
Is there a way to do this with the current API?
You can fetch user records from the .../users/ route using either the organization-defined ID, or the user login name properties. Additionally, you can fetch the user record for a user identified by the LMS UserID property. You cannot currently easily retrieve a user record based on another property in the user record: you'd need to fetch entire collection of users and then sort through for the record with the email address you're looking for. With most organizations, this is not at all ideal because of the number of users involved.
Generally improvements to the API of this nature are on the development roadmap, however there isn't a specifically deployment plan in place for an enhancement on this particular use-case. That said, enhancements to the routes to search for fundamental data objects (users, org units) are identified as having strong value by clients and that's a primary driver in determining priority for improvements..

Resources