Fix the TODO I left myself last night.

This commit is contained in:
Sam Rose 2024-03-06 10:00:02 +00:00
parent 11704ea983
commit 2b206f2105
No known key found for this signature in database
2 changed files with 23 additions and 43 deletions

View File

@ -5,56 +5,33 @@ import { DocumentType } from "../constants"
class Replication { class Replication {
source: PouchDB.Database source: PouchDB.Database
target: PouchDB.Database target: PouchDB.Database
replication?: Promise<any>
/**
*
* @param source - the DB you want to replicate or rollback to
* @param target - the DB you want to replicate to, or rollback from
*/
constructor({ source, target }: { source: string; target: string }) { constructor({ source, target }: { source: string; target: string }) {
this.source = getPouchDB(source) this.source = getPouchDB(source)
this.target = getPouchDB(target) this.target = getPouchDB(target)
} }
close() { async close() {
return Promise.all([closePouchDB(this.source), closePouchDB(this.target)]) await Promise.all([closePouchDB(this.source), closePouchDB(this.target)])
} }
promisify(operation: any, opts = {}) { replicate(opts: PouchDB.Replication.ReplicateOptions = {}) {
return new Promise(resolve => { return new Promise<PouchDB.Replication.ReplicationResult<{}>>(resolve => {
operation(this.target, opts) this.source.replicate
.on("denied", function (err: any) { .to(this.target, opts)
.on("denied", function (err) {
// a document failed to replicate (e.g. due to permissions) // a document failed to replicate (e.g. due to permissions)
throw new Error(`Denied: Document failed to replicate ${err}`) throw new Error(`Denied: Document failed to replicate ${err}`)
}) })
.on("complete", function (info: any) { .on("complete", function (info) {
return resolve(info) return resolve(info)
}) })
.on("error", function (err: any) { .on("error", function (err) {
throw new Error(`Replication Error: ${err}`) throw new Error(`Replication Error: ${err}`)
}) })
}) })
} }
/**
* Two way replication operation, intended to be promise based.
* @param opts - PouchDB replication options
*/
sync(opts: PouchDB.Replication.SyncOptions = {}) {
this.replication = this.promisify(this.source.sync, opts)
return this.replication
}
/**
* One way replication operation, intended to be promise based.
* @param opts - PouchDB replication options
*/
replicate(opts: PouchDB.Replication.ReplicateOptions = {}) {
this.replication = this.promisify(this.source.replicate.to, opts)
return this.replication
}
appReplicateOpts( appReplicateOpts(
opts: PouchDB.Replication.ReplicateOptions = {} opts: PouchDB.Replication.ReplicateOptions = {}
): PouchDB.Replication.ReplicateOptions { ): PouchDB.Replication.ReplicateOptions {

View File

@ -107,17 +107,20 @@ export async function save(ctx: UserCtx<SaveRoleRequest, SaveRoleResponse>) {
role._rev = result.rev role._rev = result.rev
ctx.body = role ctx.body = role
// TODO: need to check that the prod DB actually exists, I think it won't const devDb = context.getDevAppDB()
// if the app has never been published. const prodDb = context.getProdAppDB()
const replication = new dbCore.Replication({
source: context.getDevAppDB().name, if (await prodDb.exists()) {
target: context.getProdAppDB().name, const replication = new dbCore.Replication({
}) source: devDb.name,
await replication.replicate({ target: prodDb.name,
filter: (doc: any, params: any) => { })
return doc._id === _id await replication.replicate({
}, filter: (doc: any, params: any) => {
}) return doc._id === _id
},
})
}
} }
export async function destroy(ctx: UserCtx<void, DestroyRoleResponse>) { export async function destroy(ctx: UserCtx<void, DestroyRoleResponse>) {