本文整理匯總了TypeScript中objection.transaction函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript transaction函數的具體用法?TypeScript transaction怎麽用?TypeScript transaction使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了transaction函數的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: async
router.patch('/persons/:id/upsert', async (req, res) => {
const graph = req.body;
// Make sure only one person was sent.
if (Array.isArray(graph)) {
throw createStatusCodeError(400);
}
// Make sure the person has the correct id because `upsertGraph` uses the id fields
// to determine which models need to be updated and which inserted.
graph.id = parseInt(req.params.id, 10);
// It's a good idea to wrap `upsertGraph` call in a transaction since it
// may create multiple queries.
const upsertedGraph = await transaction(Person.knex(), trx => {
return (
Person.query(trx)
// For security reasons, limit the relations that can be upserted.
.allowUpsert('[pets, children.[pets, movies], movies, parent]')
.upsertGraph(graph)
);
});
res.send(upsertedGraph);
});
示例2: function
const saveDocument = function(document, userId) {
return transaction(DocumentModel.knex(), function(trx) {
return DocumentModel.query(trx).upsertGraphAndFetch({
...document,
userId,
});
});
};
示例3: async
router.post('/persons/:id/movies', async (req, res) => {
// Inserting a movie for a person creates two queries: the movie insert query
// and the join table row insert query. It is wise to use a transaction here.
const movie = await objection.transaction(Person, async (Person) => {
const person = await Person
.query()
.findById(req.params.id);
if (!person) {
return throwNotFound();
} else {
return person
.$relatedQuery('movies')
.insert(req.body);
}
});
res.send(movie);
});
示例4: createArtist
startedOn: String
endedOn: String
names: [ArtistNameInput!]!
}
type Mutation {
createArtist(input: NewArtistInput!): Artist!
patchArtist(input: ArtistInput!): Artist!
}
`;
export const resolvers = {
Mutation: {
createArtist(_root: any, { input }: { input: INewArtistInput }): Promise<Artist> {
return transaction(Artist.knex(), (trx) => (
createArtistAndArtistNames(input, trx)
));
},
patchArtist(_root: any, { input }: { input: IArtistInput }): Promise<Artist> {
return transaction(Artist.knex(), (trx) => (
patchArtistAndArtistNames(input, trx)
));
},
},
};
export const createArtistAndArtistNames = async (
input: INewArtistInput,
trxOrKnex?: Transaction | knex,
): Promise<Artist> => {