@xata.io/client
Advanced tools
Comparing version 0.0.0-alpha.c7ce1d5 to 0.0.0-alpha.c8d406e
@@ -0,1 +1,4 @@ | ||
import { Page } from './schema/pagination'; | ||
import { Query, QueryOptions } from './schema/query'; | ||
import { Selectable, SelectableColumn, Select } from './schema/selection'; | ||
export interface XataRecord { | ||
@@ -10,127 +13,3 @@ id: string; | ||
} | ||
export declare type Queries<T> = { | ||
[key in keyof T as T[key] extends Query<infer A, infer B> ? key : never]: T[key]; | ||
}; | ||
export declare type OmitQueries<T> = { | ||
[key in keyof T as T[key] extends Query<infer A, infer B> ? never : key]: T[key]; | ||
}; | ||
export declare type OmitLinks<T> = { | ||
[key in keyof T as T[key] extends XataRecord ? never : key]: T[key]; | ||
}; | ||
export declare type OmitMethods<T> = { | ||
[key in keyof T as T[key] extends Function ? never : key]: T[key]; | ||
}; | ||
export declare type Selectable<T> = Omit<OmitQueries<OmitMethods<T>>, 'id' | 'xata'>; | ||
export declare type Select<T, K extends keyof T> = Pick<T, K> & Queries<T> & XataRecord; | ||
export declare type Include<T> = { | ||
[key in keyof T as T[key] extends XataRecord ? key : never]?: boolean | Array<keyof Selectable<T[key]>>; | ||
}; | ||
declare type SortDirection = 'asc' | 'desc'; | ||
declare type Operator = '$gt' | '$lt' | '$ge' | '$le' | '$exists' | '$notExists' | '$endsWith' | '$startsWith' | '$pattern' | '$is' | '$isNot' | '$contains' | '$includes' | '$includesSubstring' | '$includesPattern' | '$includesAll'; | ||
declare type Constraint<T> = { | ||
[key in Operator]?: T; | ||
}; | ||
declare type DeepConstraint<T> = T extends Record<string, any> ? { | ||
[key in keyof T]?: T[key] | DeepConstraint<T[key]>; | ||
} : Constraint<T>; | ||
declare type ComparableType = number | Date; | ||
export declare const gt: <T extends ComparableType>(value: T) => Constraint<T>; | ||
export declare const ge: <T extends ComparableType>(value: T) => Constraint<T>; | ||
export declare const gte: <T extends ComparableType>(value: T) => Constraint<T>; | ||
export declare const lt: <T extends ComparableType>(value: T) => Constraint<T>; | ||
export declare const lte: <T extends ComparableType>(value: T) => Constraint<T>; | ||
export declare const le: <T extends ComparableType>(value: T) => Constraint<T>; | ||
export declare const exists: (column: string) => Constraint<string>; | ||
export declare const notExists: (column: string) => Constraint<string>; | ||
export declare const startsWith: (value: string) => Constraint<string>; | ||
export declare const endsWith: (value: string) => Constraint<string>; | ||
export declare const pattern: (value: string) => Constraint<string>; | ||
export declare const is: <T>(value: T) => Constraint<T>; | ||
export declare const isNot: <T>(value: T) => Constraint<T>; | ||
export declare const contains: <T>(value: T) => Constraint<T>; | ||
export declare const includes: (value: string) => Constraint<string>; | ||
export declare const includesSubstring: (value: string) => Constraint<string>; | ||
export declare const includesPattern: (value: string) => Constraint<string>; | ||
export declare const includesAll: (value: string) => Constraint<string>; | ||
declare type FilterConstraints<T> = { | ||
[key in keyof T]?: T[key] extends Record<string, any> ? FilterConstraints<T[key]> : T[key] | DeepConstraint<T[key]>; | ||
}; | ||
declare type CursorNavigationOptions = { | ||
first?: string; | ||
} | { | ||
last?: string; | ||
} | { | ||
after?: string; | ||
before?: string; | ||
}; | ||
declare type OffsetNavigationOptions = { | ||
size?: number; | ||
offset?: number; | ||
}; | ||
declare type PaginationOptions = CursorNavigationOptions & OffsetNavigationOptions; | ||
declare type BulkQueryOptions<T> = { | ||
page?: PaginationOptions; | ||
}; | ||
declare type QueryOrConstraint<T, R> = Query<T, R> | Constraint<T>; | ||
declare type QueryMeta = { | ||
page: { | ||
cursor: string; | ||
more: boolean; | ||
}; | ||
}; | ||
interface BasePage<T, R> { | ||
query: Query<T, R>; | ||
meta: QueryMeta; | ||
records: R[]; | ||
nextPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
previousPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
firstPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
lastPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
hasNextPage(): boolean; | ||
} | ||
declare class Page<T, R> implements BasePage<T, R> { | ||
readonly query: Query<T, R>; | ||
readonly meta: QueryMeta; | ||
readonly records: R[]; | ||
constructor(query: Query<T, R>, meta: QueryMeta, records?: R[]); | ||
nextPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
previousPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
firstPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
lastPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
hasNextPage(): boolean; | ||
} | ||
export declare class Query<T, R = T> implements BasePage<T, R> { | ||
table: string; | ||
repository: Repository<T>; | ||
readonly $any?: QueryOrConstraint<T, R>[]; | ||
readonly $all?: QueryOrConstraint<T, R>[]; | ||
readonly $not?: QueryOrConstraint<T, R>[]; | ||
readonly $none?: QueryOrConstraint<T, R>[]; | ||
readonly $sort?: Record<string, SortDirection>; | ||
readonly query: Query<T, R>; | ||
readonly meta: QueryMeta; | ||
readonly records: R[]; | ||
constructor(repository: Repository<T> | null, table: string, data: Partial<Query<T, R>>, parent?: Query<T, R>); | ||
any(...queries: Query<T, R>[]): Query<T, R>; | ||
all(...queries: Query<T, R>[]): Query<T, R>; | ||
not(...queries: Query<T, R>[]): Query<T, R>; | ||
none(...queries: Query<T, R>[]): Query<T, R>; | ||
filter(constraints: FilterConstraints<T>): Query<T, R>; | ||
filter<F extends keyof T>(column: F, value: FilterConstraints<T[F]> | DeepConstraint<T[F]>): Query<T, R>; | ||
sort<F extends keyof T>(column: F, direction: SortDirection): Query<T, R>; | ||
getPaginated(options?: BulkQueryOptions<T>): Promise<Page<T, R>>; | ||
[Symbol.asyncIterator](): AsyncIterableIterator<R>; | ||
getIterator(chunk: number, options?: Omit<BulkQueryOptions<T>, 'page'>): AsyncGenerator<R[]>; | ||
getMany(options?: BulkQueryOptions<T>): Promise<R[]>; | ||
getOne(options?: Omit<BulkQueryOptions<T>, 'page'>): Promise<R | null>; | ||
deleteAll(): Promise<number>; | ||
include(columns: Include<T>): this; | ||
nextPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
previousPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
firstPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
lastPage(size?: number, offset?: number): Promise<Page<T, R>>; | ||
hasNextPage(): boolean; | ||
} | ||
export declare abstract class Repository<T> extends Query<T, Selectable<T>> { | ||
select<K extends keyof Selectable<T>>(...columns: K[]): Query<T, Select<T, K>>; | ||
export declare abstract class Repository<T extends XataRecord> extends Query<T> { | ||
abstract create(object: Selectable<T>): Promise<T>; | ||
@@ -141,10 +20,8 @@ abstract createMany(objects: Selectable<T>[]): Promise<T[]>; | ||
abstract delete(id: string): void; | ||
abstract _runQuery<R>(query: Query<T, R>, options?: BulkQueryOptions<T>): Promise<Page<T, R>>; | ||
abstract query<R extends XataRecord, Options extends QueryOptions<T>>(query: Query<T, R>, options: Options): Promise<Page<T, typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R>>; | ||
} | ||
export declare class RestRepository<T> extends Repository<T> { | ||
client: BaseClient<any>; | ||
fetch: any; | ||
export declare class RestRepository<T extends XataRecord> extends Repository<T> { | ||
#private; | ||
constructor(client: BaseClient<any>, table: string); | ||
request<T>(method: string, path: string, body?: unknown): Promise<T | undefined>; | ||
select<K extends keyof T>(...columns: K[]): Query<T, Select<T, K>>; | ||
create(object: T): Promise<T>; | ||
@@ -155,9 +32,9 @@ createMany(objects: T[]): Promise<T[]>; | ||
delete(id: string): Promise<void>; | ||
_runQuery<R>(query: Query<T, R>, options?: BulkQueryOptions<T>): Promise<Page<T, R>>; | ||
query<R extends XataRecord, Options extends QueryOptions<T>>(query: Query<T, R>, options: Options): Promise<Page<T, typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R>>; | ||
} | ||
interface RepositoryFactory { | ||
createRepository<T>(client: BaseClient<any>, table: string): Repository<T>; | ||
createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>; | ||
} | ||
export declare class RestRespositoryFactory implements RepositoryFactory { | ||
createRepository<T>(client: BaseClient<any>, table: string): Repository<T>; | ||
createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>; | ||
} | ||
@@ -170,3 +47,3 @@ declare type BranchStrategyValue = string | undefined | null; | ||
fetch?: unknown; | ||
databaseURL: string; | ||
databaseURL?: string; | ||
branch: BranchStrategyOption; | ||
@@ -190,2 +67,2 @@ apiKey: string; | ||
export declare type Links = Record<string, Array<string[]>>; | ||
export {}; | ||
export * from './schema/operators'; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -11,2 +21,13 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
}; | ||
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
if (kind === "m") throw new TypeError("Private method is not writable"); | ||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); | ||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); | ||
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; | ||
}; | ||
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); | ||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); | ||
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); | ||
}; | ||
var __asyncValues = (this && this.__asyncValues) || function (o) { | ||
@@ -19,250 +40,11 @@ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
}; | ||
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } | ||
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { | ||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var g = generator.apply(thisArg, _arguments || []), i, q = []; | ||
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; | ||
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } | ||
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } | ||
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } | ||
function fulfill(value) { resume("next", value); } | ||
function reject(value) { resume("throw", value); } | ||
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } | ||
}; | ||
var _RestRepository_client, _RestRepository_fetch, _RestRepository_table; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.XataError = exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = exports.Query = exports.includesAll = exports.includesPattern = exports.includesSubstring = exports.includes = exports.contains = exports.isNot = exports.is = exports.pattern = exports.endsWith = exports.startsWith = exports.notExists = exports.exists = exports.le = exports.lte = exports.lt = exports.gte = exports.ge = exports.gt = void 0; | ||
exports.XataError = exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = void 0; | ||
const filters_1 = require("./schema/filters"); | ||
const pagination_1 = require("./schema/pagination"); | ||
const query_1 = require("./schema/query"); | ||
const errors_1 = require("./util/errors"); | ||
const gt = (value) => ({ $gt: value }); | ||
exports.gt = gt; | ||
const ge = (value) => ({ $ge: value }); | ||
exports.ge = ge; | ||
const gte = (value) => ({ $ge: value }); | ||
exports.gte = gte; | ||
const lt = (value) => ({ $lt: value }); | ||
exports.lt = lt; | ||
const lte = (value) => ({ $le: value }); | ||
exports.lte = lte; | ||
const le = (value) => ({ $le: value }); | ||
exports.le = le; | ||
const exists = (column) => ({ $exists: column }); | ||
exports.exists = exists; | ||
const notExists = (column) => ({ $notExists: column }); | ||
exports.notExists = notExists; | ||
const startsWith = (value) => ({ $startsWith: value }); | ||
exports.startsWith = startsWith; | ||
const endsWith = (value) => ({ $endsWith: value }); | ||
exports.endsWith = endsWith; | ||
const pattern = (value) => ({ $pattern: value }); | ||
exports.pattern = pattern; | ||
const is = (value) => ({ $is: value }); | ||
exports.is = is; | ||
const isNot = (value) => ({ $isNot: value }); | ||
exports.isNot = isNot; | ||
const contains = (value) => ({ $contains: value }); | ||
exports.contains = contains; | ||
// TODO: these can only be applied to columns of type "multiple" | ||
const includes = (value) => ({ $includes: value }); | ||
exports.includes = includes; | ||
const includesSubstring = (value) => ({ $includesSubstring: value }); | ||
exports.includesSubstring = includesSubstring; | ||
const includesPattern = (value) => ({ $includesPattern: value }); | ||
exports.includesPattern = includesPattern; | ||
const includesAll = (value) => ({ $includesAll: value }); | ||
exports.includesAll = includesAll; | ||
class Page { | ||
constructor(query, meta, records = []) { | ||
this.query = query; | ||
this.meta = meta; | ||
this.records = records; | ||
} | ||
nextPage(size, offset) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.query.getPaginated({ page: { size, offset, after: this.meta.page.cursor } }); | ||
}); | ||
} | ||
previousPage(size, offset) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.query.getPaginated({ page: { size, offset, before: this.meta.page.cursor } }); | ||
}); | ||
} | ||
firstPage(size, offset) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.query.getPaginated({ page: { size, offset, first: this.meta.page.cursor } }); | ||
}); | ||
} | ||
lastPage(size, offset) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.query.getPaginated({ page: { size, offset, last: this.meta.page.cursor } }); | ||
}); | ||
} | ||
// TODO: We need to add something on the backend if we want a hasPreviousPage | ||
hasNextPage() { | ||
return this.meta.page.more; | ||
} | ||
class Repository extends query_1.Query { | ||
} | ||
class Query { | ||
constructor(repository, table, data, parent) { | ||
// Cursor pagination | ||
this.query = this; | ||
this.meta = { page: { cursor: 'start', more: true } }; | ||
this.records = []; | ||
if (repository) { | ||
this.repository = repository; | ||
} | ||
else { | ||
this.repository = this; | ||
} | ||
this.table = table; | ||
// For some reason Object.assign(this, parent) didn't work in this case | ||
// so doing all this manually: | ||
this.$any = parent === null || parent === void 0 ? void 0 : parent.$any; | ||
this.$all = parent === null || parent === void 0 ? void 0 : parent.$all; | ||
this.$not = parent === null || parent === void 0 ? void 0 : parent.$not; | ||
this.$none = parent === null || parent === void 0 ? void 0 : parent.$none; | ||
this.$sort = parent === null || parent === void 0 ? void 0 : parent.$sort; | ||
Object.assign(this, data); | ||
// These bindings are used to support deconstructing | ||
// const { any, not, filter, sort } = xata.users.query() | ||
this.any = this.any.bind(this); | ||
this.all = this.all.bind(this); | ||
this.not = this.not.bind(this); | ||
this.filter = this.filter.bind(this); | ||
this.sort = this.sort.bind(this); | ||
this.none = this.none.bind(this); | ||
Object.defineProperty(this, 'table', { enumerable: false }); | ||
Object.defineProperty(this, 'repository', { enumerable: false }); | ||
} | ||
any(...queries) { | ||
return new Query(this.repository, this.table, { | ||
$any: (this.$any || []).concat(queries) | ||
}, this); | ||
} | ||
all(...queries) { | ||
return new Query(this.repository, this.table, { | ||
$all: (this.$all || []).concat(queries) | ||
}, this); | ||
} | ||
not(...queries) { | ||
return new Query(this.repository, this.table, { | ||
$not: (this.$not || []).concat(queries) | ||
}, this); | ||
} | ||
none(...queries) { | ||
return new Query(this.repository, this.table, { | ||
$none: (this.$none || []).concat(queries) | ||
}, this); | ||
} | ||
filter(a, b) { | ||
if (arguments.length === 1) { | ||
const constraints = a; | ||
const queries = []; | ||
for (const [column, constraint] of Object.entries(constraints)) { | ||
queries.push({ [column]: constraint }); | ||
} | ||
return new Query(this.repository, this.table, { | ||
$all: (this.$all || []).concat(queries) | ||
}, this); | ||
} | ||
else { | ||
const column = a; | ||
const value = b; | ||
return new Query(this.repository, this.table, { | ||
$all: (this.$all || []).concat({ [column]: value }) | ||
}, this); | ||
} | ||
} | ||
sort(column, direction) { | ||
const sort = Object.assign(Object.assign({}, this.$sort), { [column]: direction }); | ||
const q = new Query(this.repository, this.table, { | ||
$sort: sort | ||
}, this); | ||
return q; | ||
} | ||
getPaginated(options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.repository._runQuery(this, options); | ||
}); | ||
} | ||
[Symbol.asyncIterator]() { | ||
return __asyncGenerator(this, arguments, function* _a() { | ||
var e_1, _b; | ||
try { | ||
for (var _c = __asyncValues(this.getIterator(1)), _d; _d = yield __await(_c.next()), !_d.done;) { | ||
const [record] = _d.value; | ||
yield yield __await(record); | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_d && !_d.done && (_b = _c.return)) yield __await(_b.call(_c)); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
}); | ||
} | ||
getIterator(chunk, options = {}) { | ||
return __asyncGenerator(this, arguments, function* getIterator_1() { | ||
let offset = 0; | ||
let end = false; | ||
while (!end) { | ||
const { records, meta } = yield __await(this.getPaginated(Object.assign(Object.assign({}, options), { page: { size: chunk, offset } }))); | ||
yield yield __await(records); | ||
offset += chunk; | ||
end = !meta.page.more; | ||
} | ||
}); | ||
} | ||
getMany(options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { records } = yield this.getPaginated(options); | ||
return records; | ||
}); | ||
} | ||
getOne(options = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const records = yield this.getMany(Object.assign(Object.assign({}, options), { page: { size: 1 } })); | ||
return records[0] || null; | ||
}); | ||
} | ||
deleteAll() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// TODO: Return number of affected rows | ||
return 0; | ||
}); | ||
} | ||
include(columns) { | ||
// TODO | ||
return this; | ||
} | ||
nextPage(size, offset) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.firstPage(size, offset); | ||
}); | ||
} | ||
previousPage(size, offset) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.firstPage(size, offset); | ||
}); | ||
} | ||
firstPage(size, offset) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.getPaginated({ page: { size, offset } }); | ||
}); | ||
} | ||
lastPage(size, offset) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.getPaginated({ page: { size, offset, before: 'end' } }); | ||
}); | ||
} | ||
hasNextPage() { | ||
return this.meta.page.more; | ||
} | ||
} | ||
exports.Query = Query; | ||
class Repository extends Query { | ||
select(...columns) { | ||
return new Query(this.repository, this.table, {}); | ||
} | ||
} | ||
exports.Repository = Repository; | ||
@@ -272,7 +54,11 @@ class RestRepository extends Repository { | ||
super(null, table, {}); | ||
this.client = client; | ||
_RestRepository_client.set(this, void 0); | ||
_RestRepository_fetch.set(this, void 0); | ||
_RestRepository_table.set(this, void 0); | ||
__classPrivateFieldSet(this, _RestRepository_client, client, "f"); | ||
__classPrivateFieldSet(this, _RestRepository_table, table, "f"); | ||
const doWeHaveFetch = typeof fetch !== 'undefined'; | ||
const isInjectedFetchProblematic = !this.client.options.fetch; | ||
const isInjectedFetchProblematic = !__classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch; | ||
if (doWeHaveFetch) { | ||
this.fetch = fetch; | ||
__classPrivateFieldSet(this, _RestRepository_fetch, fetch, "f"); | ||
} | ||
@@ -283,13 +69,11 @@ else if (isInjectedFetchProblematic) { | ||
else { | ||
this.fetch = this.client.options.fetch; | ||
__classPrivateFieldSet(this, _RestRepository_fetch, __classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch, "f"); | ||
} | ||
Object.defineProperty(this, 'client', { enumerable: false }); | ||
Object.defineProperty(this, 'fetch', { enumerable: false }); | ||
Object.defineProperty(this, 'hostname', { enumerable: false }); | ||
} | ||
request(method, path, body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { databaseURL, apiKey } = this.client.options; | ||
const branch = yield this.client.getBranch(); | ||
const resp = yield this.fetch(`${databaseURL}:${branch}${path}`, { | ||
const { databaseURL, apiKey } = __classPrivateFieldGet(this, _RestRepository_client, "f").options; | ||
const branch = yield __classPrivateFieldGet(this, _RestRepository_client, "f").getBranch(); | ||
const fetchImpl = __classPrivateFieldGet(this, _RestRepository_fetch, "f"); | ||
const resp = yield fetchImpl(`${databaseURL}:${branch}${path}`, { | ||
method, | ||
@@ -324,9 +108,6 @@ headers: { | ||
} | ||
select(...columns) { | ||
return new Query(this.repository, this.table, {}); | ||
} | ||
create(object) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const record = transformObjectLinks(object); | ||
const response = yield this.request('POST', `/tables/${this.table}/data`, record); | ||
const response = yield this.request('POST', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data`, record); | ||
if (!response) { | ||
@@ -345,3 +126,3 @@ throw new Error("The server didn't return any data for the query"); | ||
const records = objects.map((object) => transformObjectLinks(object)); | ||
const response = yield this.request('POST', `/tables/${this.table}/bulk`, { records }); | ||
const response = yield this.request('POST', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/bulk`, { records }); | ||
if (!response) { | ||
@@ -361,6 +142,6 @@ throw new Error("The server didn't return any data for the query"); | ||
try { | ||
const response = yield this.request('GET', `/tables/${this.table}/data/${id}`); | ||
const response = yield this.request('GET', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data/${id}`); | ||
if (!response) | ||
return null; | ||
return this.client.initObject(this.table, response); | ||
return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response); | ||
} | ||
@@ -376,3 +157,3 @@ catch (err) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const response = yield this.request('PUT', `/tables/${this.table}/data/${id}`, object); | ||
const response = yield this.request('PUT', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data/${id}`, object); | ||
if (!response) { | ||
@@ -382,3 +163,3 @@ throw new Error("The server didn't return any data for the query"); | ||
// TODO: Review this, not sure we are properly initializing the object | ||
return this.client.initObject(this.table, response); | ||
return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response); | ||
}); | ||
@@ -388,19 +169,16 @@ } | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.request('DELETE', `/tables/${this.table}/data/${id}`); | ||
yield this.request('DELETE', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data/${id}`); | ||
}); | ||
} | ||
_runQuery(query, options) { | ||
query(query, options) { | ||
var _a, _b, _c; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const filter = { | ||
$any: query.$any, | ||
$all: query.$all, | ||
$not: query.$not, | ||
$none: query.$none | ||
}; | ||
const data = query.getData(); | ||
const body = { | ||
filter: Object.values(filter).some(Boolean) ? filter : undefined, | ||
sort: query.$sort, | ||
page: options === null || options === void 0 ? void 0 : options.page | ||
filter: Object.values(data.filter).some(Boolean) ? data.filter : undefined, | ||
sort: (_a = (0, filters_1.buildSortFilter)(options === null || options === void 0 ? void 0 : options.sort)) !== null && _a !== void 0 ? _a : data.sort, | ||
page: (_b = options === null || options === void 0 ? void 0 : options.page) !== null && _b !== void 0 ? _b : data.page, | ||
columns: (_c = options === null || options === void 0 ? void 0 : options.columns) !== null && _c !== void 0 ? _c : data.columns | ||
}; | ||
const response = yield this.request('POST', `/tables/${this.table}/query`, body); | ||
const response = yield this.request('POST', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/query`, body); | ||
if (!response) { | ||
@@ -410,4 +188,5 @@ throw new Error("The server didn't return any data for the query"); | ||
const { meta, records: objects } = response; | ||
const records = objects.map((record) => this.client.initObject(this.table, record)); | ||
return new Page(query, meta, records); | ||
const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), record)); | ||
// TODO: We should properly type this any | ||
return new pagination_1.Page(query, meta, records); | ||
}); | ||
@@ -417,2 +196,3 @@ } | ||
exports.RestRepository = RestRepository; | ||
_RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _RestRepository_table = new WeakMap(); | ||
class RestRespositoryFactory { | ||
@@ -472,3 +252,3 @@ createRepository(client, table) { | ||
getBranch() { | ||
var e_2, _a; | ||
var e_1, _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -492,3 +272,3 @@ if (this.branch) | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
@@ -498,3 +278,3 @@ try { | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
@@ -525,1 +305,2 @@ throw new Error('Unable to resolve branch value'); | ||
}; | ||
__exportStar(require("./schema/operators"), exports); |
{ | ||
"name": "@xata.io/client", | ||
"version": "0.0.0-alpha.c7ce1d5", | ||
"version": "0.0.0-alpha.c8d406e", | ||
"description": "Xata.io SDK for TypeScript and JavaScript", | ||
@@ -9,3 +9,3 @@ "main": "./dist/index.js", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "tsc", | ||
"build": "tsc -p tsconfig.build.json", | ||
"prepack": "npm run build" | ||
@@ -24,3 +24,3 @@ }, | ||
"homepage": "https://github.com/xataio/client-ts/blob/main/client/README.md", | ||
"gitHead": "c7ce1d5db2c5b098afc28dcda20d738433461317" | ||
"gitHead": "c8d406ef9b6efefa14193e205afc2cdd824de231" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
21
0
3
57288
881