本文整理汇总了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> => {