usf-cli 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,36 +1,36 @@
1
- exports.main = async (event, context) => {
2
- context.database = require('func-stateless-mongodb-sdk-nodejs').database;
3
- // If you do not specify a databaseId, the sdk will automatically search for and connect to an available database.
4
- // 如果没有指定 databaseId 的话,sdk 会自动获取可用的数据库进行连接。
5
- // Please note: if uos is authorized to store your database password in an encrypted form, this SDK will automatically fill in the password, if not authorized, you will need to specify the database password in your code
6
- // 注意:如果uos被授权以加密形式存储您的数据库密码,本sdk将自动填入密码,如果未被授权,您需要在代码中指定数据库密码。
7
- // Please note: if there are multiple available mongo databases, you must use the databaseId to specify the desired database you wish to connect to.
8
- // 注意:如果有多个可用的 mongo 数据库的话,请在代码中指定要用于连接的数据库的 databaseId
9
- const databaseId = '';
10
- const databasePassword = '';
11
- try {
12
- const database = await context.database(databaseId, databasePassword);
13
- const client = await database.connection();
14
- try {
15
- const db = await client.db('mongo');
16
- const collection = await db.createCollection('myCollection');
17
-
18
- const insertResult = await collection.insertOne({
19
- name: 'Alice',
20
- age: 25,
21
- });
22
- console.log('db insert result:', insertResult.acknowledged);
23
-
24
- const findResult = await collection.findOne({ name: 'Alice' });
25
- console.log('db query documents:', findResult);
26
- } catch (error) {
27
- console.error(error);
28
- }
29
-
30
- //test end pool
31
- await database.endConnection();
32
- console.log('closed the connection pool');
33
- } catch (error) {
34
- console.error(error);
35
- }
1
+ exports.main = async (event, context) => {
2
+ context.database = require('func-stateless-mongodb-sdk-nodejs').database;
3
+ // If you do not specify a databaseId, the sdk will automatically search for and connect to an available database.
4
+ // 如果没有指定 databaseId 的话,sdk 会自动获取可用的数据库进行连接。
5
+ // Please note: if uos is authorized to store your database password in an encrypted form, this SDK will automatically fill in the password, if not authorized, you will need to specify the database password in your code
6
+ // 注意:如果uos被授权以加密形式存储您的数据库密码,本sdk将自动填入密码,如果未被授权,您需要在代码中指定数据库密码。
7
+ // Please note: if there are multiple available mongo databases, you must use the databaseId to specify the desired database you wish to connect to.
8
+ // 注意:如果有多个可用的 mongo 数据库的话,请在代码中指定要用于连接的数据库的 databaseId
9
+ const databaseId = '';
10
+ const databasePassword = '';
11
+ try {
12
+ const database = await context.database(databaseId, databasePassword);
13
+ const client = await database.connection();
14
+ try {
15
+ const db = await client.db('mongo');
16
+ const collection = await db.createCollection('myCollection');
17
+
18
+ const insertResult = await collection.insertOne({
19
+ name: 'Alice',
20
+ age: 25,
21
+ });
22
+ console.log('db insert result:', insertResult.acknowledged);
23
+
24
+ const findResult = await collection.findOne({ name: 'Alice' });
25
+ console.log('db query documents:', findResult);
26
+ } catch (error) {
27
+ console.error(error);
28
+ }
29
+
30
+ //test end pool
31
+ await database.endConnection();
32
+ console.log('closed the connection pool');
33
+ } catch (error) {
34
+ console.error(error);
35
+ }
36
36
  };
@@ -1,60 +1,60 @@
1
- exports.main = async (event, context) => {
2
- context.database = require('func-stateless-mysql-sdk-nodejs').database;
3
- // If you do not specify a databaseId, the system will automatically search for and connect to an available database.
4
- // 如果没有指定 databaseId 的话,sdk 会自动获取可用的数据库进行连接。
5
- // Please note: if uos is authorized to store your database password in an encrypted form, this SDK will automatically fill in the password, if not authorized, you will need to specify the database password in your code.
6
- // 注意:如果uos被授权以加密形式存储您的数据库密码,本sdk将自动填入密码,如果未被授权,您需要在代码中指定数据库密码。
7
- // Please note: if there are multiple available mysql databases, you must use the databaseId to specify the desired database you wish to connect to.
8
- // 注意:如果有多个可用的 mysql 数据库的话,请在代码中指定要用于连接的数据库的 databaseId
9
- const databaseId = '';
10
- const databasePassword = '';
11
- try {
12
- const database = await context.database(databaseId, databasePassword);
13
- const connection = await database.connection();
14
- try {
15
- const databaseName = 'test';
16
- await connection.query(
17
- `CREATE DATABASE IF NOT EXISTS \`${databaseName}\``
18
- );
19
- console.log(`Database ${databaseName} ensured.`);
20
-
21
- await connection.query(`USE \`${databaseName}\``);
22
- console.log(`Using database ${databaseName}`);
23
-
24
- const createTableQuery = `
25
- CREATE TABLE IF NOT EXISTS users (
26
- id INT AUTO_INCREMENT PRIMARY KEY,
27
- name VARCHAR(255) NOT NULL,
28
- email VARCHAR(255) NOT NULL UNIQUE
29
- )
30
- `;
31
- await connection.query(createTableQuery);
32
- console.log('table created successfully');
33
-
34
- const insertQuery = `INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')`;
35
- await connection.query(insertQuery);
36
- console.log('rows inserted successfully');
37
-
38
- const [rows] = await connection.query('SELECT * FROM users');
39
- console.log('selected rows:', rows);
40
-
41
- const deleteTableQuery = `
42
- DROP TABLE IF EXISTS users
43
- `;
44
- await connection.query(deleteTableQuery);
45
- console.log('table deleted successfully');
46
- } catch (error) {
47
- console.error('error while setting up the database:', error);
48
- } finally {
49
- // Please note: mysql connection should be released manually if you successfully check it out, regardless of whether there was an error with the queries you ran on the client.
50
- // 注意:mysql 客户端需要被手动释放,无论查询过程是否发生错误
51
- await connection.release();
52
- }
53
-
54
- //test end pool
55
- await database.endConnection();
56
- console.log('closed the connection pool');
57
- } catch (error) {
58
- console.error(error);
59
- }
1
+ exports.main = async (event, context) => {
2
+ context.database = require('func-stateless-mysql-sdk-nodejs').database;
3
+ // If you do not specify a databaseId, the system will automatically search for and connect to an available database.
4
+ // 如果没有指定 databaseId 的话,sdk 会自动获取可用的数据库进行连接。
5
+ // Please note: if uos is authorized to store your database password in an encrypted form, this SDK will automatically fill in the password, if not authorized, you will need to specify the database password in your code.
6
+ // 注意:如果uos被授权以加密形式存储您的数据库密码,本sdk将自动填入密码,如果未被授权,您需要在代码中指定数据库密码。
7
+ // Please note: if there are multiple available mysql databases, you must use the databaseId to specify the desired database you wish to connect to.
8
+ // 注意:如果有多个可用的 mysql 数据库的话,请在代码中指定要用于连接的数据库的 databaseId
9
+ const databaseId = '';
10
+ const databasePassword = '';
11
+ try {
12
+ const database = await context.database(databaseId, databasePassword);
13
+ const connection = await database.connection();
14
+ try {
15
+ const databaseName = 'test';
16
+ await connection.query(
17
+ `CREATE DATABASE IF NOT EXISTS \`${databaseName}\``
18
+ );
19
+ console.log(`Database ${databaseName} ensured.`);
20
+
21
+ await connection.query(`USE \`${databaseName}\``);
22
+ console.log(`Using database ${databaseName}`);
23
+
24
+ const createTableQuery = `
25
+ CREATE TABLE IF NOT EXISTS users (
26
+ id INT AUTO_INCREMENT PRIMARY KEY,
27
+ name VARCHAR(255) NOT NULL,
28
+ email VARCHAR(255) NOT NULL UNIQUE
29
+ )
30
+ `;
31
+ await connection.query(createTableQuery);
32
+ console.log('table created successfully');
33
+
34
+ const insertQuery = `INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')`;
35
+ await connection.query(insertQuery);
36
+ console.log('rows inserted successfully');
37
+
38
+ const [rows] = await connection.query('SELECT * FROM users');
39
+ console.log('selected rows:', rows);
40
+
41
+ const deleteTableQuery = `
42
+ DROP TABLE IF EXISTS users
43
+ `;
44
+ await connection.query(deleteTableQuery);
45
+ console.log('table deleted successfully');
46
+ } catch (error) {
47
+ console.error('error while setting up the database:', error);
48
+ } finally {
49
+ // Please note: mysql connection should be released manually if you successfully check it out, regardless of whether there was an error with the queries you ran on the client.
50
+ // 注意:mysql 客户端需要被手动释放,无论查询过程是否发生错误
51
+ await connection.release();
52
+ }
53
+
54
+ //test end pool
55
+ await database.endConnection();
56
+ console.log('closed the connection pool');
57
+ } catch (error) {
58
+ console.error(error);
59
+ }
60
60
  };
@@ -1,30 +1,30 @@
1
- exports.main = async (event, context) => {
2
- context.database = require('func-stateless-postgresql-sdk-nodejs').database;
3
- // If you do not specify a databaseId, the system will automatically search for and connect to an available database.
4
- // 如果没有指定 databaseId 的话,sdk 会自动获取可用的数据库进行连接。
5
- // Please note: if uos is authorized to store your database password in an encrypted form, this SDK will automatically fill in the password, if not authorized, you will need to specify the database password in your code
6
- // 注意:如果uos被授权以加密形式存储您的数据库密码,本sdk将自动填入密码,如果未被授权,您需要在代码中指定数据库密码。
7
- // Please note: if there are multiple available postgresql databases, you must use the databaseId to specify the desired database you wish to connect to.
8
- // 注意:如果有多个可用的 postgresql 数据库的话,请在代码中指定要用于连接的数据库的 databaseId
9
- const databaseId = '';
10
- const databasePassword = '';
11
- try {
12
- const database = await context.database(databaseId, databasePassword);
13
- const connection = await database.connection();
14
- try {
15
- const result = await connection.query('SELECT tablename FROM pg_tables');
16
- console.log('db query result:', result.rows);
17
- } catch (error) {
18
- console.error('failed to query data, error:' + error);
19
- } finally {
20
- // Please note: psql client should be released manually if you successfully check it out, regardless of whether there was an error with the queries you ran on the client.
21
- // 注意:postgresql 客户端需要被手动释放,无论查询过程是否发生错误
22
- await connection.release();
23
- }
24
- // test end pool
25
- await database.endConnection();
26
- console.log('closed the connection pool');
27
- } catch (error) {
28
- console.error(error);
29
- }
1
+ exports.main = async (event, context) => {
2
+ context.database = require('func-stateless-postgresql-sdk-nodejs').database;
3
+ // If you do not specify a databaseId, the system will automatically search for and connect to an available database.
4
+ // 如果没有指定 databaseId 的话,sdk 会自动获取可用的数据库进行连接。
5
+ // Please note: if uos is authorized to store your database password in an encrypted form, this SDK will automatically fill in the password, if not authorized, you will need to specify the database password in your code
6
+ // 注意:如果uos被授权以加密形式存储您的数据库密码,本sdk将自动填入密码,如果未被授权,您需要在代码中指定数据库密码。
7
+ // Please note: if there are multiple available postgresql databases, you must use the databaseId to specify the desired database you wish to connect to.
8
+ // 注意:如果有多个可用的 postgresql 数据库的话,请在代码中指定要用于连接的数据库的 databaseId
9
+ const databaseId = '';
10
+ const databasePassword = '';
11
+ try {
12
+ const database = await context.database(databaseId, databasePassword);
13
+ const connection = await database.connection();
14
+ try {
15
+ const result = await connection.query('SELECT tablename FROM pg_tables');
16
+ console.log('db query result:', result.rows);
17
+ } catch (error) {
18
+ console.error('failed to query data, error:' + error);
19
+ } finally {
20
+ // Please note: psql client should be released manually if you successfully check it out, regardless of whether there was an error with the queries you ran on the client.
21
+ // 注意:postgresql 客户端需要被手动释放,无论查询过程是否发生错误
22
+ await connection.release();
23
+ }
24
+ // test end pool
25
+ await database.endConnection();
26
+ console.log('closed the connection pool');
27
+ } catch (error) {
28
+ console.error(error);
29
+ }
30
30
  };
@@ -1,32 +1,32 @@
1
- exports.main = async (event, context) => {
2
- context.database = require('func-stateless-redis-sdk-nodejs').database;
3
- // If you do not specify a databaseId, the system will automatically search for and connect to an available database.
4
- // 如果没有指定 databaseId 的话,sdk 会自动获取可用的数据库进行连接。
5
- // Please note: if uos is authorized to store your database password in an encrypted form, this SDK will automatically fill in the password, if not authorized, you will need to specify the database password in your code
6
- // 注意:如果uos被授权以加密形式存储您的数据库密码,本sdk将自动填入密码,如果未被授权,您需要在代码中指定数据库密码。
7
- // Please note: if there are multiple available redis, you must use the databaseId to specify the desired database you wish to connect to.
8
- // 注意:如果有多个可用的 redis 的话,请在代码中指定要用于连接的 databaseId
9
- const databaseId = '';
10
- const databasePassword = '';
11
- try {
12
- const database = await context.database(databaseId, databasePassword);
13
- const client = await database.connection();
14
- try {
15
- await client.set('someKey', 'someValue');
16
- const result = await client.get('someKey');
17
- console.log('db query result:', result);
18
- } catch (error) {
19
- console.error('failed to query data, error:' + error);
20
- } finally {
21
- // Please note: redis client should be released manually if you successfully check it out, regardless of whether there was an error with the queries you ran on the client.
22
- // 注意:redis客户端需要被手动释放,无论查询过程是否发生错误
23
- await client.release();
24
- }
25
-
26
- // test end pool
27
- await database.endConnection();
28
- console.log('closed the connection pool');
29
- } catch (error) {
30
- console.error(error);
31
- }
1
+ exports.main = async (event, context) => {
2
+ context.database = require('func-stateless-redis-sdk-nodejs').database;
3
+ // If you do not specify a databaseId, the system will automatically search for and connect to an available database.
4
+ // 如果没有指定 databaseId 的话,sdk 会自动获取可用的数据库进行连接。
5
+ // Please note: if uos is authorized to store your database password in an encrypted form, this SDK will automatically fill in the password, if not authorized, you will need to specify the database password in your code
6
+ // 注意:如果uos被授权以加密形式存储您的数据库密码,本sdk将自动填入密码,如果未被授权,您需要在代码中指定数据库密码。
7
+ // Please note: if there are multiple available redis, you must use the databaseId to specify the desired database you wish to connect to.
8
+ // 注意:如果有多个可用的 redis 的话,请在代码中指定要用于连接的 databaseId
9
+ const databaseId = '';
10
+ const databasePassword = '';
11
+ try {
12
+ const database = await context.database(databaseId, databasePassword);
13
+ const client = await database.connection();
14
+ try {
15
+ await client.set('someKey', 'someValue');
16
+ const result = await client.get('someKey');
17
+ console.log('db query result:', result);
18
+ } catch (error) {
19
+ console.error('failed to query data, error:' + error);
20
+ } finally {
21
+ // Please note: redis client should be released manually if you successfully check it out, regardless of whether there was an error with the queries you ran on the client.
22
+ // 注意:redis客户端需要被手动释放,无论查询过程是否发生错误
23
+ await client.release();
24
+ }
25
+
26
+ // test end pool
27
+ await database.endConnection();
28
+ console.log('closed the connection pool');
29
+ } catch (error) {
30
+ console.error(error);
31
+ }
32
32
  };
@@ -1,9 +1,9 @@
1
- exports.main = async function (event, context) {
2
- const { httpMethod,headers, body } = event;
3
-
4
- console.log('Http method:', httpMethod);
5
- console.log('Http headers:', JSON.stringify(headers));
6
- console.log('Request body', JSON.stringify(body));
7
-
8
- return 'hello world';
1
+ exports.main = async function (event, context) {
2
+ const { httpMethod,headers, body } = event;
3
+
4
+ console.log('Http method:', httpMethod);
5
+ console.log('Http headers:', JSON.stringify(headers));
6
+ console.log('Request body', JSON.stringify(body));
7
+
8
+ return 'hello world';
9
9
  };
@@ -1,89 +1,89 @@
1
- import path from "path";
2
- import semver from "semver";
3
- import { readPackageUp } from "read-pkg-up";
4
- import { pathToFileURL } from "url";
5
- import { createRequire } from "module";
6
-
7
- const require = createRequire(import.meta.url);
8
-
9
- function getFunctionModulePath(codeLocation) {
10
- try {
11
- return require.resolve(codeLocation);
12
- } catch (ex) {}
13
-
14
- try {
15
- return require.resolve(codeLocation + "/index.js");
16
- } catch (ex) {}
17
-
18
- return null;
19
- }
20
-
21
- const dynamicImport = new Function("modulePath", "return import(modulePath)");
22
-
23
- async function isEsModule(modulePath) {
24
- const ext = path.extname(modulePath);
25
- if (ext === ".mjs") {
26
- return true;
27
- }
28
- if (ext === ".cjs") {
29
- return false;
30
- }
31
-
32
- const pkg = await readPackageUp({
33
- cwd: path.dirname(modulePath),
34
- normalize: false,
35
- });
36
- return pkg?.packageJson.type === "module";
37
- }
38
-
39
- const dynamicRunner = async function (path, event = {}, entry = "main") {
40
- const functionModulePath = getFunctionModulePath(path);
41
- if (functionModulePath === null) {
42
- console.error(`'${path}' is not a loadable module.`);
43
- return;
44
- }
45
-
46
- let functionModule;
47
- const esModule = await isEsModule(functionModulePath);
48
- if (esModule) {
49
- if (semver.lt(process.version, MIN_NODE_VERSION_ESMODULES)) {
50
- console.error(
51
- `Cannot load ES Module on Node.js ${process.version}. ` +
52
- `Please upgrade to Node.js v${MIN_NODE_VERSION_ESMODULES} and up.`
53
- );
54
- return null;
55
- }
56
- const fpath = pathToFileURL(functionModulePath);
57
- functionModule = await dynamicImport(fpath.href);
58
- } else {
59
- functionModule = require(functionModulePath);
60
- }
61
- const userFunction = entry.split(".").reduce((code, functionTargetPart) => {
62
- if (typeof code === "undefined") {
63
- return undefined;
64
- } else {
65
- return code[functionTargetPart];
66
- }
67
- }, functionModule);
68
-
69
- if (typeof userFunction === "undefined") {
70
- console.error(`Function '${path}' not found.`);
71
- return;
72
- }
73
-
74
- if (typeof userFunction !== "function") {
75
- console.error(
76
- `'${path}/${entry}' needs to be of type function. Got: ` +
77
- `${typeof userFunction}`
78
- );
79
- return;
80
- }
81
-
82
- console.log(`Function '${path}/${entry}' is running:`);
83
- console.log("\n=================Event:");
84
- console.log(event);
85
- const res = await userFunction(event, {});
86
- console.log(`\n=================Return:\n${JSON.stringify(res)}`);
87
- };
88
-
89
- export { dynamicRunner };
1
+ import path from "path";
2
+ import semver from "semver";
3
+ import { readPackageUp } from "read-pkg-up";
4
+ import { pathToFileURL } from "url";
5
+ import { createRequire } from "module";
6
+
7
+ const require = createRequire(import.meta.url);
8
+
9
+ function getFunctionModulePath(codeLocation) {
10
+ try {
11
+ return require.resolve(codeLocation);
12
+ } catch (ex) {}
13
+
14
+ try {
15
+ return require.resolve(codeLocation + "/index.js");
16
+ } catch (ex) {}
17
+
18
+ return null;
19
+ }
20
+
21
+ const dynamicImport = new Function("modulePath", "return import(modulePath)");
22
+
23
+ async function isEsModule(modulePath) {
24
+ const ext = path.extname(modulePath);
25
+ if (ext === ".mjs") {
26
+ return true;
27
+ }
28
+ if (ext === ".cjs") {
29
+ return false;
30
+ }
31
+
32
+ const pkg = await readPackageUp({
33
+ cwd: path.dirname(modulePath),
34
+ normalize: false,
35
+ });
36
+ return pkg?.packageJson.type === "module";
37
+ }
38
+
39
+ const dynamicRunner = async function (path, event = {}, entry = "main") {
40
+ const functionModulePath = getFunctionModulePath(path);
41
+ if (functionModulePath === null) {
42
+ console.error(`'${path}' is not a loadable module.`);
43
+ return;
44
+ }
45
+
46
+ let functionModule;
47
+ const esModule = await isEsModule(functionModulePath);
48
+ if (esModule) {
49
+ if (semver.lt(process.version, MIN_NODE_VERSION_ESMODULES)) {
50
+ console.error(
51
+ `Cannot load ES Module on Node.js ${process.version}. ` +
52
+ `Please upgrade to Node.js v${MIN_NODE_VERSION_ESMODULES} and up.`
53
+ );
54
+ return null;
55
+ }
56
+ const fpath = pathToFileURL(functionModulePath);
57
+ functionModule = await dynamicImport(fpath.href);
58
+ } else {
59
+ functionModule = require(functionModulePath);
60
+ }
61
+ const userFunction = entry.split(".").reduce((code, functionTargetPart) => {
62
+ if (typeof code === "undefined") {
63
+ return undefined;
64
+ } else {
65
+ return code[functionTargetPart];
66
+ }
67
+ }, functionModule);
68
+
69
+ if (typeof userFunction === "undefined") {
70
+ console.error(`Function '${path}' not found.`);
71
+ return;
72
+ }
73
+
74
+ if (typeof userFunction !== "function") {
75
+ console.error(
76
+ `'${path}/${entry}' needs to be of type function. Got: ` +
77
+ `${typeof userFunction}`
78
+ );
79
+ return;
80
+ }
81
+
82
+ console.log(`Function '${path}/${entry}' is running:`);
83
+ console.log("\n=================Event:");
84
+ console.log(event);
85
+ const res = await userFunction(event, {});
86
+ console.log(`\n=================Return:\n${JSON.stringify(res)}`);
87
+ };
88
+
89
+ export { dynamicRunner };