Firestore Schema Validator
Elegant object modeling for Google Cloud Firestore.
Inspired by mongoose and datalize.
Installation
Requires firebase-admine
package.
npm install --save firestore-schema-validator
API Docs
Usage
Schema & Model - Simple example
// UserModel.jsconst Model schema field = const userSchema = // Path to Cloud Firestore collection. static { return 'users' } // Model Schema. static { return userSchema }
Schema & Model - Robust example
// UserModel.jsconst Model schema field = const userSchema = static { return 'users' } static { return userSchema } // You can define additional methods... static async { return await this } // ... or getters. { return Booleanthis_dataemailVerificationCode } { return ` ` } // this.toJSON() by default returns this._data, // but you might want to display it differently // (eg. don't show password in responses, // combine firstName and lastName into fullName, etc.) { return id: this_id // ID of Document stored in Cloud Firestore createdAt: this_createdAt // ISO String format date of Document's creation. updatedAt: this_updatedAt // ISO String format date of Document's last update. fullName: thisfullName email: thisemail isEmailVerified: thisisEmailVerified } // Fired when new user is successfully created and stored.UserModel // Fired when user is successfully updated and stored.UserModel // Fired when user is succsessfully deleted.UserModel // Fired during user.validate() if user.email has changed,// but *before* actually validating and storing the data.UserModel // Fired during user.validate() if user.email has changed,// but *after* actually validating and storing the data.UserModel UserModel
Working with UserModel
const admin = const User = // Initialize Firebaseadmin const user = await User // => instance of UserModel console // =>// {// id: 'x22sSpmaJek0CYS9KTsI'.// createdAt: '2019-06-14T15:46:55.108Z',// updatedAt: null,// fullName: 'Jon Doe',// email: 'jon.doe@example.com',// isEmailVerified: false,// } userfirstName = 'J'userfoo = 'bar' // Won't be stored as it's not defined in UserModel._schemaawait user // => instance of UserModel console // =>// {// id: 'x22sSpmaJek0CYS9KTsI'.// createdAt: '2019-06-14T15:46:55.108Z',// updatedAt: null,// fullName: 'J Doe',// email: 'jon.doe@example.com',// isEmailVerified: false,// } const fetchedUser = await UserModel // => instance of UserModel console // =>// {// id: 'x22sSpmaJek0CYS9KTsI'.// createdAt: '2019-06-14T15:46:55.108Z',// updatedAt: null,// fullName: 'J Doe',// email: 'jon.doe@example.com',// isEmailVerified: false,// } await fetchedUser const nonExistingUser = await UserModel // => null
Nesting Schema
field.arrayOf(fieldOrSchema)
and field.objectOf(objectOfFieldsOrSchema)
accept instances of Schema as an argument, so you can reuse repeatable schemas:
const schema field = const simplifiedAddressSchema = const userSchema = const companySchema =
TODO
Field.prototype.unique()
that checks if the value provided to field is unique in the collection.