MCPcopy
hub / github.com/sequelize/sequelize / delete

Method delete

lib/dialects/abstract/query-interface.js:910–941  ·  view source on GitHub ↗
(instance, tableName, identifier, options)

Source from the content-addressed store, hash-verified

908 }
909
910 async delete(instance, tableName, identifier, options) {
911 const cascades = [];
912 const sql = this.queryGenerator.deleteQuery(tableName, identifier, {}, instance.constructor);
913
914 options = { ...options };
915
916 // Check for a restrict field
917 if (!!instance.constructor && !!instance.constructor.associations) {
918 const keys = Object.keys(instance.constructor.associations);
919 const length = keys.length;
920 let association;
921
922 for (let i = 0; i < length; i++) {
923 association = instance.constructor.associations[keys[i]];
924 if (association.options && association.options.onDelete &&
925 association.options.onDelete.toLowerCase() === 'cascade' &&
926 association.options.useHooks === true) {
927 cascades.push(association.accessors.get);
928 }
929 }
930 }
931
932 for (const cascade of cascades) {
933 let instances = await instance[cascade](options);
934 // Check for hasOne relationship with non-existing associate ("has zero")
935 if (!instances) continue;
936 if (!Array.isArray(instances)) instances = [instances];
937 for (const _instance of instances) await _instance.destroy(options);
938 }
939 options.instance = instance;
940 return await this.sequelize.query(sql, options);
941 }
942
943 /**
944 * Delete multiple records from a table

Callers 4

testFunction · 0.80
changedMethod · 0.80
destroyMethod · 0.80
support.jsFile · 0.80

Calls 3

destroyMethod · 0.80
deleteQueryMethod · 0.45
queryMethod · 0.45

Tested by 1

testFunction · 0.64