@pg-nano/plugin-crud
v0.1.0-beta.6
Published
This plugin generates CRUD routines for Postgres tables.
Downloads
81
Readme
@pg-nano/plugin-crud
This plugin generates CRUD routines for Postgres tables.
Usage
import crud from '@pg-nano/plugin-crud'
export default defineConfig({
plugins: [
crud()
],
})
Generated routines
If you have a table named widget
with a primary key of id INT
, the following routines are generated by this plugin:
// Get a widget by ID
function getWidget(id: number): Promise<Widget>
// Create a new widget
function createWidget(data: Widget.InsertParams): Promise<Widget>
// Create a new widget with explicit ID or replace an existing one
function upsertWidget(data: Widget.UpsertParams): Promise<Widget>
// Update a widget by ID
function updateWidget(id: number, data: Partial<Widget>): Promise<Widget>
// Delete a widget by ID
function deleteWidget(id: number): Promise<boolean>
Notes
Composite primary keys
Composite primary keys are supported.
Overriding generated routines
If you declare a function of the same name as any of the generated routines, your implementation will be used instead.
In other words, you can override the generated routine by defining a function of the same name.
Caveats
Null coercion when inserting
If a column has a default value, and you pass null
to it when inserting (or upserting), the default value will be used instead. This is because the generated routine doesn't currently distinguish an intentional null
from an omission.
Updates are not affected by this limitation.
Compatible table names
The table name is used in the generated JavaScript function names, so it should be a valid JavaScript identifier.
License
MIT