Some checks failed
ci / changes (push) Has been cancelled
ci / lint (push) Has been cancelled
ci / build (push) Has been cancelled
ci / tests-unit (push) Has been cancelled
ci / tests-types (push) Has been cancelled
ci / int-cosmosdb (push) Has been cancelled
ci / int-documentdb (push) Has been cancelled
ci / int-firestore (push) Has been cancelled
ci / int-mongodb (push) Has been cancelled
ci / int-postgres (push) Has been cancelled
ci / int-postgres-custom-schema (push) Has been cancelled
ci / int-postgres-uuid (push) Has been cancelled
ci / int-sqlite (push) Has been cancelled
ci / int-sqlite-uuid (push) Has been cancelled
ci / int-supabase (push) Has been cancelled
ci / e2e-_community (push) Has been cancelled
ci / e2e-access-control (push) Has been cancelled
ci / e2e-admin-bar (push) Has been cancelled
ci / e2e-admin-root (push) Has been cancelled
ci / e2e-admin__e2e__document-view (push) Has been cancelled
ci / e2e-admin__e2e__general (push) Has been cancelled
ci / e2e-admin__e2e__list-view (push) Has been cancelled
ci / e2e-auth (push) Has been cancelled
ci / e2e-auth-basic (push) Has been cancelled
ci / e2e-bulk-edit (push) Has been cancelled
ci / e2e-field-error-states (push) Has been cancelled
ci / e2e-fields-relationship (push) Has been cancelled
ci / e2e-fields__collections__Array (push) Has been cancelled
ci / e2e-fields__collections__Blocks#config.blockreferences.ts (push) Has been cancelled
ci / e2e-fields__collections__Blocks (push) Has been cancelled
ci / e2e-fields__collections__Checkbox (push) Has been cancelled
ci / e2e-fields__collections__Collapsible (push) Has been cancelled
ci / e2e-fields__collections__ConditionalLogic (push) Has been cancelled
ci / e2e-fields__collections__CustomID (push) Has been cancelled
ci / e2e-fields__collections__Date (push) Has been cancelled
ci / e2e-fields__collections__Email (push) Has been cancelled
ci / e2e-fields__collections__Indexed (push) Has been cancelled
ci / e2e-fields__collections__JSON (push) Has been cancelled
ci / e2e-fields__collections__Number (push) Has been cancelled
ci / e2e-fields__collections__Point (push) Has been cancelled
ci / e2e-fields__collections__Radio (push) Has been cancelled
ci / e2e-fields__collections__Relationship (push) Has been cancelled
ci / e2e-fields__collections__Row (push) Has been cancelled
ci / e2e-fields__collections__Select (push) Has been cancelled
ci / e2e-fields__collections__Tabs (push) Has been cancelled
ci / e2e-fields__collections__Tabs2 (push) Has been cancelled
ci / e2e-fields__collections__Text (push) Has been cancelled
ci / e2e-fields__collections__UI (push) Has been cancelled
ci / e2e-fields__collections__Upload (push) Has been cancelled
ci / e2e-folders (push) Has been cancelled
ci / e2e-form-state (push) Has been cancelled
ci / e2e-group-by (push) Has been cancelled
ci / e2e-hooks (push) Has been cancelled
ci / e2e-i18n (push) Has been cancelled
ci / e2e-joins (push) Has been cancelled
ci / e2e-lexical__collections__LexicalHeadingFeature (push) Has been cancelled
ci / e2e-lexical__collections__LexicalJSXConverter (push) Has been cancelled
ci / e2e-lexical__collections__LexicalLinkFeature (push) Has been cancelled
ci / e2e-lexical__collections__Lexical__e2e__blocks#config.blockreferences.ts (push) Has been cancelled
ci / e2e-lexical__collections__Lexical__e2e__blocks (push) Has been cancelled
ci / e2e-lexical__collections__Lexical__e2e__main (push) Has been cancelled
ci / e2e-lexical__collections__OnDemandForm (push) Has been cancelled
ci / e2e-lexical__collections__RichText (push) Has been cancelled
ci / e2e-lexical__collections___LexicalFullyFeatured (push) Has been cancelled
ci / e2e-lexical__collections___LexicalFullyFeatured__db (push) Has been cancelled
ci / e2e-live-preview (push) Has been cancelled
ci / e2e-localization (push) Has been cancelled
ci / e2e-locked-documents (push) Has been cancelled
ci / e2e-plugin-cloud-storage (push) Has been cancelled
ci / e2e-plugin-form-builder (push) Has been cancelled
ci / e2e-plugin-import-export (push) Has been cancelled
ci / e2e-plugin-multi-tenant (push) Has been cancelled
ci / e2e-plugin-nested-docs (push) Has been cancelled
ci / e2e-plugin-seo (push) Has been cancelled
ci / e2e-query-presets (push) Has been cancelled
ci / e2e-sort (push) Has been cancelled
ci / e2e-trash (push) Has been cancelled
ci / e2e-uploads (push) Has been cancelled
ci / e2e-versions (push) Has been cancelled
ci / e2e-turbo-_community (push) Has been cancelled
ci / e2e-turbo-access-control (push) Has been cancelled
ci / e2e-turbo-admin-bar (push) Has been cancelled
ci / e2e-turbo-admin-root (push) Has been cancelled
ci / e2e-turbo-admin__e2e__document-view (push) Has been cancelled
ci / e2e-turbo-admin__e2e__general (push) Has been cancelled
ci / e2e-turbo-admin__e2e__list-view (push) Has been cancelled
ci / e2e-turbo-auth (push) Has been cancelled
ci / e2e-turbo-auth-basic (push) Has been cancelled
ci / e2e-turbo-bulk-edit (push) Has been cancelled
ci / e2e-turbo-field-error-states (push) Has been cancelled
ci / e2e-turbo-fields-relationship (push) Has been cancelled
ci / e2e-turbo-fields__collections__Array (push) Has been cancelled
ci / e2e-turbo-fields__collections__Blocks#config.blockreferences.ts (push) Has been cancelled
ci / e2e-turbo-fields__collections__Blocks (push) Has been cancelled
ci / e2e-turbo-fields__collections__Checkbox (push) Has been cancelled
ci / e2e-turbo-fields__collections__Collapsible (push) Has been cancelled
ci / e2e-turbo-fields__collections__ConditionalLogic (push) Has been cancelled
ci / e2e-turbo-fields__collections__CustomID (push) Has been cancelled
ci / e2e-turbo-fields__collections__Date (push) Has been cancelled
ci / e2e-turbo-fields__collections__Email (push) Has been cancelled
ci / e2e-turbo-fields__collections__Indexed (push) Has been cancelled
ci / e2e-turbo-fields__collections__JSON (push) Has been cancelled
ci / e2e-turbo-fields__collections__Number (push) Has been cancelled
ci / e2e-turbo-fields__collections__Point (push) Has been cancelled
ci / e2e-turbo-fields__collections__Radio (push) Has been cancelled
ci / e2e-turbo-fields__collections__Relationship (push) Has been cancelled
ci / e2e-turbo-fields__collections__Row (push) Has been cancelled
ci / e2e-turbo-fields__collections__Select (push) Has been cancelled
ci / e2e-turbo-fields__collections__Tabs (push) Has been cancelled
ci / e2e-turbo-fields__collections__Tabs2 (push) Has been cancelled
ci / e2e-turbo-fields__collections__Text (push) Has been cancelled
ci / e2e-turbo-fields__collections__UI (push) Has been cancelled
ci / e2e-turbo-fields__collections__Upload (push) Has been cancelled
ci / e2e-turbo-folders (push) Has been cancelled
ci / e2e-turbo-form-state (push) Has been cancelled
ci / e2e-turbo-group-by (push) Has been cancelled
ci / e2e-turbo-hooks (push) Has been cancelled
ci / e2e-turbo-i18n (push) Has been cancelled
ci / e2e-turbo-joins (push) Has been cancelled
ci / e2e-turbo-lexical__collections__LexicalHeadingFeature (push) Has been cancelled
ci / e2e-turbo-lexical__collections__LexicalJSXConverter (push) Has been cancelled
ci / e2e-turbo-lexical__collections__LexicalLinkFeature (push) Has been cancelled
ci / e2e-turbo-lexical__collections__Lexical__e2e__blocks#config.blockreferences.ts (push) Has been cancelled
ci / e2e-turbo-lexical__collections__Lexical__e2e__blocks (push) Has been cancelled
ci / e2e-turbo-lexical__collections__Lexical__e2e__main (push) Has been cancelled
ci / e2e-turbo-lexical__collections__OnDemandForm (push) Has been cancelled
ci / e2e-turbo-lexical__collections__RichText (push) Has been cancelled
ci / e2e-turbo-lexical__collections___LexicalFullyFeatured (push) Has been cancelled
ci / e2e-turbo-lexical__collections___LexicalFullyFeatured__db (push) Has been cancelled
ci / e2e-turbo-live-preview (push) Has been cancelled
ci / e2e-turbo-localization (push) Has been cancelled
ci / e2e-turbo-locked-documents (push) Has been cancelled
ci / e2e-turbo-plugin-cloud-storage (push) Has been cancelled
ci / e2e-turbo-plugin-form-builder (push) Has been cancelled
ci / e2e-turbo-plugin-import-export (push) Has been cancelled
ci / e2e-turbo-plugin-multi-tenant (push) Has been cancelled
ci / e2e-turbo-plugin-nested-docs (push) Has been cancelled
ci / e2e-turbo-plugin-seo (push) Has been cancelled
ci / e2e-turbo-query-presets (push) Has been cancelled
ci / e2e-turbo-sort (push) Has been cancelled
ci / e2e-turbo-trash (push) Has been cancelled
ci / e2e-turbo-uploads (push) Has been cancelled
ci / e2e-turbo-versions (push) Has been cancelled
ci / build-template-blank-mongodb (push) Has been cancelled
ci / build-template-website-mongodb (push) Has been cancelled
ci / build-template-with-payload-cloud-mongodb (push) Has been cancelled
ci / build-template-with-vercel-mongodb-mongodb (push) Has been cancelled
ci / build-template-plugin- (push) Has been cancelled
ci / build-template-with-postgres-postgres (push) Has been cancelled
ci / build-template-with-vercel-postgres-postgres (push) Has been cancelled
ci / tests-type-generation (push) Has been cancelled
ci / All Green (push) Has been cancelled
ci / Publish Canary (push) Has been cancelled
ci / analyze (push) Has been cancelled
publish-prerelease / publish-prerelease-${{ github.ref_name }}-${{ github.sha }} (push) Has been cancelled
lock-issues / lock_issues (push) Has been cancelled
stale / stale (push) Has been cancelled
audit-dependencies / audit (push) Has been cancelled
activity-notifications / run (push) Has been cancelled
361 lines
8.3 KiB
TypeScript
361 lines
8.3 KiB
TypeScript
/* eslint-disable jest/no-conditional-in-test */
|
|
/* eslint-disable jest/expect-expect */
|
|
/* eslint-disable jest/require-top-level-describe */
|
|
import type { PostgresAdapter } from '@tabshiftcms/db-postgres'
|
|
import type { PostgresDB } from '@tabshiftcms/drizzle'
|
|
|
|
import { cosineDistance, desc, gt, jaccardDistance, l2Distance, lt, sql } from 'drizzle-orm'
|
|
import path from 'path'
|
|
import { BasePayload, buildConfig, type DatabaseAdapterObj } from 'tbsh-cms'
|
|
import { fileURLToPath } from 'url'
|
|
|
|
const filename = fileURLToPath(import.meta.url)
|
|
const dirname = path.dirname(filename)
|
|
|
|
const describePostgres = process.env.PAYLOAD_DATABASE?.startsWith('postgres')
|
|
? describe
|
|
: describe.skip
|
|
|
|
describePostgres('postgres vector custom column', () => {
|
|
const vectorColumnQueryTest = async (vectorType: string) => {
|
|
const {
|
|
databaseAdapter,
|
|
}: {
|
|
databaseAdapter: DatabaseAdapterObj<PostgresAdapter>
|
|
} = await import(path.resolve(dirname, '../databaseAdapter.js'))
|
|
|
|
const init = databaseAdapter.init
|
|
|
|
// set options
|
|
databaseAdapter.init = ({ payload }) => {
|
|
const adapter = init({ payload })
|
|
|
|
adapter.extensions = {
|
|
vector: true,
|
|
}
|
|
adapter.beforeSchemaInit = [
|
|
({ schema, adapter }) => {
|
|
if (adapter?.rawTables?.posts?.columns) {
|
|
adapter.rawTables.posts.columns.embedding = {
|
|
type: vectorType,
|
|
dimensions: 5,
|
|
name: 'embedding',
|
|
}
|
|
}
|
|
return schema
|
|
},
|
|
]
|
|
return adapter
|
|
}
|
|
|
|
const config = await buildConfig({
|
|
db: databaseAdapter,
|
|
secret: 'secret',
|
|
collections: [
|
|
{
|
|
slug: 'users',
|
|
auth: true,
|
|
fields: [],
|
|
},
|
|
{
|
|
slug: 'posts',
|
|
fields: [
|
|
{
|
|
type: 'json',
|
|
name: 'embedding',
|
|
},
|
|
{
|
|
name: 'title',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
})
|
|
|
|
// do not use getPayload to avoid caching and re-using payload instance from previous tests
|
|
const payload = await new BasePayload().init({ config })
|
|
|
|
const catEmbedding = [1.5, -0.4, 7.2, 19.6, 20.2]
|
|
|
|
await payload.create({
|
|
collection: 'posts',
|
|
data: {
|
|
embedding: [-5.2, 3.1, 0.2, 8.1, 3.5],
|
|
title: 'apple',
|
|
},
|
|
})
|
|
|
|
await payload.create({
|
|
collection: 'posts',
|
|
data: {
|
|
embedding: catEmbedding,
|
|
title: 'cat',
|
|
},
|
|
})
|
|
|
|
await payload.create({
|
|
collection: 'posts',
|
|
data: {
|
|
embedding: [-5.1, 2.9, 0.8, 7.9, 3.1],
|
|
title: 'fruit',
|
|
},
|
|
})
|
|
|
|
await payload.create({
|
|
collection: 'posts',
|
|
data: {
|
|
embedding: [1.7, -0.3, 6.9, 19.1, 21.1],
|
|
title: 'dog',
|
|
},
|
|
})
|
|
|
|
const similarity = sql<number>`1 - (${cosineDistance(payload.db.tables.posts.embedding, catEmbedding)})`
|
|
|
|
const db = payload.db.drizzle as PostgresDB
|
|
|
|
const res = await db
|
|
.select()
|
|
.from(payload.db.tables.posts)
|
|
.where(gt(similarity, 0.9))
|
|
.orderBy(desc(similarity))
|
|
|
|
// Only cat and dog
|
|
expect(res).toHaveLength(2)
|
|
|
|
// similarity sort
|
|
expect(res?.[0]?.title).toBe('cat')
|
|
expect(res?.[1]?.title).toBe('dog')
|
|
}
|
|
|
|
it('should add a vector column and query it', async () => {
|
|
await vectorColumnQueryTest('vector')
|
|
})
|
|
|
|
it('should add a halfvec column and query it', async () => {
|
|
await vectorColumnQueryTest('halfvec')
|
|
})
|
|
|
|
it('should add a sparsevec column and query it', async () => {
|
|
const {
|
|
databaseAdapter,
|
|
}: {
|
|
databaseAdapter: DatabaseAdapterObj<PostgresAdapter>
|
|
} = await import(path.resolve(dirname, '../databaseAdapter.js'))
|
|
|
|
const init = databaseAdapter.init
|
|
|
|
databaseAdapter.init = ({ payload }) => {
|
|
const adapter = init({ payload })
|
|
|
|
adapter.extensions = {
|
|
vector: true,
|
|
}
|
|
|
|
adapter.beforeSchemaInit = [
|
|
({ schema, adapter }) => {
|
|
if (adapter?.rawTables?.posts?.columns) {
|
|
adapter.rawTables.posts.columns.embedding = {
|
|
type: 'sparsevec',
|
|
dimensions: 5,
|
|
name: 'embedding',
|
|
}
|
|
}
|
|
return schema
|
|
},
|
|
]
|
|
|
|
return adapter
|
|
}
|
|
|
|
const config = await buildConfig({
|
|
db: databaseAdapter,
|
|
secret: 'secret',
|
|
collections: [
|
|
{
|
|
slug: 'users',
|
|
auth: true,
|
|
fields: [],
|
|
},
|
|
{
|
|
slug: 'posts',
|
|
fields: [
|
|
{
|
|
name: 'embedding',
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'title',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
})
|
|
|
|
const payload = await new BasePayload().init({ config })
|
|
|
|
// sparse-vector format: '{index:value,...}/dims'
|
|
const catEmbedding = '{1:1,3:2,5:3}/5'
|
|
|
|
await payload.create({
|
|
collection: 'posts',
|
|
data: {
|
|
embedding: '{2:1,4:2}/5',
|
|
title: 'apple',
|
|
},
|
|
})
|
|
|
|
await payload.create({
|
|
collection: 'posts',
|
|
data: {
|
|
embedding: catEmbedding,
|
|
title: 'cat',
|
|
},
|
|
})
|
|
|
|
await payload.create({
|
|
collection: 'posts',
|
|
data: {
|
|
embedding: '{2:4,4:6}/5',
|
|
title: 'fruit',
|
|
},
|
|
})
|
|
|
|
await payload.create({
|
|
collection: 'posts',
|
|
data: {
|
|
embedding: '{1:1,3:2,5:2}/5',
|
|
title: 'dog',
|
|
},
|
|
})
|
|
|
|
const distance = sql<number>`(${l2Distance(payload.db.tables.posts.embedding, catEmbedding)})`
|
|
|
|
const db = payload.db.drizzle as PostgresDB
|
|
|
|
const res = await db
|
|
.select()
|
|
.from(payload.db.tables.posts)
|
|
.where(lt(distance, 1.1))
|
|
.orderBy(distance)
|
|
.execute()
|
|
|
|
// should return cat (distance 0) then dog
|
|
expect(res).toHaveLength(2)
|
|
expect(res?.[0]?.title).toBe('cat')
|
|
expect(res?.[1]?.title).toBe('dog')
|
|
})
|
|
|
|
it('should add a binaryvec column and query it', async () => {
|
|
const {
|
|
databaseAdapter,
|
|
}: {
|
|
databaseAdapter: DatabaseAdapterObj<PostgresAdapter>
|
|
} = await import(path.resolve(dirname, '../databaseAdapter.js'))
|
|
|
|
const init = databaseAdapter.init
|
|
|
|
// set options
|
|
databaseAdapter.init = ({ payload }) => {
|
|
const adapter = init({ payload })
|
|
|
|
adapter.extensions = {
|
|
vector: true,
|
|
}
|
|
adapter.beforeSchemaInit = [
|
|
({ schema, adapter }) => {
|
|
if (adapter?.rawTables?.posts?.columns) {
|
|
adapter.rawTables.posts.columns.embedding = {
|
|
type: 'bit',
|
|
dimensions: 5,
|
|
name: 'embedding',
|
|
}
|
|
}
|
|
return schema
|
|
},
|
|
]
|
|
return adapter
|
|
}
|
|
|
|
const config = await buildConfig({
|
|
db: databaseAdapter,
|
|
secret: 'secret',
|
|
collections: [
|
|
{
|
|
slug: 'users',
|
|
auth: true,
|
|
fields: [],
|
|
},
|
|
{
|
|
slug: 'posts',
|
|
fields: [
|
|
{
|
|
type: 'text',
|
|
name: 'embedding',
|
|
},
|
|
{
|
|
name: 'title',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
})
|
|
|
|
// do not use getPayload to avoid caching and re-using payload instance from previous tests
|
|
const payload = await new BasePayload().init({ config })
|
|
|
|
const catEmbedding = '10101'
|
|
|
|
await payload.create({
|
|
collection: 'posts',
|
|
data: {
|
|
embedding: '01010',
|
|
title: 'apple',
|
|
},
|
|
})
|
|
|
|
await payload.create({
|
|
collection: 'posts',
|
|
data: {
|
|
embedding: '10101',
|
|
title: 'cat',
|
|
},
|
|
})
|
|
|
|
await payload.create({
|
|
collection: 'posts',
|
|
data: {
|
|
embedding: '11111',
|
|
title: 'fruit',
|
|
},
|
|
})
|
|
|
|
await payload.create({
|
|
collection: 'posts',
|
|
data: {
|
|
embedding: '10100',
|
|
title: 'dog',
|
|
},
|
|
})
|
|
|
|
const similarity = sql<number>`1 - (${jaccardDistance(payload.db.tables.posts.embedding, catEmbedding)})`
|
|
|
|
const db = payload.db.drizzle as PostgresDB
|
|
|
|
const res = await db
|
|
.select()
|
|
.from(payload.db.tables.posts)
|
|
.where(gt(similarity, 0.6))
|
|
.orderBy(desc(similarity))
|
|
|
|
// Only cat and dog
|
|
expect(res).toHaveLength(2)
|
|
|
|
// similarity sort
|
|
expect(res?.[0]?.title).toBe('cat')
|
|
expect(res?.[1]?.title).toBe('dog')
|
|
})
|
|
})
|