Section 5: Constructing a Mock Database
This section guides you through setting up a mock database, which utilizes your local file system to store data as JSON. This approach is designed to streamline the initial development phase by providing a clear and editable data structure.
Note
Keep in mind that while this mock file system database is great for learning and prototyping, it is not intended for use in production environments.
1: Defining the Database Structure
We'll need our database to manage several types of data:
- Backend and frontend tokens
- Template registrations
- Template visibility states
- User-uploaded media
- User projects
- Project order statuses
- Tasks (referred to as orders) processed by GraFx
The database will be represented as a JSON file, structured according to the following TypeScript interface:
TypeScript Definition
TypeScript Defintion
type Token = {
value: string,
expires: number
}
type Template = {
id: string,
name: string,
}
type Project = {
id: string,
name: string,
}
type User = {
id: string,
admin: boolean,
projects: Map<string, Project>,
media: string[]
}
type Order = {
projectID: string,
taskID:string,
downloadURL?:string,
error?:boolean,
age?: number
}
type Database = {
tokens: {
readonlyToken: Token,
dangerousToken: Token
},
storeTemplates: Map<string,Template>,
users: Map<string,User>,
orders: Map<string,Order>
}
2: Creating the Database Code
Start by creating a new file named database.js
in your project directory.
Instead of building the code incrementally, you'll find the complete code snippet below. Copy this code and paste it into your newly created database.js
file. Utilize the "Copy to clipboard" feature available at the top of each code block for convenience.
database.js content
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
The supplied JavaScript sets up a mock database, storing data in a file named database.json
. This technique reduces the complexity associated with database management, allowing you to focus on handling API interactions.
This file exports a single function, initDB()
, which initializes the database if it doesn't exist and provides helper functions for data manipulation.
As mentioned previous, the database is actually a JSOB file. Every time a change is made to the data, the internal updateDB()
function is called. This function writes the current state of the database back to database.json
. This ensures that changes are not lost when the server is restarted.
Reading from and writing to the filesystem is handled by Node.js's fs
module.
- Reading: When the database file is read, a custom reviver function transforms certain objects into JavaScript Map objects. This function checks if an object has a dataType property set to "Map" and, if so, converts it to an actual Map object.
- Writing: Conversely, when updating the database, a replacer function transforms Map objects back into a storable format that includes the dataType property, allowing for a reversible transformation. Database Operations
Database Operations
The mock database allows you to:
- Add or remove templates
- Retrieve all templates
- Update template visibility
- Manage user projects
- Handle orders
For instance, to add a project to a user's account:
To modify the order count for a user's project: