test-easy-psql
Version:
Welcome to the test-easy-psql documentation! test-easy-psql is a simple intermediary for querying data in PostgreSQL databases. Whether you're a beginner or an experienced developer, this documentation will help you get started with test-easy-psql and lev
785 lines (781 loc) • 20.1 kB
JavaScript
"use strict";
const POSTGRESQL = "POSTGRESQL";
const MYSQL = "MYSQL";
const SUPPORTED_DRIVERS = {
[POSTGRESQL]: true,
[MYSQL]: true,
};
const START_TRANSACTION = "BEGIN;";
// DatabaseClientType.POSTGRESQL === getClientType()
// ? "BEGIN;"
// : "START TRANSACTION;";
const COMMIT = "COMMIT;";
const ROLLBACK = "ROLLBACK;";
// const IS_PORTGRES = getClientType() === DatabaseClientType.POSTGRESQL;
// const IS_MYSQL = getClientType() === DatabaseClientType.MYSQL
const IS_POSTGRES = true;
const WHERE_CLAUSE_OPERATORS = {
_and: " and ",
_or: " or ",
_in: " in ",
_nin: " not in ",
_lt: " < ",
_lte: " <= ",
_gt: " > ",
_gte: " >= ",
_is: " is ",
_is_not: " is not ",
_like: " like ",
_ilike: " ilike ",
_nlike: " not like ",
_nilike: " not ilike ",
_eq: " = ",
_neq: " <> ",
...(IS_POSTGRES && {
_in: " = any",
_any: " = any",
_nany: " <> any",
_all: " = all",
_nin: " <> all",
_contains: " @> ",
_contained_in: " <@ ",
_key_exists: " ? ",
_key_exists_any: " ?| ",
_key_exists_all: " ?& ",
_text_search: "tsquery",
_in_array: " = any ",
_nin_array: " <> any ",
_st_intersects: " ST_Intersects ",
_st_contains: " ST_Contains ",
_st_within: " ST_Within ",
_st_distance: " ST_Distance ",
_st_3d_distance: " ST_3DDistance ",
_st_covers: " ST_Covers ",
_st_overlaps: " ST_Overlaps ",
_st_touches: " ST_Touches ",
_st_crosses: " ST_Crosses ",
_st_disjoint: " ST_Disjoint ",
_st_equals: " ST_Equals ",
_st_d_within: " ST_DWithin ",
_st_nintersects: " NOT ST_Intersects ",
_st_ncontains: " NOT ST_Contains ",
_st_nwithin: " NOT ST_Within ",
_st_ncovers: " NOT ST_Covers ",
_st_noverlaps: " NOT ST_Overlaps ",
_st_ntouches: " NOT ST_Touches ",
_st_ncrosses: " NOT ST_Crosses ",
_st_ndisjoint: " NOT ST_Disjoint ",
_st_nequals: " NOT ST_Equals ",
_st_d_nwithin: " NOT ST_DWithin ",
}),
};
const QUERY_BINDER_KEYS = {
_and: true,
_or: true,
};
const REQUIRE_ARRAY_TRANSFORMATION = {
_in: true,
_nin: true,
...(IS_POSTGRES && {
_any: true,
_all: true,
}),
};
const REQUIRE_CAST_TO_NULL = {
_is: true,
_is_not: true,
};
const IS_JSON_OPERATOR = {
_contains: true,
_contained_in: true,
_key_exists: true,
_key_exists_any: true,
_key_exists_all: true,
};
const IS_JSON_KEY_OPERATOR = {
_key_exists: true,
_key_exists_any: true,
_key_exists_all: true,
};
const IS_JSON_ARRAY_KEY_OPERATOR = {
_key_exists_any: true,
_key_exists_all: true,
};
const REQUIRE_WILDCARD_TRANSFORMATION = {
_ilike: true,
_nilike: true,
};
const IS_TEXT_SEARCH_OPERATOR = {
_text_search: true,
};
const IS_ARRAY_SEARCH_OPERATOR = {
_in_array: true,
_nin_array: true,
};
const IS_POSTGIS_OPERATOR = {
_st_intersects: " ST_Intersects ",
_st_contains: " ST_Contains ",
_st_within: " ST_Within ",
_st_distance: " ST_Distance ",
_st_3d_distance: " ST_3DDistance ",
_st_covers: " ST_Covers ",
_st_overlaps: " ST_Overlaps ",
_st_touches: " ST_Touches ",
_st_crosses: " ST_Crosses ",
_st_disjoint: " ST_Disjoint ",
_st_equals: " ST_Equals ",
_st_d_within: " ST_DWithin ",
_st_d_nwithin: " NOT ST_DWithin ",
_st_nintersects: " NOT ST_Intersects ",
_st_ncontains: " NOT ST_Contains ",
_st_nwithin: " NOT ST_Within ",
_st_ncovers: " NOT ST_Covers ",
_st_noverlaps: " NOT ST_Overlaps ",
_st_ntouches: " NOT ST_Touches ",
_st_ncrosses: " NOT ST_Crosses ",
_st_ndisjoint: " NOT ST_Disjoint ",
_st_nequals: " NOT ST_Equals ",
};
const POSTGIS_DISTANCE_COMPARISON_OPERATORS = {
_eq: true,
_neq: true,
_lt: true,
_lte: true,
_gt: true,
_gte: true,
};
const SELF_UPDATE_OPERATORS = {
_inc: " + ",
_dec: " - ",
_mult: " * ",
_div: " / ",
};
const sqlTypesByDbClient = {
[POSTGRESQL]: [
{
name: "smallint",
value: "smallint",
canBeAutoIncrement: true,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "integer",
value: "integer",
alias: "int",
canBeAutoIncrement: true,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "bigint",
value: "bigint",
canBeAutoIncrement: true,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "smallserial",
value: "smallserial",
canBeAutoIncrement: true,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: true,
},
{
name: "serial",
value: "serial",
canBeAutoIncrement: true,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: true,
},
{
name: "bigserial",
value: "bigserial",
canBeAutoIncrement: true,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: true,
},
{
name: "boolean",
value: "boolean",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: true,
canHaveDefaultValue: true,
},
{
name: "character varying",
value: "character varying",
alias: "varchar",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
hasLength: true,
min: 1,
max: 255,
canHaveDefaultValue: true,
},
{
name: "char",
value: "char",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
hasLength: true,
min: 1,
max: 255,
canHaveDefaultValue: true,
},
{
name: "double precision",
value: "double precision",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "money",
value: "money",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "real",
value: "real",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "text",
value: "text",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "inet",
value: "inet",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "cidr",
value: "cidr",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "macaddr",
value: "macaddr",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "macaddr8",
value: "macaddr8",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "geometry",
value: "geometry",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "geography",
value: "geography",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "point",
value: "point",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "line",
value: "line",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "lseg",
value: "lseg",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "polygon",
value: "polygon",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "circle",
value: "circle",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "box",
value: "circle",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "path",
value: "path",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "bytea",
value: "bytea",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "date",
value: "date",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
defaultSuggestions: ["CURRENT_DATE"],
},
{
name: "time",
value: "time",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
defaultSuggestions: ["CURRENT_TIMESTAMP"],
},
{
name: "time with time zone",
value: "time with time zone",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
defaultSuggestions: ["CURRENT_TIMESTAMP"],
},
{
name: "timestamp",
value: "timestamp",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
defaultSuggestions: ["CURRENT_TIMESTAMP"],
},
{
name: "timestamp with time zone",
value: "timestamp with time zone",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
defaultSuggestions: ["CURRENT_TIMESTAMP"],
},
{
name: "json",
value: "json",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "jsonb",
value: "jsonb",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "uuid",
value: "uuid",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "xml",
value: "xml",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
],
[MYSQL]: [
{
name: "decimal",
value: "decimal",
canBeAutoIncrement: true,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "double",
value: "double",
canBeAutoIncrement: true,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "bit",
value: "bit",
canBeAutoIncrement: true,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "float",
value: "float",
canBeAutoIncrement: true,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "tinyint",
value: "tinyint",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "smallint",
value: "smallint",
canBeAutoIncrement: true,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "mediumint",
value: "mediumint",
canBeAutoIncrement: true,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "bigint",
value: "bigint",
canBeAutoIncrement: true,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "int",
value: "int",
canBeAutoIncrement: true,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "binary",
value: "binary",
canBeAutoIncrement: true,
hasLength: true,
min: 1,
max: 255,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "varbinary",
value: "varbinary",
canBeAutoIncrement: true,
hasLength: true,
min: 1,
max: 255,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "boolean",
value: "boolean",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: true,
canHaveDefaultValue: true,
},
{
name: "varchar",
value: "varchar",
alias: "varchar",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
hasLength: true,
min: 1,
max: 255,
canHaveDefaultValue: true,
},
{
name: "char",
value: "char",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
hasLength: true,
min: 1,
max: 255,
canHaveDefaultValue: true,
},
{
name: "tinytext",
value: "tinytext",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
hasLength: true,
min: 1,
max: 255,
canHaveDefaultValue: true,
},
{
name: "text",
value: "text",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
hasLength: true,
min: 1,
max: 65535,
canHaveDefaultValue: true,
},
{
name: "longtext",
value: "longtext",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "datetime",
value: "datetime",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
defaultSuggestions: ["CURRENT_DATE"],
},
{
name: "year",
value: "year",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
defaultSuggestions: ["CURRENT_DATE"],
},
{
name: "time",
value: "time",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
defaultSuggestions: ["CURRENT_TIMESTAMP"],
},
{
name: "timestamp",
value: "timestamp",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
defaultSuggestions: ["CURRENT_TIMESTAMP"],
},
{
name: "json",
value: "json",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "geomnetry",
value: "geomnetry",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "point",
value: "point",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "linestring",
value: "linestring",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "polygon",
value: "polygon",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "geometrycollection",
value: "geometrycollection",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "multilinestring",
value: "multilinestring",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: true,
disabledPrimaryKey: false,
canHaveDefaultValue: true,
},
{
name: "multipolygon",
value: "multipolygon",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
{
name: "multipoint",
value: "multipoint",
canBeAutoIncrement: false,
canBeReferencedInPrimaryKey: false,
disabledPrimaryKey: false,
canHaveDefaultValue: false,
},
],
};
const getSqlTypesByDBClient = (client) => sqlTypesByDbClient?.[client] || [];
const allowedOrderDirectionsKeys = {
ASC: "ASC",
DESC: "DESC",
asc: "ASC",
desc: "DESC",
...(IS_POSTGRES && {
asc_nulls_first: "ASC NULLS FIRST",
asc_nulls_last: "ASC NULLS LAST",
desc_nulls_first: "DESC NULLS FIRST",
desc_nulls_last: "DESC NULLS LAST",
}),
};
const SupportedAggregations = {
_count: "COUNT",
_min: "MIN",
_max: "MAX",
_avg: "AVG",
_sum: "SUM",
};
const EVENTS = {
SELECT: "select",
UPDATE: "update",
INSERT: "insert",
DELETE: "delete",
ERROR: "error",
};
const forUpdateMapper = {
for_update: "for update",
for_no_key_update: "for no key update",
for_share: "for share",
for_key_share: "for key share",
nowait: "for update nowait",
skip_locked: " for update skip locked",
};
module.exports = {
POSTGRESQL,
MYSQL,
SUPPORTED_DRIVERS,
START_TRANSACTION,
COMMIT,
ROLLBACK,
IS_POSTGRES,
WHERE_CLAUSE_OPERATORS,
QUERY_BINDER_KEYS,
REQUIRE_ARRAY_TRANSFORMATION,
REQUIRE_CAST_TO_NULL,
IS_JSON_OPERATOR,
IS_JSON_KEY_OPERATOR,
IS_JSON_ARRAY_KEY_OPERATOR,
REQUIRE_WILDCARD_TRANSFORMATION,
IS_TEXT_SEARCH_OPERATOR,
sqlTypesByDbClient,
getSqlTypesByDBClient,
allowedOrderDirectionsKeys,
SupportedAggregations,
SELF_UPDATE_OPERATORS,
EVENTS,
IS_POSTGIS_OPERATOR,
POSTGIS_DISTANCE_COMPARISON_OPERATORS,
forUpdateMapper,
};