How to use the objection.transaction.start function in objection

To help you get started, we’ve selected a few objection examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github SelfKeyFoundation / Identity-Wallet / src / main / identity / id-attribute.js View on Github external
static async delete(id) {
		const tx = await transaction.start(this.knex());
		try {
			let attr = await this.query(tx)
				.findById(id)
				.eager('documents');
			// TODO: fix for multiple documents
			if (attr.documents) await Promise.all(attr.documents.map(d => d.$query(tx).delete()));
			await attr.$query(tx).delete();
			await tx.commit();
			return attr;
		} catch (error) {
			await tx.rollback();
			throw error;
		}
	}
github elifesciences / elife-xpub / server / xpub-model / entities / manuscript / index.js View on Github external
async save() {
    const simpleSave = async (trx = null) => {
      // save manuscript and all relations
      // note that this also deletes any related entities that are not present
      await this.$query(trx).upsertGraphAndFetch(this)
      // reload related entities
      await this.$loadRelated('[teams, files, audits]', null, trx)
    }

    if (this.created && this.updated) {
      let trx
      try {
        trx = await transaction.start(BaseModel.knex())

        if (await this.needsRefresh(trx)) {
          logger.error(
            `Attempt to save Manuscript ${this.id} with updated=${
              this.updated
            }`,
          )
          // Temporarily commented out see #1162
          // throw integrityError(
          //   'updated',
          //   this.updated,
          //   'is older than the one stored in the database!',
          // )
        }

        await simpleSave(trx)
github CodeChain-io / codechain-keystore-server / bin / load.ts View on Github external
async function main() {
    const tx = await transaction.start(Model.knex());
    try {
        if (dbPath === undefined) {
            console.error("no db_path given!");
            process.exit(1);
        }

        const keystore = require(dbPath);

        for (const addr of keystore.platform) {
            const row = await KeyModel.query().findById([
                "platform",
                addr.address
            ]);
            if (row != null) {
                throw new Error(`Platform key ${addr.address} already exists!`);
            }
github SelfKeyFoundation / Identity-Wallet / src / main / wallet / wallet.js View on Github external
static async create(itm) {
		itm.publicKey = itm.publicKey.toLowerCase();
		const tx = await transaction.start(this.knex());
		try {
			let insertedItm = await this.query(tx).insertGraphAndFetch(
				{
					...itm,
					setting: {
						showDesktopNotification: 1
					},
					tokens: [
						{
							tokenId: config.constants.primaryToken === 'KEY' ? 1 : 2
						}
					]
				},
				{ relate: true }
			);
			await tx.commit();
github SelfKeyFoundation / Identity-Wallet / src / main / identity / ui-schema.js View on Github external
static async addRemote(url, repositoryId, attributeTypeId) {
		let [remote, attrType] = await Promise.all([
			this.loadRemote(url),
			this.findByUrl(url, repositoryId)
		]);
		if (!remote) {
			log.error('could not load ui schema %s from remote', url);
			return;
		}
		remote.repositoryId = repositoryId;
		remote.attributeTypeId = attributeTypeId;
		const tx = await transaction.start(this.knex());
		try {
			if (!attrType) {
				attrType = await this.create(remote, tx);
			} else {
				attrType = await attrType.$query(tx).patchAndFetch(remote);
			}

			await tx.commit();
			return attrType;
		} catch (error) {
			log.error(error);
			await tx.rollback(error);
			throw error;
		}
	}
}
github SelfKeyFoundation / Identity-Wallet / src / main / common / base-model.js View on Github external
static async queryMany(records, queryFn, externalTx) {
		const tx = externalTx || (await transaction.start(this.knex()));
		try {
			let results = await Promise.all(records.map(r => queryFn(r, tx)));
			if (!externalTx) await tx.commit();
			return results;
		} catch (error) {
			if (!externalTx) await tx.rollback();
			throw error;
		}
	}
}
github SelfKeyFoundation / Identity-Wallet / src / main / identity / repository.js View on Github external
static async addRemoteRepo(url) {
		let [remote, repo] = await Promise.all([this.loadRemote(url), this.findByUrl(url)]);
		if (!remote) {
			log.error('could not load repo %s from remote', url);
			return;
		}
		const tx = await transaction.start(this.knex());
		try {
			if (!repo) {
				repo = await this.create({ url, content: {} }, tx);
			}

			let updates = repo.diffAttributes(remote);

			await repo.updateAttributes(updates, tx);
			repo = await repo.$query(tx).patchAndFetch(remote);
			await tx.commit();
			return repo;
		} catch (error) {
			log.error(error);
			await tx.rollback(error);
			throw error;
		}
github SelfKeyFoundation / Identity-Wallet / src / main / identity / id-attribute-type.js View on Github external
static async addRemote(url) {
		let [remote, attrType] = await Promise.all([this.loadRemote(url), this.findByUrl(url)]);
		if (!remote) {
			log.error('could not load attribute type %s from remote', url);
			return;
		}
		const tx = await transaction.start(this.knex());
		try {
			if (!attrType) {
				attrType = await this.create(remote, tx);
			} else {
				attrType = await attrType.$query(tx).patchAndFetch(remote);
			}

			await tx.commit();
			return attrType;
		} catch (error) {
			log.error(error);
			await tx.rollback(error);
			throw error;
		}
	}
}