(Quick Reference)

5 Step-by-Step Guide to Creating an Implementation

Version: 2023.3.0

5 Step-by-Step Guide to Creating an Implementation

To get started with a new GORM implementation, the following steps are required:

Initial Directory Creation

git clone https://github.com/graceframework/grace-data.git
cd grace-data
mkdir grace-datastore-gorm-xyz

Setup Gradle Build

Create build.gradle:

vi grace-datastore-gorm-xyz/build.gradle

With contents:

dependencies {
    implementation project(':grace-datastore-gorm'),
            project(':grails-datastore-web'),
            project(':grace-datastore-gorm-support')

    testImplementation project(':grace-datastore-gorm-tck')
    testRuntime "javax.servlet:javax.servlet-api:$servletApiVersion"

}

Add new project to settings.gradle in root project:

vi settings.gradle

Changes shown below:

// GORM Implementations
'grace-datastore-gorm-neo4j',
'grace-datastore-gorm-xyz',
...

Create Project Source Directories

mkdir grace-datastore-gorm-xyz/src/main/groovy
mkdir grace-datastore-gorm-xyz/src/test/groovy

Generate IDE Project Files and Import into IDE (Optional)

./gradlew grace-datastore-gorm-xyz:idea

Or

./gradlew grace-datastore-gorm-xyz:eclipse

Implement Required Interfaces

In src/main/groovy create implementations:

  • org.grails.datastore.xyz.XyzDatastore extends and implements org.grails.datastore.mapping.core.AbstractDatastore

  • org.grails.datastore.xyz.XyzSession extends and implements org.grails.datastore.mapping.core.AbstractSession

  • org.grails.datastore.xyz.engine.XyzEntityPersister extends and implements org.grails.datastore.mapping.engine.NativeEntryEntityPersister

  • org.grails.datastore.xyz.query.XyzQuery extends and implements org.grails.datastore.mapping.query.Query

Create Test Suite

In src/test/groovy create org.grails.datastore.gorm.Setup class to configure TCK:

class Setup {

    static xyz
    static destroy() {
        xyz.disconnect()
    }
    static Session setup(classes) {
        def ctx = new GenericApplicationContext()
        ctx.refresh()
        xyz = new XyzDatastore(ctx)
        for (cls in classes) {
            xyz.mappingContext.addPersistentEntity(cls)
        }


        def enhancer = new GormEnhancer(xyz, new DatastoreTransactionManager(datastore: xyz))
        enhancer.enhance()

        xyz.mappingContext.addMappingContextListener({ e -> enhancer.enhance e } as MappingContext.Listener)
        xyz.applicationContext.addApplicationListener new DomainEventListener(xyz)
        xyz.applicationContext.addApplicationListener new AutoTimestampEventListener(xyz)

        xyz.connect()
    }
}

Then in src/test/groovy create test suite class to allow running tests in IDE (without this you won’t be able to run TCK tests from the IDE). Example test suite:

package org.grails.datastore.gorm

import org.junit.runners.Suite.SuiteClasses
import org.junit.runners.Suite
import org.junit.runner.RunWith
import grails.gorm.tests.*

/**
 * @author graemerocher
 */
@RunWith(Suite)
@SuiteClasses([
  FindByMethodSpec,
  ListOrderBySpec
])
class XyzTestSuite {
}

Implement the TCK!

Keep iterating until you have implemented all the tests in the TCK.