What the difference between save and insert when wanting to create new record in TypeORM

Viewed 5230

What is the difference between .save() and .insert() in the Repository when wanting to create a new record?

In the code, the comment for .insert() is:

// From https://github.com/typeorm/typeorm/blob/b6c828cc6c9786c155165d97e1f21af7cf423075/src/repository/Repository.ts#L224-L232


    /**
     * Inserts a given entity into the database.
     * Unlike save method executes a primitive operation without cascades, relations and other operations included.
     * Executes fast and efficient INSERT query.
     * Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted.
     */
    insert(entity: QueryDeepPartialEntity<Entity>|(QueryDeepPartialEntity<Entity>[])): Promise<InsertResult> {
        return this.manager.insert(this.metadata.target as any, entity);
    }

In the code, the comment for .save() is:

// From https://github.com/typeorm/typeorm/blob/b6c828cc6c9786c155165d97e1f21af7cf423075/src/repository/Repository.ts#L140-L151

    /**
     * Saves a given entity in the database.
     * If entity does not exist in the database then inserts, otherwise updates.
     */
    save<T extends DeepPartial<Entity>>(entity: T, options?: SaveOptions): Promise<T & Entity>;

    /**
     * Saves one or many given entities.
     */
    save<T extends DeepPartial<Entity>>(entityOrEntities: T|T[], options?: SaveOptions): Promise<T|T[]> {
        return this.manager.save<Entity, T>(this.metadata.target as any, entityOrEntities as any, options);
    }

I don't understand what does:

Unlike save method executes a primitive operation without cascades, relations and other operations included.

From .insert() comment

means

0 Answers
Related