0.0.136 • Published 8 years ago

cg_model v0.0.136

Weekly downloads
4
License
-
Repository
github
Last release
8 years ago

cg_model是一个用来同步更新db和cache数据的模块,目前db支持mysql(即时写、缓写、分库写),cache支持redis。

一. 基本概念

当一个对象的数据发生变化后,需要更新至db和cache中,这时就涉及到了数据同步问题。

二. 配置选项

三. 成员函数

  • create

  • load

  • update

  • remove

四. 静态函数

  • find

  • findAll

  • remove

  • removeAll

  • count

  • countAll

五. 分库分表

举例来说明使用的方法: 1、使用如下sql创建5个数据库 test test_shard_00 test_shard_01 test_shard_02 test_shard_03 其中test为主库,其他的test_shard_xx为分库。

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
  `userId` int(11) unsigned NOT NULL ,
  `money` int(10) unsigned NOT NULL,
  PRIMARY KEY (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `userId`;

CREATE TABLE `userId` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DELIMITER ;;

# Dump of PROCEDURE gen_userId
# ------------------------------------------------------------

/*!50003 DROP PROCEDURE IF EXISTS `gen_userId` */;;
/*!50003 CREATE*/ /*!50003 PROCEDURE `gen_userId`(IN count INT)
BEGIN
  IF NOT EXISTS (SELECT 1 FROM userId LIMIT 1) THEN
    INSERT INTO userId (id) VALUES(1);
  END IF;

  UPDATE userId SET id = LAST_INSERT_ID(id)+count;
    SELECT LAST_INSERT_ID() AS id;
END */;;

DELIMITER ;

2、添加mysql.json,内容如下:

{
  "test_main": {
    "connectionLimit": 10,
    "host": "localhost",
    "port": 3306,
    "user": "root",
    "password": "123456",
    "multipleStatements": true
  },
  "test_0": {
    "connectionLimit": 10,
    "host": "localhost",
    "port": 3306,
    "user": "root",
    "password": "123456",
    "multipleStatements": true
  },
  "test_1": {
    "connectionLimit": 10,
    "host": "localhost",
    "port": 3306,
    "user": "root",
    "password": "123456",
    "multipleStatements": true
  }
}

3、添加cg_model.json,内容如下:

{
    "mysql_shard": {
        "cron": "*/30 * * * * *",
        "batchCount": 100, 
        "shard_count": 4,
        "database": {
            "test": {
                "main_db": "test",
                "shard_db_format": "test_shard_%02d",
                "connection": {
                    "test_main": [
                        "test"
                    ],
                    "test_0": [
                        "test_shard_00",
                        "test_shard_01"
                    ],
                    "test_1": [
                        "test_shard_02",
                        "test_shard_03"
                    ]
                }
            }
        }
    }
}

batchCount:批量查询的数量。 shard_count:分库数量。 database:配置分库对应的mysql连接,其中connection中的key对应mysql.json中的连接配置,value是一个数组,存放一个或多个分库名。

4、创建一个model.js,文件中定义了要使用的model:

'use strict';
var CGModel = require('cg_model');

//用于创建userId
var genUserId = function(cb) {
  var dbName = this.db.getMainDBName();
  var sql = 'CALL `' + dbName + '`.`gen_userId`(1);';
  var conn = CGModel.getMysqlShardDBConn(dbName);
  conn.query(sql, [], function(err, res) {
    if (!!err) {
      cb(err);
      return;
    }
    cb(null, res[0][0].id);
  });
}

CGModel.createModel({
  name: 'User',          //用于获取model的名称

  //字段应与数据库的table或view的列名对应起来
  props: {
    userId:             { type: 'number', primary: true, defaultValue: genUserId, shard: true },
    money:              { type: 'number', defaultValue: 0, },
  },

  db: {
    type: 'mysql_shard',                //读取cg_model.json中的对应配置
    db_name: 'test',                    //读取cg_model.json中的对应配置
    tbl_name: 'user',                   //指定mysql中的table或view的名称
  },
  //我们暂时不关心cache
  cache: {
    type: 'none' 
  },
});

5、添加app.js文件:

'use strict';

var mysql = require('mysql');
var redis = require('redis');
var async = require('async');
var CGModel = require('cg_model');

//设定mysql的连接
var key, config, client;
var mysqlConfig = require('./mysql.json');
for (key in mysqlConfig) {
    config = mysqlConfig[key];
    client = mysql.createPool(config);
    CGModel.setDBClient(key, client);
}

//初始化配置
CGModel.initialize(require('./cg_model.json'));

//定义model
require('./model.js')

//创建100个user
var User = CGModel.getModel('User');
async.timesSeries(100, function(idx, cb) {
    var user = new User();
    user.createSync(cb);
}, function(err) {
    if (err) {
        console.error(err)
    } else {
        console.log('done');
    }
    process.exit(0);
});

6、运行node app.js后,可以看到4个分库中都有了数据。

0.0.136

8 years ago

0.0.135

8 years ago

0.0.134

8 years ago

0.0.133

8 years ago

0.0.132

8 years ago

0.0.131

8 years ago

0.0.130

9 years ago

0.0.129

9 years ago

0.0.128

9 years ago

0.0.127

9 years ago

0.0.125

9 years ago

0.0.124

9 years ago

0.0.123

9 years ago

0.0.122

9 years ago

0.0.121

9 years ago

0.0.120

9 years ago

0.0.119

9 years ago

0.0.118

9 years ago

0.0.116

9 years ago

0.0.115

9 years ago

0.0.114

9 years ago

0.0.113

9 years ago

0.0.112

9 years ago

0.0.111

9 years ago

0.0.110

9 years ago

0.0.109

9 years ago

0.0.108

9 years ago

0.0.107

9 years ago

0.0.106

9 years ago

0.0.105

9 years ago

0.0.104

9 years ago

0.0.103

9 years ago

0.0.102

9 years ago

0.0.101

9 years ago

0.0.100

9 years ago

0.0.99

9 years ago

0.0.98

9 years ago

0.0.97

9 years ago

0.0.96

9 years ago

0.0.95

9 years ago

0.0.94

9 years ago

0.0.93

9 years ago

0.0.92

9 years ago

0.0.91

9 years ago

0.0.90

9 years ago

0.0.89

9 years ago

0.0.88

9 years ago

0.0.87

9 years ago

0.0.86

9 years ago

0.0.85

9 years ago

0.0.84

9 years ago

0.0.83

9 years ago

0.0.82

9 years ago

0.0.81

9 years ago

0.0.80

9 years ago

0.0.79

9 years ago

0.0.78

9 years ago

0.0.77

9 years ago

0.0.76

9 years ago

0.0.75

9 years ago

0.0.74

9 years ago

0.0.73

9 years ago

0.0.72

9 years ago

0.0.71

9 years ago

0.0.69

9 years ago

0.0.68

9 years ago

0.0.67

9 years ago

0.0.66

9 years ago

0.0.63

9 years ago

0.0.62

9 years ago

0.0.61

9 years ago

0.0.60

9 years ago

0.0.59

9 years ago

0.0.58

9 years ago

0.0.57

9 years ago

0.0.56

9 years ago

0.0.55

9 years ago

0.0.54

9 years ago

0.0.53

9 years ago

0.0.52

9 years ago

0.0.51

9 years ago

0.0.50

9 years ago

0.0.49

9 years ago

0.0.48

9 years ago

0.0.47

9 years ago

0.0.46

9 years ago

0.0.45

9 years ago

0.0.44

9 years ago

0.0.43

9 years ago

0.0.42

9 years ago

0.0.41

9 years ago

0.0.40

9 years ago

0.0.39

9 years ago

0.0.38

9 years ago

0.0.37

9 years ago

0.0.36

9 years ago

0.0.35

9 years ago

0.0.34

9 years ago

0.0.33

9 years ago

0.0.32

9 years ago

0.0.31

9 years ago

0.0.29

9 years ago

0.0.28

9 years ago

0.0.27

9 years ago

0.0.26

9 years ago

0.0.25

9 years ago

0.0.24

9 years ago

0.0.23

9 years ago

0.0.22

9 years ago

0.0.21

9 years ago

0.0.20

9 years ago

0.0.19

9 years ago

0.0.18

9 years ago

0.0.17

9 years ago

0.0.16

9 years ago

0.0.15

9 years ago

0.0.14

9 years ago

0.0.13

9 years ago

0.0.12

9 years ago

0.0.11

9 years ago

0.0.10

9 years ago

0.0.9

9 years ago

0.0.8

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago