@opra/mongodb 0.16.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/LICENSE +21 -0
- package/README.md +3 -0
- package/cjs/index.js +7 -0
- package/cjs/mongo-adapter.js +109 -0
- package/cjs/mongo-collection-resource.js +103 -0
- package/cjs/mongo-entity-service.js +179 -0
- package/cjs/mongo-singleton-resource.js +63 -0
- package/cjs/package.json +3 -0
- package/cjs/transform-filter.js +96 -0
- package/cjs/transform-key-values.js +14 -0
- package/cjs/transform-patch.js +30 -0
- package/cjs/transform-projection.js +52 -0
- package/cjs/transform-sort.js +18 -0
- package/esm/index.js +7 -0
- package/esm/mongo-adapter.js +109 -0
- package/esm/mongo-collection-resource.js +103 -0
- package/esm/mongo-entity-service.js +179 -0
- package/esm/mongo-singleton-resource.js +63 -0
- package/esm/transform-filter.js +96 -0
- package/esm/transform-key-values.js +14 -0
- package/esm/transform-patch.js +30 -0
- package/esm/transform-projection.js +52 -0
- package/esm/transform-sort.js +18 -0
- package/package.json +67 -0
- package/types/index.d.ts +4 -0
- package/types/mongo-adapter.d.ts +14 -0
- package/types/mongo-collection-resource.d.ts +15 -0
- package/types/mongo-entity-service.d.ts +30 -0
- package/types/mongo-singleton-resource.d.ts +12 -0
- package/types/transform-filter.d.ts +2 -0
- package/types/transform-key-values.d.ts +3 -0
- package/types/transform-patch.d.ts +1 -0
- package/types/transform-projection.d.ts +9 -0
- package/types/transform-sort.d.ts +2 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports._transformExclusionProjection = exports._transformInclusionProjection = void 0;
|
|
4
|
+
const common_1 = require("@opra/common");
|
|
5
|
+
function transformProjection(dataType, args) {
|
|
6
|
+
const out = {};
|
|
7
|
+
// omitExclusiveFields(dataType, out);
|
|
8
|
+
const pick = args.pick && (0, common_1.pathToObjectTree)(args.include ? [...args.pick, ...args.include] : args.pick);
|
|
9
|
+
const include = !args.pick && args.include && (0, common_1.pathToObjectTree)(args.include);
|
|
10
|
+
const omit = args.omit && (0, common_1.pathToObjectTree)(args.omit);
|
|
11
|
+
if (pick || include) {
|
|
12
|
+
_transformInclusionProjection(dataType, out, pick, include, omit);
|
|
13
|
+
}
|
|
14
|
+
else
|
|
15
|
+
_transformExclusionProjection(dataType, out, omit, !omit);
|
|
16
|
+
return Object.keys(out).length ? out : undefined;
|
|
17
|
+
}
|
|
18
|
+
exports.default = transformProjection;
|
|
19
|
+
function _transformInclusionProjection(dataType, target, pick, include, omit, defaultFields) {
|
|
20
|
+
defaultFields = defaultFields ?? !pick;
|
|
21
|
+
let n;
|
|
22
|
+
for (const [k, f] of dataType.fields.entries()) {
|
|
23
|
+
if (omit?.[k] === true)
|
|
24
|
+
continue;
|
|
25
|
+
n = (defaultFields && !f.exclusive) ||
|
|
26
|
+
pick === true || pick?.[k] || include === true || include?.[k];
|
|
27
|
+
if (n) {
|
|
28
|
+
if (f.type instanceof common_1.ComplexType && (typeof n === 'object' || typeof omit?.[k] === 'object')) {
|
|
29
|
+
target[k] = {};
|
|
30
|
+
_transformInclusionProjection(f.type, target[k], pick?.[k] || include?.[k], undefined, omit?.[k], defaultFields);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
target[k] = 1;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports._transformInclusionProjection = _transformInclusionProjection;
|
|
38
|
+
function _transformExclusionProjection(dataType, target, omit, omitExclusiveFields) {
|
|
39
|
+
let n;
|
|
40
|
+
for (const [k, f] of dataType.fields.entries()) {
|
|
41
|
+
n = omit?.[k] || (omitExclusiveFields && f.exclusive);
|
|
42
|
+
if (n) {
|
|
43
|
+
if (f.type instanceof common_1.ComplexType && typeof n === 'object') {
|
|
44
|
+
target[k] = {};
|
|
45
|
+
_transformExclusionProjection(f.type, target[k], omit?.[k], omitExclusiveFields);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
target[k] = 0;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports._transformExclusionProjection = _transformExclusionProjection;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function transformSort(sort) {
|
|
4
|
+
if (!(sort && sort.length))
|
|
5
|
+
return;
|
|
6
|
+
const out = {};
|
|
7
|
+
if (sort)
|
|
8
|
+
sort.forEach(k => {
|
|
9
|
+
if (k.startsWith('-'))
|
|
10
|
+
out[k.substring(1)] = -1;
|
|
11
|
+
else if (k.startsWith('+'))
|
|
12
|
+
out[k.substring(1)] = 1;
|
|
13
|
+
else
|
|
14
|
+
out[k] = 1;
|
|
15
|
+
});
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
exports.default = transformSort;
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@opra/mongodb",
|
|
3
|
+
"version": "0.16.0",
|
|
4
|
+
"description": "Opra MongoDB adapter package",
|
|
5
|
+
"author": "Panates",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/panates/opra.git",
|
|
10
|
+
"directory": "packages/mongodb"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"compile": "tsc",
|
|
14
|
+
"prebuild": "npm run lint && npm run clean",
|
|
15
|
+
"build": "npm run build:cjs && npm run build:esm",
|
|
16
|
+
"build:cjs": "tsc -b tsconfig-build-cjs.json",
|
|
17
|
+
"build:esm": "tsc -b tsconfig-build-esm.json",
|
|
18
|
+
"postbuild": "cp README.md package.json ../../LICENSE ../../build/mongodb && cp ../../package.cjs.json ../../build/mongodb/cjs/package.json",
|
|
19
|
+
"lint": "eslint .",
|
|
20
|
+
"test": "jest",
|
|
21
|
+
"cover": "jest --collect-coverage",
|
|
22
|
+
"clean": "npm run clean:src && npm run clean:dist && npm run clean:cover",
|
|
23
|
+
"clean:src": "ts-cleanup -s src --all && ts-cleanup -s test --all",
|
|
24
|
+
"clean:dist": "rimraf ../../build/mongodb",
|
|
25
|
+
"clean:cover": "rimraf ../../coverage/mongodb"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"lodash": "^4.17.21"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@faker-js/faker": "^7.6.0",
|
|
32
|
+
"mongodb": "^5.3.0",
|
|
33
|
+
"ts-gems": "^2.3.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@opra/common": "^0.16.0",
|
|
37
|
+
"@opra/core": "^0.16.0",
|
|
38
|
+
"mongodb": ">=4.x.x"
|
|
39
|
+
},
|
|
40
|
+
"type": "module",
|
|
41
|
+
"types": "types/index.d.ts",
|
|
42
|
+
"exports": {
|
|
43
|
+
".": {
|
|
44
|
+
"require": "./cjs/index.js",
|
|
45
|
+
"default": "./esm/index.js"
|
|
46
|
+
},
|
|
47
|
+
"./cjs": "./cjs/index.js",
|
|
48
|
+
"./esm": "./esm/index.js"
|
|
49
|
+
},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=16.0",
|
|
52
|
+
"npm": ">=7.0.0"
|
|
53
|
+
},
|
|
54
|
+
"files": [
|
|
55
|
+
"bin/",
|
|
56
|
+
"cjs/",
|
|
57
|
+
"esm/",
|
|
58
|
+
"types/",
|
|
59
|
+
"LICENSE",
|
|
60
|
+
"README.md"
|
|
61
|
+
],
|
|
62
|
+
"keywords": [
|
|
63
|
+
"opra",
|
|
64
|
+
"mongodb",
|
|
65
|
+
"adapter"
|
|
66
|
+
]
|
|
67
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Request } from '@opra/core';
|
|
2
|
+
import _transformFilter from './transform-filter.js';
|
|
3
|
+
import _transformKeyValues from './transform-key-values.js';
|
|
4
|
+
import _transformPatch from './transform-patch.js';
|
|
5
|
+
import _transformProjection from './transform-projection.js';
|
|
6
|
+
import _transformSort from './transform-sort.js';
|
|
7
|
+
export declare namespace MongoAdapter {
|
|
8
|
+
const transformFilter: typeof _transformFilter;
|
|
9
|
+
const transformKeyValues: typeof _transformKeyValues;
|
|
10
|
+
const transformPatch: typeof _transformPatch;
|
|
11
|
+
const transformProjection: typeof _transformProjection;
|
|
12
|
+
const transformSort: typeof _transformSort;
|
|
13
|
+
function transformRequest(request: Request): any;
|
|
14
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import mongodb from 'mongodb';
|
|
2
|
+
import { PartialOutput } from '@opra/common';
|
|
3
|
+
import { RequestContext } from '@opra/core';
|
|
4
|
+
import { MongoEntityService } from './mongo-entity-service.js';
|
|
5
|
+
export declare abstract class MongoCollectionResource<T extends mongodb.Document, TOutput = PartialOutput<T>> {
|
|
6
|
+
defaultLimit: number;
|
|
7
|
+
create(ctx: RequestContext): Promise<TOutput>;
|
|
8
|
+
delete(ctx: RequestContext): Promise<number>;
|
|
9
|
+
deleteMany(ctx: RequestContext): Promise<number>;
|
|
10
|
+
get(ctx: RequestContext): Promise<TOutput>;
|
|
11
|
+
update(ctx: RequestContext): Promise<TOutput>;
|
|
12
|
+
updateMany(ctx: RequestContext): Promise<number>;
|
|
13
|
+
search(ctx: RequestContext): Promise<TOutput[]>;
|
|
14
|
+
abstract getService(ctx: RequestContext): MongoEntityService<T, TOutput> | Promise<MongoEntityService<T, TOutput>>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import mongodb, { UpdateFilter } from 'mongodb';
|
|
2
|
+
import { StrictOmit } from 'ts-gems';
|
|
3
|
+
import { PartialOutput, RequestContext } from '@opra/core';
|
|
4
|
+
export declare namespace MongoEntityService {
|
|
5
|
+
interface Options {
|
|
6
|
+
db?: mongodb.Db;
|
|
7
|
+
defaultLimit?: number;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export declare class MongoEntityService<T extends mongodb.Document, TOutput = PartialOutput<T>> {
|
|
11
|
+
readonly collectionName: string;
|
|
12
|
+
context: RequestContext;
|
|
13
|
+
defaultLimit: number;
|
|
14
|
+
db?: mongodb.Db;
|
|
15
|
+
session?: mongodb.ClientSession;
|
|
16
|
+
constructor(collectionName: string, options?: MongoEntityService.Options);
|
|
17
|
+
count(filter?: mongodb.Filter<T>, options?: mongodb.CountOptions): Promise<number>;
|
|
18
|
+
deleteOne(filter?: mongodb.Filter<T>, options?: mongodb.DeleteOptions): Promise<number>;
|
|
19
|
+
deleteMany(filter?: mongodb.Filter<T>, options?: mongodb.DeleteOptions): Promise<number>;
|
|
20
|
+
findOne(filter: mongodb.Filter<T>, options?: mongodb.FindOptions): Promise<TOutput | undefined>;
|
|
21
|
+
find(filter: mongodb.Filter<T>, options?: mongodb.FindOptions): Promise<TOutput[]>;
|
|
22
|
+
insertOne(doc: mongodb.OptionalUnlessRequiredId<T>, options?: mongodb.InsertOneOptions): Promise<TOutput>;
|
|
23
|
+
updateOne(filter: mongodb.Filter<T>, doc: UpdateFilter<T> | Partial<T>, options?: mongodb.UpdateOptions): Promise<TOutput | undefined>;
|
|
24
|
+
updateMany(filter: mongodb.Filter<T>, doc: UpdateFilter<T> | Partial<T>, options?: StrictOmit<mongodb.UpdateOptions, 'upsert'>): Promise<number>;
|
|
25
|
+
with(context: RequestContext, db?: mongodb.Db, session?: mongodb.ClientSession): MongoEntityService<T, TOutput>;
|
|
26
|
+
protected _onError(error: unknown): Promise<void>;
|
|
27
|
+
protected getDatabase(): mongodb.Db | Promise<mongodb.Db>;
|
|
28
|
+
protected onError?(error: unknown): void | Promise<void>;
|
|
29
|
+
protected onTransformRow?(row: mongodb.WithId<T>): mongodb.WithId<T>;
|
|
30
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import mongodb from 'mongodb';
|
|
2
|
+
import { PartialOutput } from '@opra/common';
|
|
3
|
+
import { RequestContext } from '@opra/core';
|
|
4
|
+
import { MongoEntityService } from './mongo-entity-service.js';
|
|
5
|
+
export declare abstract class MongoSingletonResource<T extends mongodb.Document, TOutput = PartialOutput<T>> {
|
|
6
|
+
defaultLimit: number;
|
|
7
|
+
create(ctx: RequestContext): Promise<TOutput>;
|
|
8
|
+
delete(ctx: RequestContext): Promise<number>;
|
|
9
|
+
get(ctx: RequestContext): Promise<TOutput>;
|
|
10
|
+
update(ctx: RequestContext): Promise<TOutput>;
|
|
11
|
+
abstract getService(ctx: RequestContext): MongoEntityService<T, TOutput> | Promise<MongoEntityService<T, TOutput>>;
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function transformPatch(doc: any): any;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import mongodb from 'mongodb';
|
|
2
|
+
import { ComplexType } from '@opra/common';
|
|
3
|
+
export default function transformProjection(dataType: ComplexType, args: {
|
|
4
|
+
pick?: string[];
|
|
5
|
+
omit?: string[];
|
|
6
|
+
include?: string[];
|
|
7
|
+
}): mongodb.Document | undefined;
|
|
8
|
+
export declare function _transformInclusionProjection(dataType: ComplexType, target: mongodb.Document, pick?: any, include?: any, omit?: any, defaultFields?: boolean): void;
|
|
9
|
+
export declare function _transformExclusionProjection(dataType: ComplexType, target: mongodb.Document, omit?: any, omitExclusiveFields?: boolean): void;
|