Skip to content
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

Sequelize.js #38

Open
MikeMiller3 opened this issue Feb 8, 2018 · 0 comments
Open

Sequelize.js #38

MikeMiller3 opened this issue Feb 8, 2018 · 0 comments

Comments

@MikeMiller3
Copy link
Owner

Sequelize.js

官方文档
中文文档
API

介绍

基于 promise 的 Node.js ORM

基本用法

建立连接

const sequelize = new Sequelize('postgres://user:[email protected]:5432/dbname');

Dao/Model/模型

const User = sequelize.define('user', {
  firstName: {
    type: Sequelize.STRING
  },
  lastName: {
    type: Sequelize.STRING
  }
});

简单查询

user = await User.findOne()
//支持async/await
console.log(user.get('firstName'));

举例:where对象,Sequence.Op常量,Sequence.fn使用数据库方法,etc

async function getTimeoutCountByDay(urls, ips, includeIp, steps, duration, startTime, endTime, offset, limit) {

	let where = {};
	if (urls && urls.length > 0) {
		where.url = {
			[Sequelize.Op.in]: urls
		};
	}

	if (includeIp && ips && ips.length > 0) {
		where.ip = {
			[Sequelize.Op.in]: ips
		}
	}

	if (!includeIp && ips && ips.length > 0) {
		where.ip = {
			[Sequelize.Op.notIn]: ips
		}
	}

	if (steps && steps.length > 0) {
		where.step = {
			[Sequelize.Op.in]: steps
		};
	}
	if (duration || duration === 0) {
		where.duration = {
			[Sequelize.Op.gte]: duration
		};
	}

	if (startTime && endTime) {
		where.serverTime = {
			[Sequelize.Op.gte]: startTime,
			[Sequelize.Op.lt]: endTime
		};
	}
	let res = await monitorEp5DaDao.findAll({
		attributes: [
			[sequelize.fn('DATE_FORMAT', sequelize.col('serverTime'), '%Y-%m-%d'), 'serverDate'],
			[sequelize.fn('COUNT', 'id'), 'count']
		],
		where: where,
		group: ['serverDate'],
		offset: offset,
		limit: limit
	});
	return res;
};

更新

批量更新

public static bulkCreate(records: Array, options: Object)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant