MCPcopy
hub / github.com/drizzle-team/drizzle-orm / stringColumnToSchema

Function stringColumnToSchema

drizzle-valibot/src/column.ts:243–296  ·  view source on GitHub ↗
(column: Column)

Source from the content-addressed store, hash-verified

241}
242
243function stringColumnToSchema(column: Column): v.GenericSchema {
244 if (isColumnType<PgUUID<ColumnBaseConfig<'string', 'PgUUID'>>>(column, ['PgUUID'])) {
245 return v.pipe(v.string(), v.uuid());
246 }
247
248 let max: number | undefined;
249 let regex: RegExp | undefined;
250 let fixed = false;
251
252 if (isColumnType<PgVarchar<any> | SQLiteText<any>>(column, ['PgVarchar', 'SQLiteText'])) {
253 max = column.length;
254 } else if (
255 isColumnType<MySqlVarChar<any> | SingleStoreVarChar<any>>(column, ['MySqlVarChar', 'SingleStoreVarChar'])
256 ) {
257 max = column.length ?? CONSTANTS.INT16_UNSIGNED_MAX;
258 } else if (isColumnType<MySqlText<any> | SingleStoreText<any>>(column, ['MySqlText', 'SingleStoreText'])) {
259 if (column.textType === 'longtext') {
260 max = CONSTANTS.INT32_UNSIGNED_MAX;
261 } else if (column.textType === 'mediumtext') {
262 max = CONSTANTS.INT24_UNSIGNED_MAX;
263 } else if (column.textType === 'text') {
264 max = CONSTANTS.INT16_UNSIGNED_MAX;
265 } else {
266 max = CONSTANTS.INT8_UNSIGNED_MAX;
267 }
268 }
269
270 if (
271 isColumnType<PgChar<any> | MySqlChar<any> | SingleStoreChar<any>>(column, [
272 'PgChar',
273 'MySqlChar',
274 'SingleStoreChar',
275 ])
276 ) {
277 max = column.length;
278 fixed = true;
279 }
280
281 if (isColumnType<PgBinaryVector<any>>(column, ['PgBinaryVector'])) {
282 regex = /^[01]+$/;
283 max = column.dimensions;
284 }
285
286 const actions: any[] = [];
287 if (regex) {
288 actions.push(v.regex(regex));
289 }
290 if (max && fixed) {
291 actions.push(v.length(max));
292 } else if (max) {
293 actions.push(v.maxLength(max));
294 }
295 return actions.length > 0 ? v.pipe(v.string(), ...actions) : v.string();
296}

Callers 1

columnToSchemaFunction · 0.70

Calls 1

isColumnTypeFunction · 0.90

Tested by

no test coverage detected