Deco records
Deco Records is a production-ready SQLite database that is easy to install and located close to the user.
Installing Deco Records on Your Site
To start the installation process, create a new environment in your site’s Deco admin panel, or reset the current environment, as a publish will be done at the end of this step.
Follow the steps below to install it on your site or watch the video.
- Log in to the Deco admin panel of your site.
- In the sidebar, click on the “Records” menu.
- Then click on
Setup Deco Records
and wait for the app installation and database creation process. During this step, several files will be created and edited on your site (deno.json, .gitignore, manifest.gen.ts, apps/deco/records.ts, drizzle.config.ts, db/schema.ts, .deco/blocks/deco-records.json). - After the installation, click on
Show diff and publish
to publish the app installation and database creation. - Review the changed files, edit the description, and finally, click on
Publish now
.
After the publishing process is complete, you will see your database when accessing the “Records” menu.
Creating Tables
You will need the files that were created during the installation of deco records on your computer. If necessary, perform a git pull from your remote project.
Follow the steps below to create new tables in your database or watch the video.
This process will use drizzle-orm and
drizzle-kit to create and manage tables in your
database through
schema migrations.
In the following example, a table named profiles
will be created with the
columns: id
, name
, and email
.
1. Edit the db/schema.ts
file to create tables.
2. Go to your site’s admin, click on the Settings
menu, then in
the Database credentials section, click Generate now
. Finally, click the icon
to copy the credentials.
3. Add the credentials to your computer’s operating system
environment variables.
4. Run the deno task db:setup:deps
in your terminal to install
the necessary dependencies to perform the schema migration. You need Deno
version 1.43.0 or higher and use the environment variable DENO_FUTURE=1
to
enable the installation of npm modules.
5. Run the deno task db:schema:update
to create the SQL files
responsible for the schema migration and apply them to the database. Run this
command whenever you make changes to your tables to generate new schema
migrations.
6. In the records menu of your site, in the deco admin, you will
see the profiles
and __drizzle__migrations
tables. The drizzle__migrations
table is auto-generated and used by drizzle-kit to manage schema migrations.
Add the auto-generated files to a git commit and push them to the remote git repository.
Reading and Writing Data
With the profiles
table created, we can now create a
section to manage profiles, where we
can list, remove, and create a profile. Create a section that will be the
profile manager.
In the previous example, the inline loader uses the drizzle
client provided by
the records app to query the database, insert, and remove profiles.
Developing Locally
To develop locally, you need to have the database access credentials, which can
be created in your site’s Deco admin panel. After adding the environment
variables provided by the admin panel, run the Deno task db:pull:prod
to dump
your database and then insert it locally into the sqlite.db
file.
To access the Deco Records database during development, you need to have the
credentials in the environment variables, which can be created in the Deco admin
panel. In addition to the credentials, you need a new environment variable
called USE_PRODUCTION_DB
with the value 1
.
Reference Links
Was this page helpful?