com.siteblade.database.orm v1.0.21
com.siteblade.database.orm
Database ORM featuring:
- Typed Entity properties
- Support for (flags) enum classes via com.siteblade.util
Getting started
This is what an entity looks like:
src/entities/User.js
Connecting to database:
Migrations and seeds
This package contains a CLI which supports entity migrations and seeds. For one application to work, it must apply the corresponding migrations.
To install the CLI, run:
$ npm install -g com.siteblade.database.orm
To use the CLI, you'll need a configuration file. For this you can run dborm init
on your project.
The following command will create a migration based on one Entity class.
$ dborm g migration -c MyEntity
The copy option (-c) may be repeated. Note that every specified copy is equivalent to table creation and not table alteration. Table alteration must be currently manually scripted into a migration using the MigrationContext.
TIP A migration can be renamed in the format
0.someLabel.js
where 0 is the migration number.
The following commands will apply specific migrations:
$ dborm migrate 0
$ dborm migrate:latest
$ dborm migrate:rollback
API
A corresponding JSDoc tool is on work yet.
Adding, finding and paginating
import { OrderBy, ItemsPerPage, Comparison } from 'com.siteblade.databaseorm';
// all descending entities
await MyEntity.all( OrderBy('updatedAt', 'desc') );
// ascending entities with (x, y)
await MyEntity.find( { x, y, }, OrderBy('updatedAt', 'asc') );
// first page
let p = await MyEntity.page( 0, ItemsPerPage(10) );
console.log( p.total, p.pageNumber, p.results );
console.log( p.toJSON() );
// paginating with constraints
await MyEntity.page( 0, { x, y } );
// delete where...
await MyEntity.deleteWhere( { x, y } );
// adding Entity data
await MyEntity.add( { x, y } );
// Comparison constraints
await MyEntity.find( { x: Comparison('>', 10) } );
Miscellanous
await entity.delete();
// average
await MyEntity.average( 'x' );
// average with constraints
await MyEntity.average( 'x', { x, y } );
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago