Comparing version 1.3.0 to 1.4.0
{ | ||
"name": "mongot", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "@types/mongodb": { |
{ | ||
"name": "mongot", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "MongoT is a modern ODM library for MongoDb.", | ||
@@ -34,3 +34,3 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"@types/mongodb": "^2.1.41", | ||
"@types/mongodb": "http://nexus.drhead.ru/repository/npm/@types/mongodb/-/mongodb-2.1.41.tgz", | ||
"mongodb": "^2.2.25" | ||
@@ -37,0 +37,0 @@ }, |
@@ -13,3 +13,3 @@ # MongoT | ||
You may need TypeScript 2+ and enable `experimentalDecorators`, | ||
You may need TypeScript 2+ and should enable `experimentalDecorators`, | ||
`emitDecoratorMetadata` in your `tsconfig.json`. | ||
@@ -47,7 +47,7 @@ | ||
A document class describes schema fields and an entity getters/setters, | ||
event handlers and helper functions. | ||
A document class describes a schema (document properties, getters/setters, | ||
hooks for insertions/updates and helper functions). | ||
Schema supports these types: `string`, `boolean`, `number`, `date`, | ||
`Object`, `SchemaFragment` (also known as sub-document) | ||
Schema supports these types: `ObjectID`, `string`, `boolean`, `number`, | ||
`date`, `Object`, `SchemaFragment` (also known as sub-document) | ||
and `array`. A `buffer` type doesn't tested at this time. | ||
@@ -61,18 +61,38 @@ | ||
# UserDocument.ts | ||
import {SchemaDocument} from 'mongot'; | ||
import {SchemaDocument, SchemaFragment, Events} from 'mongot'; | ||
import {hook, prop, document} from 'mongot'; | ||
import * as crypto from 'crypto'; | ||
@fragment | ||
class UserContactsFragment extends SchemaFragment { | ||
type: 'phone' | 'email' | 'im'; | ||
title: string; | ||
value: string; | ||
} | ||
@document | ||
class UserDocument extends SchemaDocument { | ||
@prop email: string; | ||
@prop password: string; | ||
@prop firstName: string; | ||
@prop lastName: string; | ||
@prop | ||
public email: string; | ||
@prop registered: Date = new Date(); | ||
@prop updated: Date; | ||
@prop | ||
public password: string; | ||
@hook | ||
beforeUpdate() { | ||
@prop | ||
public firstName: string; | ||
@prop | ||
public lastName: string; | ||
@prop | ||
registered: Date = new Date(); | ||
@prop | ||
updated: Date; | ||
@prop(UserContactsFragment) | ||
children: SchemaFragmentArray<UserContactsFragment>; | ||
@hook(Events.beforeUpdate) | ||
refreshUpdated() { | ||
this.updated = new Date(); | ||
@@ -84,3 +104,3 @@ } | ||
.filter(x => !!x) | ||
.join(' '); | ||
.join(' ') || 'Unknown'; | ||
} | ||
@@ -87,0 +107,0 @@ |
@@ -42,3 +42,4 @@ "use strict"; | ||
this.construct = construct || metadata.construct; | ||
this.state = connection.then(connection => connection.get(this.name, options || metadata.options)); | ||
// this.state provide a collection instance from a lazy connection | ||
this.state = connection.then((connection) => connection.get(this.name, options || metadata.options)); | ||
this.connection = connection; | ||
@@ -117,6 +118,6 @@ this.queue((collection) => __awaiter(this, void 0, void 0, function* () { | ||
if (prepared._id) { | ||
const update = prepared.toObject(); | ||
const update = prepared.extract(); | ||
return this.updateOne(prepared, { | ||
$set: Object.assign({}, ...Object.keys(update) | ||
.filter(key => key !== '_id') | ||
.filter(key => key !== document_1.PRIMARY_KEY_NAME) | ||
.map(key => ({ [key]: update[key] }))) | ||
@@ -332,3 +333,3 @@ }); | ||
}; | ||
return Object.assign(reference, doc.toObject()); | ||
return Object.assign(reference, doc.extract()); | ||
} | ||
@@ -369,3 +370,3 @@ /** | ||
yield formalized.call(Events.beforeInsert, this); | ||
const insertResult = new helpers_1.InsertResult(yield collection.insertOne(formalized.toObject(), options), formalized); | ||
const insertResult = new helpers_1.InsertResult(yield collection.insertOne(formalized.extract(), options), formalized); | ||
yield formalized.call(Events.afterInsert, this); | ||
@@ -466,3 +467,3 @@ return insertResult; | ||
if (update instanceof document_1.SchemaDocument) { | ||
const _a = update.toObject(), { _id } = _a, document = __rest(_a, ["_id"]); | ||
const _a = update.extract(), { _id } = _a, document = __rest(_a, ["_id"]); | ||
updateSchema = document; | ||
@@ -469,0 +470,0 @@ } |
@@ -8,2 +8,3 @@ import { SchemaMutate } from './metadata/mutation'; | ||
static toPlainValue(value: any): any; | ||
static extract(value: any): any; | ||
static castToArray(type: typeof SchemaArray, proto: any, value: any): SchemaArray<any>; | ||
@@ -29,2 +30,3 @@ static castToFragmentArray(type: typeof SchemaArray, proto: any, value: any): SchemaArray<any>; | ||
toJSON(): any; | ||
extract(): any; | ||
static factory<T extends SchemaMetadata>(document?: Object): T; | ||
@@ -31,0 +33,0 @@ } |
@@ -75,2 +75,30 @@ "use strict"; | ||
} | ||
static extract(value) { | ||
switch (typeof value) { | ||
case 'object': { | ||
if (value === null) { | ||
return value; | ||
} | ||
if (value instanceof SchemaMetadata) { | ||
return value.extract(); | ||
} | ||
if (value instanceof SchemaArray) { | ||
return [...value].map(v => TypeCast.extract(v)); | ||
} | ||
if (Array.isArray(value)) { | ||
return value.map(x => TypeCast.extract(x)); | ||
} | ||
if (Object.prototype.toString.call(value) === '[object Object]') { | ||
return Object.assign({}, ...Object.keys(value) | ||
.filter(key => typeof value[key] !== 'undefined') | ||
.map(key => ({ [key]: TypeCast.extract(value[key]) }))); | ||
} | ||
else { | ||
return value; | ||
} | ||
} | ||
default: | ||
return value; | ||
} | ||
} | ||
static castToArray(type, proto, value) { | ||
@@ -215,2 +243,12 @@ assert_1.ok(true === Array.isArray(value), `${type.name} need an array value for constructor given ${value.toString()}.`); | ||
} | ||
extract() { | ||
const properties = []; | ||
Object.keys(this) | ||
.forEach(key => { | ||
if (typeof this[key] !== 'undefined') { | ||
properties.push({ [key]: TypeCast.extract(this[key]) }); | ||
} | ||
}); | ||
return Object.assign({}, ...properties); | ||
} | ||
static factory(document) { | ||
@@ -217,0 +255,0 @@ return new this().__mutate(document); |
import "./reflect"; | ||
import * as MongoDb from 'mongodb'; | ||
import { SchemaDocument } from "./document"; | ||
import { Collection } from "./collection"; | ||
import { SchemaMetadata } from "./document"; | ||
export interface CollectionDecorator { | ||
(target: typeof Collection): void; | ||
(name: string, schema: typeof SchemaDocument, options?: MongoDb.CollectionCreateOptions | MongoDb.CollectionOptions): (constructor: typeof Collection) => void; | ||
(name: string, schema: typeof SchemaMetadata, options?: MongoDb.CollectionCreateOptions | MongoDb.CollectionOptions): (constructor: typeof Collection) => void; | ||
} | ||
@@ -9,0 +8,0 @@ export interface IndexDecorator { |
@@ -16,17 +16,8 @@ "use strict"; | ||
const collection_1 = require("./collection"); | ||
exports.collection = (...args) => { | ||
assert_1.ok(args.length > 0 && args.length < 4, 'Mapper @collection has invalid number of arguments: ' + args.length); | ||
assert_1.ok(typeof args[0] === 'function' || typeof args[0] === 'string', 'Mapper @collection has invalid type of first argument'); | ||
if (typeof args[0] === 'function') { | ||
const constructor = args.shift(); | ||
store_1.MetadataStore.setCollectionMetadata(constructor, name); | ||
} | ||
else { | ||
const name = args.shift(); | ||
const construct = args.shift(); | ||
const options = args.shift() || {}; | ||
return (target) => { | ||
store_1.MetadataStore.setCollectionMetadata(target, name, construct, options); | ||
}; | ||
} | ||
exports.collection = (name, construct, options = {}) => { | ||
assert_1.ok(typeof name === 'string' && name.length, 'A @collection mapper should get a valid name'); | ||
assert_1.ok(typeof construct === 'function', 'A @collection mapper should get a valid a document schema'); | ||
return (target) => { | ||
store_1.MetadataStore.setCollectionMetadata(target, name, construct, options); | ||
}; | ||
}; | ||
@@ -33,0 +24,0 @@ exports.index = (indexOrSpec, options) => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
HTTP dependency
Supply chain riskContains a dependency which resolves to a remote HTTP URL which could be used to inject untrusted code and reduce overall package reliability.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
144058
2226
181
2
- Removed@types/bson@4.2.4(transitive)
- Removed@types/mongodb@2.2.28(transitive)
- Removed@types/node@22.10.0(transitive)
- Removedbson@6.10.0(transitive)
- Removedundici-types@6.20.0(transitive)
Updated@types/mongodb@http://nexus.drhead.ru/repository/npm/@types/mongodb/-/mongodb-2.1.41.tgz