|
1 | | -import { isSubject, isWarrantObject, PolicyContext, Subject, WarrantObject, WarrantObjectLiteral } from "./Warrant"; |
| 1 | +import Warrant from "./Warrant"; |
2 | 2 |
|
3 | | -export interface ForClause { |
4 | | - object?: WarrantObject | WarrantObjectLiteral; |
5 | | - relation?: string; |
6 | | - subject?: Subject | WarrantObject; |
7 | | - context?: PolicyContext; |
| 3 | +export interface QueryOptions { |
| 4 | + lastId?: string; |
8 | 5 | } |
9 | 6 |
|
10 | | -export interface WhereClause { |
11 | | - object?: WarrantObject | WarrantObjectLiteral; |
12 | | - relation?: string; |
13 | | - subject?: Subject | WarrantObject; |
14 | | - context?: PolicyContext; |
| 7 | +export interface QueryResponse { |
| 8 | + results: QueryResult[]; |
| 9 | + lastId?: string; |
15 | 10 | } |
16 | 11 |
|
17 | | -export default class Query { |
18 | | - selectClause: string; |
19 | | - forClause: ForClause; |
20 | | - whereClause: WhereClause; |
21 | | - |
22 | | - constructor(selectClause: string, forClause: ForClause = {}, whereClause: WhereClause = {}) { |
23 | | - this.selectClause = selectClause; |
24 | | - this.forClause = forClause; |
25 | | - this.whereClause = whereClause; |
26 | | - } |
27 | | - |
28 | | - public static selectWarrants(): Query { |
29 | | - return new Query("warrants"); |
30 | | - } |
31 | | - |
32 | | - public static selectExplicitWarrants(): Query { |
33 | | - return new Query("explicit warrants"); |
34 | | - } |
35 | | - |
36 | | - public for(forClause: ForClause): Query { |
37 | | - this.forClause = forClause; |
38 | | - |
39 | | - return this; |
40 | | - } |
41 | | - |
42 | | - public where(whereClause: WhereClause): Query { |
43 | | - this.whereClause = whereClause; |
44 | | - |
45 | | - return this; |
46 | | - } |
47 | | - |
48 | | - public toObject(): Object { |
49 | | - let queryParams: { [key: string]: string } = {}; |
50 | | - |
51 | | - queryParams["select"] = this.selectClause; |
52 | | - |
53 | | - if (Object.keys(this.forClause).length > 0) { |
54 | | - let forParamsString: string = ""; |
55 | | - for (const [key, value] of Object.entries(this.forClause)) { |
56 | | - switch (key) { |
57 | | - case 'object': |
58 | | - if (isWarrantObject(value)) { |
59 | | - forParamsString += `${key}=${value.getObjectType()}:${value.getObjectId()},`; |
60 | | - } else { |
61 | | - forParamsString += `${key}=${value.objectType}:${value.objectId},`; |
62 | | - } |
63 | | - break; |
64 | | - case 'relation': |
65 | | - forParamsString += `${key}=${value},`; |
66 | | - break; |
67 | | - case 'subject': |
68 | | - if (isSubject(value)) { |
69 | | - if (value.relation) { |
70 | | - forParamsString += `${key}=${value.objectType}:${value.objectId}#${value.relation},`; |
71 | | - } else { |
72 | | - forParamsString += `${key}=${value.objectType}:${value.objectId},`; |
73 | | - } |
74 | | - } else { |
75 | | - forParamsString += `${key}=${value.getObjectType()}:${value.getObjectId()},`; |
76 | | - } |
77 | | - break; |
78 | | - case 'context': |
79 | | - let contextString = ''; |
80 | | - for (const [contextKey, contextValue] of Object.entries(value)) { |
81 | | - contextString += `${contextKey}=${contextValue} `; |
82 | | - } |
83 | | - contextString = contextString.slice(0, -1); |
84 | | - forParamsString += `${key}=${contextString},`; |
85 | | - break; |
86 | | - } |
87 | | - } |
88 | | - forParamsString = forParamsString.slice(0, -1); |
89 | | - |
90 | | - queryParams["for"] = forParamsString; |
91 | | - } |
92 | | - |
93 | | - if (Object.keys(this.whereClause).length > 0) { |
94 | | - let whereParamsString: string = ""; |
95 | | - for (const [key, value] of Object.entries(this.whereClause)) { |
96 | | - switch (key) { |
97 | | - case 'object': |
98 | | - if (isWarrantObject(value)) { |
99 | | - whereParamsString += `${key}=${value.getObjectType()}:${value.getObjectId()},`; |
100 | | - } else { |
101 | | - whereParamsString += `${key}=${value.objectType}:${value.objectId},`; |
102 | | - } |
103 | | - break; |
104 | | - case 'relation': |
105 | | - whereParamsString += `${key}=${value},`; |
106 | | - break; |
107 | | - case 'subject': |
108 | | - if (isSubject(value)) { |
109 | | - if (value.relation) { |
110 | | - whereParamsString += `${key}=${value.objectType}:${value.objectId}#${value.relation},`; |
111 | | - } else { |
112 | | - whereParamsString += `${key}=${value.objectType}:${value.objectId},`; |
113 | | - } |
114 | | - } else { |
115 | | - whereParamsString += `${key}=${value.getObjectType()}:${value.getObjectId()},`; |
116 | | - } |
117 | | - break; |
118 | | - case 'context': |
119 | | - let contextString = ''; |
120 | | - for (const [contextKey, contextValue] of Object.entries(value)) { |
121 | | - contextString += `${contextKey}=${contextValue} `; |
122 | | - } |
123 | | - contextString = contextString.slice(0, -1); |
124 | | - whereParamsString += `${key}=${contextString},`; |
125 | | - break; |
126 | | - } |
127 | | - } |
128 | | - whereParamsString = whereParamsString.slice(0, -1); |
129 | | - |
130 | | - queryParams["where"] = whereParamsString; |
131 | | - } |
132 | | - |
133 | | - return queryParams; |
134 | | - } |
| 12 | +export interface QueryResult { |
| 13 | + objectType: string; |
| 14 | + objectId: string; |
| 15 | + warrant: Warrant; |
| 16 | + isImplicit: boolean; |
| 17 | + meta: { [key: string]: any; }; |
135 | 18 | } |
0 commit comments