Add the following into your build.gradle file:
dependencies {
include “com.enonic.xp:lib-node:${xpVersion}”
}
JavaScript controller, add a require statement: const nodeLib = require(’/lib/xp/node’);
Which is my graphql.js located here:\src\main\resources\controllers
Setup my event library
Added the following to your `build.gradle` file:
dependencies {
include "com.enonic.xp:lib-event:${xpVersion}"
}
JavaScript controller, add a require statement:
const eventLib = require('/lib/xp/event');
Which is my graphql.js located here:\src\main\resources\controllers
Now I’m trying to setup connection to single repo. Where do I place this?
My goal is… after I setup my repo connection, to create 1,000 nodes with configuring this sample: var result1 = repo.create({ _name: "nerd", displayName: "Carpenter and IT expert", likes: "Plywood", numberOfUselessGadgets: 123 });
The nodelib.connect gives a connection to the main storrage.
If you want to create things then you can do that in the controller.
Just remember that the controller is executed everytime you visit a page.
So you might want to check if the content is created allready before adding a 1000 nodes.
Node apis create function is probably what you want to use.
Second addition:
Creating a 1000 nodes might take some time, and the controller renders the page so it might take a while. After you manage to get it working, you might want to take a look at services, then you could async load/create data.
You can place it anywhere. I suggest (like above), to create a service that runs the controller code. Then you access it with anything that calls the service url. If you access the service url the service controller will run.
Another option is the main.js file. You can create a repo there, but you do need extra tests to make sure its not created multiple times. So check if the repo allready exists. Since main.js is run every time you deploy an app.
Hi there @thrdcircle - To better help you out, it would be nice to understand what your objective is? Are you planning to create content via API, or are you looking to store user-generated data or other types of non-editorial data?
If you intend to create content, you should not use the node API, but the content API. The node API is a low level API that can store any type of data, and is completely schema-less. The content API however adds the CMS logic including content types to the process.
Also, the headless GraphQL API only works on top of the content API/CMS - it does not expose node-level data.
My objective is to import 1,000 “records” with some fielded IDs (4-5 non-editorial) and some blank user-generated data fields (2html areas) that may be populate once in content studio. Content type would be a general article.
The purpose is exploring the ease of the API instead of typing 1,000 pieces of content manually. Once in the repo, exploring the workflow with that large volume.
Also, looking to scope out the runtime on creating 1,000 on how much a lift for the server timing.
If you primarily want to explore creating content programmatically, there are essentially two approaches, via API, or via import. Imports are most commonly used after exporting from XP first.
It is probably because the controller is never executed. You need to be able to trigger your “local.js” controller to run. There are many ways of doing this. You could for instance trigger it when the application starts, or by mapping it to an endpoint. The simplest way of doing this is probably to convert your controller to a service (just move it to a special services folder)