shelving 1.73.1 → 1.73.2

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.
@@ -4,12 +4,6 @@ import { Sortable, SortConstraints } from "./SortConstraints.js";
4
4
  import { Constraint } from "./Constraint.js";
5
5
  import { FilterList } from "./FilterConstraint.js";
6
6
  import { SortKeys, SortList } from "./SortConstraint.js";
7
- /** Set of props for a query defined as an object. */
8
- export declare type QueryProps<T extends Data> = {
9
- readonly filter?: FilterList<T>;
10
- readonly sort?: SortList<T>;
11
- readonly limit?: number | null;
12
- };
13
7
  /** Interface that combines Filterable, Sortable, Sliceable. */
14
8
  export interface Queryable<T extends Data> extends Filterable<T>, Sortable<T> {
15
9
  /**
@@ -31,15 +25,13 @@ export interface Queryable<T extends Data> extends Filterable<T>, Sortable<T> {
31
25
  readonly limit: number | null;
32
26
  /** Return a new instance of this class with a limit set. */
33
27
  max(max: number | null): this;
34
- /** Return a new instance of this class with new filters, sorts, limits set. */
35
- query(query: QueryProps<T>): this;
36
28
  }
37
29
  /** Allows filtering, sorting, and limiting on a set of results. */
38
30
  export declare class QueryConstraints<T extends Data = Data> extends Constraint<T> implements Queryable<T> {
39
31
  readonly filters: FilterConstraints<T>;
40
32
  readonly sorts: SortConstraints<T>;
41
33
  readonly limit: number | null;
42
- constructor({ filter, sort, limit }?: QueryProps<T>);
34
+ constructor(filters?: FilterList<T>, sorts?: SortList<T>, limit?: number | null);
43
35
  filter(...filters: FilterList<T>[]): this;
44
36
  get unfilter(): this;
45
37
  match(item: T): boolean;
@@ -49,7 +41,6 @@ export declare class QueryConstraints<T extends Data = Data> extends Constraint<
49
41
  after(item: T): this;
50
42
  before(item: T): this;
51
43
  max(limit: number | null): this;
52
- query({ sort, limit, filter }: QueryProps<T>): this;
53
44
  transform(items: Iterable<T>): Iterable<T>;
54
45
  toString(): string;
55
46
  }
@@ -8,13 +8,12 @@ import { FilterConstraint } from "./FilterConstraint.js";
8
8
  // Instances to save resources for the default case (empty query).
9
9
  const EMPTY_FILTERS = new FilterConstraints(); // eslint-disable-line @typescript-eslint/no-explicit-any
10
10
  const EMPTY_SORTS = new SortConstraints(); // eslint-disable-line @typescript-eslint/no-explicit-any
11
- const EMPTY_PROPS = { filter: EMPTY_FILTERS, sort: EMPTY_SORTS, limit: null }; // eslint-disable-line @typescript-eslint/no-explicit-any
12
11
  /** Allows filtering, sorting, and limiting on a set of results. */
13
12
  export class QueryConstraints extends Constraint {
14
- constructor({ filter = EMPTY_FILTERS, sort = EMPTY_SORTS, limit = null } = EMPTY_PROPS) {
13
+ constructor(filters = EMPTY_FILTERS, sorts = EMPTY_SORTS, limit = null) {
15
14
  super();
16
- this.filters = filter instanceof FilterConstraints ? filter : new FilterConstraints(filter);
17
- this.sorts = sort instanceof SortConstraints ? sort : new SortConstraints(sort);
15
+ this.filters = filters instanceof FilterConstraints ? filters : new FilterConstraints(filters);
16
+ this.sorts = sorts instanceof SortConstraints ? sorts : new SortConstraints(sorts);
18
17
  this.limit = limit;
19
18
  }
20
19
  // Implement `Filterable`
@@ -81,15 +80,6 @@ export class QueryConstraints extends Constraint {
81
80
  limit,
82
81
  };
83
82
  }
84
- query({ sort, limit, filter }) {
85
- return {
86
- __proto__: Object.getPrototypeOf(this),
87
- ...this,
88
- filters: filter ? this.filters.filter(filter) : this.filters,
89
- sorts: sort ? this.sorts.sort(sort) : this.sorts,
90
- limit: limit !== undefined ? limit : this.limit,
91
- };
92
- }
93
83
  // Implement `Rule`
94
84
  transform(items) {
95
85
  const sorted = this.sorts.transform(this.filters.transform(items));
@@ -97,7 +87,7 @@ export class QueryConstraints extends Constraint {
97
87
  }
98
88
  // Implement toString()
99
89
  toString() {
100
- return `{"filter":${this.filters.toString()}},"sort":${this.sorts.toString()},"limit":${this.limit}}`;
90
+ return `{"filters":${this.filters.toString()}},"sorts":${this.sorts.toString()},"limit":${this.limit}}`;
101
91
  }
102
92
  }
103
93
  function* _getAfterFilters(sorts, item) {
@@ -1,5 +1,6 @@
1
- import type { QueryProps } from "../constraint/QueryConstraints.js";
2
1
  import type { Datas, Key } from "../util/data.js";
2
+ import type { FilterList } from "../constraint/FilterConstraint.js";
3
+ import type { SortList } from "../constraint/SortConstraint.js";
3
4
  import type { ItemData, AsyncItem, Item } from "./Item.js";
4
5
  import type { AsyncDatabase, Database } from "./Database.js";
5
6
  import type { AsyncQuery, Query } from "./Query.js";
@@ -8,7 +9,7 @@ interface CollectionInterface<T extends Datas = Datas, K extends Key<T> = Key<T>
8
9
  readonly db: Database<T> | AsyncDatabase<T>;
9
10
  readonly collection: K;
10
11
  /** Create a query on this item's collection. */
11
- query(query?: QueryProps<ItemData<T[K]>>): Query<T, K> | AsyncQuery<T, K>;
12
+ query(filters?: FilterList<ItemData<T[K]>>, sorts?: SortList<ItemData<T[K]>>, limit?: number | null): Query<T, K> | AsyncQuery<T, K>;
12
13
  /** Create a query on this item's collection. */
13
14
  item(id: string): Item<T, K> | AsyncItem<T, K>;
14
15
  /** Add an item to this collection. */
@@ -20,7 +21,7 @@ export declare class Collection<T extends Datas = Datas, K extends Key<T> = Key<
20
21
  readonly db: Database<T>;
21
22
  readonly collection: K;
22
23
  constructor(db: Database<T>, collection: K);
23
- query(query?: QueryProps<ItemData<T[K]>>): Query<T, K>;
24
+ query(filters?: FilterList<ItemData<T[K]>>, sorts?: SortList<ItemData<T[K]>>, limit?: number | null): Query<T, K>;
24
25
  item(id: string): Item<T, K>;
25
26
  add(data: T[K]): string;
26
27
  toString(): K;
@@ -30,7 +31,7 @@ export declare class AsyncCollection<T extends Datas = Datas, K extends Key<T> =
30
31
  readonly db: AsyncDatabase<T>;
31
32
  readonly collection: K;
32
33
  constructor(db: AsyncDatabase<T>, collection: K);
33
- query(query?: QueryProps<ItemData<T[K]>>): AsyncQuery<T, K>;
34
+ query(filters?: FilterList<ItemData<T[K]>>, sorts?: SortList<ItemData<T[K]>>, limit?: number | null): AsyncQuery<T, K>;
34
35
  item(id: string): AsyncItem<T, K>;
35
36
  add(data: T[K]): Promise<string>;
36
37
  toString(): K;
package/db/Collection.js CHANGED
@@ -4,8 +4,8 @@ export class Collection {
4
4
  this.db = db;
5
5
  this.collection = collection;
6
6
  }
7
- query(query) {
8
- return this.db.query(this.collection, query);
7
+ query(filters, sorts, limit) {
8
+ return this.db.query(this.collection, filters, sorts, limit);
9
9
  }
10
10
  item(id) {
11
11
  return this.db.item(this.collection, id);
@@ -23,8 +23,8 @@ export class AsyncCollection {
23
23
  this.db = db;
24
24
  this.collection = collection;
25
25
  }
26
- query(query) {
27
- return this.db.query(this.collection, query);
26
+ query(filters, sorts, limit) {
27
+ return this.db.query(this.collection, filters, sorts, limit);
28
28
  }
29
29
  item(id) {
30
30
  return this.db.item(this.collection, id);
package/db/Database.d.ts CHANGED
@@ -1,17 +1,18 @@
1
1
  import type { Key, Datas } from "../util/data.js";
2
- import { ItemData } from "../db/Item.js";
3
2
  import type { AsyncProvider, Provider } from "../provider/Provider.js";
4
- import type { QueryProps } from "../constraint/QueryConstraints.js";
3
+ import type { FilterList } from "../constraint/FilterConstraint.js";
4
+ import type { SortList } from "../constraint/SortConstraint.js";
5
+ import type { ItemData } from "../db/Item.js";
5
6
  import { Item, AsyncItem } from "./Item.js";
6
7
  import { Query, AsyncQuery } from "./Query.js";
7
- import { AsyncCollection, Collection } from "./Collection.js";
8
+ import { Collection, AsyncCollection } from "./Collection.js";
8
9
  /** Database with a synchronous or asynchronous provider. */
9
10
  interface AbstractDatabase<T extends Datas> {
10
11
  readonly provider: Provider<T> | AsyncProvider<T>;
11
12
  /** Create a query on a collection in this database. */
12
13
  collection<K extends Key<T>>(collection: K): Collection<T, K> | AsyncCollection<T, K>;
13
14
  /** Create a query on a collection in this database. */
14
- query<K extends Key<T>>(collection: K, query?: QueryProps<ItemData<T[K]>>): Query<T, K> | AsyncQuery<T, K>;
15
+ query<K extends Key<T>>(collection: K, filters?: FilterList<ItemData<T[K]>>, sorts?: SortList<ItemData<T[K]>>, limit?: number | null): Query<T, K> | AsyncQuery<T, K>;
15
16
  /** Reference an item in a collection in this database. */
16
17
  item<K extends Key<T>>(collection: K, id: string): Item<T, K> | AsyncItem<T, K>;
17
18
  }
@@ -20,7 +21,7 @@ export declare class Database<T extends Datas = Datas> implements AbstractDataba
20
21
  readonly provider: Provider<T>;
21
22
  constructor(provider: Provider<T>);
22
23
  collection<K extends Key<T>>(collection: K): Collection<T, K>;
23
- query<K extends Key<T>>(collection: K, query?: QueryProps<ItemData<T[K]>>): Query<T, K>;
24
+ query<K extends Key<T>>(collection: K, filters?: FilterList<ItemData<T[K]>>, sorts?: SortList<ItemData<T[K]>>, limit?: number | null): Query<T, K>;
24
25
  item<K extends Key<T>>(collection: K, id: string): Item<T, K>;
25
26
  }
26
27
  /** Database with a synchronous provider. */
@@ -28,7 +29,7 @@ export declare class AsyncDatabase<T extends Datas = Datas> implements AbstractD
28
29
  readonly provider: AsyncProvider<T>;
29
30
  constructor(provider: AsyncProvider<T>);
30
31
  collection<K extends Key<T>>(collection: K): AsyncCollection<T, K>;
31
- query<K extends Key<T>>(collection: K, query?: QueryProps<ItemData<T[K]>>): AsyncQuery<T, K>;
32
+ query<K extends Key<T>>(collection: K, filters?: FilterList<ItemData<T[K]>>, sorts?: SortList<ItemData<T[K]>>, limit?: number | null): AsyncQuery<T, K>;
32
33
  item<K extends Key<T>>(collection: K, id: string): AsyncItem<T, K>;
33
34
  }
34
35
  export {};
package/db/Database.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Item, AsyncItem } from "./Item.js";
2
2
  import { Query, AsyncQuery } from "./Query.js";
3
- import { AsyncCollection, Collection } from "./Collection.js";
3
+ import { Collection, AsyncCollection } from "./Collection.js";
4
4
  /** Database with a synchronous provider. */
5
5
  export class Database {
6
6
  constructor(provider) {
@@ -9,8 +9,8 @@ export class Database {
9
9
  collection(collection) {
10
10
  return new Collection(this, collection);
11
11
  }
12
- query(collection, query) {
13
- return new Query(this, collection, query);
12
+ query(collection, filters, sorts, limit) {
13
+ return new Query(this, collection, filters, sorts, limit);
14
14
  }
15
15
  item(collection, id) {
16
16
  return new Item(this, collection, id);
@@ -24,8 +24,8 @@ export class AsyncDatabase {
24
24
  collection(collection) {
25
25
  return new AsyncCollection(this, collection);
26
26
  }
27
- query(collection, query) {
28
- return new AsyncQuery(this, collection, query);
27
+ query(collection, filters, sorts, limit) {
28
+ return new AsyncQuery(this, collection, filters, sorts, limit);
29
29
  }
30
30
  item(collection, id) {
31
31
  return new AsyncItem(this, collection, id);
package/db/Item.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { getData } from "../util/data.js";
2
+ import { FilterConstraint } from "../constraint/FilterConstraint.js";
2
3
  import { DataUpdate } from "../update/DataUpdate.js";
3
4
  /** Reference to an item in a synchronous provider. */
4
5
  export class Item {
@@ -8,7 +9,7 @@ export class Item {
8
9
  this.id = id;
9
10
  }
10
11
  get optional() {
11
- return this.db.query(this.collection, { filter: { id: this.id }, limit: 1 });
12
+ return this.db.query(this.collection, new FilterConstraint("id", this.id), undefined, 1);
12
13
  }
13
14
  get exists() {
14
15
  return !!this.db.provider.getItem(this.collection, this.id);
@@ -43,7 +44,7 @@ export class AsyncItem {
43
44
  this.id = id;
44
45
  }
45
46
  get optional() {
46
- return this.db.query(this.collection, { filter: { id: this.id }, limit: 1 });
47
+ return this.db.query(this.collection, new FilterConstraint("id", this.id), undefined, 1);
47
48
  }
48
49
  get exists() {
49
50
  return this.db.provider.getItem(this.collection, this.id).then(Boolean);
package/db/Query.d.ts CHANGED
@@ -2,7 +2,9 @@ import type { Dispatch } from "../util/function.js";
2
2
  import type { Key, Datas } from "../util/data.js";
3
3
  import type { PartialObserver } from "../observe/Observer.js";
4
4
  import type { Observable, Unsubscribe } from "../observe/Observable.js";
5
- import { QueryConstraints, QueryProps } from "../constraint/QueryConstraints.js";
5
+ import type { FilterList } from "../constraint/FilterConstraint.js";
6
+ import type { SortList } from "../constraint/SortConstraint.js";
7
+ import { QueryConstraints } from "../constraint/QueryConstraints.js";
6
8
  import { DataUpdate, PropUpdates } from "../update/DataUpdate.js";
7
9
  import type { ItemArray, ItemValue, ItemData } from "./Item.js";
8
10
  import type { AsyncDatabase, Database } from "./Database.js";
@@ -76,7 +78,7 @@ interface QueryInterface<T extends Datas = Datas, K extends Key<T> = Key<T>> ext
76
78
  export declare class Query<T extends Datas = Datas, K extends Key<T> = Key<T>> extends QueryConstraints<ItemData<T[K]>> implements QueryInterface<T, K> {
77
79
  readonly db: Database<T>;
78
80
  readonly collection: K;
79
- constructor(db: Database<T>, collection: K, props?: QueryProps<ItemData<T[K]>>);
81
+ constructor(db: Database<T>, collection: K, filters?: FilterList<ItemData<T[K]>>, sorts?: SortList<ItemData<T[K]>>, limit?: number | null);
80
82
  get value(): ItemArray<T[K]>;
81
83
  get count(): number;
82
84
  get exists(): boolean;
@@ -94,7 +96,7 @@ export declare class Query<T extends Datas = Datas, K extends Key<T> = Key<T>> e
94
96
  export declare class AsyncQuery<T extends Datas = Datas, K extends Key<T> = Key<T>> extends QueryConstraints<ItemData<T[K]>> implements QueryInterface<T, K> {
95
97
  readonly db: AsyncDatabase<T>;
96
98
  readonly collection: K;
97
- constructor(db: AsyncDatabase<T>, collection: K, props?: QueryProps<ItemData<T[K]>>);
99
+ constructor(db: AsyncDatabase<T>, collection: K, filters?: FilterList<ItemData<T[K]>>, sorts?: SortList<ItemData<T[K]>>, limit?: number | null);
98
100
  closed?: boolean;
99
101
  get value(): Promise<ItemArray<T[K]>>;
100
102
  get count(): Promise<number>;
package/db/Query.js CHANGED
@@ -4,8 +4,8 @@ import { countItems, hasItems } from "../util/iterate.js";
4
4
  import { DataUpdate } from "../update/DataUpdate.js";
5
5
  /** Reference to a set of items in a provider. */
6
6
  export class Query extends QueryConstraints {
7
- constructor(db, collection, props) {
8
- super(props);
7
+ constructor(db, collection, filters, sorts, limit) {
8
+ super(filters, sorts, limit);
9
9
  this.db = db;
10
10
  this.collection = collection;
11
11
  }
@@ -48,8 +48,8 @@ export class Query extends QueryConstraints {
48
48
  }
49
49
  /** Reference to a set of items in a provider. */
50
50
  export class AsyncQuery extends QueryConstraints {
51
- constructor(db, collection, props) {
52
- super(props);
51
+ constructor(db, collection, filters, sorts, limit) {
52
+ super(filters, sorts, limit);
53
53
  this.db = db;
54
54
  this.collection = collection;
55
55
  }
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.73.1",
14
+ "version": "1.73.2",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
@@ -72,4 +72,4 @@ export class BatchProvider extends AsyncThroughProvider {
72
72
  }
73
73
  }
74
74
  const _getItemKey = (collection, id) => `${collection}/${id}`;
75
- const _getQueryKey = (collection, constraints) => `${collection}:${QueryConstraints.prototype.toString.call(constraints)}`;
75
+ const _getQueryKey = (collection, constraints) => `${collection}?${QueryConstraints.prototype.toString.call(constraints)}`;
@@ -238,4 +238,4 @@ export class AsyncDebugProvider extends AbstractDebugProvider {
238
238
  }
239
239
  }
240
240
  const _getItemKey = (collection, id) => `${collection}/${id}`;
241
- const _getQueryKey = (collection, constraints) => `${collection}:${QueryConstraints.prototype.toString.call(constraints)}`;
241
+ const _getQueryKey = (collection, constraints) => `${collection}?${QueryConstraints.prototype.toString.call(constraints)}`;
@@ -223,4 +223,4 @@ export class MemoryTable extends Subject {
223
223
  // When we're writing data, if there's no limit set the results don't need to be sorted (for performance).
224
224
  const _getWriteConstraints = (constraints) => (constraints.limit ? constraints : constraints.filters);
225
225
  // Queries that have no limit don't care about sorting either.
226
- const _getQueryKey = (query) => (query.limit ? `{filters:${query.filters.toString()}}` : QueryConstraints.prototype.toString.call(query));
226
+ const _getQueryKey = (query) => (query.limit ? `"filters":${query.filters.toString()}}` : QueryConstraints.prototype.toString.call(query));