@liminalfunctions/framework-vitamins 1.0.0

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,255 @@
1
+ import { v4 as uuid } from 'uuid'
2
+
3
+ import { generated_collection_interface, result } from '../../dist/type_generated_collection'
4
+
5
+ function db_query_mock(db: Map<string, any>, query: object){
6
+ let retval: any[] = [];
7
+ for(let db_entry of db.values()) {
8
+ let matches = true;
9
+ for(let [key, value] of Object.entries(query)){
10
+ if(Array.isArray(db_entry[key])) {
11
+ if(!db_entry[key].includes(value)) { matches = false; break; }
12
+ }
13
+ else if(db_entry[key] !== value) { matches = false; break; }
14
+ }
15
+
16
+ if(matches){
17
+ retval.push(db_entry);
18
+ }
19
+ }
20
+ return retval;
21
+ }
22
+
23
+ async function get_query_results<T>(query: any, database: Map<string, T>, meta_counter: Map<string, any>): Promise<T[]> {
24
+ let results = db_query_mock(database, query);
25
+ for(let result of results){
26
+ let count = meta_counter.get(result._id) ?? 0;
27
+ meta_counter.set(result._id, count + 1);
28
+ }
29
+ return results;
30
+ }
31
+
32
+ async function get<T>(document_id: string, database: Map<string, T>, meta_counter: Map<string, any>): Promise<T | undefined> {
33
+ let count = meta_counter.get(document_id) ?? 0;
34
+ meta_counter.set(document_id, count + 1);
35
+ return database.get(document_id);
36
+ }
37
+
38
+ type institution_result = {
39
+ _id: string,
40
+ name: string
41
+ }
42
+
43
+ type client_result = {
44
+ _id: string,
45
+ institution_id: string
46
+ name: string
47
+ }
48
+
49
+ type project_result = {
50
+ _id: string,
51
+ institution_id: string
52
+ client_id: string
53
+ name: string
54
+ }
55
+
56
+ type mutualism_result = {
57
+ _id: string,
58
+ institution_id: string
59
+ client_ids: string[]
60
+ name: string
61
+ }
62
+
63
+ export function gen_institution(name: string): institution_result{
64
+ return {
65
+ _id: uuid(),
66
+ name: name
67
+ }
68
+ }
69
+
70
+ export function gen_client(institution: result, name: string): client_result {
71
+ return {
72
+ _id: uuid(),
73
+ institution_id: institution._id,
74
+ name: name
75
+ }
76
+ }
77
+
78
+ export function gen_project(institution: result, client: result, name: string): project_result {
79
+ return {
80
+ _id: uuid(),
81
+ institution_id: institution._id,
82
+ client_id: client._id,
83
+ name: name
84
+ }
85
+ }
86
+
87
+ export function gen_mutualism(institution: result, clients: result[], name: string): mutualism_result {
88
+ return {
89
+ _id: uuid(),
90
+ institution_id: institution._id,
91
+ client_ids: clients.map(ele => ele._id),
92
+ name: name
93
+ }
94
+ }
95
+
96
+ export class Institution {
97
+ path: string[]
98
+ collection_id: string;
99
+ database: Map<string, any>
100
+ meta_counter: Map<string, any>
101
+ errors: boolean
102
+
103
+ project: Project
104
+ client: Client
105
+
106
+
107
+ constructor(path: string[], collection_id: string, project_database: Map<string, any>, client: Client, project: Project) {
108
+ this.path = path;
109
+ this.collection_id = collection_id;
110
+ this.database = project_database;
111
+ this.meta_counter = new Map();
112
+ this.project = project;
113
+ this.client = client;
114
+ this.errors = false;
115
+ }
116
+
117
+ async query(query: any): Promise<institution_result[]> {
118
+ if(this.errors){ throw new Error('arbitrary error')};
119
+ return await get_query_results(query, this.database, this.meta_counter);
120
+ }
121
+
122
+ document(document_id: string) {
123
+ let self = this;
124
+ return {
125
+ path: [...self.path, document_id],
126
+ collection_id: self.collection_id,
127
+ document_id: document_id,
128
+ async get(): Promise<institution_result>{
129
+ if(self.errors){ throw new Error('arbitrary error')};
130
+ return await get(document_id, self.database, self.meta_counter)
131
+ },
132
+ collection(collection_id: 'client' | 'project') {
133
+ switch(collection_id){
134
+ case 'client':
135
+ return self.client;
136
+ case 'project':
137
+ return self.project;
138
+ }
139
+ }
140
+ }
141
+ }
142
+ }
143
+
144
+ export class Client {
145
+ path: string[]
146
+ collection_id: string;
147
+ database: Map<string, any>
148
+ meta_counter: Map<string, any>
149
+ errors: boolean
150
+
151
+ mutualism: Mutualism
152
+
153
+ constructor(path: string[], collection_id: string, client_database: Map<string, any>, mutualism: Mutualism) {
154
+ this.path = path;
155
+ this.collection_id = collection_id;
156
+ this.database = client_database;
157
+ this.meta_counter = new Map();
158
+ this.mutualism = mutualism;
159
+ this.errors = false;
160
+ }
161
+
162
+ async query(query: any): Promise<client_result[]> {
163
+ if(this.errors){ throw new Error('arbitrary error')};
164
+ return await get_query_results(query, this.database, this.meta_counter);
165
+ }
166
+
167
+ document(document_id: string) {
168
+ let self = this;
169
+ return {
170
+ path: [...self.path, document_id],
171
+ collection_id: self.collection_id,
172
+ document_id: document_id,
173
+ async get(): Promise<client_result> {
174
+ if(self.errors){ throw new Error('arbitrary error')};
175
+ return await get(document_id, self.database, self.meta_counter)
176
+ },
177
+ collection(collection_id: 'mutualism') {
178
+ switch(collection_id){
179
+ case 'mutualism':
180
+ return self.mutualism;
181
+ }
182
+ }
183
+ }
184
+ }
185
+ }
186
+
187
+ export class Project {
188
+ path: string[]
189
+ collection_id: string;
190
+ database: Map<string, any>
191
+ meta_counter: Map<string, any>
192
+ errors: boolean
193
+
194
+ constructor(path: string[], collection_id: string, project_database: Map<string, any>) {
195
+ this.path = path;
196
+ this.collection_id = collection_id;
197
+ this.database = project_database;
198
+ this.meta_counter = new Map();
199
+ this.errors = false;
200
+ }
201
+
202
+ async query(query: any): Promise<project_result[]> {
203
+ if(this.errors){ throw new Error('arbitrary error')};
204
+ return await get_query_results(query, this.database, this.meta_counter);
205
+ }
206
+
207
+ document(document_id: string) {
208
+ let self = this;
209
+ return {
210
+ path: [...self.path, document_id],
211
+ collection_id: self.collection_id,
212
+ document_id: document_id,
213
+ async get(): Promise<project_result> {
214
+ if(self.errors){ throw new Error('arbitrary error')};
215
+ return await get(document_id, self.database, self.meta_counter)
216
+ },
217
+ collection() {}
218
+ }
219
+ }
220
+ }
221
+
222
+ export class Mutualism {
223
+ path: string[]
224
+ collection_id: string;
225
+ database: Map<string, any>
226
+ meta_counter: Map<string, any>
227
+ errors: boolean
228
+
229
+ constructor(path: string[], collection_id: string, mutualism_database: Map<string, any>) {
230
+ this.path = path;
231
+ this.collection_id = collection_id;
232
+ this.database = mutualism_database;
233
+ this.meta_counter = new Map();
234
+ this.errors = false;
235
+ }
236
+
237
+ async query(query: any): Promise<mutualism_result[]> {
238
+ if(this.errors){ throw new Error('arbitrary error')};
239
+ return await get_query_results(query, this.database, this.meta_counter);
240
+ }
241
+
242
+ document(document_id: string) {
243
+ let self = this;
244
+ return {
245
+ path: [...self.path, document_id],
246
+ collection_id: self.collection_id,
247
+ document_id: document_id,
248
+ async get(): Promise<mutualism_result> {
249
+ if(self.errors){ throw new Error('arbitrary error')};
250
+ return await get(document_id, self.database, self.meta_counter)
251
+ },
252
+ collection() {}
253
+ }
254
+ }
255
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "node16",
4
+ "noImplicitAny": true,
5
+ "removeComments": true,
6
+ "preserveConstEnums": true,
7
+ "outDir": "dist",
8
+ "sourceMap": true,
9
+ "declaration": true,
10
+ //"declarationMap": true//check this
11
+ },
12
+ "include": ["src/**/*", "test/0_mongoose_from_zod.test.js"],
13
+ "exclude": []
14
+ }