@ronin/syntax
Advanced tools
Comparing version 0.2.17 to 0.2.18-ben-ron-1099-experimental-127
@@ -1,1 +0,1 @@ | ||
export { D as DeepCallable, P as PromiseTuple, R as ResultRecord, e as SyntaxItem, f as getBatchProxy, o as getBatchProxySQL, h as getProperty, g as getSyntaxProxy, k as getSyntaxProxySQL, i as setProperty } from './primitives-DntPMz1o.js'; | ||
export { D as DeepCallable, d as PromiseTuple, R as ResultRecord, c as SyntaxItem, e as getBatchProxy, i as getBatchProxySQL, f as getProperty, g as getSyntaxProxy, h as getSyntaxProxySQL, s as setProperty } from './index-uBN48F2S.js'; |
@@ -8,3 +8,3 @@ import { | ||
setProperty | ||
} from "./chunk-JPFVRJ3Z.js"; | ||
} from "./chunk-IPGTH7OQ.js"; | ||
export { | ||
@@ -11,0 +11,0 @@ getBatchProxy, |
@@ -1,4 +0,329 @@ | ||
import { Q as QUERY_SYMBOLS } from './primitives-DntPMz1o.js'; | ||
export { C as Chain, F as FieldOutput, M as Model, a as ModelFieldExpressions, S as SyntaxField, c as blob, b as boolean, d as date, j as json, l as link, m as model, n as number, s as string } from './primitives-DntPMz1o.js'; | ||
import { P as PublicModel, M as ModelIndex, a as ModelField, b as ModelTrigger, G as GetInstructions, W as WithInstruction, S as StoredObject, c as SyntaxItem, Q as QUERY_SYMBOLS } from './index-uBN48F2S.js'; | ||
interface RoninFields { | ||
/** | ||
* The unique identifier for this record. | ||
*/ | ||
id: string; | ||
/** | ||
* Contains metadata about the record like creation date, last update, etc. | ||
*/ | ||
ronin: string; | ||
} | ||
type Primitives = ReturnType<typeof link> | ReturnType<typeof string> | ReturnType<typeof boolean> | ReturnType<typeof number> | ReturnType<typeof json> | ReturnType<typeof date> | ReturnType<typeof blob> | NestedFieldsPrimitives | Chain<FieldOutput<'string' | 'number' | 'boolean' | 'link' | 'json' | 'date' | 'blob'>, 'name' | 'displayAs' | 'unique' | 'required' | 'defaultValue' | 'computedAs' | 'check'>; | ||
interface NestedFieldsPrimitives { | ||
[key: string]: Primitives; | ||
} | ||
interface Model<Fields = RecordWithoutForbiddenKeys<Primitives>> extends Omit<PublicModel, 'fields' | 'indexes' | 'triggers' | 'presets'> { | ||
/** | ||
* The fields that make up this model. | ||
*/ | ||
fields?: Fields; | ||
/** | ||
* Database indexes to optimize query performance. | ||
*/ | ||
indexes?: Record<string, ModelIndex<Array<ModelField & { | ||
slug: keyof Fields; | ||
}>>>; | ||
/** | ||
* Queries that run automatically in response to other queries. | ||
*/ | ||
triggers?: Record<string, ModelTrigger<Array<ModelField & { | ||
slug: keyof Fields; | ||
}>>>; | ||
/** | ||
* Predefined query instructions that can be reused across multiple different queries. | ||
*/ | ||
presets?: Record<string, GetInstructions | WithInstruction> | ((fields: keyof Fields) => Record<string, GetInstructions | WithInstruction>); | ||
} | ||
type FieldToTypeMap = { | ||
blob: StoredObject; | ||
boolean: boolean; | ||
date: Date; | ||
json: object; | ||
link: string; | ||
number: number; | ||
string: string; | ||
}; | ||
type FieldsToTypes<F> = F extends Record<string, Primitives> ? { | ||
[K in keyof F]: F[K] extends Record<string, Primitives> ? FieldsToTypes<F[K]> : F[K]['type'] extends keyof FieldToTypeMap ? F[K]['required'] extends true ? FieldToTypeMap[F[K]['type']] : FieldToTypeMap[F[K]['type']] | null : object; | ||
} : RoninFields; | ||
type ForbiddenKeys = 'id' | 'ronin' | 'ronin.updatedAt' | 'ronin.createdBy' | 'ronin.updatedBy' | 'ronin.createdAt' | 'ronin.locked'; | ||
type RecordWithoutForbiddenKeys<V> = { | ||
[K in Exclude<string, ForbiddenKeys>]: V; | ||
} & { | ||
[K in ForbiddenKeys]?: never; | ||
}; | ||
type Expand<T> = T extends infer O ? { | ||
[K in keyof O]: O[K]; | ||
} : never; | ||
/** | ||
* Generates a model definition and adds default fields to the provided model. | ||
* | ||
* @example | ||
* ```ts | ||
* const Account = model({ | ||
* slug: 'account', | ||
* pluralSlug: 'accounts', | ||
* fields: { | ||
* name: string() | ||
* }, | ||
* }); | ||
* ``` | ||
* | ||
* @template T - A generic type representing the model structure, which contains a slug | ||
* and fields. | ||
* @param model - An object containing the slug and fields of the model. | ||
* | ||
* @returns The generated model definition. | ||
*/ | ||
declare const model: <Fields extends RecordWithoutForbiddenKeys<Primitives>>(model: Model<Fields> | (() => Model<Fields>)) => Expand<RoninFields & FieldsToTypes<Fields>>; | ||
/** A utility type that maps an attribute's type to a function signature. */ | ||
type AttributeSignature<T, Attribute> = T extends boolean ? () => any : Attribute extends keyof Omit<ModelFieldExpressions<T>, 'type' | 'slug'> ? ModelFieldExpressions<T>[Attribute] : never; | ||
/** | ||
* Represents a chain of field attributes in the form of a function chain. | ||
* | ||
* - `Attrs`: The interface describing your attributes (e.g., { required: boolean; }). | ||
* - `Used`: A union of the keys already used in the chain. | ||
* | ||
* For each attribute key `K` not in `Used`, create a method using the signature derived | ||
* from that attribute's type. Calling it returns a new `Chain` marking `K` as used. | ||
*/ | ||
type Chain<Attrs, Used extends keyof Attrs = never> = { | ||
[K in Exclude<keyof Attrs, Used | 'type'>]: (...args: Array<AttributeSignature<TypeToTSType<Attrs['type']>, K>>) => Chain<Attrs, Used | K>; | ||
} & ('type' extends keyof Attrs ? { | ||
readonly required: boolean; | ||
readonly type: Attrs['type']; | ||
} : object); | ||
type TypeToTSType<Type> = Type extends 'string' ? string : Type extends 'number' ? number : Type extends 'boolean' ? boolean : Type extends 'blob' ? Blob : Type extends 'date' ? Date : object; | ||
type FieldOutput<Type extends ModelField['type']> = Omit<Extract<ModelField & ModelFieldExpressions<TypeToTSType<Type>>, { | ||
type: Type; | ||
}>, 'slug'>; | ||
type ModelFieldExpressions<Type> = { | ||
check?: (fields: Record<string, Type>) => Type; | ||
computedAs?: (fields: Record<string, Type>) => { | ||
value: Type; | ||
kind: 'VIRTUAL' | 'STORED'; | ||
}; | ||
defaultValue?: (() => Type) | Type; | ||
}; | ||
type SyntaxField<Type extends ModelField['type']> = SyntaxItem<FieldOutput<Type>> & any; | ||
/** | ||
* Creates a string field definition returning an object that includes the field type | ||
* ("string") and attributes. | ||
* | ||
* @example | ||
* ```ts | ||
* import { model, string } from '@ronin/syntax/schema'; | ||
* | ||
* const User = model({ | ||
* slug: 'user', | ||
* | ||
* fields: { | ||
* name: string(), | ||
* email: string({ required: true, unique: true }) | ||
* }, | ||
* }); | ||
* ``` | ||
* | ||
* @param attributes - Optional field attributes. | ||
* | ||
* @returns A field of type "string" with the specified attributes. | ||
*/ | ||
declare const string: (initialAttributes?: Partial<Omit<ModelField, keyof ModelFieldExpressions<TypeToTSType<Type>>> & { | ||
type: "link"; | ||
target: string; | ||
kind?: "one" | "many"; | ||
actions?: { | ||
onDelete?: "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION"; | ||
onUpdate?: "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION"; | ||
}; | ||
} & ModelFieldExpressions<string>>) => Chain<FieldOutput<"string">, never>; | ||
/** | ||
* Creates a number field definition returning an object that includes the field type | ||
* ("number") and attributes. | ||
* | ||
* @example | ||
* ```ts | ||
* import { model, number } from '@ronin/syntax/schema'; | ||
* | ||
* const User = model({ | ||
* slug: 'user', | ||
* | ||
* fields: { | ||
* points: number(), | ||
* numReferrals: number({ defaultValue: 0 }) | ||
* }, | ||
* }); | ||
* ``` | ||
* | ||
* @param attributes - Optional field attributes. | ||
* | ||
* @returns A field of type "number" with the specified attributes. | ||
*/ | ||
declare const number: (initialAttributes?: Partial<Omit<ModelField, keyof ModelFieldExpressions<TypeToTSType<Type>>> & { | ||
type: "link"; | ||
target: string; | ||
kind?: "one" | "many"; | ||
actions?: { | ||
onDelete?: "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION"; | ||
onUpdate?: "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION"; | ||
}; | ||
} & ModelFieldExpressions<number>>) => Chain<FieldOutput<"number">, never>; | ||
/** | ||
* Creates a link field definition returning an object that includes the field type | ||
* ("link") and attributes. | ||
* | ||
* @example | ||
* ```ts | ||
* import { model, link } from '@ronin/syntax/schema'; | ||
* | ||
* const User = model({ | ||
* slug: 'user', | ||
* | ||
* fields: { | ||
* account: link({ target: 'account' }), | ||
* posts: link({ target: 'post', kind: 'many', required: true }) | ||
* }, | ||
* }); | ||
* ``` | ||
* | ||
* @param attributes - Optional field attributes. | ||
* | ||
* @returns A field of type "link" with the specified attributes. | ||
*/ | ||
declare const link: (initialAttributes?: Partial<Omit<ModelField, keyof ModelFieldExpressions<TypeToTSType<Type>>> & { | ||
type: "link"; | ||
target: string; | ||
kind?: "one" | "many"; | ||
actions?: { | ||
onDelete?: "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION"; | ||
onUpdate?: "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION"; | ||
}; | ||
} & ModelFieldExpressions<object>>) => Chain<FieldOutput<"link">, never>; | ||
/** | ||
* Creates a JSON field definition returning an object that includes the field type | ||
* ("json") and attributes. | ||
* | ||
* @example | ||
* ```ts | ||
* import { model, json } from '@ronin/syntax/schema'; | ||
* | ||
* const User = model({ | ||
* slug: 'user', | ||
* | ||
* fields: { | ||
* settings: json() | ||
* }, | ||
* }); | ||
* ``` | ||
* | ||
* @param attributes - Optional field attributes. | ||
* | ||
* @returns A field of type "json" with the specified attributes. | ||
*/ | ||
declare const json: (initialAttributes?: Partial<Omit<ModelField, keyof ModelFieldExpressions<TypeToTSType<Type>>> & { | ||
type: "link"; | ||
target: string; | ||
kind?: "one" | "many"; | ||
actions?: { | ||
onDelete?: "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION"; | ||
onUpdate?: "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION"; | ||
}; | ||
} & ModelFieldExpressions<object>>) => Chain<FieldOutput<"json">, never>; | ||
/** | ||
* Creates a date field definition returning an object that includes the field type | ||
* ("date") and attributes. | ||
* | ||
* @example | ||
* ```ts | ||
* import { model, date } from '@ronin/syntax/schema'; | ||
* | ||
* const User = model({ | ||
* slug: 'user', | ||
* | ||
* fields: { | ||
* lastActiveAt: date(), | ||
* deactivatedAt: date({ defaultValue: null }) | ||
* }, | ||
* }); | ||
* ``` | ||
* | ||
* @param attributes - Optional field attributes. | ||
* | ||
* @returns A field of type "date" with the specified attributes. | ||
*/ | ||
declare const date: (initialAttributes?: Partial<Omit<ModelField, keyof ModelFieldExpressions<TypeToTSType<Type>>> & { | ||
type: "link"; | ||
target: string; | ||
kind?: "one" | "many"; | ||
actions?: { | ||
onDelete?: "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION"; | ||
onUpdate?: "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION"; | ||
}; | ||
} & ModelFieldExpressions<Date>>) => Chain<FieldOutput<"date">, never>; | ||
/** | ||
* Creates a boolean field definition returning an object that includes the field type | ||
* ("boolean") and attributes. | ||
* | ||
* @example | ||
* ```ts | ||
* import { model, boolean } from '@ronin/syntax/schema'; | ||
* | ||
* const User = model({ | ||
* slug: 'user', | ||
* | ||
* fields: { | ||
* earlyAccess: boolean(), | ||
* isVerified: boolean({ defaultValue: false, required: true }) | ||
* }, | ||
* }); | ||
* ``` | ||
* | ||
* @param attributes - Optional field attributes. | ||
* | ||
* @returns A field of type "boolean" with the specified attributes. | ||
*/ | ||
declare const boolean: (initialAttributes?: Partial<Omit<ModelField, keyof ModelFieldExpressions<TypeToTSType<Type>>> & { | ||
type: "link"; | ||
target: string; | ||
kind?: "one" | "many"; | ||
actions?: { | ||
onDelete?: "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION"; | ||
onUpdate?: "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION"; | ||
}; | ||
} & ModelFieldExpressions<boolean>>) => Chain<FieldOutput<"boolean">, never>; | ||
/** | ||
* Creates a blob field definition returning an object that includes the field type | ||
* ("blob") and attributes. | ||
* | ||
* @example | ||
* ```ts | ||
* import { model, blob } from '@ronin/syntax/schema'; | ||
* | ||
* const User = model({ | ||
* slug: 'user', | ||
* | ||
* fields: { | ||
* avatar: blob(), | ||
* contents: blob({ required: true }) | ||
* }, | ||
* }); | ||
* ``` | ||
* | ||
* @param attributes - Optional field attributes. | ||
* | ||
* @returns A field of type "blob" with the specified attributes. | ||
*/ | ||
declare const blob: (initialAttributes?: Partial<Omit<ModelField, keyof ModelFieldExpressions<TypeToTSType<Type>>> & { | ||
type: "link"; | ||
target: string; | ||
kind?: "one" | "many"; | ||
actions?: { | ||
onDelete?: "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION"; | ||
onUpdate?: "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION"; | ||
}; | ||
} & ModelFieldExpressions<Blob>>) => Chain<FieldOutput<"blob">, never>; | ||
/** Valid operators for string concatenation */ | ||
@@ -95,3 +420,11 @@ type StringOperator = '||'; | ||
declare const json_insert: (json: string, path: string, value: string) => string; | ||
/** | ||
* Concatenates a list of strings together. | ||
* | ||
* @param values - The list of strings to concatenate. | ||
* | ||
* @returns An expression representing the concatenated string. | ||
*/ | ||
declare const concat: (...values: Array<string | unknown | Record<typeof QUERY_SYMBOLS.EXPRESSION, string>>) => string; | ||
export { abs, json_insert, json_patch, json_replace, json_set, op, random, sql, strftime }; | ||
export { type Chain, type FieldOutput, type Model, type ModelFieldExpressions, type SyntaxField, abs, blob, boolean, concat, date, json, json_insert, json_patch, json_replace, json_set, link, model, number, op, random, sql, strftime, string }; |
import { | ||
abs, | ||
blob, | ||
boolean, | ||
date, | ||
json, | ||
json_insert, | ||
json_patch, | ||
json_replace, | ||
json_set, | ||
link, | ||
model, | ||
number, | ||
op, | ||
random, | ||
sql, | ||
strftime, | ||
string | ||
} from "./chunk-JPFVRJ3Z.js"; | ||
QUERY_SYMBOLS, | ||
getQuerySymbol, | ||
getSyntaxProxy | ||
} from "./chunk-IPGTH7OQ.js"; | ||
// src/schema/model.ts | ||
var model = (model2) => { | ||
return getSyntaxProxy({ modelType: true })(model2); | ||
}; | ||
// src/schema/primitives.ts | ||
var primitive = (type) => { | ||
return (initialAttributes = {}) => { | ||
return getSyntaxProxy({ propertyValue: true })({ | ||
...initialAttributes, | ||
type | ||
}); | ||
}; | ||
}; | ||
var string = primitive("string"); | ||
var number = primitive("number"); | ||
var link = primitive("link"); | ||
var json = primitive("json"); | ||
var date = primitive("date"); | ||
var boolean = primitive("boolean"); | ||
var blob = primitive("blob"); | ||
// src/helpers/expressions.ts | ||
var expression = (expression2) => { | ||
return { [QUERY_SYMBOLS.EXPRESSION]: expression2 }; | ||
}; | ||
var op = (left, operator, right) => { | ||
let leftValue = left; | ||
if (typeof left === "object" && QUERY_SYMBOLS.EXPRESSION in left) { | ||
leftValue = left[QUERY_SYMBOLS.EXPRESSION]; | ||
} | ||
let rightValue = right; | ||
if (typeof right === "object" && QUERY_SYMBOLS.EXPRESSION in right) { | ||
rightValue = right[QUERY_SYMBOLS.EXPRESSION]; | ||
} | ||
let wrappedLeft = leftValue; | ||
if (typeof leftValue === "string" && !(typeof left === "object" && (QUERY_SYMBOLS.EXPRESSION in left || QUERY_SYMBOLS.FIELD in left))) { | ||
wrappedLeft = `'${leftValue}'`; | ||
} | ||
let wrappedRight = rightValue; | ||
if (typeof rightValue === "string" && !(typeof right === "object" && (QUERY_SYMBOLS.EXPRESSION in right || QUERY_SYMBOLS.FIELD in right))) { | ||
wrappedRight = `'${rightValue}'`; | ||
} | ||
return expression(`(${wrappedLeft} ${operator} ${wrappedRight})`); | ||
}; | ||
// src/helpers/functions.ts | ||
var sql = (expressions) => { | ||
return expression(expressions); | ||
}; | ||
var random = () => { | ||
return expression("random()"); | ||
}; | ||
var abs = (value) => { | ||
const valueExpression = typeof value === "object" && QUERY_SYMBOLS.EXPRESSION in value ? value[QUERY_SYMBOLS.EXPRESSION] : value; | ||
return expression(`abs(${valueExpression})`); | ||
}; | ||
var strftime = (format, timestamp) => { | ||
return expression(`strftime('${format}', '${timestamp}')`); | ||
}; | ||
var json_patch = (patch, input) => { | ||
return expression(`json_patch('${patch}', '${input}')`); | ||
}; | ||
var json_set = (json2, path, value) => { | ||
return expression(`json_set('${json2}', '${path}', '${value}')`); | ||
}; | ||
var json_replace = (json2, path, value) => { | ||
return expression( | ||
`json_replace('${json2}', '${path}', '${value}')` | ||
); | ||
}; | ||
var json_insert = (json2, path, value) => { | ||
return expression(`json_insert('${json2}', '${path}', '${value}')`); | ||
}; | ||
var concat = (...values) => { | ||
const formattedValues = values.map((value) => { | ||
const symbol = getQuerySymbol(value); | ||
return symbol?.type === "expression" ? symbol.value : `'${value}'`; | ||
}); | ||
return expression(`concat(${formattedValues.join(", ")})`); | ||
}; | ||
export { | ||
@@ -24,2 +92,3 @@ abs, | ||
boolean, | ||
concat, | ||
date, | ||
@@ -26,0 +95,0 @@ json, |
{ | ||
"name": "@ronin/syntax", | ||
"version": "0.2.17", | ||
"version": "0.2.18-ben-ron-1099-experimental-127", | ||
"type": "module", | ||
@@ -44,3 +44,3 @@ "description": "Allows for defining RONIN queries and schemas in code.", | ||
"@biomejs/biome": "1.9.4", | ||
"@ronin/compiler": "0.14.14", | ||
"@ronin/compiler": "0.16.3", | ||
"@types/bun": "1.2.1", | ||
@@ -47,0 +47,0 @@ "tsup": "8.3.6", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
68423
1619