MCPcopy
hub / github.com/pocketbase/pocketbase / jsDiffTemplate

Method jsDiffTemplate

plugins/migratecmd/templates.go:132–295  ·  view source on GitHub ↗
(new *core.Collection, old *core.Collection)

Source from the content-addressed store, hash-verified

130}
131
132func (p *plugin) jsDiffTemplate(new *core.Collection, old *core.Collection) (string, error) {
133 if new == nil && old == nil {
134 return "", errors.New("the diff template require at least one of the collection to be non-nil")
135 }
136
137 if new == nil {
138 return p.jsDeleteTemplate(old)
139 }
140
141 if old == nil {
142 return p.jsCreateTemplate(new)
143 }
144
145 upParts := []string{}
146 downParts := []string{}
147 varName := "collection"
148
149 newMap, err := toMap(new)
150 if err != nil {
151 return "", err
152 }
153
154 oldMap, err := toMap(old)
155 if err != nil {
156 return "", err
157 }
158
159 // non-fields
160 // -----------------------------------------------------------------
161
162 upDiff := diffMaps(oldMap, newMap, "fields", "created", "updated")
163 if len(upDiff) > 0 {
164 downDiff := diffMaps(newMap, oldMap, "fields", "created", "updated")
165
166 rawUpDiff, err := marhshalWithoutEscape(upDiff, " ", " ")
167 if err != nil {
168 return "", err
169 }
170
171 rawDownDiff, err := marhshalWithoutEscape(downDiff, " ", " ")
172 if err != nil {
173 return "", err
174 }
175
176 upParts = append(upParts, "// update collection data")
177 upParts = append(upParts, fmt.Sprintf("unmarshal(%s, %s)", string(rawUpDiff), varName)+"\n")
178 // ---
179 downParts = append(downParts, "// update collection data")
180 downParts = append(downParts, fmt.Sprintf("unmarshal(%s, %s)", string(rawDownDiff), varName)+"\n")
181 }
182
183 // fields
184 // -----------------------------------------------------------------
185
186 oldFieldsSlice, ok := oldMap["fields"].([]any)
187 if !ok {
188 return "", errors.New(`oldMap["fields"] is not []any`)
189 }

Callers 1

Calls 8

jsDeleteTemplateMethod · 0.95
jsCreateTemplateMethod · 0.95
toMapFunction · 0.85
diffMapsFunction · 0.85
marhshalWithoutEscapeFunction · 0.85
GetByIdMethod · 0.80
GetIdMethod · 0.65
EqualMethod · 0.65

Tested by

no test coverage detected