0.3.2 • Published 6 months ago

@dwtechs/antity-pgsql v0.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

License: MIT npm version last version release date Jest:coverage minified size

Synopsis

Antity-pgsql.js adds PostgreSQL features to Antity.js library.

  • Very lightweight
  • Thoroughly tested
  • Works in Javascript, Typescript
  • Can be used as EcmaScrypt module
  • Written in Typescript

Support

  • node: 22

This is the oldest targeted versions. The library should work properly on older versions of Node.js but we do not support it officially.

Installation

$ npm i @dwtechs/antity-pgsql

Usage

ES6 / TypeScript

import { SQLEntity } from "@dwtechs/antity-pgsql";
import { normalizeName, normalizeNickname } from "@dwtechs/checkard";

const entity = new Entity("consumers", [
  {
    key: "id",
    type: "integer",
    min: 0,
    max: 120,
    typeCheck: true,
    methods: ["GET", "PUT", "DELETE"],
    required: true,
    safe: true,
    sanitize: true,
    normalize: true,
    validate: true,
    sanitizer: null,
    normalizer: null,
    validator: null,
  },
  {
    key: "firstName",
    type: "string",
    min: 0,
    max: 255,
    typeCheck: true,
    methods: ["GET", "POST", "PUT", "DELETE"],
    required: true,
    safe: true,
    sanitize: true,
    normalize: true,
    validate: true,
    sanitizer: null,
    normalizer: normalizeName,
    validator: null,
  },
  {
    key: "lastName",
    type: "string",
    min: 0,
    max: 255,
    typeCheck: true,
    methods: ["GET", "POST", "PUT", "DELETE"],
    required: true,
    safe: true,
    sanitize: true,
    normalize: true,
    validate: true,
    sanitizer: null,
    normalizer: normalizeName,
    validator: null,
  },
  {
    key: "nickname",
    type: "string",
    min: 0,
    max: 255,
    typeCheck: true,
    methods: ["GET", "POST", "PUT", "DELETE"],
    required: true,
    safe: true,
    sanitize: true,
    normalize: true,
    validate: true,
    sanitizer: null,
    normalizer: normalizeNickname,
    validator: null,
  },
]);

// add a consumer. Used when loggin in from user service
router.gett("/", ..., entity.get);
router.post("/", entity.normalize, entity.validate, ..., entity.add);
router.put("/", entity.normalize, entity.validate, ..., entity.update);
router.put("/", ..., entity.archive);

API Reference

type Operation = "select" | "insert" | "update" | "merge" | "delete";

type MatchMode =  
  "startsWith" | 
  "endsWith" |
  "contains" |
  "notContains" |
  "equals" |
  "notEquals" |
  "between" |
  "in" |
  "lt" |
  "lte" |
  "gt" |
  "gte" |
  "is" |
  "isNot" |
  "before" |
  "after" |
  "st_contains" |
  "st_dwithin";


type Filter = {
  value: string | number | boolean | Date | number[];
  subProps?: string[];
  matchMode?: MatchMode;
}

class SQLEntity {
  constructor(name: string, properties: Property[]);
  get name(): string;
  get table(): string;
  get unsafeProps(): string[];
  get properties(): Property[];
  set name(name: string);
  set table(table: string);

  query: {
    select: (paginate: boolean) => string;
    update: (rows: Record<string, unknown>[], consumerId: number | string, consumerName: string) => {
        query: string;
        args: unknown[];
    };
    insert: (rows: Record<string, unknown>[], consumerId: number | string, consumerName: string, rtn?: string) => {
        query: string;
        args: unknown[];
    };
    delete: () => string;
    return: (prop: string) => string;
  };
  get: (req: Request, res: Response, next: NextFunction) => void;
  add: (req: Request, res: Response, next: NextFunction) => Promise<void>;
  update: (req: Request, res: Response, next: NextFunction) => Promise<void>;
  archive: (req: Request, res: Response, next: NextFunction) => Promise<void>;
  delete: (req: Request, res: Response, next: NextFunction) => void;

}

function filter(
  first: number,
  rows: number,
  sortField: string | null,
  sortOrder: Sort = "ASC",
  filters: Filters | null,
): { filterClause: string, args: (Filter["value"])[] };

function execute(
  query: string, 
  args: (string | number | boolean | Date | number[])[], 
  client: any,
): Promise<PGResponse>;

get(), add(), update(), archive() and delete() methods are made to be used as Express.js middlewares. Each method will look for data to work on in the req.body.rows parameter.

Match modes

List of possible match modes :

NamealiastypesDescription
startsWithstringWhether the value starts with the filter value
containsstringWhether the value contains the filter value
endsWithstringWhether the value ends with the filter value
notContainsstringWhether the value does not contain filter value
equalsstring | numberWhether the value equals the filter value
notEqualsstring | numberWhether the value does not equal the filter value
instring[] | number[]Whether the value contains the filter value
ltstring | numberWhether the value is less than the filter value
ltestring | numberWhether the value is less than or equals to the filter value
gtstring | numberWhether the value is greater than the filter value
gtestring | numberWhether the value is greater than or equals to the filter value
isdate | boolean | nullWhether the value equals the filter value, alias to equals
isNotdate | boolean | nullWhether the value does not equal the filter value, alias to notEquals
beforedateWhether the date value is before the filter date
afterdateWhether the date value is after the filter date
betweendate2 | number2Whether the value is between the filter values
st_containsgeometryWhether the geometry completely contains other geometries
st_dwithingeometryWhether geometries are within a specified distance from another geometry

Types

List of compatible match modes for each property types

NameMatch modes
stringstartsWith,contains,endsWith,notContains,equals,notEquals,lt,lte,gt,gte
numberequals,notEquals,lt,lte,gt,gte
dateis,isNot,before,after
booleanis,isNot
string[]in
number[]in,between
date[]between
geometryst_contains,st_dwithin

List of secondary types :

Nameequivalent
integernumber
floatnumber
evennumber
oddnumber
positivenumber
negativenumber
powerOfTwonumber
asciinumber
arrayany[]
jwtstring
symbolstring
emailstring
passwordstring
regexstring
ipAddressstring
slugstring
hexadecimalstring
datedate
timestampdate
functionstring
htmlElementstring
htmlEventAttributestring
nodestring
jsonobject
objectobject

Contributors

Antity.js is still in development and we would be glad to get all the help you can provide. To contribute please read contributor.md for detailed installation guide.

Stack

PurposeChoiceMotivation
repositoryGithubhosting for software development version control using Git
package managernpmdefault node.js package manager
languageTypeScriptstatic type checking along with the latest ECMAScript features
module bundlerRollupadvanced module bundler for ES6 modules
unit testingJestdelightful testing with a focus on simplicity
0.3.2

6 months ago

0.3.1

6 months ago

0.3.0

6 months ago

0.2.1

6 months ago

0.2.0

6 months ago

0.1.1

6 months ago

0.1.0

6 months ago