npm package: @offlinely/server
Description: This package will help you to keep your **offline!** app to be synced with a mongodb database online.
Latest version: 3.0.9

# Welcome to Offlinely!

This package will help you to keep your **offline!** app to be synced with a mongodb database online.

## Installation

```yarn install @offlinely/server```

For client integration, take a look at [@offlinely/idb](https://www.npmjs.com/package/@offlinely/idb)

## How to use the *Server*
* Creating models:
  
  Same as [@offlinely/idb](https://www.npmjs.com/package/@offlinely/idb), and you need to create an instance of `IDB.Database` for each db, if you have only one App, you will just need one instance of `IDB.Database`.
  Make sure you have the same structures in backend and front.
  For more about the model structure, take a look at the doc in [@offlinely/idb](https://www.npmjs.com/package/@offlinely/idb)

  Example:

  ```ts
  // models/car-brand.ts

  import IDB from  '@offlinely/backend'
  const createApp = () => {
    return new IDB.Database({
      User: {
        firstname:  String,
        password: {$type:  String, $required:  true},
        access:  Boolean
      },  
      Car: {
        name:  String,
        user: { $ref: 'User'}
      },
    })
  }

  export default createApp
  ```

* Creating the server
  ```ts
  import  IDB  from  "@offlinely/backend"
  import  createApp  from  "./models/car-brand"

  const server = new IDB({
    TanaApp: createApp(),
    TulearApp: createApp()
    // here, keys 'App1' and 'App2' will be the Mongo database name
  })
  ```
⚠️ Important: Each object in IBD must be new Objects, here `createApp` creates a new instance of with a Car branding model ⚠

## Documentation
#### Database (Models)
* Models are just an instance of `IDB.Database` with custom keys and types.
  Types may be: `String`, `Number`, `Boolean`, `Date`, `File` (see File section), `Enum` (See Enum section), or a **Complex Structure** from your weird imagination


  ```ts
  // server.ts
  export const myDatabase = new IDB.Database({
    User: {
      firstame: String,
      lastname: String,
      age: Number,
      access: Boolean,
      // Adding $required: false will set the reference not required for creating the item
      parents: {
        dad: {$ref: 'User', $required: false},
        mom: {$ref: 'User'}
      }
    },
    ...
  })
  ```
#### Server
* Server is an instance of IDB that, it will manage the syncing, the databases and ~~custom events and actions~~ `[TODO]`.
* You can sync as many app as you want in one instance on IDB

  `class IDB(models, IDBConfig)`
  ```ts
  const server =  new IDB({
    EniDB: createSchoolDBModel(),
    EmitDB: createSchoolDBModel(),
    EstiDB: createSchoolDBModel(),
    RemaxDB: createSellDBModel(),
    MinisoDB: createSellDBModel(),
    BikerDB: createBikeManagingDBModel()
  }, {
    port: 4000,
    mongoString: 'mongo://...'
  })
  ```

  | **Name** | **Type** | **Required** | **Default** | **Description** |
  |---|---|--:|---|---|
  | schema | Object | Yes | - | Backend socket backend URL |
  | IDBConfig.mongoString | string | No | `"mongo://localhost:27017"` | Connexion string of mongo database |
  | IDBConfig.port | number | No | `8000` | The server port that will be used |

  
#### Linking with the FrontEnd

* To sync the server with a frontEnd app, you have to get a token from the backend. To do so call the getTokens method of the created server object.
* You have to get these tokens just once, they will be valid even if the server restarts

  ```ts
  const server =  new IDB(...)

  server.onReady(() => {
    console.log(server.getTokens())
  })
  ```