-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Object generation based on schema input.
- Loading branch information
1 parent
73dd063
commit fb38420
Showing
4 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { createOperation, z } from '../generated/wundergraph.factory'; | ||
import Ajv from 'ajv'; | ||
import addFormats from 'ajv-formats'; | ||
|
||
const ajv = new Ajv({ | ||
schemaId: '$id', | ||
strict: false, | ||
validateSchema: false, | ||
addUsedSchema: false | ||
}); | ||
addFormats(ajv); | ||
|
||
export default createOperation.mutation({ | ||
input: z.object({ | ||
object: z.any() | ||
}), | ||
handler: async ({ input, context, operations }) => { | ||
try { | ||
console.log('Step 1: Received input object:', JSON.stringify(input.object, null, 2)); | ||
|
||
if (!input.object.$schema) { | ||
throw new Error('Object must have a $schema property'); | ||
} | ||
console.log('Step 2: Validated $schema property exists:', input.object.$schema); | ||
|
||
// Fetch the schema | ||
console.log('Step 3: Fetching schema from database...'); | ||
const { data: schemaData, error: schemaError } = await context.supabase | ||
.from('db') | ||
.select('json') | ||
.eq('json->>$id', input.object.$schema) | ||
.single(); | ||
|
||
if (schemaError) { | ||
throw new Error('Failed to fetch schema: ' + schemaError.message); | ||
} | ||
console.log('Step 4: Schema fetched successfully:', JSON.stringify(schemaData.json, null, 2)); | ||
|
||
const schema = schemaData.json; | ||
|
||
// Calculate CID for the object | ||
console.log('Step 5: Calculating CID for the object...'); | ||
const calcCIDResult = await operations.mutate({ | ||
operationName: 'calculateCID', | ||
input: { json: input.object }, | ||
}); | ||
|
||
if (!calcCIDResult.data?.success) { | ||
throw new Error('Failed to calculate CID: ' + (calcCIDResult.data?.error || 'Unknown error')); | ||
} | ||
console.log('Step 6: CID calculated successfully:', calcCIDResult.data.json.$id); | ||
|
||
const objectWithId = { | ||
...calcCIDResult.data.json, | ||
$id: calcCIDResult.data.json.$id, | ||
$schema: input.object.$schema | ||
}; | ||
console.log('Step 7: Created object with ID:', JSON.stringify(objectWithId, null, 2)); | ||
|
||
// Validate the object against the schema | ||
console.log('Step 8: Validating object against schema...'); | ||
const validate = ajv.compile(schema); | ||
const valid = validate(objectWithId); | ||
|
||
if (!valid) { | ||
console.error('Validation errors:', ajv.errorsText(validate.errors)); | ||
throw new Error('Validation error: ' + ajv.errorsText(validate.errors)); | ||
} | ||
console.log('Step 9: Object validated successfully against schema'); | ||
|
||
// Insert the object into the database | ||
console.log('Step 10: Inserting object into database...'); | ||
const { data, error } = await context.supabase | ||
.from('db') | ||
.insert({ json: objectWithId }) | ||
.select(); | ||
|
||
if (error) { | ||
throw new Error('Database insert error: ' + error.message); | ||
} | ||
console.log('Step 11: Object inserted successfully:', JSON.stringify(data[0], null, 2)); | ||
|
||
return { | ||
success: true, | ||
insertedData: data[0] | ||
}; | ||
} catch (error) { | ||
console.error('Unexpected error in insertObject:', error); | ||
return { | ||
success: false, | ||
error: 'Unexpected error', | ||
details: error.message | ||
}; | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,10 @@ | |
operationName: 'insertDB' | ||
}); | ||
const insertObjectMutation = createMutation({ | ||
operationName: 'insertObject' | ||
}); | ||
function sortByTimestamp(a, b) { | ||
return new Date(b.json.timestamp).getTime() - new Date(a.json.timestamp).getTime(); | ||
} | ||
|
@@ -94,13 +98,61 @@ | |
console.log('HominioDB: Selected item changed, resetting expandedProperties'); | ||
expandedProperties = []; | ||
} | ||
async function insertHardcodedObject() { | ||
message = { text: '', type: '' }; | ||
const hardcodedObject = { | ||
$schema: 'https://alpha.ipfs.homin.io/QmWqLHcJsrMpmZrbxfMA3oM2YVbHjJfGw94UbZgBwm1mYR', | ||
$id: '', // This will be filled by the server | ||
prev: null, | ||
type: 'object', | ||
title: 'John Doe User Object', | ||
author: 'HominioDB Client', | ||
version: 1, | ||
description: 'A sample user object for John Doe', | ||
properties: { | ||
username: 'john_doe', | ||
email: '[email protected]', | ||
password: 'hashedpassword123', | ||
profile: { | ||
fullName: 'John Doe', | ||
birthDate: '1990-01-01', | ||
bio: "I'm a software developer who loves coding and outdoor activities.", | ||
location: { | ||
country: 'United States', | ||
city: 'San Francisco' | ||
} | ||
}, | ||
settings: { | ||
theme: 'dark', | ||
notifications: true, | ||
language: 'en' | ||
} | ||
} | ||
}; | ||
const result = await $insertObjectMutation.mutateAsync({ | ||
object: hardcodedObject | ||
}); | ||
if (result.success) { | ||
message = { text: 'Hardcoded object inserted successfully!', type: 'success' }; | ||
await $query.refetch(); | ||
} else { | ||
message = { text: `Failed: ${result.details}`, type: 'error' }; | ||
console.error('Failed:', result.details); | ||
} | ||
} | ||
</script> | ||
|
||
<div class="flex h-full text-gray-900 bg-tertiary-100 dark:bg-surface-800 dark:text-white"> | ||
<!-- Left side: List view --> | ||
<div class="w-[300px] p-4 overflow-y-auto border-r border-surface-300-600-token"> | ||
<button on:click={generateRandomObject} class="mb-4 btn variant-filled-primary"> | ||
Generate Random Object | ||
Generate Schema | ||
</button> | ||
<button on:click={insertHardcodedObject} class="mb-4 ml-2 btn variant-filled-secondary"> | ||
Insert Object | ||
</button> | ||
|
||
{#if message.text} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters