@lcdev/objection
v0.3.3
Published
Utilities for objection and knex
Downloads
3
Keywords
Readme
Objection Utilities
Provides some utility functions for common things we do with Objection.
yarn add @lcdev/objection@VERSION
Logging
Use addLogsToKnex
to include queries in your logger (@lcdev/logger
) output.
import { addLogsToKnex } from '@lcdev/objection';
// sets up logger to write queries as they happen
// usually, we'd do this during construction of the connection
addLogsToKnex(connection, logger);
Identifier Mapping
Use identifierMapping
to rename columns when parsing from database and when formatting into.
import { identifierMapping } from '@lcdev/objection';
class MyModel extends Model {
static tableName = 'some_table_name';
static columnNameMappers = identifierMapping({
x: 'primaryFeatureX',
y: 'primaryFeatureY',
});
...
}
Errors (Postgres)
Use postgresErrMiddleware
to catch specific postgres errors.
import { postgresErrMiddleware, PostgresErrors } from '@lcdev/objection';
// specific catch-all for reference errors
app.use(
postgresErrMiddleware({
[PostgresErrors.ForeignKeyViolation]: () =>
Promise.reject(
err(
400,
'A reference between items in the app would be broken with this action',
ApiErrorCode.FKError,
),
),
}),
);