@ruiapp/rapid-core 0.0.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.
- package/dist/bootstrapApplicationConfig.d.ts +3 -0
- package/dist/core/eventManager.d.ts +7 -0
- package/dist/core/http-types.d.ts +3 -0
- package/dist/core/httpHandler.d.ts +18 -0
- package/dist/core/plugin.d.ts +6 -0
- package/dist/core/pluginManager.d.ts +27 -0
- package/dist/core/request.d.ts +15 -0
- package/dist/core/response.d.ts +17 -0
- package/dist/core/routeContext.d.ts +17 -0
- package/dist/core/routesBuilder.d.ts +4 -0
- package/dist/core/server.d.ts +83 -0
- package/dist/dataAccess/dataAccessor.d.ts +20 -0
- package/dist/dataAccess/entityManager.d.ts +6 -0
- package/dist/dataAccess/entityMapper.d.ts +3 -0
- package/dist/dataAccess/filterHelper.d.ts +2 -0
- package/dist/dataAccess/propertyMapper.d.ts +3 -0
- package/dist/deno-std/assert/assert.d.ts +2 -0
- package/dist/deno-std/assert/assertion_error.d.ts +4 -0
- package/dist/deno-std/datetime/to_imf.d.ts +17 -0
- package/dist/deno-std/http/cookie.d.ts +134 -0
- package/dist/helpers/entityHelpers.d.ts +1 -0
- package/dist/helpers/inputHelper.d.ts +1 -0
- package/dist/helpers/runCollectionEntityHttpHandler.d.ts +5 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +3590 -0
- package/dist/plugins/authManager/httpHandlers/createSession.d.ts +8 -0
- package/dist/plugins/authManager/httpHandlers/deleteSession.d.ts +4 -0
- package/dist/plugins/authManager/httpHandlers/getMyProfile.d.ts +4 -0
- package/dist/plugins/authManager/httpHandlers/index.d.ts +5 -0
- package/dist/plugins/authManager/mod.d.ts +16 -0
- package/dist/plugins/authManager/models/AccessToken.d.ts +3 -0
- package/dist/plugins/authManager/models/index.d.ts +2 -0
- package/dist/plugins/authManager/routes/getMyProfile.d.ts +3 -0
- package/dist/plugins/authManager/routes/index.d.ts +2 -0
- package/dist/plugins/authManager/routes/signin.d.ts +3 -0
- package/dist/plugins/authManager/routes/signout.d.ts +3 -0
- package/dist/plugins/dataManager/httpHandlers/addEntityRelations.d.ts +4 -0
- package/dist/plugins/dataManager/httpHandlers/countCollectionEntities.d.ts +4 -0
- package/dist/plugins/dataManager/httpHandlers/createCollectionEntitiesBatch.d.ts +4 -0
- package/dist/plugins/dataManager/httpHandlers/createCollectionEntity.d.ts +4 -0
- package/dist/plugins/dataManager/httpHandlers/deleteCollectionEntityById.d.ts +4 -0
- package/dist/plugins/dataManager/httpHandlers/findCollectionEntities.d.ts +4 -0
- package/dist/plugins/dataManager/httpHandlers/findCollectionEntityById.d.ts +4 -0
- package/dist/plugins/dataManager/httpHandlers/queryDatabase.d.ts +4 -0
- package/dist/plugins/dataManager/httpHandlers/removeEntityRelations.d.ts +4 -0
- package/dist/plugins/dataManager/httpHandlers/updateCollectionEntityById.d.ts +4 -0
- package/dist/plugins/dataManager/mod.d.ts +16 -0
- package/dist/plugins/metaManager/httpHandlers/getMetaModelDetail.d.ts +4 -0
- package/dist/plugins/metaManager/httpHandlers/listMetaModels.d.ts +4 -0
- package/dist/plugins/metaManager/mod.d.ts +15 -0
- package/dist/plugins/routeManager/httpHandlers/httpProxy.d.ts +4 -0
- package/dist/plugins/routeManager/httpHandlers/listMetaRoutes.d.ts +4 -0
- package/dist/plugins/routeManager/mod.d.ts +15 -0
- package/dist/plugins/webhooks/mod.d.ts +24 -0
- package/dist/plugins/webhooks/pluginConfig.d.ts +48 -0
- package/dist/polyfill.d.ts +1 -0
- package/dist/proxy/mod.d.ts +13 -0
- package/dist/proxy/types.d.ts +17 -0
- package/dist/queryBuilder/index.d.ts +1 -0
- package/dist/queryBuilder/queryBuilder.d.ts +34 -0
- package/dist/server.d.ts +31 -0
- package/dist/types.d.ts +327 -0
- package/dist/utilities/httpUtility.d.ts +1 -0
- package/dist/utilities/jwtUtility.d.ts +8 -0
- package/dist/utilities/rapidUtility.d.ts +2 -0
- package/dist/utilities/typeUtility.d.ts +3 -0
- package/package.json +29 -0
- package/rollup.config.js +20 -0
- package/src/bootstrapApplicationConfig.ts +524 -0
- package/src/core/eventManager.ts +21 -0
- package/src/core/http-types.ts +4 -0
- package/src/core/httpHandler.ts +29 -0
- package/src/core/plugin.ts +13 -0
- package/src/core/pluginManager.ts +143 -0
- package/src/core/request.ts +23 -0
- package/src/core/response.ts +77 -0
- package/src/core/routeContext.ts +38 -0
- package/src/core/routesBuilder.ts +86 -0
- package/src/core/server.ts +144 -0
- package/src/dataAccess/dataAccessor.ts +110 -0
- package/src/dataAccess/entityManager.ts +651 -0
- package/src/dataAccess/entityMapper.ts +74 -0
- package/src/dataAccess/filterHelper.ts +47 -0
- package/src/dataAccess/propertyMapper.ts +27 -0
- package/src/deno-std/assert/assert.ts +9 -0
- package/src/deno-std/assert/assertion_error.ts +7 -0
- package/src/deno-std/datetime/to_imf.ts +47 -0
- package/src/deno-std/http/cookie.ts +398 -0
- package/src/helpers/entityHelpers.ts +24 -0
- package/src/helpers/inputHelper.ts +11 -0
- package/src/helpers/runCollectionEntityHttpHandler.ts +34 -0
- package/src/index.ts +12 -0
- package/src/plugins/authManager/httpHandlers/createSession.ts +57 -0
- package/src/plugins/authManager/httpHandlers/deleteSession.ts +22 -0
- package/src/plugins/authManager/httpHandlers/getMyProfile.ts +43 -0
- package/src/plugins/authManager/httpHandlers/index.ts +10 -0
- package/src/plugins/authManager/mod.ts +56 -0
- package/src/plugins/authManager/models/AccessToken.ts +56 -0
- package/src/plugins/authManager/models/index.ts +5 -0
- package/src/plugins/authManager/routes/getMyProfile.ts +15 -0
- package/src/plugins/authManager/routes/index.ts +9 -0
- package/src/plugins/authManager/routes/signin.ts +15 -0
- package/src/plugins/authManager/routes/signout.ts +15 -0
- package/src/plugins/dataManager/httpHandlers/addEntityRelations.ts +76 -0
- package/src/plugins/dataManager/httpHandlers/countCollectionEntities.ts +22 -0
- package/src/plugins/dataManager/httpHandlers/createCollectionEntitiesBatch.ts +57 -0
- package/src/plugins/dataManager/httpHandlers/createCollectionEntity.ts +43 -0
- package/src/plugins/dataManager/httpHandlers/deleteCollectionEntityById.ts +38 -0
- package/src/plugins/dataManager/httpHandlers/findCollectionEntities.ts +35 -0
- package/src/plugins/dataManager/httpHandlers/findCollectionEntityById.ts +30 -0
- package/src/plugins/dataManager/httpHandlers/queryDatabase.ts +29 -0
- package/src/plugins/dataManager/httpHandlers/removeEntityRelations.ts +72 -0
- package/src/plugins/dataManager/httpHandlers/updateCollectionEntityById.ts +53 -0
- package/src/plugins/dataManager/mod.ts +150 -0
- package/src/plugins/metaManager/httpHandlers/getMetaModelDetail.ts +14 -0
- package/src/plugins/metaManager/httpHandlers/listMetaModels.ts +13 -0
- package/src/plugins/metaManager/mod.ts +419 -0
- package/src/plugins/routeManager/httpHandlers/httpProxy.ts +15 -0
- package/src/plugins/routeManager/httpHandlers/listMetaRoutes.ts +13 -0
- package/src/plugins/routeManager/mod.ts +97 -0
- package/src/plugins/webhooks/mod.ts +144 -0
- package/src/plugins/webhooks/pluginConfig.ts +74 -0
- package/src/polyfill.ts +5 -0
- package/src/proxy/mod.ts +47 -0
- package/src/proxy/types.ts +21 -0
- package/src/queryBuilder/index.ts +1 -0
- package/src/queryBuilder/queryBuilder.ts +424 -0
- package/src/server.ts +192 -0
- package/src/types.ts +438 -0
- package/src/utilities/httpUtility.ts +23 -0
- package/src/utilities/jwtUtility.ts +16 -0
- package/src/utilities/rapidUtility.ts +5 -0
- package/src/utilities/typeUtility.ts +11 -0
- package/tsconfig.json +19 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { RpdApplicationConfig } from "~/types";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
models: [
|
|
5
|
+
{
|
|
6
|
+
name: "webhook",
|
|
7
|
+
namespace: "sys",
|
|
8
|
+
singularCode: "webhook",
|
|
9
|
+
pluralCode: "webhooks",
|
|
10
|
+
schema: "public",
|
|
11
|
+
tableName: "sys_webhooks",
|
|
12
|
+
properties: [
|
|
13
|
+
{
|
|
14
|
+
name: "id",
|
|
15
|
+
code: "id",
|
|
16
|
+
columnName: "id",
|
|
17
|
+
type: "integer",
|
|
18
|
+
required: true,
|
|
19
|
+
autoIncrement: true,
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
name: "name",
|
|
23
|
+
code: "name",
|
|
24
|
+
columnName: "name",
|
|
25
|
+
type: "text",
|
|
26
|
+
required: true,
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: "url",
|
|
30
|
+
code: "url",
|
|
31
|
+
columnName: "url",
|
|
32
|
+
type: "text",
|
|
33
|
+
required: true,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: "secret",
|
|
37
|
+
code: "secret",
|
|
38
|
+
columnName: "secret",
|
|
39
|
+
type: "text",
|
|
40
|
+
required: false,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: "namespace",
|
|
44
|
+
code: "namespace",
|
|
45
|
+
columnName: "namespace",
|
|
46
|
+
type: "text",
|
|
47
|
+
required: true,
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
name: "model singular code",
|
|
51
|
+
code: "modelSingularCode",
|
|
52
|
+
columnName: "model_singular_code",
|
|
53
|
+
type: "text",
|
|
54
|
+
required: true,
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: "events",
|
|
58
|
+
code: "events",
|
|
59
|
+
columnName: "events",
|
|
60
|
+
type: "json",
|
|
61
|
+
required: false,
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
name: "enabled",
|
|
65
|
+
code: "enabled",
|
|
66
|
+
columnName: "enabled",
|
|
67
|
+
type: "boolean",
|
|
68
|
+
required: true,
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
routes: [],
|
|
74
|
+
} satisfies RpdApplicationConfig;
|
package/src/polyfill.ts
ADDED
package/src/proxy/mod.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { fetchWithTimeout } from "../utilities/httpUtility";
|
|
2
|
+
import { ProxyContext, ProxyOptions } from "./types";
|
|
3
|
+
import { RouteContext} from "~/core/routeContext";
|
|
4
|
+
|
|
5
|
+
export async function doProxy(
|
|
6
|
+
sourceRouterCtx: RouteContext,
|
|
7
|
+
options: ProxyOptions,
|
|
8
|
+
) {
|
|
9
|
+
const proxyCtx = createProxyContext(sourceRouterCtx, options);
|
|
10
|
+
const targetRes = await sendTargetRequest(proxyCtx);
|
|
11
|
+
sendSourceResponse(proxyCtx, targetRes);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function createProxyContext(
|
|
15
|
+
sourceRouterCtx: RouteContext,
|
|
16
|
+
options: ProxyOptions,
|
|
17
|
+
) {
|
|
18
|
+
return {
|
|
19
|
+
sourceContext: {
|
|
20
|
+
request: sourceRouterCtx.request,
|
|
21
|
+
response: sourceRouterCtx.response,
|
|
22
|
+
},
|
|
23
|
+
targetContext: {},
|
|
24
|
+
options: options,
|
|
25
|
+
} satisfies ProxyContext;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function sendTargetRequest(proxyCtx: ProxyContext) {
|
|
29
|
+
const { sourceContext, options: proxyOptions } = proxyCtx;
|
|
30
|
+
const { target, timeout } = proxyOptions;
|
|
31
|
+
const { request: srcReq } = sourceContext;
|
|
32
|
+
const { method } = srcReq;
|
|
33
|
+
|
|
34
|
+
const reqInit: RequestInit = {
|
|
35
|
+
method,
|
|
36
|
+
};
|
|
37
|
+
return await fetchWithTimeout(target, reqInit, timeout);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function sendSourceResponse(
|
|
41
|
+
proxyCtx: ProxyContext,
|
|
42
|
+
targetRes: Response,
|
|
43
|
+
) {
|
|
44
|
+
const { response: srcRes } = proxyCtx.sourceContext;
|
|
45
|
+
srcRes.status = targetRes.status;
|
|
46
|
+
srcRes.body = targetRes.body;
|
|
47
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { RapidRequest } from "~/core/request";
|
|
2
|
+
import { RunProxyHandlerOptions } from "../types";
|
|
3
|
+
import { RapidResponse } from "~/core/response";
|
|
4
|
+
|
|
5
|
+
export interface ProxyContext {
|
|
6
|
+
sourceContext: ProxySourceContext;
|
|
7
|
+
targetContext: ProxyTargetContext;
|
|
8
|
+
options: ProxyOptions;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ProxySourceContext {
|
|
12
|
+
request: RapidRequest;
|
|
13
|
+
response: RapidResponse;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ProxyTargetContext {
|
|
17
|
+
request?: RapidRequest;
|
|
18
|
+
response?: RapidResponse;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type ProxyOptions = RunProxyHandlerOptions;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./queryBuilder";
|
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import * as _ from "lodash";
|
|
2
|
+
import {
|
|
3
|
+
CountEntityOptions,
|
|
4
|
+
DeleteEntityOptions,
|
|
5
|
+
EntityFilterOptions,
|
|
6
|
+
EntityFilterRelationalOperators,
|
|
7
|
+
FindEntityLogicalFilterOptions,
|
|
8
|
+
FindEntityOptions,
|
|
9
|
+
FindEntityRelationalFilterOptions,
|
|
10
|
+
FindEntityUnaryFilterOptions,
|
|
11
|
+
CreateEntityOptions,
|
|
12
|
+
RpdDataModel,
|
|
13
|
+
RpdDataModelProperty,
|
|
14
|
+
UpdateEntityOptions,
|
|
15
|
+
FindEntitySetFilterOptions,
|
|
16
|
+
QuoteTableOptions,
|
|
17
|
+
} from "../types";
|
|
18
|
+
|
|
19
|
+
const objLeftQuoteChar = '"';
|
|
20
|
+
const objRightQuoteChar = '"';
|
|
21
|
+
|
|
22
|
+
const relationalOperatorsMap = new Map<EntityFilterRelationalOperators, string>(
|
|
23
|
+
[
|
|
24
|
+
["eq", "="],
|
|
25
|
+
["ne", "<>"],
|
|
26
|
+
["gt", ">"],
|
|
27
|
+
["gte", ">="],
|
|
28
|
+
["lt", "<"],
|
|
29
|
+
["lte", "<="],
|
|
30
|
+
],
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
export interface BuildQueryContext {
|
|
35
|
+
params: any[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface InitQueryBuilderOptions {
|
|
39
|
+
dbDefaultSchema: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export default class QueryBuilder {
|
|
43
|
+
#dbDefaultSchema: string;
|
|
44
|
+
|
|
45
|
+
constructor(options: InitQueryBuilderOptions) {
|
|
46
|
+
this.#dbDefaultSchema = options.dbDefaultSchema;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
quoteTable(options: QuoteTableOptions) {
|
|
50
|
+
const { schema, tableName } = options;
|
|
51
|
+
if (schema) {
|
|
52
|
+
return `${this.quoteObject(schema)}.${this.quoteObject(tableName)}`;
|
|
53
|
+
} else if (this.#dbDefaultSchema) {
|
|
54
|
+
return `${this.quoteObject(this.#dbDefaultSchema)}.${this.quoteObject(tableName)}`;
|
|
55
|
+
} else {
|
|
56
|
+
return this.quoteObject(tableName);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
quoteObject(name: string) {
|
|
61
|
+
return `${objLeftQuoteChar}${name}${objRightQuoteChar}`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
select(model: RpdDataModel, options: FindEntityOptions) {
|
|
65
|
+
const ctx: BuildQueryContext = {
|
|
66
|
+
params: [],
|
|
67
|
+
};
|
|
68
|
+
let { properties, filters, orderBy, pagination } = options;
|
|
69
|
+
let command = "SELECT ";
|
|
70
|
+
if (!properties || !properties.length) {
|
|
71
|
+
command += "* FROM ";
|
|
72
|
+
} else {
|
|
73
|
+
command += properties.map(this.quoteObject).join(", ");
|
|
74
|
+
command += " FROM ";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
command += this.quoteTable(model);
|
|
78
|
+
|
|
79
|
+
if (filters && filters.length) {
|
|
80
|
+
command += " WHERE ";
|
|
81
|
+
command += buildFiltersQuery(ctx, filters);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (orderBy && orderBy.length) {
|
|
85
|
+
command += " ORDER BY ";
|
|
86
|
+
command += orderBy.map((item) => {
|
|
87
|
+
const quotedName = this.quoteObject(item.field);
|
|
88
|
+
return item.desc ? quotedName + " DESC" : quotedName;
|
|
89
|
+
}).join(", ");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (pagination) {
|
|
93
|
+
command += " OFFSET ";
|
|
94
|
+
ctx.params.push(pagination.offset);
|
|
95
|
+
command += "$" + ctx.params.length;
|
|
96
|
+
|
|
97
|
+
command += " LIMIT ";
|
|
98
|
+
ctx.params.push(pagination.limit);
|
|
99
|
+
command += "$" + ctx.params.length;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
command,
|
|
104
|
+
params: ctx.params,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
count(model: RpdDataModel, options: CountEntityOptions) {
|
|
109
|
+
const ctx: BuildQueryContext = {
|
|
110
|
+
params: [],
|
|
111
|
+
};
|
|
112
|
+
let { filters } = options;
|
|
113
|
+
let command = 'SELECT COUNT(*)::int as "count" FROM ';
|
|
114
|
+
|
|
115
|
+
command += this.quoteTable(model);
|
|
116
|
+
|
|
117
|
+
if (filters && filters.length) {
|
|
118
|
+
command += " WHERE ";
|
|
119
|
+
command += buildFiltersQuery(ctx, filters);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
command,
|
|
124
|
+
params: ctx.params,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
insert(model: RpdDataModel, options: CreateEntityOptions) {
|
|
129
|
+
const params: any[] = [];
|
|
130
|
+
const ctx: BuildQueryContext = {
|
|
131
|
+
params,
|
|
132
|
+
};
|
|
133
|
+
const { entity } = options;
|
|
134
|
+
let command = "INSERT INTO ";
|
|
135
|
+
|
|
136
|
+
command += this.quoteTable(model);
|
|
137
|
+
|
|
138
|
+
const propertyNames: string[] = Object.keys(entity);
|
|
139
|
+
let values = "";
|
|
140
|
+
propertyNames.forEach((propertyName, index) => {
|
|
141
|
+
if (index) {
|
|
142
|
+
values += ", ";
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
let property: RpdDataModelProperty | null = null;
|
|
146
|
+
if (model) {
|
|
147
|
+
property = _.find(
|
|
148
|
+
model.properties,
|
|
149
|
+
(e: RpdDataModelProperty) => e.code === propertyName,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (property && property.type === "json") {
|
|
154
|
+
params.push(JSON.stringify(entity[propertyName]));
|
|
155
|
+
values += `$${params.length}::jsonb`;
|
|
156
|
+
} else {
|
|
157
|
+
params.push(entity[propertyName]);
|
|
158
|
+
values += `$${params.length}`;
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
command += ` (${propertyNames.map(this.quoteObject).join(", ")})`;
|
|
163
|
+
command += ` VALUES (${values}) RETURNING *`;
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
command,
|
|
167
|
+
params: ctx.params,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
update(model: RpdDataModel, options: UpdateEntityOptions) {
|
|
172
|
+
const params: any[] = [];
|
|
173
|
+
const ctx: BuildQueryContext = {
|
|
174
|
+
params,
|
|
175
|
+
};
|
|
176
|
+
let { entity, filters } = options;
|
|
177
|
+
let command = "UPDATE ";
|
|
178
|
+
|
|
179
|
+
command += this.quoteTable(model);
|
|
180
|
+
|
|
181
|
+
command += " SET ";
|
|
182
|
+
const propertyNames: string[] = Object.keys(entity);
|
|
183
|
+
propertyNames.forEach((propertyName, index) => {
|
|
184
|
+
if (index) {
|
|
185
|
+
command += ", ";
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
let property: RpdDataModelProperty | null = null;
|
|
189
|
+
if (model) {
|
|
190
|
+
property = _.find(
|
|
191
|
+
model.properties,
|
|
192
|
+
(e: RpdDataModelProperty) => (e.columnName || e.code) === propertyName,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (property && property.type === "json") {
|
|
197
|
+
params.push(JSON.stringify(entity[propertyName]));
|
|
198
|
+
command += `${this.quoteObject(propertyName)}=$${params.length}::jsonb`;
|
|
199
|
+
} else {
|
|
200
|
+
params.push(entity[propertyName]);
|
|
201
|
+
command += `${this.quoteObject(propertyName)}=$${params.length}`;
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
if (filters && filters.length) {
|
|
206
|
+
command += " WHERE ";
|
|
207
|
+
command += buildFiltersQuery(ctx, filters);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
command += " RETURNING *";
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
command,
|
|
214
|
+
params: ctx.params,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
delete(model: RpdDataModel, options: DeleteEntityOptions) {
|
|
219
|
+
const params: any[] = [];
|
|
220
|
+
const ctx: BuildQueryContext = {
|
|
221
|
+
params,
|
|
222
|
+
};
|
|
223
|
+
let { filters } = options;
|
|
224
|
+
let command = "DELETE FROM ";
|
|
225
|
+
|
|
226
|
+
command += this.quoteTable(model);
|
|
227
|
+
|
|
228
|
+
if (filters && filters.length) {
|
|
229
|
+
command += " WHERE ";
|
|
230
|
+
command += buildFiltersQuery(ctx, filters);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return {
|
|
234
|
+
command,
|
|
235
|
+
params: ctx.params,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function buildFiltersQuery(
|
|
241
|
+
ctx: BuildQueryContext,
|
|
242
|
+
filters: EntityFilterOptions[],
|
|
243
|
+
) {
|
|
244
|
+
return buildFilterQuery(0, ctx, {
|
|
245
|
+
operator: "and",
|
|
246
|
+
filters,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function buildFilterQuery(
|
|
251
|
+
level: number,
|
|
252
|
+
ctx: BuildQueryContext,
|
|
253
|
+
filter: EntityFilterOptions,
|
|
254
|
+
): string {
|
|
255
|
+
const { operator } = filter;
|
|
256
|
+
if (
|
|
257
|
+
operator === "eq" || operator === "ne" || operator === "gt" ||
|
|
258
|
+
operator === "gte" || operator === "lt" || operator === "lte"
|
|
259
|
+
) {
|
|
260
|
+
return buildRelationalFilterQuery(ctx, filter);
|
|
261
|
+
} else if (operator === "and" || operator === "or") {
|
|
262
|
+
return buildLogicalFilterQuery(level, ctx, filter);
|
|
263
|
+
} else if (operator === "null" || operator === "notNull") {
|
|
264
|
+
return buildUnaryFilterQuery(ctx, filter);
|
|
265
|
+
} else if (operator === "in" || operator === "notIn") {
|
|
266
|
+
return buildInFilterQuery(ctx, filter);
|
|
267
|
+
} else if (operator === "contains") {
|
|
268
|
+
return buildContainsFilterQuery(ctx, filter);
|
|
269
|
+
} else if (operator === "notContains") {
|
|
270
|
+
return buildNotContainsFilterQuery(ctx, filter);
|
|
271
|
+
} else if (operator === "startsWith") {
|
|
272
|
+
return buildStartsWithFilterQuery(ctx, filter);
|
|
273
|
+
} else if (operator === "notStartsWith") {
|
|
274
|
+
return buildNotStartsWithFilterQuery(ctx, filter);
|
|
275
|
+
} else if (operator === "endsWith") {
|
|
276
|
+
return buildEndsWithFilterQuery(ctx, filter);
|
|
277
|
+
} else if (operator === "notEndsWith") {
|
|
278
|
+
return buildNotEndsWithFilterQuery(ctx, filter);
|
|
279
|
+
} else {
|
|
280
|
+
throw new Error(`Filter operator '${operator}' is not supported.`);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function buildLogicalFilterQuery(
|
|
285
|
+
level: number,
|
|
286
|
+
ctx: BuildQueryContext,
|
|
287
|
+
filter: FindEntityLogicalFilterOptions,
|
|
288
|
+
) {
|
|
289
|
+
let dbOperator;
|
|
290
|
+
if (filter.operator === "and") {
|
|
291
|
+
dbOperator = " AND ";
|
|
292
|
+
} else {
|
|
293
|
+
dbOperator = " OR ";
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
let command = filter.filters.map(buildFilterQuery.bind(null, level + 1, ctx))
|
|
297
|
+
.join(dbOperator);
|
|
298
|
+
if (level) {
|
|
299
|
+
return `(${command})`;
|
|
300
|
+
}
|
|
301
|
+
return command;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function buildUnaryFilterQuery(
|
|
305
|
+
ctx: BuildQueryContext,
|
|
306
|
+
filter: FindEntityUnaryFilterOptions,
|
|
307
|
+
) {
|
|
308
|
+
let command = this.quoteObject(filter.field);
|
|
309
|
+
if (filter.operator === "null") {
|
|
310
|
+
command += " IS NULL";
|
|
311
|
+
} else {
|
|
312
|
+
command += " IS NOT NULL";
|
|
313
|
+
}
|
|
314
|
+
return command;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function buildInFilterQuery(
|
|
318
|
+
ctx: BuildQueryContext,
|
|
319
|
+
filter: FindEntitySetFilterOptions,
|
|
320
|
+
) {
|
|
321
|
+
let command = this.quoteObject(filter.field);
|
|
322
|
+
|
|
323
|
+
if (filter.operator === "in") {
|
|
324
|
+
command += " = ";
|
|
325
|
+
} else {
|
|
326
|
+
command += " <> ";
|
|
327
|
+
}
|
|
328
|
+
ctx.params.push(filter.value);
|
|
329
|
+
command += `ANY($${ctx.params.length}::${filter.itemType || "int"}[])`;
|
|
330
|
+
|
|
331
|
+
return command;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function buildContainsFilterQuery(
|
|
335
|
+
ctx: BuildQueryContext,
|
|
336
|
+
filter: FindEntityRelationalFilterOptions,
|
|
337
|
+
) {
|
|
338
|
+
let command = this.quoteObject(filter.field);
|
|
339
|
+
|
|
340
|
+
command += " LIKE ";
|
|
341
|
+
ctx.params.push(`%${filter.value}%`);
|
|
342
|
+
command += "$" + ctx.params.length;
|
|
343
|
+
|
|
344
|
+
return command;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function buildNotContainsFilterQuery(
|
|
348
|
+
ctx: BuildQueryContext,
|
|
349
|
+
filter: FindEntityRelationalFilterOptions,
|
|
350
|
+
) {
|
|
351
|
+
let command = this.quoteObject(filter.field);
|
|
352
|
+
|
|
353
|
+
command += " NOT LIKE ";
|
|
354
|
+
ctx.params.push(`%${filter.value}%`);
|
|
355
|
+
command += "$" + ctx.params.length;
|
|
356
|
+
|
|
357
|
+
return command;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function buildStartsWithFilterQuery(
|
|
361
|
+
ctx: BuildQueryContext,
|
|
362
|
+
filter: FindEntityRelationalFilterOptions,
|
|
363
|
+
) {
|
|
364
|
+
let command = this.quoteObject(filter.field);
|
|
365
|
+
|
|
366
|
+
command += " LIKE ";
|
|
367
|
+
ctx.params.push(`${filter.value}%`);
|
|
368
|
+
command += "$" + ctx.params.length;
|
|
369
|
+
|
|
370
|
+
return command;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function buildNotStartsWithFilterQuery(
|
|
374
|
+
ctx: BuildQueryContext,
|
|
375
|
+
filter: FindEntityRelationalFilterOptions,
|
|
376
|
+
) {
|
|
377
|
+
let command = this.quoteObject(filter.field);
|
|
378
|
+
|
|
379
|
+
command += " NOT LIKE ";
|
|
380
|
+
ctx.params.push(`${filter.value}%`);
|
|
381
|
+
command += "$" + ctx.params.length;
|
|
382
|
+
|
|
383
|
+
return command;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function buildEndsWithFilterQuery(
|
|
387
|
+
ctx: BuildQueryContext,
|
|
388
|
+
filter: FindEntityRelationalFilterOptions,
|
|
389
|
+
) {
|
|
390
|
+
let command = this.quoteObject(filter.field);
|
|
391
|
+
|
|
392
|
+
command += " LIKE ";
|
|
393
|
+
ctx.params.push(`%${filter.value}`);
|
|
394
|
+
command += "$" + ctx.params.length;
|
|
395
|
+
|
|
396
|
+
return command;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function buildNotEndsWithFilterQuery(
|
|
400
|
+
ctx: BuildQueryContext,
|
|
401
|
+
filter: FindEntityRelationalFilterOptions,
|
|
402
|
+
) {
|
|
403
|
+
let command = this.quoteObject(filter.field);
|
|
404
|
+
|
|
405
|
+
command += " NOT LIKE ";
|
|
406
|
+
ctx.params.push(`%${filter.value}`);
|
|
407
|
+
command += "$" + ctx.params.length;
|
|
408
|
+
|
|
409
|
+
return command;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function buildRelationalFilterQuery(
|
|
413
|
+
ctx: BuildQueryContext,
|
|
414
|
+
filter: FindEntityRelationalFilterOptions,
|
|
415
|
+
) {
|
|
416
|
+
let command = this.quoteObject(filter.field);
|
|
417
|
+
|
|
418
|
+
command += relationalOperatorsMap.get(filter.operator);
|
|
419
|
+
|
|
420
|
+
ctx.params.push(filter.value);
|
|
421
|
+
command += "$" + ctx.params.length;
|
|
422
|
+
|
|
423
|
+
return command;
|
|
424
|
+
}
|