BVB Source Codes

waterline Show waterline.js Source code

Return Download waterline: download waterline.js Source code - Download waterline Source code - Type:.js
  1. //  鈻堚枅鈺    鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈺     鈻堚枅鈺椻枅鈻堚枅鈺   鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺
  2. //  鈻堚枅鈺    鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻暁鈺愨晲鈻堚枅鈺斺晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺     鈻堚枅鈺戔枅鈻堚枅鈻堚晽  鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨晲鈺
  3. //  鈻堚枅鈺 鈻堚晽 鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈺     鈻堚枅鈺戔枅鈻堚晹鈻堚枅鈺 鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈺
  4. //  鈻堚枅鈺戔枅鈻堚枅鈺椻枅鈻堚晳鈻堚枅鈺斺晲鈺愨枅鈻堚晳   鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺     鈻堚枅鈺戔枅鈻堚晳鈺氣枅鈻堚晽鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺
  5. //  鈺氣枅鈻堚枅鈺斺枅鈻堚枅鈺斺暆鈻堚枅鈺  鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晳鈻堚枅鈺 鈺氣枅鈻堚枅鈻堚晳鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  6. //   鈺氣晲鈺愨暆鈺氣晲鈺愨暆 鈺氣晲鈺  鈺氣晲鈺   鈺氣晲鈺   鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺濃暁鈺愨暆鈺氣晲鈺  鈺氣晲鈺愨晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺
  7. //
  8.  
  9. var assert = require('assert');
  10. var _ = require('@sailshq/lodash');
  11. var async = require('async');
  12. var Schema = require('waterline-schema');
  13. var DatastoreBuilder = require('./waterline/utils/system/datastore-builder');
  14. var CollectionBuilder = require('./waterline/utils/system/collection-builder');
  15. var WLModelConstructor = require('./waterline/collection');
  16.  
  17.  
  18. /**
  19.  * ORM
  20.  *
  21.  * Construct an ORM instance.
  22.  *
  23.  * @constructs {ORM}
  24.  */
  25. module.exports = function ORM() {
  26.  
  27.   // Start by setting up an array of model definitions.
  28.   // (This will hold the raw model definitions that were passed in,
  29.   // plus any implicitly introduced models-- but that part comes later)
  30.   var modelDefs = [];
  31.  
  32.   // Hold a map of the instantaited and active datastores and models.
  33.   var modelMap = {};
  34.   var datastoreMap = {};
  35.  
  36.   // This "context" dictionary will be passed into the WLModel when instantiating.
  37.   // This is a stop gap to prevent re-writing all the "collection query" stuff.
  38.   var context = {
  39.     collections: modelMap,
  40.     datastores:  datastoreMap
  41.   };
  42.  
  43.  
  44.   // Now build an ORM instance.
  45.   var orm = {};
  46.  
  47.  
  48.   //  鈹屸攢鈹愨攢鈹 鈹攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹  鈹屸攢鈹愨敩鈹€鈹愨攲鈹攼 鈺︹晲鈺椻晹鈺愨晽鈺斺晲鈺椻暒鈺斺晲鈺椻晹鈺︹晽鈺斺晲鈺椻暒鈺愨晽鈺斺暒鈺椻晹鈺愨晽鈺斺暒鈺椻晹鈺愨晽鈺
  49.   //  鈹溾敜 鈹屸敶鈹敇鈹溾攢鈹樷攤 鈹傗敂鈹€鈹愨敎鈹   鈹 鈹傗敎鈹敇鈹傗攤鈹 鈺犫暒鈺濃晳鈺 鈺 鈺︹晳鈺氣晲鈺 鈺 鈺戔暎 鈺犫暒鈺濃晳鈺戔晳鈺 鈺 鈺戔晳鈺戔暎 鈺
  50.   //  鈹斺攢鈹樷敶 鈹斺攢鈹  鈹斺攢鈹樷敂鈹€鈹樷敂鈹€鈹  鈹斺攢鈹樷敶鈹斺攢鈹 鈹磑鈺┾暁鈺愨暁鈺愨暆鈺氣晲鈺濃暕鈺氣晲鈺 鈺 鈺氣晲鈺濃暕鈺氣晲鈺 鈺┾暁鈺愨暆鈺愨暕鈺濃暁鈺愨暆鈺┾晲鈺
  51.   /**
  52.    * .registerModel()
  53.    *
  54.    * Register a model definition.
  55.    *
  56.    * @param  {Dictionary) model
  57.    */
  58.   orm.registerModel = function registerModel(modelDef) {
  59.     modelDefs.push(modelDef);
  60.   };
  61.   // Alias for backwards compatibility:
  62.   orm.loadCollection = function _loadCollection_is_deprecated(){
  63.     console.warn('\n'+
  64.       'Warning: As of Waterline 0.13, `loadCollection()` is now `registerModel()`.  Please call that instead.\n'+
  65.       'I get what you mean, so I temporarily renamed it for you this time, but here is a stack trace\n'+
  66.       'so you know where this is coming from in the code, and can change it to prevent future warnings:\n'+
  67.       '```\n'+
  68.       (new Error()).stack+'\n'+
  69.       '```\n'
  70.     );
  71.     orm.registerModel.apply(orm, Array.prototype.slice.call(arguments));
  72.   };
  73.  
  74.  
  75.   //  鈹屸攢鈹愨攢鈹 鈹攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹  鈹屸攢鈹愨敩鈹€鈹愨攲鈹攼 鈺︹晹鈺椻晹鈺︹晹鈺︹晽鈺︹晹鈺愨晽鈺  鈺︹晹鈺愨晽鈺斺晲鈺
  76.   //  鈹溾敜 鈹屸敶鈹敇鈹溾攢鈹樷攤 鈹傗敂鈹€鈹愨敎鈹   鈹 鈹傗敎鈹敇鈹傗攤鈹 鈺戔晳鈺戔晳鈺 鈺 鈺戔暊鈺愨暎鈺  鈺戔晹鈺愨暆鈺戔暎
  77.   //  鈹斺攢鈹樷敶 鈹斺攢鈹  鈹斺攢鈹樷敂鈹€鈹樷敂鈹€鈹  鈹斺攢鈹樷敶鈹斺攢鈹 鈹磑鈺┾暆鈺氣暆鈺 鈺 鈺┾暕 鈺┾暕鈺愨暆鈺┾暁鈺愨暆鈺氣晲鈺
  78.  
  79.   /**
  80.    * .initialize()
  81.    *
  82.    * Start the ORM and set up active datastores.
  83.    *
  84.    * @param  {Dictionary}   options
  85.    * @param  {Function} cb
  86.    */
  87.   orm.initialize = function initialize(options, cb) {
  88.     // Ensure the ORM hasn't already been initialized.
  89.     // (This prevents all sorts of issues, because model definitions are modified in-place.)
  90.     if (_.keys(modelMap).length) {
  91.       throw new Error('A Waterline ORM instance cannot be initialized more than once. To reset the ORM, create a new instance of it by running `new Waterline()`.');
  92.     }
  93.  
  94.     // Backwards-compatibility for `connections`:
  95.     if (!_.isUndefined(options.connections)){
  96.       assert(_.isUndefined(options.datastores), 'Attempted to provide backwards-compatibility for `connections`, but `datastores` were ALSO provided!');
  97.       options.datastores = options.connections;
  98.       console.warn('\n'+
  99.         'Warning: `connections` is no longer supported.  Please use `datastores` instead.\n'+
  100.         'I get what you mean, so I temporarily renamed it for you this time, but here is a stack trace\n'+
  101.         'so you know where this is coming from in the code, and can change it to prevent future warnings:\n'+
  102.         '```\n'+
  103.         (new Error()).stack+'\n'+
  104.         '```\n'
  105.       );
  106.       delete options.connections;
  107.     }//>-
  108.  
  109.     // Usage assertions
  110.     if (_.isUndefined(options) || !_.keys(options).length) {
  111.       throw new Error('Usage Error: .initialize(options, callback)');
  112.     }
  113.  
  114.     if (_.isUndefined(options.adapters) || !_.isPlainObject(options.adapters)) {
  115.       throw new Error('Options must contain an `adapters` dictionary');
  116.     }
  117.  
  118.     if (_.isUndefined(options.datastores) || !_.isPlainObject(options.datastores)) {
  119.       throw new Error('Options must contain a `datastores` dictionary');
  120.     }
  121.  
  122.  
  123.     // Build up all the datastores used by our models.
  124.     try {
  125.       datastoreMap = DatastoreBuilder(options.adapters, options.datastores);
  126.     } catch (e) {
  127.       return cb(e);
  128.     }
  129.  
  130.     // Build a schema map
  131.     var internalSchema;
  132.     try {
  133.       internalSchema = new Schema(modelDefs, options.defaults);
  134.     } catch (e) {
  135.       return cb(e);
  136.     }
  137.  
  138.  
  139.     // Check the internal "schema map" for any junction models that were
  140.     // implicitly introduced above.  Whenever one is found, extend it using
  141.     // our WLModel constructor, then push it on to our set of modelDefs.
  142.     _.each(internalSchema, function(val, table) {
  143.       if (!val.junctionTable) {
  144.         return;
  145.       }
  146.  
  147.       modelDefs.push(WLModelConstructor.extend(internalSchema[table]));
  148.     });
  149.  
  150.  
  151.     // Hydrate each model definition (in-place), and also set up a
  152.     // reference to it in the model map.
  153.     _.each(modelDefs, function (modelDef) {
  154.  
  155.       // Set the attributes and schema values using the normalized versions from
  156.       // Waterline-Schema where everything has already been processed.
  157.       var schemaVersion = internalSchema[modelDef.prototype.identity.toLowerCase()];
  158.  
  159.       // Set normalized values from the schema version on the collection
  160.       modelDef.prototype.identity = schemaVersion.identity.toLowerCase();
  161.       modelDef.prototype.tableName = schemaVersion.tableName;
  162.       modelDef.prototype.datastore = schemaVersion.datastore;
  163.       modelDef.prototype.primaryKey = schemaVersion.primaryKey;
  164.       modelDef.prototype.meta = schemaVersion.meta;
  165.       modelDef.prototype.attributes = schemaVersion.attributes;
  166.       modelDef.prototype.schema = schemaVersion.schema;
  167.       modelDef.prototype.hasSchema = schemaVersion.hasSchema;
  168.  
  169.       // Mixin junctionTable or throughTable if available
  170.       if (_.has(schemaVersion, 'junctionTable')) {
  171.         modelDef.prototype.junctionTable = schemaVersion.junctionTable;
  172.       }
  173.  
  174.       if (_.has(schemaVersion, 'throughTable')) {
  175.         modelDef.prototype.throughTable = schemaVersion.throughTable;
  176.       }
  177.  
  178.       var collection = CollectionBuilder(modelDef, datastoreMap, context);
  179.  
  180.       // Store the instantiated collection so it can be used
  181.       // internally to create other records
  182.       modelMap[collection.identity.toLowerCase()] = collection;
  183.  
  184.     });
  185.  
  186.  
  187.     // Register each datastore with the correct adapter.
  188.     // (This is async because the `registerDatastore` method in adapters
  189.     // is async.  But since they're not interdependent, we run them all in parallel.)
  190.     async.each(_.keys(datastoreMap), function(item, nextItem) {
  191.  
  192.       var datastore = datastoreMap[item];
  193.       var usedSchemas = {};
  194.  
  195.       if (_.isFunction(datastore.adapter.registerConnection)) {
  196.         throw new Error('The adapter for datastore `' + item + '` is invalid: the `registerConnection` method must be renamed to `registerDatastore`.');
  197.       }
  198.  
  199.       // Note: at this point, the datastore should always have a usable adapter
  200.       // set as its `adapter` property.
  201.  
  202.       // Check if the datastore's adapter has a `registerDatastore` method
  203.       if (!_.has(datastore.adapter, 'registerDatastore')) {
  204.         return setImmediate(function() {
  205.           nextItem();
  206.         });
  207.       }
  208.  
  209.       // Add the datastore name as an identity property on the config
  210.       datastore.config.identity = item;
  211.  
  212.       // Get all the collections using the datastore and build up a normalized
  213.       // map that can be passed down to the adapter.
  214.       _.each(_.uniq(datastore.collections), function(modelName) {
  215.         var collection = modelMap[modelName];
  216.         var identity = modelName;
  217.  
  218.         // Normalize the identity to use as the tableName for use in the adapter
  219.         if (_.has(Object.getPrototypeOf(collection), 'tableName')) {
  220.           identity = Object.getPrototypeOf(collection).tableName;
  221.         }
  222.  
  223.         usedSchemas[identity] = {
  224.           primaryKey: collection.primaryKey,
  225.           definition: collection.schema,
  226.           tableName: collection.tableName || identity,
  227.           identity: identity
  228.         };
  229.       });
  230.  
  231.       // Call the `registerDatastore` adapter method.
  232.       datastore.adapter.registerDatastore(datastore.config, usedSchemas, nextItem);
  233.  
  234.     }, function(err) {
  235.       if (err) {
  236.         return cb(err);
  237.       }
  238.  
  239.       // Build up and return the ontology.
  240.       var ontology = {
  241.         collections: modelMap,
  242.         datastores: datastoreMap
  243.       };
  244.  
  245.       return cb(undefined, ontology);
  246.  
  247.     });//</async.each>
  248.  
  249.   };
  250.  
  251.  
  252.   //  鈹屸攢鈹愨攢鈹 鈹攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹  鈹屸攢鈹愨敩鈹€鈹愨攲鈹攼鈺斺暒鈺椻晹鈺愨晽鈺斺晲鈺椻暒鈺愨晽鈺斺暒鈺椻晹鈺愨晽鈺 鈺︹晹鈺椻晹
  253.   //  鈹溾敜 鈹屸敶鈹敇鈹溾攢鈹樷攤 鈹傗敂鈹€鈹愨敎鈹   鈹 鈹傗敎鈹敇鈹傗攤鈹 鈺 鈺戔暎 鈺犫晲鈺b暊鈺︹暆 鈺戔晳鈺 鈺戔晳鈺戔晳鈺戔晳鈺
  254.   //  鈹斺攢鈹樷敶 鈹斺攢鈹  鈹斺攢鈹樷敂鈹€鈹樷敂鈹€鈹  鈹斺攢鈹樷敶鈹斺攢鈹 鈹磑鈺 鈺氣晲鈺濃暕 鈺┾暕鈺氣晲鈺愨暕鈺濃暁鈺愨暆鈺氣暕鈺濃暆鈺氣暆
  255.   orm.teardown = function teardown(cb) {
  256.  
  257.     async.each(_.keys(datastoreMap), function(item, next) {
  258.       var datastore = datastoreMap[item];
  259.  
  260.       // Check if the adapter has a teardown method implemented.
  261.  
  262.       // If not, then just skip this datastore.
  263.       if (!_.has(datastore.adapter, 'teardown')) {
  264.         return setImmediate(function() {
  265.           next();
  266.         });
  267.       }
  268.  
  269.       // But otherwise, call its teardown method.
  270.       datastore.adapter.teardown(item, next);
  271.     }, cb);
  272.  
  273.   };
  274.  
  275.  
  276.   //  鈺︹晲鈺椻晹鈺愨晽鈺斺暒鈺椻暒 鈺︹暒鈺愨晽鈺斺晽鈺  鈹屸攼鈹屸攲鈹€鈹愨敩 鈹  鈹屸攢鈹愨敩鈹€鈹愨攲鈹攼  鈹攲鈹愨攲鈹屸攢鈹愨攲鈹攼鈹屸攢鈹愨攲鈹愨攲鈹屸攢鈹愨攲鈹€鈹
  277.   //  鈺犫暒鈺濃晳鈺  鈺 鈺 鈺戔暊鈺︹暆鈺戔晳鈺  鈹傗攤鈹傗敎鈹 鈹傗攤鈹  鈹 鈹傗敎鈹敇鈹傗攤鈹  鈹傗攤鈹傗攤鈹斺攢鈹 鈹 鈹溾攢鈹も攤鈹傗攤鈹  鈹溾敜
  278.   //  鈺┾暁鈺愨暁鈺愨暆 鈺 鈺氣晲鈺濃暕鈺氣晲鈺濃暁鈺  鈹樷敂鈹樷敂鈹€鈹樷敂鈹粹敇  鈹斺攢鈹樷敶鈹斺攢鈹 鈹  鈹粹敇鈹斺敇鈹斺攢鈹 鈹 鈹 鈹粹敇鈹斺敇鈹斺攢鈹樷敂鈹€鈹
  279.   return orm;
  280.  
  281. };
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289. //  鈺斺晲鈺椻晲鈺 鈺︹晹鈺︹晽鈺斺晲鈺椻晹鈺椻晹鈺斺晲鈺椻暒鈺斺晲鈺椻晹鈺椻晹鈺斺晲鈺
  290. //  鈺戔暎 鈺斺暕鈺︹暆 鈺 鈺戔暎 鈺戔晳鈺戔暁鈺愨晽鈺戔晳 鈺戔晳鈺戔晳鈺氣晲鈺
  291. //  鈺氣晲鈺濃暕 鈺氣晲 鈺 鈺氣晲鈺濃暆鈺氣暆鈺氣晲鈺濃暕鈺氣晲鈺濃暆鈺氣暆鈺氣晲鈺
  292.  
  293. // Expose the stateless Waterline `Model` constructor for direct access聽from your application.
  294. module.exports.Model = WLModelConstructor;
  295. //(note that we also expose `Collection` as an alias, but only for backwards compatibility)
  296. module.exports.Collection = WLModelConstructor;
  297.  
downloadwaterline.js Source code - Download waterline Source code
Related Source Codes/Software:
notepad-plus-plus - Notepad++ official repository h... 2017-01-10
che - Eclipse Che: Next-generation Eclipse IDE. Open sou... 2017-01-10
Gource - oftware version control visualization ... 2017-01-10
FDFullscreenPopGesture - A UINavigationController's category to enable full... 2017-01-10
node-style-guide - A guide for styling your node.js / JavaScript code... 2017-01-09
Workerman - An asynchronous event driven PHP framework for eas... 2017-01-10
structor - An advanced visual editor for React components ... 2017-01-10
golearn - Machine Learning for Go 2017-01-10
poisontap - Exploits locked/password protected computers over ... 2017-01-10
kcptun - A Simple UDP Tunnel Based On KCP 2017-01-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top