當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript objection.transaction函數代碼示例

本文整理匯總了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);
  });
開發者ID:fl0w,項目名稱:objection.js,代碼行數:25,代碼來源:api.ts

示例2: function

const saveDocument = function(document, userId) {
  return transaction(DocumentModel.knex(), function(trx) {
    return DocumentModel.query(trx).upsertGraphAndFetch({
      ...document,
      userId,
    });
  });
};
開發者ID:carlospaelinck,項目名稱:publications-js,代碼行數:8,代碼來源:index.ts

示例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);
  });
開發者ID:johannessjoberg,項目名稱:objection.js,代碼行數:19,代碼來源:api.ts

示例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> => {
開發者ID:zaeleus,項目名稱:lp-api,代碼行數:31,代碼來源:Mutation.ts


注:本文中的objection.transaction函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。