Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Needs/data #68

Merged
merged 13 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 37 additions & 81 deletions src/lib/db/DatabaseManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,19 @@ import { addRxPlugin } from "rxdb";
import { RxDBDevModePlugin } from "rxdb/plugins/dev-mode";
import { getRxStorageDexie } from "rxdb/plugins/storage-dexie";
import { createRxDatabase, RxDatabase } from "rxdb";
import { v4 as uuidv4 } from "uuid";

import toolkitItemSchema from "./schemas/toolkitItemSchema.json";
import moodRecordSchema from "./schemas/moodRecordSchema.json";
import categoriesSchema from "./schemas/categoriesSchema.json";
import { v4 as uuidv4 } from "uuid";
import needsCategoriesSchema from "./schemas/categoriesSchema.json";
import needsSchema from "./schemas/categoriesSchema.json";
import nextActionsSchema from "./schemas/categoriesSchema.json";

addRxPlugin(RxDBDevModePlugin);
import { categories, toolkit } from "./seed/toolkit";
import { needsCategories, needs, nextActions } from "./seed/needs";

const seedData = {
categories: [
{ id: uuidv4(), name: "Replace", timestamp: new Date().toISOString() },
{ id: uuidv4(), name: "Barrier", timestamp: new Date().toISOString() },
{ id: uuidv4(), name: "Distract", timestamp: new Date().toISOString() },
{
id: uuidv4(),
name: "Change Status",
timestamp: new Date().toISOString(),
},
],
toolkit: [
{
id: uuidv4(),
name: "Listen to my favourite music",
categories: ["Replace", "Barrier"],
checked: false,
infoUrl: "https://google.com/music",
imageUrl:
"https://daily.jstor.org/wp-content/uploads/2023/01/good_times_with_bad_music_1050x700.jpg",
timestamp: new Date().toISOString(),
},
{
id: uuidv4(),
name: "Breathing exercises",
categories: ["Distract"],
checked: false,
infoUrl: "https://www.youtube.com/watch?v=DbDoBzGY3vo",
imageUrl:
"https://www.bhf.org.uk/-/media/images/information-support/heart-matters/2023/december/wellbeing/deep-breathing-620x400.png?h=400&w=620&rev=4506ebd34dab4476b56c225b6ff3ad60&hash=B3CFFEEE704E4432D101432CEE8B2766",
timestamp: new Date().toISOString(),
},
{
id: uuidv4(),
name: "Call a friend",
categories: ["Distract", "Change status"],
checked: false,
infoUrl: "https://example.com/call",
imageUrl:
"https://t4.ftcdn.net/jpg/04/63/63/59/360_F_463635935_IweuYhCqZRtHp3SLguQL8svOVroVXvvZ.jpg",
timestamp: new Date().toISOString(),
},
{
id: uuidv4(),
name: "Drink water",
categories: ["Distract", "Change status"],
checked: false,
infoUrl: "https://example.com/call",
imageUrl:
"https://content.health.harvard.edu/wp-content/uploads/2023/07/b8a1309a-ba53-48c7-bca3-9c36aab2338a.jpg",
timestamp: new Date().toISOString(),
},
],
};
addRxPlugin(RxDBDevModePlugin);

let dbInstance: RxDatabase | null = null;

Expand All @@ -87,7 +39,14 @@ class DatabaseManager {
ignoreDuplicate: true,
});

const requiredCollections = ["categories", "mood_records", "toolkit_items"];
const requiredCollections = [
"categories",
"mood_records",
"toolkit_items",
"needs_categories",
"needs",
"next_actions",
];
const existingCollections = Object.keys(dbInstance.collections);

for (const collection of requiredCollections) {
Expand All @@ -99,7 +58,7 @@ class DatabaseManager {
}
}

console.log("Database initialized.");
console.log("Database initialised.");
await this.seedDatabase();
return dbInstance;
}
Expand All @@ -109,42 +68,33 @@ class DatabaseManager {
dbInstance = await this.createDatabase();
}
if (!dbInstance) {
throw new Error("Failed to initialize the database.");
throw new Error("Failed to initialise the database.");
}
return dbInstance;
}

private async seedDatabase() {
console.log("Seeding database...");
try {
await this.seedCategories();
await this.seedToolkitItems();
await this.seed("categories", categories);
await this.seed("toolkit_items", toolkit);
await this.seed("needs_categories", needsCategories);
await this.seed("needs", needs);
await this.seed("next_actions", nextActions);
} catch (error) {
console.error("Error during database seeding:", error);
}
}

private async seedCategories() {
if (!dbInstance) throw new Error("Database not initialized.");
if (!dbInstance.collections.categories) {
throw new Error("Categories collection is missing.");
}
const existingDocs = await dbInstance.categories.find().exec();
if (existingDocs.length === 0) {
console.log("Seeding categories...");
await dbInstance.categories.bulkInsert(seedData.categories);
}
}
private async seed<T>(collectionName: string, data: T[]) {
if (!dbInstance) throw new Error("Database not initialised.");
const collection = dbInstance.collections[collectionName];
if (!collection)
throw new Error(`${collectionName} collection is missing.`);

private async seedToolkitItems() {
if (!dbInstance) throw new Error("Database not initialized.");
if (!dbInstance.collections.toolkit_items) {
throw new Error("Toolkit items collection is missing.");
}
const existingDocs = await dbInstance.toolkit_items.find().exec();
const existingDocs = await collection.find().exec();
if (existingDocs.length === 0) {
console.log("Seeding toolkit items...");
await dbInstance.toolkit_items.bulkInsert(seedData.toolkit);
console.log(`Seeding ${collectionName}...`);
await collection.bulkInsert(data);
}
}

Expand All @@ -156,6 +106,12 @@ class DatabaseManager {
return { schema: moodRecordSchema };
case "toolkit_items":
return { schema: toolkitItemSchema };
case "needs_categories":
return { schema: needsCategoriesSchema };
case "needs":
return { schema: needsSchema };
case "next_actions":
return { schema: nextActionsSchema };
default:
throw new Error(`Unknown collection: ${collectionName}`);
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/db/schemas/categoriesSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
"maxLength": 100
},
"name": {
"type": "string"
"type": "string",
"maxLength": 255
},
"timestamp": {
"type": "string",
"format": "date-time"
}
},
"required": ["id", "name", "timestamp"]
}
}
20 changes: 20 additions & 0 deletions src/lib/db/schemas/needsCategoriesSchema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": 0,
"primaryKey": "id",
"type": "object",
"properties": {
"id": {
"type": "string",
"maxLength": 100
},
"name": {
"type": "string",
"maxLength": 100
},
"timestamp": {
"type": "string",
"format": "date-time"
}
},
"required": ["id", "name", "timestamp"]
}
43 changes: 43 additions & 0 deletions src/lib/db/schemas/needsSchema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"version": 0,
"primaryKey": "id",
"type": "object",
"properties": {
"id": {
"type": "string",
"maxLength": 100
},
"name": {
"type": "string",
"maxLength": 100
},
"selectedTimestamps": {
"type": "array",
"items": {
"type": "string",
"format": "date-time"
}
},
"selectedExpiry": {
"type": "string",
"format": "date-time"
},
"category": {
"type": "string",
"ref": "needs_categories",
"maxLength": 100
},
"timestamp": {
"type": "string",
"format": "date-time"
}
},
"required": [
"id",
"name",
"category",
"timestamp",
"selectedTimestamps",
"selectedExpiry"
]
}
43 changes: 43 additions & 0 deletions src/lib/db/schemas/nextActionsSchema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"version": 0,
"primaryKey": "id",
"type": "object",
"properties": {
"id": {
"type": "string",
"maxLength": 100
},
"name": {
"type": "string",
"maxLength": 255
},
"need": {
"type": "string",
"ref": "needs",
"maxLength": 100
},
"selectedTimestamps": {
"type": "array",
"items": {
"type": "string",
"format": "date-time"
}
},
"selectedExpiry": {
"type": "string",
"format": "date-time"
},
"timestamp": {
"type": "string",
"format": "date-time"
}
},
"required": [
"id",
"name",
"need",
"timestamp",
"selectedTimestamps",
"selectedExpiry"
]
}
15 changes: 6 additions & 9 deletions src/lib/db/schemas/toolkitItemSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"type": "string",
"maxLength": 100
},
"name": { "type": "string" },
"name": {
"type": "string",
"maxLength": 255
},
"categories": {
"type": "array",
"items": {
Expand All @@ -24,11 +27,5 @@
"format": "date-time"
}
},
"required": [
"id",
"name",
"categories",
"checked",
"timestamp"
]
}
"required": ["id", "name", "categories", "checked", "timestamp"]
}
Loading
Loading