//fsdb.js
//实现一个文件系统读取数据库
const fs = require("fs")
function get(key){
fs.readFile("./db.json", (err, data)=>{
const json = JSON.parse(data)
console.log(json[key])
})
}
fonction set(key, value){
//可能是空文件, 则设置为空对象
const json = data ? JSOJN.parse(data):[]
json[key] = value//设置值
fs.writeFile('./db.json', JSON.stringify(json), err=>{
if(err){
console.log(err)}
console.log('写入成功')
})
}
//命令行接口部分
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.on('line', function(input){
const [op, key, value] = require.split(' ')
if(op === 'get){
get(key)
}else if(op === 'set){
set(key, value)
}else if(op === 'quit'){
rl.close()
}else{
console.log('没有该操作')
}
})
rl.on('close', function(){
console.log(''程序结束)
process.exit(0)
})
//mysql.js
const mysql = require('mysql')
//链接配置
const cfg = {
host: 'localhost',
user: 'root',
password: ' example',
database: 'kkk',//确保数据库存在
}
//创建连接对象
const conn = mysql.createConnection(cfg)
//链接
conn.connect(err=>{
if(err){
throw err
}else{
console.log('connect success')
}
})
//查询 conn.query()
//创建表
const CREATE_SQL = `
CREATE TABLE IF NOT EXISTS TEST(
id INT NULL AUTO_INCREMENT,
message VARCHAR(45) NULL,
PRIMARY KEY (id)
)`
const INSERT_SQL = `INSERT INTO test(message) VALUES(?)`
const SELECT_SQL = `SELECT * FROM test`
conn.query(CREATE_SQL, err=>{
if(err){
throw err}
//
conn.query(INSERT_SQL, 'hellow,world', (err, result)=>{
if(err){
throw err}
console.log(result)
conn.query(SELECT_SQL, (err, result)=>{
console.log(result)
conn.end()//若query语句有嵌套,则end需要在此执行
})
})
})
`
es2017 写法
// mysql2.js
(async () => {
// get the client
const mysql = require('mysql2/promise');
// 连接配置
const cfg = {
host: "localhost",
user: "root",
password: "example", // 修改为你的密码
database: "kaikeba" // 请确保数据库存在
};
// create the connection
const connection = await mysql.createConnection(cfg);
// 查询 conn.query()
// 创建表
const CREATE_SQL = `CREATE TABLE IF NOT EXISTS test (
id INT NOT NULL AUTO_INCREMENT,
message VARCHAR(45) NULL,
PRIMARY KEY (id))`;
const INSERT_SQL = `INSERT INTO test(message) VALUES(?)`;
const SELECT_SQL = `SELECT * FROM test`;
// query database
let ret = await connection.execute(CREATE_SQL);
console.log('create:', ret)
ret = await connection.execute(INSERT_SQL, ['abc']);
console.log('insert:', ret)
const [rows, fields] = await connection.execute(SELECT_SQL);
console.log('select:', rows)
})()
(async () => {
const Sequelize = require("sequelize");
// 建⽴连接
const sequelize = new Sequelize("kaikeba", "root", "example", {
host: "localhost",
dialect: "mysql",
operatorsAliases: false
});
// 定义模型
const Fruit = sequelize.define("Fruit", {
name: {
type: Sequelize.STRING(20), allowNull: false },
price: {
type: Sequelize.FLOAT, allowNull: false },
stock: {
type: Sequelize.INTEGER, defaultValue: 0 }
});
let ret = await Fruit.sync()
console.log('sync',ret)
ret = await Fruit.create({
name: "⾹蕉",
price: 3.5
})
console.log('create',ret)
ret = await Fruit.findAll()
await Fruit.update(
{
price: 4 },
{
where: {
name:'⾹蕉'} }
)
console.log('findAll',JSON.stringify(ret))
const Op = Sequelize.Op;
ret = await Fruit.findAll({
// where: { price: { [Op.lt]:4 }, stock: { [Op.gte]: 100 } }
where: {
price: {
[Op.lt]: 4, [Op.gt]: 2 } }
})
console.log('findAll', JSON.stringify(ret, '', '\t'))
})()
const Fruit = sequelize.define("Fruit", {
}, {
timestamps: false
});
指定表名freezeTableName: true 或 tableName:'xxx'
设置前者则以modelName作为表名;设置后者则按其值作为表名。
蛇形命名 underscored: true,
默认驼峰命名
UUID 主键
id: {
type: Sequelize.DataTypes.UUID,
defaultValue: Sequelize.DataTypes.UUIDV1,
primaryKey: true
},
// 定义为属性的⼀部分
name: {
type: Sequelize.STRING,
allowNull: false,
get() {
const fname = this.getDataValue("name");
const price = this.getDataValue("price");
const stock = this.getDataValue("stock");
return `${
fname}(价格:¥${
price} 库存:${
stock}kg)`;
}
}
// 定义为模型选项
// options中 {
getterMethods:{
amount(){
return this.getDataValue("stock") + "kg";
}
},
setterMethods:{
amount(val){
const idx = val.indexOf('kg');
const v = val.slice(0, idx);
this.setDataValue('stock', v);
}
}
}
// 通过模型实例触发setterMethods
Fruit.findAll().then(fruits => {
console.log(JSON.stringify(fruits));
// 修改amount,触发setterMethods
fruits[0].amount = '150kg';
fruits[0].save();
});
price: {
validate: {
isFloat: {
msg: "价格字段请输⼊数字" },
min: {
args: [0], msg: "价格字段必须⼤于0" }
}
},
stock: {
validate: {
isNumeric: {
msg: "库存字段请输⼊数字" }
}
}
// 添加类级别⽅法
Fruit.classify = function(name) {
const tropicFruits = ['⾹蕉', '芒果', '椰⼦']; // 热带⽔果
return tropicFruits.includes(name) ? '热带⽔果':'其他⽔果';
};
// 添加实例级别⽅法
Fruit.prototype.totalPrice = function(count) {
return (this.price * count).toFixed(2);
};
// 使⽤类⽅法
['⾹蕉','草莓'].forEach(f => console.log(f+'是'+Fruit.classify(f)));
// 使⽤实例⽅法
Fruit.findAll().then(fruits => {
const [f1] = fruits;
console.log(`买5kg${
f1.name}需要¥${
f1.totalPrice(5)}`);
});
// 通过id查询(不⽀持了)
Fruit.findById(1).then(fruit => {
// fruit是⼀个Fruit实例,若没有则为null
console.log(fruit.get());
});
// 通过属性查询
Fruit.findOne({
where: {
name: "⾹蕉" } }).then(fruit => {
// fruit是⾸个匹配项,若没有则为null
console.log(fruit.get());
});
// 指定查询字段
Fruit.findOne({
attributes: ['name'] }).then(fruit => {
// fruit是⾸个匹配项,若没有则为null
console.log(fruit.get());
});
// 获取数据和总条数
Fruit.findAndCountAll().then(result => {
console.log(result.count);
console.log(result.rows.length);
});
// 查询操作符
const Op = Sequelize.Op;
Fruit.findAll({
// where: { price: { [Op.lt]:4 }, stock: { [Op.gte]: 100 } }
where: {
price: {
[Op.lt]:4,[Op.gt]:2 }}
}).then(fruits => {
console.log(fruits.length);
});
// 或语句
Fruit.findAll({
// where: { [Op.or]:[{price: { [Op.lt]:4 }}, {stock: { [Op.gte]: 100
}}] }
where: {
price: {
[Op.or]:[{
[Op.gt]:3 }, {
[Op.lt]:2 }]}}
}).then(fruits => {
console.log(fruits[0].get());
});
// 分⻚
Fruit.findAll({
offset: 0,
limit: 2,
})
// 排序
Fruit.findAll({
order: [['price', 'DESC']],
})
// 聚合
Fruit.max("price").then(max => {
console.log("max", max);
});
Fruit.sum("price").then(sum => {
console.log("sum", sum);
});
Fruit.findById(1).then(fruit => {
// ⽅式1
fruit.price = 4;
fruit.save().then(()=>console.log('update!!!!'));
});
// ⽅式2
Fruit.update({
price:4}, {
where:{
id:1}}).then(r => {
console.log(r);
console.log('update!!!!')
})
删除
// ⽅式1
Fruit.findOne({
where: {
id: 1 } }).then(r => r.destroy());
// ⽅式2
Fruit.destroy({
where: {
id: 1 } }).then(r => console.log(r));
const log = (text, data) => {
console.log(`===========${
text}========`)
console.log(JSON.stringify(data,null,"\t"))
console.log(`==========================`)
}
const sequelize = require('./util/database')
// 定义模型 1:N 关系
const Product = require('./models/product');
const User = require('./models/user');
const Cart = require('./models/cart');
const CartItem = require('./models/cart-item');
Product.belongsTo(User, {
constraints: true,
onDelete: 'CASCADE'
});
User.hasMany(Product)
await sequelize.sync({
force: true })
// 创建⽤户
let user = await User.fin
if (!user) {
user = await User.create({
name: 'kaikeba',
email: '[email protected]'
})
}
// 添加商品
let product = await user.createProduct({
title: '商品⼀',
price: 123,
imageUrl: 'abc.png',
description: '商品描述'
})
log('product', product)
// N : N关系
User.hasOne(Cart)
Cart.belongsTo(User)
Cart.belongsToMany(Product, {
through: CartItem
});
Product.belongsToMany(Cart, {
through: CartItem
});
await sequelize.sync({
force: true })
// 创建⽤户
let user = await User.findByPk(1)
if (!user) {
user = await User.create({
name: 'kaikeba',
email: '[email protected]'
})
}
// 添加商品
let product = await user.createProduct({
title: '商品⼀',
price: 123,
imageUrl: 'abc.png',
description: '商品描述'
})
log('product', product)
// 添加购物⻋
await user.createCart()
ret = await User.findAll({
include: [Cart] })
log('getCart:', ret)
// 添加购物⻋商品
let cart = await user.getCart()
await cart.addProduct(product, {
through: {
quantity: 1 }
})
// 获取购物⻋商品数量
const productId = 1
const item = await cart.getProducts({
where: {
id: productId
}
})
log('item', item)
// 商品是否存在
if(item.length > 0){
console.log('商品存在....................')
await cart.addProduct(product, {
through: {
quantity: item[0].cartItem.quantity + 1 }
})
}else{
await cart.addProduct(product, {
through: {
quantity: 1 }
})
}
log('cart', cart)
Restful服务
实践指南 http://www.ruanyifeng.com/blog/2014/05/restful_api.html
原理 http://www.ruanyifeng.com/blog/2011/09/restful.html