ts-sql-codegen
Version:
Generates ts-sql-query table mappings from tbls schema output
153 lines • 5.76 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.fieldMappings = exports.FieldMappingSchema = exports.GeneratedFieldSchema = exports.GeneratedFieldTypeSchema = exports.ImportedItemSchema = exports.StrOrRegExpSchema = void 0;
const z = __importStar(require("zod"));
exports.StrOrRegExpSchema = z.string().or(z.instanceof(RegExp));
exports.ImportedItemSchema = z.object({
/** Name of import */
name: z.string(),
/** Path from which we should import */
importPath: z.string().nullish(),
/**
* Whether this is a default import
*
* @default false
*/
isDefault: z.boolean().nullish(),
/**
* Whether this is a relative import
*
* @default true
*/
isRelative: z.boolean().nullish()
});
exports.GeneratedFieldTypeSchema = z.object({
/**
* Specify that this field uses a custom database type or an enum type
*/
kind: z.enum(["custom", "customComparable", "enum", "customInt", "customDouble", "customUuid", "customLocalDate", "customLocalTime", "customLocalDateTime"]).nullish(),
/**
* This name is a database type identifier as expected by ts-sql-query
*
* These names are not database specific and may not match FieldMappingSchema.columnType eg. for database level type (which tbls outputs in the schema yaml) can be varchar but the columnType that ts-sql-query uses will be 'string'
*/
dbType: z.object({ name: z.string() }).nullish(),
/**
* If present, used as a generic type argument to field factory. This is typically useful when
* dealing with custom database types or enum types.
*
* If importPath is not present, then an import will not be added. This can result in a compile time error
* if the type is not globally available.
*/
tsType: exports.ImportedItemSchema.nullish(),
/**
* Specify a type adapter for the generated field.
*
* If not present, we will attempt to use GeneratorOpts.common.typeAdapter.importPath or throw if absent.
*/
adapter: exports.ImportedItemSchema.nullish(),
});
exports.GeneratedFieldSchema = z.object({
type: exports.GeneratedFieldTypeSchema.nullish(),
name: z.string().nullish(),
isComputed: z.boolean().nullish(),
isOptional: z.boolean().nullish(),
hasDefault: z.boolean().nullish()
});
exports.FieldMappingSchema = z.object({
/** Optional criteria (string or regex) to match column name */
columnName: exports.StrOrRegExpSchema.nullish(),
/** Optional criteria (string or regex) to match table name */
tableName: exports.StrOrRegExpSchema.nullish(),
/**
* Optional criteria (string or regex) to match column type (in database)
*
* This will be used to match against the type name as
* present in the tbls output schema yaml file.
*/
columnType: exports.StrOrRegExpSchema.nullish(),
/**
* Can be used to customize the field name or type mapping
* in the generated field.
*
* Set to false to omit mapping of this field
*/
generatedField: exports.GeneratedFieldSchema.or(z.literal(false)),
comment: z.string().nullish(),
});
exports.fieldMappings = [
{
columnType: /(char|text)/i,
generatedField: { type: { dbType: { name: "string" } } },
},
{
columnType: /bool/i,
generatedField: { type: { dbType: { name: "boolean" } } },
},
{
columnType: /bigint|bigserial/i,
generatedField: { type: { dbType: { name: "bigint" } } },
},
{
columnType: /int/i,
generatedField: { type: { dbType: { name: "int" } } },
},
{
columnType: /^uuid$/i,
generatedField: { type: { dbType: { name: "uuid" } } },
},
{
columnType: /(timestamp|datetime)/i,
generatedField: { type: { dbType: { name: "localDateTime" } } },
},
{
columnType: /date/i,
generatedField: { type: { dbType: { name: "localDate" } } },
},
{
columnType: /time/i,
generatedField: { type: { dbType: { name: "localTime" } } },
},
{
columnType: /(double|float)/i,
generatedField: { type: { dbType: { name: "double" } } },
},
{
columnType: /(decimal|numeric)/i,
generatedField: { type: { dbType: { name: "stringDouble" } } },
}
];
//# sourceMappingURL=field-mappings.js.map