@fraym/crud 0.5.0 → 0.7.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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # crud-nodejs
2
2
 
3
- Client implementation in javascript for the CRUD service [streams](https://github.com/fraym/crud).
3
+ Client implementation in javascript for the [CRUD service](https://github.com/fraym/crud).
4
4
 
5
5
  ## Installation
6
6
 
@@ -22,9 +22,11 @@ Use the `crud` cli command to automatically apply your crud schemas to the crud
22
22
  Your type schemas have to match the glob you specify in the `CRUD_SCHEMA_GLOB` env variable (default: `./src/**/*.graphql`).
23
23
  You can specify the address (and port) of the crud service instance you use in the `CRUD_SERVER_ADDRESS` env variable (default: `127.0.0.1:9000`).
24
24
 
25
+ You need to add a file that contains all built-in directives to your type schemas. The latest version of this file can be found [here](default.graphql).
26
+
25
27
  ### Config
26
28
 
27
- use a `.env` file or env variables to configure cte clients and the command:
29
+ Use a `.env` file or env variables to configure cte clients and the command:
28
30
 
29
31
  ```env
30
32
  CRUD_SERVER_ADDRESS=127.0.0.1:9000
@@ -110,13 +112,19 @@ await client.delete("tenantId", "YourCrudType", "id");
110
112
  ### Get a single data element
111
113
 
112
114
  The name of `YourCrudType` has to equal your type name in your schema (also in casing).
113
- The `id` has to match the id of the data that you want to delete.
115
+ The `id` has to match the id of the data that you want to get.
114
116
 
115
117
  ```typescript
116
118
  const data = await client.getData("tenantId", "YourCrudType", "id");
117
119
  ```
118
120
 
119
- ### Get (paginated) data
121
+ You can specify a fourth parameter if you want to return a empty dataset instead of getting an error when querying a non existing element:
122
+
123
+ ```typescript
124
+ const data = await client.getData("tenantId", "YourCrudType", "id", true);
125
+ ```
126
+
127
+ ### Get (paginated / filtered) data
120
128
 
121
129
  The name of `YourCrudType` has to equal your type name in your schema (also in casing).
122
130
 
@@ -134,6 +142,59 @@ const page = 1; // number of the page you want to select, first page starts at:
134
142
  const data = await client.getDataList("tenantId", "YourCrudType", limit, page);
135
143
  ```
136
144
 
145
+ With filter:
146
+
147
+ ```typescript
148
+ const data = await client.getDataList("tenantId", "YourCrudType", undefined, undefined, {
149
+ fields: {
150
+ fieldName: {
151
+ operation: "equals",
152
+ type: "Int",
153
+ value: 123,
154
+ },
155
+ },
156
+ });
157
+ ```
158
+
159
+ All `Filter`s are evaluated by:
160
+
161
+ - checking that all field filters match
162
+ - checking that all `and` filters match
163
+ - checking that one of the `or` filters match
164
+
165
+ Avaliable types:
166
+
167
+ - `String`
168
+ - `ID`
169
+ - `DateTime`
170
+ - `Int`
171
+ - `Float`
172
+ - `Boolean`
173
+
174
+ Avaliable operators for all types:
175
+
176
+ - `equals`
177
+ - `notEquals`
178
+
179
+ Avaliable options for the filter type `DateTime`:
180
+
181
+ - `inArray`
182
+ - `notInArray`
183
+ - `after`
184
+ - `before`
185
+
186
+ Avaliable options for the filter type `String` and `ID`:
187
+
188
+ - `inArray`
189
+ - `notInArray`
190
+
191
+ Avaliable options for the filter type `Int` and `Float`:
192
+
193
+ - `lessThan`
194
+ - `greaterThan`
195
+ - `lessThanOrEqual`
196
+ - `greaterThanOrEqual`
197
+
137
198
  ### Gracefully close the clients
138
199
 
139
200
  You won't lose any data if you don't. Use it for your peace of mind.
package/dist/cmd/crud.js CHANGED
@@ -118,7 +118,8 @@ const getTypeData = (t) => {
118
118
  name === "Float" ||
119
119
  name === "ID" ||
120
120
  name === "Boolean" ||
121
- name === "Int"
121
+ name === "Int" ||
122
+ name === "DateTime"
122
123
  ? {
123
124
  str: name,
124
125
  }
@@ -1,13 +1,13 @@
1
1
  import { ClientConfig } from "../config/config";
2
2
  import { CreatedCrudData } from "./create";
3
3
  import { GetCrudData } from "./getData";
4
- import { GetCrudDataList } from "./getDataList";
4
+ import { Filter, GetCrudDataList } from "./getDataList";
5
5
  export interface DeliveryClient {
6
6
  create: (tenantId: string, type: string, data: Record<string, any>) => Promise<CreatedCrudData>;
7
7
  update: (tenantId: string, type: string, id: string, data: Record<string, any>) => Promise<void>;
8
8
  delete: (tenantId: string, type: string, id: string) => Promise<void>;
9
9
  getData: (tenantId: string, type: string, id: string, returnEmptyDataIfNotFound?: boolean) => Promise<GetCrudData | null>;
10
- getDataList: (tenantId: string, type: string, limit?: number, page?: number) => Promise<GetCrudDataList | null>;
10
+ getDataList: (tenantId: string, type: string, limit?: number, page?: number, filter?: Filter) => Promise<GetCrudDataList | null>;
11
11
  close: () => Promise<void>;
12
12
  }
13
13
  export declare const newDeliveryClient: (config?: ClientConfig) => Promise<DeliveryClient>;
@@ -28,8 +28,8 @@ const newDeliveryClient = async (config) => {
28
28
  const getData = async (tenantId, type, id, returnEmptyDataIfNotFound = false) => {
29
29
  return await (0, getData_1.getCrudData)(tenantId, type, id, returnEmptyDataIfNotFound, serviceClient);
30
30
  };
31
- const getDataList = async (tenantId, type, limit = 0, page = 1) => {
32
- return await (0, getDataList_1.getCrudDataList)(tenantId, type, limit, page, serviceClient);
31
+ const getDataList = async (tenantId, type, limit = 0, page = 1, filter = { fields: {}, and: [], or: [] }) => {
32
+ return await (0, getDataList_1.getCrudDataList)(tenantId, type, limit, page, filter, serviceClient);
33
33
  };
34
34
  const close = async () => {
35
35
  serviceClient.close();
@@ -10,6 +10,7 @@ const getCrudData = async (tenantId, type, id, returnEmptyDataIfNotFound, servic
10
10
  limit: 0,
11
11
  page: 0,
12
12
  returnEmptyDataIfNotFound,
13
+ filter: { fields: {}, and: [], or: [] },
13
14
  }, (error, response) => {
14
15
  if (error) {
15
16
  reject(error.message);
@@ -4,4 +4,14 @@ export interface GetCrudDataList {
4
4
  page: number;
5
5
  data: Record<string, any>[];
6
6
  }
7
- export declare const getCrudDataList: (tenantId: string, type: string, limit: number, page: number, serviceClient: DeliveryServiceClient) => Promise<GetCrudDataList | null>;
7
+ export interface Filter {
8
+ fields: Record<string, FieldFilter>;
9
+ and: Filter[];
10
+ or: Filter[];
11
+ }
12
+ export interface FieldFilter {
13
+ type: string;
14
+ operation: string;
15
+ value: any;
16
+ }
17
+ export declare const getCrudDataList: (tenantId: string, type: string, limit: number, page: number, filter: Filter, serviceClient: DeliveryServiceClient) => Promise<GetCrudDataList | null>;
@@ -1,7 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getCrudDataList = void 0;
4
- const getCrudDataList = async (tenantId, type, limit, page, serviceClient) => {
4
+ const getProtobufDataFilter = (filter) => {
5
+ const fields = {};
6
+ for (const fieldName in filter.fields) {
7
+ const field = filter.fields[fieldName];
8
+ let value = "";
9
+ if (field.type === "String" && typeof field.value == "string") {
10
+ value = field.value;
11
+ }
12
+ else {
13
+ value = JSON.stringify(field.value);
14
+ }
15
+ fields[fieldName] = {
16
+ operation: field.operation,
17
+ type: field.type,
18
+ value,
19
+ };
20
+ }
21
+ return {
22
+ fields: fields,
23
+ and: filter.and.map(and => getProtobufDataFilter(and)),
24
+ or: filter.or.map(or => getProtobufDataFilter(or)),
25
+ };
26
+ };
27
+ const getCrudDataList = async (tenantId, type, limit, page, filter, serviceClient) => {
5
28
  return new Promise((resolve, reject) => {
6
29
  serviceClient.getEntries({
7
30
  tenantId,
@@ -10,6 +33,7 @@ const getCrudDataList = async (tenantId, type, limit, page, serviceClient) => {
10
33
  limit,
11
34
  page,
12
35
  returnEmptyDataIfNotFound: false,
36
+ filter: getProtobufDataFilter(filter),
13
37
  }, (error, response) => {
14
38
  if (error) {
15
39
  reject(error.message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fraym/crud",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "license": "UNLICENSED",
5
5
  "homepage": "https://github.com/fraym/crud-nodejs",
6
6
  "repository": {
@@ -27,12 +27,11 @@
27
27
  "crud": "dist/cmd/crud.js"
28
28
  },
29
29
  "dependencies": {
30
- "@fraym/crud-proto": "^1.0.0-alpha.6",
30
+ "@fraym/crud-proto": "^1.0.0-alpha.7",
31
31
  "@graphql-tools/graphql-file-loader": "^7.5.11",
32
32
  "@graphql-tools/load": "^7.8.6",
33
33
  "@grpc/grpc-js": "^1.7.2",
34
34
  "dotenv": "^16.0.3",
35
- "fs": "^0.0.1-security",
36
35
  "graphql": "^16.6.0",
37
36
  "yargs": "^17.6.2"
38
37
  },