@memberjunction/data-context 0.9.2

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 @@
1
+ export * from './types';
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAuB"}
@@ -0,0 +1,115 @@
1
+ import { BaseEntity, EntityInfo, QueryInfo } from "@memberjunction/core";
2
+ import { UserViewEntityExtended } from "@memberjunction/core-entities";
3
+ export declare class DataContextFieldInfo {
4
+ Name: string;
5
+ Type: string;
6
+ Description?: string;
7
+ }
8
+ export declare class DataContextItem {
9
+ /**
10
+ * The type of the item, either "view", "query", "full_entity", or "sql", or "single_record"
11
+ */
12
+ Type: 'view' | 'query' | 'full_entity' | 'sql' | 'single_record';
13
+ /**
14
+ * The primary key of the single record in the system, only used if type = 'single_record'
15
+ */
16
+ RecordID: string;
17
+ /**
18
+ * EntityID - the ID of the entity in the system, only used if type = 'full_entity', 'view', or 'single_record' --- for type of 'query' or 'sql' this property is not used as results can come from any number of entities in combination
19
+ */
20
+ EntityID?: number;
21
+ /**
22
+ * ViewID - the ID of the view in the system, only used if type = 'view'
23
+ */
24
+ ViewID?: number;
25
+ /**
26
+ * QueryID - the ID of the query in the system, only used if type = 'query'
27
+ */
28
+ QueryID?: number;
29
+ /**
30
+ * The name of the view, query, or entity in the system. Not used with type='single_record' or type='sql'
31
+ */
32
+ RecordName: string;
33
+ /**
34
+ * SQL - the SQL statement to execute, only used if type = 'sql'
35
+ */
36
+ SQL?: string;
37
+ /**
38
+ * The name of the entity in the system, only used if type = 'full_entity', 'view', or 'single_record' --- for type of 'query' or 'sql' this property is not used as results can come from any number of entities in combination
39
+ */
40
+ EntityName?: string;
41
+ Fields: DataContextFieldInfo[];
42
+ /**
43
+ * This field can be used at run time to stash the record ID in the database of the Data Context Item, if it was already saved. For items that haven't/won't be saved, this property can be ignored.
44
+ */
45
+ DataContextItemID?: number;
46
+ /**
47
+ * ViewEntity - the object instantiated that contains the metadata for the UserView being used - only populated if the type is 'view', also this is NOT to be sent to/from the API server, it is a placeholder that can be used
48
+ * within a given tier like in the MJAPI server or in the UI.
49
+ */
50
+ ViewEntity?: UserViewEntityExtended;
51
+ /**
52
+ * SingleRecord - the object instantiated that contains the data for the single record being used - only populated if the type is 'single_record' - also this is NOT to be sent to/from the API server, it is a placeholder that can be used in a given tier
53
+ */
54
+ SingleRecord?: BaseEntity;
55
+ /**
56
+ * Entity - the object that contains metadata for the entity being used, only populated if the type is 'full_entity' or 'view' - also this is NOT to be sent to/from the API server, it is a placeholder that can be used
57
+ * within a given tier like in the MJAPI server or in the UI.
58
+ */
59
+ Entity?: EntityInfo;
60
+ /** Additional Description has any other information that might be useful for someone (or an LLM) intepreting the contents of this data item */
61
+ AdditionalDescription?: string;
62
+ /**
63
+ * Generated description of the item which is dependent on the type of the item
64
+ */
65
+ get Description(): string;
66
+ /**
67
+ * Create a new DataContextItem from a UserViewEntity class instance
68
+ * @param viewEntity
69
+ */
70
+ static FromViewEntity(viewEntity: UserViewEntityExtended): DataContextItem;
71
+ /**
72
+ * Create a new DataContextItem from a BaseEntity class instance
73
+ * @param singleRecord
74
+ * @returns
75
+ */
76
+ static FromSingleRecord(singleRecord: BaseEntity): DataContextItem;
77
+ /**
78
+ * Create a new DataContextItem from a QueryInfo class instance
79
+ * @param query
80
+ * @returns
81
+ */
82
+ static FromQuery(query: QueryInfo): DataContextItem;
83
+ /**
84
+ * Create a new DataContextItem from a EntityInfo class instance
85
+ * @param entity
86
+ * @returns
87
+ */
88
+ static FromFullEntity(entity: EntityInfo): DataContextItem;
89
+ Data?: any[];
90
+ /**
91
+ * Validates that the Data property is set. Valid states include a zero length array, or an array with one or more elements. If the Data property is not set, this method will return false
92
+ * @returns
93
+ */
94
+ ValidateDataExists(): boolean;
95
+ }
96
+ export declare class DataContext {
97
+ Items: DataContextItem[];
98
+ /**
99
+ * Simple validation method that determines if all of the items in the data context have data set. This doesn't mean the items have data in them as zero-length data is consider valid, it is checking to see if the Data property is set on each item or not
100
+ * @returns
101
+ */
102
+ ValidateDataExists(): boolean;
103
+ /**
104
+ * Return a simple object that will have a property for each item in our Items array. We will name each item sequentially as data_item_1, data_item_2, etc, using the itemPrefix parameter
105
+ * @param itemPrefix defaults to 'data_item_' and can be set to anything desired
106
+ * @returns
107
+ */
108
+ ConvertToSimpleObject(itemPrefix?: string): any;
109
+ /**
110
+ * Return a string that contains a type definition for a simple object for this data context. The object will have a property for each item in our Items array. We will name each item sequentially as data_item_1, data_item_2, etc, using the itemPrefix parameter
111
+ * @param itemPrefix defaults to 'data_item_' and can be set to anything desired
112
+ * @returns
113
+ */
114
+ CreateSimpleObjectTypeDefinition(itemPrefix?: string): string;
115
+ }
package/dist/types.js ADDED
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DataContext = exports.DataContextItem = exports.DataContextFieldInfo = void 0;
4
+ class DataContextFieldInfo {
5
+ }
6
+ exports.DataContextFieldInfo = DataContextFieldInfo;
7
+ class DataContextItem {
8
+ constructor() {
9
+ /*
10
+ * The fields in the view, query, or entity
11
+ */
12
+ this.Fields = [];
13
+ }
14
+ /**
15
+ * Generated description of the item which is dependent on the type of the item
16
+ */
17
+ get Description() {
18
+ let ret = '';
19
+ switch (this.Type) {
20
+ case 'view':
21
+ ret = `View: ${this.RecordName}, From Entity: ${this.EntityName}`;
22
+ break;
23
+ case 'query':
24
+ ret = `Query: ${this.RecordName}`;
25
+ break;
26
+ case 'full_entity':
27
+ ret = `Full Entity - All Records: ${this.EntityName}`;
28
+ break;
29
+ case 'sql':
30
+ ret = `SQL Statement: ${this.RecordName}`;
31
+ break;
32
+ default:
33
+ ret = `Unknown Type: ${this.Type}`;
34
+ break;
35
+ }
36
+ if (this.AdditionalDescription && this.AdditionalDescription.length > 0)
37
+ ret += ` (More Info: ${this.AdditionalDescription})`;
38
+ return ret;
39
+ }
40
+ /**
41
+ * Create a new DataContextItem from a UserViewEntity class instance
42
+ * @param viewEntity
43
+ */
44
+ static FromViewEntity(viewEntity) {
45
+ const instance = new DataContextItem();
46
+ // update our data from the viewEntity definition
47
+ instance.Type = 'view';
48
+ instance.ViewEntity = viewEntity;
49
+ instance.Entity = viewEntity.ViewEntityInfo;
50
+ instance.EntityName = viewEntity.ViewEntityInfo.Name;
51
+ instance.ViewID = viewEntity.ID;
52
+ instance.RecordName = viewEntity.Name;
53
+ instance.Fields = viewEntity.ViewEntityInfo.Fields.map(f => {
54
+ return {
55
+ Name: f.Name,
56
+ Type: f.Type,
57
+ Description: f.Description
58
+ };
59
+ });
60
+ return instance;
61
+ }
62
+ /**
63
+ * Create a new DataContextItem from a BaseEntity class instance
64
+ * @param singleRecord
65
+ * @returns
66
+ */
67
+ static FromSingleRecord(singleRecord) {
68
+ const instance = new DataContextItem();
69
+ instance.Type = 'single_record';
70
+ instance.RecordID = singleRecord.PrimaryKey.Value;
71
+ instance.EntityID = singleRecord.EntityInfo.ID;
72
+ instance.EntityName = singleRecord.EntityInfo.Name;
73
+ instance.SingleRecord = singleRecord;
74
+ return instance;
75
+ }
76
+ /**
77
+ * Create a new DataContextItem from a QueryInfo class instance
78
+ * @param query
79
+ * @returns
80
+ */
81
+ static FromQuery(query) {
82
+ const instance = new DataContextItem();
83
+ instance.Type = 'query';
84
+ instance.QueryID = query.ID;
85
+ instance.RecordName = query.Name;
86
+ instance.Fields = query.Fields.map(f => {
87
+ return {
88
+ Name: f.Name,
89
+ Type: f.SQLBaseType,
90
+ Description: f.Description
91
+ };
92
+ });
93
+ return instance;
94
+ }
95
+ /**
96
+ * Create a new DataContextItem from a EntityInfo class instance
97
+ * @param entity
98
+ * @returns
99
+ */
100
+ static FromFullEntity(entity) {
101
+ const instance = new DataContextItem();
102
+ instance.Type = 'full_entity';
103
+ instance.EntityID = entity.ID;
104
+ instance.EntityName = entity.Name;
105
+ instance.Entity = entity;
106
+ instance.RecordName = entity.Name;
107
+ instance.Fields = entity.Fields.map(f => {
108
+ return {
109
+ Name: f.Name,
110
+ Type: f.Type,
111
+ Description: f.Description
112
+ };
113
+ });
114
+ return instance;
115
+ }
116
+ /**
117
+ * Validates that the Data property is set. Valid states include a zero length array, or an array with one or more elements. If the Data property is not set, this method will return false
118
+ * @returns
119
+ */
120
+ ValidateDataExists() {
121
+ return this.Data ? this.Data.length >= 0 : false; // can have 0 to many rows, just need to make sure we have a Data object to work with
122
+ }
123
+ }
124
+ exports.DataContextItem = DataContextItem;
125
+ class DataContext {
126
+ constructor() {
127
+ this.Items = [];
128
+ }
129
+ /**
130
+ * Simple validation method that determines if all of the items in the data context have data set. This doesn't mean the items have data in them as zero-length data is consider valid, it is checking to see if the Data property is set on each item or not
131
+ * @returns
132
+ */
133
+ ValidateDataExists() {
134
+ if (this.Items)
135
+ return !this.Items.some(i => !i.ValidateDataExists()); // if any data item is invalid, return false
136
+ else
137
+ return false;
138
+ }
139
+ /**
140
+ * Return a simple object that will have a property for each item in our Items array. We will name each item sequentially as data_item_1, data_item_2, etc, using the itemPrefix parameter
141
+ * @param itemPrefix defaults to 'data_item_' and can be set to anything desired
142
+ * @returns
143
+ */
144
+ ConvertToSimpleObject(itemPrefix = 'data_item_') {
145
+ //
146
+ const ret = {};
147
+ for (let i = 0; i < this.Items.length; i++) {
148
+ ret[`${itemPrefix}${i}`] = this.Items[i].Data;
149
+ }
150
+ return ret;
151
+ }
152
+ /**
153
+ * Return a string that contains a type definition for a simple object for this data context. The object will have a property for each item in our Items array. We will name each item sequentially as data_item_1, data_item_2, etc, using the itemPrefix parameter
154
+ * @param itemPrefix defaults to 'data_item_' and can be set to anything desired
155
+ * @returns
156
+ */
157
+ CreateSimpleObjectTypeDefinition(itemPrefix = 'data_item_') {
158
+ let sOutput = "";
159
+ for (let i = 0; i < this.Items.length; i++) {
160
+ const item = this.Items[i];
161
+ sOutput += `${itemPrefix}${i}: []; // ${item.Description}\n`;
162
+ }
163
+ return `{${sOutput}}`;
164
+ }
165
+ }
166
+ exports.DataContext = DataContext;
167
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAGA,MAAa,oBAAoB;CAIhC;AAJD,oDAIC;AAED,MAAa,eAAe;IAA5B;QAyCI;;UAEE;QACF,WAAM,GAA2B,EAAE,CAAC;IAiJxC,CAAC;IAtHG;;OAEG;IACH,IAAI,WAAW;QACX,IAAI,GAAG,GAAW,EAAE,CAAC;QACrB,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;YAChB,KAAK,MAAM;gBACP,GAAG,GAAG,SAAS,IAAI,CAAC,UAAU,kBAAkB,IAAI,CAAC,UAAU,EAAE,CAAC;gBAClE,MAAM;YACV,KAAK,OAAO;gBACR,GAAG,GAAG,UAAU,IAAI,CAAC,UAAU,EAAE,CAAC;gBAClC,MAAM;YACV,KAAK,aAAa;gBACd,GAAG,GAAG,8BAA8B,IAAI,CAAC,UAAU,EAAE,CAAC;gB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G,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACzC,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAC3B,OAAO,IAAI,GAAG,UAAU,GAAG,CAAC,YAAY,IAAI,CAAC,WAAW,IAAI,CAAC;QACjE,CAAC;QACD,OAAO,IAAI,OAAO,GAAG,CAAC;IAC1B,CAAC;CACJ;AAzCD,kCAyCC"}
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@memberjunction/data-context",
3
+ "version": "0.9.2",
4
+ "description": "This library provides a set of objects that handle run-time loading of data contexts as well as types to be able to use across application tiers for interacting with data contexts.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "/dist"
9
+ ],
10
+ "scripts": {
11
+ "start": "ts-node-dev src/index.ts",
12
+ "build": "tsc",
13
+ "test": "echo \"Error: no test specified\" && exit 1"
14
+ },
15
+ "author": "MemberJunction.com",
16
+ "license": "ISC",
17
+ "devDependencies": {
18
+ "ts-node-dev": "^2.0.0",
19
+ "typescript": "^5.3.3"
20
+ },
21
+ "dependencies": {
22
+ "@memberjunction/core-entities": "^0.9.124"
23
+ }
24
+ }
package/readme.md ADDED
@@ -0,0 +1,3 @@
1
+ # @memberjunction/data-context
2
+
3
+ The `@memberjunction/data-context` library provides a set of objects that handle run-time loading of data contexts as well as types to be able to use across application tiers for interacting with data contexts.