| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | const sql = require('./sql'); | 
					
						
							| 
									
										
										
										
											2018-01-28 21:00:54 -05:00
										 |  |  | const source_id = require('./source_id'); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | 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) { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |     await addEntitySync("note_tree", noteTreeId, sourceId) | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 21:57:46 -05:00
										 |  |  | async function addNoteReorderingSync(parentNoteId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("note_reordering", parentNoteId, sourceId) | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  | async function addNoteHistorySync(noteRevisionId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("note_revisions", noteRevisionId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  | async function addOptionsSync(name, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("options", name, 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) { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |     await addEntitySync("note_images", noteImageId, sourceId); | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  | async function addAttributeSync(attributeId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("attributes", attributeId, sourceId); | 
					
						
							| 
									
										
										
										
											2018-01-09 22:09:45 -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", { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         entityName: entityName, | 
					
						
							|  |  |  |         entityId: entityId, | 
					
						
							|  |  |  |         syncDate: utils.nowDate(), | 
					
						
							| 
									
										
										
										
											2018-01-28 21:00:54 -05:00
										 |  |  |         sourceId: 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
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         await sql.execute("UPDATE options SET value = (SELECT MAX(id) FROM sync) WHERE 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  | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |       WHERE sync.entityName = '${entityName}'  | 
					
						
							|  |  |  |         AND sync.entityId NOT IN (SELECT ${entityKey} FROM ${entityName})`);
 | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function fillSyncRows(entityName, entityKey) { | 
					
						
							|  |  |  |     await cleanupSyncRowsForMissingEntities(entityName, entityKey); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const entityIds = await sql.getColumn(`SELECT ${entityKey} FROM ${entityName}`); | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const entityId of entityIds) { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |         const existingRows = await sql.getValue("SELECT COUNT(id) FROM sync WHERE entityName = ? AND entityId = ?", [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", { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |                 entityName: entityName, | 
					
						
							|  |  |  |                 entityId: entityId, | 
					
						
							|  |  |  |                 sourceId: "SYNC_FILL", | 
					
						
							|  |  |  |                 syncDate: utils.nowDate() | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 13:55:13 -05:00
										 |  |  | async function fillAllSyncRows() { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     await fillSyncRows("notes", "noteId"); | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |     await fillSyncRows("note_tree", "noteTreeId"); | 
					
						
							|  |  |  |     await fillSyncRows("note_revisions", "noteRevisionId"); | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     await fillSyncRows("recent_notes", "noteTreeId"); | 
					
						
							|  |  |  |     await fillSyncRows("images", "imageId"); | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |     await fillSyncRows("note_images", "noteImageId"); | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     await fillSyncRows("attributes", "attributeId"); | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2018-01-09 22:09:45 -05:00
										 |  |  |     addAttributeSync, | 
					
						
							| 
									
										
										
										
											2018-01-30 20:12:19 -05:00
										 |  |  |     addEntitySync, | 
					
						
							| 
									
										
										
										
											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
										 |  |  | }; |