@xata.io/client 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +258 -1
- package/dist/index.cjs +12 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +128 -25
- package/dist/index.mjs +12 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -2
package/dist/index.d.ts
CHANGED
@@ -2873,7 +2873,7 @@ declare type ValueAtColumn<O, P extends SelectableColumn<O>> = P extends '*' ? V
|
|
2873
2873
|
declare type MAX_RECURSION = 5;
|
2874
2874
|
declare type NestedColumns<O, RecursivePath extends any[]> = RecursivePath['length'] extends MAX_RECURSION ? never : If<IsObject<O>, Values<{
|
2875
2875
|
[K in DataProps<O>]: If<IsArray<RemoveNullable<O[K]>>, K, // If the property is an array, we stop recursion. We don't support object arrays yet
|
2876
|
-
If<IsObject<RemoveNullable<O[K]>>, RemoveNullable<O[K]> extends XataRecord ? SelectableColumn<RemoveNullable<O[K]>, [...RecursivePath, O[K]]> extends string ? K | `${K}.${
|
2876
|
+
If<IsObject<RemoveNullable<O[K]>>, RemoveNullable<O[K]> extends XataRecord ? SelectableColumn<RemoveNullable<O[K]>, [...RecursivePath, O[K]]> extends infer Column ? Column extends string ? K | `${K}.${Column}` : never : never : `${K}.${StringKeys<RemoveNullable<O[K]>> | '*'}`, // This allows usage of objects that are not links
|
2877
2877
|
K>>;
|
2878
2878
|
}>, never>;
|
2879
2879
|
declare type DataProps<O> = Exclude<StringKeys<O>, StringKeys<XataRecord>>;
|
@@ -2902,16 +2902,11 @@ interface BaseData {
|
|
2902
2902
|
/**
|
2903
2903
|
* Represents a persisted record from the database.
|
2904
2904
|
*/
|
2905
|
-
interface XataRecord extends Identifiable {
|
2905
|
+
interface XataRecord<ExtraMetadata extends Record<string, unknown> = Record<string, unknown>> extends Identifiable {
|
2906
2906
|
/**
|
2907
|
-
*
|
2907
|
+
* Get metadata of this record.
|
2908
2908
|
*/
|
2909
|
-
|
2910
|
-
/**
|
2911
|
-
* Number that is increased every time the record is updated.
|
2912
|
-
*/
|
2913
|
-
version: number;
|
2914
|
-
};
|
2909
|
+
getMetadata(): XataRecordMetadata & ExtraMetadata;
|
2915
2910
|
/**
|
2916
2911
|
* Retrieves a refreshed copy of the current record from the database.
|
2917
2912
|
*/
|
@@ -2919,7 +2914,7 @@ interface XataRecord extends Identifiable {
|
|
2919
2914
|
/**
|
2920
2915
|
* Performs a partial update of the current record. On success a new object is
|
2921
2916
|
* returned and the current object is not mutated.
|
2922
|
-
* @param
|
2917
|
+
* @param partialUpdate The columns and their values that have to be updated.
|
2923
2918
|
* @returns A new record containing the latest values for all the columns of the current record.
|
2924
2919
|
*/
|
2925
2920
|
update(partialUpdate: Partial<EditableData<Omit<this, keyof XataRecord>>>): Promise<Readonly<SelectedPick<this, ['*']>>>;
|
@@ -2938,11 +2933,18 @@ declare type Link<Record extends XataRecord> = Omit<XataRecord, 'read' | 'update
|
|
2938
2933
|
/**
|
2939
2934
|
* Performs a partial update of the current record. On success a new object is
|
2940
2935
|
* returned and the current object is not mutated.
|
2941
|
-
* @param
|
2936
|
+
* @param partialUpdate The columns and their values that have to be updated.
|
2942
2937
|
* @returns A new record containing the latest values for all the columns of the current record.
|
2943
2938
|
*/
|
2944
2939
|
update(partialUpdate: Partial<EditableData<Omit<Record, keyof XataRecord>>>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
|
2945
2940
|
};
|
2941
|
+
declare type XataRecordMetadata = {
|
2942
|
+
/**
|
2943
|
+
* Number that is increased every time the record is updated.
|
2944
|
+
*/
|
2945
|
+
version: number;
|
2946
|
+
warnings?: string[];
|
2947
|
+
};
|
2946
2948
|
declare function isIdentifiable(x: any): x is Identifiable & Record<string, unknown>;
|
2947
2949
|
declare function isXataRecord(x: any): x is XataRecord & Record<string, unknown>;
|
2948
2950
|
declare type EditableData<O extends BaseData> = {
|
@@ -3114,17 +3116,27 @@ declare class Query<Record extends XataRecord, Result extends XataRecord = Recor
|
|
3114
3116
|
*
|
3115
3117
|
* ```
|
3116
3118
|
* query.filter("columnName", columnValue)
|
3117
|
-
* query.filter(
|
3118
|
-
*
|
3119
|
-
*
|
3119
|
+
* query.filter("columnName", operator(columnValue)) // Use gt, gte, lt, lte, startsWith,...
|
3120
|
+
* ```
|
3121
|
+
*
|
3122
|
+
* @param column The name of the column to filter.
|
3123
|
+
* @param value The value to filter.
|
3124
|
+
* @returns A new Query object.
|
3125
|
+
*/
|
3126
|
+
filter<F extends SelectableColumn<Record>>(column: F, value: Filter<ValueAtColumn<Record, F>>): Query<Record, Result>;
|
3127
|
+
/**
|
3128
|
+
* Builds a new query object adding one or more constraints. Examples:
|
3129
|
+
*
|
3130
|
+
* ```
|
3131
|
+
* query.filter({ "columnName": columnValue })
|
3120
3132
|
* query.filter({
|
3121
3133
|
* "columnName": operator(columnValue) // Use gt, gte, lt, lte, startsWith,...
|
3122
3134
|
* })
|
3123
3135
|
* ```
|
3124
3136
|
*
|
3137
|
+
* @param filters A filter object
|
3125
3138
|
* @returns A new Query object.
|
3126
3139
|
*/
|
3127
|
-
filter<F extends SelectableColumn<Record>>(column: F, value: Filter<ValueAtColumn<Record, F>>): Query<Record, Result>;
|
3128
3140
|
filter(filters: Filter<Record>): Query<Record, Result>;
|
3129
3141
|
/**
|
3130
3142
|
* Builds a new query with a new sort option.
|
@@ -3139,45 +3151,114 @@ declare class Query<Record extends XataRecord, Result extends XataRecord = Recor
|
|
3139
3151
|
* @returns A new Query object.
|
3140
3152
|
*/
|
3141
3153
|
select<K extends SelectableColumn<Record>>(columns: NonEmptyArray<K>): Query<Record, SelectedPick<Record, NonEmptyArray<K>>>;
|
3154
|
+
/**
|
3155
|
+
* Get paginated results
|
3156
|
+
*
|
3157
|
+
* @returns A page of results
|
3158
|
+
*/
|
3142
3159
|
getPaginated(): Promise<Page<Record, Result>>;
|
3160
|
+
/**
|
3161
|
+
* Get paginated results
|
3162
|
+
*
|
3163
|
+
* @param options Pagination options
|
3164
|
+
* @returns A page of results
|
3165
|
+
*/
|
3143
3166
|
getPaginated(options: OmitBy<QueryOptions<Record>, 'columns'>): Promise<Page<Record, Result>>;
|
3167
|
+
/**
|
3168
|
+
* Get paginated results
|
3169
|
+
*
|
3170
|
+
* @param options Pagination options
|
3171
|
+
* @returns A page of results
|
3172
|
+
*/
|
3144
3173
|
getPaginated<Options extends RequiredBy<QueryOptions<Record>, 'columns'>>(options: Options): Promise<Page<Record, SelectedPick<Record, typeof options['columns']>>>;
|
3174
|
+
/**
|
3175
|
+
* Get results in an iterator
|
3176
|
+
*
|
3177
|
+
* @async
|
3178
|
+
* @returns Async interable of results
|
3179
|
+
*/
|
3145
3180
|
[Symbol.asyncIterator](): AsyncIterableIterator<Result>;
|
3181
|
+
/**
|
3182
|
+
* Build an iterator of results
|
3183
|
+
*
|
3184
|
+
* @returns Async generator of results array
|
3185
|
+
*/
|
3146
3186
|
getIterator(): AsyncGenerator<Result[]>;
|
3187
|
+
/**
|
3188
|
+
* Build an iterator of results
|
3189
|
+
*
|
3190
|
+
* @param options Pagination options with batchSize
|
3191
|
+
* @returns Async generator of results array
|
3192
|
+
*/
|
3147
3193
|
getIterator(options: OmitBy<QueryOptions<Record>, 'columns' | 'pagination'> & {
|
3148
3194
|
batchSize?: number;
|
3149
3195
|
}): AsyncGenerator<Result[]>;
|
3196
|
+
/**
|
3197
|
+
* Build an iterator of results
|
3198
|
+
*
|
3199
|
+
* @param options Pagination options with batchSize
|
3200
|
+
* @returns Async generator of results array
|
3201
|
+
*/
|
3150
3202
|
getIterator<Options extends RequiredBy<OmitBy<QueryOptions<Record>, 'pagination'>, 'columns'> & {
|
3151
3203
|
batchSize?: number;
|
3152
3204
|
}>(options: Options): AsyncGenerator<SelectedPick<Record, typeof options['columns']>[]>;
|
3153
3205
|
/**
|
3154
3206
|
* Performs the query in the database and returns a set of results.
|
3155
|
-
* @param options Additional options to be used when performing the query.
|
3156
3207
|
* @returns An array of records from the database.
|
3157
3208
|
*/
|
3158
3209
|
getMany(): Promise<Result[]>;
|
3210
|
+
/**
|
3211
|
+
* Performs the query in the database and returns a set of results.
|
3212
|
+
* @param options Additional options to be used when performing the query.
|
3213
|
+
* @returns An array of records from the database.
|
3214
|
+
*/
|
3159
3215
|
getMany(options: OmitBy<QueryOptions<Record>, 'columns'>): Promise<Result[]>;
|
3216
|
+
/**
|
3217
|
+
* Performs the query in the database and returns a set of results.
|
3218
|
+
* @param options Additional options to be used when performing the query.
|
3219
|
+
* @returns An array of records from the database.
|
3220
|
+
*/
|
3160
3221
|
getMany<Options extends RequiredBy<QueryOptions<Record>, 'columns'>>(options: Options): Promise<SelectedPick<Record, typeof options['columns']>[]>;
|
3161
3222
|
/**
|
3162
3223
|
* Performs the query in the database and returns all the results.
|
3163
3224
|
* Warning: If there are a large number of results, this method can have performance implications.
|
3164
|
-
* @param options Additional options to be used when performing the query.
|
3165
3225
|
* @returns An array of records from the database.
|
3166
3226
|
*/
|
3167
3227
|
getAll(): Promise<Result[]>;
|
3228
|
+
/**
|
3229
|
+
* Performs the query in the database and returns all the results.
|
3230
|
+
* Warning: If there are a large number of results, this method can have performance implications.
|
3231
|
+
* @param options Additional options to be used when performing the query.
|
3232
|
+
* @returns An array of records from the database.
|
3233
|
+
*/
|
3168
3234
|
getAll(options: OmitBy<QueryOptions<Record>, 'columns' | 'pagination'> & {
|
3169
3235
|
batchSize?: number;
|
3170
3236
|
}): Promise<Result[]>;
|
3237
|
+
/**
|
3238
|
+
* Performs the query in the database and returns all the results.
|
3239
|
+
* Warning: If there are a large number of results, this method can have performance implications.
|
3240
|
+
* @param options Additional options to be used when performing the query.
|
3241
|
+
* @returns An array of records from the database.
|
3242
|
+
*/
|
3171
3243
|
getAll<Options extends RequiredBy<OmitBy<QueryOptions<Record>, 'pagination'>, 'columns'> & {
|
3172
3244
|
batchSize?: number;
|
3173
3245
|
}>(options: Options): Promise<SelectedPick<Record, typeof options['columns']>[]>;
|
3174
3246
|
/**
|
3175
3247
|
* Performs the query in the database and returns the first result.
|
3176
|
-
* @param options Additional options to be used when performing the query.
|
3177
3248
|
* @returns The first record that matches the query, or null if no record matched the query.
|
3178
3249
|
*/
|
3179
3250
|
getFirst(): Promise<Result | null>;
|
3251
|
+
/**
|
3252
|
+
* Performs the query in the database and returns the first result.
|
3253
|
+
* @param options Additional options to be used when performing the query.
|
3254
|
+
* @returns The first record that matches the query, or null if no record matched the query.
|
3255
|
+
*/
|
3180
3256
|
getFirst(options: OmitBy<QueryOptions<Record>, 'columns' | 'pagination'>): Promise<Result | null>;
|
3257
|
+
/**
|
3258
|
+
* Performs the query in the database and returns the first result.
|
3259
|
+
* @param options Additional options to be used when performing the query.
|
3260
|
+
* @returns The first record that matches the query, or null if no record matched the query.
|
3261
|
+
*/
|
3181
3262
|
getFirst<Options extends RequiredBy<OmitBy<QueryOptions<Record>, 'pagination'>, 'columns'>>(options: Options): Promise<SelectedPick<Record, typeof options['columns']> | null>;
|
3182
3263
|
/**
|
3183
3264
|
* Builds a new query object adding a cache TTL in milliseconds.
|
@@ -3185,10 +3266,33 @@ declare class Query<Record extends XataRecord, Result extends XataRecord = Recor
|
|
3185
3266
|
* @returns A new Query object.
|
3186
3267
|
*/
|
3187
3268
|
cache(ttl: number): Query<Record, Result>;
|
3269
|
+
/**
|
3270
|
+
* Retrieve next page of records
|
3271
|
+
*
|
3272
|
+
* @returns A new page object.
|
3273
|
+
*/
|
3188
3274
|
nextPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
|
3275
|
+
/**
|
3276
|
+
* Retrieve previous page of records
|
3277
|
+
*
|
3278
|
+
* @returns A new page object
|
3279
|
+
*/
|
3189
3280
|
previousPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
|
3281
|
+
/**
|
3282
|
+
* Retrieve first page of records
|
3283
|
+
*
|
3284
|
+
* @returns A new page object
|
3285
|
+
*/
|
3190
3286
|
firstPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
|
3287
|
+
/**
|
3288
|
+
* Retrieve last page of records
|
3289
|
+
*
|
3290
|
+
* @returns A new page object
|
3291
|
+
*/
|
3191
3292
|
lastPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
|
3293
|
+
/**
|
3294
|
+
* @returns Boolean indicating if there is a next page
|
3295
|
+
*/
|
3192
3296
|
hasNextPage(): boolean;
|
3193
3297
|
}
|
3194
3298
|
|
@@ -3525,13 +3629,12 @@ declare class SearchPlugin<Schemas extends Record<string, BaseData>> extends Xat
|
|
3525
3629
|
constructor(db: SchemaPluginResult<Schemas>);
|
3526
3630
|
build({ getFetchProps }: XataPluginOptions): SearchPluginResult<Schemas>;
|
3527
3631
|
}
|
3528
|
-
declare type SearchXataRecord = XataRecord
|
3529
|
-
|
3530
|
-
|
3531
|
-
|
3532
|
-
|
3533
|
-
|
3534
|
-
};
|
3632
|
+
declare type SearchXataRecord = XataRecord<SearchExtraProperties>;
|
3633
|
+
declare type SearchExtraProperties = {
|
3634
|
+
table: string;
|
3635
|
+
highlight?: {
|
3636
|
+
[key: string]: string[] | {
|
3637
|
+
[key: string]: any;
|
3535
3638
|
};
|
3536
3639
|
};
|
3537
3640
|
};
|
package/dist/index.mjs
CHANGED
@@ -1138,7 +1138,9 @@ function isIdentifiable(x) {
|
|
1138
1138
|
return isObject(x) && isString(x?.id);
|
1139
1139
|
}
|
1140
1140
|
function isXataRecord(x) {
|
1141
|
-
|
1141
|
+
const record = x;
|
1142
|
+
const metadata = record?.getMetadata();
|
1143
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1142
1144
|
}
|
1143
1145
|
|
1144
1146
|
function isSortFilterString(value) {
|
@@ -1528,7 +1530,8 @@ const transformObjectLinks = (object) => {
|
|
1528
1530
|
};
|
1529
1531
|
const initObject = (db, schema, table, object) => {
|
1530
1532
|
const result = {};
|
1531
|
-
|
1533
|
+
const { xata, ...rest } = object ?? {};
|
1534
|
+
Object.assign(result, rest);
|
1532
1535
|
const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
|
1533
1536
|
if (!columns)
|
1534
1537
|
console.error(`Table ${table} not found in schema`);
|
@@ -1536,10 +1539,10 @@ const initObject = (db, schema, table, object) => {
|
|
1536
1539
|
const value = result[column.name];
|
1537
1540
|
switch (column.type) {
|
1538
1541
|
case "datetime": {
|
1539
|
-
const date = new Date(value);
|
1540
|
-
if (isNaN(date.getTime())) {
|
1542
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1543
|
+
if (date && isNaN(date.getTime())) {
|
1541
1544
|
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1542
|
-
} else {
|
1545
|
+
} else if (date) {
|
1543
1546
|
result[column.name] = date;
|
1544
1547
|
}
|
1545
1548
|
break;
|
@@ -1564,7 +1567,10 @@ const initObject = (db, schema, table, object) => {
|
|
1564
1567
|
result.delete = function() {
|
1565
1568
|
return db[table].delete(result["id"]);
|
1566
1569
|
};
|
1567
|
-
|
1570
|
+
result.getMetadata = function() {
|
1571
|
+
return xata;
|
1572
|
+
};
|
1573
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1568
1574
|
Object.defineProperty(result, prop, { enumerable: false });
|
1569
1575
|
}
|
1570
1576
|
Object.freeze(result);
|