10 examples of 'sequelize bulk create' in JavaScript

Every line of 'sequelize bulk create' 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
377public async createBulk(model: string, data: any[], options: { transaction?: Transaction }) {
378 if (data.length > 1000) {
379 const chunks = [];
380 let i = 0;
381 while (i < data.length) {
382 chunks.push(data.slice(i, i + 1000));
383 i += 1000;
384 }
385 const ids_all: any = [];
386 for (const chunk of chunks) {
387 [].push.apply(ids_all, await this.createBulk(model, chunk, options));
388 }
389 return ids_all;
390 }
391 let result: any;
392 try {
393 result = (await this._collection(model).insertMany(data, { safe: true }));
394 } catch (error) {
395 throw _processSaveError(error);
396 }
397 let error;
398 const ids = result.ops.map((doc: any) => {
399 const id = _objectIdToString(doc._id);
400 if (id) {
401 delete doc._id;
402 } else {
403 error = new Error('unexpected result');
404 }
405 return id;
406 });
407 if (error) {
408 throw error;
409 } else {
410 return ids;
411 }
412}
371async createBulk(model, data, options) {
372 if (data.length > 1000) {
373 const chunks = [];
374 let i = 0;
375 while (i < data.length) {
376 chunks.push(data.slice(i, i + 1000));
377 i += 1000;
378 }
379 const ids_all = [];
380 for (const chunk of chunks) {
381 [].push.apply(ids_all, await this.createBulk(model, chunk, options));
382 }
383 return ids_all;
384 }
385 let result;
386 try {
387 result = (await this._collection(model).insertMany(data, { safe: true }));
388 }
389 catch (error) {
390 throw _processSaveError(error);
391 }
392 let error;
393 const ids = result.ops.map((doc) => {
394 const id = _objectIdToString(doc._id);
395 if (id) {
396 delete doc._id;
397 }
398 else {
399 error = new Error('unexpected result');
400 }
401 return id;
402 });
403 if (error) {
404 throw error;
405 }
406 else {
407 return ids;
408 }
409}
138static async bulkCreate(userId, transactions) {
139 const fullTransactions = transactions.map(op => {
140 return { userId, ...op };
141 });
142 return await bulkInsert(repo(), fullTransactions);
143}
49async function bulkInsert(client, each) {
50 await each(async function (data) {
51 await insertOne(client, data);
52 });
53}
88static afterBulkCreateHook(instances: Hook[], options: any): void {}
44bulkAdd(data) {
45 if (data.length === 0) {
46 return
47 }
48
49 const insertStatement = this.db.prepare(
50 `INSERT INTO ${this.table} ` +
51 '(sequence, id, serialized) VALUES ' +
52 '(:sequence, :id, :serialized);',
53 )
54
55 try {
56 this.db.prepare('BEGIN;').run()
57
58 data.forEach(d =>
59 insertStatement.run({
60 sequence: d.sequence,
61 id: d.transaction.id,
62 serialized: Buffer.from(d.transaction.serialized, 'hex'),
63 }),
64 )
65
66 this.db.prepare('COMMIT;').run()
67 } finally {
68 if (this.db.inTransaction) {
69 this.db.prepare('ROLLBACK;').run()
70 }
71 }
72}
224function testBulkOperations() {
225 let dataStore: Backendless.DataStore = Backendless.Persistence.of('str');
226
227 let resultPromiseListOfString: Promise>;
228 let resultListOfString: Array;
229
230 let resultPromiseString: Promise;
231 let resultString: string;
232
233 resultPromiseListOfString = dataStore.bulkCreate([{}, {}, {}]);
234 resultListOfString = dataStore.bulkCreateSync([{}, {}, {}]);
235
236 resultPromiseString = dataStore.bulkUpdate('where clause string', {foo: 'bar'});
237 resultString = dataStore.bulkUpdateSync('where clause string', {foo: 'bar'});
238
239 resultPromiseString = dataStore.bulkDelete('where clause string');
240 resultPromiseString = dataStore.bulkDelete(['objectId1', 'objectId2', 'objectId3']);
241 resultPromiseString = dataStore.bulkDelete([{objectId: 'objectId1'}]);
242 resultPromiseString = dataStore.bulkDelete([{objectId: 'objectId1', foo: 'bar'}]);
243
244 resultString = dataStore.bulkDeleteSync('where clause string');
245 resultString = dataStore.bulkDeleteSync(['objectId1', 'objectId2', 'objectId3']);
246 resultString = dataStore.bulkDeleteSync([{objectId: 'objectId1'}]);
247 resultString = dataStore.bulkDeleteSync([{objectId: 'objectId1', foo: 'bar'}]);
248}
100bulkInsertDocuments(documents){
101 let commands = []
102 for (let doc of documents) {
103 commands.push({index:{_index:this.index, _type:this.type, _id:doc.id }})
104 commands.push(doc)
105 }
106 console.log('Sending commands...')
107 return this.client.bulk({requestTimeout: Infinity, body:commands}).then((res)=> {
108 console.log(`indexed ${res.items.length} items in ${res.took}ms`)
109 }).catch((e)=> {
110 console.log(e)
111 })
112}
60async create(objs) {
61 let entities = objs;
62 if (!_.isArray(entities)) {
63 entities = [entities];
64 }
65
66 entities.forEach((item) => {
67 const entity = item;
68 if (!entity._id) {
69 entity._id = idGenerator.generate();
70 }
71 entity.createdOn = new Date();
72
73 this._validateSchema(entity);
74 });
75
76 await this._collection.insert(entities);
77 entities.forEach((doc) => {
78 this._bus.emit('created', {
79 doc,
80 });
81 });
82
83 return entities.length > 1 ? entities : entities[0];
84}
78Insert(collection, data) {
79 var self = this;
80
81 return new Promise(function (resolve, reject) {
82 var _collection = self.DB.collection(collection);
83 _collection.insert(data, function (err, result) {
84 if(err){
85 reject(err);
86 return;
87 }
88 resolve(result);
89 });
90 });
91 }

Related snippets