convex-ents
Advanced tools
Comparing version 0.7.7 to 0.8.0
@@ -16,3 +16,3 @@ import * as convex_values from 'convex/values'; | ||
}; | ||
declare const vApproach: convex_values.Validator<"cascade" | "paginate", false, never>; | ||
declare const vApproach: convex_values.VUnion<"cascade" | "paginate", [convex_values.VLiteral<"cascade", "required">, convex_values.VLiteral<"paginate", "required">], "required", never>; | ||
type Approach = Infer<typeof vApproach>; | ||
@@ -19,0 +19,0 @@ declare function scheduledDeleteFactory<EntsDataModel extends GenericEntsDataModel>(entDefinitions: EntsDataModel, options?: { |
@@ -194,8 +194,4 @@ "use strict"; | ||
function defineEntFromTable(definition) { | ||
const validator = definition.documentType; | ||
const validatorJson = ( | ||
// @ts-expect-error Private field | ||
validator.json | ||
); | ||
if (validatorJson.type !== "object") { | ||
const validator = definition.validator; | ||
if (validator.kind !== "object") { | ||
throw new Error( | ||
@@ -205,10 +201,3 @@ "Only tables with object definition are supported in Ents, not unions" | ||
} | ||
const tableSchema = Object.fromEntries( | ||
Object.entries(validatorJson.value).map(([k, v3]) => [ | ||
k, | ||
// @ts-expect-error Private constructor | ||
new import_values.Validator(v3.fieldType, v3.optional) | ||
]) | ||
); | ||
const entDefinition = defineEnt(tableSchema); | ||
const entDefinition = defineEnt(validator.fields); | ||
entDefinition.indexes = definition.indexes; | ||
@@ -220,2 +209,3 @@ entDefinition.searchIndexes = definition.searchIndexes; | ||
var EntDefinitionImpl = class { | ||
validator; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
@@ -237,2 +227,3 @@ // @ts-ignore | ||
this.documentSchema = documentSchema; | ||
this.validator = import_values.v.object(documentSchema); | ||
} | ||
@@ -239,0 +230,0 @@ index(name, fields) { |
@@ -1,7 +0,7 @@ | ||
import { DefineSchemaOptions, SchemaDefinition, GenericDocument, GenericTableIndexes, GenericTableSearchIndexes, GenericTableVectorIndexes, TableDefinition, SearchIndexConfig, VectorIndexConfig, GenericDataModel, DataModelFromSchemaDefinition } from 'convex/server'; | ||
import { Validator, GenericId, PropertyValidators, ObjectType } from 'convex/values'; | ||
import { DefineSchemaOptions, SchemaDefinition, GenericTableIndexes, GenericTableSearchIndexes, GenericTableVectorIndexes, TableDefinition, SearchIndexConfig, VectorIndexConfig, GenericDataModel, DataModelFromSchemaDefinition } from 'convex/server'; | ||
import { PropertyValidators, GenericValidator, Validator, VId, GenericId, VOptional, VFloat64, VObject, ObjectType, VAny } from 'convex/values'; | ||
declare function defineEntSchema<Schema extends Record<string, EntDefinition>, StrictTableNameTypes extends boolean = true>(schema: Schema, options?: DefineSchemaOptions<StrictTableNameTypes>): SchemaDefinition<Schema, StrictTableNameTypes>; | ||
declare function defineEnt<DocumentSchema extends Record<string, Validator<any, any, any>>>(documentSchema: DocumentSchema): EntDefinition<ExtractDocument<ObjectValidator<DocumentSchema>>, ExtractFieldPaths<ObjectValidator<DocumentSchema>>>; | ||
declare function defineEntFromTable<Document extends GenericDocument = GenericDocument, FieldPaths extends string = string, Indexes extends GenericTableIndexes = {}, SearchIndexes extends GenericTableSearchIndexes = {}, VectorIndexes extends GenericTableVectorIndexes = {}>(definition: TableDefinition<Document, FieldPaths, Indexes, SearchIndexes, VectorIndexes>): EntDefinition<Document, FieldPaths, Indexes, SearchIndexes, VectorIndexes>; | ||
declare function defineEnt<DocumentSchema extends PropertyValidators>(documentSchema: DocumentSchema): EntDefinition<ObjectValidator<DocumentSchema>>; | ||
declare function defineEntFromTable<DocumentType extends GenericValidator = GenericValidator, Indexes extends GenericTableIndexes = {}, SearchIndexes extends GenericTableSearchIndexes = {}, VectorIndexes extends GenericTableVectorIndexes = {}>(definition: TableDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes>): EntDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes>; | ||
type GenericEdges = Record<string, GenericEdgeConfig>; | ||
@@ -14,3 +14,12 @@ type GenericEdgeConfig = { | ||
}; | ||
interface EntDefinition<Document extends GenericDocument = GenericDocument, FieldPaths extends string = string, Indexes extends GenericTableIndexes = {}, SearchIndexes extends GenericTableSearchIndexes = {}, VectorIndexes extends GenericTableVectorIndexes = {}, Edges extends GenericEdges = {}> extends TableDefinition<Document, FieldPaths, Indexes, SearchIndexes, VectorIndexes> { | ||
type ExtractFieldPaths<T extends Validator<any, any, any>> = T["fieldPaths"] | keyof SystemFields; | ||
type ObjectFieldType<FieldName extends string, T extends Validator<any, any, any>> = T["isOptional"] extends "optional" ? { | ||
[key in FieldName]?: T["type"]; | ||
} : { | ||
[key in FieldName]: T["type"]; | ||
}; | ||
type AddField<V extends GenericValidator, FieldName extends string, P extends GenericValidator> = V extends VObject<infer TypeScriptType, infer Fields, infer IsOptional, infer FieldPaths> ? VObject<Expand<TypeScriptType & ObjectFieldType<FieldName, P>>, Expand<Fields & { | ||
FieldName: P; | ||
}>, IsOptional, FieldPaths | FieldName> : V extends VAny ? VAny : never; | ||
interface EntDefinition<DocumentType extends Validator<any, any, any> = Validator<any, any, any>, Indexes extends GenericTableIndexes = {}, SearchIndexes extends GenericTableSearchIndexes = {}, VectorIndexes extends GenericTableVectorIndexes = {}, Edges extends GenericEdges = {}> extends TableDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes> { | ||
/** | ||
@@ -26,3 +35,3 @@ * Define an index on this table. | ||
*/ | ||
index<IndexName extends string, FirstFieldPath extends FieldPaths, RestFieldPaths extends FieldPaths[]>(name: IndexName, fields: [FirstFieldPath, ...RestFieldPaths]): EntDefinition<Document, FieldPaths, Expand<Indexes & Record<IndexName, [FirstFieldPath, ...RestFieldPaths, "_creationTime"]>>, SearchIndexes, VectorIndexes, Edges>; | ||
index<IndexName extends string, FirstFieldPath extends ExtractFieldPaths<DocumentType>, RestFieldPaths extends ExtractFieldPaths<DocumentType>[]>(name: IndexName, fields: [FirstFieldPath, ...RestFieldPaths]): EntDefinition<DocumentType, Expand<Indexes & Record<IndexName, [FirstFieldPath, ...RestFieldPaths, "_creationTime"]>>, SearchIndexes, VectorIndexes, Edges>; | ||
/** | ||
@@ -37,7 +46,7 @@ * Define a search index on this table. | ||
*/ | ||
searchIndex<IndexName extends string, SearchField extends FieldPaths, FilterFields extends FieldPaths = never>(name: IndexName, indexConfig: Expand<SearchIndexConfig<SearchField, FilterFields>>): EntDefinition<Document, FieldPaths, Indexes, Expand<SearchIndexes & Record<IndexName, { | ||
searchIndex<IndexName extends string, SearchField extends ExtractFieldPaths<DocumentType>, FilterFields extends ExtractFieldPaths<DocumentType> = never>(name: IndexName, indexConfig: Expand<SearchIndexConfig<SearchField, FilterFields>>): EntDefinition<DocumentType, Indexes, Expand<SearchIndexes & Record<IndexName, { | ||
searchField: SearchField; | ||
filterFields: FilterFields; | ||
}>>, VectorIndexes, Edges>; | ||
vectorIndex<IndexName extends string, VectorField extends FieldPaths, FilterFields extends FieldPaths = never>(name: IndexName, indexConfig: Expand<VectorIndexConfig<VectorField, FilterFields>>): EntDefinition<Document, FieldPaths, Indexes, SearchIndexes, Expand<VectorIndexes & Record<IndexName, { | ||
vectorIndex<IndexName extends string, VectorField extends ExtractFieldPaths<DocumentType>, FilterFields extends ExtractFieldPaths<DocumentType> = never>(name: IndexName, indexConfig: Expand<VectorIndexConfig<VectorField, FilterFields>>): EntDefinition<DocumentType, Indexes, SearchIndexes, Expand<VectorIndexes & Record<IndexName, { | ||
vectorField: VectorField; | ||
@@ -47,6 +56,6 @@ dimensions: number; | ||
}>>, Edges>; | ||
field<FieldName extends string, T extends Validator<any, any, any>>(field: FieldName, validator: T): EntDefinition<Document & ObjectFieldType<FieldName, T>, FieldPaths | FieldName, Indexes, SearchIndexes, VectorIndexes, Edges>; | ||
field<FieldName extends string, T extends GenericValidator>(field: FieldName, validator: T): EntDefinition<AddField<DocumentType, FieldName, T>, Indexes, SearchIndexes, VectorIndexes, Edges>; | ||
field<FieldName extends string, T extends Validator<any, any, any>>(field: FieldName, validator: T, options: { | ||
index: true; | ||
}): EntDefinition<Document & ObjectFieldType<FieldName, T>, FieldPaths | FieldName, Indexes & { | ||
}): EntDefinition<AddField<DocumentType, FieldName, T>, Indexes & { | ||
[key in FieldName]: [FieldName, "_creationTime"]; | ||
@@ -56,11 +65,9 @@ }, SearchIndexes, VectorIndexes, Edges>; | ||
unique: true; | ||
}): EntDefinition<Document & ObjectFieldType<FieldName, T>, FieldPaths | FieldName, Indexes & { | ||
}): EntDefinition<AddField<DocumentType, FieldName, T>, Indexes & { | ||
[key in FieldName]: [FieldName, "_creationTime"]; | ||
}, SearchIndexes, VectorIndexes, Edges>; | ||
field<FieldName extends string, T extends Validator<any, false, any>>(field: FieldName, validator: T, options: { | ||
field<FieldName extends string, T extends Validator<any, "required", any>>(field: FieldName, validator: T, options: { | ||
default: T["type"]; | ||
}): EntDefinition<Document & ObjectFieldType<FieldName, T>, FieldPaths | FieldName, Indexes, SearchIndexes, VectorIndexes, Edges>; | ||
edge<EdgeName extends string>(edge: EdgeName): EntDefinition<Document & { | ||
[key in `${EdgeName}Id`]: GenericId<`${EdgeName}s`>; | ||
}, FieldPaths | `${EdgeName}Id`, Indexes & { | ||
}): EntDefinition<AddField<DocumentType, FieldName, T>, Indexes, SearchIndexes, VectorIndexes, Edges>; | ||
edge<EdgeName extends string>(edge: EdgeName): EntDefinition<AddField<DocumentType, `${EdgeName}Id`, VId<GenericId<`${EdgeName}s`>>>, Indexes & { | ||
[key in `${EdgeName}Id`]: [`${EdgeName}Id`, "_creationTime"]; | ||
@@ -77,5 +84,3 @@ }, SearchIndexes, VectorIndexes, Edges & { | ||
field: FieldName; | ||
}): EntDefinition<Document & { | ||
[key in NoInfer<FieldName>]: GenericId<`${EdgeName}s`>; | ||
}, FieldPaths | NoInfer<FieldName>, Indexes & { | ||
}): EntDefinition<AddField<DocumentType, NoInfer<FieldName>, VId<GenericId<`${EdgeName}s`>>>, Indexes & { | ||
[key in NoInfer<FieldName>]: [NoInfer<FieldName>, "_creationTime"]; | ||
@@ -93,5 +98,3 @@ }, SearchIndexes, VectorIndexes, Edges & { | ||
to: ToTable; | ||
}): EntDefinition<Document & { | ||
[key in NoInfer<FieldName>]: GenericId<ToTable>; | ||
}, FieldPaths | NoInfer<FieldName>, Indexes & { | ||
}): EntDefinition<AddField<DocumentType, NoInfer<FieldName>, VId<GenericId<`${ToTable}`>>>, Indexes & { | ||
[key in NoInfer<FieldName>]: [NoInfer<FieldName>, "_creationTime"]; | ||
@@ -110,3 +113,3 @@ }, SearchIndexes, VectorIndexes, Edges & { | ||
deletion?: "soft"; | ||
}): EntDefinition<Document, FieldPaths, Indexes, SearchIndexes, VectorIndexes, Edges & { | ||
}): EntDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes, Edges & { | ||
[key in EdgeName]: { | ||
@@ -124,3 +127,3 @@ name: EdgeName; | ||
deletion?: "soft"; | ||
}): EntDefinition<Document, FieldPaths, Indexes, SearchIndexes, VectorIndexes, Edges & { | ||
}): EntDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes, Edges & { | ||
[key in EdgeName]: { | ||
@@ -142,3 +145,3 @@ name: EdgeName; | ||
deletion?: "soft"; | ||
}): EntDefinition<Document, FieldPaths, Indexes, SearchIndexes, VectorIndexes, Edges & { | ||
}): EntDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes, Edges & { | ||
[key in EdgesName]: { | ||
@@ -164,3 +167,3 @@ name: EdgesName; | ||
deletion?: "soft"; | ||
}): EntDefinition<Document, FieldPaths, Indexes, SearchIndexes, VectorIndexes, Edges & { | ||
}): EntDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes, Edges & { | ||
[key in EdgesName]: { | ||
@@ -183,3 +186,3 @@ name: EdgesName; | ||
field?: string; | ||
}): EntDefinition<Document, FieldPaths, Indexes, SearchIndexes, VectorIndexes, Edges & { | ||
}): EntDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes, Edges & { | ||
[key in EdgesName]: { | ||
@@ -210,3 +213,3 @@ name: EdgesName; | ||
inverseField?: string; | ||
}): EntDefinition<Document, FieldPaths, Indexes, SearchIndexes, VectorIndexes, Edges & { | ||
}): EntDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes, Edges & { | ||
[key in EdgesName]: { | ||
@@ -237,3 +240,3 @@ name: EdgesName; | ||
inverseField?: string; | ||
}): EntDefinition<Document, FieldPaths, Indexes, SearchIndexes, VectorIndexes, Edges & { | ||
}): EntDefinition<DocumentType, Indexes, SearchIndexes, VectorIndexes, Edges & { | ||
[key in EdgesName]: { | ||
@@ -261,5 +264,3 @@ name: EdgesName; | ||
*/ | ||
deletion(type: "soft"): EntDefinition<Document & { | ||
deletionTime?: number; | ||
}, FieldPaths | "deletionTime", Indexes, SearchIndexes, VectorIndexes, Edges>; | ||
deletion(type: "soft"): EntDefinition<AddField<DocumentType, "deletionTime", VOptional<VFloat64>>, Indexes, SearchIndexes, VectorIndexes, Edges>; | ||
/** | ||
@@ -278,12 +279,5 @@ * Add the "scheduled" deletion behavior to this ent. | ||
delayMs: number; | ||
}): EntDefinition<Document & { | ||
deletionTime?: number; | ||
}, FieldPaths | "deletionTime", Indexes, SearchIndexes, VectorIndexes, Edges>; | ||
}): EntDefinition<AddField<DocumentType, "deletionTime", VOptional<VFloat64>>, Indexes, SearchIndexes, VectorIndexes, Edges>; | ||
} | ||
type NoInfer<T> = [T][T extends any ? 0 : never]; | ||
type ObjectFieldType<FieldName extends string, T extends Validator<any, any, any>> = T["isOptional"] extends true ? { | ||
[key in FieldName]?: T["type"]; | ||
} : { | ||
[key in FieldName]: T["type"]; | ||
}; | ||
type EdgeConfig = { | ||
@@ -320,14 +314,9 @@ name: string; | ||
}; | ||
type ExtractDocument<T extends Validator<any, any, any>> = Expand<SystemFields & T["type"]>; | ||
type Expand<ObjectType extends Record<any, any>> = ObjectType extends Record<any, any> ? { | ||
[Key in keyof ObjectType]: ObjectType[Key]; | ||
} : never; | ||
type ExtractFieldPaths<T extends Validator<any, any, any>> = T["fieldPaths"] | keyof SystemFields; | ||
type SystemFields = { | ||
_creationTime: number; | ||
}; | ||
type ObjectValidator<Validators extends PropertyValidators> = Validator<ObjectType<Validators>, false, { | ||
[Property in keyof Validators]: JoinFieldPaths<Property & string, Validators[Property]["fieldPaths"]> | Property; | ||
}[keyof Validators] & string>; | ||
type JoinFieldPaths<Start extends string, End extends string> = `${Start}.${End}`; | ||
type ObjectValidator<Validators extends PropertyValidators> = VObject<ObjectType<Validators>, Validators>; | ||
type GenericEntsDataModel = GenericDataModel & Record<string, GenericEntModel>; | ||
@@ -344,3 +333,3 @@ type GenericEntModel = { | ||
type EntDataModelFromSchema<SchemaDef extends SchemaDefinition<any, boolean>> = DataModelFromSchemaDefinition<SchemaDef> & { | ||
[TableName in keyof SchemaDef["tables"] & string]: SchemaDef["tables"][TableName] extends EntDefinition<any, any, any, any, any, infer Edges> ? { | ||
[TableName in keyof SchemaDef["tables"] & string]: SchemaDef["tables"][TableName] extends EntDefinition<any, any, any, any, infer Edges> ? { | ||
edges: Edges; | ||
@@ -347,0 +336,0 @@ } : never; |
@@ -189,8 +189,4 @@ "use strict"; | ||
function defineEntFromTable(definition) { | ||
const validator = definition.documentType; | ||
const validatorJson = ( | ||
// @ts-expect-error Private field | ||
validator.json | ||
); | ||
if (validatorJson.type !== "object") { | ||
const validator = definition.validator; | ||
if (validator.kind !== "object") { | ||
throw new Error( | ||
@@ -200,10 +196,3 @@ "Only tables with object definition are supported in Ents, not unions" | ||
} | ||
const tableSchema = Object.fromEntries( | ||
Object.entries(validatorJson.value).map(([k, v2]) => [ | ||
k, | ||
// @ts-expect-error Private constructor | ||
new import_values.Validator(v2.fieldType, v2.optional) | ||
]) | ||
); | ||
const entDefinition = defineEnt(tableSchema); | ||
const entDefinition = defineEnt(validator.fields); | ||
entDefinition.indexes = definition.indexes; | ||
@@ -215,2 +204,3 @@ entDefinition.searchIndexes = definition.searchIndexes; | ||
var EntDefinitionImpl = class { | ||
validator; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
@@ -232,2 +222,3 @@ // @ts-ignore | ||
this.documentSchema = documentSchema; | ||
this.validator = import_values.v.object(documentSchema); | ||
} | ||
@@ -234,0 +225,0 @@ index(name, fields) { |
{ | ||
"name": "convex-ents", | ||
"version": "0.7.7", | ||
"version": "0.8.0", | ||
"description": "Relations, default values, unique fields, RLS for Convex", | ||
@@ -31,3 +31,3 @@ "keywords": [ | ||
"peerDependencies": { | ||
"convex": "^1.11.2" | ||
"convex": "^1.13.0" | ||
}, | ||
@@ -34,0 +34,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
817680
5789