11/* eslint-disable @typescript-eslint/ban-ts-comment */
22/* eslint-disable @typescript-eslint/no-explicit-any */
3- import { Expr , query as q } from "faunadb" ;
4- import { Table , FaunaSchema , RelationshipField } from "./types" ;
3+ import { Expr , ExprArg , query as q } from "faunadb" ;
4+
5+ import {
6+ Table ,
7+ FaunaSchema ,
8+ RelationshipField ,
9+ ScalarType ,
10+ ScalarField ,
11+ } from "./types" ;
512
613const generateRelationQueries = ( field : RelationshipField ) => {
714 const existingRelation = ( id : string ) =>
@@ -83,22 +90,97 @@ const generateRelationQueries = (field: RelationshipField) => {
8390 } ;
8491} ;
8592
86- export const definitions = (
87- table : Pick < Table , "apiName" | "id" >
88- ) : {
89- queries : {
93+ export const definitions : {
94+ operators : Record <
95+ string ,
96+ {
97+ allowedTypes : Array < ScalarType > ;
98+ argType : ( type : ScalarType ) => ScalarField [ "type" ] ;
99+ expr : ( fieldId : string , value : ExprArg ) => Expr ;
100+ }
101+ > ;
102+ queries : ( table : Pick < Table , "apiName" | "id" > ) => {
90103 [ p : string ] : {
91104 name : ( ) => string ;
92- // @ts -ignore
93- query : ( args : any , faunaSchema ?: FaunaSchema ) => Expr ;
105+ query : ( args : any , faunaSchema : FaunaSchema ) => Expr ;
94106 } ;
95107 } ;
96- } => ( {
97- queries : {
108+ } = {
109+ operators : {
110+ equals : {
111+ allowedTypes : [ "String" , "Number" , "Boolean" , "JSON" , "EmailAddress" ] ,
112+ argType : ( type : ScalarType ) => type ,
113+ expr : ( fieldId : string , value : ExprArg ) =>
114+ //@ts -ignore
115+ q . Equals ( q . Select ( [ "data" , fieldId ] , q . Get ( q . Var ( "ref" ) ) , null ) , value ) ,
116+ } ,
117+ notEquals : {
118+ allowedTypes : [ "String" , "Number" , "Boolean" , "JSON" , "EmailAddress" ] ,
119+ argType : ( type : ScalarType ) => type ,
120+ expr : ( fieldId : string , value : ExprArg ) =>
121+ q . Not (
122+ q . Equals (
123+ //@ts -ignore
124+ q . Select ( [ "data" , fieldId ] , q . Get ( q . Var ( "ref" ) ) , null ) ,
125+ value
126+ )
127+ ) ,
128+ } ,
129+ lt : {
130+ allowedTypes : [ "String" , "Number" , "Boolean" , "JSON" , "EmailAddress" ] ,
131+ argType : ( type : ScalarType ) => type ,
132+ expr : ( fieldId : string , value : ExprArg ) =>
133+ //@ts -ignore
134+ q . LT ( q . Select ( [ "data" , fieldId ] , q . Get ( q . Var ( "ref" ) ) , null ) , value ) ,
135+ } ,
136+ gt : {
137+ allowedTypes : [ "String" , "Number" , "Boolean" , "JSON" , "EmailAddress" ] ,
138+ argType : ( type : ScalarType ) => type ,
139+ expr : ( fieldId : string , value : ExprArg ) =>
140+ //@ts -ignore
141+ q . GT ( q . Select ( [ "data" , fieldId ] , q . Get ( q . Var ( "ref" ) ) , null ) , value ) ,
142+ } ,
143+ startsWith : {
144+ allowedTypes : [ "String" , "EmailAddress" ] ,
145+ argType : ( type : ScalarType ) => type ,
146+ expr : ( fieldId : string , value : ExprArg ) =>
147+ //@ts -ignore
148+ q . StartsWith (
149+ q . Select ( [ "data" , fieldId ] , q . Get ( q . Var ( "ref" ) ) , "" ) ,
150+ value
151+ ) ,
152+ } ,
153+ endsWith : {
154+ allowedTypes : [ "String" , "EmailAddress" ] ,
155+ argType : ( type : ScalarType ) => type ,
156+ expr : ( fieldId : string , value : ExprArg ) =>
157+ //@ts -ignore
158+ q . EndsWith ( q . Select ( [ "data" , fieldId ] , q . Get ( q . Var ( "ref" ) ) , "" ) , value ) ,
159+ } ,
160+ contains : {
161+ allowedTypes : [ "String" , "EmailAddress" ] ,
162+ argType : ( type : ScalarType ) => type ,
163+ expr : ( fieldId : string , value : ExprArg ) =>
164+ //@ts -ignore
165+ q . ContainsStr (
166+ q . Select ( [ "data" , fieldId ] , q . Get ( q . Var ( "ref" ) ) , "" ) ,
167+ value
168+ ) ,
169+ } ,
170+ } ,
171+ queries : ( table : Pick < Table , "apiName" | "id" > ) => ( {
98172 findMany : {
99173 name : ( ) : string => "query" + table . apiName ,
100174 // @ts -ignore
101- query : ( args ) : Expr => {
175+ query : (
176+ args : {
177+ first : number ;
178+ before : string ;
179+ after : string ;
180+ where : Record < string , Record < string , ExprArg > > ;
181+ } ,
182+ faunaSchema : FaunaSchema
183+ ) : Expr => {
102184 const options : { size : number ; after ?: Expr ; before ?: Expr } = {
103185 size : args . first ,
104186 } ;
@@ -108,19 +190,31 @@ export const definitions = (
108190 if ( args . before ) {
109191 options . before = q . Ref ( q . Collection ( table . id ) , args . before ) ;
110192 }
111- let filter : boolean | Expr = true ;
112- if ( args . where ?. title_eq ) {
113- filter = q . Equals (
114- args . where . title_eq ,
115- q . Select ( [ "data" , "294845251673129473" ] , q . Get ( q . Var ( "ref" ) ) )
193+ const filters : Array < boolean | Expr > = [ ] ;
194+ if ( args . where ) {
195+ for ( const [ fieldName , operators ] of Object . entries ( args . where ) ) {
196+ for ( const [ operator , value ] of Object . entries ( operators ) ) {
197+ filters . push (
198+ definitions . operators [ operator ] . expr (
199+ faunaSchema [ table . apiName ] . fields [ fieldName ] . id ,
200+ value
201+ )
202+ ) ;
203+ }
204+ }
205+ }
206+ if ( filters . length > 0 ) {
207+ return q . Map (
208+ q . Filter (
209+ q . Paginate ( q . Documents ( q . Collection ( table . id ) ) , { ...options } ) ,
210+ q . Lambda ( "ref" , q . And ( ...filters ) )
211+ ) ,
212+ q . Lambda ( "ref" , q . Get ( q . Var ( "ref" ) ) )
116213 ) ;
117214 }
118-
119215 return q . Map (
120- q . Filter (
121- q . Paginate ( q . Documents ( q . Collection ( table . id ) ) , { ...options } ) ,
122- q . Lambda ( "ref" , filter )
123- ) ,
216+ q . Paginate ( q . Documents ( q . Collection ( table . id ) ) , { ...options } ) ,
217+
124218 q . Lambda ( "ref" , q . Get ( q . Var ( "ref" ) ) )
125219 ) ;
126220 } ,
@@ -228,5 +322,5 @@ export const definitions = (
228322 ) ;
229323 } ,
230324 } ,
231- } ,
232- } ) ;
325+ } ) ,
326+ } ;
0 commit comments