Skip to content

Commit

Permalink
fix(db): bug in creating default records due to wrong check
Browse files Browse the repository at this point in the history
  • Loading branch information
rishikanthc committed Oct 19, 2024
1 parent ebbcf06 commit 4254e2c
Showing 1 changed file with 47 additions and 16 deletions.
63 changes: 47 additions & 16 deletions src/lib/fileFuncs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { env } from '$env/dynamic/private';

export async function ensureCollectionExists(pb) {
// const pb = new PocketBase('http://localhost:8090');
try {
Expand All @@ -7,6 +8,7 @@ export async function ensureCollectionExists(pb) {
console.error(`Unable to login to db: ${error.message}`, {});
}

// Ensure "scribo" collection exists
try {
const collections = await pb.collections.getList(1, 50, { filter: `name='scribo'` });
if (collections.items.length === 0) {
Expand Down Expand Up @@ -51,10 +53,13 @@ export async function ensureCollectionExists(pb) {
console.error(`Failed to check or create collection: ${error.message}`, {});
}

// Check if the "settings" collection exists
try {
const collections = await pb.collections.getList();
const settingsCollection = collections.items.find((col) => col.name === 'settings');

if (!settingsCollection) {
// Create "settings" collection if it doesn't exist
await pb.collections.create({
name: 'settings',
schema: [
Expand All @@ -68,7 +73,13 @@ export async function ensureCollectionExists(pb) {
{ name: 'wizard', type: 'bool'}
]
});
console.log('Settings collection created.');
}

// Check if the "settings" record exists
const settingsRecords = await pb.collection('settings').getList(1, 1);
if (settingsRecords.items.length === 0) {
// Create default "settings" record if it doesn't exist
await pb.collection('settings').create({
model: 'tiny',
openai: '',
Expand All @@ -79,33 +90,53 @@ export async function ensureCollectionExists(pb) {
diarize: false,
wizard: false
});

console.log('Settings collection created.');
console.log('Default settings record created.');
} else {
console.log('Settings record already exists.');
}
} catch (error) {
console.error('Error ensuring collection exists:', error);
console.error('Error ensuring settings collection or record exists:', error);
}

// Check if the "templates" collection exists
try {
const collections = await pb.collections.getList();
const templateCollection = collections.items.find((col) => col.name === 'templates');
if (!templateCollection) {
await pb.collections.create({
name: 'templates',
schema: [
{ name: 'title', type: 'text' },
{ name: 'prompt', type: 'text' }
]
});
const collections = await pb.collections.getList();
const templateCollection = collections.items.find((col) => col.name === 'templates');

console.log('Template collection created.');
if (!templateCollection) {
// Create "templates" collection if it doesn't exist
await pb.collections.create({
name: 'templates',
schema: [
{ name: 'title', type: 'text' },
{ name: 'prompt', type: 'text' }
]
});
console.log('Template collection created.');

// Create a default template record
await pb.collection('templates').create({
title: 'Default template',
prompt: 'Provide a concise and comprehensive summary for the transcript.'
});
console.log('Default template record created.');
} else {
console.log('Templates collection already exists.');

// Check if the default template record exists
const templateRecords = await pb.collection('templates').getList(1, 50, { filter: `title='Default template'` });
if (templateRecords.items.length === 0) {
// Create default template record if it doesn't exist
await pb.collection('templates').create({
title: 'Default template',
prompt: 'Provide a concise and comprehensive summary for the transcript.'
});
console.log('Default template record created.');
} else {
console.log('Default template record already exists.');
}
} catch (error) {
console.error('Error ensuring collection exists:', error);
}
} catch (error) {
console.error('Error ensuring templates collection exists:', error);
}
}

0 comments on commit 4254e2c

Please sign in to comment.