forked from pryv/open-pryv.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalStoreEventQueries.ts
More file actions
81 lines (70 loc) · 2.39 KB
/
localStoreEventQueries.ts
File metadata and controls
81 lines (70 loc) · 2.39 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
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
/**
* @license
* Copyright (C) Pryv https://pryv.com
* This file is part of Pryv.io and released under BSD-Clause-3 License
* Refer to LICENSE file
*/
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
/**
* Converters to common query logic for localStores
* Might by moved tp @pryv/datastore repo
*/
const timestamp = require('unix-timestamp');
const DELTA_TO_CONSIDER_IS_NOW = 5; // 5 seconds
export { localStorePrepareOptions, localStorePrepareQuery };
/**
* Convert store API options params to local store options
*/
function localStorePrepareOptions (options: any) {
const localOptions = {
sort: { time: options.sortAscending ? 1 : -1 },
skip: options.skip,
limit: options.limit
};
return localOptions;
}
/**
* Convert store API query params to an array of queries
*/
function localStorePrepareQuery (query: any) {
const localQuery: any[] = [];
// trashed
switch (query.state) {
case 'trashed':
localQuery.push({ type: 'equal', content: { field: 'trashed', value: true } });
break;
case 'all':
break;
default:
localQuery.push({ type: 'equal', content: { field: 'trashed', value: false } });
}
// modified since
if (query.modifiedSince != null) {
localQuery.push({ type: 'greater', content: { field: 'modified', value: query.modifiedSince } });
}
// types
if (query.types && query.types.length > 0) {
localQuery.push({ type: 'typesList', content: query.types });
}
// if streams are defined
if (query.streams && query.streams.length !== 0) {
localQuery.push({ type: 'streamsQuery', content: query.streams });
}
// -------------- time selection -------------- //
if (query.toTime != null) {
localQuery.push({ type: 'lowerOrEqual', content: { field: 'time', value: query.toTime } });
}
// running
if (query.running) {
localQuery.push({ type: 'equal', content: { field: 'endTime', value: null } });
} else if (query.fromTime != null) {
const now = timestamp.now() - DELTA_TO_CONSIDER_IS_NOW;
if (query.fromTime <= now && (query.toTime == null || query.toTime >= now)) { // timeFrame includes now
localQuery.push({ type: 'greaterOrEqualOrNull', content: { field: 'endTime', value: query.fromTime } });
} else {
localQuery.push({ type: 'greaterOrEqual', content: { field: 'endTime', value: query.fromTime } });
}
}
return localQuery;
}