@xata.io/client
Advanced tools
Comparing version 0.1.4 to 0.1.5
export interface XataRecord { | ||
_id: string; | ||
_version: number; | ||
id: string; | ||
xata: { | ||
version: number; | ||
}; | ||
read(): Promise<this>; | ||
@@ -20,3 +22,3 @@ update(data: Selectable<this>): Promise<this>; | ||
}; | ||
export declare type Selectable<T> = Omit<OmitQueries<OmitMethods<T>>, '_id' | '_version'>; | ||
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; | ||
@@ -28,3 +30,5 @@ export declare type Include<T> = { | ||
declare type Operator = '$gt' | '$lt' | '$ge' | '$le' | '$exists' | '$notExists' | '$endsWith' | '$startsWith' | '$pattern' | '$is' | '$isNot' | '$contains' | '$includes' | '$includesSubstring' | '$includesPattern' | '$includesAll'; | ||
declare type Constraint<T> = Partial<Record<Operator, T>>; | ||
declare type Constraint<T> = { | ||
[key in Operator]?: T; | ||
}; | ||
declare type DeepConstraint<T> = T extends Record<string, any> ? { | ||
@@ -34,8 +38,8 @@ [key in keyof T]?: T[key] | DeepConstraint<T[key]>; | ||
declare type ComparableType = number | Date; | ||
export declare const gt: <T extends ComparableType>(value: T) => Partial<Record<Operator, T>>; | ||
export declare const ge: <T extends ComparableType>(value: T) => Partial<Record<Operator, T>>; | ||
export declare const gte: <T extends ComparableType>(value: T) => Partial<Record<Operator, T>>; | ||
export declare const lt: <T extends ComparableType>(value: T) => Partial<Record<Operator, T>>; | ||
export declare const lte: <T extends ComparableType>(value: T) => Partial<Record<Operator, T>>; | ||
export declare const le: <T extends ComparableType>(value: T) => Partial<Record<Operator, T>>; | ||
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>; | ||
@@ -46,5 +50,5 @@ export declare const notExists: (column: string) => Constraint<string>; | ||
export declare const pattern: (value: string) => Constraint<string>; | ||
export declare const is: <T>(value: T) => Partial<Record<Operator, T>>; | ||
export declare const isNot: <T>(value: T) => Partial<Record<Operator, T>>; | ||
export declare const contains: <T>(value: T) => Partial<Record<Operator, T>>; | ||
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>; | ||
@@ -51,0 +55,0 @@ export declare const includesSubstring: (value: string) => Constraint<string>; |
@@ -226,4 +226,4 @@ "use strict"; | ||
const value = body[key]; | ||
if (value && typeof value === 'object' && typeof value._id === 'string') { | ||
body[key] = value._id; | ||
if (value && typeof value === 'object' && typeof value.id === 'string') { | ||
body[key] = value.id; | ||
} | ||
@@ -296,11 +296,11 @@ } | ||
if (value && typeof value === 'object') { | ||
const { _id } = value; | ||
if (Object.keys(value).find((col) => !col.startsWith('_'))) { | ||
const { id } = value; | ||
if (Object.keys(value).find((col) => col === 'id')) { | ||
o[field] = this.initObject(linkTable, value); | ||
} | ||
else if (_id) { | ||
else if (id) { | ||
o[field] = { | ||
_id, | ||
id, | ||
get: () => { | ||
this.db[linkTable].read(_id); | ||
this.db[linkTable].read(id); | ||
} | ||
@@ -313,9 +313,9 @@ }; | ||
o.read = function () { | ||
return db[table].read(o['_id']); | ||
return db[table].read(o['id']); | ||
}; | ||
o.update = function (data) { | ||
return db[table].update(o['_id'], data); | ||
return db[table].update(o['id'], data); | ||
}; | ||
o.delete = function () { | ||
return db[table].delete(o['_id']); | ||
return db[table].delete(o['id']); | ||
}; | ||
@@ -322,0 +322,0 @@ for (const prop of ['read', 'update', 'delete']) { |
@@ -128,7 +128,7 @@ "use strict"; | ||
test('returns a single object', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = { records: [{ _id: '1234' }] }; | ||
const result = { records: [{ id: '1234' }] }; | ||
const expected = { method: 'POST', path: '/tables/users/query', body: {} }; | ||
expectRequest(expected, () => __awaiter(void 0, void 0, void 0, function* () { | ||
const first = yield users.select().getOne(); | ||
expect(first === null || first === void 0 ? void 0 : first._id).toBe(result.records[0]._id); | ||
expect(first === null || first === void 0 ? void 0 : first.id).toBe(result.records[0].id); | ||
}), result); | ||
@@ -155,8 +155,8 @@ })); | ||
test('updates and object successfully', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const object = { _id: 'rec_1234', _version: 1, name: 'Ada' }; | ||
const expected = { method: 'PUT', path: `/tables/users/data/${object._id}`, body: object }; | ||
const object = { id: 'rec_1234', xata: { version: 1 }, name: 'Ada' }; | ||
const expected = { method: 'PUT', path: `/tables/users/data/${object.id}`, body: object }; | ||
expectRequest(expected, () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = yield users.update(object._id, object); | ||
expect(result._id).toBe(object._id); | ||
}), { _id: object._id }); | ||
const result = yield users.update(object.id, object); | ||
expect(result.id).toBe(object.id); | ||
}), { id: object.id }); | ||
})); | ||
@@ -176,3 +176,3 @@ }); | ||
test('successful', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const created = { _id: 'rec_1234', _version: 0 }; | ||
const created = { id: 'rec_1234', _version: 0 }; | ||
const object = { name: 'Ada' }; | ||
@@ -182,5 +182,5 @@ const expected = { method: 'POST', path: '/tables/users/data', body: object }; | ||
const result = yield users.create(object); | ||
expect(result._id).toBe(created._id); | ||
expect(result.id).toBe(created.id); | ||
}), created); | ||
})); | ||
}); |
{ | ||
"name": "@xata.io/client", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Xata.io SDK for TypeScript and JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -148,3 +148,3 @@ import { BaseClient, RestRepository, XataError, XataRecord } from './'; | ||
test('returns a single object', async () => { | ||
const result = { records: [{ _id: '1234' }] }; | ||
const result = { records: [{ id: '1234' }] }; | ||
const expected = { method: 'POST', path: '/tables/users/query', body: {} }; | ||
@@ -155,3 +155,3 @@ expectRequest( | ||
const first = await users.select().getOne(); | ||
expect(first?._id).toBe(result.records[0]._id); | ||
expect(first?.id).toBe(result.records[0].id); | ||
}, | ||
@@ -187,11 +187,11 @@ result | ||
test('updates and object successfully', async () => { | ||
const object = { _id: 'rec_1234', _version: 1, name: 'Ada' } as User; | ||
const expected = { method: 'PUT', path: `/tables/users/data/${object._id}`, body: object }; | ||
const object = { id: 'rec_1234', xata: { version: 1 }, name: 'Ada' } as User; | ||
const expected = { method: 'PUT', path: `/tables/users/data/${object.id}`, body: object }; | ||
expectRequest( | ||
expected, | ||
async () => { | ||
const result = await users.update(object._id, object); | ||
expect(result._id).toBe(object._id); | ||
const result = await users.update(object.id, object); | ||
expect(result.id).toBe(object.id); | ||
}, | ||
{ _id: object._id } | ||
{ id: object.id } | ||
); | ||
@@ -213,3 +213,3 @@ }); | ||
test('successful', async () => { | ||
const created = { _id: 'rec_1234', _version: 0 }; | ||
const created = { id: 'rec_1234', _version: 0 }; | ||
const object = { name: 'Ada' } as User; | ||
@@ -221,3 +221,3 @@ const expected = { method: 'POST', path: '/tables/users/data', body: object }; | ||
const result = await users.create(object); | ||
expect(result._id).toBe(created._id); | ||
expect(result.id).toBe(created.id); | ||
}, | ||
@@ -224,0 +224,0 @@ created |
export interface XataRecord { | ||
_id: string; | ||
_version: number; | ||
id: string; | ||
xata: { | ||
version: number; | ||
}; | ||
read(): Promise<this>; | ||
@@ -26,3 +28,3 @@ update(data: Selectable<this>): Promise<this>; | ||
export type Selectable<T> = Omit<OmitQueries<OmitMethods<T>>, '_id' | '_version'>; | ||
export type Selectable<T> = Omit<OmitQueries<OmitMethods<T>>, 'id' | 'xata'>; | ||
@@ -57,3 +59,3 @@ export type Select<T, K extends keyof T> = Pick<T, K> & Queries<T> & XataRecord; | ||
// E.g. startsWith cannot be used with numbers | ||
type Constraint<T> = Partial<Record<Operator, T>>; | ||
type Constraint<T> = { [key in Operator]?: T }; | ||
@@ -343,4 +345,4 @@ type DeepConstraint<T> = T extends Record<string, any> | ||
const value = body[key]; | ||
if (value && typeof value === 'object' && typeof (value as Record<string, unknown>)._id === 'string') { | ||
body[key] = (value as XataRecord)._id; | ||
if (value && typeof value === 'object' && typeof (value as Record<string, unknown>).id === 'string') { | ||
body[key] = (value as XataRecord).id; | ||
} | ||
@@ -424,10 +426,10 @@ } | ||
if (value && typeof value === 'object') { | ||
const { _id } = value as any; | ||
if (Object.keys(value).find((col) => !col.startsWith('_'))) { | ||
const { id } = value as any; | ||
if (Object.keys(value).find((col) => col === 'id')) { | ||
o[field] = this.initObject(linkTable, value); | ||
} else if (_id) { | ||
} else if (id) { | ||
o[field] = { | ||
_id, | ||
id, | ||
get: () => { | ||
this.db[linkTable].read(_id); | ||
this.db[linkTable].read(id); | ||
} | ||
@@ -441,9 +443,9 @@ }; | ||
o.read = function () { | ||
return db[table].read(o['_id'] as string); | ||
return db[table].read(o['id'] as string); | ||
}; | ||
o.update = function (data: any) { | ||
return db[table].update(o['_id'] as string, data); | ||
return db[table].update(o['id'] as string, data); | ||
}; | ||
o.delete = function () { | ||
return db[table].delete(o['_id'] as string); | ||
return db[table].delete(o['id'] as string); | ||
}; | ||
@@ -450,0 +452,0 @@ |
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
1266
48585