-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
82 lines (77 loc) · 2.2 KB
/
index.js
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
const { div, pre, code, text, textarea } = require("@saltcorn/markup/tags");
const { features, getState } = require("@saltcorn/data/db/state");
//const db = require("@saltcorn/data/db");
const { sqlBinOp } = require("@saltcorn/data/plugin-helper");
const ppArray = (v) => {
if (!v) return "";
if (Array.isArray(v)) return v.map((vl) => vl.toString()).join(",");
return "";
};
const pgvector = {
name: "PGVector",
setTypeAttributesForCalculatedFields: true,
sql_name: sqlBinOp ? ({ dimensions }) => `vector(${+dimensions})` : "vector", //legacy
distance_operators: sqlBinOp && {
nearL2: sqlBinOp("<->", "target", "field"),
inner: sqlBinOp("<#>", "target", "field"),
},
fieldviews: {
show: {
isEdit: false,
run: (v) => pre({ class: "wsprewrap" }, code(ppArray(v))),
},
edit: {
isEdit: true,
run: (nm, v, attrs, cls) =>
textarea(
{
class: ["form-control", cls],
name: encodeURIComponent(nm),
id: `input${encodeURIComponent(nm)}`,
rows: 10,
},
typeof v === "undefined" ? "" : text(ppArray(v)) || ""
),
},
},
attributes: [
{
label: "Dimensions",
name: "dimensions",
type: "Integer",
required: true,
sublabel:
"Once set this cannot be changed. To change dimensions, delete and recreate field",
},
],
read: (v, attrs) => {
switch (typeof v) {
//case "string":
// return v ? v.split(",") : undefined;
default:
return v;
}
},
};
module.exports = {
sc_plugin_api_version: 1,
types: [pgvector],
plugin_name: "pgvector",
/*onLoad() {
console.log("load");
db.pool.on("connect", async function (client) {
// https://github.com/pgvector/pgvector-node/blob/master/src/pg/index.js
const result = await client.query(
"SELECT typname, oid, typarray FROM pg_type WHERE typname = $1",
["vector"]
);
if (result.rowCount < 1) {
throw new Error("vector type not found in the database");
}
const oid = result.rows[0].oid;
client.setTypeParser(oid, "text", function (value) {
return JSON.stringify(value);
});
});
},*/
};