neg-cloud-data

0.1.0 • Public • Published

Cloud Data Client for node.

Features

  • 通过更简洁的方式访问 cloud data,避免拼接 URL,减少错误发生。

Test

npm test

Install

npm install neg-cloud-data --save

Usage

// 方法签名
getById(collectionName, id, fields, options);

get(collectionName, config, options);

insert(collectionName, dataObj, options);

update(collectionName, dataObj, options);

updatePartial(collectionName, updatedObj, options);

upsert(collectionName, dataObj, options);

delete(collectionName, id, options);

/**
 * 条件删除数据
 */
deleteWhen(collectionName, condition, options);

/**
 * 清空整个collection的数据
 * @param collectionName 要清空的Collection
 */
clear(collectionName, options);

/**
 * 批量插入,不会校验primary key,请在调用前校验
 * @param collectionName 集合名称
 * @param itemArr 要批量插入的数据Array<Object>
 */
batchInsert(collectionName, itemArr, options);

/**
 * 批量更新,通过主键判断,有则更新,无则删除
 * @param collectionName 集合名称
 * @param itemArr 要批量插入的数据Array<Object>
 */
batchUpdate(collectionName, itemArr, options);
var CloudDataStore = require('neg-cloud-data').CloudDataStore;
// new CloudDataStore(restApiHost: string, storeName: string [, dbPwd: string])
var db = new CloudDataStore('http://10.16.75.24:3000/datastore/v1', 'cloud_data_client_test', '12345');

//insert、update、upsert、delete

// insert(collectionName: string, insertObj: object)
db.insert('users', { id: 1, name: 'insert' })
  .then(data => {
    console.log('insert succeed.');
  })
  .catch(err => console.log(err));

// update(collectionName: string, insertObj: object)
db.update('users', { id: 1, name: 'update' }).then(data => {
  done();
}, done);

// upsert(collectionName: string, insertObj: object)
db.upsert('users', { id: 1, name: 'upsert' }).then(() => done(), done);

// delete(collectionName: string, id: string|number)
db.delete('users', 1)
  .then(data => {
    done();
  })
  .catch(err => console.log(err));

//query

// getById(collectionName: string, id: string|number [, fields: array])
db.getById('users', '10').then(data => {
  //do something
});

// only return name property.
db.getById('users', '10', ['name']).then(data => {
  //do something
});

// get(collectionName: string [,config: object])
db.get('users').then(data => {
  //do something
});

db.get('users', { pageSize: 100 }).then(data => {
  //do something
});

// 按照条件删除
db.deleteWhen('users', {});

当前支持的 config 如下,更多暂未实现

{
  pageSize: number, //每页数据条数,默认10条
  pageIndex: number, //指定页码,默认1
  fields: array, //指定要查询出的属性,默认all
  sortField:string, //排序字段
  descSort: true, //排序方式,descSort只有true一个选项,其他情况则为asc
  andQuery: object, //and条件
  orQuery: object, //or条件(注意,所有or条件会先匹配,然后和其他and条件一起and)
}

andQuery 和 orQuery 支持的操作符号如下:

使用 In 操作符:EmployeeNumber={"$in":[9527, 9528]}

使用 NotIn 操作符:EmployeeNumber={"$nin":[9527, 9528]}

使用 Greater 操作符:EmployeeNumber={"$gt":9527}

使用 GreaterEqual 操作符:EmployeeNumber={"$gte":9527}

使用 Less 操作符:EmployeeNumber={"$lt":9527}

使用 LessEqual 操作符:EmployeeNumber={"$lte":9527}

使用 GreaterAndLess 操作符:EmployeeNumber={"$gt":9527, "$lt": 9528}

使用 Between 操作符:EmployeeNumber={"$gte":9527, "$lte": 9528}

使用 Match 操作符:Name={"$match":"/abc/i"} ,对 Name 字段进行正则查询,可以理解为 Name like '%abc%'

配置样例:

var query = {
  pageSize: 100,
  pageIndex: 1,
  sortField: 'id',
  descSort: true,
  fields: ['id'],
  andQuery: {
    id: { $gte: 1, $lte: 11 }
  },
  orQuery: {
    id: '10'
  }
};

0.0.6 新增

0.0.6 版本中,新增了批量更新和批量插入功能,用法如下:

var CloudDataStore = require('neg-cloud-data').CloudDataStore;
// new CloudDataStore(restApiHost: string, storeName: string [, dbPwd: string])
var db = new CloudDataStore('http://10.16.75.24:3000/datastore/v1', 'cloud_data_client_test', '12345');

// 批量新增多个数据
db.batchInsert('users', [{ userId: 1, userName: 'user1' }, { userId: 2, userName: 'user2' }])
  .then(successCallback)
  .catch(errCallback);

// 批量更新多个数据
db.batchUpdate('users', [{ userId: 1, userName: 'user1' }, { userId: 2, userName: 'user2' }])
  .then(successCallback)
  .catch(errCallback);

0.1.0 新增

0.1.0 版本中,增加了自定义请求 header 功能,使用方式如下:

// 初始化DB的时候,第四个参数为 options,如果指定了此处的 processRequestOptions 方法,则可以自定义options,注意,必须要返回。
const db = new CloudDataStore('http://10.16.75.24:3000/datastore/v1', 'cloud_data_client_test', '12345', {
  processRequestOptions(reqOptions) {
    reqOptions.headers['test'] = 666;
    testOptions = reqOptions;
    return reqOptions;
  }
});

对于每个操作方法,到新增了 options,可以通过如下代码,传递 自定义headers

db.getById('users', '10', { headers: {'custom-header': 12345 } }).then(data => {
  //do something
});

Readme

Keywords

Package Sidebar

Install

npm i neg-cloud-data

Weekly Downloads

3

Version

0.1.0

License

MIT

Unpacked Size

22.5 kB

Total Files

13

Last publish

Collaborators

  • hm910705