How to use the orm.enforce function in orm

To help you get started, we’ve selected a few orm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rafaelkaufmann / q-orm / examples / basic.js View on Github external
var Person = db.qDefine("person", {
		name      : String,
		surname   : String,
		age       : Number,
		male      : Boolean,
		continent : [ "Europe", "America", "Asia", "Africa", "Australia", "Antartica" ], // ENUM type
		photo     : Buffer, // BLOB/BINARY
		data      : Object // JSON encoded
	}, {
		methods: {
			fullName: function () {
				return this.name + ' ' + this.surname;
			}
		},
		validations: {
			age: orm.enforce.ranges.number(18, undefined, "under-age")
		}
	});

	return Person.qAll({ surname: "Doe" })
	.then(function (people) {
		// SQL: "SELECT * FROM person WHERE surname = 'Doe'"

		console.log("People found: %d", people.length);
		console.log("First person: %s, age %d", people[0].fullName(), people[0].age);

		people[0].age = 16;
		return people[0].qSave()
		.fail(function (err) {
			console.log(err.stack);
		});
	});
github DnD-industries / stellar_tipping_bot / src / models / transaction.js View on Github external
*/
  const Transaction = db.define('transaction', {
      source: String,
      target: String,
      cursor: String,
      memoId: String,
      type: ['deposit', 'withdrawal', 'refund'],
      createdAt: String,
      amount: String,
      asset: String,
      hash: String,
      credited: Boolean,
      refunded: Boolean
    }, {
    validations : {
      source : orm.enforce.required('source is required'),
      target : orm.enforce.required('target is required'),
      type : orm.enforce.required('type is required'),
      amount : orm.enforce.required('amount is required'),
      createdAt: orm.enforce.required('createdAt is required'),
      hash: [orm.enforce.unique('Hash already exists.'), orm.enforce.required()]
    },
    methods: {
      /**
       * Refund money from the main account to the source public account of the transaction
       *
       * You get the stellar object from the adapter config.
       *
       * @param stellar The stellar object to send the refund with
       * @param transaction The transaction to be refunded
       * @returns {Promise}
       */
github DnD-industries / stellar_tipping_bot / src / models / transaction.js View on Github external
memoId: String,
      type: ['deposit', 'withdrawal', 'refund'],
      createdAt: String,
      amount: String,
      asset: String,
      hash: String,
      credited: Boolean,
      refunded: Boolean
    }, {
    validations : {
      source : orm.enforce.required('source is required'),
      target : orm.enforce.required('target is required'),
      type : orm.enforce.required('type is required'),
      amount : orm.enforce.required('amount is required'),
      createdAt: orm.enforce.required('createdAt is required'),
      hash: [orm.enforce.unique('Hash already exists.'), orm.enforce.required()]
    },
    methods: {
      /**
       * Refund money from the main account to the source public account of the transaction
       *
       * You get the stellar object from the adapter config.
       *
       * @param stellar The stellar object to send the refund with
       * @param transaction The transaction to be refunded
       * @returns {Promise}
       */
      refund: async function (stellar, transaction) {
        const Transaction = db.models.transaction;
        const Action      = db.models.action;
        const Account     = db.models.account;
github es / OnlineJudge / app / models / article.js View on Github external
module.exports = function(db, callback) {
    var ArticleSchema = db.define('article', {
        created: Date,
        title: String,
        content: String
    }, {
        autoFetch: true,

        /**
         * Validations
         */
        validations: {
            title: orm.enforce.notEmptyString("Title can't be empty")
        }
    });
    return callback();
 };
github DnD-industries / stellar_tipping_bot / src / models / account.js View on Github external
if(!this.hasAcceptedTerms) {
          this.hasAcceptedTerms = false
        }

        if (!this.createdAt) {
          this.createdAt = now.toISOString()
        }
        if (!this.balance) {
          this.balance = '0.0000000'
        }
        this.updatedAt = now.toISOString()
      }
    },

    validations : {
      adapter : orm.enforce.required('adapter is required'),
      uniqueId : orm.enforce.required('uniqueId is required'),
      walletAddress : orm.enforce.unique(`Can't register the same wallet for two different users`),
    }
  })

  /**
   * Account save get or create.
   * doc is optional (adapter and uniqueId are taken if not given)
   */
  Account.getOrCreate = async function (adapter, uniqueId, doc) {
    return await Account.withinTransaction(async () => {
      let a = await Account.oneAsync({ adapter, uniqueId })
      if (!a) {
        doc = doc || {}
        if (!doc.hasOwnProperty('adapter')) {
          doc.adapter = adapter
github DnD-industries / stellar_tipping_bot / src / models / account.js View on Github external
}

        if (!this.createdAt) {
          this.createdAt = now.toISOString()
        }
        if (!this.balance) {
          this.balance = '0.0000000'
        }
        this.updatedAt = now.toISOString()
      }
    },

    validations : {
      adapter : orm.enforce.required('adapter is required'),
      uniqueId : orm.enforce.required('uniqueId is required'),
      walletAddress : orm.enforce.unique(`Can't register the same wallet for two different users`),
    }
  })

  /**
   * Account save get or create.
   * doc is optional (adapter and uniqueId are taken if not given)
   */
  Account.getOrCreate = async function (adapter, uniqueId, doc) {
    return await Account.withinTransaction(async () => {
      let a = await Account.oneAsync({ adapter, uniqueId })
      if (!a) {
        doc = doc || {}
        if (!doc.hasOwnProperty('adapter')) {
          doc.adapter = adapter
        }
        if (!doc.hasOwnProperty('uniqueId')) {
github DnD-industries / stellar_tipping_bot / src / models / action.js View on Github external
module.exports = (db) => {

  /**
   * A generic action
   */
  const Action = db.define('action', {
      type: ['withdrawal', 'deposit', 'transfer', 'refund'],
      amount: String,
      address: String,
      hash: String,
      createdAt: String
    }, {
    validations : {
      type: orm.enforce.required('Type is required.'),
      amount: orm.enforce.required('Amount is required.'),
      hash: orm.enforce.required('Hash is required.'),
      createdAt: orm.enforce.required('createdAt is required.')
    },
    hooks: {
      beforeSave: function () {
        const now = new Date()
        if (!this.createdAt) {
          this.createdAt = now.toISOString()
        }
      }
    }
  })

  Action.hasOne('sourceAccount', db.models.account, { reverse: 'sourceActions' })
  Action.hasOne('targetAccount', db.models.account, { reverse: 'targetActions' })
github DnD-industries / stellar_tipping_bot / src / models / action.js View on Github external
/**
   * A generic action
   */
  const Action = db.define('action', {
      type: ['withdrawal', 'deposit', 'transfer', 'refund'],
      amount: String,
      address: String,
      hash: String,
      createdAt: String
    }, {
    validations : {
      type: orm.enforce.required('Type is required.'),
      amount: orm.enforce.required('Amount is required.'),
      hash: orm.enforce.required('Hash is required.'),
      createdAt: orm.enforce.required('createdAt is required.')
    },
    hooks: {
      beforeSave: function () {
        const now = new Date()
        if (!this.createdAt) {
          this.createdAt = now.toISOString()
        }
      }
    }
  })

  Action.hasOne('sourceAccount', db.models.account, { reverse: 'sourceActions' })
  Action.hasOne('targetAccount', db.models.account, { reverse: 'targetActions' })

  return Action
}
github DnD-industries / stellar_tipping_bot / src / models / slack-auth.js View on Github external
module.exports = (db) => {

  /**
   * Storage and representation of an oauth token that has been given to us
   */
  const SlackAuth = db.define('slackauth', {
    team: String,
    token: String,
    bot_token: String,
    created_at: String
  }, {
    validations : {
      team: orm.enforce.required('Team is required.'),
      token: orm.enforce.required('Token is required.'),
      bot_token: orm.enforce.required('Bot Token is required'),
      created_at: orm.enforce.required('created_at is required.')
    },
    hooks: {
      beforeSave: function () {
        const now = new Date()
        if (!this.created_at) {
          this.created_at = now.toISOString()
        }
      }
    }
  })

  /**
   *
github komarserjio / notejam / express / notejam / models.js View on Github external
Pad.hasOne("user", User, { required: true, reverse: 'pads' });

  var Note = db.define("notes", {
    id         : { type: "serial", key: true },
    name       : { type: "text" },
    text       : { type: "text" },
    created_at : { type: "date", time: true },
    updated_at : { type: "date", time: true }
  }, {
    methods: {
      updatedAt: function () {
        return moment(this.updated_at).fromNow();
      }
    },
    validations: {
      name: orm.enforce.notEmptyString("Name is required"),
      text: orm.enforce.notEmptyString("Text is required"),
    }
  });
  Note.hasOne("user", User, { required: true, reverse: 'notes' });
  Note.hasOne("pad", Pad, { required: false, reverse: 'notes' });

  return cb();
};

orm

NodeJS Object-relational mapping

MIT
Latest version published 1 year ago

Package Health Score

60 / 100
Full package analysis