@sprucelabs/data-stores 11.1.0 → 11.2.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.
- package/build/{CursorPager.d.ts → cursors/CursorPager.d.ts} +4 -4
- package/build/{CursorPager.js → cursors/CursorPager.js} +9 -2
- package/build/cursors/CursorPagerFaker.d.ts +6 -0
- package/build/cursors/CursorPagerFaker.js +25 -0
- package/build/esm/{CursorPager.d.ts → cursors/CursorPager.d.ts} +4 -4
- package/build/esm/{CursorPager.js → cursors/CursorPager.js} +10 -3
- package/build/esm/cursors/CursorPagerFaker.d.ts +6 -0
- package/build/esm/cursors/CursorPagerFaker.js +30 -0
- package/build/esm/index.d.ts +4 -2
- package/build/esm/index.js +4 -2
- package/build/index.d.ts +4 -2
- package/build/index.js +6 -3
- package/package.json +1 -1
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { QueryOptions } from '
|
|
1
|
+
import { QueryOptions } from '../types/query.types';
|
|
2
2
|
export interface CursorQueryOptions extends Omit<QueryOptions, 'skip'> {
|
|
3
3
|
limit: number;
|
|
4
4
|
next: string | null;
|
|
5
5
|
previous: string | null;
|
|
6
6
|
}
|
|
7
|
-
interface
|
|
7
|
+
export interface RecordsWithCursors<Response extends Record<string, any>[] = Record<string, any>[]> {
|
|
8
8
|
records: Response;
|
|
9
9
|
next: string | null;
|
|
10
10
|
previous: string | null;
|
|
11
11
|
}
|
|
12
12
|
export default class CursorPager {
|
|
13
|
-
static find<S extends SimpleStore, Find extends S['find'] = S['find'], Query extends Parameters<Find>[0] = Parameters<Find>[0], PromisedResponse extends ReturnType<Find> = ReturnType<Find>, Response extends UnPromisify<PromisedResponse> = UnPromisify<PromisedResponse>>(store: S, query: Query, options: CursorQueryOptions): Promise<
|
|
13
|
+
static find<S extends SimpleStore, Find extends S['find'] = S['find'], Query extends Parameters<Find>[0] = Parameters<Find>[0], PromisedResponse extends ReturnType<Find> = ReturnType<Find>, Response extends UnPromisify<PromisedResponse> = UnPromisify<PromisedResponse>>(store: S, query: Query, options: CursorQueryOptions): Promise<RecordsWithCursors<Response>>;
|
|
14
14
|
private static getSortFieldValuesFromRecord;
|
|
15
15
|
private static trackNewest;
|
|
16
16
|
private static parseCursor;
|
|
@@ -21,7 +21,7 @@ export default class CursorPager {
|
|
|
21
21
|
sort: NonNullable<O['sort']>;
|
|
22
22
|
};
|
|
23
23
|
}
|
|
24
|
-
interface SimpleStore {
|
|
24
|
+
export interface SimpleStore {
|
|
25
25
|
find: (...args: any[]) => Promise<any>;
|
|
26
26
|
}
|
|
27
27
|
declare type UnPromisify<T> = T extends Promise<infer U> ? U : T;
|
|
@@ -104,7 +104,14 @@ class CursorPager {
|
|
|
104
104
|
return JSON.stringify(cursor);
|
|
105
105
|
}
|
|
106
106
|
static prepareQueryOptions(options) {
|
|
107
|
-
const { previous, sort = [] } = (0, just_clone_1.default)((0, schema_1.assertOptions)(options, ['limit']));
|
|
107
|
+
const { previous, sort = [], limit, } = (0, just_clone_1.default)((0, schema_1.assertOptions)(options, ['limit']));
|
|
108
|
+
if (limit < 1) {
|
|
109
|
+
throw new schema_1.SchemaError({
|
|
110
|
+
code: 'INVALID_PARAMETERS',
|
|
111
|
+
parameters: ['limit'],
|
|
112
|
+
friendlyMessage: 'Your limit must be above zero!',
|
|
113
|
+
});
|
|
114
|
+
}
|
|
108
115
|
if (!sort.find((p) => p.field === 'id')) {
|
|
109
116
|
sort.push({
|
|
110
117
|
field: 'id',
|
|
@@ -116,7 +123,7 @@ class CursorPager {
|
|
|
116
123
|
s.direction = s.direction === 'asc' ? 'desc' : 'asc';
|
|
117
124
|
}
|
|
118
125
|
}
|
|
119
|
-
return Object.assign(Object.assign({}, options), { limit:
|
|
126
|
+
return Object.assign(Object.assign({}, options), { limit: limit + 1, sort });
|
|
120
127
|
}
|
|
121
128
|
}
|
|
122
129
|
exports.default = CursorPager;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { CursorQueryOptions, RecordsWithCursors, SimpleStore } from './CursorPager';
|
|
2
|
+
export default class CursorPagerFaker {
|
|
3
|
+
private static originalFind;
|
|
4
|
+
static setResponse(response: (store: SimpleStore, query: Record<string, any>, options: CursorQueryOptions) => Promise<Partial<RecordsWithCursors<Record<string, any>[]>>>): void;
|
|
5
|
+
static beforeEach(): Promise<void>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const CursorPager_1 = __importDefault(require("./CursorPager"));
|
|
7
|
+
class CursorPagerFaker {
|
|
8
|
+
static setResponse(response) {
|
|
9
|
+
//@ts-ignore
|
|
10
|
+
CursorPager_1.default.find = async (...args) => {
|
|
11
|
+
//@ts-ignore
|
|
12
|
+
const results = await response(...args);
|
|
13
|
+
return Object.assign({ next: null, previous: null, records: [] }, results);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
static async beforeEach() {
|
|
17
|
+
if (!this.originalFind) {
|
|
18
|
+
this.originalFind = CursorPager_1.default.find.bind(CursorPager_1.default);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
CursorPager_1.default.find = this.originalFind;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.default = CursorPagerFaker;
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { QueryOptions } from '
|
|
1
|
+
import { QueryOptions } from '../types/query.types';
|
|
2
2
|
export interface CursorQueryOptions extends Omit<QueryOptions, 'skip'> {
|
|
3
3
|
limit: number;
|
|
4
4
|
next: string | null;
|
|
5
5
|
previous: string | null;
|
|
6
6
|
}
|
|
7
|
-
interface
|
|
7
|
+
export interface RecordsWithCursors<Response extends Record<string, any>[] = Record<string, any>[]> {
|
|
8
8
|
records: Response;
|
|
9
9
|
next: string | null;
|
|
10
10
|
previous: string | null;
|
|
11
11
|
}
|
|
12
12
|
export default class CursorPager {
|
|
13
|
-
static find<S extends SimpleStore, Find extends S['find'] = S['find'], Query extends Parameters<Find>[0] = Parameters<Find>[0], PromisedResponse extends ReturnType<Find> = ReturnType<Find>, Response extends UnPromisify<PromisedResponse> = UnPromisify<PromisedResponse>>(store: S, query: Query, options: CursorQueryOptions): Promise<
|
|
13
|
+
static find<S extends SimpleStore, Find extends S['find'] = S['find'], Query extends Parameters<Find>[0] = Parameters<Find>[0], PromisedResponse extends ReturnType<Find> = ReturnType<Find>, Response extends UnPromisify<PromisedResponse> = UnPromisify<PromisedResponse>>(store: S, query: Query, options: CursorQueryOptions): Promise<RecordsWithCursors<Response>>;
|
|
14
14
|
private static getSortFieldValuesFromRecord;
|
|
15
15
|
private static trackNewest;
|
|
16
16
|
private static parseCursor;
|
|
@@ -21,7 +21,7 @@ export default class CursorPager {
|
|
|
21
21
|
sort: NonNullable<O['sort']>;
|
|
22
22
|
};
|
|
23
23
|
}
|
|
24
|
-
interface SimpleStore {
|
|
24
|
+
export interface SimpleStore {
|
|
25
25
|
find: (...args: any[]) => Promise<any>;
|
|
26
26
|
}
|
|
27
27
|
declare type UnPromisify<T> = T extends Promise<infer U> ? U : T;
|
|
@@ -18,7 +18,7 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
18
18
|
}
|
|
19
19
|
return t;
|
|
20
20
|
};
|
|
21
|
-
import { assertOptions } from '@sprucelabs/schema';
|
|
21
|
+
import { assertOptions, SchemaError } from '@sprucelabs/schema';
|
|
22
22
|
import clone from 'just-clone';
|
|
23
23
|
export default class CursorPager {
|
|
24
24
|
static find(store, query, options) {
|
|
@@ -110,7 +110,14 @@ export default class CursorPager {
|
|
|
110
110
|
return JSON.stringify(cursor);
|
|
111
111
|
}
|
|
112
112
|
static prepareQueryOptions(options) {
|
|
113
|
-
const { previous, sort = [] } = clone(assertOptions(options, ['limit']));
|
|
113
|
+
const { previous, sort = [], limit, } = clone(assertOptions(options, ['limit']));
|
|
114
|
+
if (limit < 1) {
|
|
115
|
+
throw new SchemaError({
|
|
116
|
+
code: 'INVALID_PARAMETERS',
|
|
117
|
+
parameters: ['limit'],
|
|
118
|
+
friendlyMessage: 'Your limit must be above zero!',
|
|
119
|
+
});
|
|
120
|
+
}
|
|
114
121
|
if (!sort.find((p) => p.field === 'id')) {
|
|
115
122
|
sort.push({
|
|
116
123
|
field: 'id',
|
|
@@ -122,6 +129,6 @@ export default class CursorPager {
|
|
|
122
129
|
s.direction = s.direction === 'asc' ? 'desc' : 'asc';
|
|
123
130
|
}
|
|
124
131
|
}
|
|
125
|
-
return Object.assign(Object.assign({}, options), { limit:
|
|
132
|
+
return Object.assign(Object.assign({}, options), { limit: limit + 1, sort });
|
|
126
133
|
}
|
|
127
134
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { CursorQueryOptions, RecordsWithCursors, SimpleStore } from './CursorPager';
|
|
2
|
+
export default class CursorPagerFaker {
|
|
3
|
+
private static originalFind;
|
|
4
|
+
static setResponse(response: (store: SimpleStore, query: Record<string, any>, options: CursorQueryOptions) => Promise<Partial<RecordsWithCursors<Record<string, any>[]>>>): void;
|
|
5
|
+
static beforeEach(): Promise<void>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import CursorPager from './CursorPager.js';
|
|
11
|
+
export default class CursorPagerFaker {
|
|
12
|
+
static setResponse(response) {
|
|
13
|
+
//@ts-ignore
|
|
14
|
+
CursorPager.find = (...args) => __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
//@ts-ignore
|
|
16
|
+
const results = yield response(...args);
|
|
17
|
+
return Object.assign({ next: null, previous: null, records: [] }, results);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
static beforeEach() {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
if (!this.originalFind) {
|
|
23
|
+
this.originalFind = CursorPager.find.bind(CursorPager);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
CursorPager.find = this.originalFind;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
package/build/esm/index.d.ts
CHANGED
|
@@ -15,5 +15,7 @@ export * from './types/query.types';
|
|
|
15
15
|
export * from './types/stores.types';
|
|
16
16
|
export * from './constants';
|
|
17
17
|
export { default as generateId } from './utilities/generateId';
|
|
18
|
-
export { default as CursorPager } from './CursorPager';
|
|
19
|
-
export * from './CursorPager';
|
|
18
|
+
export { default as CursorPager } from './cursors/CursorPager';
|
|
19
|
+
export * from './cursors/CursorPager';
|
|
20
|
+
export { default as CursorPagerFaker } from './cursors/CursorPagerFaker';
|
|
21
|
+
export * from './cursors/CursorPager';
|
package/build/esm/index.js
CHANGED
|
@@ -15,5 +15,7 @@ export * from './types/query.types.js';
|
|
|
15
15
|
export * from './types/stores.types.js';
|
|
16
16
|
export * from './constants.js';
|
|
17
17
|
export { default as generateId } from './utilities/generateId.js';
|
|
18
|
-
export { default as CursorPager } from './CursorPager.js';
|
|
19
|
-
export * from './CursorPager.js';
|
|
18
|
+
export { default as CursorPager } from './cursors/CursorPager.js';
|
|
19
|
+
export * from './cursors/CursorPager.js';
|
|
20
|
+
export { default as CursorPagerFaker } from './cursors/CursorPagerFaker.js';
|
|
21
|
+
export * from './cursors/CursorPager.js';
|
package/build/index.d.ts
CHANGED
|
@@ -15,5 +15,7 @@ export * from './types/query.types';
|
|
|
15
15
|
export * from './types/stores.types';
|
|
16
16
|
export * from './constants';
|
|
17
17
|
export { default as generateId } from './utilities/generateId';
|
|
18
|
-
export { default as CursorPager } from './CursorPager';
|
|
19
|
-
export * from './CursorPager';
|
|
18
|
+
export { default as CursorPager } from './cursors/CursorPager';
|
|
19
|
+
export * from './cursors/CursorPager';
|
|
20
|
+
export { default as CursorPagerFaker } from './cursors/CursorPagerFaker';
|
|
21
|
+
export * from './cursors/CursorPager';
|
package/build/index.js
CHANGED
|
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
17
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.CursorPager = exports.generateId = exports.mongoUtil = exports.NeDbDatabase = exports.MongoDatabase = exports.StoreLoader = exports.DatabaseFactory = exports.StoreFactory = exports.AbstractStore = exports.DatabaseFixture = exports.AbstractDatabaseTest = void 0;
|
|
20
|
+
exports.CursorPagerFaker = exports.CursorPager = exports.generateId = exports.mongoUtil = exports.NeDbDatabase = exports.MongoDatabase = exports.StoreLoader = exports.DatabaseFactory = exports.StoreFactory = exports.AbstractStore = exports.DatabaseFixture = exports.AbstractDatabaseTest = void 0;
|
|
21
21
|
var AbstractDatabaseTest_1 = require("./tests/AbstractDatabaseTest");
|
|
22
22
|
Object.defineProperty(exports, "AbstractDatabaseTest", { enumerable: true, get: function () { return __importDefault(AbstractDatabaseTest_1).default; } });
|
|
23
23
|
var DatabaseFixture_1 = require("./fixtures/DatabaseFixture");
|
|
@@ -45,6 +45,9 @@ __exportStar(require("./types/stores.types"), exports);
|
|
|
45
45
|
__exportStar(require("./constants"), exports);
|
|
46
46
|
var generateId_1 = require("./utilities/generateId");
|
|
47
47
|
Object.defineProperty(exports, "generateId", { enumerable: true, get: function () { return __importDefault(generateId_1).default; } });
|
|
48
|
-
var CursorPager_1 = require("./CursorPager");
|
|
48
|
+
var CursorPager_1 = require("./cursors/CursorPager");
|
|
49
49
|
Object.defineProperty(exports, "CursorPager", { enumerable: true, get: function () { return __importDefault(CursorPager_1).default; } });
|
|
50
|
-
__exportStar(require("./CursorPager"), exports);
|
|
50
|
+
__exportStar(require("./cursors/CursorPager"), exports);
|
|
51
|
+
var CursorPagerFaker_1 = require("./cursors/CursorPagerFaker");
|
|
52
|
+
Object.defineProperty(exports, "CursorPagerFaker", { enumerable: true, get: function () { return __importDefault(CursorPagerFaker_1).default; } });
|
|
53
|
+
__exportStar(require("./cursors/CursorPager"), exports);
|