{"version":3,"file":"session.cjs","names":["SQLiteAsyncSession","entityKind","NoopLogger","SQLiteAsyncPreparedQuery","sql","SQLiteAsyncTransaction"],"sources":["../../src/expo-sqlite/session.ts"],"sourcesContent":["import type { SQLiteDatabase, SQLiteRunResult } from 'expo-sqlite';\nimport { entityKind } from '~/entity.ts';\nimport type { Logger } from '~/logger.ts';\nimport { NoopLogger } from '~/logger.ts';\nimport type { AnyRelations } from '~/relations.ts';\nimport { type Query, sql } from '~/sql/sql.ts';\nimport {\n\tSQLiteAsyncPreparedQuery,\n\ttype SQLiteAsyncPreparedQueryConfig as PreparedQueryConfigBase,\n\tSQLiteAsyncSession,\n\tSQLiteAsyncTransaction,\n\ttype SQLiteQueryExecutors,\n} from '~/sqlite-core/async/session.ts';\nimport type { SQLiteDialect } from '~/sqlite-core/dialect.ts';\nimport type { SQLiteExecuteMethod, SQLiteTransactionConfig } from '~/sqlite-core/session.ts';\nimport type { DrizzleTypeError } from '~/utils.ts';\n\nexport interface ExpoSQLiteSessionOptions {\n\tlogger?: Logger;\n}\n\nexport type ExpoSQLiteRunResult = SQLiteRunResult;\n\ntype PreparedQueryConfig = Omit<PreparedQueryConfigBase, 'statement' | 'run'>;\n\nexport class ExpoSQLiteSession<TRelations extends AnyRelations>\n\textends SQLiteAsyncSession<'sync', ExpoSQLiteRunResult, TRelations>\n{\n\tstatic override readonly [entityKind]: string = 'ExpoSQLiteSession';\n\n\tprivate logger: Logger;\n\n\tconstructor(\n\t\tprivate client: SQLiteDatabase,\n\t\tdialect: SQLiteDialect,\n\t\tprivate relations: TRelations,\n\t\tprivate options: ExpoSQLiteSessionOptions = {},\n\t) {\n\t\tsuper(dialect, 'sync');\n\t\tthis.logger = options.logger ?? new NoopLogger();\n\t}\n\n\tprepareQuery<T extends Omit<PreparedQueryConfig, 'run'>>(\n\t\tquery: Query,\n\t\tmode: 'arrays' | 'objects' | 'raw',\n\t\t_prepare: boolean,\n\t\texecuteMethod?: SQLiteExecuteMethod,\n\t\tmapper?: (rows: any[]) => any,\n\t\tqueryMetadata?: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t},\n\t): SQLiteAsyncPreparedQuery<T & { run: ExpoSQLiteRunResult }> {\n\t\tconst executors: SQLiteQueryExecutors<'sync'> = {\n\t\t\tall: (params) => {\n\t\t\t\tconst stmt = this.client.prepareSync(query.sql);\n\t\t\t\ttry {\n\t\t\t\t\treturn mode === 'arrays'\n\t\t\t\t\t\t? stmt.executeForRawResultSync(params as any[]).getAllSync()\n\t\t\t\t\t\t: stmt.executeSync(params as any[]).getAllSync();\n\t\t\t\t} finally {\n\t\t\t\t\tstmt.finalizeSync();\n\t\t\t\t}\n\t\t\t},\n\t\t\tget: (params) => {\n\t\t\t\tconst stmt = this.client.prepareSync(query.sql);\n\t\t\t\ttry {\n\t\t\t\t\treturn mode === 'arrays'\n\t\t\t\t\t\t? stmt.executeForRawResultSync(params as any[]).getFirstSync()\n\t\t\t\t\t\t: stmt.executeSync(params as any[]).getFirstSync();\n\t\t\t\t} finally {\n\t\t\t\t\tstmt.finalizeSync();\n\t\t\t\t}\n\t\t\t},\n\t\t\trun: (params) => {\n\t\t\t\tconst stmt = this.client.prepareSync(query.sql);\n\t\t\t\ttry {\n\t\t\t\t\tconst res = stmt.executeSync(params as any[]);\n\t\t\t\t\treturn {\n\t\t\t\t\t\tchanges: res.changes,\n\t\t\t\t\t\tlastInsertRowId: res.lastInsertRowId,\n\t\t\t\t\t};\n\t\t\t\t} finally {\n\t\t\t\t\tstmt.finalizeSync();\n\t\t\t\t}\n\t\t\t},\n\t\t\tvalues: (params) => {\n\t\t\t\tconst stmt = this.client.prepareSync(query.sql);\n\t\t\t\ttry {\n\t\t\t\t\treturn stmt.executeForRawResultSync(params as any[]).getAllSync();\n\t\t\t\t} finally {\n\t\t\t\t\tstmt.finalizeSync();\n\t\t\t\t}\n\t\t\t},\n\t\t};\n\n\t\treturn new SQLiteAsyncPreparedQuery(\n\t\t\t'sync',\n\t\t\texecuteMethod,\n\t\t\texecutors,\n\t\t\tquery,\n\t\t\tmapper,\n\t\t\tmode,\n\t\t\tthis.logger,\n\t\t\tundefined,\n\t\t\tqueryMetadata,\n\t\t\tundefined,\n\t\t);\n\t}\n\n\toverride transaction<T>(\n\t\ttransaction: (tx: ExpoSQLiteTransaction<TRelations>) => T,\n\t\tconfig: SQLiteTransactionConfig = {},\n\t): T {\n\t\tif (config?.behavior === 'concurrent') throw new Error('Concurrent transactions are not supported by driver');\n\n\t\tconst tx = new ExpoSQLiteTransaction('sync', this.dialect, this, this.relations);\n\t\tthis.run(sql.raw(`begin${config?.behavior ? ' ' + config.behavior : ''}`));\n\t\ttry {\n\t\t\tconst result = transaction(tx);\n\t\t\tthis.run(sql`commit`);\n\t\t\treturn result;\n\t\t} catch (err) {\n\t\t\tthis.run(sql`rollback`);\n\t\t\tthrow err;\n\t\t}\n\t}\n}\n\nexport class ExpoSQLiteTransaction<\n\tTRelations extends AnyRelations,\n> extends SQLiteAsyncTransaction<'sync', ExpoSQLiteRunResult, TRelations> {\n\tstatic override readonly [entityKind]: string = 'ExpoSQLiteTransaction';\n\n\toverride transaction<T>(\n\t\ttransaction: (\n\t\t\ttx: ExpoSQLiteTransaction<TRelations>,\n\t\t) => T extends Promise<any> ? DrizzleTypeError<\"Sync drivers can't use async functions in transactions!\">\n\t\t\t: T,\n\t): T {\n\t\tconst savepointName = `sp${this.nestedIndex}`;\n\t\tconst tx = new ExpoSQLiteTransaction(\n\t\t\t'sync',\n\t\t\tthis.dialect,\n\t\t\tthis.session,\n\t\t\tthis._.relations,\n\t\t\tthis.nestedIndex + 1,\n\t\t);\n\t\tthis.session.run(sql.raw(`savepoint ${savepointName}`));\n\t\ttry {\n\t\t\tconst result = transaction(tx);\n\t\t\tthis.session.run(sql.raw(`release savepoint ${savepointName}`));\n\t\t\treturn result as T;\n\t\t} catch (err) {\n\t\t\tthis.session.run(sql.raw(`rollback to savepoint ${savepointName}`));\n\t\t\tthrow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