Database Methods: Get Model
Obtaining a specific database model.
ðŠķ Similar to the .getModels() method, this method only retrieves the specific 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( ) ; 5 merlin.connect( "MY-GIRLS-DATABASE") ; 6 7 merlin.getModel( 'girls-in-my-house') .then( e => { 8 console.log( e) 9 //returns: 10 { 11 "name": "girls-in-my-house", 12 "indexes": [ "id_", "name", "$order" ], 13 "size": "4698 bytes", 14 "records": 1000 15 } 16 17 } ) .catch( e => { 18 console.log( e) 19 //returns There's no 'guide' model in your Database! 20 } ) ;
This method is useful if you want to retrieve the data directly from MerlinDB, after it has been inserted.
For example, suppose you want to:
1: Retrieve a list of shoes from your traditional database, be it NoSql, Sql...;
2: If this list does not exist in MerlinDB, you want to save it;
3: However, if the list exists, you want to retrieve the data directly from MerlinDB, without needing to search for it in your traditional database and consuming resources for nothing.
See the following example to make it clearer:
1 merlin.connect( "USER") ; 2 3 const shoesSchema = Schema( { 4 name: String , 5 brand: { type: String } 6 } ) ;7 8 const shoesModel = merlin.model( 'User-shoes', shoesSchema) ;9 10 merlin.getModel( 'User-shoes') .then( e => { 11 //If the model exists, retrieve it directly. 12 getAllShoes( ) ; 13 14 } ) .catch( e => { 15 //If they don't exist, you can retrieve them in your... 16 //...traditional database. and insert them 17 InsertShoes( ) ; 18 } ) ;19 20 function async getAllShoes( ) { 21 var shoes = await shoesModel.find( ) ; 22 console.log( shoes) ; 23 //returns: { users-shoes-list } 24 [ 25 { name: "Air Max 90 ", brand: "Nike"} , 26 { name: "Adidas OwnTheGame", brand: "Adidas"} , 27 //... 28 ] 29 } 30 31 function async InsertShoes( ) { 32 //retrieve your data from the traditional database... 33 var shoesInMyTraditionalDbs = await axios.post... 34 //Or idk, using 'GET' 35 var anyMethod = await fetch... /* or XMLHttpRequest... */ 36 //returns 37 [ 38 { name: "Air Max 90 ", brand: "Nike"} , 39 { name: "OwnTheGame", brand: "Adidas"} , 40 /* ... */ 41 ] 42 43 var shoes = await shoesModel.insert( shoesInMyTraditionalDbs) ; 44 console.log( shoes) ; 45 { 46 "acknowledged": true , 47 insertedIds: ["4As32...","7dRw2..."], 48 data: [ 49 { name: "Air Max 90 ", brand: "Nike"} , 50 { name: "Adidas", brand: "Adidas"} , 51 /* ... */ 52 ] 53 }