UNPKG

@omneedia/client-js

Version:

Isomorphic Javascript client for Omneedia

252 lines 7.22 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const request_1 = __importDefault(require("./request")); class OmneediaQueryDB { constructor(table, ref) { this.SELECT = []; this.INSERT = {}; this.UPDATE = {}; this.DELETE = false; this.WHERE = []; this.ref = {}; this.FilterOperator = [ 'eq', 'neq', 'gt', 'gte', 'lt', 'lte', 'like', 'ilike', 'is', 'in', 'cs', 'cd', 'sl', 'sr', 'nxl', 'nxr', 'adj', 'ov', 'fts', 'plfts', 'phfts', 'wfts', ]; this.TABLE = table; this.ref = ref; this.request = new request_1.default(this); } rangeLt(key, value) { this.WHERE.push(`${key}=sl.${value}`); return this; } rangeGt(key, value) { this.WHERE.push(`${key}=sr.${value}`); return this; } rangeLte(key, value) { this.WHERE.push(`${key}=nxr.${value}`); return this; } rangeGte(key, value) { this.WHERE.push(`${key}=nxl.${value}`); return this; } rangeAdjacent(key, value) { this.WHERE.push(`${key}=adj.${value}`); return this; } overlaps(key, value) { if (!Array.isArray(value)) value = [value]; value = `[${value.join(',')}]`; this.WHERE.push(`${key}=ov.${value}`); return this; } not(key, value, o) { if (this.FilterOperator.indexOf(value) == -1) return this; this.WHERE.push(`${key}=not.${value}.${o}`); return this; } is(key, value) { this.WHERE.push(`${key}=is.${value}`); return this; } contains(key, value) { if (!Array.isArray(value)) value = [value]; value = `{${value.join(',')}}`; this.WHERE.push(`${key}=cs.${value}`); return this; } containedBy(key, value) { if (!Array.isArray(value)) value = [value]; value = `{${value.join(',')}}`; this.WHERE.push(`${key}=cd.${value}`); return this; } or(key) { if (!Array.isArray(key)) key = [key]; if (key.length == 1) key = key[0].split(','); this.WHERE.push(`or=(${key.join(',')})`); return this; } and(key) { if (!Array.isArray(key)) key = [key]; if (key.length == 1) key = key[0].split(','); this.WHERE.push(`and=(${key.join(',')})`); return this; } textSearch(key, value, o) { let typePart = ''; if (o) { if (o.type === 'plain') { typePart = 'pl'; } else if (o.type === 'phrase') { typePart = 'ph'; } else if (o.type === 'websearch') { typePart = 'w'; } } let method = `${typePart}fts`; this.WHERE.push(`${key}=${method}.${value}`); return this; } in(key, value) { if (!Array.isArray(value)) value = [value]; value = `(${value.join(',')})`; this.WHERE.push(`${key}=in.${value}`); return this; } nxr(key, value) { if (!Array.isArray(value)) value = [value]; value = `(${value.join(',')})`; this.WHERE.push(`${key}=nxr.${value}`); return this; } sl(key, value) { if (!Array.isArray(value)) value = [value]; value = `(${value.join(',')})`; this.WHERE.push(`${key}=sl.${value}`); return this; } adj(key, value) { if (!Array.isArray(value)) value = [value]; value = `(${value.join(',')})`; this.WHERE.push(`${key}=adj.${value}`); return this; } imatch(key) { for (var el in key) { this.WHERE.push(`${el}=eq.${key[el]}`); } return this; } match(key) { for (var el in key) { this.WHERE.push(`${el}=eq.${key[el]}`); } return this; } gt(key, value) { this.WHERE.push(`${key}=gt.${value}`); return this; } gte(key, value) { this.WHERE.push(`${key}=gte.${value}`); return this; } lt(key, value) { this.WHERE.push(`${key}=lt.${value}`); return this; } lte(key, value) { this.WHERE.push(`${key}=lte.${value}`); return this; } neq(key, value) { this.WHERE.push(`${key}=neq.${value}`); return this; } like(key, value) { this.WHERE.push(`${key}=like.${value}`); return this; } ilike(key, value) { this.WHERE.push(`${key}=ilike.${value}`); return this; } eq(key, value) { this.WHERE.push(`${key}=eq.${value}`); return this; } select(...s) { if (!s) s = ['']; if (s.length == 1) s = s[0].split(','); this.SELECT = s; return this; } insert(o) { this.INSERT = o; return this; } update(o) { this.UPDATE = o; return this; } delete() { this.DELETE = true; return this; } then(resolve, reject) { return __awaiter(this, void 0, void 0, function* () { var uri = `/${this.TABLE}?`; if (Object.keys(this.INSERT).length > 0) { var url = this.ref.PREFIX.REST + uri; return resolve(this.request.post(url, this.INSERT)); } if (this.SELECT.length > 0) uri += `select=${this.SELECT.join(',')}&`; if (this.WHERE.length > 0) uri += this.WHERE.join('&'); var url = this.ref.PREFIX.REST + uri; var method = 'get'; if (this.DELETE === true) method = 'delete'; if (Object.keys(this.UPDATE).length > 0) { resolve(this.request.patch(url, this.UPDATE)); } else resolve(this.request[method](url)); }); } } exports.default = OmneediaQueryDB; //# sourceMappingURL=db.js.map