-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtable-provider.js
More file actions
362 lines (352 loc) · 11.8 KB
/
Copy pathtable-provider.js
File metadata and controls
362 lines (352 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
const db = require("@saltcorn/data/db");
const { eval_expression } = require("@saltcorn/data/models/expression");
const Workflow = require("@saltcorn/data/models/workflow");
const Form = require("@saltcorn/data/models/form");
const FieldRepeat = require("@saltcorn/data/models/fieldrepeat");
const Field = require("@saltcorn/data/models/field");
const Table = require("@saltcorn/data/models/table");
const { discover_tables } = require("@saltcorn/data/models/discovery");
const {
aggregation_query_fields,
joinfield_renamer,
} = require("@saltcorn/data/models/internal/query");
const { getState } = require("@saltcorn/data/db/state");
const { mkTable } = require("@saltcorn/markup");
const { pre, code } = require("@saltcorn/markup/tags");
const { getConnectObject } = require("@saltcorn/data/db/connect");
const { isNode } = require("@saltcorn/data/utils");
const { deleteWhere, count, select, insert, update } = isNode()
? require("@saltcorn/postgres/postgres")(getConnectObject)
: {};
const {
sqlsanitize,
mkWhere,
mkSelectOptions,
orderByIsObject,
orderByIsOperator,
} = require("@saltcorn/db-common/internal");
const { getConnection } = require("./connections");
const configuration_workflow = (req) =>
new Workflow({
onDone: (ctx) => {
(ctx.fields || []).forEach((f) => {
if (f.summary_field) {
if (!f.attributes) f.attributes = {};
f.attributes.summary_field = f.summary_field;
}
});
return ctx;
},
steps: [
{
name: "table",
form: async () => {
return new Form({
fields: [
{
name: "host",
label: "Database host",
type: "String",
required: true,
exclude_from_mobile: true,
},
{
name: "port",
label: "Port",
type: "Integer",
required: true,
default: 5432,
exclude_from_mobile: true,
},
{
name: "user",
label: "User",
type: "String",
required: true,
exclude_from_mobile: true,
},
{
name: "password",
label: "Password",
type: "String",
fieldview: "password",
required: true,
sublabel:
"If blank, use environment variable <code>SC_EXTPG_PASS_{database name}</code>",
exclude_from_mobile: true,
},
{
name: "database",
label: "Database",
type: "String",
required: true,
exclude_from_mobile: true,
},
{
name: "schema",
label: "Schema",
type: "String",
exclude_from_mobile: true,
},
{
name: "table_name",
label: "Table name",
type: "String",
required: true,
exclude_from_mobile: true,
},
{
name: "read_only",
label: "Read-only",
type: "Bool",
},
{
name: "ssl",
label: "SSL",
type: "Bool",
},
{
name: "ca",
label: "CA",
type: "String",
fieldview: "textarea",
exclude_from_mobile: true,
showIf: { ssl: true },
},
{
name: "key",
label: "Key",
type: "String",
fieldview: "textarea",
exclude_from_mobile: true,
showIf: { ssl: true },
},
{
name: "cert",
label: "Certificate",
type: "String",
fieldview: "textarea",
exclude_from_mobile: true,
showIf: { ssl: true },
},
],
});
},
},
{
name: "fields",
form: async (ctx) => {
const pool = await getConnection(ctx);
const pack = await discover_tables(
[ctx.table_name],
ctx.schema,
pool,
);
const tables = await Table.find({});
const real_fkey_opts = tables.map((t) => `Key to ${t.name}`);
const fkey_opts = ["File", ...real_fkey_opts];
const form = new Form({
fields: [
{
input_type: "section_header",
label: "Column types",
},
new FieldRepeat({
name: "fields",
fields: [
{
name: "name",
label: "Name",
type: "String",
required: true,
},
{
name: "label",
label: "Label",
type: "String",
required: true,
},
{
name: "type",
label: "Type",
type: "String",
required: true,
attributes: {
options: getState().type_names.concat(fkey_opts || []),
},
},
{
name: "primary_key",
label: "Primary key",
type: "Bool",
//showIf: { type: pkey_options },
},
{
name: "summary_field",
label: "Summary field",
sublabel:
"The field name, on the target table, which will be used to pick values for this key",
type: "String",
showIf: { type: real_fkey_opts },
},
],
}),
],
});
if (!ctx.fields || !ctx.fields.length) {
if (!form.values) form.values = {};
form.values.fields = pack.tables[0].fields;
} else {
(ctx.fields || []).forEach((f) => {
if (f.type === "Key" && f.reftable_name)
f.type = `Key to ${f.reftable_name}`;
if (f.attributes?.summary_field)
f.summary_field = f.attributes?.summary_field;
const reftable_name =
f.reftable_name || typeof f.type === "string"
? f.type.replace("Key to ", "")
: null;
const reftable = reftable_name && Table.findOne(reftable_name);
const repeater = form.fields.find((ff) => ff.isRepeat);
const sum_form_field = repeater.fields.find(
(ff) => ff.name === "summary_field",
);
if (reftable && sum_form_field) {
sum_form_field.showIf.type = sum_form_field.showIf.type.filter(
(t) => t !== f.type,
);
repeater.fields.push(
new Field({
name: "summary_field",
label: "Summary field for " + f.name,
sublabel: `The field name, on the ${reftable_name} table, which will be used to pick values for this key`,
type: "String",
showIf: { type: f.type },
attributes: {
options: reftable.fields.map((f) => f.name),
},
}),
);
}
});
}
return form;
},
},
],
});
module.exports = {
"PostgreSQL remote table": {
configuration_workflow,
fields: (cfg) => {
return cfg?.fields || [];
},
get_table: (cfg) => {
return {
disableFiltering: true,
...(cfg?.read_only
? {}
: {
deleteRows: async (where, user) => {
const pool = await getConnection(cfg);
return await deleteWhere(cfg.table_name, where, {
schema: cfg.schema || "public",
client: pool,
});
},
updateRow: async (updRow, id, user) => {
const pool = await getConnection(cfg);
return await update(cfg.table_name, updRow, id, {
schema: cfg.schema || "public",
client: pool,
});
},
insertRow: async (rec, user) => {
const pool = await getConnection(cfg);
return await insert(cfg.table_name, rec, {
schema: cfg.schema || "public",
client: pool,
noid: true,
});
},
}),
countRows: async (where, opts) => {
const pool = await getConnection(cfg);
return await count(cfg.table_name, where || {}, {
schema: cfg.schema || "public",
client: pool,
});
},
aggregationQuery: async (aggregations, options) => {
const pool = await getConnection(cfg);
const { sql, values, groupBy } = aggregation_query_fields(
cfg.table_name,
aggregations,
{ ...options, schema: cfg.schema || "public" },
);
const res = await pool.query(sql, values);
if (groupBy) return res.rows;
return res.rows[0];
},
distinctValues: async (fieldnm, whereObj) => {
const pool = await getConnection(cfg);
if (whereObj) {
const { where, values } = mkWhere(whereObj, db.isSQLite);
const res = await pool.query(
`select distinct "${db.sqlsanitize(
fieldnm,
)}" from ${db.sqlsanitize(
cfg.table_name,
)} ${where} order by "${db.sqlsanitize(fieldnm)}"`,
values,
);
return res.rows.map((r) => r[fieldnm]);
} else {
const res = await pool.query(
`select distinct "${db.sqlsanitize(
fieldnm,
)}" from ${db.sqlsanitize(
cfg.table_name,
)} order by "${db.sqlsanitize(fieldnm)}"`,
);
return res.rows.map((r) => r[fieldnm]);
}
},
getRows: async (where, opts) => {
const pool = await getConnection(cfg);
const qres = await select(cfg.table_name, where, {
...opts,
schema: cfg.schema || "public",
client: pool,
});
return qres;
},
getJoinedRows: async (opts) => {
const pool = await getConnection(cfg);
const pseudoTable = new Table({
name: cfg.table_name,
fields: cfg.fields,
});
const { sql, values, joinFields, aggregations } =
await pseudoTable.getJoinedQuery({
schema: cfg.schema || "public",
...opts,
ignoreExternal: true,
});
if (db.get_sql_logging?.()) console.log(sql, values);
const res = await pool.query(sql, values);
let rows = joinfield_renamer
? joinfield_renamer(joinFields, aggregations)(res.rows)
: res.rows;
for (const k of Object.keys(joinFields || {})) {
if (!joinFields?.[k].lookupFunction) continue;
for (const row of rows) {
row[k] = await joinFields[k].lookupFunction(row);
}
}
return rows;
},
};
},
},
};