A ColdBox ORM Engine
🏡 Utah
🏬 Ortus
📦 Quick, qb, ColdBox Elixir
👨👩👦👦 1 wife, 3 kids
// config/ColdBox.cfc
component {
function configure() {
moduleSettings = {
"quick" = {
"defaultGrammar" = "AutoDiscover"
}
};
}
}
box install quick
// models/User.cfc
component extends="quick.models.BaseEntity" {
property name="id";
property name="email"
update="false"
insert="true";
property name="password";
property name="createdDate"
column="created_date";
property name="modifiedDate"
column="modified_date";
property name="lastLoggedIn"
column="last_logged_in"
nullValue="REALLY_NULL";
property name="number"
sqltype="cf_sql_varchar"
convertToNull="false";
}
// models/User.cfc
component extends="quick.models.BaseEntity" {
property name="bcrypt" inject="@BCrypt" persistent="false";
property name="password";
property name="createdDate" column="created_date";
/* ... */
function setPassword( password ) {
assignAttribute(
"password",
bcrypt.hashPassword( password )
);
return this;
}
function getCreatedDate() {
return dateFormat(
retrieveAttribute( "createdDate" ),
"mm/dd/yyyy"
);
}
}
// handlers/users.cfc
component {
function index( event, rc, prc ) {
var user = getInstance( "User" );
/* ... */
}
}
// handlers/users.cfc
component {
property name="userService" inject="quickService:User";
/* ... */
}
// handlers/users.cfc
component {
property name="userService" inject="quickService:User";
// GET /users
function index( event, rc, prc ) {
prc.users = userService.all();
}
// GET /users/:id
function show( event, rc, prc ) {
prc.user = getInstance( "User" ).findOrFail( rc.id );
}
}
// handlers/users.cfc
component {
property name="userService" inject="quickService:User";
// GET /users/search
function search( event, rc, prc ) {
prc.users = userService
.whereBetween( "createdDate", rc.startDate, rc.endDate )
.when( rc.search != "", function( q ) {
q.where( "username", "like", "%#rc.search#%" );
} );
.get();
}
// GET /users/:username
function show( event, rc, prc ) {
prc.user = userService
.where( "username", rc.username )
.firstOrFail();
}
}
// handlers/users.cfc
component {
property name="userService" inject="quickService:User";
// POST /users
function store( event, rc, prc ) {
userService.create( {
"email" = rc.email,
"password" = rc.password,
"createdDate" = now(),
"modifiedDate" = now()
} );
relocate( "users" );
}
// PUT /users/:id
function update( event, rc, prc ) {
var user = userService.findOrFail( rc.id );
user.update( {
"email" = rc.email,
"password" = rc.password,
"modifiedDate" = now()
} );
relocate( "users.#user.getId()#" );
}
}
// models/User.cfc
component extends="quick.models.BaseEntity" {
function keyType() {
return variables._wirebox
.getInstance( "NullKeyType@quick" );
}
}
// handlers/users.cfc
component {
property name="userService" inject="quickService:User";
// DELETE /users/:id
function delete( event, rc, prc ) {
var user = userService.findOrFail( rc.id );
user.delete();
// OR
userService.deleteAll( rc.id );
}
}
// handlers/inactiveUsers.cfc
component {
property name="userService" inject="quickService:User";
// PATCH /inactive-users
function update( event, rc, prc ) {
userService
.where( "last_logged_in", "<", dateAdd( "m", -3, now() ) )
.updateAll( {
"active" = 0
} );
}
// DELETE /inactive-users
function delete( event, rc, prc ) {
userService
.where( "active", 0 )
.deleteAll()
}
}
// models/User.cfc
component extends="quick.models.BaseEntity" {
property name="id";
property name="createdDate"
column="created_date";
/* ... */
function scopeLatest( query ) {
return query.orderBy( "createdDate", "desc" );
}
}
// handlers/users.cfc
component {
function index( event, rc, prc ) {
prc.users = getInstance( "User" )
.latest()
.get();
}
}
// models/User.cfc
component extends="quick.models.BaseEntity" {
property name="id";
property name="createdDate"
column="created_date";
/* ... */
function scopeCreatedInYear( query, year ) {
return query.whereBetween(
"createdDate",
createDate( year, 1, 1 ),
createDate( year, 12, 31 )
);
}
}
// handlers/users.cfc
component {
function index( event, rc, prc ) {
prc.users = getInstance( "User" )
.createdInYear( 2018 )
.get();
}
}
// models/Post.cfc
component extends="quick.models.BaseEntity" {
property name="id";
property name="title";
property name="userId" column="user_id";
/* ... */
function author() {
return belongsTo( "User" );
}
}
<!-- views/post/show.cfm -->
<cfoutput>
<h3>#prc.post.getTitle()#</h3>
<h4>By #prc.post.getAuthor().getEmail()#</h4>
<!-- ... -->
</cfoutput>
// models/Post.cfc
component extends="quick.models.BaseEntity" {
property name="id";
property name="slug";
/* ... */
function comments() {
return hasMany( "Comment" );
}
}
// handlers/posts.cfc
component {
// GET /posts/:slug
function show( event, rc, prc ) {
prc.post = getInstance( "Post" ).whereSlug( slug ).findOrFail();
prc.comments = prc.post.comments().active().get();
}
}
// models/Post.cfc
component extends="quick.models.BaseEntity" {
property name="id";
property name="slug";
/* ... */
function comments() {
return hasMany( "Comment" );
}
function activeComments() {
return comments().active();
}
}
// handlers/posts.cfc
component {
// GET /posts/:slug
function show( event, rc, prc ) {
prc.post = getInstance( "Post" ).whereSlug( slug ).findOrFail();
prc.comments = prc.post.getActiveComments();
}
}
<!-- views/posts/show.cfm -->
<cfoutput>
<cfloop var="#prc.posts#" item="post">
<h3>#post.getTitle()#</h3>
<h5>By #post.getAuthor().getEmail()#</h5>
<!-- ... -->
</cfloop>
</cfoutput>
SELECT * FROM `posts` WHERE `id` = ? LIMIT 1
SELECT * FROM `users` WHERE `id` = ? LIMIT 1
Queries Executed
Number of Posts | Number of Queries |
---|---|
1 | 2 |
2 | 3 |
10 | 11 |
// handlers/posts.cfc
component {
// GET /posts/:slug
function show( event, rc, prc ) {
prc.post = getInstance( "Post" )
.with( "author" )
.whereSlug( slug )
.findOrFail();
}
}
Number of Posts | Number of Queries |
---|---|
1 | 2 |
2 | 2 |
10 | 2 |
SELECT * FROM `posts` WHERE `id` = ? LIMIT 1
SELECT * FROM `users` WHERE `id` IN (?, ?, ?, ?, ...)
Queries Executed
// handlers/users.cfc
component {
// GET /users
function show( event, rc, prc ) {
prc.users = getInstance( "User" )
.with( "posts.comments" )
.get();
}
}
Number of Posts | Number of Queries |
---|---|
1 | 3 |
10 | 3 |
100 | 3 |
SELECT * FROM `users`
SELECT * FROM `posts` WHERE `user_id` IN (?, ?, ?, ?, ...)
SELECT * FROM `comments` WHERE `post_id` IN (?, ?, ?, ?, ...)
Queries Executed
getInstance( "User" ).findOrFail( 1 ).getMemento();
{
"id" = 1,
"email" = "john@example.com",
"createdDate" = "{ts '2019-04-13 02:33:44'}",
"modifiedDate" = "{ts '2019-04-13 02:33:44'}"
}
component extends="quick.models.BaseEntity" {
property name="id";
property name="email";
property name="createdDate" column="created_date";
property name="modifiedDate" column="modified_date";
}
component extends="quick.models.BaseEntity" {
/* ... */
function getMemento() {
return {
"id" = getId(),
"email" = getEmail(),
"createdDate" = dateFormat( getCreatedDate(), "mm/dd/yyyy" ),
"modifiedDate" = dateFormat( getModifiedDate(), "mm/dd/yyyy" )
};
}
}
getInstance( "User" ).findOrFail( 1 ).getMemento();
{
"id" = 1,
"email" = "john@example.com",
"createdDate" = "04/13/2019",
"modifiedDate" = "04/13/2019"
}
component extends="quick.models.BaseEntity" {
this.memento = { /*... */ };
function getMemento() {
return $getMemento();
}
}
// models/User.cfc
component extends="quick.models.BaseEntity" {
/* ... */
function posts() {
return hasMany( "Post", "user_id" );
}
function scopeWithPostCount( query ) {
query.select( "users.*" )
.subselect( "postCount", function( query ) {
query.selectRaw( "COUNT(*)" )
.from( "posts" )
.whereColumn( "user_id", "users.id" );
} );
}
}
<!-- /views/posts/show.cfc -->
<cfoutput>
<p>#prc.user.getEmail()# has #prc.user.getPostCount()# post(s).</p>
</cfoutput>
// models/User.cfc
component extends="quick.models.BaseEntity" {
/* ... */
function scopeWithLatestPost( query ) {
variables._nullValues[ "latestPostId" ] = "";
query.select( "users.*" )
.subselect( "latestPostId", function( query ) {
query.select( "id" )
.from( "posts" )
.whereColumn( "user_id", "users.id" )
.orderBy( "created_date", "desc" )
.limit( 1 );
} )
.with( "latestPost" );
}
function latestPost() {
return belongsTo( "Post", "latestPostId" );
}
}
<!-- /views/posts/show.cfc -->
<cfoutput>
<h3>#prc.user.getEmail()#</h3>
<dl>
<dt>Latest Post</dt>
<dd>#prc.user.getLatestPost().getTitle()</dd>
</dl>
</cfoutput>
instanceReady | preLoad | postLoad |
preSave | postSave | preInsert |
postInsert | preUpdate | postUpdate |
preDelete | postDelete |
component
extends="quick.models.BaseEntity"
table="tbl_posts"
datasource="secondarydb"
grammar="PostgresGrammar"
{
/* ... */
}