@surefire/surefire v1.1.0
Introducing StokeJS
A sophisticated ODM (Object-Document Mapper) framework for Google Firebase to easily and rapidly perform typical operations between the front and backends.
Firebase consists of an extensive set of libraries for connecting to, and interacting with the core Firebase services. They are broad in capability, but they do leave opportunity for simplifying those typical interactions.
One key example of this is the absence of an ORM/ODM (such as Sequelize, ActiveRecord etc.) Given the nature of Firebase, some of the typical features, such as associations don't really make sense, but one much needed ability is validations. Having to hand-roll your own, while not difficult, seems unnecessary given the frequency at which it would be used.
Models
It is important to note that there are really two different types of models, those that reside client-side, and those that are server-side (i.e. cloud functions). They are different due to the Firebase/Firestore SDKs that execute in the different environments. Client-side models must remain lightweight while server-side may require more detailed capabilities.
class ServerModel {}
class Model {}
Validations
At the heart of the requirements is the ability to perform validations. Determining the optionality, type, lengths etc of attributes on a model is an integral part of most applications. While we may need to support models on both the client and server side, they must adhere to the same validation rules.
Ignite will utilize an existing, well-rounded validation framework for validating schema. The schema can be applied to either side the model is to be used. They will need to run on demand, or automatically prior to creating or updating a record.
Queries
Firestore again supports a vast range of query constructs through the client sdks. They involve the construction, execution and receipt of results that can then be utilized within the application. While queries can vary and there will be times for specific, nuanced queries, the vast majority are standard. They typically involve simple criteria, pagination and sorting. In many cases, models will also utilize advanced search (i.e. full-text) as well. These must be accounted for.
Hooks
While lifecycle events are necessary, they are achieved in a very different manner to how traditional ORMs work. Specifically, they will happen on the server-side, and as such, need only apply to server-side models.
Model definition
class ProjectModel extends EntityModel {
// Schema utilizes yup
static schema = {}
}
class ProjectService extends EntityService {
// Implied pluralized if not stated
static collection = 'projects'
// Schema utilizes yup
static schema = {}
}
Basic methods
class EntityModel {
// Static methods
static async function count () {}
static async function findOne () {}
static async function findAll () {}
static async function findAndCountAll () {}
static async function searchAll () {}
static async function searchAndCountAll () {}
// Instance methods
async function save () {}
async function update () {}
async function delete () {}
async function validate () {}
async function isValid () {}
}
Usage
// Client-side
const project = new Project(formData)
await project.save()
// Server-side
const project = new Project()
3 years ago