forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema-info.js
More file actions
56 lines (49 loc) · 1.25 KB
/
schema-info.js
File metadata and controls
56 lines (49 loc) · 1.25 KB
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
const ensureJson = require('./ensure-json');
class SchemaInfo {
/**
* @param {import('../sequelize-db')}
* @param {import('../lib/config')} config
*/
constructor(sequelizeDb, config) {
this.sequelizeDb = sequelizeDb;
this.config = config;
}
/**
* Get schemaInfo for schema id
* @param {string} schemaCacheId
*/
async getSchemaInfo(schemaCacheId) {
const doc = await this.sequelizeDb.Cache.findOne({
where: { id: schemaCacheId },
});
if (!doc) {
return;
}
return ensureJson(doc.data);
}
/**
* Save schemaInfo to cache db object
* @param {string} schemaCacheId
* @param {object} schema
*/
async saveSchemaInfo(schemaCacheId, schema) {
const id = schemaCacheId;
const existing = await this.sequelizeDb.Cache.findOne({ where: { id } });
const ONE_DAY = 1000 * 60 * 60 * 24;
const expiryDate = new Date(Date.now() + ONE_DAY);
if (!existing) {
return this.sequelizeDb.Cache.create({
id,
data: schema,
expiryDate,
name: 'schema cache',
});
} else {
return this.sequelizeDb.Cache.update(
{ data: schema, expiryDate, name: 'schema cache' },
{ where: { id } }
);
}
}
}
module.exports = SchemaInfo;