【问题标题】:How to get ID of last inserted row using denodb?如何使用 denodb 获取最后插入行的 ID?
【发布时间】:2022-01-02 11:43:00
【问题描述】:

我正在处理DenoDb 的工作,并试图使createCat 函数返回Promise<CatModel> 类型。 最好的方法是什么? 这是我当前的代码:

import { Model, Database, SQLite3Connector, DataTypes } from 'https://deno.land/x/denodb/mod.ts';

interface CatModel {
    id: bigint;
    name: string;
}

const connector = new SQLite3Connector({
  filepath: './db.sqlite',
});
const db = new Database(connector);

class CatSchema extends Model {
  static table = 'cats';
  static fields = {
      id: {
          type: DataTypes.BIG_INTEGER,
          primaryKey: true,
          autoIncrement: true,
      },
      name: {
          type: DataTypes.STRING,
      }
  };
}

db.link([CatSchema]);
await db.sync({ drop: true });

const createCat = async (name: string): Promise<CatSchema> => {
    return await CatSchema.create({
        name: name
    });
};

const dataCat = await createCat("Three");
console.log(dataCat);

await db.close();

Deno.exit(1);

我正在尝试制作这样的功能:

const createCat = async (name: string): Promise<CatModel>

如何正确地将Promise&lt;CatSchema&gt; 转换为Promise&lt;CatModel&gt;? 我计划将来将DenoDB 的工作隐藏在CatsRepo 类中,只提供CatModel。这是一个好的解决方案吗?

【问题讨论】:

    标签: typescript deno denodb


    【解决方案1】:

    Model.create 返回一个对象,其中包含最后插入的行的 id 作为number,该属性称为lastInsertId。因为denodb返回一个number类型,所以使用bigint作为CatModel的id没有太大价值,所以你可以把它改成number,然后像这样修改你的函数:

    so-70550010.ts:

    import {
      Database,
      DataTypes,
      Model,
      SQLite3Connector,
    } from "https://deno.land/x/denodb@v1.0.40/mod.ts";
    
    class CatSchema extends Model {
      static table = "cats";
      static fields = {
        id: {
          type: DataTypes.BIG_INTEGER,
          primaryKey: true,
          autoIncrement: true,
        },
        name: {
          type: DataTypes.STRING,
        },
      };
    }
    
    interface CatModel {
      id: number;
      name: string;
    }
    
    async function createCat(name: string): Promise<CatModel> {
      const model = await CatSchema.create({ name });
      const id = model.lastInsertId as number;
      return { id, name };
    }
    
    async function main() {
      const connector = new SQLite3Connector({ filepath: ":memory:" });
      const db = new Database(connector);
      db.link([CatSchema]);
      await db.sync({ drop: true });
    
      for (const name of ["One", "Two", "Three"]) {
        const catModel = await createCat(name);
        console.log(catModel);
      }
    
      await db.close();
      await connector.close();
      Deno.exit();
    }
    
    if (import.meta.main) main();
    
    $ deno run so-70550010.ts
    { id: 1, name: "One" }
    { id: 2, name: "Two" }
    { id: 3, name: "Three" }
    

    如果您希望在使用 SQLite 时进行较低级别的控制,我建议您使用 https://deno.land/x/sqlite 而不是 denodb

    【讨论】:

    • 你认为通过find会更好吗? const createCat = async (name: string): Promise&lt;CatModel|null&gt; =&gt; { const result = await CatSchema.create({ name: name }); const id = result.lastInsertId as number; const data: CatSchema = await CatSchema.find(id); console.log(data); if (data.name !== name) { return null; } const model: CatModel = { id: data.id as number, name: data.name as string }; return model; };
    • 不,无需查询范围内已有的数据。
    猜你喜欢
    • 2019-11-21
    • 2023-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2023-03-29
    • 1970-01-01
    • 2014-09-10
    相关资源
    最近更新 更多