-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
156 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { LibAdapters, IMemoryDb, NotSupported, QueryResult } from '../interfaces.ts'; | ||
import { LibAdapters, IMemoryDb, NotSupported, QueryResult, SlonikAdapterOptions } from '../interfaces.ts'; | ||
import lru from 'https://deno.land/x/[email protected]/mod.ts'; | ||
import { compareVersions } from '../utils.ts'; | ||
import { toLiteral } from '../misc/pg-utils.ts'; | ||
|
@@ -24,7 +24,7 @@ function timeoutOrImmediate(fn: () => void, time: number) { | |
|
||
const delay = (time: number | undefined) => new Promise<void>(done => timeoutOrImmediate(done, time ?? 0)); | ||
|
||
function replaceQueryArgs$(this: void, sql: string, values: any[]) { | ||
export function replaceQueryArgs$(this: void, sql: string, values: any[]) { | ||
return sql.replace(/\$(\d+)/g, (str: any, istr: any) => { | ||
const i = Number.parseInt(istr); | ||
if (i > values.length) { | ||
|
@@ -210,16 +210,82 @@ export class Adapters implements LibAdapters { | |
return created; | ||
} | ||
|
||
createSlonik(queryLatency?: number) { | ||
const { createMockPool, createMockQueryResult } = __non_webpack_require__('slonik'); | ||
return createMockPool({ | ||
query: async (sql: string, args: any[]) => { | ||
await delay(queryLatency ?? 0); | ||
const formatted = replaceQueryArgs$(sql, args); | ||
const ret = this.db.public.many(formatted); | ||
return createMockQueryResult(ret); | ||
}, | ||
}); | ||
createSlonik(opts?: SlonikAdapterOptions) { | ||
const slonik = __non_webpack_require__('slonik'); | ||
if (typeof slonik.createMockPool !== 'function') { | ||
// see https://github.com/gajus/slonik/blob/main/packages/slonik/src/factories/createPool.ts | ||
// and @slonik/pg-driver https://github.com/gajus/slonik/blob/main/packages/pg-driver/src/factories/createPgDriverFactory.ts | ||
const { createDriverFactory } = __non_webpack_require__('@slonik/driver'); | ||
const createResultParserInterceptor = () => { | ||
return { | ||
transformRow: async (executionContext: any, actualQuery: any, row: any) => { | ||
const { resultParser } = executionContext; | ||
if (!resultParser) { | ||
return row; | ||
} | ||
// It is recommended (but not required) to parse async to avoid blocking the event loop during validation | ||
const validationResult = await resultParser.safeParseAsync(row); | ||
|
||
if (!validationResult.success) { | ||
throw new slonik.SchemaValidationError( | ||
actualQuery, | ||
row, | ||
validationResult.error.issues | ||
); | ||
} | ||
|
||
return validationResult.data; | ||
}, | ||
}; | ||
}; | ||
return slonik.createPool('', { | ||
...opts?.createPoolOptions ?? {}, | ||
...opts?.zodValidation ? { interceptors: [createResultParserInterceptor(), ...opts?.createPoolOptions?.interceptors ?? []] } : {}, | ||
// driverFactory: factory, | ||
driverFactory: createDriverFactory(() => { | ||
let connected = false; | ||
return { | ||
createPoolClient: async () => { | ||
return { | ||
connect: async () => { | ||
await delay(opts?.queryLatency ?? 0); | ||
connected = true; | ||
}, | ||
end: async () => { | ||
await delay(opts?.queryLatency ?? 0); | ||
connected = false; | ||
}, | ||
query: async (sql: string, args: any[]) => { | ||
if (!connected) { | ||
throw new Error('your fake pg pool is not connected'); | ||
} | ||
if (sql === 'DISCARD ALL') { | ||
return { rows: [] }; | ||
} | ||
await delay(opts?.queryLatency ?? 0); | ||
const formatted = replaceQueryArgs$(sql, args); | ||
const result = this.db.public.query(formatted); | ||
return result; | ||
}, | ||
stream: (sql: any, values: any) => { | ||
throw new Error('pg-mem does not support streaming'); | ||
}, | ||
} | ||
}, | ||
} | ||
}) | ||
}) | ||
} else { | ||
// old slonik versions | ||
return slonik.createMockPool({ | ||
query: async (sql: string, args: any[]) => { | ||
await delay(opts?.queryLatency ?? 0); | ||
const formatted = replaceQueryArgs$(sql, args); | ||
const ret = this.db.public.many(formatted); | ||
return slonik.createMockQueryResult(ret); | ||
}, | ||
}, opts?.clientConfigurationInput); | ||
} | ||
} | ||
|
||
|
||
|
@@ -347,6 +413,12 @@ export class Adapters implements LibAdapters { | |
return that.createKnex(); | ||
} | ||
|
||
async execute(query: string, params: any[] = [], method: 'all' | 'get' | 'run' = 'all'): Promise<any> { | ||
// pg-mem does not support `set names 'utf8';` yet | ||
// the hack in line 355 works only for a single query while this one works beforehand to strip it from multiline queries | ||
return super.execute(query.replace('set names \'utf8\';', ''), params, method); | ||
} | ||
|
||
} | ||
// see https://github.com/mikro-orm/mikro-orm/blob/master/packages/postgresql/src/PostgreSqlDriver.ts | ||
class PgMemDriver extends AbstractSqlDriver<PgMemConnection> { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { DataType, FunctionDefinition } from '../interfaces.ts'; | ||
|
||
export const numberFunctions: FunctionDefinition[] = [ | ||
{ | ||
name: 'greatest', | ||
args: [DataType.integer], | ||
argsVariadic: DataType.integer, | ||
returns: DataType.integer, | ||
implementation: (...args: number[]) => Math.max(...args), | ||
}, | ||
{ | ||
name: 'least', | ||
args: [DataType.integer], | ||
argsVariadic: DataType.integer, | ||
returns: DataType.integer, | ||
implementation: (...args: number[]) => Math.min(...args), | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { newDb } from './db.ts'; | ||
export { enableStatementLocationTracking } from './parser/parse-cache.ts'; | ||
export { replaceQueryArgs$ } from './adapters/index.ts'; | ||
export * from './interfaces.ts'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters