Skip to content

Server Actions and this Keyword #74124

Answered by maciej-ka
jintaokoong asked this question in Help
Discussion options

You must be logged in to vote

SQL insert has an optional RETURNING * part.
It will return data of just created row(s).
And its supported also in sqlite.

Here is createItem rewriten to use it:

export const createItem = async (title: string) => {
  return new Promise<number>((resolve, reject) => {
    db.serialize(() => {
      db.all(
        `
            INSERT INTO items (title) VALUES ($title) RETURNING id;
        `,
        {
          $title: title
        },
        function (err, rows: {id: number}[]) {
          if (err) {
            reject(err);
          }
          resolve(rows[0].id);
        }
      )
    })
  });
};

Replies: 5 comments 18 replies

Comment options

You must be logged in to vote
1 reply
@jintaokoong
Comment options

Comment options

You must be logged in to vote
7 replies
@maciej-ka
Comment options

@icyJoseph
Comment options

@maciej-ka
Comment options

@icyJoseph
Comment options

@jintaokoong
Comment options

Comment options

You must be logged in to vote
4 replies
@maciej-ka
Comment options

@icyJoseph
Comment options

@jintaokoong
Comment options

@icyJoseph
Comment options

Comment options

You must be logged in to vote
2 replies
@jintaokoong
Comment options

@maciej-ka
Comment options

Answer selected by jintaokoong
Comment options

You must be logged in to vote
4 replies
@unstubbable
Comment options

@maciej-ka
Comment options

@unstubbable
Comment options

@maciej-ka
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
4 participants