@sprucelabs/data-stores 25.0.49 → 25.1.1
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/cursors/BatchCursor.d.ts +19 -0
- package/build/cursors/BatchCursor.js +45 -0
- package/build/esm/cursors/BatchCursor.d.ts +19 -0
- package/build/esm/cursors/BatchCursor.js +53 -0
- package/build/esm/index.d.ts +2 -0
- package/build/esm/index.js +2 -0
- package/build/esm/stores/AbstractStore.d.ts +2 -0
- package/build/esm/stores/AbstractStore.js +6 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +4 -1
- package/build/stores/AbstractStore.d.ts +2 -0
- package/build/stores/AbstractStore.js +4 -0
- package/package.json +1 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Schema, SchemaFieldNames } from '@sprucelabs/schema';
|
|
2
|
+
import AbstractStore from '../stores/AbstractStore';
|
|
3
|
+
import { QueryOptions } from '../types/query.types';
|
|
4
|
+
import { PrepareOptions } from '../types/stores.types';
|
|
5
|
+
export default class BatchCursorImpl<ResponseRecord> implements BatchCursor<ResponseRecord> {
|
|
6
|
+
private store;
|
|
7
|
+
private options?;
|
|
8
|
+
private query?;
|
|
9
|
+
private constructor();
|
|
10
|
+
static Cursor<Response>(store: AbstractStore<Schema>, query?: Record<string, any>, options?: FindBatchOptions): BatchCursor<Response>;
|
|
11
|
+
next(): Promise<ResponseRecord[] | null>;
|
|
12
|
+
private bumpCursorPosition;
|
|
13
|
+
}
|
|
14
|
+
export interface FindBatchOptions<IncludePrivateFields extends boolean = true, FullSchema extends Schema = Schema, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>> extends Omit<QueryOptions, 'includeFields'>, PrepareOptions<IncludePrivateFields, FullSchema, F> {
|
|
15
|
+
batchSize?: number;
|
|
16
|
+
}
|
|
17
|
+
export interface BatchCursor<ResponseRecord> {
|
|
18
|
+
next(): Promise<ResponseRecord[] | null>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
+
var t = {};
|
|
4
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
+
t[p] = s[p];
|
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
+
t[p[i]] = s[p[i]];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
class BatchCursorImpl {
|
|
15
|
+
constructor(store, query, options) {
|
|
16
|
+
this.store = store;
|
|
17
|
+
this.query = query;
|
|
18
|
+
this.options = options;
|
|
19
|
+
}
|
|
20
|
+
static Cursor(store, query, options) {
|
|
21
|
+
return new this(store, query, options);
|
|
22
|
+
}
|
|
23
|
+
async next() {
|
|
24
|
+
var _a;
|
|
25
|
+
const _b = (_a = this.options) !== null && _a !== void 0 ? _a : {}, { batchSize = 10 } = _b, rest = __rest(_b, ["batchSize"]);
|
|
26
|
+
const matches = await this.store.find(Object.assign({}, this.query), {
|
|
27
|
+
limit: batchSize,
|
|
28
|
+
}, Object.assign({}, rest));
|
|
29
|
+
if (matches.length === 0) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
this.bumpCursorPosition(matches);
|
|
33
|
+
return matches;
|
|
34
|
+
}
|
|
35
|
+
bumpCursorPosition(matches) {
|
|
36
|
+
const last = matches[matches.length - 1];
|
|
37
|
+
if (last) {
|
|
38
|
+
if (!this.query) {
|
|
39
|
+
this.query = {};
|
|
40
|
+
}
|
|
41
|
+
this.query.id = { $gt: last.id };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.default = BatchCursorImpl;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Schema, SchemaFieldNames } from '@sprucelabs/schema';
|
|
2
|
+
import AbstractStore from '../stores/AbstractStore';
|
|
3
|
+
import { QueryOptions } from '../types/query.types';
|
|
4
|
+
import { PrepareOptions } from '../types/stores.types';
|
|
5
|
+
export default class BatchCursorImpl<ResponseRecord> implements BatchCursor<ResponseRecord> {
|
|
6
|
+
private store;
|
|
7
|
+
private options?;
|
|
8
|
+
private query?;
|
|
9
|
+
private constructor();
|
|
10
|
+
static Cursor<Response>(store: AbstractStore<Schema>, query?: Record<string, any>, options?: FindBatchOptions): BatchCursor<Response>;
|
|
11
|
+
next(): Promise<ResponseRecord[] | null>;
|
|
12
|
+
private bumpCursorPosition;
|
|
13
|
+
}
|
|
14
|
+
export interface FindBatchOptions<IncludePrivateFields extends boolean = true, FullSchema extends Schema = Schema, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>> extends Omit<QueryOptions, 'includeFields'>, PrepareOptions<IncludePrivateFields, FullSchema, F> {
|
|
15
|
+
batchSize?: number;
|
|
16
|
+
}
|
|
17
|
+
export interface BatchCursor<ResponseRecord> {
|
|
18
|
+
next(): Promise<ResponseRecord[] | null>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
11
|
+
var t = {};
|
|
12
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
+
t[p] = s[p];
|
|
14
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
+
t[p[i]] = s[p[i]];
|
|
18
|
+
}
|
|
19
|
+
return t;
|
|
20
|
+
};
|
|
21
|
+
export default class BatchCursorImpl {
|
|
22
|
+
constructor(store, query, options) {
|
|
23
|
+
this.store = store;
|
|
24
|
+
this.query = query;
|
|
25
|
+
this.options = options;
|
|
26
|
+
}
|
|
27
|
+
static Cursor(store, query, options) {
|
|
28
|
+
return new this(store, query, options);
|
|
29
|
+
}
|
|
30
|
+
next() {
|
|
31
|
+
var _a;
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
const _b = (_a = this.options) !== null && _a !== void 0 ? _a : {}, { batchSize = 10 } = _b, rest = __rest(_b, ["batchSize"]);
|
|
34
|
+
const matches = yield this.store.find(Object.assign({}, this.query), {
|
|
35
|
+
limit: batchSize,
|
|
36
|
+
}, Object.assign({}, rest));
|
|
37
|
+
if (matches.length === 0) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
this.bumpCursorPosition(matches);
|
|
41
|
+
return matches;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
bumpCursorPosition(matches) {
|
|
45
|
+
const last = matches[matches.length - 1];
|
|
46
|
+
if (last) {
|
|
47
|
+
if (!this.query) {
|
|
48
|
+
this.query = {};
|
|
49
|
+
}
|
|
50
|
+
this.query.id = { $gt: last.id };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
package/build/esm/index.d.ts
CHANGED
|
@@ -21,3 +21,5 @@ export * from './cursors/CursorPager';
|
|
|
21
21
|
export { default as CursorPagerFaker } from './cursors/CursorPagerFaker';
|
|
22
22
|
export * from './cursors/CursorPager';
|
|
23
23
|
export { default as databaseAssertUtil } from './tests/databaseAssertUtil';
|
|
24
|
+
export { default as BatchCursor } from './cursors/BatchCursor';
|
|
25
|
+
export * from './cursors/BatchCursor';
|
package/build/esm/index.js
CHANGED
|
@@ -21,3 +21,5 @@ export * from './cursors/CursorPager.js';
|
|
|
21
21
|
export { default as CursorPagerFaker } from './cursors/CursorPagerFaker.js';
|
|
22
22
|
export * from './cursors/CursorPager.js';
|
|
23
23
|
export { default as databaseAssertUtil } from './tests/databaseAssertUtil.js';
|
|
24
|
+
export { default as BatchCursor } from './cursors/BatchCursor.js';
|
|
25
|
+
export * from './cursors/BatchCursor.js';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Schema, SchemaFieldNames, SchemaPartialValues, SchemaPublicFieldNames, SchemaValues } from '@sprucelabs/schema';
|
|
2
|
+
import { FindBatchOptions } from '../cursors/BatchCursor';
|
|
2
3
|
import AbstractMutexer from '../mutexers/AbstractMutexer';
|
|
3
4
|
import { Database } from '../types/database.types';
|
|
4
5
|
import { QueryBuilder, QueryOptions } from '../types/query.types';
|
|
@@ -32,6 +33,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
|
|
|
32
33
|
findOne<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>> | null>;
|
|
33
34
|
count(query?: QueryBuilder<QueryRecord>): Promise<number>;
|
|
34
35
|
find<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, queryOptions?: Omit<QueryOptions, 'includeFields'>, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<Awaited<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>>>[]>;
|
|
36
|
+
findBatch<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(query?: QueryBuilder<QueryRecord>, options?: FindBatchOptions<IncludePrivateFields, FullSchema, F>): Promise<import("../cursors/BatchCursor").BatchCursor<SchemaValues<FullSchema, CreateEntityInstances, IncludePrivateFields, false, F, PF>>>;
|
|
35
37
|
upsertOne<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, updates: UpdateRecord & CreateRecord & {
|
|
36
38
|
id?: string;
|
|
37
39
|
}, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<Response<FullSchema, CreateEntityInstances, IncludePrivateFields, PF, F>>;
|
|
@@ -20,6 +20,7 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
20
20
|
};
|
|
21
21
|
import SchemaEntity, { normalizeSchemaValues, validateSchemaValues, } from '@sprucelabs/schema';
|
|
22
22
|
import { SCRAMBLE_VALUE } from '../constants.js';
|
|
23
|
+
import BatchCursorImpl from '../cursors/BatchCursor.js';
|
|
23
24
|
import SpruceError from '../errors/SpruceError.js';
|
|
24
25
|
import AbstractMutexer from '../mutexers/AbstractMutexer.js';
|
|
25
26
|
import { saveOperations, } from '../types/stores.types.js';
|
|
@@ -136,6 +137,11 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
136
137
|
return [];
|
|
137
138
|
});
|
|
138
139
|
}
|
|
140
|
+
findBatch(query, options) {
|
|
141
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
142
|
+
return BatchCursorImpl.Cursor(this, query, options);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
139
145
|
upsertOne(query, updates, options) {
|
|
140
146
|
return __awaiter(this, void 0, void 0, function* () {
|
|
141
147
|
const mutexKey = 'upsertOne';
|
package/build/index.d.ts
CHANGED
|
@@ -21,3 +21,5 @@ export * from './cursors/CursorPager';
|
|
|
21
21
|
export { default as CursorPagerFaker } from './cursors/CursorPagerFaker';
|
|
22
22
|
export * from './cursors/CursorPager';
|
|
23
23
|
export { default as databaseAssertUtil } from './tests/databaseAssertUtil';
|
|
24
|
+
export { default as BatchCursor } from './cursors/BatchCursor';
|
|
25
|
+
export * from './cursors/BatchCursor';
|
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.databaseAssertUtil = exports.CursorPagerFaker = exports.CursorPager = exports.generateId = exports.DataStoresError = exports.mongoUtil = exports.NeDbDatabase = exports.MongoDatabase = exports.StoreLoader = exports.DatabaseFactory = exports.StoreFactory = exports.AbstractStore = exports.DatabaseFixture = exports.AbstractDatabaseTest = void 0;
|
|
20
|
+
exports.BatchCursor = exports.databaseAssertUtil = exports.CursorPagerFaker = exports.CursorPager = exports.generateId = exports.DataStoresError = 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");
|
|
@@ -55,3 +55,6 @@ Object.defineProperty(exports, "CursorPagerFaker", { enumerable: true, get: func
|
|
|
55
55
|
__exportStar(require("./cursors/CursorPager"), exports);
|
|
56
56
|
var databaseAssertUtil_1 = require("./tests/databaseAssertUtil");
|
|
57
57
|
Object.defineProperty(exports, "databaseAssertUtil", { enumerable: true, get: function () { return __importDefault(databaseAssertUtil_1).default; } });
|
|
58
|
+
var BatchCursor_1 = require("./cursors/BatchCursor");
|
|
59
|
+
Object.defineProperty(exports, "BatchCursor", { enumerable: true, get: function () { return __importDefault(BatchCursor_1).default; } });
|
|
60
|
+
__exportStar(require("./cursors/BatchCursor"), exports);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Schema, SchemaFieldNames, SchemaPartialValues, SchemaPublicFieldNames, SchemaValues } from '@sprucelabs/schema';
|
|
2
|
+
import { FindBatchOptions } from '../cursors/BatchCursor';
|
|
2
3
|
import AbstractMutexer from '../mutexers/AbstractMutexer';
|
|
3
4
|
import { Database } from '../types/database.types';
|
|
4
5
|
import { QueryBuilder, QueryOptions } from '../types/query.types';
|
|
@@ -32,6 +33,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
|
|
|
32
33
|
findOne<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>> | null>;
|
|
33
34
|
count(query?: QueryBuilder<QueryRecord>): Promise<number>;
|
|
34
35
|
find<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, queryOptions?: Omit<QueryOptions, 'includeFields'>, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<Awaited<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>>>[]>;
|
|
36
|
+
findBatch<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(query?: QueryBuilder<QueryRecord>, options?: FindBatchOptions<IncludePrivateFields, FullSchema, F>): Promise<import("../cursors/BatchCursor").BatchCursor<SchemaValues<FullSchema, CreateEntityInstances, IncludePrivateFields, false, F, PF>>>;
|
|
35
37
|
upsertOne<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, updates: UpdateRecord & CreateRecord & {
|
|
36
38
|
id?: string;
|
|
37
39
|
}, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<Response<FullSchema, CreateEntityInstances, IncludePrivateFields, PF, F>>;
|
|
@@ -39,6 +39,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
39
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
40
|
const schema_1 = __importStar(require("@sprucelabs/schema"));
|
|
41
41
|
const constants_1 = require("../constants");
|
|
42
|
+
const BatchCursor_1 = __importDefault(require("../cursors/BatchCursor"));
|
|
42
43
|
const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
|
|
43
44
|
const AbstractMutexer_1 = __importDefault(require("../mutexers/AbstractMutexer"));
|
|
44
45
|
const stores_types_1 = require("../types/stores.types");
|
|
@@ -143,6 +144,9 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
143
144
|
}
|
|
144
145
|
return [];
|
|
145
146
|
}
|
|
147
|
+
async findBatch(query, options) {
|
|
148
|
+
return BatchCursor_1.default.Cursor(this, query, options);
|
|
149
|
+
}
|
|
146
150
|
async upsertOne(query, updates, options) {
|
|
147
151
|
const mutexKey = 'upsertOne';
|
|
148
152
|
await this.lock(mutexKey);
|