-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstorage.ts
232 lines (199 loc) · 5.91 KB
/
storage.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { google, sheets_v4 } from 'googleapis'
import * as _ from 'lodash'
import { Errors } from './errors'
import { Query, Document } from './types'
import * as bluebird from 'bluebird'
export interface IStorage {
insert(docs: Document[]): Promise<Document[]>
find(query?: Query): Promise<Document[]>
update(
query: Query,
toUpdate: Partial<Document>,
opts?: { updatedOnce: boolean },
): Promise<Document[]>
remove(query: Query): Promise<Document[]>
load(): Promise<void>
}
export interface IStorageOptions {
db: string // google spreadsheet id
table?: string
// google spreadsheet settings
apiKey?: string
keyFile?: string
// storage settings
cacheTimeoutMs?: number // ms
}
export default class GoogleStorage implements IStorage {
private sheets: sheets_v4.Sheets
private db: string
private table: string
private schema: string[] = []
private schemaMetaStore: { [name: string]: { col: number } } = {}
private data: string[][] = new Array()
private lastUpdated: Date | null = null
private cacheTimeoutMs: number
constructor(opts: IStorageOptions) {
if (!opts.apiKey && !opts.keyFile) {
throw new Errors.StorageOptionsError()
}
const auth = opts.apiKey
? opts.apiKey
: new google.auth.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
keyFile: opts.keyFile,
})
this.sheets = google.sheets({
version: 'v4',
auth: auth || '',
})
this.db = opts.db
this.table = opts.table || 'Sheet1'
this.cacheTimeoutMs = opts.cacheTimeoutMs || 5000
}
_findRows(query?: Query): number[] {
const queryKeys = _.keys(query)
const nums: number[] = []
_.each(this.data, (row, rowNum) => {
if (!query || !queryKeys.length) {
nums.push(rowNum)
return
}
for (const fieldName of queryKeys) {
if (!this.schemaMetaStore[fieldName]) {
return
}
const colNum = this.schemaMetaStore[fieldName].col
const field = row[colNum]
if (_.toString(field) !== _.toString(query[fieldName])) {
return
}
}
nums.push(rowNum)
})
return nums
}
_rowToDoc = (row: string[]): Document => {
const doc: Document = {}
for (let colNum = 0; colNum < row.length; ++colNum) {
const field = row[colNum]
const fieldName = this.schema[colNum]
doc[fieldName] = _.toString(field)
}
return doc
}
_docToRow = (doc: Document): string[] => {
const row = new Array(this.schema.length)
for (const fieldName of _.keys(doc)) {
if (this.schemaMetaStore[fieldName]) {
row[this.schemaMetaStore[fieldName].col] = _.toString(doc[fieldName])
}
}
return row
}
async find(query?: Query): Promise<Document[]> {
await this._checkCacheTimeout()
const rowNums = this._findRows(query)
const docs = _.map(rowNums, (rowNum) => this._rowToDoc(this.data[rowNum]))
return docs
}
async insert(docs: Document[]): Promise<Document[]> {
await this._checkCacheTimeout()
const rows = _.map(docs, this._docToRow)
const range = this.table.includes('!') ? this.table : `${this.table}!A:A`
await this.sheets.spreadsheets.values.append({
spreadsheetId: this.db,
range,
valueInputOption: 'USER_ENTERED',
requestBody: {
values: rows,
},
})
this.data.push(...rows)
return _.map(rows, this._rowToDoc)
}
async update(
query: Query,
toUpdate: Document,
opts: { updatedOnce: boolean } = { updatedOnce: false },
): Promise<Document[]> {
await this._checkCacheTimeout()
const rowNums = this._findRows(query)
const { updatedOnce } = opts
if (!rowNums.length) {
return []
}
const docs: Document[] = []
await bluebird.map(
updatedOnce ? [rowNums[0]] : rowNums,
async (rowNum) => {
const oldDoc = this._rowToDoc(this.data[rowNum])
const newDoc = { ...oldDoc, ...toUpdate }
const row = this._docToRow(newDoc)
docs.push(this._rowToDoc(row))
await this.sheets.spreadsheets.values.update({
spreadsheetId: this.db,
range: `${this.table}!A${rowNum + 2}`,
valueInputOption: 'USER_ENTERED',
requestBody: {
values: [row],
},
})
this.data[rowNum] = row
},
{ concurrency: 10 },
)
return docs
}
async remove(query: Query): Promise<Document[]> {
await this._checkCacheTimeout()
const rowNums = this._findRows(query)
const emptyDoc = _.mapValues(this.schemaMetaStore, () => '')
const emptyRow = this._docToRow(emptyDoc)
const oldDoc: Document[] = []
await bluebird.map(
rowNums,
async (rowNum) => {
oldDoc.push(this._rowToDoc(this.data[rowNum]))
await this.sheets.spreadsheets.values.update({
spreadsheetId: this.db,
range: `${this.table}!A${rowNum + 2}`,
valueInputOption: 'USER_ENTERED',
requestBody: {
values: [emptyRow],
},
})
this.data[rowNum] = emptyRow
},
{ concurrency: 10 },
)
return oldDoc
}
async _checkCacheTimeout(): Promise<boolean> {
if (!this.lastUpdated || Date.now() - this.lastUpdated.getTime() >= this.cacheTimeoutMs) {
await this.load()
return true
}
return false
}
async load() {
const res = await this.sheets.spreadsheets.values.get({
range: this.table,
spreadsheetId: this.db,
})
const rows = res.data.values
if (!rows || rows.length === 0) {
throw new Errors.StorageFormatError()
}
const schema = rows[0]
const schemaMetaStore: { [key: string]: { col: number } } = _.zipObject(
rows[0],
_.map(rows[0], (_, colNum) => ({
col: colNum,
})),
)
this.schema = schema
this.schemaMetaStore = schemaMetaStore
this.data = _.slice(rows, 1)
this.lastUpdated = new Date()
}
}