This is port of StarDao to Node.js. While not identical (obviously), it follows the same philosophy:
- Partials Entity Schemas
- No
save()
method - No arbitrary queries
- Built in validate functions
yarn add stardao
load(id)
update(id, updates, updatedById)
updateAndReturn(id, updates, updatedById)
destroy(id, deletedById)
dropTable()
initTable(db, options)
Here's an example of how you might extend.
const StarDao = require('stardao')
const db = StarDao.monk('mongodb://localhost:27017/nb_test')
class UserDao extends StarDao {
constructor(opts) {
super(opts);
}
// Extend Dao with your custom methods here.
loadByEmail(email) {
return this.collection.findOne({ email: email });
}
}
module.exports = new UserDao({
name: 'user',
collection: db.get('user'),
indexes: [[{ email: 1 }, { unique: true }]]
updateSchema: StarDao.joi.object().keys({
email: StarDao.joi.string().email(),
password: StarDao.joi.string().regex(/^[a-zA-Z0-9]{3,30}$/)
}),
newSchema: StarDao.joi.object().keys({
email: StarDao.joi.string().email().required(),
password: StarDao.joi.string().regex(/^[a-zA-Z0-9]{3,30}$/).required()
});,
baseSchema: StarDao.joi.object().keys({
id: StarDao.joi.string().required(),
email: StarDao.joi.string().email().required(),
password: StarDao.joi.string().regex(/^[a-zA-Z0-9]{3,30}$/).required(),
createAt: StarDao.joi.date().required(),
createId: StarDao.joi.string(),
updateAt: StarDao.joi.date(),
updateId: StarDao.joi.string(),
deleteAt: StarDao.joi.date(),
deleteId: StarDao.joi.string()
})
});
Now you have access to all of the built-in Dao methods plus UserDao.loadByEmail
.
Instantiates the db. This is just a wrapper around Automattic's Monk library.