-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
107 lines (97 loc) · 3.73 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
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
/**
* This type describes the options that your connector expects to recieve
* This could include username + password, host + port, etc
* @typedef {Object} ConnectorOptions
* @property {string} SomeOption
*/
import { EvidenceType } from "@evidence-dev/db-commons";
/**
* @see https://docs.evidence.dev/plugins/create-source-plugin/#options-specification
* @see https://github.com/evidence-dev/evidence/blob/main/packages/postgres/index.cjs#L316
*/
export const options = {
SomeOption: {
title: "Some Option",
description:
"This object defines how SomeOption should be displayed and configured in the Settings UI",
type: "string", // options: 'string' | 'number' | 'boolean' | 'select' | 'file'
},
};
/**
* Implementing this function creates a "file-based" connector
*
* Each file in the source directory will be passed to this function, and it will return
* either an array, or an async generator {@see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*}
* that contains the query results
*
* @see https://docs.evidence.dev/plugins/create-source-plugin/
* @type {import("@evidence-dev/db-commons").GetRunner<ConnectorOptions>}
*/
export const getRunner = (options) => {
console.debug(`SomeOption = ${options.SomeOption}`);
// This function will be called for EVERY file in the sources directory
// If you are expecting a specific file type (e.g. SQL files), make sure to filter
// to exclude others.
// If you are using some local database file (e.g. a sqlite or duckdb file)
// You may also need to filter that file out as well
return async (queryText, queryPath) => {
// Note: add your logic to process each file queryText and/or queryPath here
// ...
// Example output, delete or modify as needed
const output = {
rows: [
{ someInt: 1, someString: "string" },
{ someInt: 2, someString: "string2" },
],
columnTypes: [
{
name: "someInt",
evidenceType: EvidenceType.NUMBER,
typeFidelity: "inferred",
},
{
name: "someString",
evidenceType: EvidenceType.STRING,
typeFidelity: "inferred",
},
],
expectedRowCount: 2,
};
return output;
};
};
// Uncomment to use the advanced source interface
// This uses the `yield` keyword, and returns the same type as getRunner, but with an added `name` and `content` field (content is used for caching)
// sourceFiles provides an easy way to read the source directory to check for / iterate through files
// /** @type {import("@evidence-dev/db-commons").ProcessSource<ConnectorOptions>} */
// export async function* processSource(options, sourceFiles, utilFuncs) {
// yield {
// title: "some_demo_table",
// content: "SELECT * FROM some_demo_table", // This is ONLY used for caching
// rows: [], // rows can be an array
// columnTypes: [
// {
// name: "someInt",
// evidenceType: EvidenceType.NUMBER,
// typeFidelity: "inferred",
// },
// ],
// };
// yield {
// title: "some_other_demo_table",
// content: "SELECT * FROM some_other_demo_table", // This is ONLY used for caching
// rows: async function* () {}, // rows can be a generator function for returning batches of results (e.g. if an API is paginated, or database supports cursors)
// columnTypes: [
// {
// name: "someOtherInt",
// evidenceType: EvidenceType.NUMBER,
// typeFidelity: "inferred",
// },
// ],
// };
// throw new Error("Process Source has not yet been implemented");
// }
/** @type {import("@evidence-dev/db-commons").ConnectionTester<ConnectorOptions>} */
export const testConnection = async (opts) => {
return true;
};