11// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
22
33import type { IDataEngine } from '@objectstack/core' ;
4- import { createAdapter , type CleanedWhere , type JoinConfig } from 'better-auth/adapters' ;
4+ import type { CleanedWhere } from 'better-auth/adapters' ;
55
66/**
77 * ObjectQL Adapter for better-auth
@@ -11,7 +11,7 @@ import { createAdapter, type CleanedWhere, type JoinConfig } from 'better-auth/a
1111 * third-party ORMs like drizzle-orm.
1212 *
1313 * @param dataEngine - ObjectQL data engine instance
14- * @returns better-auth Adapter instance
14+ * @returns better-auth CustomAdapter
1515 */
1616export function createObjectQLAdapter ( dataEngine : IDataEngine ) {
1717 /**
@@ -100,58 +100,54 @@ export function createObjectQLAdapter(dataEngine: IDataEngine) {
100100 return converted ;
101101 }
102102
103- return createAdapter ( {
104- id : 'objectql' ,
105-
106- async create ( { model, data, select } ) {
103+ return {
104+ create : async < T extends Record < string , any > > ( { model, data } : { model : string ; data : T ; select ?: string [ ] } ) : Promise < T > => {
107105 const objectName = toObjectName ( model ) ;
108106 const objectData = convertDataToObjectQL ( data ) ;
109107
110108 const result = await dataEngine . insert ( objectName , objectData ) ;
111- return convertDataFromObjectQL ( result ) ;
109+ return convertDataFromObjectQL ( result ) as T ;
112110 } ,
113111
114- async findOne ( { model, where, select, join } ) {
112+ findOne : async < T > ( { model, where, select } : { model : string ; where : CleanedWhere [ ] ; select ?: string [ ] ; join ?: any } ) : Promise < T | null > => {
115113 const objectName = toObjectName ( model ) ;
116114 const filter = convertWhere ( where ) ;
117115
118- const fields = select ?. map ( toFieldName ) ;
119-
120116 const result = await dataEngine . findOne ( objectName , {
121117 filter,
122- fields ,
118+ select : select ?. map ( toFieldName ) ,
123119 } ) ;
124120
125- return result ? convertDataFromObjectQL ( result ) : null ;
121+ return result ? convertDataFromObjectQL ( result ) as T : null ;
126122 } ,
127123
128- async findMany ( { model, where, limit, offset, sortBy, join } ) {
124+ findMany : async < T > ( { model, where, limit, offset, sortBy } : { model : string ; where ?: CleanedWhere [ ] ; limit : number ; offset ?: number ; sortBy ?: { field : string ; direction : 'asc' | 'desc' } ; join ?: any } ) : Promise < T [ ] > => {
129125 const objectName = toObjectName ( model ) ;
130126 const filter = where ? convertWhere ( where ) : { } ;
131127
132- const sort = sortBy ? {
128+ const sort = sortBy ? [ {
133129 field : toFieldName ( sortBy . field ) ,
134- direction : sortBy . direction ,
135- } : undefined ;
130+ order : sortBy . direction as 'asc' | 'desc' ,
131+ } ] : undefined ;
136132
137133 const results = await dataEngine . find ( objectName , {
138134 filter,
139135 limit : limit || 100 ,
140- offset,
141- sort : sort ? [ sort ] : undefined ,
136+ skip : offset ,
137+ sort,
142138 } ) ;
143139
144- return results . map ( convertDataFromObjectQL ) ;
140+ return results . map ( r => convertDataFromObjectQL ( r ) ) as T [ ] ;
145141 } ,
146142
147- async count ( { model, where } ) {
143+ count : async ( { model, where } : { model : string ; where ?: CleanedWhere [ ] } ) : Promise < number > => {
148144 const objectName = toObjectName ( model ) ;
149145 const filter = where ? convertWhere ( where ) : { } ;
150146
151147 return await dataEngine . count ( objectName , { filter } ) ;
152148 } ,
153149
154- async update ( { model, where, update } ) {
150+ update : async < T > ( { model, where, update } : { model : string ; where : CleanedWhere [ ] ; update : Record < string , any > } ) : Promise < T | null > => {
155151 const objectName = toObjectName ( model ) ;
156152 const filter = convertWhere ( where ) ;
157153 const updateData = convertDataToObjectQL ( update ) ;
@@ -167,10 +163,10 @@ export function createObjectQLAdapter(dataEngine: IDataEngine) {
167163 id : record . id ,
168164 } ) ;
169165
170- return result ? convertDataFromObjectQL ( result ) : null ;
166+ return result ? convertDataFromObjectQL ( result ) as T : null ;
171167 } ,
172168
173- async updateMany ( { model, where, update } ) {
169+ updateMany : async ( { model, where, update } : { model : string ; where : CleanedWhere [ ] ; update : Record < string , any > } ) : Promise < number > => {
174170 const objectName = toObjectName ( model ) ;
175171 const filter = convertWhere ( where ) ;
176172 const updateData = convertDataToObjectQL ( update ) ;
@@ -189,7 +185,7 @@ export function createObjectQLAdapter(dataEngine: IDataEngine) {
189185 return records . length ;
190186 } ,
191187
192- async delete ( { model, where } ) {
188+ delete : async ( { model, where } : { model : string ; where : CleanedWhere [ ] } ) : Promise < void > => {
193189 const objectName = toObjectName ( model ) ;
194190 const filter = convertWhere ( where ) ;
195191
@@ -202,7 +198,7 @@ export function createObjectQLAdapter(dataEngine: IDataEngine) {
202198 await dataEngine . delete ( objectName , { filter : { id : record . id } } ) ;
203199 } ,
204200
205- async deleteMany ( { model, where } ) {
201+ deleteMany : async ( { model, where } : { model : string ; where : CleanedWhere [ ] } ) : Promise < number > => {
206202 const objectName = toObjectName ( model ) ;
207203 const filter = convertWhere ( where ) ;
208204
@@ -216,17 +212,5 @@ export function createObjectQLAdapter(dataEngine: IDataEngine) {
216212
217213 return records . length ;
218214 } ,
219- } , {
220- // Adapter configuration
221- adapterId : 'objectql' ,
222- adapterName : 'ObjectQL' ,
223- supportsNumericIds : false ,
224- supportsUUIDs : true ,
225- supportsJSON : true ,
226- supportsDates : true ,
227- supportsBooleans : true ,
228- supportsArrays : false ,
229- // ObjectQL handles ID generation
230- disableIdGeneration : false ,
231- } ) ;
215+ } ;
232216}
0 commit comments