9 examples of 'mongoose schema methods' in JavaScript

Every line of 'mongoose schema methods' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
120static get _schema() {
121 return new mongoose.Schema({
122 title: String,
123 body: String,
124 tags: [ String ],
125 posted: Date
126 });
127}
6create(mongoose) {
7 console.log('defined user\'s schema');
8
9 let userSchema = mongoose.Schema({
10 id: { type: String, required: true, unique: true, 'default': '' },
11 hashed_password: { type: String, required: true, 'default': '' },
12 salt: {type: String, required: true },
13 name: { type: String, index: 'hashed', 'default': '' },
14 age: { type: Number, 'default': -1 },
15 create_at: { type: Date, index: { unique: false }, 'default': Date.now() },
16 update_at: { type: Date, index: { unique: false }, 'default': Date.now() }
17 });
18
19 return userSchema;
20}
6function getSchema () {
7 var s = mongoose.Schema({
8 single: String
9 , array: [String]
10 })
11 s.index({ single: 'text', array: 'text' })
12 return s;
13}
1export default function FlowSchemaBuilder(mongoose) {
2 let Schema = mongoose.Schema;
3
4 const JobLogSchema = new Schema({
5 jobId: { type: Number, require: true },
6 step: { type: Number, require: true },
7 personHuid: { type: String, require: true },
8 message: { type: String, required: true },
9 createdOn: { type: Date, default: Date.now }
10 }, { _id: false });
11
12 const FlowLogSchema = new Schema({
13 step: { type: Number, require: true },
14 message: { type: String, required: true },
15 createdOn: { type: Date, default: Date.now }
16 }, { _id: false });
17
18 const FlowSchema = new Schema({
19 stepsTaken: { type: Number, required: true, default: -1 },
20 substepsTaken: { type: Array, required: false, default: [] },
21 totalSteps: { type: Number, required: false },
22 jobType: { type: String, required: false },
23 jobId: { type: Number, required: true },
24 phase: { type: String, default: 'NoPhase', required: true },
25 jobData: { type: Schema.Types.Mixed, required: true },
26 relatedJobs: { type: Schema.Types.Mixed, required: true },
27 jobLogs: [JobLogSchema],
28 flowLogs: [FlowLogSchema],
29 isCancelled: { type: Boolean, required: true, default: false },
30 completed: { type: Boolean, required: true, default: false },
31 result: { type: Schema.Types.Mixed, default: null, required: false }
32 }, { collection: 'flow' });
33
34 return FlowSchema;
35}
19createModelFromSchema(modelName, schema) {
20 return mongoose_1.model(modelName, schema);
21}
349setArrayItems(schema: ISchema) {
350 this.items = new Schema(schema, this)
351 return this.items
352}
26function MongooseArray(values, path, doc) {
27 var arr = [].concat(values);
28
29 utils.decorate( arr, MongooseArray.mixin );
30 arr.isMongooseArray = true;
31
32 var _options = { enumerable: false, configurable: true, writable: true };
33 var keys = Object.keys(MongooseArray.mixin).
34 concat(['isMongooseArray', 'validators', '_path']);
35 for (var i = 0; i < keys.length; ++i) {
36 Object.defineProperty(arr, keys[i], _options);
37 };
38
39 arr._atomics = {};
40 arr.validators = [];
41 arr._path = path;
42
43 // Because doc comes from the context of another function, doc === global
44 // can happen if there was a null somewhere up the chain (see #3020)
45 // RB Jun 17, 2015 updated to check for presence of expected paths instead
46 // to make more proof against unusual node environments
47 if (doc && doc instanceof Document) {
48 arr._parent = doc;
49 arr._schema = doc.schema.path(path);
50 }
51
52 return arr;
53}
219export function schema(): Schema {
220 if (!Schema.schema) {
221 // tslint:disable-next-line: no-unused-expression
222 new Schema();
223 }
224 return Schema.schema;
225}
26function Document(obj, schema, fields, skipId, skipInit) {
27 if (!(this instanceof Document)) {
28 return new Document(obj, schema, fields, skipId, skipInit);
29 }
30
31 if (utils.isObject(schema) && !schema.instanceOfSchema) {
32 schema = new Schema(schema);
33 }
34
35 // When creating EmbeddedDocument, it already has the schema and he doesn't need the _id
36 schema = this.schema || schema;
37
38 // Generate ObjectId if it is missing, but it requires a scheme
39 if (!this.schema && schema.options._id) {
40 obj = obj || {};
41
42 if (obj._id === undefined) {
43 obj._id = new ObjectId();
44 }
45 }
46
47 if (!schema) {
48 throw new MongooseError.MissingSchemaError();
49 }
50
51 this.$__setSchema(schema);
52
53 NodeJSDocument.call(this, obj, fields, skipId, skipInit);
54
55 applyHooks(this, schema, { decorateDoc: true });
56
57 // apply methods
58 for (var m in schema.methods) {
59 this[m] = schema.methods[m];
60 }
61 // apply statics
62 for (var s in schema.statics) {
63 this[s] = schema.statics[s];
64 }
65}

Related snippets