Let’s move into another interesting sections, the database where we store information processed. In this post we are store data into local SQLite database/
Denodb-ORM
This is a third party ORM module for deno which will help us to connect MySQL, MariaDB, SQLite and Postgres databases. The ORM module work through Model, so we can perform operations using Model, no worry about confusing, queries and statements.
APP
Our app is a simple Todo application, which store Todo’s in a MySQL database.
- create a configuration
- Create a SQLite file
- Create Models
- Link Model and Sync
Configuration
Usually we kept the database file under a config folder, the name of our file will database.ts and content s follows. In the final steps of the configuration we export the model.
import { Model, Database, SQLite3Connector, DataTypes } from "../deps.ts";
const connector = new SQLite3Connector({
filepath: './database.sqlite',
});
const db = new Database(connector)
// NOTE Models
class Todo extends Model {
static table = 'todos';
static fields = {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
},
item: {
type: DataTypes.STRING,
}
,
description: {
type: DataTypes.STRING,
}
};
}
// NOTE Linking Model with DB
db.link([Todo])
await db.sync()
export default Todo;
Sync () – will create the tables for you. You have to create the database which in not created by the sync.