Skip to content

Commit

Permalink
feat(BaseEntity): Enable per entity datasources and grammars
Browse files Browse the repository at this point in the history
An entity's datasource and grammar can now be set using component-level metadata
using the `datasource` and `grammar` keys respectively.
  • Loading branch information
wpdebruin authored and elpete committed Aug 22, 2018
1 parent 897277d commit 561f368
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions models/BaseEntity.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ component accessors="true" {
property name="mapping";
property name="fullName";
property name="table";
property name="queryoptions";
property name="readonly" default="false";
property name="attributeCasing" default="none";
property name="key" default="id";
property name="attributes";
property name="meta";

/*=====================================
= Instance Data =
=====================================*/
Expand Down Expand Up @@ -183,7 +184,7 @@ component accessors="true" {

function all() {
return eagerLoadRelations(
newQuery().from( getTable() ).get()
newQuery().from( getTable() ).get( options = getQueryOptions() )
.map( function( attributes ) {
return newEntity()
.setAttributesData( attributes )
Expand All @@ -195,7 +196,7 @@ component accessors="true" {

function get() {
return eagerLoadRelations(
getQuery().get().map( function( attributes ) {
getQuery().get( options = getQueryOptions() ).map( function( attributes ) {
return newEntity()
.setAttributesData( attributes )
.setOriginalAttributes( attributes )
Expand All @@ -205,7 +206,7 @@ component accessors="true" {
}

function first() {
var attributes = getQuery().first();
var attributes = getQuery().first( getQueryOptions() );
return newEntity()
.setAttributesData( attributes )
.setOriginalAttributes( attributes )
Expand All @@ -220,7 +221,7 @@ component accessors="true" {
} ) )
.addSelect( getKey() )
.from( getTable() )
.find( id, getKey() );
.find( id, getKey() , getQueryOptions() );
if ( structIsEmpty( data ) ) {
return;
}
Expand Down Expand Up @@ -248,7 +249,7 @@ component accessors="true" {
}

function firstOrFail() {
var attributes = getQuery().first();
var attributes = getQuery().first( getQueryOptions() );
if ( structIsEmpty( attributes ) ) {
throw(
type = "EntityNotFound",
Expand All @@ -271,7 +272,7 @@ component accessors="true" {

function refresh() {
setRelationshipsData( {} );
setAttributesData( newQuery().from( getTable() ).find( getKeyValue(), getKey() ) );
setAttributesData( newQuery().from( getTable() ).find( getKeyValue(), getKey(), getQueryOptions() ) );
return this;
}

Expand All @@ -295,7 +296,7 @@ component accessors="true" {
return { value = value, cfsqltype = getSqlTypeForAttribute( key ) };
}
return value;
} ) );
} ), getQueryOptions() );
setOriginalAttributes( getAttributesData() );
setLoaded( true );
fireEvent( "postUpdate", { entity = this } );
Expand All @@ -312,7 +313,7 @@ component accessors="true" {
return { value = value, cfsqltype = getSqlTypeForAttribute( key ) };
}
return value;
} ) );
} ), getQueryOptions() );
getKeyType().postInsert( this, result );
setOriginalAttributes( getAttributesData() );
setLoaded( true );
Expand All @@ -326,7 +327,7 @@ component accessors="true" {
function delete() {
guardReadOnly();
fireEvent( "preDelete", { entity = this } );
newQuery().delete( getKeyValue(), getKey() );
newQuery().delete( getKeyValue(), getKey(), getQueryOptions() );
setLoaded( false );
fireEvent( "postDelete", { entity = this } );
return this;
Expand All @@ -344,15 +345,15 @@ component accessors="true" {
function updateAll( attributes = {} ) {
guardReadOnly();
guardAgainstReadOnlyAttributes( attributes );
return getQuery().update( attributes );
return getQuery().update( attributes, getQueryOptions() );
}

function deleteAll( ids = [] ) {
guardReadOnly();
if ( ! arrayIsEmpty( ids ) ) {
getQuery().whereIn( getKey(), ids );
}
return getQuery().delete();
return getQuery().delete( options = getQueryOptions() );
}

/*=====================================
Expand Down Expand Up @@ -569,7 +570,7 @@ component accessors="true" {
} ).unique();
var relatedEntity = invoke( entities.get( 1 ), relationName ).getRelated();
var owningKey = invoke( entities.get( 1 ), relationName ).getOwningKey();
var relations = relatedEntity.resetQuery().whereIn( owningKey, keys.get() ).get();
var relations = relatedEntity.resetQuery().whereIn( owningKey, keys.get() ).get( options = getQueryOptions() );

return matchRelations( entities, relations, relationName );
}
Expand Down Expand Up @@ -600,6 +601,10 @@ component accessors="true" {
}

public function newQuery() {
var md = getMeta();
if ( md.keyExists( "grammar" ) ) {
builder.setGrammar( wirebox.getInstance( md.grammar & "@qb" ) );
}
variables.query = builder.newQuery()
.setReturnFormat( function( q ) {
return wirebox.getInstance(
Expand Down Expand Up @@ -758,6 +763,12 @@ component accessors="true" {
setEntityName( md.entityName );
param md.table = str.plural( str.snake( getEntityName() ) );
setTable( md.table );
if (structKeyExists(md,"datasource")) {
md.queryoptions = { datasource=md.datasource };
} else {
md.queryoptions = {};
}
setQueryOptions( md.queryoptions);
param md.readonly = false;
setReadOnly( md.readonly );
param md.properties = [];
Expand Down

0 comments on commit 561f368

Please sign in to comment.