type-mongodb
Advanced tools
Comparing version 1.0.0-beta.9 to 1.0.0-beta.10
@@ -17,5 +17,7 @@ import 'reflect-metadata'; | ||
create?: boolean; | ||
createJSValue?: (v: any) => any; | ||
} | ||
export declare function Field(options?: FieldOptions): PropertyDecorator; | ||
export declare function Field(embedded?: () => any, options?: FieldOptions): PropertyDecorator; | ||
export declare function Id(options?: FieldOptions): PropertyDecorator; | ||
export declare function Parent(): PropertyDecorator; | ||
@@ -22,0 +24,0 @@ export interface AbstractDiscriminatorOptions { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Discriminator = exports.Parent = exports.Field = exports.Document = void 0; | ||
exports.Discriminator = exports.Parent = exports.Id = exports.Field = exports.Document = void 0; | ||
require("reflect-metadata"); | ||
@@ -24,7 +24,16 @@ const definitionStorage_1 = require("../utils/definitionStorage"); | ||
} | ||
addFieldDefinition(field, Object.assign(Object.assign({}, options), { DocumentClass: target.constructor, type: reflection_1.fieldToType(target, field, options.type), propertyName: field, fieldName: options.name || field, isEmbedded: typeof embedded !== 'undefined', embedded, create: options.create })); | ||
addFieldDefinition(target, field, options, embedded); | ||
}; | ||
} | ||
exports.Field = Field; | ||
function addFieldDefinition(field, def) { | ||
function Id(options = {}) { | ||
return (target, field) => { | ||
addFieldDefinition(target, field, Object.assign(Object.assign({}, options), { isId: true, | ||
// IDs should default to creating the value | ||
create: typeof options.create === 'boolean' ? options.create : true })); | ||
}; | ||
} | ||
exports.Id = Id; | ||
function addFieldDefinition(target, field, options, embedded) { | ||
const def = Object.assign(Object.assign({}, options), { DocumentClass: target.constructor, propertyName: field, fieldName: options.name || field, isId: options.isId === true, isEmbedded: typeof embedded !== 'undefined', embedded, type: reflection_1.fieldToType(target, field, options.type), shouldCreateJSValue: typeof options.create === 'boolean' ? options.create : false }); | ||
if (definitionStorage_1.definitionStorage.fields.has(def.DocumentClass)) { | ||
@@ -31,0 +40,0 @@ definitionStorage_1.definitionStorage.fields.get(def.DocumentClass).set(field, def); |
@@ -20,5 +20,8 @@ "use strict"; | ||
this.idField = this.fields.get('_id'); | ||
// root documents must have an `_id` field. | ||
if (this.isRoot() && !this.idField) { | ||
errors_1.InternalError.throw(`The "${this.DocumentClass.name}" document is missing a decorated "_id" property`); | ||
if ( | ||
// root documents must have an `_id` field | ||
(this.isRoot() && !this.idField) || | ||
// _id fields must be valid "IDs" | ||
(this.idField && !this.idField.isId)) { | ||
errors_1.InternalError.throw(`The "${this.DocumentClass.name}" document is missing an "@Id" decorated "_id" property`); | ||
} | ||
@@ -25,0 +28,0 @@ } |
@@ -23,3 +23,4 @@ import { DocumentClass, Newable } from '../typings'; | ||
extensions?: Record<any, any>; | ||
create: boolean; | ||
isId: boolean; | ||
shouldCreateJSValue: boolean; | ||
} | ||
@@ -26,0 +27,0 @@ export interface ParentDefinition<T = any> { |
@@ -14,3 +14,3 @@ import { Newable } from '../typings'; | ||
readonly fieldName: string; | ||
readonly database: string; | ||
readonly isId: boolean; | ||
readonly embedded?: () => any; | ||
@@ -25,4 +25,3 @@ readonly isEmbedded: boolean; | ||
createJSValue(value?: any): any; | ||
private prepareOpts; | ||
} | ||
//# sourceMappingURL=FieldMetadata.d.ts.map |
@@ -8,6 +8,6 @@ "use strict"; | ||
constructor(opts) { | ||
this.prepareOpts(opts); | ||
this.DocumentClass = opts.DocumentClass; | ||
this.propertyName = opts.propertyName; | ||
this.fieldName = opts.fieldName; | ||
this.isId = opts.isId; | ||
this.embedded = opts.embedded; | ||
@@ -19,6 +19,9 @@ this.isEmbedded = opts.isEmbedded; | ||
this.type = opts.type; | ||
this.shouldCreateJSValue = this.type ? opts.create === true : false; | ||
this.shouldCreateJSValue = opts.shouldCreateJSValue; | ||
if (this.type && !(this.type instanceof types_1.Type)) { | ||
errors_1.InternalError.throw(`Invalid type for property "${this.propertyName}"`); | ||
} | ||
if (this.shouldCreateJSValue && !this.type) { | ||
errors_1.InternalError.throw(`${this.constructor.name}.${this.propertyName} cannot have "create" be true without a valid "type"`); | ||
} | ||
} | ||
@@ -28,20 +31,4 @@ createJSValue(value) { | ||
} | ||
prepareOpts(opts) { | ||
// don't allow "create" option with type is not set | ||
if (typeof opts.type === 'undefined' && | ||
typeof opts.create !== 'undefined') { | ||
errors_1.InternalError.throw(`${opts.DocumentClass.name} document cannot use "create" field option without a "type"`); | ||
} | ||
// force "_id" fields to auto-create values | ||
if (opts.fieldName === '_id' && typeof opts.type !== 'undefined') { | ||
opts.create = true; | ||
} | ||
// don't auto-create type values by default | ||
else if (typeof opts.type !== 'undefined' && | ||
typeof opts.create === 'undefined') { | ||
opts.create = false; | ||
} | ||
} | ||
} | ||
exports.FieldMetadata = FieldMetadata; | ||
//# sourceMappingURL=FieldMetadata.js.map |
{ | ||
"name": "type-mongodb", | ||
"version": "1.0.0-beta.9", | ||
"version": "1.0.0-beta.10", | ||
"description": "A simple decorator based MongoDB ODM.", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -26,8 +26,8 @@ <h1 align="center" style="border-bottom: none;">🔗 type-mongodb</h1> | ||
```typescript | ||
import { Field } from 'type-mongodb'; | ||
import { Id, Field } from 'type-mongodb'; | ||
import { ObjectId } from 'mongodb'; | ||
abstract class BaseDocument { | ||
@Field() | ||
_id: ObjectId = new ObjectId(); | ||
@Id() | ||
_id: ObjectId; | ||
@@ -191,2 +191,20 @@ get id(): string { | ||
What about custom IDs? You can either create your own type that extends `Type`, or use our built-ins: | ||
```typescript | ||
import { Id, Field, UUIDType } from 'type-mongodb'; | ||
@Document() | ||
class User { | ||
@Id({ type: UUIDType }) | ||
_id: string; | ||
// fields can also be a "UUID" type. | ||
@Field({ | ||
type: UUIDType /* create: true (pass this to auto-generate the uuid, otherwise, omit) */ | ||
}) | ||
uuid: string; | ||
} | ||
``` | ||
What about events? We want the base class to have createdAt and updatedAt be mapped | ||
@@ -193,0 +211,0 @@ correctly. |
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
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
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
228016
2860
285