Yet another SQL builder for JavaScript and Postgres. The API was designed to be very similar to writing plain SQL with almost no abstraction layers, while still maintaining some conveniences.
npm install pg-scriptYet another SQL builder for JavaScript and Postgres.
The API was designed to be very similar to writing plain SQL with almost no abstraction layers, while still maintaining some conveniences.
```
npm install pg-script
`typescript
import { Pool } from 'pg'
import { DatabasePool } from 'pg-script'
const connectionString = process.env.DATABASE_URL
const pool = new Pool({ connectionString })
const db = new DatabasePool(pool)
async function main () {
const status = 'published'
// Run the query
const { rows } = await db
.SELECTid, titleposts
.FROMstatus = ${status}
.WHEREpublish_date DESC
.ORDER_BY
// Just generate the SQL and the parameters
const [sql, params] = db
.SELECTid, titleposts
.FROMstatus = ${status}
.WHEREpublish_date DESC
.ORDER_BY
.toSql()
// Just generate the SQL
const sql = db
.SELECTid, titleposts
.FROMstatus = ${status}
.WHEREpublish_date DESC
.ORDER_BY`
.toString()
}
`sql`
SELECT id, title FROM "posts" WHERE status = $1 ORDER BY publish_date DESC
`typescript
const status = 'published'
db
.SELECTid, titleposts
.FROMstatus = ${status}
.WHEREpublish_date DESC
.ORDER_BY
// OR
db
.SELECTid, titleposts
.FROMpublish_date DESC
.WHERE({ status: 'published' })
.ORDER_BY`
`sql`
UPDATE "posts" SET status = $1 WHERE id = $2
`typescriptposts
db
.UPDATEstatus = ${status}
.SETid = ${id}
.WHERE`
`typescriptposts
db
.UPDATE`
.SET({ status: 'published' })
.WHERE({ id: postId })
`sql`
DELETE FROM "posts" WHERE id = $1
`typescriptposts
db
.DELETE_FROMid = ${id}
.WHEREtitle
.RETURNING`
`sql`
INSERT INTO "posts" ("title", "status") VALUES ($1, $2) RETURNING id
`typescriptposts
db
.INSERT_INTOid
.VALUES({
title: 'Hello, world',
status: 'published'
})
.RETURNING`
`sql`
SELECT id, name FROM "users" WHERE (EXISTS (SELECT id FROM "posts" WHERE author_id = users.id)) ORDER BY name ASC
`typescriptid, name
db
.SELECTusers
.FROMid
.WHERE(EXISTS(SELECT.FROMposts.WHEREauthor_id = users.id))name ASC
.ORDER_BY`
Without helper methods, running the query returns the default pg result:
`typescriptid, name
// Destructuring rows
const { rows, rowCount } = await db
.SELECTuser
.FROM
// Destructuring the first row
const { rows: [firstRow] } = await db
.SELECTnameuser
.FROMid = ${userId}
.WHERE`
Helper methods are available to reduce the need for destructuring for common use cases.
Returns the first result. If there is none, throws an error. Accepts an optional error message.
`typescriptname
const row = await db
.SELECTuser
.FROMid = ${userId}
.WHEREUser with id ${userId} not found
.find({ error: })`
Returns the first result. If there is none, returns null instead.
`typescriptname
const row = await db
.SELECTuser
.FROMid = ${userId}
.WHERE`
.first()
Returns the rows of the result.
`typescriptid, name
const rows = await db
.SELECTuser
.FROM`
.list()
Returns the rows along with a count. Useful for pagination.
`typescript
const pageNumber = 0
const pageSize = 10
const { rows, count } = await db
.SELECTid, nameuser
.FROM``
.page(pageNumber, pageSize)