Skip to content

Instantly share code, notes, and snippets.

@BlairCurrey
Last active August 16, 2024 19:09
Show Gist options
  • Save BlairCurrey/ece2557b75487fc2bec6c006526b0324 to your computer and use it in GitHub Desktop.
Save BlairCurrey/ece2557b75487fc2bec6c006526b0324 to your computer and use it in GitHub Desktop.
// Illustrates how calling knex.query() can return a query builder while
// await knex.query() (or knex.query().then(...)) execute the query.
// Basically, the answer is `then()`
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
// Stack Overflow post asking the same thing:
// - https://stackoverflow.com/questions/65319822/how-does-knex-await-execute-a-database-query
class QueryBuilder {
createTable(tableName) {
// just return the query builder for example purposes
return this;
}
then(resolve) {
// simulates table creation
setTimeout(() => {
resolve("Table created successfully");
}, 1000);
}
}
class Knex {
constructor() {
this.schema = new QueryBuilder();
}
}
// Usage Example
const knex = new Knex();
async function main() {
const nonAwaited = knex.schema.createTable("users");
console.log("non awaited result:", nonAwaited);
const awaited = await knex.schema.createTable("users");
console.log("Awaited result:", awaited);
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment