Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fritx/mongo-model-2
MongoDB base model layer
https://github.com/fritx/mongo-model-2
model mongodb orm
Last synced: about 11 hours ago
JSON representation
MongoDB base model layer
- Host: GitHub
- URL: https://github.com/fritx/mongo-model-2
- Owner: fritx
- Created: 2018-06-09T14:59:29.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-06-09T15:07:23.000Z (over 6 years ago)
- Last Synced: 2024-12-15T13:41:18.175Z (about 1 month ago)
- Topics: model, mongodb, orm
- Language: JavaScript
- Homepage: https://npmjs.com/package/mongo-model-2
- Size: 15.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# mongo-model-2
```plain
npm i -S [email protected]
npm i -S mongo-model-2
``````js
await Order.dangerouslyDrop()
await Order.setupIndexes()docs = await Order.find({ filter })
doc = await Order.find({ filter, one: true })ret = await Order.insert({ docs })
ret = await Order.insert({ doc })ret = await Order.update({ filter, set })
ret = await Order.delete({ filter })// pagination & relation
let { total, docs, refs } = await Order.list({
filter, fields,
pagin: { sort, skip, limit },
relation: { user: { fields: ['uid', 'name'] } }
})
``````js
// model/Order.js
let Product = require('./Product')
let User = require('./User')
let B = require('./Base')
let _ = require('lodash')let C = class Order extends B {
// ...
}let sumProp = _.assign({}, Product.priceProp, { range: [0, null] })
C.schema = _.assign({}, B.schema, {
product: { ref: Product },
amount: { type: Number, range: [1, 99], step: 1 },
sum: sumProp,
user: { ref: User },
address: { type: String }
})C.indexes = _.assign({}, B.indexes, {
user_1: [{ user: 1 }]
})module.exports = C
``````js
// model/Base.js
let list = [
'mongo-model-2/src/SchemaBase',
'mongo-model-2/src/DbBase',
'mongo-model-2/src/CollBase',
'mongo-model-2/src/DateBase',
'mongo-model-2/example/UidBase',
'mongo-model-2/example/PaginBase',
'mongo-model-2/example/RelationBase',
'./AppBase'
].map(require)// 将list循环继承 出一个最终的Base类
let Base = list.reduce((B, fn, i) => {
let C = fn(B)
return C
}, null)module.exports = Base
Base.mongoConfig = {
port: process.env.MONGO_PORT,
dbName: process.env.MONGO_DBNAME
}
```