convex
Version:
Client for the Convex Cloud
158 lines (157 loc) • 4.71 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
import {
isValidator,
v
} from "../values/validator.js";
export class TableDefinition {
/**
* @internal
*/
constructor(documentType) {
__publicField(this, "indexes");
__publicField(this, "searchIndexes");
__publicField(this, "vectorIndexes");
// The type of documents stored in this table.
__publicField(this, "validator");
this.indexes = [];
this.searchIndexes = [];
this.vectorIndexes = [];
this.validator = documentType;
}
/**
* Define an index on this table.
*
* To learn about indexes, see [Defining Indexes](https://docs.convex.dev/using/indexes).
*
* @param name - The name of the index.
* @param fields - The fields to index, in order. Must specify at least one
* field.
* @returns A {@link TableDefinition} with this index included.
*/
index(name, fields) {
this.indexes.push({ indexDescriptor: name, fields });
return this;
}
/**
* Define a search index on this table.
*
* To learn about search indexes, see [Search](https://docs.convex.dev/text-search).
*
* @param name - The name of the index.
* @param indexConfig - The search index configuration object.
* @returns A {@link TableDefinition} with this search index included.
*/
searchIndex(name, indexConfig) {
this.searchIndexes.push({
indexDescriptor: name,
searchField: indexConfig.searchField,
filterFields: indexConfig.filterFields || []
});
return this;
}
/**
* Define a vector index on this table.
*
* To learn about vector indexes, see [Vector Search](https://docs.convex.dev/vector-search).
*
* @param name - The name of the index.
* @param indexConfig - The vector index configuration object.
* @returns A {@link TableDefinition} with this vector index included.
*/
vectorIndex(name, indexConfig) {
this.vectorIndexes.push({
indexDescriptor: name,
vectorField: indexConfig.vectorField,
dimensions: indexConfig.dimensions,
filterFields: indexConfig.filterFields || []
});
return this;
}
/**
* Work around for https://github.com/microsoft/TypeScript/issues/57035
*/
self() {
return this;
}
/**
* Export the contents of this definition.
*
* This is called internally by the Convex framework.
* @internal
*/
export() {
return {
indexes: this.indexes,
searchIndexes: this.searchIndexes,
vectorIndexes: this.vectorIndexes,
documentType: this.validator.json
};
}
}
export function defineTable(documentSchema) {
if (isValidator(documentSchema)) {
return new TableDefinition(documentSchema);
} else {
return new TableDefinition(v.object(documentSchema));
}
}
export class SchemaDefinition {
/**
* @internal
*/
constructor(tables, options) {
__publicField(this, "tables");
__publicField(this, "strictTableNameTypes");
__publicField(this, "schemaValidation");
this.tables = tables;
this.schemaValidation = options?.schemaValidation === void 0 ? true : options.schemaValidation;
}
/**
* Export the contents of this definition.
*
* This is called internally by the Convex framework.
* @internal
*/
export() {
return JSON.stringify({
tables: Object.entries(this.tables).map(([tableName, definition]) => {
const { indexes, searchIndexes, vectorIndexes, documentType } = definition.export();
return {
tableName,
indexes,
searchIndexes,
vectorIndexes,
documentType
};
}),
schemaValidation: this.schemaValidation
});
}
}
export function defineSchema(schema, options) {
return new SchemaDefinition(schema, options);
}
const _systemSchema = defineSchema({
_scheduled_functions: defineTable({
name: v.string(),
args: v.array(v.any()),
scheduledTime: v.float64(),
completedTime: v.optional(v.float64()),
state: v.union(
v.object({ kind: v.literal("pending") }),
v.object({ kind: v.literal("inProgress") }),
v.object({ kind: v.literal("success") }),
v.object({ kind: v.literal("failed"), error: v.string() }),
v.object({ kind: v.literal("canceled") })
)
}),
_storage: defineTable({
sha256: v.string(),
size: v.float64(),
contentType: v.optional(v.string())
})
});
//# sourceMappingURL=schema.js.map