René's URL Explorer Experiment


Title: Script Runner - Nullstack

Open Graph 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

Open Graph 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

Opengraph URL: https://nullstack.app/script-runner

Generator: Created with Nullstack - https://nullstack.app

direct link

Domain: nullstack.app

og:imagehttps://nullstack.app/image-1200x630.png
og:typewebsite
og:site_nameNullstack
og:localeen-US
mobile-web-app-capableyes
apple-mobile-web-app-capableyes
application-nameNullstack
apple-mobile-web-app-titleNullstack
msapplication-starturl/
msapplication-TileColor#2d3748
nullstack{"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"},"environment":{"client":false,"server":true,"development":false,"production":true,"mode":"ssg","key":"23ad2dd940244ab04e281282c05613bd73043c4f","name":""},"settings":{},"worker":{"enabled":true,"fetching":false,"preload":["/application-startup","/context-data","/context-environment","/context-instances","/context-page","/context-project","/context-secrets","/context-settings","/context","/frequently-asked-questions","/full-stack-lifecycle","/getting-started","/how-to-customize-webpack","/how-to-deploy-a-nullstack-application","/jsx-elements","/nullstack-logo","/persistent-components","/proxy","/refs","/routes-and-params","/script-runner","/server-functions","/server-request-and-response","/server-side-rendering","/service-worker","/single-page-applications","/stateful-components","/stateless-components","/static-site-generation","/styles","/transpilation-and-security","/two-way-bindings","/typescript","/what-is-nullstack","/illustrations/nulla-dps.webp","/illustrations/nulla-fullbody.png","/illustrations/nulla-glue.webp","/illustrations/nulla-healer.webp","/illustrations/nulla-hero.webp","/illustrations/nulla-map.webp","/illustrations/nulla-scrolls.webp","/illustrations/nulla-tanker.webp","/illustrations/nulla-tools.webp","/arrow.webp","/stars.webp","/footer.webp","/contributors","/roboto-v20-latin-300.woff2","/roboto-v20-latin-500.woff2","/crete-round-v9-latin-regular.woff2"],"staleWhileRevalidate":[],"cacheFirst":[],"headers":{},"api":"","cdn":"","protocol":"https","queues":{},"online":true,"responsive":true},"params":{"slug":"script-runner"},"project":{"domain":"nullstack.app","name":"Nullstack","color":"#d22365","viewport":"width=device-width, initial-scale=1, shrink-to-fit=no","type":"website","display":"standalone","orientation":"portrait","scope":"/","root":"/","sitemap":true,"favicon":"/favicon-96x96.png","disallow":[],"icons":{"72":"/icon-72x72.png","96":"/icon-96x96.png","128":"/icon-128x128.png","144":"/icon-144x144.png","152":"/icon-152x152.png","180":"/icon-180x180.png","192":"/icon-192x192.png","384":"/icon-384x384.png","512":"/icon-512x512.png"},"backgroundColor":"#2d3748"},"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":"

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

After compilation you can access it's keys running a script in another file that simply imports the environment bundle.<\\/p>\n

This pattern is much more convenient than having to duplicate your logic for one-off scripts.<\\/p>\n

You must manually start the application to make sure that the context is populated.<\\/p>\n

You must exit the process manually unless you want to have a script that is continuously alive.<\\/p>\n

The example bellow is a simple JavaScript file under scripts/seed.js<\\/strong> used for seeding the MongoDB database<\\/a> registered in the context:<\\/p>\n

// import from .production instead if you run this in production mode<\\/span>\nconst<\\/span> {<\\/span> default<\\/span>:<\\/span> context }<\\/span> =<\\/span> require<\\/span>(<\\/span>'../.development/server.js'<\\/span>)<\\/span>;<\\/span>\nconst<\\/span> Faker =<\\/span> require<\\/span>(<\\/span>'@faker-js/faker'<\\/span>)<\\/span>;<\\/span>\n\n// registers 5 random fake users<\\/span>\nasync<\\/span> function<\\/span> seed<\\/span>(<\\/span>)<\\/span> {<\\/span>\n  await<\\/span> context.<\\/span>start<\\/span>(<\\/span>)<\\/span>;<\\/span>\n  const<\\/span> {<\\/span> database,<\\/span> project }<\\/span> =<\\/span> context;<\\/span>\n  for<\\/span> (<\\/span>let<\\/span> id =<\\/span> 0<\\/span>;<\\/span> id <<\\/span> 5<\\/span>;<\\/span> id++<\\/span>)<\\/span> {<\\/span>\n    await<\\/span> database.<\\/span>collection<\\/span>(<\\/span>'users'<\\/span>)<\\/span>.<\\/span>insertOne<\\/span>(<\\/span>{<\\/span>\n      id,<\\/span>\n      username<\\/span>:<\\/span> Faker.<\\/span>name.<\\/span>firstName<\\/span>(<\\/span>)<\\/span>\n    }<\\/span>)<\\/span>;<\\/span>\n  }<\\/span>\n  console.<\\/span>log<\\/span>(<\\/span>`<\\/span>5 users seeded to <\\/span>${<\\/span>project.<\\/span>name}<\\/span><\\/span>`<\\/span><\\/span>)<\\/span>;<\\/span>\n  process.<\\/span>exit<\\/span>(<\\/span>0<\\/span>)<\\/span>;<\\/span>\n}<\\/span>\n\nseed<\\/span>(<\\/span>)<\\/span>\n<\\/code><\\/pre>\n

To run it you can simply execute it as a normal node command.<\\/p>\n

\n

🔥 You must always run it form the root in order to pick the correct .env file<\\/p>\n<\\/blockquote>\n

><\\/span> node scripts/<\\/span>seed.<\\/span>js\n5<\\/span> users seeded\n<\\/code><\\/pre>\n

Script runners are great for many things like:<\\/p>\n

    \n
  • seeding a database in specific environment<\\/li>\n
  • testing context<\\/code> behaviors<\\/li>\n
  • one-off tasks<\\/li>\n
  • cron jobs<\\/li>\n<\\/ul>\n

    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"}}}},"context":{"mode":"light","oppositeMode":"dark"}}

Links:

Nullstack https://nullstack.app/
What is Nullstack?https://nullstack.app/what-is-nullstack
Documentationhttps://nullstack.app/getting-started
Exampleshttps://nullstack.app/examples
Contributorshttps://nullstack.app/contributors
F.A.Qhttps://nullstack.app/frequently-asked-questions
Waifuhttps://nullstack.app/waifu
Portuguêshttps://nullstack.app/pt-br
🇧🇷https://nullstack.app/pt-br
https://discord.gg/eDZfKz264v
Starhttps://github.com/nullstack/nullstack
What is Nullstack?https://nullstack.app/what-is-nullstack
Getting startedhttps://nullstack.app/getting-started
Stateless Componentshttps://nullstack.app/stateless-components
Stateful componentshttps://nullstack.app/stateful-components
Full stack lifecyclehttps://nullstack.app/full-stack-lifecycle
Server functionshttps://nullstack.app/server-functions
Contexthttps://nullstack.app/context
Two-way bindingshttps://nullstack.app/two-way-bindings
JSX elementshttps://nullstack.app/jsx-elements
Refshttps://nullstack.app/refs
Styleshttps://nullstack.app/styles
Persistent Componentshttps://nullstack.app/persistent-components
Routes and paramshttps://nullstack.app/routes-and-params
Context datahttps://nullstack.app/context-data
Context instanceshttps://nullstack.app/context-instances
Context environmenthttps://nullstack.app/context-environment
Context pagehttps://nullstack.app/context-page
Context projecthttps://nullstack.app/context-project
Context settingshttps://nullstack.app/context-settings
Context secretshttps://nullstack.app/context-secrets
Server request and responsehttps://nullstack.app/server-request-and-response
Service Workerhttps://nullstack.app/service-worker
Proxyhttps://nullstack.app/proxy
Application Startuphttps://nullstack.app/application-startup
Script runnerhttps://nullstack.app/script-runner
How to customize Webpackhttps://nullstack.app/how-to-customize-webpack
Transpilation and Securityhttps://nullstack.app/transpilation-and-security
TypeScripthttps://nullstack.app/typescript
Server-side renderinghttps://nullstack.app/server-side-rendering
Static site generationhttps://nullstack.app/static-site-generation
Single page applicationshttps://nullstack.app/single-page-applications
Nullstack Logohttps://nullstack.app/nullstack-logo
How to deployhttps://nullstack.app/how-to-deploy-a-nullstack-application
MongoDB databasehttps://nullstack.app/how-to-use-mongodb-with-nullstack
Next Step https://nullstack.app/script-runner#next-step
Advanced concepts: How to customize Webpackhttps://nullstack.app/how-to-customize-webpack
Join our Discordhttps://discord.gg/eDZfKz264v
https://twitter.com/nullstackapp
https://github.com/nullstack/nullstack
https://discord.gg/eDZfKz264v
https://www.youtube.com/nullstack
https://www.linkedin.com/company/nullstack-app
Leave a star on GitHubhttps://github.com/nullstack/nullstack/stargazers

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no


URLs of crawlers that visited me.