-
Notifications
You must be signed in to change notification settings - Fork 2
/
db-io.ts
167 lines (159 loc) · 4.01 KB
/
db-io.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Copyright 2016 Stephane M. Catala
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* Limitations under the License.
*/
;
import { Observable } from '@reactivex/rxjs'
import { WriteOpts, ReadOpts, DocRef, DocIdRange, DocRevs, DocId } from './'
import newCoreDbIo, { CoreDbIo, isCoreDbIoLike } from './core-db-io'
import { logRx, isObject, isFunction } from './utils'
/**
* @public
* @factory DbIoFactory
* @param {DbIoFactorySpec} spec
* @return {DbIo}
*/
export interface DbIoFactory {
(db: any, spec: DbIoFactorySpec): DbIo
}
/**
* @public
* @interface DbIoFactorySpec
*/
export interface DbIoFactorySpec {
/**
* @public
* @prop {WriteOpts} write
*/
write: WriteOpts,
/**
* @public
* @prop {ReadOpts} read
*/
read: ReadOpts,
/**
* @public
* @prop {{ write?: CoreDbIo, read?: CoreDbIo }} coreDbIo
*/
coreDbIo?: {
write?: CoreDbIo
read?: CoreDbIo
}
}
/**
* @public
* @interface DbIo
* a pair of IO methods with which to store or read documents
* to/from the wrapped {PouchDB} database.
*/
export interface DbIo {
/**
* @public
* @method write
* store input documents to the wrapped {PouchDB} database.
* @param {DocRef[]|DocRef} docs
* @return {Observable<DocRef[]|DocRef>}
*/
write (docs: DocRef[]|DocRef): Observable<DocRef[]|DocRef>
/**
* @public
* @method read
* read documents from the wrapped {PouchDB} database.
* @param {DocRef[]|DocIdRange|DocRevs|DocRef} refs
* @return {Observable<DocRef[]|DocRef>}
*/
read (refs: DocRef[]|DocIdRange|DocRevs|DocRef): Observable<DocRef[]|DocRef>
}
/**
* @public
* @function DbIoDuckTypable
* duck-type validation: checks for `write` and `read` methods.
* @prop {any} val?
* @return {val is DbIo}
*/
export interface DbIoDuckTypable {
(val?: any): val is DbIo
}
/**
* @private
* @class DbIoClass
* @implements {DbIo}
*/
class DbIoClass implements DbIo {
/**
* @public
* @see {DbIoFactory}
*/
static newInstance: DbIoFactory =
function (db: any, spec: DbIoFactorySpec): DbIo {
const coreDbIo = {
write: getCoreDbIo(db, 'write', spec.coreDbIo || spec.write),
read: getCoreDbIo(db, 'read', spec.coreDbIo || spec.read)
}
return new DbIoClass(coreDbIo)
}
/**
* @public
* @see {DbIoDuckTypable}
*/
static isDbIoLike: DbIoDuckTypable =
function (val?:any): val is DbIo {
return isObject(val) && isFunction(val.write) && isFunction(val.read)
}
/**
* @public
* @see {DbIo#write}
*/
write (src: DocRef[]|DocRef): Observable<DocRef[]|DocRef> {
return Observable.fromPromise(this.coreDbIo.write.access(src))
}
/**
* @public
* @see {DbIo#read}
*/
read (src: DocRef[]|DocIdRange|DocRevs|DocRef): Observable<DocRef[]|DocRef> {
return Observable.fromPromise(this.coreDbIo.read.access(src))
}
/**
* @private
* @constructor
* @param {{ write: CoreDbIo, read: CoreDbIo }} coreDbIo
*/
constructor (private coreDbIo: { write: CoreDbIo, read: CoreDbIo }) {}
}
/**
* @private
* @function getCoreDbIo
* @param {PouchDb} db
* @param {'write'|'read'} type
* @param {CoreDbIo|WriteOpts|ReadOpts} opts
* @return {CoreDbIo}
*/
function getCoreDbIo (db: any, type: 'write'|'read',
opts: CoreDbIo|WriteOpts|ReadOpts): CoreDbIo {
return isCoreDbIoLike(opts) ? <CoreDbIo> opts : newCoreDbIo({
db: db,
type: type,
opts: <WriteOpts|ReadOpts> opts
})
}
/**
* @public
* @see {DbIoDuckTypable}
*/
export const isDbIoLike = DbIoClass.isDbIoLike
/**
* @public
* @see {DbIoFactory}
*/
const newDbIo: DbIoFactory = DbIoClass.newInstance
export default newDbIo