MCPcopy Create free account
hub / github.com/zenstackhq/zenstack / getDefaultValue

Function getDefaultValue

packages/cli/src/actions/pull/provider/postgresql.ts:224–313  ·  view source on GitHub ↗
({ defaultValue, fieldType, datatype, datatype_name, services, enums })

Source from the content-addressed store, hash-verified

222 }
223 },
224 getDefaultValue({ defaultValue, fieldType, datatype, datatype_name, services, enums }) {
225 const val = defaultValue.trim();
226
227 // Handle enum defaults (PostgreSQL returns 'value'::enum_type)
228 if (datatype === 'enum' && datatype_name) {
229 const enumDef = enums.find((e) => getDbName(e) === datatype_name);
230 if (enumDef) {
231 // Extract the enum value from the default (format: 'VALUE'::"enum_type")
232 const enumValue = val.replace(/'/g, '').split('::')[0]?.trim();
233 const enumField = enumDef.fields.find((f) => getDbName(f) === enumValue);
234 if (enumField) {
235 return (ab) => ab.ReferenceExpr.setTarget(enumField);
236 }
237 }
238 // Fall through to typeCastingConvert if datatype_name lookup fails
239 return typeCastingConvert({ defaultValue, enums, val, services });
240 }
241
242 switch (fieldType) {
243 case 'DateTime':
244 if (val === 'CURRENT_TIMESTAMP' || val === 'now()') {
245 return (ab) => ab.InvocationExpr.setFunction(getFunctionRef('now', services));
246 }
247
248 if (val.includes('::')) {
249 return typeCastingConvert({ defaultValue, enums, val, services });
250 }
251
252 // Fallback to string literal for other DateTime defaults
253 return (ab) => ab.StringLiteral.setValue(val);
254
255 case 'Int':
256 case 'BigInt':
257 if (val.startsWith('nextval(')) {
258 return (ab) => ab.InvocationExpr.setFunction(getFunctionRef('autoincrement', services));
259 }
260
261 if (val.includes('::')) {
262 return typeCastingConvert({ defaultValue, enums, val, services });
263 }
264 return (ab) => ab.NumberLiteral.setValue(val);
265
266 case 'Float':
267 if (val.includes('::')) {
268 return typeCastingConvert({ defaultValue, enums, val, services });
269 }
270 return normalizeFloatDefault(val);
271
272 case 'Decimal':
273 if (val.includes('::')) {
274 return typeCastingConvert({ defaultValue, enums, val, services });
275 }
276 return normalizeDecimalDefault(val);
277
278 case 'Boolean':
279 return (ab) => ab.BooleanLiteral.setValue(val === 'true');
280
281 case 'String':

Callers

nothing calls this directly

Calls 10

getDbNameFunction · 0.90
getFunctionRefFunction · 0.90
normalizeFloatDefaultFunction · 0.90
normalizeDecimalDefaultFunction · 0.90
typeCastingConvertFunction · 0.85
findMethod · 0.80
setTargetMethod · 0.80
setFunctionMethod · 0.80
setValueMethod · 0.45
addArgMethod · 0.45

Tested by

no test coverage detected