HTML5 Application Database Sync

I am currently working on a project that includes storing data in a SQL-Lite HTML5 database. I currently have a schema as follows (4 tables):

TransData: ----------------------------------------------------------- | TID | UserName | TransColor | ... | Date | Note | ----------------------------------------------------------- | 6 | Brendan | Red | ... | | | ----------------------------------------------------------- | 7 | Brendan | Red | ... | | 1 | ----------------------------------------------------------- FullData: ----------------------------------------------------------- | TID | UserName | TransColor | ... | Date | Note | ----------------------------------------------------------- | 1 | Brendan | Red | ... | | Start | ----------------------------------------------------------- | ... | Brendan | Red | ... | | | ----------------------------------------------------------- | 40 | Brendan | Red | ... | | End | ----------------------------------------------------------- SalamanderData: ---------------------------------------------------- | SID | SalamanderName | Length | ... | TID | ---------------------------------------------------- | 1 | Northern-Slimy | 16 | ... | 6 | ---------------------------------------------------- | 2 | Two-Lined | 26 | ... | 6 | ---------------------------------------------------- | 3 | Two-Lined | 12 | ... | 7 | ---------------------------------------------------- SalamanderData: ---------------------------------------------------- | SID | SalamanderName | Length | ... | TID | ---------------------------------------------------- | 1 | Northern-Slimy | 16 | ... | 6 | ---------------------------------------------------- | 2 | Two-Lined | 26 | ... | 6 | ---------------------------------------------------- | 3 | Two-Lined | 12 | ... | 7 | ---------------------------------------------------- 

Note. The Note column in TransData is used to indicate the starting point of the collection data in the FullData field.

The database between my application and the server SHOULD NOT BE IN SYNC. I'm just trying to dump all these tables into the database on the server (and by dump I mean, update links to other tables, and then insert them into the server database).

I was going to use MAX(TID-Server) + TID-App = new TID-Server and cascade updates down the tables.

How would you do that?

+7
source share
3 answers

From Dan Pichelman's comment, the problem is that the client inserts the records into the local database and for this it must determine the primary keys for them. But, considering that all different clients do the same, new PCs will collide when they hit the server.

This is a common problem - these are systems that are physically disabled (at least sometimes) or where there cannot be a single point of failure, for example, in a generator with a common sequence.

Some common solutions:

GUID

Here PK is a 128-bit (or more) random number. The likelihood that any two PCs are the same is extremely small. But to reduce collision changes even further, the GUID algorithm includes seeding with unique machine identifiers (MACs) and time. Two GUIDs created on the same machine will never collide, nor will GUIDs be displayed on machines with different MAC addresses. Most machines and languages ​​have their own functions for generating GUIDs, but JavaScript does not. Cm:

Section naming scheme

In this scheme, PK is again a large number (bit-bit really), and you break it down into a hierarchical one. A good example is the international telephone system (at least to portable numbers). Here the phone number is divided by:

  • Country code: for example, USA - 1
  • Area code: for example, Sunnyvale - 615
  • Subscriber number controlled by the exchange.

In your case, you can split the number into:

  • User login (for example, a unique number for each user)
  • Session identifier (for example, a number unique to each login per user to distinguish between different sessions of the same user, but in different browsers / computers).
  • Serial number

Combining all three, you will have a guaranteed unique PC.

PK License Server

The first two sentences have the advantage of being completely disabled. If you have a connected client, you may have a web service that supplies a PC when the client requests them.

For efficiency, he can return a batch of, say, 100 numbers. It can even be returned when the user logs in.

The client can use them all and request more. There are times when a client forgets the state and leaves a “hole” in the global sequence of the PC. This will almost certainly not be a problem.

Some considerations

Sometimes you may need sequential PKs for the purpose of organizing tables. In this case, you need to order by the customer or at the time of creation? If this is important, you can evaluate the section naming scheme above. Put the client or time as the first section, if necessary. Also add more columns to the table.

Unless you need a fixed partition naming scheme structure, a GUID will work well.

If you need central coordination, use a PC license server.

+6
source

I don’t know an aesthetically pleasing way to do this, but I “solved” it by writing stored procedures:

The first table is simple - update or paste as needed. If you do an insert, get the primary key of the newly inserted record, then process the dependent tables accordingly (which usually means inserting data with a new primary key). Repeat as necessary when you go through a relationship. For 4 tables, you should be fine, but I would not want to do this for 40.

In my case, it was dirty and included temporary lookup tables with oldPK and newPK in them.

It was also a rather long and tedious bit of code, the only function of which is that it works.

0
source

This is a little contrary to what you requested, so just comment if it is completely untrue and I will remove it. But you did not indicate any special reasons why the server / client cannot synchronize the main key (which, in my opinion, is really a problem here).

I also make the assumption (from your data and your question) that we are talking about webapp with user-created content (for example, a log) that is uploaded to the server from time to time.

So, as far as sports are concerned, do you consider using a primary key that is created by several fields? Thus, you can have an automatic increase in the local database, dumping data to the server and will not interfere with other user data. An example of a SalamanderData table might be something in a row:

 CREATE TABLE SalamanderData ( SID int NOT NULL, SalamanderName varchar(255), Length int, ... ..., TID int NOT NULL, CONSTRAINT pk_SDataEntry PRIMARY KEY (SID,TID) ) 

What then would create PK from SID and TID.

Reading my post and checking out the other answers, I understand that this is what Andrew suggested, so even if that helps you, you should really accept his answer

keeping the answer in order to possibly clarify the solution and give an example of code

0
source

All Articles