Skip to content

Instantly share code, notes, and snippets.

@chriscarlsondev
Last active July 17, 2019 18:56
Show Gist options
  • Select an option

  • Save chriscarlsondev/87efc1995e478c1550f2bcb0a283c841 to your computer and use it in GitHub Desktop.

Select an option

Save chriscarlsondev/87efc1995e478c1550f2bcb0a283c841 to your computer and use it in GitHub Desktop.
Q&A for the module on Learning a New Codebase

How are the syntaxes async and await useful when writing JavaScript code?

The word “async” before a function means the function always returns a promise and wraps non-promises in it. The keyword await makes JavaScript wait until that promise settles and returns its result. Async Await makes execution sequential.

With respect to Knex, how does the transaction method relate to BEGIN and COMMIT syntax in PostgreSQL?

In PostgreSQL, BEGIN initiates a transaction block. All statements after a BEGIN command will be executed in a single transaction until an explicit COMMIT or ROLLBACK is given.

In Knex, all queries within a transaction are executed on the same database connection, and run the entire set of queries as a single unit of work. Any failure will mean the database will rollback any queries executed on that connection to the pre-transaction state. So the transaction method is effectively surrounding the SQL query with BEGIN/COMMIT so that if anything fails the database returns to the state it was before the SQL command was executed.

What is a "sequence table" in PostgreSQL?

CREATE SEQUENCE creates a new sequence number generator. This involves creating and initializing a new special single-row table with the name name. After a sequence is created, you can use nextval, currval, and setval functions to operate on the sequence.

What does RESTART IDENTITY CASCADE do?

Code Sample: TRUNCATE thingful_reviews, thingful_things, thingful_users RESTART IDENTITY CASCADE;

RESTART IDENTITY and CASCADE are paramaters for the TRUNCATE statement.

RESTART IDENITY automatically restarts sequences owned by columns of the truncated table(s). CASCADE automatically truncates all tables that have foreign-key references to any of the named tables, or to any tables added to the group due to CASCADE.

What does SELECT setval('blogful_users_id_seq', 1) do?

Makes it so that nextval will return 1 for sequence blogful_users_id_seq.

Can you find where treeize is being used?

Treeize turns flat associative data (as from SQL queries) or flat array-of-values data (as from CSV/excel) into deep API-ready object graphs. https://www.npmjs.com/package/treeize It's used in this application to take information about a Thing or a Review and serialize it before returning it as part of the response.

What is PostgreSQL SERIAL PRIMARY KEY?

PRIMARY KEY makes the column the primary key for the table.

By assigning the SERIAL pseudo-type to the id column, PostgreSQL will create a sequence object and set the next value generated by the sequence as the default value for the column. It will also add the NOT NULL constraint to the column because a sequence always generates an integer, which is a non-null value. It will then assign the owner of the sequence to the id column; as a result, the sequence object is deleted when the id column or table is dropped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment