0.0.2 • Published 7 years ago
stardao v0.0.2
stardao-node
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
Getting Started
yarn add stardao
What you get fo' free
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.
Example
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
.
Public API
Dao
Dao.initTable(): void
Dao.dropTable(): void
Dao.create(model: NewSchema, uuid?: string): Promise<BaseSchema>
Dao.update(id: string, updates: UpdateSchema, updateId?: string): Promise<ResultObject>
Dao.updateAndReturn(id: string, updates: UpdateSchema, updateId?: string): Promise<BaseSchema>
Dao.remove(id: string, deleteId?: string): Promise<ResultObject>
Dao.load(id: string): Promise<BaseSchema>
Dao.validateSync(model: Object): { error, model }
Dao.validateNewSync(model: Object): { error, model }
Dao.validateUpdateSync(model: Object, dao: Dao<Collection, Schema>): { error, model }
Dao.validate(model: Object, cb?: Function): Promise
Dao.validateUpdate(model: Object, cb?: Function): Promise
Dao.validateNew(model: Object, cb?: Function): Promise
Monk
StarDao.monk(ConnectionString: string, ?options: Object): MonkInstance
Instantiates the db. This is just a wrapper around Automattic's Monk library.