Database Methods: Rename model

Renaming a model that already exists in the database.

🪶 This method is useful if you want to rename an existing model, it renames the model without changing the model's data or indexes.

This method has 3 parameters:

param 1 actualName (String): Current name of the model;
param 2 rename (String): Name you want to rename;
param 3 schema (Object): The schema of your current model.
1
  • import
  • MerlinDB
  • from
  • "@chrisaxxwell/merlin-db";
    2
  • //Or if you are using 'merlindb.min.js' just call 'new MerlinDB()';
  • 3
    4
  • const
  • merlin =
  • new
  • MerlinDB(
  • )
  • ;
    5merlin
  • .connect(
  • "MY-STORE-DATABASE"
  • )
  • ;
    6
    7
  • const
  • shoesSchema =
  • {
  • 8  
  • name:
  • {
  • type:
  • String
  • ,
  • unique:
  • true
  • }
  • ,
    9  
  • brand:
  • String
  • ,
    10
  • }
  • 11
  • //returns
  • 12merlin
  • .renameModel(
  • 'My-Shoes', 'Shoes', shoesSchema
  • )
  • .then(
  • e =>
  • {
  • 13   console
  • .log(
  • e
  • )
  • ;
    14  
  • //returns:
  • 15  
  • {
  • 16  
  • "oldModel":
  • "My-Shoes",
    17  
  • "newModel":
  • "Shoes",
    18  
  • "renamed":
  • true
  • ,
    19  
  • "data":
  • [
    20        
  • {
  • 21            
  • "name":
  • "Air Max
  • 90
  • ",
    22            
  • "brand":
  • "Nike",
    23            
  • "bunda":
  • "asdasd",
    24            
  • "$order":
  • 1719162286047
  • ,
    25            
  • "id_":
  • "667855aeTW96aW4b2d30add1"
    26        
  • }
  • ,
    27        
  • /*  ...  */
  • 28   ],
    29  
  • "status":
  • "Successful"
    30  
  • }
  • ;
    31
    32
  • }
  • )
  • .catch(
  • e =>
  • {
  • 33   console
  • .log(
  • e
  • )
  • ;
    34  
  • //If the current model exists
  • 35  
  • //returns: There's no 'My-Shoes' model in your Database!
  • 36
    37  
  • //If a model with the name you want to rename exists
  • 38  
  • //returns: 'Shoes' model already exists!
  • 39
  • }
  • )