Skip to content

Commit

Permalink
[ACTIONS/SOURCES] Mssql server new actions sources (PipedreamHQ#8265)
Browse files Browse the repository at this point in the history
* Added files back

* pnpm-lock.yaml

* pnpm-lock.yaml
  • Loading branch information
jcortes authored Oct 5, 2023
1 parent d1d5bd6 commit d5c7a6c
Show file tree
Hide file tree
Showing 13 changed files with 780 additions and 22 deletions.
3 changes: 0 additions & 3 deletions components/microsoft_sql_server/.gitignore

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import app from "../../microsoft_sql_server.app.mjs";

export default {
key: "microsoft_sql_server-execute-query",
name: "Execute Query",
description: "Executes a SQL query and returns the results. [See the documentation](https://learn.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql?view=sql-server-ver16)",
type: "action",
version: "0.0.1",
props: {
app,
query: {
type: "string",
label: "Query",
description: "The SQL query to execute",
default: "select * from mytable where id = @input_parameter",
},
inputs: {
type: "object",
label: "Inputs",
description: "The inputs to the query. These will be available as @input_parameter in the query. For example, if you provide an input named 'id', you can use @id in the query.",
optional: true,
},
},
run({ $: step }) {
const {
app,
inputs,
query,
} = this;

return app.executeQuery({
step,
query,
inputs,
summary: () => "Successfully executed query.",
});
},
};
54 changes: 54 additions & 0 deletions components/microsoft_sql_server/actions/insert-row/insert-row.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import app from "../../microsoft_sql_server.app.mjs";

export default {
key: "microsoft_sql_server-insert-row",
name: "Insert Row",
description: "Inserts a new row in a table. [See the documentation](https://learn.microsoft.com/en-us/sql/t-sql/statements/insert-transact-sql?view=sql-server-ver16)",
type: "action",
version: "0.0.1",
props: {
app,
table: {
description: "The table to insert a row to",
propDefinition: [
app,
"table",
],
reloadProps: true,
},
},
async additionalProps() {
const {
app,
table,
} = this;

const { recordset } = await app.listColumns({
table,
});
const columnNames = recordset.map(({ COLUMN_NAME: columnName }) => columnName);
return columnNames.reduce((acc, columnName) => ({
...acc,
[columnName]: {
type: "string",
label: columnName,
description: `The value for the ${columnName} column`,
optional: true,
},
}), {});
},
run({ $: step }) {
const {
app,
table,
...inputs
} = this;

return app.insertRow({
step,
table,
inputs,
summary: () => "Successfully inserted row.",
});
},
};
13 changes: 0 additions & 13 deletions components/microsoft_sql_server/app/microsoft_sql_server.app.ts

This file was deleted.

15 changes: 15 additions & 0 deletions components/microsoft_sql_server/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const SUMMARY_LABEL = "$summary";
const BASE_URL = "https://microsoft_sql_server.com";
const VERSION_PATH = "/v1";
const DEFAULT_MAX = 600;
const DEFAULT_LIMIT = 60;
const CURRENT_COLUMNS = "currentColumns";

export default {
SUMMARY_LABEL,
BASE_URL,
VERSION_PATH,
DEFAULT_MAX,
DEFAULT_LIMIT,
CURRENT_COLUMNS,
};
11 changes: 11 additions & 0 deletions components/microsoft_sql_server/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
async function streamIterator(stream) {
const resources = [];
for await (const resource of stream) {
resources.push(resource);
}
return resources;
}

export default {
streamIterator,
};
199 changes: 199 additions & 0 deletions components/microsoft_sql_server/microsoft_sql_server.app.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import mssql from "mssql";
import { ConfigurationError } from "@pipedream/platform";
import constants from "./common/constants.mjs";
import utils from "./common/utils.mjs";

export default {
type: "app",
app: "microsoft_sql_server",
propDefinitions: {
table: {
type: "string",
label: "Table",
description: "The database table to watch for changes",
async options() {
const { recordset } = await this.listTables();
return recordset.map(({ TABLE_NAME: columnName }) => columnName);
},
},
column: {
type: "string",
label: "Column",
description: "The name of a column in the table.",
async options({ table }) {
const { recordset } = await this.listColumns({
table,
});
return recordset.map(({ COLUMN_NAME: columnName }) => columnName);
},
},
},
methods: {
exportSummary(step) {
if (!step?.export) {
throw new ConfigurationError("The summary method should be bind to the step object aka `$`");
}
return (msg = "") => step.export(constants.SUMMARY_LABEL, msg);
},
getConfig() {
const {
host, port, username, password, database, encrypt, trustServerCertificate,
} = this.$auth;
return {
user: username,
password,
database,
server: host,
port: Number(port),
options: {
// for azure
encrypt: String(encrypt).toLowerCase() === "true",
// true for local dev / self-signed certs
trustServerCertificate: String(trustServerCertificate).toLowerCase() === "true",
},
};
},
getConnection() {
return mssql.connect(this.getConfig());
},
async executeQuery({
step = this, summary, query = "", inputs = {},
} = {}) {
let connection;

try {
connection = await this.getConnection();

const input =
Object.entries(inputs)
.reduce((req, inputArgs) =>
req.input(...inputArgs), connection.request());

const response = await input.query(query);

if (typeof summary === "function") {
this.exportSummary(step)(summary(response));
}

return response;

} catch (error) {
console.log("Error executing query", error);
throw error;

} finally {
if (connection) {
await connection.close();
}
}
},
listTables(args = {}) {
const query = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'";
return this.executeQuery({
query,
...args,
});
},
listColumns({
table, ...args
} = {}) {
const query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @table";
return this.executeQuery({
query,
inputs: {
table,
},
...args,
});
},
insertRow({
table, inputs = {}, ...args
} = {}) {
const columns = Object.keys(inputs);
const values = columns.map((key) => `@${key}`).join(", ");
const query = `INSERT INTO ${table} (${columns.join(", ")}) VALUES (${values})`;
return this.executeQuery({
query,
inputs,
...args,
});
},
listNewColumns({
table, columns = [], ...args
} = {}) {
const whereColumns =
columns.length
? `AND COLUMN_NAME NOT IN (${columns.map((c) => `'${c}'`)})`
: "";
const query = `
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @table ${whereColumns}
`;
return this.executeQuery({
query,
inputs: {
table,
},
...args,
});
},
listResources({
table, column, page = 1, limit = constants.DEFAULT_LIMIT, ...args
} = {}) {
return this.executeQuery({
query: `
SELECT * FROM (
SELECT ROW_NUMBER() OVER (ORDER BY @column) AS pdId, * FROM ${table}
) AS subQuery
WHERE pdId
BETWEEN ((@page - 1) * @limit + 1)
AND (@page * @limit)
`,
inputs: {
column,
page,
limit,
},
...args,
});
},
async *getResourcesStream({
resourceFn,
resourceFnArgs,
resourceName,
max = constants.DEFAULT_MAX,
}) {
let page = 1;
let resourcesCount = 0;

while (true) {
const response = await resourceFn({
...resourceFnArgs,
page,
});

const nextResources = resourceName && response[resourceName] || response;

if (!nextResources?.length) {
console.log("No more resources found");
return;
}

for (const resource of nextResources) {
yield resource;
resourcesCount += 1;

if (resourcesCount >= max) {
return;
}
}

page += 1;
}
},
paginate(args = {}) {
const stream = this.getResourcesStream(args);
return utils.streamIterator(stream);
},
},
};
11 changes: 7 additions & 4 deletions components/microsoft_sql_server/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"name": "@pipedream/microsoft_sql_server",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Microsoft SQL Server Components",
"main": "dist/app/microsoft_sql_server.app.mjs",
"main": "microsoft_sql_server.app.mjs",
"keywords": [
"pipedream",
"microsoft_sql_server"
],
"files": ["dist"],
"homepage": "https://pipedream.com/apps/microsoft_sql_server",
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"dependencies": {
"@pipedream/platform": "^1.5.1",
"mssql": "^10.0.1"
},
"publishConfig": {
"access": "public"
}
}
}
14 changes: 14 additions & 0 deletions components/microsoft_sql_server/sources/common/base.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ConfigurationError } from "@pipedream/platform";
import app from "../../microsoft_sql_server.app.mjs";

export default {
props: {
app,
db: "$.service.db",
},
methods: {
generateMeta() {
throw new ConfigurationError("generateMeta is not implemented");
},
},
};
Loading

0 comments on commit d5c7a6c

Please sign in to comment.