Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 17x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 17x 2x 2x 2x 2x 2x 2x 2x 2x 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 1x 1x 1x 1x 1x 1x 1x 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 2x 2x 2x 2x 17x 1x 1x | import {adapter, type Errors, type IErrorMap, type IMeta} from '@feasibleone/blong/types';
import mongoUriBuilder from 'mongo-uri-builder';
import {
type BSON,
type Filter,
MongoClient,
type OptionalId,
type Sort,
type UpdateFilter,
} from 'mongodb';
export interface IConfig {
mongodb: object;
}
const errorMap: IErrorMap = {
'mongodb.generic': 'Mongodb Error',
'mongodb.invalid': 'Invalid Mongodb Operation',
'mongodb.notFound': 'Mongodb Not Found',
'mongodb.exists': 'Mongodb Exists',
'mongodb.unique': 'Mongodb Unique',
'mongodb.missingKey': 'Missing key value for {key}',
};
let _errors: Errors<typeof errorMap>;
export default adapter<IConfig>(({utError}) => {
_errors ||= utError.register(errorMap);
return {
activation: {
default: {
type: 'mongodb',
},
},
async start() {
this.config.context = {
mongodb: new MongoClient(mongoUriBuilder(this.config.mongodb)),
};
await this.config.context.mongodb!.connect();
super.connect();
return super.start();
},
async stop(...params: unknown[]) {
let result;
try {
await this.config.context.mongodb!.close();
} finally {
this.config.context = {};
result = await super.stop(...params);
}
return result;
},
async exec(
params:
| ({
select?: string;
order?: string;
limit?: number;
offset?: number;
sort?: Sort;
collection?: string;
where?: object;
operators?: object;
} & Record<string, unknown>)
| unknown[],
$meta: IMeta,
) {
const {method} = $meta;
const [, _table, operation] = method!.split('.');
let table = _table;
if (!Array.isArray(params) && _table === 'collection') {
const {collection, ...rest} = params;
if (collection) {
table = collection;
params = rest;
}
}
const key = table.split(/\W/, 1)[0] + 'Id';
switch (operation) {
case 'get': {
// get single document
if (Array.isArray(params)) {
throw this.error(_errors['mongodb.invalid'](), $meta);
}
const nonArrayParams = params as Record<string, unknown>;
const {select = '*', sort, [key]: _id, ...where} = nonArrayParams;
return this.config.context
.mongodb!.db()
.collection(table)
.findOne(
{
...(_id != null ? {_id: _id as BSON.ObjectId} : {}),
...(where as Record<string, unknown>),
},
{
projection:
select === '*'
? undefined
: (select as string).split(',').reduce(
(acc, field) => ({
...acc,
[field.trim()]: 1,
}),
{},
),
sort: sort as import('mongodb').Sort | undefined,
},
);
}
case 'find': {
// find multiple documents
if (Array.isArray(params)) {
throw this.error(_errors['mongodb.invalid'](), $meta);
}
const {select = '*', order, limit, offset, [key]: _id, ...where} = params;
return this.config.context
.mongodb!.db()
.collection(table)
.find(
{
...(_id != null ? {_id: _id as BSON.ObjectId} : {}),
...where,
},
{
projection:
select === '*'
? undefined
: (select as string).split(',').reduce(
(acc, field) => ({
...acc,
[field.trim()]: 1,
}),
{},
),
limit: typeof limit === 'number' ? limit : undefined,
skip: typeof offset === 'number' ? offset : undefined,
sort: order
? (order as string).split(',').reduce(
(acc, field) => ({
...acc,
[field.trim().replace(/^-/, '')]: field.startsWith(
'-',
)
? -1
: 1,
}),
{},
)
: undefined,
},
)
.toArray();
}
case 'add': {
// add single document
if (Array.isArray(params)) {
throw this.error(_errors['mongodb.invalid'](), $meta);
}
const {[key]: _id, ...rest} = params;
return this.config.context
.mongodb!.db()
.collection(table)
.insertOne(_id !== undefined ? {_id, ...rest} : rest);
}
case 'edit': {
// edit single document with full replace
if (Array.isArray(params)) {
throw this.error(_errors['mongodb.invalid'](), $meta);
}
const {[key]: _id, where, operators, ...rest} = params;
return this.config.context
.mongodb!.db()
.collection(table)
.updateOne(
{...(_id != null ? {_id: _id as BSON.ObjectId} : {}), ...where},
{$set: rest, ...(operators as object)},
);
}
case 'remove': // remove single document
if (!(key in params)) {
throw this.error(_errors['mongodb.missingKey']({key}), $meta);
}
return this.config.context
.mongodb!.db()
.collection(table)
.deleteOne({
_id: (params as Record<string, unknown>)[key] as
| BSON.ObjectId
| undefined,
});
case 'merge': {
// edit single document with partial update
if (Array.isArray(params)) {
throw this.error(_errors['mongodb.invalid'](), $meta);
}
const {[key]: _id, ...rest} = params;
return this.config.context
.mongodb!.db()
.collection(table)
.updateMany(
{...(_id != null ? {_id: _id as BSON.ObjectId} : {})},
{$set: rest},
{upsert: true},
);
}
case 'insert': {
// insert multiple documents
if (!Array.isArray(params)) {
throw this.error(_errors['mongodb.invalid'](), $meta);
}
return this.config.context
.mongodb!.db()
.collection(table)
.insertMany(params as OptionalId<BSON.Document>[]);
}
case 'update': {
if (Array.isArray(params)) {
throw this.error(_errors['mongodb.invalid'](), $meta);
}
const {[key]: _id, update, ...where} = params;
return this.config.context
.mongodb!.db()
.collection(table)
.updateMany(
{...(_id != null ? {_id: _id as BSON.ObjectId} : {}), ...where},
update as UpdateFilter<BSON.Document>,
);
}
case 'delete': // delete multiple documents
return this.config.context
.mongodb!.db()
.collection(table)
.deleteMany(params as Filter<BSON.Document>);
}
throw this.error(_errors['mongodb.generic'](), $meta);
},
};
});
|