# NodeJS SDK(v.0.0.1) **Published by:** [laodao](https://paragraph.com/@ukpkmkk/) **Published on:** 2024-02-05 **URL:** https://paragraph.com/@ukpkmkk/nodejs-sdk-v-0-0-1 ## Content NodeJS SDK(v.0.0.1) 时空 JavaScript (NodeJS) SDK(JavaScript 版本 >= 19.8.0) 安装说明 git clone https://github.com/spaceandtimelabs/SxT-NodeJS-SDK.git 在运行代码之前,将其重命名为并确保您的凭据已在文件中正确设置。.env.sample.env.env 安装 SDK 依赖项: 壳 重要笔记 SDK 将为您创建一个新用户,除非您在 中提供现有用户名 、和,然后它将向该用户进行身份验证。 PRIVATEKEYPUBLICKEY.env 如果您在用户帐户之间进行更改,则还必须更改 .env 文件中的密钥。 中的代码演示了如何调用SDK。examples.js 可安装的 NPM 包即将推出。 ***注意:***由于 biscuits 实现使用需要 Rust 的 Web Assembly,因此在使用 NodeSDK 运行脚本时需要使用此标志。--experimental-wasm-modules 巴什 node --experimental-wasm-modules my-sdk-script.js 特征 会议 SDK 在基于文件的会话中实现持久存储。 加密 它支持 ED25519 公钥加密,用于饼干授权和保护平台中的数据。 SQL支持 🪄 支持 DDL:配置资源 - 执行所有 CREATE、ALTER、DROP 命令 🧪 支持 DML:修改数据 - 执行所有 INSERT、UPDATE、MERGE、DELETE 命令 💥 支持 SQL:执行查询 - 执行所有 SELECT 命令 🔎 支持 SQL 视图:执行视图 平台发现 用于获取有关数据库资源的元数据和信息。 命名空间 表格 表格列 表索引 表主键 表关系 表主键引用 表外键引用 例子 初始化SDK JavaScript // Initializing the Space and Time SDK for use. import SpaceAndTimeSDK from "./SpaceAndTimeSDK.js"; const initSDK = SpaceAndTimeSDK.init(); 使用空间和时间平台进行身份验证 当 SDK 注册新帐户时,它会将您的密钥保存到 .env 文件中。确保保存在身份验证和饼干生成中使用的私钥,否则您将无法访问用户和使用该密钥创建的表。 生成的有效期为 25 分钟,有效期为 30 分钟。AccessTokenRefreshToken JavaScript // Authenticate yourself using the Space and Time SDK. let [tokenResponse, tokenError] = await initSDK.Authenticate(); console.log(tokenResponse, tokenError); 生成饼干 您可以为一个表创建多个饼干令牌,从而允许您为用户提供不同的访问级别。所有能力的列表,请参考biscuit授权文档。 具有, , , ,权限的饼干生成示例select queryinsert queryupdate querydelete querycreate table JavaScript const biscuitCapabilityContainer = [ { biscuitOperation: "dql_select", biscuitResource: "ETH.TESTTABLE" }, { biscuitOperation: "dml_insert", biscuitResource: "ETH.TESTTABLE" }, { biscuitOperation: "dml_update", biscuitResource: "ETH.TESTTABLE" }, { biscuitOperation: "dml_delete", biscuitResource: "ETH.TESTTABLE" }, { biscuitOperation: "ddl_create", biscuitResource: "ETH.TESTTABLE" }, ] let biscuitBuilder = biscuit``; for(const {biscuitOperation, biscuitResource} of biscuitCapabilityContainer) { biscuitBuilder.merge(block`capability(${biscuitOperation},${biscuitResource});`); } // Generate the biscuit token biscuitToken = generateBiscuit("ETH.TESTTABLE", biscuitPrivateKey) DDL、DML 和 DQL 注意:要创建新架构,需要权限。ddl_create JavaScript // Create a Schema let [createSchemaResponse, createSchemaError] = await initSDK.CreateSchema("CREATE SCHEMA ETH"); console.log('Response: ', createSchemaResponse) console.log('Error: ', createSchemaError) // Only for Create Table Queries // for ALTER and DROP, use DDL() let [CreateTableResponse, CreateTableError] = await initSDK.CreateTable("ETH.TESTTABLE", "CREATE TABLE ETH.TESTTABLE(id INT PRIMARY KEY, test VARCHAR)", "permissioned", publickey, biscuit); console.log('Response: ', CreateTableResponse) console.log('Error: ', CreateTableError) // For ALTER and DROP let [DDLresponse, DDLerror] = await initSDK.DDL("ETH.TESTTABLE", "ALTER TABLE ETH.TESTTABLE ADD TEST2 VARCHAR", biscuit); console.log('Response: ', DDLresponse) console.log('Error: ', DDLerror) // DML // Use DML() to insert, update, delete and merge queries let [DMLResponse, DMLError] = await initSDK.DML("ETH.TESTTABLE", "INSERT INTO ETH.TESTTABLE VALUES(5, 'X5')", biscuit); console.log('Response: ', DMLResponse) console.log('Error: ', DMLError) // DQL for selecting content from the blockchain tables. let [DQLResponse, DQLError] = await initSDK.DQL("ETH.TESTTABLE", "SELECT * FROM ETH.TESTTABLE", biscuit); console.log('Response: ', DQLResponse) console.log('Error: ', DQLError) 发现 Discovery SDK 调用需要用户登录。 JavaScript // List Namespaces let [getNameSpaceResponse, getNameSpaceError] = await initSDK.getNameSpaces(); console.log('Response: ', getNameSpaceResponse) console.log('Error: ', getNameSpaceError) // List Tables in a given namespace // Possible scope values - ALL = all tables, PUBLIC = non-permissioned tables, PRIVATE = tables created by a requesting user let [getTableResponse, getTableError] = await initSDK.getTables("ALL","ETH"); console.log('Response: ', getTableResponse) console.log('Error: ', getTableError) // List columns for a given table in a namespace let [getTableColumnResponse, getTableColumnError] = await initSDK.getTableColumns("ETH","TESTTABLE"); console.log('Response: ', getTableColumnResponse) console.log('Error: ', getTableColumnError) // List table index for a given table in a namespace let [getTableIndexesResponse, getTableIndexesError] = await initSDK.getTableIndexes("ETH","TESTTABLE"); console.log('Response: ', getTableIndexesResponse) console.log('Error: ', getTableIndexesError) // List table primary key for a given table in a namespace let [getPrimaryKeyResponse, getPrimaryKeyError] = await initSDK.getPrimaryKeys("ETH", "TESTTABLE"); console.log('Response: ', getPrimaryKeyResponse) console.log('Error: ', getPrimaryKeyError) // List table relations for a namespace and scope let [tableRelationshipResponse, tableRelationshipError] = await initSDK.getTableRelationships("ETH", "PRIVATE"); console.log('Response: ', tableRelationshipResponse) console.log('Error: ', tableRelationshipError) // List table primary key references for a table, column and a namespace let [primaryKeyReferenceResponse, primaryKeyReferenceError] = await initSDK.getPrimaryKeyReferences("ETH", "TESTTABLE", "TEST"); console.log('Response: ', primaryKeyReferenceResponse) console.log('Error: ', primaryKeyReferenceError) // List table foreign key references for a table, column and a namespace let [foreignKeyReferenceResponse, foreignKeyReferenceError] = await initSDK.getForeignKeyReferences("ETH", "TESTTABLE", "TEST"); console.log('Response: ', foreignKeyReferenceResponse) console.log('Error: ', foreignKeyReferenceError) 贮存 对于文件存储,可以采用以下方法: JavaScript // File Storage Methods SpaceAndTimeInit.write_to_file(AccessToken, RefreshToken, AccessTokenExpires, RefreshTokenExpires) SpaceAndTimeInit.read_file_contents() ## Publication Information - [laodao](https://paragraph.com/@ukpkmkk/): Publication homepage - [All Posts](https://paragraph.com/@ukpkmkk/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@ukpkmkk): Subscribe to updates