Source control 

Each Gadget application's definition can be fully captured in text files which are great for version controlling using Git!

Use Gadget's CLI ggt which allows you to clone your app's files into a local directory and manage any changes between your local directory and the Gadget editor (in real time!).

Using git with Gadget 

Cloning your code to a local directory allows you to use source control tools like Git to track changes to your code and collaborate with others, as well as all of the standard Github ecosystem, like Github Actions for CI and CD.

ggt syncs files to and from a local filesystem, but your Gadget app still runs in the cloud. This means you get all the benefits of your local development setup, like your editor with your plugins and configuration, as well as your command line tools for things like linting and formatting. You also get all the benefits of Gadget's managed development and production environments where you don't need to manage any infrastructure, software versions, containers, etc.

Changes made locally are immediately reflected in Gadget with ggt dev, so you can code as you might normally, and still use all of Gadget's web-based tools for building. The editor, API playground, and your application's API will all live-update to reflect your changes.

Setup source control with ggt 

To use source control for your app, you'll need to have a Gadget account, a Gadget app, and a local terminal for running commands.

  1. To get started, run the following command into your local terminal (“test-app” should be replaced with the name of your Gadget app in the command).
terminal
npx ggt@latest dev ./test-app --app=test-app

This command will download ggt, Gadget's CLI tool, and begin a sync session that two-way syncs between your local copy and Gadget's cloud infrastructure.

  1. Change directories into your app's directory, and notice all your apps' files:
terminal
cd ./test-app ls
  1. Create a repository on Github, and initialize the git repository locally within your app directory:
terminal
git init
  1. From Github grab the remote url for your Github repository and add it to your Gadget app git repo.
terminal
git remote add origin https://github.com/ehmerasim/your-app.git
  1. Commit all your locally synced Gadget files to the repository.
terminal
git commit -m "adding synced gadget files"
  1. Push your commit to Github.
terminal
git push -u origin main

Voila, you've now successfully pushed your Gadget code to be managed by source control!

Git/Github workflow (PR changes) - example 

  1. Create a new Gadget app and set up your Gadget app on GitHub using the above steps. Head back to Github and ensure your repository contains your pushed Gadget files.

  2. Create and checkout to a new branch within your local directory.

terminal
git checkout -b add-shopify-connection
  1. Head back to Gadget and under the Settings page navigate to Plugins and click on the Shopify connection to add it.
navigate to settings and add the Shopify connection to source controlled app
  1. Now follow the steps to add the Shopify connection to your Gadget app. If you need a refresher check out our connection quickstart.

  2. Once your Gadget app is now successfully connected head back to your local Gadget files and notice the instant changes within the model files added for your Shopify models. Also, head to your settings.gadget.ts file and you'll now notice the meta information representing you've successfully connected to Shopify.

Shopify connection in the meta-data file for settings
settings.gadget.ts
TypeScript
export const settings: GadgetSettings = { type: "gadget/settings/v1", plugins: { connections: { shopify: { apiVersion: "2024-01", enabledModels: ["shopifyProduct", "shopifyProductImage"], type: "partner", scopes: ["read_products"], }, }, authentications: { settings: { redirectOnSignIn: "/signed-in", signInPath: "/sign-in", unauthorizedUserRedirect: "signInPath", defaultSignedInRoles: ["signed-in"], }, methods: { emailPassword: true, googleOAuth: { scopes: ["email", "profile"], offlineAccess: false }, }, }, }, };
  1. Now commit and push the new file changes from the addition of the Shopify connection to the branch.
terminal
git commit -m "adds shopify files"
  1. Head back over to GitHub and you'll notice the commit changes.
Commit changes to GitHub in source controlled app
  1. Switch back to your main branch and head to the settings.gadget.ts file and now you'll notice the Shopify connection is removed.
settings.gadget.ts
TypeScript
export const settings: GadgetSettings = { type: "gadget/settings/v1", plugins: { authentications: { settings: { redirectOnSignIn: "/signed-in", signInPath: "/sign-in", unauthorizedUserRedirect: "signInPath", defaultSignedInRoles: ["signed-in"], }, methods: { emailPassword: true, googleOAuth: { scopes: ["email", "profile"], offlineAccess: false }, }, }, }, };
  1. Back in Github, create a pull request based on your commit and merge it into the main branch.
Merge committed changes in GitHub PR for source controlled app

Branching within environments workflow - example 

Continuing from the walkthrough example above, let's take a look at branching through different environments.

When creating multiple branches within different environments always ensure you are synced up to the appropriate environment

  1. Stop ggt running on your current environment by hitting CTRL + C.

  2. Head back to Gadget and create a new development environment.

Create new environment for branching into
  1. Back in your local directory run ggt again but this time pass the environment flag specifying the name of the newly created environment so ggt doesn't automatically sync back up with the previous environment.
terminal
ggt dev --env=testing
  1. And now that my local is set up to my testing environment I can go ahead and create a new branch as I normally would.
terminal
git checkout -b test-branch

Deploying changes to production 

When you're ready to deploy any changes to production:

  1. Double-check your development environment: Before you initiate the deployment, make sure your local filesystem is synchronized with your Gadget development environment..

  2. Pull any changes from merged PR's: After your PR is merged, switch to the main branch in your codebase and pull your changes.

  3. Choosing your deployment method:

  • Gadget interface

  • Terminal (using ggt): Alternatively, you can use ggt to deploy your application. ggt deploy will execute a deploy from where ever you run it. ggt will check if you are in the correct environment and confirm that your local and Gadget filesystems are synchronized. If there are any discrepancies or conflicts, ggt will notify you before proceeding with the deployment.

GGT source control reference commands. 

terminal
USAGE ggt [COMMAND] COMMANDS sync Clone your local filesystem with your environment's filesystem status Show the status of your local and environment's filesystem push Push your local filesystem pull Pull your environment's filesystem deploy Deploy your environment to production list List your available applications login Log in to your account logout Log out of your account whoami Print the currently logged in account version Print this version of ggt FLAGS -h, --help Print how to use the command -v, --verbose Print more verbose output --json Print all output as newline-delimited JSON Run "ggt [COMMAND] -h" for more information about a specific command. ggt status -h Show changes made to your local filesystem and your environment's filesystem. Changes will be calculated from the last time you ran "ggt dev", "ggt push", or "ggt pull" in the chosen directory. ggt push -h Push changes from your local filesystem to your environment's filesystem. Changes will be calculated from the last time you ran "ggt dev", "ggt push", or "ggt pull" on your local filesystem. USAGE ggt push EXAMPLES $ ggt push $ ggt push --force $ ggt push --force --env=staging $ ggt push --force --env=staging --allow-unknown-directory FLAGS -a, --app=<name> The application to push files to -e, --env=<name> The environment to push files to --force Discard un-synchronized environment changes Run "ggt push --help" for more information. ggt pull -h Pull changes from your environment's filesystem to your local filesystem. Changes will be calculated from the last time you ran "ggt dev", "ggt push", or "ggt pull" on your local filesystem. USAGE ggt pull EXAMPLES $ ggt pull $ ggt pull --force $ ggt pull --force --env=staging $ ggt pull --force --env=staging --app=example --allow-unknown-directory FLAGS -a, --app=<name> The application to pull files from -e, --env=<name> The environment to pull files from --force Discard un-synchronized local changes Run "ggt pull --help" for more information. ggt deploy Deploy your development environment to production. Your local filesystem must be in sync with your development environment before you can deploy. Changes will be calculated from the last time you ran "ggt dev", "ggt push", or "ggt pull" on your local filesystem. USAGE ggt deploy EXAMPLES $ ggt deploy $ ggt deploy --from=staging $ ggt deploy --from=staging --force $ ggt deploy --from=staging --force --allow-problems FLAGS -a, --app=<name> The application to deploy -e, --from=<env> The environment to deploy from --force Discard un-synchronized environment changes Run "ggt deploy --help" for more information.

Working with Gadget metadata files 

How Gadget metadata files work 

When you clone your Gadget app using ggt you'll notice a few new metadata files found in your local directory which contain key reference information about Gadget app's models, settings, and permissions.

  • Each model in your app will have a schema.gadget.ts file describing the database for that model
  • Each app will have a root-level settings.gadget.ts describing the app's settings
  • Each app will have an accessControl/permissions.gadget.ts file describing the app's roles and permissions

These metadata files correspond exactly to Gadget's web interface, and changes made in one will be reflected in the other when using ggt dev.

An example app's filesystem looks like this locally:

text
api/ // the backend folder powering your app's api models/ foo/ schema.gadget.ts // the metadata for the foo model actions/ create.js update.js delete.js bar/ schema.gadget.ts // the metadata for the bar model actions/ create.js update.js delete.js actions/ globalActionA.js globalActionB.js web/ // the frontend folder powering your app's experience pages/ index.js about.js components/ header.js footer.js accessControl/ permissions.gadget.ts // the metadata for the app's roles and permissions settings.gadget.ts // the metadata for the app's settings

Model storageKeys 

The storageKey property in metadata files identifies the actual data on disk for your model or field in Gadget's underlying database. When you rename a model or a field, Gadget uses the storage key to continue to serve the data from the old name. Without this storageKey, Gadget wouldn't know which field got renamed when, so storageKey is a required property on all models and fields.

Changing the storage key for a model or a field will reset the data for that model or field immediately! It is strongly advised to not edit, rename, or remove any storageKey within the metadata files as doing so can result in your app breaking.

Data stored under a particular storageKey in the Gadget database will remain stored under that key for at least 7 days after it stops being used. If you delete a model (and thus delete it's storageKey), but then revert and re-create the model with the same storageKey, the data from before you deleted the model will still be stored and will be found when you make API calls for the model.

Invalid metadata - risks of manual modification 

It is highly recommended that if you need to make changes to your model schema, connections, or roles and permissions you do not directly do so by editing the metadata file, any changes should be configured within Gadget.

Adding objects and properties directly to the metadata file in most instances will be invalid and your app will not work as expected, in most cases this is due to the unique storageKey's Gadget generates for certain objects.

Removing properties from the file can work at times but is also highly risky and should proceed with caution if doing so.

Fatal errors 

Fatal errors in Gadget refer to critical issues that arise due to incorrect, corrupted, or incompatible modifications in the metadata or folder structure within your local directory, leading to a complete failure of your app. These errors are deemed "fatal" because they prevent the proper functioning of how your Gadget app works. If you run into any fatal error head over to Gadget and within your app you'll notice you won't have access to edit anything and are presented with a list of errors that you must resolve locally in order for your app to continue functioning properly.

schema.gadget.ts 

schema.gadget.ts
TypeScript
import type { GadgetModel } from "gadget-server"; export const schema: GadgetModel = { type: "gadget/model-schema/v1", storageKey: "DataModel-bbbb123qwerty", fields: { fieldTwo: { type: "string", storageKey: "bbbb", }, fieldThree: { type: "richText", storageKey: "bbbb", }, fieldFour: { type: "boolean", default: true, storageKey: "bbbb", }, fieldFive: { type: "enum", options: ["yo", "dawg"], acceptMultipleSelections: true, acceptUnlistedOptions: true, storageKey: "bbbb", }, fieldSix: { type: "dateTime", includeTime: true, default: "2023-10-15T23:19:55Z", storageKey: "bbbb", }, fieldSeven: { type: "file", allowPublicAccess: true, storageKey: "bbbb", }, fieldEight: { type: "email", storageKey: "bbbb", validations: { email: true, }, }, fieldNine: { type: "json", default: [ { title: "The Great Gatsby", author: "F. Scott Fitzgerald", year: 1925, }, ], storageKey: "bbbb", }, fieldTen: { type: "computed", sourceFile: "models/modelA/file.gelly", storageKey: "bbbb", }, fieldEleven: { type: "belongsTo", parent: { model: "foo", }, storageKey: "bbbb", }, fieldTwelve: { type: "hasMany", child: { model: "foo", belongsToField: "bar", }, storageKey: "bbbb", }, fieldThirteen: { type: "hasManyThrough", sibling: { model: "student", relatedField: "course", }, join: { model: "registration", belongsToSelfField: "student", belongsToSiblingField: "course", }, storageKey: "bbbb", }, fieldFifteen: { type: "roleList", default: ["signedIn"], storageKey: "bbbb", }, fieldSixteen: { type: "password", validations: { strongPassword: true, }, storageKey: "bbbb", }, }, };

settings.gadget.ts 

settings.gadget.ts
TypeScript
import type { GadgetSettings } from "gadget-server"; export const settings: GadgetSettings = { type: "gadget/settings/v1", frameworkVersion: "0.2", plugins: { connections: { sentry: true, openai: true, shopify: { type: "admin", apiVersion: "2023-07", }, shopify: { type: "partner", scopes: ["read_products", "write_products"], apiVersion: "2023-07", }, }, authentications: { settings: { signInPath: "/sign-in", redirectOnSignIn: "/signed-in", unauthorizedUserRedirect: "signInPath", defaultSignedInRoles: ["signed-in"], }, emailPassword: true, googleOAuth: { scopes: [ "https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email", "openid", ], offlineAccess: true, }, }, }, };

permissions.gadget.ts 

permissions.gadget.ts
TypeScript
import type { GadgetAccessControl } from "gadget-server"; // This file describes your access control permissions // For more information on this file http://docs.gadget.dev export const accessControl: GadgetAccessControl = { type: "gadget/access-control/v1", roles: { unauthenticated: { storageKey: "unauthenticated", }, "custom role": { storageKey: "Role-abc123", default: { read: true, action: true, }, models: { modelA: { read: { filter: "models/foo/tenancy.gelly" }, actions: { create: true, update: true, delete: true, }, }, }, actions: { globalActionA: true, }, }, }, };

Was this page helpful?