-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathindex.json
More file actions
1 lines (1 loc) · 10.2 KB
/
index.json
File metadata and controls
1 lines (1 loc) · 10.2 KB
1
{"instances":{"application":{"persistent":false},"Header/0-0-0-0":{"expanded":false,"persistent":false,"locale":"en-US","i18n":{"home":{"title":"Nullstack","href":"/"},"links":[{"title":"What is Nullstack?","href":"/what-is-nullstack"},{"title":"Documentation","href":"/getting-started"},{"title":"Examples","href":"/examples"},{"title":"Contributors","href":"/contributors"},{"title":"F.A.Q","href":"/frequently-asked-questions"},{"title":"Waifu","href":"/waifu"}],"menu":{"title":"Toggle Menu"},"action":{"title":"Get Started","href":"/getting-started"},"search":{"title":"Search [ctrl + k]"},"language":{"title":"Português","href":"/pt-br"},"mode":{"dark":"Night Mode","light":"Day Mode"}}},"Article/0-0-0-18/script-runner":{"title":"Script Runner","html":"<p>The context is a simple javascript object that is completely decoupled from the framework itself and can be taken advantage of to use the same context as the main application to run scripts.<\\/p>\n<p>After compilation you can access it's keys running a script in another file that simply imports the environment bundle.<\\/p>\n<p>This pattern is much more convenient than having to duplicate your logic for one-off scripts.<\\/p>\n<p>You must manually start the application to make sure that the context is populated.<\\/p>\n<p>You must exit the process manually unless you want to have a script that is continuously alive.<\\/p>\n<p>The example bellow is a simple JavaScript file under <strong>scripts/seed.js<\\/strong> used for seeding the <a href=\"/how-to-use-mongodb-with-nullstack\">MongoDB database<\\/a> registered in the context:<\\/p>\n<pre><code class=\"language-jsx\"><span class=\"token comment\">// import from .production instead if you run this in production mode<\\/span>\n<span class=\"token keyword\">const<\\/span> <span class=\"token punctuation\">{<\\/span> <span class=\"token keyword\">default<\\/span><span class=\"token operator\">:<\\/span> context <span class=\"token punctuation\">}<\\/span> <span class=\"token operator\">=<\\/span> <span class=\"token function\">require<\\/span><span class=\"token punctuation\">(<\\/span><span class=\"token string\">'../.development/server.js'<\\/span><span class=\"token punctuation\">)<\\/span><span class=\"token punctuation\">;<\\/span>\n<span class=\"token keyword\">const<\\/span> Faker <span class=\"token operator\">=<\\/span> <span class=\"token function\">require<\\/span><span class=\"token punctuation\">(<\\/span><span class=\"token string\">'@faker-js/faker'<\\/span><span class=\"token punctuation\">)<\\/span><span class=\"token punctuation\">;<\\/span>\n\n<span class=\"token comment\">// registers 5 random fake users<\\/span>\n<span class=\"token keyword\">async<\\/span> <span class=\"token keyword\">function<\\/span> <span class=\"token function\">seed<\\/span><span class=\"token punctuation\">(<\\/span><span class=\"token punctuation\">)<\\/span> <span class=\"token punctuation\">{<\\/span>\n <span class=\"token keyword\">await<\\/span> context<span class=\"token punctuation\">.<\\/span><span class=\"token function\">start<\\/span><span class=\"token punctuation\">(<\\/span><span class=\"token punctuation\">)<\\/span><span class=\"token punctuation\">;<\\/span>\n <span class=\"token keyword\">const<\\/span> <span class=\"token punctuation\">{<\\/span> database<span class=\"token punctuation\">,<\\/span> project <span class=\"token punctuation\">}<\\/span> <span class=\"token operator\">=<\\/span> context<span class=\"token punctuation\">;<\\/span>\n <span class=\"token keyword\">for<\\/span> <span class=\"token punctuation\">(<\\/span><span class=\"token keyword\">let<\\/span> id <span class=\"token operator\">=<\\/span> <span class=\"token number\">0<\\/span><span class=\"token punctuation\">;<\\/span> id <span class=\"token operator\"><<\\/span> <span class=\"token number\">5<\\/span><span class=\"token punctuation\">;<\\/span> id<span class=\"token operator\">++<\\/span><span class=\"token punctuation\">)<\\/span> <span class=\"token punctuation\">{<\\/span>\n <span class=\"token keyword\">await<\\/span> database<span class=\"token punctuation\">.<\\/span><span class=\"token function\">collection<\\/span><span class=\"token punctuation\">(<\\/span><span class=\"token string\">'users'<\\/span><span class=\"token punctuation\">)<\\/span><span class=\"token punctuation\">.<\\/span><span class=\"token function\">insertOne<\\/span><span class=\"token punctuation\">(<\\/span><span class=\"token punctuation\">{<\\/span>\n id<span class=\"token punctuation\">,<\\/span>\n <span class=\"token literal-property property\">username<\\/span><span class=\"token operator\">:<\\/span> Faker<span class=\"token punctuation\">.<\\/span>name<span class=\"token punctuation\">.<\\/span><span class=\"token function\">firstName<\\/span><span class=\"token punctuation\">(<\\/span><span class=\"token punctuation\">)<\\/span>\n <span class=\"token punctuation\">}<\\/span><span class=\"token punctuation\">)<\\/span><span class=\"token punctuation\">;<\\/span>\n <span class=\"token punctuation\">}<\\/span>\n console<span class=\"token punctuation\">.<\\/span><span class=\"token function\">log<\\/span><span class=\"token punctuation\">(<\\/span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`<\\/span><span class=\"token string\">5 users seeded to <\\/span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${<\\/span>project<span class=\"token punctuation\">.<\\/span>name<span class=\"token interpolation-punctuation punctuation\">}<\\/span><\\/span><span class=\"token template-punctuation string\">`<\\/span><\\/span><span class=\"token punctuation\">)<\\/span><span class=\"token punctuation\">;<\\/span>\n process<span class=\"token punctuation\">.<\\/span><span class=\"token function\">exit<\\/span><span class=\"token punctuation\">(<\\/span><span class=\"token number\">0<\\/span><span class=\"token punctuation\">)<\\/span><span class=\"token punctuation\">;<\\/span>\n<span class=\"token punctuation\">}<\\/span>\n\n<span class=\"token function\">seed<\\/span><span class=\"token punctuation\">(<\\/span><span class=\"token punctuation\">)<\\/span>\n<\\/code><\\/pre>\n<p>To run it you can simply execute it as a normal node command.<\\/p>\n<blockquote>\n<p>🔥 You must always run it form the root in order to pick the correct .env file<\\/p>\n<\\/blockquote>\n<pre><code class=\"language-bash\"><span class=\"token operator\">><\\/span> node scripts<span class=\"token operator\">/<\\/span>seed<span class=\"token punctuation\">.<\\/span>js\n<span class=\"token number\">5<\\/span> users seeded\n<\\/code><\\/pre>\n<p>Script runners are great for many things like:<\\/p>\n<ul>\n<li>seeding a database in specific environment<\\/li>\n<li>testing <code>context<\\/code> behaviors<\\/li>\n<li>one-off tasks<\\/li>\n<li>cron jobs<\\/li>\n<\\/ul>\n<p>Note that you are not limited to the above, you can be creative and use it for more complex things like watching blockchain events or task needs access to the same context as your application.<\\/p>\n","persistent":true,"locale":"en-US","i18n":{"lead":"Have any questions or suggestions?","cta":"Join our Discord","next":"Next Step","learn":"Learn more about"},"description":"The context is a simple javascript object that is completely decoupled from the framework itself and can be taken advantage of to use the same context as the main application to run scripts","topics":[{"title":"About Nullstack","links":[{"title":"What is Nullstack?","href":"/what-is-nullstack"},{"title":"Getting started","href":"/getting-started"}]},{"title":"Core Features","links":[{"title":"Stateless Components","href":"/stateless-components"},{"title":"Stateful components","href":"/stateful-components"},{"title":"Full stack lifecycle","href":"/full-stack-lifecycle"},{"title":"Server functions","href":"/server-functions"},{"title":"Context","href":"/context"},{"title":"Two-way bindings","href":"/two-way-bindings"},{"title":"JSX elements","href":"/jsx-elements"},{"title":"Refs","href":"/refs"},{"title":"Styles","href":"/styles"},{"title":"Persistent Components","href":"/persistent-components"}]},{"title":"Context Keys","links":[{"title":"Routes and params","href":"/routes-and-params"},{"title":"Context data","href":"/context-data"},{"title":"Context instances","href":"/context-instances"},{"title":"Context environment","href":"/context-environment"},{"title":"Context page","href":"/context-page"},{"title":"Context project","href":"/context-project"},{"title":"Context settings","href":"/context-settings"},{"title":"Context secrets","href":"/context-secrets"},{"title":"Server request and response","href":"/server-request-and-response"},{"title":"Service Worker","href":"/service-worker"}]},{"title":"Advanced concepts","links":[{"title":"Proxy","href":"/proxy"},{"title":"Application Startup","href":"/application-startup"},{"title":"Script runner","href":"/script-runner"},{"title":"How to customize Webpack","href":"/how-to-customize-webpack"},{"title":"Transpilation and Security","href":"/transpilation-and-security"},{"title":"TypeScript","href":"/typescript"}]},{"title":"Build Modes","links":[{"title":"Server-side rendering","href":"/server-side-rendering"},{"title":"Static site generation","href":"/static-site-generation"},{"title":"Single page applications","href":"/single-page-applications"}]},{"title":"Other","links":[{"title":"Nullstack Logo","href":"/nullstack-logo"},{"title":"How to deploy","href":"/how-to-deploy-a-nullstack-application"}]}]},"GoogleAnalytics/0-0-0-19":{"persistent":false},"Loader/0-0-0-21":{"persistent":false},"Footer/0-0-0-22":{"persistent":false,"locale":"en-US","i18n":{"links":[{"title":"YouTube","href":"https://www.youtube.com/channel/UCUNPaxoppH3lu6JTrUX78Ww"},{"title":"Twitter","href":"https://twitter.com/nullstackapp"},{"title":"GitHub","href":"https://github.com/nullstack"}],"star":{"story":"Want to show your love and help us spread the word?","action":"Leave a star on GitHub"}}}},"page":{"image":"/image-1200x630.png","status":200,"locale":"en-US","title":"Script Runner - Nullstack","description":"The context is a simple javascript object that is completely decoupled from the framework itself and can be taken advantage of to use the same context as the main application to run scripts"}}