xdriver 2.0.2 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +139 -100
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,108 +1,147 @@
1
- ## 文档懒得写,更详细API查看源码 目录:src/ts/
1
+ # 说明
2
2
 
3
- ### indexDB
3
+ ## 初始化Connection
4
4
 
5
+ ```javascript
6
+ import {KeyRange, Driver} from 'xdriver';
7
+ // 初始化数据库驱动
8
+ const driver = new Driver('demo', version);
9
+ // 定义表结构以及初始化数据
10
+ driver.defineTables({
11
+ name: 'player',
12
+ primaryKey: 'id',
13
+ autoIncrement: true,
14
+ indexes: [
15
+ {name: 'age_index', column: 'age'},
16
+ {name: 'name_index', column: 'name'},
17
+ {name: 'double_index', column: ['sex', 'age']},
18
+ ],
19
+ rows: [{name: 'xxx', age: '', sex: ''}] //初始化数据
20
+ });
21
+ const connect = await driver.connect();
5
22
  ```
6
- import Driver from 'ndriver';
7
- const driver = new Driver('demo', version?);
8
- driver.on("process", function (state) {
9
- console.log(state)
10
- })
11
- driver.open().then(function () {
12
- var arrPlayers = []
13
- for(let i=0; i<100; i++) {
14
- arrPlayers.push({
15
- name: `韩梅梅${i}`,
16
- sex: `女${i}`
17
- })
18
- }
19
- driver.createTable({
20
- name: 'player',
21
- primaryKey: 'id',
22
- autoIncrement: true,
23
- indexes: [
24
- {name: 'name_index', column: 'name', unique: true},
25
- ],
26
- data : arrPlayers
27
- })
28
- })
23
+
24
+ ## 查询操作
25
+
26
+ ```javascript
27
+ const player = connect.table('player');
28
+
29
+ let results = await player.queryByIndex('age_index', KeyRange.between(10, 20));
30
+ results = await player.queryByIndex('double_index', KeyRange.eq(['女3', 0]));
31
+ results = await player.query(KeyRange.between(10, 20))
32
+ results = connect.queryByIndex('tableName', 'indexName', KeyRange.between(10, 20))
29
33
  ```
30
34
 
31
- ### API
32
- #### constructor(name, version?)
33
- name: 数据库名字
34
- version: 数据库版本 默认1
35
- eg: var driver = new Driver('database')
36
- ### open
37
- 打开数据库连接 返回promise对象 在resolve中可以创建表或者删除表
38
- driver.open().then(() => {
39
- driver.createTable({
40
- name: 'table',
41
- primaryKey: 'key',
42
- indexes: [
43
- {name: 'name_index', column: 'key', unique: true},
44
- ],
45
- })
46
- })
47
- #### Driver.prototype.insert(table, data)
48
- table: 表名
49
- data: 需要插入的数据 obj or array
50
- eg: driver.insert({key: 'key', name: '', ...})
51
- #### Driver.prototype.select(table, keyRange?, limit?)
52
- table: 表名
53
- keyRange: KeyRange 包含多种比较操作
54
- limit: 取出记录数
55
- eg: driver.select('table', keyRange.eq('key'), 1).then(rs => console.log(rs))
56
-
57
- #### Driver.prototype.selectByKey(table, key)
58
- table:表名
59
- key: 主键
60
- eg: driver.selectByKey('key').then(rs => console.log(rs))
61
-
62
- #### Driver.prototype. count (table, keyRange?)
63
- 根据条件统计记录数
64
- table: 表名
65
- keyRange: @see select
66
- eg: driver.count('table').then(size => console.log(size))
67
- #### Driver.prototype. update (table, modify, where?)
68
- 数据更新,如果数据不存在则新增
69
- table: 表名
70
- modify: 修改的对象
71
- where:条件
72
- eg: driver.update('table', {name: 'xxxx'}, {key: 'key'})
73
- #### Driver.prototype.delete (table, key)
74
- table: 表名
75
- key: 主键
76
- 根据主键删除
77
- eg: driver.delete('table', 'key')
78
-
79
- #### Driver.prototype.truncate(table)
80
- 清空某张表
81
- table:表名
82
-
83
- ### 示例
35
+ ## API
36
+
37
+ ### Connection
84
38
 
85
39
  ```
86
- const driver = new Driver('pallet', 1);
87
- driver.on("process", function (state) {
88
- console.log(state)
89
- })
90
- driver.open().then(function () {
91
- var arrPlayers = []
92
- for(let i=0; i<100; i++) {
93
- arrPlayers.push({
94
- name: `韩梅梅${i}`,
95
- sex: `女${i}`
96
- })
97
- }
98
- driver.createTable({
99
- name: 'player',
100
- primaryKey: 'id',
101
- autoIncrement: true,
102
- indexes: [
103
- {name: 'name_index', column: 'name', unique: true},
104
- ],
105
- data : arrPlayers
106
- })
107
- })
40
+ /**
41
+ * 查询数据
42
+ * @param table 表名
43
+ * @param query 查询条件
44
+ * @param count 查询数量
45
+ */
46
+ query<R extends Row>(table: string, query?: IDBValidKey | IDBKeyRange, count: number = 1000): Promise<RowPacket<R>>;
47
+
48
+ /**
49
+ * 查询数据
50
+ * @param table 表名
51
+ * @param key 查询条件
52
+ * @param index 索引名称
53
+ * @param count 查询数量
54
+ */
55
+ select<R extends Row>(table: string, key?: IDBValidKey | IDBKeyRange, index?: string, count: number = 1000): Promise<RowPacket<R>>;
56
+
57
+ /**
58
+ * 获取数据
59
+ * @param table 表名
60
+ * @param key 查询条件
61
+ */
62
+ queryByKey<R extends Row>(table: string, key: IDBValidKey | IDBKeyRange): Promise<RowPacket<R>>;
63
+
64
+ /**
65
+ *
66
+ * @param table
67
+ * @param indexName 索引名称
68
+ * @param key key or keyRange (对应的是索引包含的列的数据)
69
+ * @param count 返回的列数
70
+ */
71
+ async queryByIndex<R extends Row>(table: string, indexName: string, key?: IDBValidKey | IDBKeyRange, count?: number): Promise<RowPacket<R>>;
72
+
73
+ /**
74
+ * 获取数据
75
+ * @param table
76
+ * @param id
77
+ * @param indexName
78
+ */
79
+ async queryById<R extends Row>(table: string, id: any | Array<any>, indexName?: string): Promise<R>;
80
+
81
+ /**
82
+ * 获取数据
83
+ * @param table 表名
84
+ * @param key 查询条件
85
+ */
86
+ count(table: string, key: IDBValidKey | IDBKeyRange);
87
+
88
+ /**
89
+ * 插入数据
90
+ * @param table
91
+ * @param data
92
+ */
93
+ insert(table: string, data: Array<Record<string, any>>);
94
+
95
+ /**
96
+ * 更新数据
97
+ * @param table 表名
98
+ * @param modify 更新内容
99
+ * @param key 条件
100
+ */
101
+ update(table: string, modify: Row, key?: IDBValidKey | IDBKeyRange | null);
102
+
103
+ /**
104
+ * 删除数据
105
+ * @param table 表名
106
+ * @param key 索引名称
107
+ */
108
+ delete(table: string, key: IDBValidKey | IDBKeyRange);
109
+
110
+ /**
111
+ * 清空表
112
+ * @param table 表名
113
+ */
114
+ truncate(table: string);
115
+
116
+ /**
117
+ * 获取内存分页模型对象
118
+ * @param table
119
+ * @param pageSize
120
+ */
121
+ getPagination<R extends Row>(table: string, pageSize: number = 10): Promise<Pagination<R>>;
122
+
123
+ /**
124
+ * 扫描数据
125
+ * @param table 表名
126
+ * @param key key or keyRange (对应的是主键的数据)
127
+ * @param direction 游标方向
128
+ * @param indexName 索引名称
129
+ */
130
+ async scan<R extends Row>(table: string, key?: IDBValidKey | IDBKeyRange, direction: IDBCursorDirection = 'next', indexName?: string): Promise<RowPacket<R>>;
131
+
132
+ /**
133
+ * 获取所有数据
134
+ * @param table
135
+ */
136
+ getAllData<R extends Row>(table: string): Promise<Array<R>>;
137
+
138
+ /**
139
+ * 分页查询
140
+ * @param table
141
+ * @param pageNo 页码
142
+ * @param pageSize 每页数量
143
+ * @param key (对应的是主键的数据)
144
+ * @param indexName 索引名称
145
+ */
146
+ async paginate<R extends Row>(table: string, pageNo: number = 1, pageSize: number = 10, key?: IDBValidKey | IDBKeyRange, indexName?: string): Promise<RowPacket<R>>;
108
147
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xdriver",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "A simple driver for IndexDB",
5
5
  "main": "./lib/index",
6
6
  "files": [
@@ -16,7 +16,7 @@
16
16
  "url": "https://gitee.com/xxx/indexdb.git"
17
17
  },
18
18
  "keywords": [
19
- "storage"
19
+ "indexdb"
20
20
  ],
21
21
  "author": "xs",
22
22
  "license": "ISC",