有没有办法在TypeORM中访问`last_inserted_id`?

时间:2017-11-21 19:06:40

标签: node.js express typeorm

具体来说,有没有办法在TypeORM事务中访问last_inserted_id?即:

 try {
        // transaction
        await getManager().transaction(async (trManager): Promise<any> => {

            const company = new Company();
            const savedCompany = await trManager.save(company);
            const companyId = savedCompany.lastInsertedId;

            // ..............more saves..............//

            // await trManager.save(otherEntityUsingCompanyId);

        });
    } catch (err) {
        console.error("err: ", err);         
    }

我彻底查看了文档(诚然,如果我错过了某些内容,也许还不够彻底)并且没有看到任何内容。我发现的最接近的文档看起来很相似:

const userId = manager.getId(user); // userId === 1

这似乎是一个常见的用例,我假设我错过了一些东西,这就是为什么我犹豫是否提出问题。任何帮助,将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:4)

想出来。使用returning方法...

const inserts = await getConnection()
            .createQueryBuilder()
            .insert()
            .into(Company)
            .values([
                { Name: "abcdef", Address: "456 denny lane" }, 
                { Name: "ghijkil", Address: "899 ihop lane" }
            ])
            .returning("INSERTED.*")
            .printSql()
            .execute();

// `inserts` holds array of inserted objects

答案 1 :(得分:1)

library(vegan)
#> Loading required package: permute
#> Loading required package: lattice
#> This is vegan 2.5-5

# Make metadata ----------------------------------------------------------------
metadata <- cbind.data.frame(
  FishID = as.character(rep(1:36, each = 2)),
  NetPen = rep(c("NP1", "NP2", "NP3", "NP4", "NP5", "NP6"), each = 12),
  Diet = rep(c("Ref", "Soy"), each = 36),
  Origin = rep(c("Mucosa", "Contents"), times = 36)) 

# Make feature table -----------------------------------------------------------
set.seed(1910)

ref <- data.frame(replicate(100,sample(0:10, 36, rep = TRUE)))
soy <- data.frame(replicate(100,sample(0:20, 36, rep = TRUE)))
table <- rbind(ref, soy)

# Nested PERMANOVA -------------------------------------------------------------
# Calculate distance metrix
dist_bray <- vegdist(table, method = "bray")

# Define permutation scheme 
perm <- how(nperm  = 999, 
            within = Within(type = "free"),
            plots  = with(metadata, Plots(strata = NetPen, type = "free")))    

# Nested PERMANOVA 
adonis2(dist_bray ~ Diet/NetPen/FishID + Origin + Diet:Origin, data = metadata, permutations = perm)
#> Permutation test for adonis under reduced model
#> Terms added sequentially (first to last)
#> Plots: NetPen, plot permutation: free
#> Permutation: free
#> Number of permutations: 999
#> 
#> adonis2(formula = dist_bray ~ Diet/NetPen/FishID + Origin + Diet:Origin, data = metadata, permutations = perm)
#>                    Df SumOfSqs      R2       F Pr(>F)  
#> Diet                1   1.5080 0.25271 23.6485  0.033 *
#> Origin              1   0.0738 0.01237  1.1573  0.172  
#> Diet:NetPen         4   0.2088 0.03500  0.8187  0.925  
#> Diet:Origin         1   0.0584 0.00978  0.9155  0.696  
#> Diet:NetPen:FishID 30   1.9502 0.32682  1.0195  0.354  
#> Residual           34   2.1680 0.36333                 
#> Total              71   5.9672 1.00000                 
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

对于MySql,您可以从结果中获取last_insert_id。看起来像下面。

OUTPUT or RETURNING clause only supported by Microsoft SQL Server or PostgreSQL databases.