-
Notifications
You must be signed in to change notification settings - Fork 31
feat: exportToJson and createFromJson #393
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
Open
dudanogueira
wants to merge
4
commits into
weaviate:main
Choose a base branch
from
dudanogueira:export-create-from-json
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
223c2fc
implements exportToJson and createFromJson
dudanogueira bc64ed6
fix test by deleting the created collections separately
dudanogueira 8d36666
remove integration tests and keep only unit tests.
dudanogueira 44131ad
make mock test runnable.
dudanogueira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,338 @@ | ||
| import express from 'express'; | ||
| import { Server as HttpServer } from 'http'; | ||
| import { createServer, Server as GrpcServer } from 'nice-grpc'; | ||
| import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import weaviate, { WeaviateClient } from '../../src/index.js'; | ||
| import { | ||
| HealthCheckRequest, | ||
| HealthCheckResponse, | ||
| HealthCheckResponse_ServingStatus, | ||
| HealthDefinition, | ||
| HealthServiceImplementation, | ||
| } from '../../src/proto/google/health/v1/health.js'; | ||
| import { WeaviateClass } from '../../src/v2/index.js'; | ||
|
|
||
| // Mock schema data | ||
| const mockExportedSchema: WeaviateClass = { | ||
| class: 'TestCollection', | ||
| description: 'A test collection for JSON export', | ||
| properties: [ | ||
| { | ||
| name: 'title', | ||
| dataType: ['text'], | ||
| }, | ||
| { | ||
| name: 'content', | ||
| dataType: ['text'], | ||
| }, | ||
| { | ||
| name: 'publishDate', | ||
| dataType: ['date'], | ||
| }, | ||
| ], | ||
| vectorConfig: { | ||
| default: { | ||
| vectorIndexType: 'hnsw', | ||
| vectorizer: { | ||
| 'text2vec-contextionary': { | ||
| vectorizeClassName: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const mockComplexSchema: WeaviateClass = { | ||
| class: 'ComplexCollection', | ||
| description: 'Complex collection with nested properties', | ||
| properties: [ | ||
| { | ||
| name: 'metadata', | ||
| dataType: ['object'], | ||
| nestedProperties: [ | ||
| { | ||
| name: 'author', | ||
| dataType: ['text'], | ||
| }, | ||
| { | ||
| name: 'tags', | ||
| dataType: ['text[]'], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'score', | ||
| dataType: ['number'], | ||
| }, | ||
| ], | ||
| invertedIndexConfig: { | ||
| indexTimestamps: true, | ||
| indexPropertyLength: true, | ||
| }, | ||
| }; | ||
|
|
||
| const mockNamedVectorSchema: WeaviateClass = { | ||
| class: 'NamedVectorCollection', | ||
| properties: [ | ||
| { | ||
| name: 'content', | ||
| dataType: ['text'], | ||
| }, | ||
| ], | ||
| vectorConfig: { | ||
| custom_vector: { | ||
| vectorIndexType: 'hnsw', | ||
| vectorizer: { | ||
| none: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const makeRestApp = (version: string, createdSchemas: Map<string, WeaviateClass>) => { | ||
| const httpApp = express(); | ||
| httpApp.use(express.json()); | ||
|
|
||
| // Meta endpoint required for client instantiation | ||
| httpApp.get('/v1/meta', (req, res) => res.send({ version })); | ||
|
|
||
| // Export schema endpoint - GET /v1/schema/:className | ||
| httpApp.get('/v1/schema/:className', (req, res) => { | ||
| const className = req.params.className; | ||
| const schema = createdSchemas.get(className); | ||
|
|
||
| if (!schema) { | ||
| res.status(404).send({ error: `Collection ${className} not found` }); | ||
| return; | ||
| } | ||
|
|
||
| res.send(schema); | ||
| }); | ||
|
|
||
| // Create schema endpoint - POST /v1/schema | ||
| httpApp.post('/v1/schema', (req, res) => { | ||
| const schema: WeaviateClass = req.body; | ||
|
|
||
| if (!schema.class) { | ||
| res.status(400).send({ error: 'Class name is required' }); | ||
| return; | ||
| } | ||
|
|
||
| // Store the created schema | ||
| createdSchemas.set(schema.class, schema); | ||
|
|
||
| res.status(200).send(schema); | ||
| }); | ||
|
|
||
| return httpApp; | ||
| }; | ||
|
|
||
| const makeGrpcApp = () => { | ||
| // gRPC health check required for client instantiation | ||
| const healthMockImpl: HealthServiceImplementation = { | ||
| check: (request: HealthCheckRequest): Promise<HealthCheckResponse> => | ||
| Promise.resolve(HealthCheckResponse.create({ status: HealthCheckResponse_ServingStatus.SERVING })), | ||
| watch: vi.fn(), | ||
| }; | ||
|
|
||
| const grpcApp = createServer(); | ||
| grpcApp.add(HealthDefinition, healthMockImpl); | ||
|
|
||
| return grpcApp; | ||
| }; | ||
|
|
||
| const makeMockServers = async (weaviateVersion: string, httpPort: number, grpcAddress: string) => { | ||
| // Pre-populate with mock schemas | ||
| const createdSchemas = new Map<string, WeaviateClass>(); | ||
| createdSchemas.set('TestCollection', mockExportedSchema); | ||
| createdSchemas.set('ComplexCollection', mockComplexSchema); | ||
| createdSchemas.set('NamedVectorCollection', mockNamedVectorSchema); | ||
|
|
||
| const rest = makeRestApp(weaviateVersion, createdSchemas); | ||
| const grpc = makeGrpcApp(); | ||
| const server = await rest.listen(httpPort); | ||
| await grpc.listen(grpcAddress); | ||
| return { rest: server, grpc }; | ||
| }; | ||
|
|
||
| describe('Mock testing of exportToJson and createFromJson', () => { | ||
| let servers: { | ||
| rest: HttpServer; | ||
| grpc: GrpcServer; | ||
| }; | ||
| let client: WeaviateClient; | ||
|
|
||
| beforeAll(async () => { | ||
| servers = await makeMockServers('1.27.0', 8920, 'localhost:8921'); | ||
| client = await weaviate.connectToLocal({ port: 8920, grpcPort: 8921 }); | ||
| }); | ||
|
|
||
| afterAll(() => Promise.all([servers.rest.close(), servers.grpc.shutdown()])); | ||
|
|
||
| describe('exportToJson', () => { | ||
| it('should export a simple collection schema to JSON', async () => { | ||
| const collectionName = 'TestCollection'; | ||
| const exportedSchema = await client.collections.exportToJson(collectionName); | ||
|
|
||
| expect(exportedSchema).toBeDefined(); | ||
| expect(exportedSchema.class).toEqual(collectionName); | ||
| expect(exportedSchema.description).toEqual('A test collection for JSON export'); | ||
| expect(exportedSchema.properties).toBeDefined(); | ||
| expect(exportedSchema.properties?.length).toEqual(3); | ||
| }); | ||
|
|
||
| it('should export collection with correct property types', async () => { | ||
| const exportedSchema = await client.collections.exportToJson('TestCollection'); | ||
|
|
||
| const titleProp = exportedSchema.properties?.find((p) => p.name === 'title'); | ||
| expect(titleProp?.dataType).toEqual(['text']); | ||
|
|
||
| const contentProp = exportedSchema.properties?.find((p) => p.name === 'content'); | ||
| expect(contentProp?.dataType).toEqual(['text']); | ||
|
|
||
| const publishDateProp = exportedSchema.properties?.find((p) => p.name === 'publishDate'); | ||
| expect(publishDateProp?.dataType).toEqual(['date']); | ||
| }); | ||
|
|
||
| it('should export a collection with complex configuration', async () => { | ||
| const exportedSchema = await client.collections.exportToJson('ComplexCollection'); | ||
|
|
||
| expect(exportedSchema.class).toEqual('ComplexCollection'); | ||
| expect(exportedSchema.properties?.length).toEqual(2); | ||
|
|
||
| const metadataProp = exportedSchema.properties?.find((p) => p.name === 'metadata'); | ||
| expect(metadataProp?.dataType).toEqual(['object']); | ||
| expect(metadataProp?.nestedProperties).toBeDefined(); | ||
| expect(metadataProp?.nestedProperties?.length).toEqual(2); | ||
|
|
||
| expect(exportedSchema.invertedIndexConfig).toBeDefined(); | ||
| expect(exportedSchema.invertedIndexConfig?.indexTimestamps).toEqual(true); | ||
| expect(exportedSchema.invertedIndexConfig?.indexPropertyLength).toEqual(true); | ||
| }); | ||
|
|
||
| it('should export collection with named vectors', async () => { | ||
| const exportedSchema = await client.collections.exportToJson('NamedVectorCollection'); | ||
|
|
||
| expect(exportedSchema.vectorConfig).toBeDefined(); | ||
| expect(exportedSchema.vectorConfig?.custom_vector).toBeDefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createFromJson', () => { | ||
| it('should create a collection from JSON schema', async () => { | ||
| const schemaJson: WeaviateClass = { | ||
| class: 'NewTestCollection', | ||
| description: 'A test collection created from JSON', | ||
| properties: [ | ||
| { | ||
| name: 'author', | ||
| dataType: ['text'], | ||
| }, | ||
| { | ||
| name: 'rating', | ||
| dataType: ['number'], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const collection = await client.collections.createFromJson(schemaJson); | ||
|
|
||
| expect(collection).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should create a collection from minimal JSON schema', async () => { | ||
| const minimalSchema: WeaviateClass = { | ||
| class: 'MinimalCollection', | ||
| properties: [ | ||
| { | ||
| name: 'text', | ||
| dataType: ['text'], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const collection = await client.collections.createFromJson(minimalSchema); | ||
|
|
||
| expect(collection).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should create a collection with complex nested properties', async () => { | ||
| const complexSchema: WeaviateClass = { | ||
| class: 'ComplexNestedCollection', | ||
| description: 'Collection with nested properties', | ||
| properties: [ | ||
| { | ||
| name: 'metadata', | ||
| dataType: ['object'], | ||
| nestedProperties: [ | ||
| { | ||
| name: 'author', | ||
| dataType: ['text'], | ||
| }, | ||
| { | ||
| name: 'tags', | ||
| dataType: ['text[]'], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const collection = await client.collections.createFromJson(complexSchema); | ||
|
|
||
| expect(collection).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should create a collection with vector configuration', async () => { | ||
| const vectorSchema: WeaviateClass = { | ||
| class: 'VectorCollection', | ||
| properties: [ | ||
| { | ||
| name: 'content', | ||
| dataType: ['text'], | ||
| }, | ||
| ], | ||
| vectorConfig: { | ||
| named_vector: { | ||
| vectorIndexType: 'hnsw', | ||
| vectorizer: { | ||
| none: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const collection = await client.collections.createFromJson(vectorSchema); | ||
|
|
||
| expect(collection).toBeDefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('round-trip export and import', () => { | ||
| it('should export and re-import a schema successfully', async () => { | ||
| // Export existing schema | ||
| const exportedSchema = await client.collections.exportToJson('TestCollection'); | ||
|
|
||
| // Modify for re-import | ||
| exportedSchema.class = 'ReimportedCollection'; | ||
| exportedSchema.description = 'Reimported from exported schema'; | ||
|
|
||
| // Create new collection from exported schema | ||
| const reimportedCollection = await client.collections.createFromJson(exportedSchema); | ||
|
|
||
| expect(reimportedCollection).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should preserve complex configuration in round-trip', async () => { | ||
| const exportedSchema = await client.collections.exportToJson('ComplexCollection'); | ||
|
|
||
| // Change name and re-import | ||
| exportedSchema.class = 'ReimportedComplexCollection'; | ||
|
|
||
| const collection = await client.collections.createFromJson(exportedSchema); | ||
|
|
||
| expect(collection).toBeDefined(); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that this function just posts the
schemaJsondirectly to Weaviate, shouldn't the type ofschemaJsonbeWeaviateClass? E.g.,src/schema/classCreator.ts