• Products and Services
    • Products
    • Services
    • Solutions / Projects
    • Support
  • Download
  • Blog
  • Company
    • Team
    • For investors
    • Press
  • Community
  • Career
  • Contact
  • Products and Services
    • Products
    • Services
    • Solutions / Projects
    • Support
  • Download
  • Blog
  • Company
    • Team
    • For investors
    • Press
  • Community
  • Career
  • Contact

Blog

  • You are here:
  • Home
  • Configuring foreign keys for online-offline sync
Blog categories
  • AngularJS
  • CorpJS
  • JavaScript
  • JayData
  • JayStack OData V4 Server
  • Microservices
  • molinio
  • MongoDB
  • Node.js
  • OData
  • ReactJS
  • TypeScript
Tag Cloud
.NET Core AngularJS asp.net core C# CorpJS dotnet Edm decorator EF EF Core Entity Framework Core examples extension GeoJSON Global query filters html5 hybrid app IndexedDb IndexedDb transaction indices indices sqlite javascipt data management JayData jaydata pro JaySvcUtil Kendo UI MEAN Stack membership levels Microsoft Dynamics CRM modules molin.io odata Queryable DB react RSS Ruby on Rails SAP soft-delete sql sqlite database transactions in SQlite tutorial TypeScript WebSQL

Configuring foreign keys for online-offline sync

  • On 25th February 2014
  • Posted by admin
  • JayData, odata

As you might know, JayData supports saving and querying related entities without joining the tables on foreign keys manually. If you haven’t tried it, start with JayData and relationship article.

This article will demonstrate the way to configure the name of foreign keys in the relationships.

Continuing the Northwind example in the mention article, you create a simple 1:N relationship between product and categories using the following JayData model definition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var Category = $data.Entity.extend('Category', {
    CategoryId: { key: true, type: "int", nullable: false, computed: true },
    Name: { type: "string", required: true },
    Products: { type: Array, elementType: "Product", inverseProperty: "Category" }
});
 
var Product = $data.Entity.extend('Product', {
    ProductId: { key: true, type: 'int', nullable: false, computed: true },
    Name: { type: 'string', nullable: false, required: true, maxLength: 50 },
    Category: { type: 'Category', inverseProperty: 'Products' },
});
 
var Northwind = $data.EntityContext.extend('Northwind', {
    Categories: { type: $data.EntitySet, elementType: Category },
    Products: { type: $data.EntitySet, elementType: Product }
});

After saving your entities using this data model, you end up with the database that has a Products table automatically filled Category__CategoryId column.

image

Although the auto-created foreign key is convenient, this behavior could make the development more difficult if you have an online-offline mobile application and want to implement data synchronization – for example over OData.

The configuration of foreign keys are very easy to change Category__CategoryId to the desired CategoryId column name – the definition of parent entity must be extended with the keys array property.

1
2
3
4
5
var Product = $data.Entity.extend('Product', {
    ProductId: { key: true, type: 'int', nullable: false, computed: true },
    Name: { type: 'string', nullable: false, required: true, maxLength: 50 },
    Category: { type: 'Category', inverseProperty: 'Products', keys: ['CategoryId'] },
});

After modifying the data model and re-creating your database you will have a brand-new schema with your preferred foreign key.

image

Compatible versions: JayData 1.3.6+, JayData Pro 1.3.6+

Compatible storage providers: JayData API features (see the include/extend row)

Source: JayData.org

0 Comments

Synchronizing changed entities in online-offline scenarios

Building Web Apps on the MEAN Stack with OData in Microsoft Azure

Scroll
  • Home
    • Company
    • Career
    • Press
  • Products
    • JayData
    • molinio
    • Support
    • License
  • Community
    • Blog
    • CorpJS
Latest Blogposts
  • Getting familiar with Entity Framework Core 2.0: Global query filters

  • JayStack OData V4 Server – Type Definitions and Enums

  • Repatch – the simplified Redux

  • CorpJS Budapest #8

  • Creating Todo application from Molinio templates

Go to Blog!

  • Facebook
  • Linkedin
  • Rss
  • Twitter
  • Youtube
  • Github
  • Privacy Policy
  • Terms of Service
  • Give Us Feedback!
Copyright JayStack Technologies LLC. 2015. All Rights Reserverd. Non-JayStack logos/trademarks are copyright to their respective owners and do not constitute an endorsement of any JayStack products.

We are using cookies to give you the best experience on our website.

You can find out more about which cookies we are using or switch them off in settings.

JayStack
Powered by GDPR plugin

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.

3rd Party Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

Please enable Strictly Necessary Cookies first so that we can save your preferences!