@xata.io/client
Advanced tools
Comparing version 0.0.0-alpha.123dd7a to 0.0.0-alpha.1d71802
@@ -57,11 +57,47 @@ export interface XataRecord { | ||
}; | ||
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> = { | ||
filter?: FilterConstraints<T>; | ||
sort?: { | ||
column: keyof T; | ||
direction?: SortDirection; | ||
} | keyof T; | ||
page?: PaginationOptions; | ||
}; | ||
declare type QueryOrConstraint<T, R> = Query<T, R> | Constraint<T>; | ||
export declare class Query<T, R = 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; | ||
@@ -74,2 +110,5 @@ repository: Repository<T>; | ||
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>); | ||
@@ -83,6 +122,14 @@ any(...queries: Query<T, R>[]): 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?: BulkQueryOptions<T>): Promise<R | null>; | ||
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; | ||
} | ||
@@ -92,6 +139,7 @@ export declare abstract class Repository<T> extends Query<T, Selectable<T>> { | ||
abstract create(object: Selectable<T>): Promise<T>; | ||
abstract createMany(objects: Selectable<T>[]): Promise<T[]>; | ||
abstract read(id: string): Promise<T | null>; | ||
abstract update(id: string, object: Partial<T>): Promise<T>; | ||
abstract delete(id: string): void; | ||
abstract query<R>(query: Query<T, R>): Promise<R[]>; | ||
abstract _runQuery<R>(query: Query<T, R>, options?: BulkQueryOptions<T>): Promise<Page<T, R>>; | ||
} | ||
@@ -102,9 +150,10 @@ export declare class RestRepository<T> extends Repository<T> { | ||
constructor(client: BaseClient<any>, table: string); | ||
request(method: string, path: string, body?: unknown): Promise<any>; | ||
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>; | ||
createMany(objects: T[]): Promise<T[]>; | ||
read(id: string): Promise<T | null>; | ||
update(id: string, object: Partial<T>): Promise<T>; | ||
delete(id: string): Promise<void>; | ||
query<R>(query: Query<T, R>): Promise<R[]>; | ||
_runQuery<R>(query: Query<T, R>, options?: BulkQueryOptions<T>): Promise<Page<T, R>>; | ||
} | ||
@@ -123,3 +172,3 @@ interface RepositoryFactory { | ||
fetch?: unknown; | ||
databaseURL: string; | ||
databaseURL?: string; | ||
branch: BranchStrategyOption; | ||
@@ -126,0 +175,0 @@ apiKey: string; |
@@ -18,4 +18,17 @@ "use strict"; | ||
}; | ||
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]); } | ||
}; | ||
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; | ||
const errors_1 = require("./util/errors"); | ||
const gt = (value) => ({ $gt: value }); | ||
@@ -58,4 +71,39 @@ exports.gt = gt; | ||
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 Query { | ||
constructor(repository, table, data, parent) { | ||
// Cursor pagination | ||
this.query = this; | ||
this.meta = { page: { cursor: 'start', more: true } }; | ||
this.records = []; | ||
if (repository) { | ||
@@ -133,15 +181,47 @@ this.repository = repository; | ||
} | ||
// TODO: pagination. Maybe implement different methods for different type of paginations | ||
// and one to simply get the first records returned by the query with no pagination. | ||
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* () { | ||
// TODO: use options | ||
return this.repository.query(this); | ||
const { records } = yield this.getPaginated(options); | ||
return records; | ||
}); | ||
} | ||
getOne(options) { | ||
getOne(options = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// TODO: use options | ||
const arr = yield this.getMany(); // TODO, limit to 1 | ||
return arr[0] || null; | ||
const records = yield this.getMany(Object.assign(Object.assign({}, options), { page: { size: 1 } })); | ||
return records[0] || null; | ||
}); | ||
@@ -151,3 +231,3 @@ } | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// Return number of affected rows | ||
// TODO: Return number of affected rows | ||
return 0; | ||
@@ -160,2 +240,25 @@ }); | ||
} | ||
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; | ||
} | ||
} | ||
@@ -173,21 +276,12 @@ exports.Query = Query; | ||
this.client = client; | ||
const { fetch } = client.options; | ||
if (fetch) { | ||
const doWeHaveFetch = typeof fetch !== 'undefined'; | ||
const isInjectedFetchProblematic = !this.client.options.fetch; | ||
if (doWeHaveFetch) { | ||
this.fetch = fetch; | ||
} | ||
else if (typeof window === 'object') { | ||
this.fetch = window.fetch; | ||
else if (isInjectedFetchProblematic) { | ||
throw new Error(errors_1.errors.falsyFetchImplementation); | ||
} | ||
else if (typeof require === 'function') { | ||
try { | ||
this.fetch = require('node-fetch'); | ||
} | ||
catch (err) { | ||
try { | ||
this.fetch = require('cross-fetch'); | ||
} | ||
catch (err) { | ||
throw new Error('No fetch implementation found. Please provide one in the constructor'); | ||
} | ||
} | ||
else { | ||
this.fetch = this.client.options.fetch; | ||
} | ||
@@ -228,3 +322,3 @@ Object.defineProperty(this, 'client', { enumerable: false }); | ||
if (resp.status === 204) | ||
return; | ||
return undefined; | ||
return resp.json(); | ||
@@ -238,18 +332,36 @@ }); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const body = Object.assign({}, object); | ||
for (const key of Object.keys(body)) { | ||
const value = body[key]; | ||
if (value && typeof value === 'object' && typeof value.id === 'string') { | ||
body[key] = value.id; | ||
} | ||
const record = transformObjectLinks(object); | ||
const response = yield this.request('POST', `/tables/${this.table}/data`, record); | ||
if (!response) { | ||
throw new Error("The server didn't return any data for the query"); | ||
} | ||
const obj = yield this.request('POST', `/tables/${this.table}/data`, body); | ||
return this.client.initObject(this.table, obj); | ||
const finalObject = yield this.read(response.id); | ||
if (!finalObject) { | ||
throw new Error('The server failed to save the record'); | ||
} | ||
return finalObject; | ||
}); | ||
} | ||
createMany(objects) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const records = objects.map((object) => transformObjectLinks(object)); | ||
const response = yield this.request('POST', `/tables/${this.table}/bulk`, { records }); | ||
if (!response) { | ||
throw new Error("The server didn't return any data for the query"); | ||
} | ||
// TODO: Use filer.$any() to get all the records | ||
const finalObjects = yield Promise.all(response.recordIDs.map((id) => this.read(id))); | ||
if (finalObjects.some((object) => !object)) { | ||
throw new Error('The server failed to save the record'); | ||
} | ||
return finalObjects; | ||
}); | ||
} | ||
read(id) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const obj = yield this.request('GET', `/tables/${this.table}/data/${id}`); | ||
return this.client.initObject(this.table, obj); | ||
const response = yield this.request('GET', `/tables/${this.table}/data/${id}`); | ||
if (!response) | ||
return null; | ||
return this.client.initObject(this.table, response); | ||
} | ||
@@ -265,4 +377,8 @@ catch (err) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const obj = yield this.request('PUT', `/tables/${this.table}/data/${id}`, object); | ||
return this.client.initObject(this.table, obj); | ||
const response = yield this.request('PUT', `/tables/${this.table}/data/${id}`, object); | ||
if (!response) { | ||
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); | ||
}); | ||
@@ -275,3 +391,3 @@ } | ||
} | ||
query(query) { | ||
_runQuery(query, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -286,6 +402,12 @@ const filter = { | ||
filter: Object.values(filter).some(Boolean) ? filter : undefined, | ||
sort: query.$sort | ||
sort: query.$sort, | ||
page: options === null || options === void 0 ? void 0 : options.page | ||
}; | ||
const result = yield this.request('POST', `/tables/${this.table}/query`, body); | ||
return result.records.map((record) => this.client.initObject(this.table, record)); | ||
const response = yield this.request('POST', `/tables/${this.table}/query`, body); | ||
if (!response) { | ||
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); | ||
}); | ||
@@ -349,3 +471,3 @@ } | ||
getBranch() { | ||
var e_1, _a; | ||
var e_2, _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -369,3 +491,3 @@ if (this.branch) | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
@@ -375,3 +497,3 @@ try { | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
@@ -393,1 +515,10 @@ throw new Error('Unable to resolve branch value'); | ||
}; | ||
// TODO: We can find a better implementation for links | ||
const transformObjectLinks = (object) => { | ||
return Object.entries(object).reduce((acc, [key, value]) => { | ||
if (value && typeof value === 'object' && typeof value.id === 'string') { | ||
return Object.assign(Object.assign({}, acc), { [key]: value.id }); | ||
} | ||
return Object.assign(Object.assign({}, acc), { [key]: value }); | ||
}, {}); | ||
}; |
@@ -222,7 +222,10 @@ "use strict"; | ||
const { calls } = request.mock; | ||
expect(calls.length).toBe(1); | ||
const [method, path, body] = calls[0]; | ||
expect(method).toBe(expectedRequest.method); | ||
expect(path).toBe(expectedRequest.path); | ||
expect(JSON.stringify(body)).toBe(JSON.stringify(expectedRequest.body)); | ||
const requests = Array.isArray(expectedRequest) ? expectedRequest : [expectedRequest]; | ||
expect(calls.length).toBe(requests.length); | ||
for (let i = 0; i < calls.length; i++) { | ||
const [method, path, body] = calls[i]; | ||
expect(method).toBe(requests[i].method); | ||
expect(path).toBe(requests[i].path); | ||
expect(JSON.stringify(body)).toBe(JSON.stringify(requests[i].body)); | ||
} | ||
}); | ||
@@ -235,3 +238,6 @@ } | ||
const expected = { method: 'POST', path: '/tables/users/query', body: {} }; | ||
expectRequest(users, expected, () => users.getMany(), { records: [] }); | ||
expectRequest(users, expected, () => users.getMany(), { | ||
records: [], | ||
meta: { page: { cursor: '', more: false } } | ||
}); | ||
})); | ||
@@ -241,3 +247,6 @@ test('query with one filter', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const expected = { method: 'POST', path: '/tables/users/query', body: { filter: { $all: [{ name: 'foo' }] } } }; | ||
expectRequest(users, expected, () => users.filter('name', 'foo').getMany(), { records: [] }); | ||
expectRequest(users, expected, () => users.filter('name', 'foo').getMany(), { | ||
records: [], | ||
meta: { page: { cursor: '', more: false } } | ||
}); | ||
})); | ||
@@ -248,4 +257,4 @@ }); | ||
const { users } = buildClient(); | ||
const result = { records: [{ id: '1234' }] }; | ||
const expected = { method: 'POST', path: '/tables/users/query', body: {} }; | ||
const result = { records: [{ id: '1234' }], meta: { page: { cursor: '', more: false } } }; | ||
const expected = { method: 'POST', path: '/tables/users/query', body: { page: { size: 1 } } }; | ||
expectRequest(users, expected, () => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -258,4 +267,4 @@ const first = yield users.select().getOne(); | ||
const { users } = buildClient(); | ||
const result = { records: [] }; | ||
const expected = { method: 'POST', path: '/tables/users/query', body: {} }; | ||
const result = { records: [], meta: { page: { cursor: '', more: false } } }; | ||
const expected = { method: 'POST', path: '/tables/users/query', body: { page: { size: 1 } } }; | ||
expectRequest(users, expected, () => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -303,3 +312,10 @@ const first = yield users.getOne(); | ||
const object = { name: 'Ada' }; | ||
const expected = { method: 'POST', path: '/tables/users/data', body: object }; | ||
const expected = [ | ||
{ method: 'POST', path: '/tables/users/data', body: object }, | ||
{ | ||
method: 'GET', | ||
path: '/tables/users/data/rec_1234', | ||
body: undefined | ||
} | ||
]; | ||
expectRequest(users, expected, () => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -306,0 +322,0 @@ const result = yield users.create(object); |
{ | ||
"name": "@xata.io/client", | ||
"version": "0.0.0-alpha.123dd7a", | ||
"version": "0.0.0-alpha.1d71802", | ||
"description": "Xata.io SDK for TypeScript and JavaScript", | ||
@@ -23,3 +23,3 @@ "main": "./dist/index.js", | ||
"homepage": "https://github.com/xataio/client-ts/blob/main/client/README.md", | ||
"gitHead": "123dd7a5eb1a79e75fd18921d67310c4bf277237" | ||
"gitHead": "1d718029f56a7c43372b996f0a899a558fba8a6d" | ||
} |
@@ -265,5 +265,5 @@ import { BaseClient, RestRepository, XataClientOptions, XataError, XataRecord } from './'; | ||
users: RestRepository<User>, | ||
expectedRequest: ExpectedRequest, | ||
expectedRequest: ExpectedRequest[] | ExpectedRequest, | ||
callback: () => void, | ||
response?: unknown | ||
response?: any | ||
) { | ||
@@ -276,7 +276,13 @@ const request = jest.fn(async () => response); | ||
const { calls } = request.mock; | ||
expect(calls.length).toBe(1); | ||
const [method, path, body] = calls[0] as any; | ||
expect(method).toBe(expectedRequest.method); | ||
expect(path).toBe(expectedRequest.path); | ||
expect(JSON.stringify(body)).toBe(JSON.stringify(expectedRequest.body)); | ||
const requests = Array.isArray(expectedRequest) ? expectedRequest : [expectedRequest]; | ||
expect(calls.length).toBe(requests.length); | ||
for (let i = 0; i < calls.length; i++) { | ||
const [method, path, body] = calls[i] as any; | ||
expect(method).toBe(requests[i].method); | ||
expect(path).toBe(requests[i].path); | ||
expect(JSON.stringify(body)).toBe(JSON.stringify(requests[i].body)); | ||
} | ||
} | ||
@@ -290,3 +296,6 @@ | ||
const expected = { method: 'POST', path: '/tables/users/query', body: {} }; | ||
expectRequest(users, expected, () => users.getMany(), { records: [] }); | ||
expectRequest(users, expected, () => users.getMany(), { | ||
records: [], | ||
meta: { page: { cursor: '', more: false } } | ||
}); | ||
}); | ||
@@ -298,3 +307,6 @@ | ||
const expected = { method: 'POST', path: '/tables/users/query', body: { filter: { $all: [{ name: 'foo' }] } } }; | ||
expectRequest(users, expected, () => users.filter('name', 'foo').getMany(), { records: [] }); | ||
expectRequest(users, expected, () => users.filter('name', 'foo').getMany(), { | ||
records: [], | ||
meta: { page: { cursor: '', more: false } } | ||
}); | ||
}); | ||
@@ -307,4 +319,4 @@ }); | ||
const result = { records: [{ id: '1234' }] }; | ||
const expected = { method: 'POST', path: '/tables/users/query', body: {} }; | ||
const result = { records: [{ id: '1234' }], meta: { page: { cursor: '', more: false } } }; | ||
const expected = { method: 'POST', path: '/tables/users/query', body: { page: { size: 1 } } }; | ||
expectRequest( | ||
@@ -324,4 +336,4 @@ users, | ||
const result = { records: [] }; | ||
const expected = { method: 'POST', path: '/tables/users/query', body: {} }; | ||
const result = { records: [], meta: { page: { cursor: '', more: false } } }; | ||
const expected = { method: 'POST', path: '/tables/users/query', body: { page: { size: 1 } } }; | ||
expectRequest( | ||
@@ -387,3 +399,11 @@ users, | ||
const object = { name: 'Ada' } as User; | ||
const expected = { method: 'POST', path: '/tables/users/data', body: object }; | ||
const expected = [ | ||
{ method: 'POST', path: '/tables/users/data', body: object }, | ||
{ | ||
method: 'GET', | ||
path: '/tables/users/data/rec_1234', | ||
body: undefined | ||
} | ||
]; | ||
expectRequest( | ||
@@ -390,0 +410,0 @@ users, |
249
src/index.ts
@@ -0,1 +1,3 @@ | ||
import { errors } from './util/errors'; | ||
export interface XataRecord { | ||
@@ -93,3 +95,9 @@ id: string; | ||
type CursorNavigationOptions = { first?: string } | { last?: string } | { after?: string; before?: string }; | ||
type OffsetNavigationOptions = { size?: number; offset?: number }; | ||
type PaginationOptions = CursorNavigationOptions & OffsetNavigationOptions; | ||
type BulkQueryOptions<T> = { | ||
page?: PaginationOptions; | ||
/** TODO: Not implemented yet | ||
filter?: FilterConstraints<T>; | ||
@@ -102,2 +110,3 @@ sort?: | ||
| keyof T; | ||
**/ | ||
}; | ||
@@ -107,3 +116,51 @@ | ||
export class Query<T, R = T> { | ||
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; | ||
} | ||
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[] = []) { | ||
this.query = query; | ||
this.meta = meta; | ||
this.records = records; | ||
} | ||
async nextPage(size?: number, offset?: number): Promise<Page<T, R>> { | ||
return this.query.getPaginated({ page: { size, offset, after: this.meta.page.cursor } }); | ||
} | ||
async previousPage(size?: number, offset?: number): Promise<Page<T, R>> { | ||
return this.query.getPaginated({ page: { size, offset, before: this.meta.page.cursor } }); | ||
} | ||
async firstPage(size?: number, offset?: number): Promise<Page<T, R>> { | ||
return this.query.getPaginated({ page: { size, offset, first: this.meta.page.cursor } }); | ||
} | ||
async lastPage(size?: number, offset?: number): Promise<Page<T, R>> { | ||
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(): boolean { | ||
return this.meta.page.more; | ||
} | ||
} | ||
export class Query<T, R = T> implements BasePage<T, R> { | ||
table: string; | ||
@@ -118,2 +175,7 @@ repository: Repository<T>; | ||
// Cursor pagination | ||
readonly query: Query<T, R> = this; | ||
readonly meta: QueryMeta = { page: { cursor: 'start', more: true } }; | ||
readonly records: R[] = []; | ||
constructor(repository: Repository<T> | null, table: string, data: Partial<Query<T, R>>, parent?: Query<T, R>) { | ||
@@ -238,17 +300,37 @@ if (repository) { | ||
// TODO: pagination. Maybe implement different methods for different type of paginations | ||
// and one to simply get the first records returned by the query with no pagination. | ||
async getPaginated(options?: BulkQueryOptions<T>): Promise<Page<T, R>> { | ||
return this.repository._runQuery(this, options); | ||
} | ||
async *[Symbol.asyncIterator](): AsyncIterableIterator<R> { | ||
for await (const [record] of this.getIterator(1)) { | ||
yield record; | ||
} | ||
} | ||
async *getIterator(chunk: number, options: Omit<BulkQueryOptions<T>, 'page'> = {}): AsyncGenerator<R[]> { | ||
let offset = 0; | ||
let end = false; | ||
while (!end) { | ||
const { records, meta } = await this.getPaginated({ ...options, page: { size: chunk, offset } }); | ||
yield records; | ||
offset += chunk; | ||
end = !meta.page.more; | ||
} | ||
} | ||
async getMany(options?: BulkQueryOptions<T>): Promise<R[]> { | ||
// TODO: use options | ||
return this.repository.query(this); | ||
const { records } = await this.getPaginated(options); | ||
return records; | ||
} | ||
async getOne(options?: BulkQueryOptions<T>): Promise<R | null> { | ||
// TODO: use options | ||
const arr = await this.getMany(); // TODO, limit to 1 | ||
return arr[0] || null; | ||
async getOne(options: Omit<BulkQueryOptions<T>, 'page'> = {}): Promise<R | null> { | ||
const records = await this.getMany({ ...options, page: { size: 1 } }); | ||
return records[0] || null; | ||
} | ||
async deleteAll(): Promise<number> { | ||
// Return number of affected rows | ||
// TODO: Return number of affected rows | ||
return 0; | ||
@@ -261,2 +343,22 @@ } | ||
} | ||
async nextPage(size?: number, offset?: number): Promise<Page<T, R>> { | ||
return this.firstPage(size, offset); | ||
} | ||
async previousPage(size?: number, offset?: number): Promise<Page<T, R>> { | ||
return this.firstPage(size, offset); | ||
} | ||
async firstPage(size?: number, offset?: number): Promise<Page<T, R>> { | ||
return this.getPaginated({ page: { size, offset } }); | ||
} | ||
async lastPage(size?: number, offset?: number): Promise<Page<T, R>> { | ||
return this.getPaginated({ page: { size, offset, before: 'end' } }); | ||
} | ||
hasNextPage(): boolean { | ||
return this.meta.page.more; | ||
} | ||
} | ||
@@ -271,2 +373,4 @@ | ||
abstract createMany(objects: Selectable<T>[]): Promise<T[]>; | ||
abstract read(id: string): Promise<T | null>; | ||
@@ -279,3 +383,3 @@ | ||
// Used by the Query object internally | ||
abstract query<R>(query: Query<T, R>): Promise<R[]>; | ||
abstract _runQuery<R>(query: Query<T, R>, options?: BulkQueryOptions<T>): Promise<Page<T, R>>; | ||
} | ||
@@ -291,18 +395,11 @@ | ||
const { fetch } = client.options; | ||
const doWeHaveFetch = typeof fetch !== 'undefined'; | ||
const isInjectedFetchProblematic = !this.client.options.fetch; | ||
if (fetch) { | ||
if (doWeHaveFetch) { | ||
this.fetch = fetch; | ||
} else if (typeof window === 'object') { | ||
this.fetch = window.fetch; | ||
} else if (typeof require === 'function') { | ||
try { | ||
this.fetch = require('node-fetch'); | ||
} catch (err) { | ||
try { | ||
this.fetch = require('cross-fetch'); | ||
} catch (err) { | ||
throw new Error('No fetch implementation found. Please provide one in the constructor'); | ||
} | ||
} | ||
} else if (isInjectedFetchProblematic) { | ||
throw new Error(errors.falsyFetchImplementation); | ||
} else { | ||
this.fetch = this.client.options.fetch; | ||
} | ||
@@ -315,3 +412,3 @@ | ||
async request(method: string, path: string, body?: unknown) { | ||
async request<T>(method: string, path: string, body?: unknown): Promise<T | undefined> { | ||
const { databaseURL, apiKey } = this.client.options; | ||
@@ -329,2 +426,3 @@ const branch = await this.client.getBranch(); | ||
}); | ||
if (!resp.ok) { | ||
@@ -344,3 +442,4 @@ try { | ||
} | ||
if (resp.status === 204) return; | ||
if (resp.status === 204) return undefined; | ||
return resp.json(); | ||
@@ -354,17 +453,47 @@ } | ||
async create(object: T): Promise<T> { | ||
const body = { ...object } as Record<string, unknown>; | ||
for (const key of Object.keys(body)) { | ||
const value = body[key]; | ||
if (value && typeof value === 'object' && typeof (value as Record<string, unknown>).id === 'string') { | ||
body[key] = (value as XataRecord).id; | ||
} | ||
const record = transformObjectLinks(object); | ||
const response = await this.request<{ | ||
id: string; | ||
xata: { version: number }; | ||
}>('POST', `/tables/${this.table}/data`, record); | ||
if (!response) { | ||
throw new Error("The server didn't return any data for the query"); | ||
} | ||
const obj = await this.request('POST', `/tables/${this.table}/data`, body); | ||
return this.client.initObject(this.table, obj); | ||
const finalObject = await this.read(response.id); | ||
if (!finalObject) { | ||
throw new Error('The server failed to save the record'); | ||
} | ||
return finalObject; | ||
} | ||
async createMany(objects: T[]): Promise<T[]> { | ||
const records = objects.map((object) => transformObjectLinks(object)); | ||
const response = await this.request<{ | ||
recordIDs: string[]; | ||
}>('POST', `/tables/${this.table}/bulk`, { records }); | ||
if (!response) { | ||
throw new Error("The server didn't return any data for the query"); | ||
} | ||
// TODO: Use filer.$any() to get all the records | ||
const finalObjects = await Promise.all(response.recordIDs.map((id) => this.read(id))); | ||
if (finalObjects.some((object) => !object)) { | ||
throw new Error('The server failed to save the record'); | ||
} | ||
return finalObjects as T[]; | ||
} | ||
async read(id: string): Promise<T | null> { | ||
try { | ||
const obj = await this.request('GET', `/tables/${this.table}/data/${id}`); | ||
return this.client.initObject(this.table, obj); | ||
const response = await this.request< | ||
T & { id: string; xata: { version: number; table?: string; warnings?: string[] } } | ||
>('GET', `/tables/${this.table}/data/${id}`); | ||
if (!response) return null; | ||
return this.client.initObject(this.table, response); | ||
} catch (err) { | ||
@@ -377,4 +506,12 @@ if ((err as XataError).status === 404) return null; | ||
async update(id: string, object: Partial<T>): Promise<T> { | ||
const obj = await this.request('PUT', `/tables/${this.table}/data/${id}`, object); | ||
return this.client.initObject(this.table, obj); | ||
const response = await this.request<{ | ||
id: string; | ||
xata: { version: number }; | ||
}>('PUT', `/tables/${this.table}/data/${id}`, object); | ||
if (!response) { | ||
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); | ||
} | ||
@@ -386,3 +523,3 @@ | ||
async query<R>(query: Query<T, R>): Promise<R[]> { | ||
async _runQuery<R>(query: Query<T, R>, options?: BulkQueryOptions<T>): Promise<Page<T, R>> { | ||
const filter = { | ||
@@ -394,8 +531,21 @@ $any: query.$any, | ||
}; | ||
const body = { | ||
filter: Object.values(filter).some(Boolean) ? filter : undefined, | ||
sort: query.$sort | ||
sort: query.$sort, | ||
page: options?.page | ||
}; | ||
const result = await this.request('POST', `/tables/${this.table}/query`, body); | ||
return result.records.map((record: object) => this.client.initObject(this.table, record)); | ||
const response = await this.request<{ | ||
records: object[]; | ||
meta: { page: { cursor: string; more: boolean } }; | ||
}>('POST', `/tables/${this.table}/query`, body); | ||
if (!response) { | ||
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<R>(this.table, record)); | ||
return new Page(query, meta, records); | ||
} | ||
@@ -421,3 +571,3 @@ } | ||
fetch?: unknown; | ||
databaseURL: string; | ||
databaseURL?: string; | ||
branch: BranchStrategyOption; | ||
@@ -524,1 +674,12 @@ apiKey: string; | ||
}; | ||
// TODO: We can find a better implementation for links | ||
const transformObjectLinks = (object: any) => { | ||
return Object.entries(object).reduce((acc, [key, value]) => { | ||
if (value && typeof value === 'object' && typeof (value as Record<string, unknown>).id === 'string') { | ||
return { ...acc, [key]: (value as XataRecord).id }; | ||
} | ||
return { ...acc, [key]: value }; | ||
}, {}); | ||
}; |
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
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
89182
13
1947