@cenchat/cloud-firestore-modeldeprecated

Model system designed for Cloud Firestore

Usage no npm install needed!

<script type="module">
  import cenchatCloudFirestoreModel from 'https://cdn.skypack.dev/@cenchat/cloud-firestore-model';
</script>

README

Cloud Firestore Model

Library for managing your Cloud Firestore Models in Node.js

Installation

npm install --save @cenchat/cloud-firestore-model`

Usage

Defining Models

Let's assume that we place this code under src/models/user.js

const { Attribute, Model } = require('@cenchat/cloud-firestore-model');

class User extends Model {
  get validation() {
    return {
      age: new Attribute('number'),
      createdOn: new Attribute('timestamp', { isEditable: false }),
      name: new Attribute('string', { maxLength: 50 }),
    }
  }
}

module.exports = User;

Creating a Record

const admin = require('firebase-admin');

const User = require('src/models/user');

const newUser = new User(admin.firestore(), 'users');

newUser.attribute = { age: 20, createdOn: new Date(), name: 'Foo Bar' };

newUser.validate().then(() => {
  newUser.ref.set(newUser.attribute);
}).catch((error) => {
  console.log(`Invalid: ${error}`);
});

Updating a Record

const admin = require('firebase-admin');

const User = require('src/models/user');

const existingUser = new User(admin.firestore(), 'users/user_a');

existingUser.load().then(() => {
  existingUser.attribute = { age: 20 };

  existingUser.validate().then(() => {
    existingUser.ref.update(existingUser.attribute);
  }).catch((error) => {
    console.log(`Invalid: ${error}`);
  });
});

Developing

Installation

  • git clone <repository-url> this repository
  • cd cloud-firestore-model
  • npm install

Running Tests

  • npm test

Further Reading / Useful Links