universal-model
Write models and generate mongoose schemas and client-side on-rest models
It's just a concept yet
Inspired by
Why ?
- Модели монгуса не наследуемы
- По моделям монгуса сложно сторить JSON схемы
- Контроллеры постоянно получаются однотипными
- Фронт постоянно получается однотипным
- Потому что на фронте, чтото все равно нужно будет подключать для превалидации моделей
- Это чтото должно поддерживать асинхронные клиент-серверные валидаторы (например для проверки занят ли юзернейм)
- Когда получили данные с сервера аякс запросом работать как с моделью, а не как с куском JSON'a
Мысли
How to use
Use like mongoose schema:
Write schema in models/User.js
models/User.server.js
const schema = username: type: String required: true index: unique: true tolowercase: true trim: true password: type: String name: type: String role: type: String collection: 'user' timestamps: true schemastatics { const re = /^@$/; return re}schemastatics { return Math}schemamethods { return _} const SALT_WORK_FACTOR = 10schema;
Working with model on server
models/User.server.js
// Mongoose// user.server.js mongoose/// etc const MongooseUser = mongoose// orconst MongooseUser = User MongooseUser
Make restful routes easy
// Express restful route const restfulRoute = app// orapp
Its makes routes:
GET /api/user
GET /api/user/:id
POST /api/user
PUT /api/user/:id
DELETE /api/user/:id
Work on client-side
models/User.client.js
const ClientUser = User
UserPage.jsx
ClientUser
Мысли по universal-model
isCollection = true; criteria = {}; skip = 0; limit = 20; select = null; async { await if return res else return res0 } { } { thisskip = skip; return this; } { thisisCollection = true; thiscriteria = criteria; this return this; } { thisisCollection = false; thiscriteria = criteria; this return this; } { return this } // UM => // schema mongoose + как общаться// static// __v: 1 exte static _base = '/api/universal-model'; static _model = '/api/user'; firstname = 'isuvorov' firstname = 'isuvorov' { } { if return _base: this_base _model: this_model _isNew: true } static { return _base: this_base _model: this_model } static { return _base: this_base _model: this_model } User const user = user; // {ignore: 1}user // async methoduserasdasd = 123123user