• nodejs笔记


    创建koa项目

    // 设置环境变量的工具
    npm i cross-env -D
    
    
    // package.json
    
     "scripts": {
        "start": "node bin/www",
        "dev": "cross-env NODE_ENV=dev ./node_modules/.bin/nodemon bin/www",
        "prd": "cross-env NODE_ENV=production pm2 start bin/www",
        "test": "echo "Error: no test specified" && exit 1"
      },
    

    ejs的使用

    • 变量
    • 判断和循环
    • 引用组件

    mysql的基础使用

    增删改查

    use koa_weibo; 
    
    -- 倒序查找
    -- select * from blogs order by id desc
    
    -- select username,password from users where username='zhangsan' and `password` ='123';
    
    -- insert into blogs  (title,  content, userid) values('红十字会', '希望公开透明', 2);
    
    -- update blogs set content = '加油,中国' where id ='1';
    
    delete from blogs where id= 2;
    
    -- 查询总数
    select count (id)  as `count` from blogs
    
    
    -- 查询页数
    -- 限制1行 跳过一行 
    select * from users order by id limit 1 offset 1;
    

    外键

    ​ 连表查询和外键不是必须一定要有的,但是外键可以让数据同步实施更新

    • 如何创建外键?

      Navicat创建外键 方式

    • 更新限制 & 删除级联

      ​ 创建外键后设置好更新和删除的操作即可实现

    • 连表查询

      查询到不同表之间相互关联的数据

      select  * from blogs inner join users on users.id = blogs.userid;
      
      
      //
      select blogs.*, users.username, users.nickname from blogs  inner join users on users.id = blogs.userid;
      
      //
      select blogs.*, users.username, users.nickname 
      from blogs  inner join users on users.id = blogs.userid
      where users.username = 'zhangsan';
      

    sequelize

    const { Blog, User } = require('./model');
    
    !(async function() {
      const zhangsan = await User.create({
        userName: 'zhangsan',
        password: ' 123456',
        nickName: '张三'
      });
      console.log(zhangsan.dataValues);
    
      const blogs = await Blog.create({
        title: '标题1',
        content: ' 内容1',
        userId: 1
      });
      console.log(blogs.dataValues);
    })();
    
    
      // 分页
      const blogListAndCount = await Blog.findAndCountAll({
        limit: 2,
        offset: 0,
        order: [
          ['id', 'desc']
        ]
      });
    
      console.log(
        blogListAndCount.count,
        blogListAndCount.rows.map(blog =>
          blog.dataValues
        ));
    

    连表查询

    // 创建外键关联
    Blog.belongsTo(User, {
      // 创建外键关联
      // Blogs.userId ->Users.id
      // 默认情况就是关联id
      foreignKey: 'userId'
    });
    
    // 和上面的效果等同
    User.hasMany(Blog, {
      foreignKey: 'userId'
    });
    

    第一个使用Blog.belongs(...) 和第二个的User.hasMany(...)外键效果相同,但是第一个可以通过blog的表查询联动查询到user中的,儿第二个可以实现反过来。

      const blogListWithUser = await Blog.findAndCountAll({
        order: [
          ['id', 'desc']
        ],
        include: [
          {
            model: User,
            attributes: ['userName', 'nickName'],
            where: {
              userName: 'zhangsan1'
            }
          }
        ]
      });
    
        console.log(blogListWithUser);
        console.log(blogListWithUser.rows.map(blog => {
          const blogVal = blog.dataValues;
          blogVal.user = blogVal.user.dataValues;
          return blogVal;
        }));
    
     const userListWithBlog = await User.findAndCountAll({
          attributes: ['userName', 'nickName'],
          include: [
            {
              model: Blog,
            }
          ]
        });
        
        // output
        console.log(userListWithBlog.count,
        userListWithBlog.rows.map(user => {
            const userVal = user.dataValues;
            userVal.blogs = userVal.blogs.map(val => val.dataValues);
            return userVal;
          }
        )
      );
    

    外键有一对多,和多对一的关系

    更新数据

    const { User } = require('./model');
    
    !(async function() {
      const updateRes = await User.update(
        {
          nickName: '李四222'
        },
        {
          where: {
            userName: 'zhangsan1'
          }
        }
      );
      console.log(updateRes);
    })();
    

    删除数据

    const { User, Blog } = require('./model');
    !(async function() {
      const delBlogRes = await Blog.destroy({
        where: {
          id: 4
        }
      });
      console.log(delBlogRes);
    })();
    
    

    连接池

    const Sequelize = require('sequelize');
    
    const conf = {
      host: 'localhost',
      dialect: 'mysql'
    };
    
    // 线上环境使用的连接池
    // conf.pool = {
    //   // 连接池中的最大数量
    //   max: 5,
    //   min: 0,
    //   // 如果一个连接池10s之内没有被使用,
    //   idle: 10000
    // };
    
    const seq = new Sequelize('koa_weibo', 'root', '123456', conf);
    
    module.exports = seq;
    

    redis的使用

    打开一个 cmd 窗口 使用 cd 命令切换目录到 C:
    edis 运行:
    
    redis-server.exe redis.windows.conf
    reids-cli
    

    image-20200205154759644

    image-20200205161352362

    将cookie和session保存到redis

    安装依赖

    yarn  add koa-redis
    // 生成session的工具
    yarn add koa-generic-session
    

    jest单元测试

  • 相关阅读:
    Oc中UIKit框架的继承结构
    iOS中的事件传递和响应者链
    音频视频,音乐,录音参考资料
    对象序列化(对对象的归档NSKeyedArchiver、接档NSKeyedUnarchiver)的理解
    iOS--UI篇——UIToolbar的简单使用
    iOS--内购的使用方法
    协调-分布式锁
    协调-协调架构原理
    协调-分布式配置
    队列-生产者消费者模式
  • 原文地址:https://www.cnblogs.com/daixixi/p/12264538.html
Copyright © 2020-2023  润新知