-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added postgres module for Postgres.js
- Loading branch information
Showing
5 changed files
with
80 additions
and
9 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
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,45 @@ | ||
const { fromSql, toSql } = require('..'); | ||
const utils = require('../utils'); | ||
|
||
async function types(sql) { | ||
const typeInfo = await sql`SELECT regtype('vector')::oid AS vector, regtype('halfvec')::oid AS halfvec, regtype('sparsevec')::oid AS sparsevec`; | ||
|
||
const vectorOid = typeInfo[0].vector; | ||
const halfvecOid = typeInfo[0].halfvec; | ||
const sparsevecOid = typeInfo[0].sparsevec; | ||
|
||
if (!vectorOid) { | ||
throw new Error('vector type not found in the database'); | ||
} | ||
|
||
const types = { | ||
vector: { | ||
to: vectorOid, | ||
from: [vectorOid], | ||
serialize: (v) => utils.vectorToSql(v), | ||
parse: (v) => utils.vectorFromSql(v) | ||
} | ||
}; | ||
|
||
if (halfvecOid) { | ||
types['halfvec'] = { | ||
to: halfvecOid, | ||
from: [halfvecOid], | ||
serialize: (v) => utils.halfvecToSql(v), | ||
parse: (v) => utils.halfvecFromSql(v) | ||
}; | ||
} | ||
|
||
if (sparsevecOid) { | ||
types['sparsevec'] = { | ||
to: sparsevecOid, | ||
from: [sparsevecOid], | ||
serialize: (v) => utils.sparsevecToSql(v), | ||
parse: (v) => utils.sparsevecFromSql(v) | ||
}; | ||
} | ||
|
||
return types; | ||
} | ||
|
||
module.exports = {types, fromSql, toSql}; |
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,11 @@ | ||
export function types(sql: any): Promise<{ | ||
vector: { | ||
to: any; | ||
from: any[]; | ||
serialize: (v: any) => any; | ||
parse: (v: any) => any; | ||
}; | ||
}>; | ||
import { fromSql } from ".."; | ||
import { toSql } from ".."; | ||
export { fromSql, toSql }; |