<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          JugglingDB跨數(shù)據(jù)庫的 ORM 框架

          聯(lián)合創(chuàng)作 · 2023-09-23 18:35

          JugglingDB 是一個跨數(shù)據(jù)庫的 ORM 框架,提供了訪問大多數(shù)數(shù)據(jù)庫的通用接口,支持包括:mysql, mongodb, redis, neo4j and js-memory-storage. 你可擴展其他數(shù)據(jù)庫的適配器,支持回調和鉤子。

          示例代碼:

          var Schema = require('./jugglingdb').Schema;
          var schema = new Schema('redis', {port: 6379}); //port number depends on your configuration
          // define models
          var Post = schema.define('Post', {
              title:     { type: String, length: 255 },
              content:   { type: Schema.Text },
              date:      { type: Date,    default: Date.now },
              published: { type: Boolean, default: false }
          });
          // simplier way to describe model
          var User = schema.define('User', {
              name:         String,
              bio:          Schema.Text,
              approved:     Boolean,
              joinedAt:     Date,
              age:          Number
          });
          
          // setup relationships
          User.hasMany(Post,   {as: 'posts',  foreignKey: 'userId'});
          // creates instance methods:
          // user.posts(conds)
          // user.posts.build(data) // like new Post({userId: user.id});
          // user.posts.create(data) // build and save
          
          Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
          // creates instance methods:
          // post.author(callback) -- getter when called with function
          // post.author() -- sync getter when called without params
          // post.author(user) -- setter when called with object
          
          schema.automigrate(); // required only for mysql NOTE: it will drop User and Post tables
          
          // work with models:
          var user = new User;
          user.save(function (err) {
              var post = user.posts.build({title: 'Hello world'});
              post.save(console.log);
          });
          
          // or just call it as function (with the same result):
          var user = User();
          user.save(...);
          
          // Common API methods
          
          // just instantiate model
          new Post
          // save model (of course async)
          Post.create(cb);
          // all posts
          Post.all(cb)
          // all posts by user
          Post.all({where: {userId: user.id}, order: 'id', limit: 10, skip: 20});
          // the same as prev
          user.posts(cb)
          // same as new Post({userId: user.id});
          user.posts.build
          // save as Post.create({userId: user.id}, cb);
          user.posts.create(cb)
          // find instance by id
          User.find(1, cb)
          // count instances
          User.count([conditions, ]cb)
          // destroy instance
          user.destroy(cb);
          // destroy all instances
          User.destroyAll(cb);
          
          // Setup validations
          User.validatesPresenceOf('name', 'email')
          User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}});
          User.validatesInclusionOf('gender', {in: ['male', 'female']});
          User.validatesExclusionOf('domain', {in: ['www', 'billing', 'admin']});
          User.validatesNumericalityOf('age', {int: true});
          User.validatesUniquenessOf('email', {message: 'email is not unique'});
          
          user.isValid(function (valid) {
              if (!valid) {
                  user.errors // hash of errors {attr: [errmessage, errmessage, ...], attr: ...}    
              }
          })
          
          瀏覽 13
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  三级片在线视频观看 | 夜夜躁很很躁日日躁麻豆 | 51国产视频| 欧美日韩成人一区二区在线观看 | 欧美成人午夜 |