forked from PipedreamHQ/pipedream
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ACTIONS/SOURCES] Mssql server new actions sources (PipedreamHQ#8265)
* Added files back * pnpm-lock.yaml * pnpm-lock.yaml
- Loading branch information
Showing
13 changed files
with
780 additions
and
22 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
components/microsoft_sql_server/actions/execute-query/execute-query.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
components/microsoft_sql_server/actions/insert-row/insert-row.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
components/microsoft_sql_server/app/microsoft_sql_server.app.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
199
components/microsoft_sql_server/microsoft_sql_server.app.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}, | ||
}, | ||
}; |
Oops, something went wrong.