Skip to content

忘掉Mongoose吧,这是更优雅的Mongorito #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
isNeilLin opened this issue Nov 23, 2017 · 0 comments
Open

忘掉Mongoose吧,这是更优雅的Mongorito #14

isNeilLin opened this issue Nov 23, 2017 · 0 comments
Labels
服务端 node,go等服务端语言

Comments

@isNeilLin
Copy link
Owner

MongoDB安装

mac

# 进入 /usr/local
cd /usr/local

# 下载
sudo curl -O https://fastdl.mongodb.org/osx/mongodb-osx-x86_64-3.4.2.tgz

# 解压
sudo tar -zxvf mongodb-osx-x86_64-3.4.2.tgz

# 重命名为 mongodb 目录
sudo mv mongodb-osx-x86_64-3.4.2 mongodb

# 把安装目录/bin添加到 PATH 路径中
export PATH=/usr/local/mongodb/bin:$PATH

# 创建数据库存储目录 /data/db
cd /
sudo mkdir data
cd /data
sudo mkdir db

# 启动 mongodb,默认数据库目录即为 /data/db
sudo mongod
# 如果没有创建全局路径 PATH,需要进入以下目录
cd /usr/local/mongodb/bin
sudo ./mongod

Mongorito

MongoDB ORM for Node based on Redux

安装

npm install mongorito --save
# or
yarn add mongorito

创建数据库连接

const { Database } = require('mongorito');
const db = new Database('localhost/test');
await db.connect();

创建Model

//class “Model”没有任何属性和方法
const { Model } = require('mongorito'); 

class Post extends Model {}

// 在数据库里注册 Model
db.register(Post);

创建Document

// 创建一个Model的实例
const post = new Post();
//or 
const post = new Post({
    title: 'First Post',
    author: {
        name: 'Neil Lin',
        age: 23
    },
    content: 'This is content',
    created: Date.now()
})

获取document中的字段

const title = post.get('title');
const author = post.get('author.name')

更新

post.set('title','Second Post');
post.set({
    author: {
        name: 'monica'
    },
    content: 'This content has already updated.'
})

await post.save()

删除

// 删除单个字段
post.unset('created');

// 删除多个字段
post.unset(['created','author.age'])

await post.save();

// 删除document
await post.remove();

自增字段

const post = new Post({
    views: 0
})

await post.increment('views');

post.get('views'); // => 1

await post.increment('views', 2);

post.get('views'); // => 3

一次增加多个字段

const post = new Post({
    views: 10,
    comments: 10
})

await post.increment({
    views: 5,
    comments: 2
})

post.get('views') // => 15
post.get('comments') // => 12

嵌套其他Model

class Post extends Model {}
class Author extends Model {}
class Comments extends Model {}

Post.embeds('author', Author)
Post.embeds('comments', Comments)

const post = new Post({
    title: 'Great post',
	author: new Author({name: 'Steve'}),
	comments: [new Comment({body: 'Interesting!'})]
})
//or
const post = new Post({
    title: 'Great post',
	author: {
		name: 'Steve'
	},
	comments: [{
		body: 'Interesting!'
	}]
})

await post.save();

查询

// 查找全部posts
await Post.find();

// 查找�tag为"js"的posts
await Post.find({tag: "js"});
// or
await Post.where({tag: "js"}).find();

// 查找最近5条posts
await Post
        .limit(5)
        .sort('created','desc')
        .find();

// 查找其中一条
await Post.findOne({title: 'First Post'});

// 获取posts条数
await Post.count({author: 'Neil Lin'});

插件

使用第三方插件

const timestamp = require('mongorito-timestamps');

db.use(timestamp({
    createdAt: 'created', // default 'created_at'
    updatedAt: 'updated' // default 'updated_at'
}))
// 或者只用于某个Model
Post.use(timestamp({
    createdAt: 'created', // default 'created_at'
    updatedAt: 'updated' // default 'updated_at'
}))

const post = new Post({title: 'Hello'});
await post.save();

post.get('created_at');
//=> 2017-05-17T18:02:06.612Z

编写插件

const extendPost = Post => {
    Post.Recently = function() {
        return this
			.limit(5)
			.sort('created', 'desc')
			.find();
    }

    Post.prototype.setTag = function(tag) {
        this.set('tag',tag);
    }
}

Post.use(extendPost);

const post = new Post();

post.setTag('mongodb');

await post.Recently(); //=> [Post, Post, Post]
@isNeilLin isNeilLin added the Node label Nov 23, 2017
@isNeilLin isNeilLin added 服务端 node,go等服务端语言 and removed Node labels Jun 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
服务端 node,go等服务端语言
Projects
None yet
Development

No branches or pull requests

1 participant