Adicionar tarefa
Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Cria e salva uma entidade do tipo hipotético "Tarefa"
Mais informações
Para ver a documentação detalhada que inclui este exemplo de código, consulte:
Exemplo de código
Exceto em caso de indicação contrária, o conteúdo desta página é licenciado de acordo com a Licença de atribuição 4.0 do Creative Commons, e as amostras de código são licenciadas de acordo com a Licença Apache 2.0. Para mais detalhes, consulte as políticas do site do Google Developers. Java é uma marca registrada da Oracle e/ou afiliadas.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page demonstrates how to create and save a "Task" entity in Datastore using various programming languages.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples show how to define the "Task" entity with fields like "description," "created," and "done" in C#, Java, Node.js, PHP, Python, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eThe examples illustrate the process of setting up the Datastore client and creating or allocating a key for the new entity before its creation.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples use the 'datastore.save', 'datastore.put' or 'datastore.insert' commands to save the new entity.\u003c/p\u003e\n"],["\u003cp\u003eThe page provides links to the Datastore client libraries and API reference documentation for each language and also authentication documentation.\u003c/p\u003e\n"]]],[],null,["Creates and saves an Entity of the hypothetical type \"Task\"\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Getting started with the Firestore in Datastore mode API](/datastore/docs/datastore-api-tutorial)\n\nCode sample \n\nC#\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode C# API\nreference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /// \u003csummary\u003e\n /// Adds a task entity to the Datastore\n /// \u003c/summary\u003e\n /// \u003cparam name=\"description\"\u003eThe task description.\u003c/param\u003e\n /// \u003creturns\u003eThe key of the entity.\u003c/returns\u003e\n Key AddTask(string description)\n {\n Entity task = new Entity()\n {\n Key = _keyFactory.CreateIncompleteKey(),\n [\"description\"] = new Value()\n {\n StringValue = description,\n ExcludeFromIndexes = true\n },\n [\"created\"] = DateTime.UtcNow,\n [\"done\"] = false\n };\n return _db.Insert(task);\n }\n\nJava\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Java API\nreference documentation](https://cloud.google.com/java/docs/reference/google-cloud-datastore/latest/history).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /**\n * Adds a task entity to the Datastore.\n *\n * @param description The task description\n * @return The {@link Key} of the entity\n * @throws DatastoreException if the ID allocation or put fails\n */\n Key addTask(String description) {\n Key key = datastore.allocateId(keyFactory.newKey());\n Entity task =\n Entity.newBuilder(key)\n .set(\n \"description\",\n StringValue.newBuilder(description).setExcludeFromIndexes(true).build())\n .set(\"created\", Timestamp.now())\n .set(\"done\", false)\n .build();\n datastore.put(task);\n return key;\n }\n\nNode.js\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Node.js API\nreference documentation](https://cloud.google.com/nodejs/docs/reference/datastore/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n async function addTask(description) {\n const taskKey = datastore.key('Task');\n const entity = {\n key: taskKey,\n data: [\n {\n name: 'created',\n value: new Date().toJSON(),\n },\n {\n name: 'description',\n value: description,\n excludeFromIndexes: true,\n },\n {\n name: 'done',\n value: false,\n },\n ],\n };\n\n try {\n await datastore.save(entity);\n console.log(`Task ${taskKey.id} created successfully.`);\n } catch (err) {\n console.error('ERROR:', err);\n }\n }\n\nPHP\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode PHP API\nreference documentation](https://googleapis.github.io/google-cloud-php/#/docs/cloud-datastore/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n use Google\\Cloud\\Datastore\\DatastoreClient;\n\n /**\n * Create a new task with a given description.\n *\n * @param string $projectId The Google Cloud project ID.\n * @param string $description\n */\n function add_task(string $projectId, string $description)\n {\n $datastore = new DatastoreClient(['projectId' =\u003e $projectId]);\n\n $taskKey = $datastore-\u003ekey('Task');\n $task = $datastore-\u003eentity(\n $taskKey,\n [\n 'created' =\u003e new DateTime(),\n 'description' =\u003e $description,\n 'done' =\u003e false\n ],\n ['excludeFromIndexes' =\u003e ['description']]\n );\n $datastore-\u003einsert($task);\n printf('Created new task with ID %d.' . PHP_EOL, $task-\u003ekey()-\u003epathEnd()['id']);\n }\n\nPython\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Python API\nreference documentation](https://cloud.google.com/python/docs/reference/datastore/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/datastore/latest/\n\n def add_task(client: https://cloud.google.com/python/docs/reference/datastore/latest/.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html, description: str):\n # Create an incomplete key for an entity of kind \"Task\". An incomplete\n # key is one where Datastore will automatically generate an Id\n key = client.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html#google_cloud_datastore_client_Client_key(\"Task\")\n\n # Create an unsaved Entity object, and tell Datastore not to index the\n # `description` field\n task = https://cloud.google.com/python/docs/reference/datastore/latest/.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.entity.Entity.html(key, exclude_from_indexes=(\"description\",))\n\n # Apply new field values and save the Task entity to Datastore\n task.update(\n {\n \"created\": datetime.datetime.now(tz=datetime.timezone.utc),\n \"description\": description,\n \"done\": False,\n }\n )\n client.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html#google_cloud_datastore_client_Client_put(task)\n return task.key\n\nRuby\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Ruby API\nreference documentation](/ruby/docs/reference/google-cloud-datastore/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n def add_task description\n require \"google/cloud/datastore\"\n\n datastore = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-datastore/latest/Google-Cloud-Datastore.html.https://cloud.google.com/ruby/docs/reference/google-cloud-datastore/latest/Google-Cloud-Datastore.html\n\n task = datastore.entity \"Task\" do |t|\n t[\"description\"] = description\n t[\"created\"] = Time.now\n t[\"done\"] = false\n t.https://cloud.google.com/ruby/docs/reference/google-cloud-datastore/latest/Google-Cloud-Datastore-Entity.html \"description\", true\n end\n\n datastore.save task\n\n puts task.key.id\n\n task.key.id\n end\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=datastore)."]]