'Michelangelo_-_Creation_of_Adam' 'Edgar Degas - La famille Bellelli ' ' Claude Monet, Impression, soleil levant ' 'Renoir:7505c319-a5ee-4285-a045-f783d6f7e975'
Parse.com Cloud Code is like JavaScript Pseudo-Triggers on MongoDB
Using Parse.com allows me tremendous simplicity when writing mobile apps: it lets me focus on just writing the mobile code and the database, but lets me use an N-tier deployment model and a remote NoSQL database. However, there are times when I really want to run a tiny piece of code inside the database — the same way I would use stored procedures or triggers in a relational database — or inside the REST server as I would normally do with server-side business logic.
For example, one of my mobile apps (PatchTrader) enables Scouts to more easily trade patches at large Scouting events. In this app, whenever a Scout “checks-in” at a new location, any of their older check-ins should be marked as expired in the remote database. I could have the mobile app save the new check-in to the remote database, then retrieve any old check-ins for that user and reach back to the server to expire each one, but this is a terrible use of mobile bandwidth and battery life. It also very fragile. PatchTrader is used at campsites with bad cell service, so it is very possible that the app will be unable to successfully complete all that work, thus leaving old check-ins still showing as “active” in the database. In an ideal world, I’d want a database trigger to fire on the first update and mark the other rows as expired, or I’d have my “check-in” RESTful service handle this … but I didn’t build a REST server and I’m not on a relational database.
Enter Parse’s “cloud code” — it fills that need nicely, and once again saves me from having to write a full server application when I just need one little bit of server-side code.
Parse’s “cloud code” allows you to run custom JavaScript inside the Parse server rather than in your client app, but still without requiring you to build a real REST server. You can use that cloud code to do things such as providing custom server-side logic, doing more efficient database processing or specialized counts, sending push notifications, and most importantly in my “PatchTrader” case, it provides hooks for code that can be run before or after a database object is saved or deleted in the MongoDB database. Pseudo-triggers on MongoDB!
Admittedly these are not real triggers but instead merely server-side JavaScript that is called by Parse’s underlying Node.js engine, but they feel like database triggers so calling them triggers better fits my mental model. And while one could argue that database triggers are evil because they hide business logic inside the database rather than keeping it visible in the server-side code, my DBA hat would reply that there are valid cases for triggers. Then my developer hat would chime in and explain that in this case, PatchTrader is a free app with a very limited audience that I’m doing in my spare time and it is tracking nothing more serious than map check-ins while trading patches, and my time would be better spent enhancing the app rather than building a REST server for it, so the trade-off is acceptable.
To show a simple cloud code example, this is the “after save” code that gets fired off by the server whenever a new check-in is saved by a PatchTrader user:
As you can see, it’s very simple and it gets the job done without adding any extra burden to the mobile app or mobile bandwidth, and it prevents bad data from gathering in the database.
It’s very handy, when used appropriately. If you’re a Parse.com developer and you haven’t played with Cloud Code, I urge you to take a look!
How to Restore a Single iOS App from a Backup
- I am using iTunes 12.2 on Windows with iOS 8.4.1 for the directions below although this should work in iTunes on Mac OS X as well.
- The directions assume you’ve made a backup onto your laptop or desktop computer. Backups to iCloud won’t work.
- If successful, this will restore both the old version of the app plus any data that had been backed up for that app.
- In iTunes, TURN OFF automatic syncing of devices.
- In Windows, this is in “Edit -> Preferences -> Devices”
- On Macintosh, this is in “iTunes -> Preferences -> Devices”
- Do NOT update your apps in iTunes.
- On the iPhone, delete the existing app from the phone.
- If you don’t delete the existing app from your phone before doing the backup below, the backup will overwrite the old app version in your backup with the new version from the phone.
- If you do accidentally do this, see “How to Import an Older App Version” below for how to fix this.
- Connect your phone to your computer. Wait for it to appear in iTunes.
- Select your phone in the iTunes left panel under “Devices” or via the “Device” toolbar button.
- Back up the existing apps from your phone to your computer via “Transfer Purchases” in iTunes.
- This is done either by right-clicking on the “Devices -> Apps” menu for your phone or selecting “File -> Devices -> Transfer purchases from [your phone name]“.
- On Windows, you may need to press F10 to see the File menu.
- In iTunes, click the “Apps” button at the top of the Summary page for your phone.
- Scroll down through the list of your apps until you find the app you want to restore. The button next to the app should read “Install”. Click this button.
- Click the “Apply” button down towards the bottom right of the iTunes window. This will start the normal syncing process, and it will also reinstall the app onto your phone.
- The status bar at the bottom of the iTunes window might make it look like all your apps will be removed. Don’t panic — they won’t be.
- The replacement app — the old version that you just restored — will appear in the first available icon position on the iPhone. If you previously had the app in a folder, you will need to manually put it back into the folder.
- If everything worked, cheer wildly.
- Open the recycling bin, or wherever you stored the old IPA files.
- Find the IPA file for the old version of the app.
- Open iTunes.
- Drag the .IPA file into the “Library” section of iTunes.
- Click “Replace” when iTunes warns you that you are about to replace a new version of that app with an old version.
- Resume the process above.
Parse.com – All the Joys of a RESTful Server without the Coding
Many times my mobile apps need a remote or a shared database, but often they need nothing more than only that: the mobile client and the remote database. I could write a new RESTful server for each of those apps but when the app needs only standard CRUD operations, writing a new REST server each time feels like overkill. (And connecting directly to a remote database over the Internet is a horrible evil idea!)
This is where Parse.com shines: it provides REST services, authentication, user management, security, and data storage, letting me focus on writing just the client code and the database design. It lets me deliver an N-tier app but build it as if it were just a simple 2-tier app, leveraging a NoSQL database that “feels” like it is connected directly to my mobile app. (Parse uses MongoDB and Node.js behind the scenes.) Parse also provides very nice SDKs, providing full support for features like async processing in C#/Xamarin (my mobile platform of choice!), JavaScript, and Objective-C among others. Parse also provides much more: the ability to run arbitrary JavaScript code, easy support for push notifications, hosting of static HTML pages, and more – but for simple apps, the basic data storage and REST services are all I need.
Moreover, Parse provides a very generous free tier so I can get started on an app or prototype an app for a client for no cost, then scale it up into the paid tiers when the app grows or traffic spikes.
I highly recommend it!
(Apparently I’m coming fairly late to the “Parse” party – it seems like many others already knew about this gem. Now that I’ve finally tried it, however, I’m kicking myself for not using it earlier so I’m doing my part to get the word out. And no, I have no connection whatsoever with Parse.com other than being a fan.)
Stored Procedure to Display Row Counts for all Tables in any SQL Server Database
Using the technique I discussed in this post, the following SQL Server stored procedure can be used to display the number of rows in each table and used/unused space for all tables in a specified database. Put the stored procedure into whatever database you use for your Utilities database, then run the procedure from anywhere, passing in the name of the database for which to display row counts.
Note: The contents of the “@sql” SQL Query used inside this stored procedure is from the StackOverflow.com answer by “marc_s” on Oct 25 ’11 (edited Oct 29 ’13) to the question “Get size of all tables in database“. My contribution was simply to turn that query into a stored procedure that could access any database and to add the “Grand Total” values.