JayData.org

CRUD with JayData in a content management like application

Author: Viktor Lazar April 30th, 2012

, , ,


CRUD (Create-Read-Update-Delete) applications are common on the web. Because of that, it helps a lot if you have a good database management framework in your hands. JayData is the best if you want to develop a client-side web application and don’t want to deal with server-side coding. If you have an OData ready database, you can reach it directly from the client-side with the OData provider which is a built-in feature in the JayData library.

newsreader

Problems of complex CRUD applications

In CRUD applications, you have to deal with complex data types and one-to-one, one-to-many and even many-to-many associations. Without a proper framework and no database programming experience, this can be a hard work and a pain while you want to create the application of your dreams.

With JayData the complexity of the problems are reducing significantly. You can grab the entity context definition with JaySvcUtil utility, which translates between OData metadata and JavaScript context definition. You can start work immediately after this procedure.

Filter and order by

The most common task in CRUD is filtering and ordering. Let’s see a part of the example application:

Here you can see a few common tasks already. The first is the filter-if issue. You want to filter by a certain field of the entity, but only if a condition is satisfied. You can achieve this by storing the query state in a local variable. Later, you can continue to expand the query with more functions. We order the query on the Title field (ascending order) and finally render the result set with a template engine (which is independent).

categories

Map

Here is an advanced query from the example code. We want to retrieve an article, and after the query completes, we want to get the tags associated to this exact article.

Using the map function, you can retrieve the entity data in a custom way. Here, we are only interested in the main fields of the entity. Mapping is currently restricted to single, non-hierarchical object definition. Because we wrote our template engine to handle callbacks, we can execute another query there. This is useful if you can’t retrieve the required information in a single query because of limitations.

editarticle

In the query on the TagConnections entity set, you can see a good example on mapping an associated entity to custom property names. This comes handy in the template engine.

Update and create new

Updating and creating articles in our example application is a complex task. We have to handle the TagConnections entity set, depending on the edited article’s changed (or unchanged) tags property. We remove all the TagConnections from the Article entity, connect new or existing Tags to it and finally remove all unnecessary tags, which don’t exist in the TagConnections entity set any more.

Creating a new article is the same as updating, but instead of querying the article we want to update, we simply create a new instance of an Article entity and add it to the Articles entity set of the entity context.

Delete

In the process of entity deletion, you have to be carful, because your entity could have dependencies. We start to delete an article by removing the TagConnections from it, then we actually delete the Article entity and finally (as in the update or create function) we remove the unnecessary Tags.

These examples are advanced use cases of JayData and the JavaScript Language Query (JSLQ). It’s highly recommended to debug the example step-by-step and try to extend the functionality of the application or write your own CRUD beauty. It will take a little bit of practice to get the feel of it, but if you have experience with database programming, you will see that this object-oriented way is easier to handle than writing complex SQL scripts and stored procedures, and has one more major advantage: it’s platform independent!


, , ,