azure-ai-search-orm
TypeScript icon, indicating that this package has built-in type declarations

0.0.20 • Public • Published

Azure AI Search ORM

A fully type-safe "ORM" for Azure AI Search (formerly Cognitive Search) inspired by Drizzle ORM.

Define a fluent schema:

import { string, index, int32 } from "orm/schema";

export const realEstate = index("realestate-us-sample-index", {
  listingId: string("listingId").key(),
  beds: int32("beds").filterable().sortable().facetable(),
  baths: int32("baths").filterable().sortable().facetable(),
  description: string("description").searchable(),
  squareFeet: int32("sqft").filterable().sortable().facetable(),
});

and use all the methods you'd usually use on a SearchClient, but with excellent TypeScript support:

const searchIndexClient = new SearchIndexClient(endpoint, identity);

const srch = connect(searchIndexClient, schema);

const data = await srch.realEstate.search(undefined, {
  top: 20,
  select: ["listingId", "description"],
});

Supported Field Types

Azure AI Search ORM supports most of the AI Search EDM data types.

Primitives

// Edm.String
myField: string("myField");

// Edm.Int32
myField: int32("myField");

// Edm.Int64
myField: int64("myField");

//Edm.Double
myField: double("myField");

//Edm.Boolean
myField: boolean("myField");

Collections

// Collection(Edm.String)
myCollection: stringCollection("myCollection");

// Collection(Edm.Int32)
myCollection: int32Collection("myCollection");

// Collection(Edm.Int64)
myCollection: int64Collection("myCollection");

// Collection(Edm.Double)
myCollection: doubleCollection("myCollection");

// Collection(Edm.DateTimeOffset)
myCollection: dateCollection("myCollection");

// Collection(Edm.Boolean)
myCollection: booleanCollection("myCollection");

// Collection(Edm.ComplexType)
myCollection: collection("myCollection", {
  // ComplexType object fields
  myField: string("myField"),
});

Suggesters

Add a suggester to a field in the index schema:

import { index, string } from "azure-ai-search-orm";

const hotels = index("hotels-sample-index", {
  name: string("name").suggester(),
});

and use AI Search's suggest() method with Typescript:

const { results } = await srch.hotels.suggest("my query", "sg", {
  select: ["name"],
  searchFields: ["name"],
});

"sg" is the default suggester name.

Extras

Field names in the ORM don't need to match the names in the datasource. This automatically creates a field mapping in the indexer.

export const realEstate = index("realestate-us-sample-index", {
  squareFeet: int32("sqft").filterable().sortable().facetable(),
  // ^ORM field name  ^datasource column name
});

Note: This only works on top-level primitives. Complex fields aren't supported in a field mapping.

Dependencies (0)

    Dev Dependencies (8)

    Package Sidebar

    Install

    npm i azure-ai-search-orm

    Weekly Downloads

    7

    Version

    0.0.20

    License

    ISC

    Unpacked Size

    1.03 MB

    Total Files

    6

    Last publish

    Collaborators

    • ihusemann