MCPcopy
hub / github.com/Automattic/mongoose / create

Function create

lib/model.js:1765–1829  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1763
1764
1765 function create() {
1766 if (options._automatic) {
1767 if (model.schema.options.autoIndex === false ||
1768 (model.schema.options.autoIndex == null && model.db.config.autoIndex === false)) {
1769 return done();
1770 }
1771 }
1772
1773 const index = indexes.shift();
1774 if (!index) {
1775 return done();
1776 }
1777 if (options._automatic && index[1]._autoIndex === false) {
1778 return create();
1779 }
1780
1781 if (baseSchemaIndexes.find(i => utils.deepEqual(i, index))) {
1782 return create();
1783 }
1784
1785 const indexFields = clone(index[0]);
1786 const indexOptions = clone(index[1]);
1787
1788 delete indexOptions._autoIndex;
1789 decorateDiscriminatorIndexOptions(model.schema, indexOptions);
1790 applyWriteConcern(model.schema, indexOptions);
1791 applySchemaCollation(indexFields, indexOptions, model.schema.options);
1792
1793 indexSingleStart(indexFields, options);
1794
1795 // Just in case `createIndex()` throws a sync error
1796 let promise = null;
1797 try {
1798 promise = model.collection.createIndex(indexFields, indexOptions);
1799 } catch (err) {
1800 if (!indexError) {
1801 indexError = err;
1802 }
1803 if (!model.$caught) {
1804 model.emit('error', err);
1805 }
1806
1807 indexSingleDone(err, indexFields, indexOptions);
1808 create();
1809 return;
1810 }
1811
1812 promise.then(
1813 name => {
1814 indexSingleDone(null, indexFields, indexOptions, name);
1815 create();
1816 },
1817 err => {
1818 if (!indexError) {
1819 indexError = err;
1820 }
1821 if (!model.$caught) {
1822 model.emit('error', err);

Callers 1

_ensureIndexesFunction · 0.70

Calls 5

doneFunction · 0.85
indexSingleStartFunction · 0.85
indexSingleDoneFunction · 0.85
findMethod · 0.65
cloneFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…