-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support refresh materialized view (#990)
* feat: add postgres range types (#1086) * feat: support refresh naterialized view * fix tests by adding .materialized() to remove the matview * fix failing test * fix: References typo (#1092) * chore: refresh-view-node.ts => refresh-materialized-view-node.ts * chore: export node in index.ts --------- Co-authored-by: Isak <16906089+kansson@users.noreply.github.com> Co-authored-by: Jonathan Wu <jonathanwu70@gmail.com>
- Loading branch information
1 parent
6f0bc77
commit 5d38c1b
Showing
11 changed files
with
284 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { freeze } from '../util/object-utils.js' | ||
import { OperationNode } from './operation-node.js' | ||
import { SchemableIdentifierNode } from './schemable-identifier-node.js' | ||
|
||
export type RefreshMaterializedViewNodeParams = Omit< | ||
Partial<RefreshMaterializedViewNode>, | ||
'kind' | 'name' | ||
> | ||
|
||
export interface RefreshMaterializedViewNode extends OperationNode { | ||
readonly kind: 'RefreshMaterializedViewNode' | ||
readonly name: SchemableIdentifierNode | ||
readonly concurrently?: boolean | ||
readonly withNoData?: boolean | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const RefreshMaterializedViewNode = freeze({ | ||
is(node: OperationNode): node is RefreshMaterializedViewNode { | ||
return node.kind === 'RefreshMaterializedViewNode' | ||
}, | ||
|
||
create(name: string): RefreshMaterializedViewNode { | ||
return freeze({ | ||
kind: 'RefreshMaterializedViewNode', | ||
name: SchemableIdentifierNode.create(name), | ||
}) | ||
}, | ||
|
||
cloneWith( | ||
createView: RefreshMaterializedViewNode, | ||
params: RefreshMaterializedViewNodeParams, | ||
): RefreshMaterializedViewNode { | ||
return freeze({ | ||
...createView, | ||
...params, | ||
}) | ||
}, | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { OperationNodeSource } from '../operation-node/operation-node-source.js' | ||
import { CompiledQuery } from '../query-compiler/compiled-query.js' | ||
import { Compilable } from '../util/compilable.js' | ||
import { preventAwait } from '../util/prevent-await.js' | ||
import { QueryExecutor } from '../query-executor/query-executor.js' | ||
import { QueryId } from '../util/query-id.js' | ||
import { freeze } from '../util/object-utils.js' | ||
import { RefreshMaterializedViewNode } from '../operation-node/refresh-materialized-view-node.js' | ||
|
||
export class RefreshMaterializedViewBuilder implements OperationNodeSource, Compilable { | ||
readonly #props: RefreshMaterializedViewBuilderProps | ||
|
||
constructor(props: RefreshMaterializedViewBuilderProps) { | ||
this.#props = freeze(props) | ||
} | ||
|
||
/** | ||
* Adds the "concurrently" modifier. | ||
* | ||
* Use this to refresh the view without locking out concurrent selects on the materialized view. | ||
* | ||
* WARNING! | ||
* This cannot be used with the "with no data" modifier. | ||
*/ | ||
concurrently(): RefreshMaterializedViewBuilder { | ||
return new RefreshMaterializedViewBuilder({ | ||
...this.#props, | ||
node: RefreshMaterializedViewNode.cloneWith(this.#props.node, { | ||
concurrently: true, | ||
withNoData: false, | ||
}), | ||
}) | ||
} | ||
|
||
/** | ||
* Adds the "with data" modifier. | ||
* | ||
* If specified (or defaults) the backing query is executed to provide the new data, and the materialized view is left in a scannable state | ||
*/ | ||
withData(): RefreshMaterializedViewBuilder { | ||
return new RefreshMaterializedViewBuilder({ | ||
...this.#props, | ||
node: RefreshMaterializedViewNode.cloneWith(this.#props.node, { | ||
withNoData: false, | ||
}), | ||
}) | ||
} | ||
|
||
/** | ||
* Adds the "with no data" modifier. | ||
* | ||
* If specified, no new data is generated and the materialized view is left in an unscannable state. | ||
* | ||
* WARNING! | ||
* This cannot be used with the "concurrently" modifier. | ||
*/ | ||
withNoData(): RefreshMaterializedViewBuilder { | ||
return new RefreshMaterializedViewBuilder({ | ||
...this.#props, | ||
node: RefreshMaterializedViewNode.cloneWith(this.#props.node, { | ||
withNoData: true, | ||
concurrently: false, | ||
}), | ||
}) | ||
} | ||
|
||
/** | ||
* Simply calls the provided function passing `this` as the only argument. `$call` returns | ||
* what the provided function returns. | ||
*/ | ||
$call<T>(func: (qb: this) => T): T { | ||
return func(this) | ||
} | ||
|
||
toOperationNode(): RefreshMaterializedViewNode { | ||
return this.#props.executor.transformQuery( | ||
this.#props.node, | ||
this.#props.queryId, | ||
) | ||
} | ||
|
||
compile(): CompiledQuery { | ||
return this.#props.executor.compileQuery( | ||
this.toOperationNode(), | ||
this.#props.queryId, | ||
) | ||
} | ||
|
||
async execute(): Promise<void> { | ||
await this.#props.executor.executeQuery(this.compile(), this.#props.queryId) | ||
} | ||
} | ||
|
||
preventAwait( | ||
RefreshMaterializedViewBuilder, | ||
"don't await RefreshMaterializedViewBuilder instances directly. To execute the query you need to call `execute`", | ||
) | ||
|
||
export interface RefreshMaterializedViewBuilderProps { | ||
readonly queryId: QueryId | ||
readonly executor: QueryExecutor | ||
readonly node: RefreshMaterializedViewNode | ||
} |
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
Oops, something went wrong.