(pg: PGliteWithExtensions)
| 25 | } |
| 26 | |
| 27 | async function startSyncToDatabase(pg: PGliteWithExtensions) { |
| 28 | // Check if there are any issues already in the database |
| 29 | const issues = await pg.query(`SELECT 1 FROM issue LIMIT 1`) |
| 30 | const hasIssuesAtStart = issues.rows.length > 0 |
| 31 | |
| 32 | let issueShapeInitialSyncDone = false |
| 33 | let commentShapeInitialSyncDone = false |
| 34 | let postInitialSyncDone = false |
| 35 | |
| 36 | if (!hasIssuesAtStart && !postInitialSyncDone) { |
| 37 | updateSyncStatus(`initial-sync`, `Downloading shape data...`) |
| 38 | } |
| 39 | |
| 40 | let postInitialSyncDoneResolver: () => void |
| 41 | const postInitialSyncDonePromise = new Promise<void>((resolve) => { |
| 42 | postInitialSyncDoneResolver = resolve |
| 43 | }) |
| 44 | |
| 45 | const doPostInitialSync = async () => { |
| 46 | if ( |
| 47 | !hasIssuesAtStart && |
| 48 | issueShapeInitialSyncDone && |
| 49 | commentShapeInitialSyncDone && |
| 50 | !postInitialSyncDone |
| 51 | ) { |
| 52 | postInitialSyncDone = true |
| 53 | updateSyncStatus(`initial-sync`, `Creating indexes...`) |
| 54 | await postInitialSync(pg) |
| 55 | postInitialSyncDoneResolver() |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | const issueUrl = new URL(`${ELECTRIC_URL}/v1/shape`) |
| 60 | if (ELECTRIC_SOURCE_SECRET) { |
| 61 | issueUrl.searchParams.set(`secret`, ELECTRIC_SOURCE_SECRET) |
| 62 | } |
| 63 | |
| 64 | // Issues Sync |
| 65 | const issuesSync = await pg.sync.syncShapeToTable({ |
| 66 | shape: { |
| 67 | url: issueUrl.toString(), |
| 68 | params: { |
| 69 | table: `issue`, |
| 70 | source_id: ELECTRIC_SOURCE_ID, |
| 71 | }, |
| 72 | }, |
| 73 | table: `issue`, |
| 74 | primaryKey: [`id`], |
| 75 | shapeKey: `issues`, |
| 76 | commitGranularity: `up-to-date`, |
| 77 | useCopy: true, |
| 78 | onInitialSync: async () => { |
| 79 | issueShapeInitialSyncDone = true |
| 80 | await pg.exec(`ALTER TABLE issue ENABLE TRIGGER ALL`) |
| 81 | doPostInitialSync() |
| 82 | }, |
| 83 | }) |
| 84 | issuesSync.subscribe( |
no test coverage detected