Comparing version 0.2.0 to 0.3.0
@@ -13,4 +13,4 @@ /// <reference types="node" /> | ||
* @example | ||
* { | ||
* overrides: { "identity_provider.linkedin": "LinkedIn" } | ||
* overrides: { | ||
* "identity_provider.linkedin": "LinkedIn" | ||
* } | ||
@@ -20,2 +20,27 @@ */ | ||
prefix?: string; | ||
/** | ||
* Schemas that should be included/excluded when generating types. | ||
* | ||
* By default if this is null/not specified, "public" will be the only schema added, but if this property | ||
* is specified, public will be excluded by default and has to be included in the list to be added to the output. | ||
* If a schema is added by its name, i.e. 'log' all tables from that schema will be added. | ||
* If a schema name is added with an exclamation mark it will be ignored, i.e. "!log". | ||
* | ||
* The table-type will be prefixed by its schema name, the table log.message will become LogMessage. | ||
* | ||
* @example | ||
* // This will include "public" and "log", but exclude the "auth" schema. | ||
* schema: ["public", "log", "!auth"] | ||
* @default | ||
* "public" | ||
*/ | ||
schema?: string[] | string; | ||
/** | ||
* A comma separated list or an array of tables that should be excluded when | ||
* generating types. | ||
* | ||
* @example | ||
* exclude: ["migration", "migration_lock"] | ||
*/ | ||
exclude?: string[] | string; | ||
}; | ||
@@ -22,0 +47,0 @@ /** |
18
main.js
@@ -25,3 +25,3 @@ "use strict"; | ||
async function updateTypes(db, options) { | ||
var _options$overrides; | ||
var _options$overrides, _ref, _ref2; | ||
@@ -33,3 +33,8 @@ const overrides = (_options$overrides = options.overrides) !== null && _options$overrides !== void 0 ? _options$overrides : {}; | ||
["// The TypeScript definitions below are automatically generated.\n", "// Do not touch them, or risk, your modifications being lost.\n\n"].forEach(line => output.write(line)); | ||
const schema = (_ref = typeof options.schema === "string" ? options.schema.split(",").map(x => x.trim()) : options.schema) !== null && _ref !== void 0 ? _ref : ["public"]; // Schemas to include or exclude | ||
const [includeSchemas, excludeSchemas] = schema.reduce((acc, s) => acc[+s.startsWith("!")].push(s) && acc, [[], []]); // Tables to exclude | ||
const exclude = (_ref2 = typeof options.exclude === "string" ? options.exclude.split(",").map(x => x.trim()) : options.exclude) !== null && _ref2 !== void 0 ? _ref2 : []; | ||
if (options.prefix) { | ||
@@ -69,6 +74,10 @@ output.write(options.prefix); | ||
const columns = await db.withSchema("information_schema").table("columns").where("table_schema", "public").orderBy("table_name").orderBy("ordinal_position").select("table_name as table", "column_name as column", db.raw("(is_nullable = 'YES') as nullable"), "column_default as default", "data_type as type", "udt_name as udt"); // The list of database tables as enum | ||
const columns = await db.withSchema("information_schema").table("columns").whereIn("table_schema", includeSchemas).whereNotIn("table_schema", excludeSchemas).whereNotIn("table_name", exclude).orderBy("table_schema").orderBy("table_name").orderBy("ordinal_position").select("table_schema as schema", "table_name as table", "column_name as column", db.raw("(is_nullable = 'YES') as nullable"), "column_default as default", "data_type as type", "udt_name as udt"); // The list of database tables as enum | ||
output.write("export enum Table {\n"); | ||
Array.from(new Set(columns.map(x => x.table))).forEach(value => { | ||
const tableSet = new Set(columns.map(x => { | ||
const schema = x.schema !== "public" ? `${x.schema}.` : ""; | ||
return `${schema}${x.table}`; | ||
})); | ||
Array.from(tableSet).forEach(value => { | ||
var _overrides$value; | ||
@@ -86,3 +95,4 @@ | ||
const tableName = (_overrides$x$table = overrides[x.table]) !== null && _overrides$x$table !== void 0 ? _overrides$x$table : (0, _upperFirst2.default)((0, _camelCase2.default)(x.table)); | ||
output.write(`export type ${tableName} = {\n`); | ||
const schemaName = x.schema !== "public" ? (0, _upperFirst2.default)((0, _camelCase2.default)(x.schema)) : ""; | ||
output.write(`export type ${schemaName}${tableName} = {\n`); | ||
} | ||
@@ -89,0 +99,0 @@ |
{ | ||
"name": "knex-types", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Generates TypeScript definitions (types) from a (PostgreSQL) database schema.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
13493
187