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 {
source: 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 }) {
this.source = getPouchDB(source)
this.target = getPouchDB(target)
}
close() {
return Promise.all([closePouchDB(this.source), closePouchDB(this.target)])
async close() {
await Promise.all([closePouchDB(this.source), closePouchDB(this.target)])
}
promisify(operation: any, opts = {}) {
return new Promise(resolve => {
operation(this.target, opts)
.on("denied", function (err: any) {
replicate(opts: PouchDB.Replication.ReplicateOptions = {}) {
return new Promise<PouchDB.Replication.ReplicationResult<{}>>(resolve => {
this.source.replicate
.to(this.target, opts)
.on("denied", function (err) {
// a document failed to replicate (e.g. due to permissions)
throw new Error(`Denied: Document failed to replicate ${err}`)
})
.on("complete", function (info: any) {
.on("complete", function (info) {
return resolve(info)
})
.on("error", function (err: any) {
.on("error", function (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(
opts: PouchDB.Replication.ReplicateOptions = {}
): PouchDB.Replication.ReplicateOptions {

View File

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