1- const default_reservation = null
1+ const reservations = require ( '@masthead-data/dataform-plugin' )
2+
23const RESERVATION_CONFIG = [
34 {
45 tag : 'high_slots' ,
@@ -24,61 +25,7 @@ const RESERVATION_CONFIG = [
2425 }
2526]
2627
27- const RESERVATION_SETS = RESERVATION_CONFIG . map ( cfg => ( {
28- ...cfg ,
29- set : new Set ( cfg . actions )
30- } ) )
31-
32-
33- /**
34- * Extracts action name from Dataform context object
35- * @param {Object } ctx - Dataform context object
36- * @returns {string|null } The extracted action name or null if not found
37- */
38- function getActionName ( ctx ) {
39- if ( ! ctx ) return null
40-
41- if ( typeof ctx . self === 'function' && ! ctx ?. operation ) {
42- // Try primary method: ctx.self()
43- const selfName = ctx . self ( )
44- if ( selfName ) return selfName . replace ( / ` / g, '' ) . trim ( )
45- } else if ( ctx ?. operation ?. proto ?. target ) {
46- // Fallback: construct from proto target
47- const t = ctx ?. operation ?. proto ?. target
48- return t ? [ t . database , t . name ] . join ( '.' ) : null
49- }
50-
51- return null
52- }
53-
54- /**
55- * Determines the appropriate reservation for a given action name
56- * @param {string } actionName - The action name (without backticks)
57- * @returns {string|null } The reservation identifier or null if no reservation assignment needed
58- */
59- function getReservation ( actionName ) {
60- if ( ! actionName || typeof actionName !== 'string' ) return default_reservation
61-
62- for ( const { reservation, set } of Object . values ( RESERVATION_SETS ) ) {
63- if ( set . has ( actionName ) ) {
64- return reservation
65- }
66- }
67-
68- return default_reservation
69- }
70-
71-
72- /**
73- * Generates the reservation SQL statement for a given Dataform context
74- * @param {Object } ctx - Dataform context object with self() method and/or proto.target
75- * @returns {string } The SQL statement to set reservation or empty string
76- */
77- function reservation_setter ( ctx ) {
78- const actionName = getActionName ( ctx )
79- const reservation = getReservation ( actionName )
80- return reservation ? `SET @@reservation='${ reservation } ';` : ''
81- }
28+ const reservation_setter = reservations . createReservationSetter ( RESERVATION_CONFIG )
8229
8330module . exports = {
8431 reservation_setter
0 commit comments