Created
September 15, 2022 15:49
-
-
Save 34fame/8a5ee6ba96b47aa8d6411839c82dfef0 to your computer and use it in GitHub Desktop.
Complete code example of functional, factory design pattern implemented in JavaScript
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
/* Import Libraries */ | |
const { Validator } = require('jsonschema') | |
const v = new Validator() | |
/* END Import Libraries */ | |
/* Constants */ | |
const stores = { | |
FirebaseFirestore: new DbFirebaseFirestoreFactory(), | |
FirebaseRealtime: () => {}, | |
} | |
const models = { | |
products: { | |
init: new ProductModel(), | |
schema: { | |
type: 'object', | |
}, | |
}, | |
users: { | |
init: new UserModel(), | |
schema: { | |
type: 'object', | |
properties: { | |
name: { type: 'string' }, | |
age: { type: 'number' }, | |
}, | |
required: ['name'], | |
}, | |
}, | |
} | |
const validateJson = (obj, schema) => { | |
log('>>> validateJson', obj, schema) | |
const test = v.validate(obj, schema) | |
if (test.valid) { | |
log('<<< validateJson', { valid: true }) | |
return { valid: true } | |
} | |
log('<<< validateJson', { valid: false, errors: test.errors }) | |
return { valid: false, errors: test.errors } | |
} | |
const log = console.log | |
/* END Constants */ | |
/* Database Factories */ | |
function DbStoreFactory(store) { | |
return stores[store] | |
} | |
function DbFirebaseFirestoreFactory() { | |
return { | |
list: function ({ collection, orderBy, limit }) { | |
log('>>> DbFirebaseFirestoreFactory.list', collection) | |
return new Promise((res, rej) => { | |
setTimeout(() => { | |
log('<<< DbFirebaseFirestoreFactory.list', []) | |
res([]) | |
}, 500) | |
}) | |
}, | |
} | |
} | |
/* END Datasbase Factories */ | |
/* Model Factories */ | |
function ModelFactory(model) { | |
const db = new DbStoreFactory('FirebaseFirestore') | |
return { | |
list: async function () { | |
log('>>> ModelFactory.list', model) | |
const result = await db.list({ collection: model }) | |
log('<<< ModelFactory.list', result) | |
return result | |
}, | |
...models[model].init, | |
} | |
} | |
function UserModel() { | |
return { | |
create: function (userObj) { | |
log('>>> UserModel.create', userObj) | |
const test = validateJson(userObj, models.users.schema) | |
if (!test.valid) { | |
for (let error of test.errors) { | |
console.error(error.stack) | |
} | |
log('<<< UserModel.create', false) | |
return false | |
} | |
userObj = { | |
...userObj, | |
id: new Date().valueOf(), | |
} | |
log('<<< UserModel.create', userObj.id) | |
return userObj.id | |
}, | |
} | |
} | |
function ProductModel() { | |
return { | |
create: function (productObj) { | |
log('>>> ProductModel.create', productObj) | |
const test = validateJson(productObj, models.products.schema) | |
if (!test.valid) { | |
for (let error of test.errors) { | |
console.error(error.stack) | |
} | |
log('<<< ProductModel.create', false) | |
return false | |
} | |
productObj = { | |
...productObj, | |
id: new Date().valueOf(), | |
} | |
log('<<< ProductModel.create', productObj.id) | |
return productObj.id | |
}, | |
} | |
} | |
/* END Model Factories */ | |
/* Client */ | |
const client = async () => { | |
const user = new ModelFactory('users') | |
const users = await user.list() | |
user.create({ name: 'Troy', age: 49 }) | |
const product = new ModelFactory('products') | |
const products = await product.list() | |
product.create({ name: 'soap' }) | |
} | |
client() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment