Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORM for firestore [closed]

Are there any ORMs that support firestore in nodejs? I especially used to like ndb in python.

like image 877
Gubbi Avatar asked Oct 12 '17 02:10

Gubbi


People also ask

Is Firebase an ORM?

Arbel Firebase Orm is an ORM that can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, and Electron platforms and can be used with TypeScript and JavaScript (ES5, ES6, ES7, ES8). Firebase ORM supports only Active Record pattern for now.

Does firestore Create collection if not exists?

Collections and documents are created implicitly in Cloud Firestore. Simply assign data to a document within a collection. If either the collection or document does not exist, Cloud Firestore creates it.

Why are my firestore reads so high?

If you leave the console open on a collection or document with busy write activity then the Firebase console will automatically read the changes that update the console's display. Most of the time this is the reason for unexpected high reads.


1 Answers

We're working on one (by we I mean Invertase / creators of react-native-firebase). If you've used mongoose or waterline on node before then you'll find it overly familiar as that's what we're using for inspiration.

This is all still internal, but to give you an idea of the api here's one of the models / usages examples we have internally:

const User = model('User', {
  // auto create/update date fields
  autoCreatedAt: true,
  autoUpdatedAt: true,

  // auto created/updated by fields, uses current auth user or 'service-account'
  autoUpdatedBy: true,
  autoCreatedBy: true,

  // toggle schema/less. If turned off, this will allow you to store arbitrary
  // data in a record. If turned on, only attributes defined in the model's
  // attributes object will be stored.
  schema: true,

  attributes: {
    first_name: {
      type: 'string',
      required: true
    },
    last_name: {
      type: 'string',
      required: true
    },

    // virtual field support
    full_name() {
      return `${this.first_name} ${this.last_name}`;
    },

    age: {
      type: 'integer'
    },
    email: {
      type: 'email',
      required: true
    },
    someBool: {
      type: 'boolean',
      defaultsTo: false
    },

    // association example- in this case a child collection of the users doc
    // e.g /users/id/posts
    posts: {
      collection: 'posts',
    }
  }
});

// magic methods based on attributes
// findByX or findOneByX
User.findByAge(27).then((val) => {
  // val = [] or [document object]
}).catch((error) => {
  debugger;
});

// magic methods based on attributes
// findByX or findOneByX
User.findByName('mike').then((val) => {
  // val = [] or [document object]
}).catch((error) => {
  debugger;
});

// find a single document
User.findOne().then((val) => {
  // val = undefined or document object
}).catch((error) => {
  debugger;
});

// find multiple docs
User.find({
  name: 'mike',
  age: 27,
  someBool: true,
}).then((val) => {
  // val = [] or [document object]
}).catch((error) => {
  debugger;
});

// find multiple docs with age between range
User.find({
  someBool: true,
  age: {
    $gte: 27,
    $lte: 35
  }
}).then((val) => {
  // val = [] or [document object]
}).catch((error) => {
  debugger;
});

Keep an eye out on our discord, Github org or twitter - hope to have a public alpha in a few days.

The example above doesn't showcase everything we're planning but we're planning to support things like paginations (skip, limit, page), createOrUpdate, findOrCreate, subscribe() - for realtime, multiple range filters (first one sent to firestore, the rest done client side) etc

Update:

A very early on repo has been made public on github here. It generally works, docs still need doing and a few things missing code wise - for usage see the tests - it's well tested (test driven development), if you'd like to contribute then please do :) we're on pause for Firepit right now as we're pushing a release of React Native Firebase at the moment.

like image 113
Salakar Avatar answered Oct 16 '22 08:10

Salakar