Skip to content

Instantly share code, notes, and snippets.

@bchiang7
Last active November 12, 2019 18:25
Show Gist options
  • Save bchiang7/8356716bb309fd3b49983aea3aaa59da to your computer and use it in GitHub Desktop.
Save bchiang7/8356716bb309fd3b49983aea3aaa59da to your computer and use it in GitHub Desktop.
// models/Base.js
class Base {
constructor(args) {
const results = this.validatedData(args);
Object.assign(this, results);
}
get schema() {
throw ('You must declare a schema');
}
// Return the collection name that corresponds to Firestore
// Method should be overwritten in child models
get collectionName() {
return this.constructor.collectionName();
}
// Returns the expected schema of the model
expectedSchema() {
// stripUnknown will remove any properties that aren't on the schema
return Joi.object().keys(this.schema).options({ stripUnknown: true });
}
// Return validated fields
validatedData(args) {
const { error, value } = Joi.validate(args, this.expectedSchema());
if (error) {
throw new Error(error);
}
return value;
}
async create() {
...
}
async save() {
...
}
static async findById(id) {
...
}
static async findAll() {
...
}
...
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment