-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPostgresStorage.ts
199 lines (172 loc) · 5.07 KB
/
PostgresStorage.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
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
/***********************************************************************************
*
* botbuilder-storage-postgres
* Copyright 2019 TD Ameritrade. Released under the terms of the MIT license.
*
***********************************************************************************/
import { Storage, StoreItems } from "botbuilder";
import { Sequelize, Model, DataTypes, Op } from "sequelize";
export interface PostgresStorageConfig {
uri: string;
collection?: string;
logging?: boolean | ((sql: string, timing?: number) => void);
}
class PostgresStoreItem extends Model {
public id!: number;
public data!: JSON;
}
export class PostgresStorageError extends Error {
public static readonly NO_CONFIG_ERROR: PostgresStorageError = new PostgresStorageError(
"PostgresStorageConfig is required."
);
public static readonly NO_URI_ERROR: PostgresStorageError = new PostgresStorageError(
"PostgresStorageConfig.uri is required."
);
}
interface PostgresStoreItem {
id: number;
data: JSON;
}
export class PostgresStorage implements Storage {
private config: PostgresStorageConfig;
private connection: Sequelize;
static readonly DEFAULT_COLLECTION_NAME: string = `state`;
constructor(config: PostgresStorageConfig) {
this.config = PostgresStorage.ensureConfig({ ...config });
}
public static ensureConfig(
config: PostgresStorageConfig
): PostgresStorageConfig {
if (!config) {
throw PostgresStorageError.NO_CONFIG_ERROR;
}
if (!config.uri || config.uri.trim() === "") {
throw PostgresStorageError.NO_URI_ERROR;
}
if (!config.collection || config.collection.trim() == "") {
config.collection = PostgresStorage.DEFAULT_COLLECTION_NAME;
}
return config as PostgresStorageConfig;
}
public async connect(): Promise<Sequelize> {
const sequelize = new Sequelize(this.config.uri, {
// ...options
dialect: "postgres",
pool: {
max: 10,
min: 0,
acquire: 30000,
idle: 10000
},
logging: this.config.logging
});
await PostgresStoreItem.init(
{
id: {
type: DataTypes.STRING,
primaryKey: true
},
data: {
type: DataTypes.JSONB,
allowNull: false
}
},
{ sequelize, tableName: this.config.collection, timestamps: false }
);
await PostgresStoreItem.sync();
this.connection = sequelize;
return this.connection;
}
public async ensureConnected(): Promise<Sequelize> {
if (!this.connection) {
await this.connect();
}
return this.connection;
}
public async read(stateKeys: string[]): Promise<StoreItems> {
if (!stateKeys || stateKeys.length == 0) {
return {};
}
await this.ensureConnected();
const items = await PostgresStoreItem.findAll({
where: { id: { [Op.in]: stateKeys } }
});
return await items.reduce((accum, item): StoreItems => {
accum[item.id] = item.data;
return accum;
}, {});
}
public async write(changes: StoreItems): Promise<void> {
if (!changes || Object.keys(changes).length === 0) {
return;
}
await this.ensureConnected();
async function asyncForEach(
array: any[],
callback: {
(key: string): Promise<void>;
(arg0: any, arg1: number, arg2: any[]): void;
}
) {
for (let index: number = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
const writeAsync = async () => {
await asyncForEach(
Object.keys(changes),
async (key: string): Promise<void> => {
const query = `INSERT INTO ${PostgresStoreItem.tableName} (id, data)
VALUES (:id, :data)
ON CONFLICT (id) DO UPDATE SET data = ${PostgresStoreItem.tableName}.data || :data`;
await this.connection.query(query, {
replacements: {
id: key,
data: JSON.stringify(changes[key])
}
});
}
);
};
writeAsync();
}
public async delete(keys: string[]): Promise<void> {
if (!keys || keys.length == 0) {
return;
}
await this.ensureConnected();
await PostgresStoreItem.destroy({ where: { id: { [Op.in]: keys } } });
}
// public static shouldSlam(etag: string): boolean {
// return etag === "*" || !etag;
// }
public static randHex(n: number): number {
if (n <= 0) {
return null;
}
let rs: number;
try {
rs = Math.ceil(n / 2);
/* note: could do this non-blocking, but still might fail */
} catch (ex) {
rs += Math.random();
}
return rs;
}
// public static createFilter(key: string, etag: any): object {
// if (this.shouldSlam(etag)) {
// return { id: key };
// }
// return { id: key, "state.eTag": etag };
// }
get Sequelize(): Sequelize {
return this.connection;
}
public async close(): Promise<void> {
if (this.connection) {
await this.connection.close();
delete this.connection;
}
}
}