| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | const sql = require('./sql'); | 
					
						
							|  |  |  | const source_id = require('./source_id'); | 
					
						
							|  |  |  | const utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2017-12-13 23:03:48 -05:00
										 |  |  | const sync_setup = require('./sync_setup'); | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | const log = require('./log'); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  | async function addNoteSync(noteId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("notes", noteId, sourceId) | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  | async function addNoteTreeSync(noteTreeId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("notes_tree", noteTreeId, sourceId) | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  | async function addNoteReorderingSync(parentNoteTreeId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("notes_reordering", parentNoteTreeId, sourceId) | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  | async function addNoteHistorySync(noteHistoryId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("notes_history", noteHistoryId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  | async function addOptionsSync(optName, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("options", optName, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-30 21:44:26 -05:00
										 |  |  | async function addRecentNoteSync(noteTreeId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("recent_notes", noteTreeId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-06 15:56:00 -05:00
										 |  |  | async function addImageSync(imageId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("images", imageId, sourceId); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  | async function addNoteImageSync(noteImageId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("notes_image", noteImageId, sourceId); | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  | async function addEntitySync(entityName, entityId, sourceId) { | 
					
						
							| 
									
										
										
										
											2017-12-14 07:57:31 -05:00
										 |  |  |     await sql.replace("sync", { | 
					
						
							|  |  |  |         entity_name: entityName, | 
					
						
							|  |  |  |         entity_id: entityId, | 
					
						
							|  |  |  |         sync_date: utils.nowDate(), | 
					
						
							| 
									
										
										
										
											2017-12-23 13:55:13 -05:00
										 |  |  |         source_id: sourceId || source_id.getCurrentSourceId() | 
					
						
							| 
									
										
										
										
											2017-12-14 07:57:31 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!sync_setup.isSyncSetup) { | 
					
						
							|  |  |  |         // this is because the "server" instances shouldn't have outstanding pushes
 | 
					
						
							|  |  |  |         // useful when you fork the DB for new "client" instance, it won't try to sync the whole DB
 | 
					
						
							| 
									
										
										
										
											2017-12-15 21:14:10 -05:00
										 |  |  |         await sql.execute("UPDATE options SET opt_value = (SELECT MAX(id) FROM sync) WHERE opt_name IN('last_synced_push', 'last_synced_pull')"); | 
					
						
							| 
									
										
										
										
											2017-12-13 20:34:21 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | async function cleanupSyncRowsForMissingEntities(entityName, entityKey) { | 
					
						
							|  |  |  |     await sql.execute(`
 | 
					
						
							|  |  |  |       DELETE  | 
					
						
							|  |  |  |       FROM sync  | 
					
						
							|  |  |  |       WHERE sync.entity_name = '${entityName}'  | 
					
						
							|  |  |  |         AND sync.entity_id NOT IN (SELECT ${entityKey} FROM ${entityName})`);
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function fillSyncRows(entityName, entityKey) { | 
					
						
							|  |  |  |     await cleanupSyncRowsForMissingEntities(entityName, entityKey); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |     const entityIds = await sql.getFirstColumn(`SELECT ${entityKey} FROM ${entityName}`); | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const entityId of entityIds) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         const existingRows = await sql.getFirstValue("SELECT COUNT(id) FROM sync WHERE entity_name = ? AND entity_id = ?", [entityName, entityId]); | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // we don't want to replace existing entities (which would effectively cause full resync)
 | 
					
						
							|  |  |  |         if (existingRows === 0) { | 
					
						
							|  |  |  |             log.info(`Creating missing sync record for ${entityName} ${entityId}`); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             await sql.insert("sync", { | 
					
						
							|  |  |  |                 entity_name: entityName, | 
					
						
							|  |  |  |                 entity_id: entityId, | 
					
						
							|  |  |  |                 source_id: "SYNC_FILL", | 
					
						
							|  |  |  |                 sync_date: utils.nowDate() | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 13:55:13 -05:00
										 |  |  | async function fillAllSyncRows() { | 
					
						
							|  |  |  |     await fillSyncRows("notes", "note_id"); | 
					
						
							|  |  |  |     await fillSyncRows("notes_tree", "note_tree_id"); | 
					
						
							|  |  |  |     await fillSyncRows("notes_history", "note_history_id"); | 
					
						
							|  |  |  |     await fillSyncRows("recent_notes", "note_tree_id"); | 
					
						
							| 
									
										
										
										
											2018-01-06 15:56:00 -05:00
										 |  |  |     await fillSyncRows("images", "image_id"); | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  |     await fillSyncRows("notes_image", "note_image_id"); | 
					
						
							| 
									
										
										
										
											2017-12-23 13:55:13 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     addNoteSync, | 
					
						
							|  |  |  |     addNoteTreeSync, | 
					
						
							|  |  |  |     addNoteReorderingSync, | 
					
						
							|  |  |  |     addNoteHistorySync, | 
					
						
							|  |  |  |     addOptionsSync, | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  |     addRecentNoteSync, | 
					
						
							| 
									
										
										
										
											2018-01-06 15:56:00 -05:00
										 |  |  |     addImageSync, | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  |     addNoteImageSync, | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  |     cleanupSyncRowsForMissingEntities, | 
					
						
							| 
									
										
										
										
											2017-12-23 13:55:13 -05:00
										 |  |  |     fillAllSyncRows | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | }; |