Last active
November 12, 2019 18:25
-
-
Save bchiang7/8356716bb309fd3b49983aea3aaa59da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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