-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to apex-database-layer, an open-source library used to easily mock Salesforce database operations in Apex.
Apex Database Layer was designed to be feature-rich, yet easy to use, closely mirroring standard platform patterns.
You get the following out of the box:
- Support for all standard
Databaseclass DML & SOQL operations - Easily mock DML operations and SOQL queries.
- A Query framework that enables strongly-typed, yet dynamic SOQL queries.
- Easily generate test records without using DML or SOQL.
- Switch between real & mock database operations in Apex Tests, with a single line of code
- An optional Plugin Framework allows you to fine-tune the platform to your exact use case.
- Simplicity: The framework uses just a couple of Apex classes (and a custom metadata type, to support Plugins).
Best of all, the framework is built 100% on the Salesforce platform, using standard Salesforce technology. It's open source, and free, and it always will be.
apex-database-layer is available for free. It can be downloaded in one of two flavors:
- As an unlocked package with no namespace
- As a managed package, using the
apxspnamespace
You can find the latest or past versions in the Releases tab.
Use the following command to install the package in your environment:
sf package install --package {{package_version_id}} --wait 10Once intalled, use DatabaseLayer.Dml for all of your DML operations:
// Don't use these standard apex DML methods:
insert account;
Database.insert(account);
// Use this instead:
DatabaseLayer.Dml.doInsert(account);Use DatabaseLayer.Soql for all of your SOQL queries:
List<Account> accounts = (List<Account>) DatabaseLayer.Soql.newQuery(Account.SObjectType)
?.addSelect(Account.Name)
?.addWhere(Account.OwnerId, Soql.EQUALS, UserInfo.getUserId())
?.addOrderBy(Account.LastModifiedDate, Soql.SortDirection.DESCENDING)
?.setRowLimit(200)
?.toSoql()
?.query();Once this is done, you can instantly decouple your Dml and Soql operations from the Salesforce database in apex tests, with just a single line of code:
DatabaseLayer.useMocks();In apex tests, you can easily generate test records for use in mocks, that would otherwise require extensive database operations:
// This operation takes ~2ms; would require 4 separate DML operations otherwise:
OpportunityContactRole contactRole = (OpportunityContactRole) new MockRecord(OpportunityContactRole.SObjectType)
?.withId()
?.toSObject();Check out the sidebar for articles about:
- Mocking database operations, and why it's important
- Techniques for mocking using the framework
- Documentation for all public classes & methods
- Generating Test Records
- Dml
- Soql
- Cmdt
- Plugins
- DatabaseLayer
- Dml
- MockDml
- MockRecord
- Cmdt
- MockCmdt
- MockSoql
-
Soql
- Soql.AggregateResult
- Soql.Aggregation
- Soql.Binder
- Soql.Builder
- Soql.Condition
- Soql.ConditionalLogic
- Soql.Criteria
- Soql.Cursor
- Soql.Function
- Soql.InnerQuery
- Soql.InvalidParameterValueException
- Soql.LogicType
- Soql.NullOrder
- Soql.Operation
- Soql.Operator
- Soql.ParentField
- Soql.PreAndPostProcessor
- Soql.QueryLocator
- Soql.Request
- Soql.Scope
- Soql.Selectable
- Soql.SortDirection
- Soql.SortOrder
- Soql.Subquery
- Soql.TypeOf
- Soql.Usage
- Soql.WhenClause