@fjell/core 4.4.0 → 4.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ import { AllItemTypeArrays } from './keys';
2
+ export declare class AItemService<S extends string, L1 extends string, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
3
+ private pkType;
4
+ private parentService;
5
+ constructor(pkType: S, parentService?: AItemService<L1, L2, L3, L4, L5, never>);
6
+ getPkType: () => S;
7
+ getKeyTypes: () => AllItemTypeArrays<S, L1, L2, L3, L4, L5>;
8
+ }
@@ -0,0 +1,38 @@
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value: value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
+ }
14
+ class AItemService {
15
+ constructor(pkType, parentService){
16
+ _define_property(this, "pkType", void 0);
17
+ _define_property(this, "parentService", null);
18
+ _define_property(this, "getPkType", ()=>{
19
+ return this.pkType;
20
+ });
21
+ _define_property(this, "getKeyTypes", ()=>{
22
+ let keyTypes = [
23
+ this.getPkType()
24
+ ];
25
+ if (this.parentService) {
26
+ keyTypes = keyTypes.concat(this.parentService.getKeyTypes());
27
+ }
28
+ return keyTypes;
29
+ });
30
+ this.pkType = pkType;
31
+ if (parentService) {
32
+ this.parentService = parentService;
33
+ }
34
+ }
35
+ }
36
+
37
+ export { AItemService };
38
+ //# sourceMappingURL=AItemService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AItemService.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,16 @@
1
+ export declare class Dictionary<T, V> {
2
+ protected map: {
3
+ [key: string]: V;
4
+ };
5
+ protected hashFunction: (key: T) => string;
6
+ constructor(map?: {
7
+ [key: string]: V;
8
+ }, hashFunction?: (key: T) => string);
9
+ set(key: T, item: V): void;
10
+ get(key: T): V | null;
11
+ delete(key: T): void;
12
+ keys(): T[];
13
+ values(): V[];
14
+ includesKey(key: T): boolean;
15
+ clone(): Dictionary<T, V>;
16
+ }
@@ -0,0 +1,67 @@
1
+ import LibLogger from './logger.js';
2
+
3
+ function _define_property(obj, key, value) {
4
+ if (key in obj) {
5
+ Object.defineProperty(obj, key, {
6
+ value: value,
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true
10
+ });
11
+ } else {
12
+ obj[key] = value;
13
+ }
14
+ return obj;
15
+ }
16
+ const logger = LibLogger.get("Dictionary");
17
+ class Dictionary {
18
+ set(key, item) {
19
+ logger.trace('set', {
20
+ key,
21
+ item
22
+ });
23
+ const hashedKey = this.hashFunction(key);
24
+ this.map[hashedKey] = item;
25
+ }
26
+ get(key) {
27
+ logger.trace('get', {
28
+ key
29
+ });
30
+ const hashedKey = this.hashFunction(key);
31
+ return this.map[hashedKey] || null;
32
+ }
33
+ delete(key) {
34
+ logger.trace('delete', {
35
+ key
36
+ });
37
+ const hashedKey = this.hashFunction(key);
38
+ delete this.map[hashedKey];
39
+ }
40
+ keys() {
41
+ return Object.keys(this.map).map((key)=>JSON.parse(key));
42
+ }
43
+ values() {
44
+ return Object.values(this.map);
45
+ }
46
+ includesKey(key) {
47
+ return Object.keys(this.map).includes(this.hashFunction(key));
48
+ }
49
+ clone() {
50
+ const clonedMap = Object.assign({}, this.map);
51
+ const clone = new Dictionary(clonedMap, this.hashFunction);
52
+ return clone;
53
+ }
54
+ constructor(map, hashFunction){
55
+ _define_property(this, "map", {});
56
+ _define_property(this, "hashFunction", (key)=>JSON.stringify(key));
57
+ if (map) {
58
+ this.map = map;
59
+ }
60
+ if (hashFunction) {
61
+ this.hashFunction = hashFunction;
62
+ }
63
+ }
64
+ }
65
+
66
+ export { Dictionary };
67
+ //# sourceMappingURL=dictionary.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dictionary.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,11 @@
1
+ export * from './dictionary';
2
+ export * from './keys';
3
+ export * from './items';
4
+ export { IFactory } from './item/IFactory';
5
+ export { AItemService } from './AItemService';
6
+ export * from './key/KUtils';
7
+ export * from './item/IFactory';
8
+ export * from './item/IQFactory';
9
+ export * from './item/IQUtils';
10
+ export * from './item/IUtils';
11
+ export * from './item/ItemQuery';
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ export { Dictionary } from './dictionary.js';
2
+ export { IFactory } from './item/IFactory.js';
3
+ export { AItemService } from './AItemService.js';
4
+ export { abbrevIK, abbrevLKA, cPK, constructPriKey, generateKeyArray, ikToLKA, isComKey, isComKeyEqual, isItemKey, isItemKeyEqual, isLocKey, isLocKeyEqual, isPriKey, isPriKeyEqual, isValidComKey, isValidItemKey, isValidLocKey, isValidLocKeyArray, isValidPriKey, itemKeyToLocKeyArray, lkaToIK, locKeyArrayToItemKey, primaryType, toKeyTypeArray } from './key/KUtils.js';
5
+ export { IQFactory } from './item/IQFactory.js';
6
+ export { abbrevAgg, abbrevCompoundCondition, abbrevCondition, abbrevQuery, abbrevRef, isQueryMatch, paramsToQuery, queryToParams } from './item/IQUtils.js';
7
+ export { isComItem, isPriItem, validateKeys, validatePK } from './item/IUtils.js';
8
+ export { isCondition } from './item/ItemQuery.js';
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;"}
@@ -0,0 +1,16 @@
1
+ import { Item } from '../items';
2
+ import { ComKey, PriKey } from '../keys';
3
+ export declare class IFactory<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
4
+ private item;
5
+ constructor(props?: Record<string, any>);
6
+ addRef(i: Item<any, any | never, any | never, any | never, any | never, any | never>, name?: string): this;
7
+ static addRef<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(i: V, name?: string): IFactory<V, S, L1, L2, L3, L4, L5>;
8
+ addDefaultEvents(): this;
9
+ addEvent(name: string, at: Date | null, by?: ComKey<any, any | never, any | never, any | never, any | never, any | never> | PriKey<any>): this;
10
+ static addEvent<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(name: string, at: Date | null, by?: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>): IFactory<V, S, L1, L2, L3, L4, L5>;
11
+ addProp(name: string, value: string | number | boolean | Date): this;
12
+ static addProp<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(name: string, value: string | number | boolean | Date): IFactory<V, S, L1, L2, L3, L4, L5>;
13
+ addProps(props: Record<string, any>): this;
14
+ static addProps<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(props: Record<string, any>): IFactory<V, S, L1, L2, L3, L4, L5>;
15
+ toItem(): V;
16
+ }
@@ -0,0 +1,89 @@
1
+ import deepmerge from 'deepmerge';
2
+ import { primaryType } from '../key/KUtils.js';
3
+
4
+ function _define_property(obj, key, value) {
5
+ if (key in obj) {
6
+ Object.defineProperty(obj, key, {
7
+ value: value,
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true
11
+ });
12
+ } else {
13
+ obj[key] = value;
14
+ }
15
+ return obj;
16
+ }
17
+ class IFactory {
18
+ addRef(i, name) {
19
+ const ik = i.key;
20
+ const refName = name || primaryType(ik);
21
+ if (!this.item.refs) {
22
+ this.item.refs = {};
23
+ }
24
+ this.item.refs[refName] = ik;
25
+ return this;
26
+ }
27
+ static addRef(i, name) {
28
+ return new IFactory().addRef(i, name);
29
+ }
30
+ addDefaultEvents() {
31
+ if (!this.item.events) {
32
+ this.item.events = {};
33
+ }
34
+ const now = new Date();
35
+ if (!this.item.events.created) {
36
+ this.item.events.created = {
37
+ at: now
38
+ };
39
+ }
40
+ if (!this.item.events.updated) {
41
+ this.item.events.updated = {
42
+ at: now
43
+ };
44
+ }
45
+ if (!this.item.events.deleted) {
46
+ this.item.events.deleted = {
47
+ at: null
48
+ };
49
+ }
50
+ return this;
51
+ }
52
+ addEvent(name, at, by) {
53
+ if (!this.item.events) {
54
+ this.item.events = {};
55
+ }
56
+ this.item.events[name] = {
57
+ at,
58
+ by
59
+ };
60
+ return this;
61
+ }
62
+ static addEvent(name, at, by) {
63
+ return new IFactory().addEvent(name, at, by);
64
+ }
65
+ addProp(name, value) {
66
+ this.item[name] = value;
67
+ return this;
68
+ }
69
+ static addProp(name, value) {
70
+ return new IFactory().addProp(name, value);
71
+ }
72
+ addProps(props) {
73
+ this.item = deepmerge(this.item, props);
74
+ return this;
75
+ }
76
+ static addProps(props) {
77
+ return new IFactory().addProps(props);
78
+ }
79
+ toItem() {
80
+ return this.item;
81
+ }
82
+ constructor(props = {}){
83
+ _define_property(this, "item", {});
84
+ this.item = deepmerge(this.item, props);
85
+ }
86
+ }
87
+
88
+ export { IFactory };
89
+ //# sourceMappingURL=IFactory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IFactory.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,23 @@
1
+ import { CompoundType, Condition, ConditionOperator, EventQuery, ItemQuery, OrderDirection } from './ItemQuery';
2
+ export declare class IQFactory {
3
+ private query;
4
+ constructor(query?: ItemQuery);
5
+ orderBy(field: string, direction?: OrderDirection): this;
6
+ agg(name: string, query: ItemQuery): this;
7
+ event(name: string, query: EventQuery): this;
8
+ conditions(conditions: Condition[], compoundType?: CompoundType): this;
9
+ limit(limit: number): this;
10
+ offset(offset: number): this;
11
+ pk(kt: string, pk: string, name?: string): this;
12
+ condition(column: string, value: string[] | string | number[] | number | boolean | Date, operator?: ConditionOperator): this;
13
+ static all(): IQFactory;
14
+ static orderBy(field: string, direction?: OrderDirection): IQFactory;
15
+ static agg(name: string, query: ItemQuery): IQFactory;
16
+ static event(name: string, query: EventQuery): IQFactory;
17
+ static limit(limit: number): IQFactory;
18
+ static offset(offset: number): IQFactory;
19
+ static pk(kt: string, pk: string, name?: string): IQFactory;
20
+ static condition(column: string, value: string[] | string | number[] | number | boolean | Date, operator?: ConditionOperator): IQFactory;
21
+ static conditions(conditions: Condition[], compoundType?: CompoundType): IQFactory;
22
+ toQuery(): ItemQuery;
23
+ }
@@ -0,0 +1,150 @@
1
+ import { cPK } from '../key/KUtils.js';
2
+ import { isCondition } from './ItemQuery.js';
3
+
4
+ function _define_property(obj, key, value) {
5
+ if (key in obj) {
6
+ Object.defineProperty(obj, key, {
7
+ value: value,
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true
11
+ });
12
+ } else {
13
+ obj[key] = value;
14
+ }
15
+ return obj;
16
+ }
17
+ class IQFactory {
18
+ orderBy(field, direction = 'asc') {
19
+ if (!this.query.orderBy) {
20
+ this.query.orderBy = [];
21
+ }
22
+ this.query.orderBy.push({
23
+ field,
24
+ direction
25
+ });
26
+ return this;
27
+ }
28
+ agg(name, query) {
29
+ if (!this.query.aggs) {
30
+ this.query.aggs = {};
31
+ }
32
+ this.query.aggs[name] = query;
33
+ return this;
34
+ }
35
+ event(name, query) {
36
+ if (!this.query.events) {
37
+ this.query.events = {};
38
+ }
39
+ this.query.events[name] = query;
40
+ return this;
41
+ }
42
+ conditions(conditions, compoundType = 'AND') {
43
+ for (const condition of conditions){
44
+ if (!isCondition(condition)) {
45
+ throw new Error(`Invalid condition: ${JSON.stringify(condition)}`);
46
+ }
47
+ }
48
+ if (!this.query.compoundCondition) {
49
+ // If there is no top-level compound condition, create one
50
+ // with the given compound type. This will mostly likely be the most common case.
51
+ this.query.compoundCondition = {
52
+ compoundType,
53
+ conditions: conditions
54
+ };
55
+ } else {
56
+ // If there is already a top-level compound condition, create a new compound condition
57
+ // and add it to the conditions array of the top-level compound condition.
58
+ const compoundCondition = {
59
+ compoundType,
60
+ conditions
61
+ };
62
+ this.query.compoundCondition.conditions.push(compoundCondition);
63
+ }
64
+ return this;
65
+ }
66
+ limit(limit) {
67
+ this.query.limit = limit;
68
+ return this;
69
+ }
70
+ offset(offset) {
71
+ this.query.offset = offset;
72
+ return this;
73
+ }
74
+ // TODO: right now, we're only supporting PK refs for queries. Should add support for CKs
75
+ pk(kt, pk, name) {
76
+ if (!this.query.refs) {
77
+ this.query.refs = {};
78
+ }
79
+ const refName = name || kt;
80
+ this.query.refs[refName] = cPK(pk, kt);
81
+ return this;
82
+ }
83
+ condition(column, value, operator = '==') {
84
+ const condition = {
85
+ column,
86
+ value,
87
+ operator
88
+ };
89
+ if (isCondition(condition)) {
90
+ if (!this.query.compoundCondition) {
91
+ // If there is no top-level compound condition, create one
92
+ // with the default compound type of 'AND'.
93
+ this.query.compoundCondition = {
94
+ compoundType: 'AND',
95
+ conditions: []
96
+ };
97
+ }
98
+ this.query.compoundCondition.conditions.push(condition);
99
+ return this;
100
+ } else {
101
+ throw new Error(`Invalid condition: ${JSON.stringify(condition)}`);
102
+ }
103
+ }
104
+ static all() {
105
+ const iqFactory = new IQFactory();
106
+ return iqFactory;
107
+ }
108
+ static orderBy(field, direction = 'asc') {
109
+ const iqFactory = new IQFactory();
110
+ return iqFactory.orderBy(field, direction);
111
+ }
112
+ static agg(name, query) {
113
+ const iqFactory = new IQFactory();
114
+ return iqFactory.agg(name, query);
115
+ }
116
+ static event(name, query) {
117
+ const iqFactory = new IQFactory();
118
+ return iqFactory.event(name, query);
119
+ }
120
+ static limit(limit) {
121
+ const iqFactory = new IQFactory();
122
+ return iqFactory.limit(limit);
123
+ }
124
+ static offset(offset) {
125
+ const iqFactory = new IQFactory();
126
+ return iqFactory.offset(offset);
127
+ }
128
+ static pk(kt, pk, name) {
129
+ const iqFactory = new IQFactory();
130
+ return iqFactory.pk(kt, pk, name);
131
+ }
132
+ static condition(column, value, operator = '==') {
133
+ const iqFactory = new IQFactory();
134
+ return iqFactory.condition(column, value, operator);
135
+ }
136
+ static conditions(conditions, compoundType = 'AND') {
137
+ const iqFactory = new IQFactory();
138
+ return iqFactory.conditions(conditions, compoundType);
139
+ }
140
+ toQuery() {
141
+ return this.query;
142
+ }
143
+ constructor(query = {}){
144
+ _define_property(this, "query", {});
145
+ this.query = query;
146
+ }
147
+ }
148
+
149
+ export { IQFactory };
150
+ //# sourceMappingURL=IQFactory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IQFactory.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,35 @@
1
+ import { Item } from '../items';
2
+ import { ComKey, PriKey } from '../keys';
3
+ import { CompoundCondition, Condition, ItemQuery, QueryParams } from './ItemQuery';
4
+ /**
5
+ * When we query or search, we're sending a GET request. This converts everything in ItemQuery into a flat
6
+ * object that can be sent over a GET request.
7
+ *
8
+ * Note that there is some discussion about this. Evidently Elastic supports search with POST, but that also
9
+ * feels like a bit of a hack. It's not a RESTful way to do things. So we're sticking with GET for now.
10
+ *
11
+ * For reference, look at RFC 9110 "HTTP Semantics", June which clarified on top of and RFC 7231. It's possible
12
+ * but there are so many caveats and conditions in the standard, it's not worth it.
13
+ *
14
+ * Anticipating the next question - "isn't there a limit to the length of a URL?" The specification does not
15
+ * specify a limit, and there are limits in various browsers and servers - Apache is 4,000 chars, Chrome is 2M chars.
16
+ * Short answer is that if this query is being used to craft something that complex, it is probably a better idea
17
+ * to provide an action or a custom query endpoint on the server.
18
+ *
19
+ * @param query
20
+ * @returns QueryParams ready to be get over a GET request.
21
+ */
22
+ export declare const queryToParams: (query: ItemQuery) => QueryParams;
23
+ /**
24
+ * This method translates from a flat QueryParams object with stringify'd JSON back to a full ItemQuery.
25
+ *
26
+ * @param params Parameters sent over a GET request
27
+ * @returns A fully hydrated ItemQuery object.
28
+ */
29
+ export declare const paramsToQuery: (params: QueryParams) => ItemQuery;
30
+ export declare const isQueryMatch: <S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(item: Item<S, L1, L2, L3, L4, L5>, query: ItemQuery) => boolean;
31
+ export declare const abbrevQuery: (query: ItemQuery | null | undefined) => string;
32
+ export declare const abbrevRef: <S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(key: string, ref: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>) => string;
33
+ export declare const abbrevAgg: (key: string, agg: ItemQuery) => string;
34
+ export declare const abbrevCompoundCondition: (compoundCondition: CompoundCondition) => string;
35
+ export declare const abbrevCondition: (condition: Condition | CompoundCondition) => string;