@dataplan/pg
Version:
PostgreSQL step classes for Grafast
123 lines • 5.37 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.withPgClientTransaction = exports.withPgClient = exports.SideEffectWithPgClientStep = void 0;
exports.sideEffectWithPgClient = sideEffectWithPgClient;
exports.sideEffectWithPgClientTransaction = sideEffectWithPgClientTransaction;
exports.loadOneWithPgClient = loadOneWithPgClient;
exports.loadManyWithPgClient = loadManyWithPgClient;
const grafast_1 = require("grafast");
/**
* Runs the given `callback` against the given `executor` using any plan data
* from `$data` (which can be `constant(null)` if you don't need it). Typically
* useful for running custom transactions.
*/
class SideEffectWithPgClientStep extends grafast_1.Step {
static { this.$$export = {
moduleName: "@dataplan/pg",
exportName: "SideEffectWithPgClientStep",
}; }
constructor(executor, $data, callback) {
super();
this.callback = callback;
this.isSyncAndSafe = false;
this.executor = executor;
this.contextId = this.addDependency(this.executor.context());
this.dataId = this.addDependency($data);
// Be sure to set this _after_ we've added the dependencies - we don't want them to be dependent on us!
this.hasSideEffects = true;
}
execute({ indexMap, values, }) {
const contextDep = values[this.contextId];
const dataDep = values[this.dataId];
return indexMap((i) => {
const context = contextDep.at(i);
const data = dataDep.at(i);
const { withPgClient, pgSettings } = context;
return withPgClient(pgSettings, (client) => this.callback(client, data));
});
}
}
exports.SideEffectWithPgClientStep = SideEffectWithPgClientStep;
function sideEffectWithPgClient(executor, $data, callback) {
return new SideEffectWithPgClientStep(executor, $data ?? (0, grafast_1.constant)($data), callback);
}
function sideEffectWithPgClientTransaction(executor, $data, callback) {
return sideEffectWithPgClient(executor, $data ?? (0, grafast_1.constant)($data), (client, data) => client.withTransaction((txClient) => callback(txClient, data)));
}
/** @deprecated Use `sideEffectWithPgClient` or `loadOneWithPgClient` or `loadManyWithPgClient` instead */
exports.withPgClient = sideEffectWithPgClient;
/** @deprecated Use `sideEffectWithPgClientTransaction` instead (or `loadOneWithPgClient`/`loadManyWithPgClient` if you're not doing a mutation) */
exports.withPgClientTransaction = sideEffectWithPgClientTransaction;
function loadOneWithPgClient(executor, lookup, loader) {
const newLoader = transformLoadOneLoader(executor, loader);
return (0, grafast_1.loadOne)(lookup, newLoader);
}
const transformedLoaderCache = new WeakMap();
// Identical, other than types, to transformLoadManyLoader
function transformLoadOneLoader(executor, loader) {
let cacheByExecutor = transformedLoaderCache.get(executor);
if (!cacheByExecutor) {
cacheByExecutor = new WeakMap();
transformedLoaderCache.set(executor, cacheByExecutor);
}
const existing = cacheByExecutor.get(loader);
if (existing) {
return existing;
}
else {
const loaderObject = typeof loader === "function"
? { load: loader, shared: undefined }
: loader;
const transformedLoader = {
...loaderObject,
shared: () => ({
...unthunk(loaderObject.shared),
pgExecutorContext: executor.context(),
}),
load(lookups, info) {
const { shared: { pgExecutorContext }, } = info;
return pgExecutorContext.withPgClient(pgExecutorContext.pgSettings, (pgClient) => Promise.resolve(loaderObject.load(pgClient, lookups, info)));
},
};
cacheByExecutor.set(loader, transformedLoader);
return transformedLoader;
}
}
// Identical, other than types, to transformLoadOneLoader
function transformLoadManyLoader(executor, loader) {
let cacheByExecutor = transformedLoaderCache.get(executor);
if (!cacheByExecutor) {
cacheByExecutor = new WeakMap();
transformedLoaderCache.set(executor, cacheByExecutor);
}
const existing = cacheByExecutor.get(loader);
if (existing) {
return existing;
}
else {
const loaderObject = typeof loader === "function"
? { load: loader, shared: undefined }
: loader;
const transformedLoader = {
...loaderObject,
shared: () => ({
...unthunk(loaderObject.shared),
pgExecutorContext: executor.context(),
}),
load(lookups, info) {
const { shared: { pgExecutorContext }, } = info;
return pgExecutorContext.withPgClient(pgExecutorContext.pgSettings, (pgClient) => Promise.resolve(loaderObject.load(pgClient, lookups, info)));
},
};
cacheByExecutor.set(loader, transformedLoader);
return transformedLoader;
}
}
function unthunk(t) {
return typeof t === "function" ? t() : t;
}
function loadManyWithPgClient(executor, lookup, loader) {
const newLoader = transformLoadManyLoader(executor, loader);
return (0, grafast_1.loadMany)(lookup, newLoader);
}
//# sourceMappingURL=withPgClient.js.map
;