![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
workers-qb
Advanced tools
Zero dependencies Query Builder for Cloudflare Workers
This module provides a simple standardized interface while keeping the benefits and speed of using raw queries over a traditional ORM.
workers-qb
is not intended to provide ORM-like functionality, rather to make it easier to interact with the database
from code for direct SQL access using convenient wrapper methods.
Currently, 2 databases are supported:
Read the documentation workers-qb.massadas.com!
npm install workers-qb --save
import { D1QB } from 'workers-qb'
export interface Env {
DB: any
}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const qb = new D1QB(env.DB)
const fetched = await qb.fetchOne({
tableName: 'employees',
fields: 'count(*) as count',
where: {
conditions: 'active = ?1',
params: [true],
},
})
return Response.json({
activeEmployees: fetched.results?.count || 0,
})
},
}
Remember to close the connection using ctx.waitUntil(qb.close());
or await qb.close();
at the end of your request.
You may also reuse this connection to execute multiple queries, or share it between multiple requests if you are using
a connection pool in front of your PostgreSQL.
You must also enable node_compat = true
in your wrangler.toml
You need to install node-postgres
:
npm install pg --save
import { PGQB } from 'workers-qb'
export interface Env {
DB_URL: string
}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const qb = new PGQB(env.DB_URL)
await qb.connect()
const fetched = await qb.fetchOne({
tableName: 'employees',
fields: 'count(*) as count',
where: {
conditions: 'active = ?1',
params: [true],
},
})
ctx.waitUntil(qb.close())
return Response.json({
activeEmployees: fetched.results?.count || 0,
})
},
}
const qb = new D1QB(env.DB)
const fetched = await qb.fetchOne({
tableName: 'employees',
fields: 'count(*) as count',
where: {
conditions: 'department = ?1',
params: ['HQ'],
},
})
console.log(`There are ${fetched.results.count} employees in the HR department`)
import { OrderTypes } from 'workers-qb'
const qb = new D1QB(env.DB)
const fetched = await qb.fetchAll({
tableName: 'employees',
fields: ['role', 'count(*) as count'],
where: {
conditions: 'department = ?1',
params: ['HR'],
},
groupBy: 'role',
orderBy: {
count: OrderTypes.DESC,
},
})
console.log(`Roles in the HR department:`)
fetched.results.forEach((employee) => {
console.log(`${employee.role} has ${employee.count} employees`)
})
import { Raw } from 'workers-qb'
const qb = new D1QB(env.DB)
const inserted = await qb.insert({
tableName: 'employees',
data: {
name: 'Joe',
role: 'manager',
department: 'store',
created_at: new Raw('CURRENT_TIMESTAMP'),
},
returning: '*',
})
console.log(inserted) // This will contain the data after SQL triggers and primary keys that are automated
import { Raw } from 'workers-qb'
const qb = new D1QB(env.DB)
const inserted = await qb.insert({
tableName: 'employees',
data: [
{
name: 'Joe',
role: 'manager',
department: 'store',
created_at: new Raw('CURRENT_TIMESTAMP'),
},
{
name: 'John',
role: 'employee',
department: 'store',
created_at: new Raw('CURRENT_TIMESTAMP'),
},
{
name: 'Mickael',
role: 'employee',
department: 'store',
created_at: new Raw('CURRENT_TIMESTAMP'),
},
],
})
const updated = await qb.update({
tableName: 'employees',
data: {
role: 'CEO',
department: 'HQ',
},
where: {
conditions: 'id = ?1',
params: [123],
},
})
console.log(`Lines affected in this query: ${updated.changes}`)
const deleted = await qb.delete({
tableName: 'employees',
where: {
conditions: 'id = ?1',
params: [123],
},
})
console.log(`Lines affected in this query: ${deleted.changes}`)
const created = await qb.createTable({
tableName: 'testTable',
schema: `
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
`,
ifNotExists: true,
})
const dropped = await qb.dropTable({
tableName: 'testTable',
})
FAQs
Zero dependencies Query Builder for Cloudflare Workers
The npm package workers-qb receives a total of 0 weekly downloads. As such, workers-qb popularity was classified as not popular.
We found that workers-qb demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.