Create and Share Snippets创建和共享代码段
On this page本页内容
Experimental feature实验特性
This feature is experimental. MongoDB does not provide support for Snippets. 此功能是实验性的。MongoDB不支持Snippets。This feature may be changed or removed at any time without prior notice.此功能可能随时更改或删除,恕不另行通知。
Bugs are not expected, however should you encounter one, please open an issue in the GitHub repository预计不会出现错误,但如果您遇到错误,请在GitHub存储库 for this project.
中打开此项目的问题。
You can write scripts to manipulate data or carry out administrative tasks in 您可以编写脚本来操作mongosh
. mongosh
中的数据或执行管理任务。Packaging a script as a snippet provides a way to easily share scripts within your organization or across the MongoDB user community.将脚本打包为片段提供了一种在组织内或MongoDB用户社区中轻松共享脚本的方法。
This page discusses:本页讨论:
Preparing a snippet package.准备代码段包。Publishing the snippet package to a registry.将代码段包发布到注册表。
For examples of scripts and the metadata files in snippet packages, see the snippets in the community snippet registry有关代码片段包中的脚本和元数据文件的示例,请参阅GitHub上社区代码片段注册表 on GitHub.
中的代码片段。
If you plan to submit your snippet to the community registry如果您计划将代码段提交到社区注册表, be sure to review the information in Contribute a Snippet Package to the MongoDB Community.
,请务必查看向MongoDB社区贡献代码段包中的信息。
Create a Snippet Package创建代码段包
The steps in this section focus on packaging a script. For more details on writing scripts see Write Scripts.本节中的步骤侧重于打包脚本。有关编写脚本的更多详细信息,请参阅编写脚本。
Prepare the Files准备文件
Fork the Community Repository.分叉社区存储库。
If you plan to contribute to the community repository, fork the snippets project repository.如果您计划为社区存储库做出贡献,请派生snippets项目存储库。
You do not have to fork the community repository if you want to create a private repo, but you should manually recreate a similar directory structure as you work through the following steps.如果要创建私有repo,则不必派生社区存储库,但在执行以下步骤时,应手动重新创建类似的目录结构。
Create a Package Directory.创建包目录。
Create a directory for your snippet package under the 在分叉存储库中的snippets
directory in the forked repository. snippets
目录下为snippet包创建一个目录。This directory will contain the code for your script and several metadata files.该目录将包含脚本的代码和几个元数据文件。
This example shows directories for two snippet packages, 此示例显示了两个snippet包(decrypt-cards
and update-auth
. decrypt-cards
和update-auth
)的目录。The contents of the community snippets为了清楚起见,省略了社区snippets directories are omitted for clarity.
目录的内容。
mongo-snippets
|
├── scripts
│ ├── make-index.js
│ └── show-index.js
└── snippets
├── analyze-schema
├── decrypt-cards
│ ├── LICENSE-Community.txt
│ ├── README.md
│ ├── error-matchers.js
│ ├── index.js
│ └── package.json
├── mock-collection
├── mongocompat
├── resumetoken
├── spawn-mongod
└── update-auth
├── LICENSE
├── README.md
├── index.js
└── package.json
Create LICENSE
.创建LICENSE
。
LICENSE
.Create a 创建一个LICENSE
file. LICENSE
文件。You will need to enter a license identifier string later, so try to chose a license from the SPDX license list.稍后需要输入许可证标识符字符串,因此请尝试从SPDX许可证列表中选择一个许可证。
Create index.js
.创建index.js
。
index.js
.Create an 创建一个index.js
file.index.js
文件。
This file contains the entry point to your code that is exposed in the该文件包含在mongosh
console.mongosh
控制台中公开的代码的入口点。The script is written in JavaScript and defines your new functions.该脚本是用JavaScript编写的,并定义了您的新函数。The script can be in a single file or multiple files.脚本可以在单个文件中,也可以在多个文件中。The script can call other files and local or remote npm modules.该脚本可以调用其他文件以及本地或远程npm模块。To要require()
a remote npm module use the construction:require()
远程npm模块,请使用以下构造:const localRequire = require('module').createRequire(__filename);)
For an example, see index.js有关示例,请参阅in the
resumetoken
snippet.resumetoken
片段中的index.js。
index.js
is referenced in package.json.package.json
中引用了index.js
。The MongoDB repository has example code.MongoDB存储库有示例代码。
If you have an existing script, either rename it 如果您有一个现有的脚本,请将其重命名为index.js
or create an index.js
file to load it. index.js
或创建一个index.js
文件来加载它。For an example of an 有关加载其他脚本的index.js
file that loads other scripts, see this one in the community repository.
index.js
文件的示例,请参阅社区存储库中的这一个
。
Prepare the package.json
File准备package.json
文件
package.json
Filepackage.json
contains metadata that the package registry uses to manage snippets.包含包注册表用于管理代码段的元数据。
A minimal 一个最小的package.json
file looks like this:package.json
文件如下所示:
{
"name": "@mongosh/snippet-resumetoken",
"snippetName": "resumetoken",
"version": "1.0.2",
"description": "Resume token decoder script",
"main": "index.js",
"license": "Apache-2.0",
"publishConfig": {
"access": "public"
}
}
The parameters are:参数为:
Field | Description |
---|---|
"name" | |
"snippetName" | install .install 等命令一起使用的名称。 |
"version" | |
"description" | |
"main" | index.js . Note that functions in other files can be scoped so that they are also available in the the mongosh shell.index.js 的起点。请注意,其他文件中的函数可以设置作用域,以便它们也可以在mongosh shell中使用。 |
"license" | |
"publishConfig" | public is typical, but npm provides other optionspublic 是典型的,但npm也提供了其他选项。 |
Use this code to create a skeleton 使用此代码创建一个骨架package.json
file. package.json
文件。Edit the file and replace each 编辑文件并替换每个UPDATE
to insert the values for your snippet package.UPDATE
以插入代码段包的值。
{
"name": "@UPDATE/UPDATE",
"snippetName": "UPDATE",
"version": "UPDATE",
"description": "UPDATE",
"main": "UPDATE",
"license": "UPDATE",
"publishConfig": {
"access": "UPDATE"
}
}
There are several examples of MongoDB GitHub存储库package.json
files in the MongoDB GitHub repository.中有几个
package.json
文件的例子。
MongoDB uses npm as a package registry.MongoDB使用npm作为包注册表。
npm relies on the npm依赖于package.json
file to manage packages. package.json
文件来管理包。Refer to the npm package documentation有关 for more information about
package.json
.package.json
的更多信息,请参阅npm包文档。
Publish a Snippet发布代码段
To share your snippet, you must publish you snippet package to a registry. 若要共享代码片段,必须将代码片段包发布到注册表。The package will contain:该包将包含:
Your code您的代码README.md
LICENSE
filepackage.json
When the files are complete, follow these steps to create and publish your snippet package.文件完成后,请按照以下步骤创建并发布代码段包。
Create a registry index file.创建注册表索引文件。
The registry index file is not the same as the 注册表索引文件与包含代码段的index.js
file that contains your snippet code. index.js
文件不同。The registry index file, 注册表索引文件index.bson.br
, contains metadata for the snippet packages in your registry.index.bson.br
包含注册表中代码段包的元数据。
The registry index file must be compressed before it is uploaded for use. 在上载注册表索引文件以供使用之前,必须对其进行压缩。The make-index.jsscript目录中的make-index.js utility in the scripts directory walks through your snippet source directories gathering the information that is needed to create the registry index file.
实用程序遍历代码段源目录,集合创建注册表索引文件所需的信息。
After it creates the registry index file, 创建注册表索引文件后,make-index.js
script also compresses it.make-index.js
脚本也会对其进行压缩。
Run make-index.js从 from the
mongo-snippets
directory create the index.mongo-snippets
目录运行make-index.js创建索引。
node ./scripts/make-index.js
The output of this script is a brotli-compressed这个脚本的输出是一个brotli-compressed registry index file,
index.bson.br
.注册表索引文件
index.bson.br
。
You can use show-index.js您可以使用show-index.js to view the compressed registry index file.
来查看压缩的注册表索引文件。
Using 使用make-index.js
is the preferred way to create a registry index, but you can also create a registry index manually.make-index.js
是创建注册表索引的首选方式,但您也可以手动创建注册表索引。
Install the New Snippet Package安装新的代码段包
Follow these steps to install your new snippet package:按照以下步骤安装新的snippet包:
Contribute a Snippet Package to the MongoDB Community为MongoDB社区贡献一个Snippet包
If you have written a code snippet that might be useful for other MongoDB users, you are invited to contribute it to the community repository如果您编写了一个可能对其他MongoDB用户有用的代码片段,我们将邀请您将其贡献给GitHub上托管的社区存储库 hosted on GitHub.
。
To submit a snippet to the shared MongoDB repository:要将代码段提交到共享的MongoDB存储库,请执行以下操作:
Complete the Contributor Agreement.完成出资人协议。
Read and complete the MongoDB Contributor Agreement.阅读并完成MongoDB贡献者协议。
Clone the Repository.克隆存储库。
Fork and clone the snippet project repository从GitHub派生并克隆snippet项目存储库 from GitHub.
。
Create Your Package.创建您的程序包。
Create your snippet package. 创建代码段包。Be sure it contains the following files:请确保它包含以下文件:
package.json
index.js
README.md
LICENSE
You do not have to create a registry index file. 您不必创建注册表索引文件。If your snippet package is accepted, MongoDB will update the registry index file.如果您的snippet包被接受,MongoDB将更新注册表索引文件。
Submit Your Snippet Code.提交您的代码段。
Open a pull request against the snippet project repository.打开针对代码段项目存储库的拉请求。
MongoDB will review your pull request. If it is accepted, we will:MongoDB将审查您的拉取请求。如果被接受,我们将:
Merge your code into our GitHub repository.将您的代码合并到GitHub存储库中。Publish it to the npm registry.将其发布到npm注册表。Add it to the snippet index.将其添加到代码段索引中。