> For the complete documentation index, see [llms.txt](https://samypesse.gitbook.io/blini/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://samypesse.gitbook.io/blini/guides/getting-started.md).

# Getting started

## Installing

Blini is an npm module, so to install it you do:

```
$ npm install Blini --save
```

## Importing

Once you've install it, you'll need to import it.

```javascript
import { Schema, Model, Validation, Type, Connection } from 'Blini'
```

## Create a connection to MongoDB

The first step

In addition to loading the module, you need to give Blini a connection to work with. Without it, Blini can't access the database.

The connection doesn't require to be initialized, it'll be established as soon as database operations are required.

```javascript
const connection = new Connection('mongodb://localhost:27017/myproject');
```

## Create a schema

A schema describe the structure of the documents. It is basically a map of field name to [`Type`](https://github.com/samypesse/blini/tree/4acfa8dba5ac2fc91fc7fc7128682ae3287cbff8/docs/types.md).

```javascript
const userSchema = new Schema({
    username: Type.String({
        validation: [
            Validation.required(),
            Validation.minLength(3, 'Username should be 3 characters min'),
            Validation.maxLength(30, 'Username should be 40 characters max')
        ],
        index: {
            unique: true
        }
    }),
    books: Type.Set(Type.Ref('Book'), {
        validations: [
            Validation.default([]),
        ]
    })
})
```

Follow the ["Create a Schema"](/blini/guides/create-schema.md) guide to get more insight on how schemas work.

## Create a new document model

Once the data schema is defined, the next step is to create the model which store operations around the database for a schema.

```javascript
class User extends Model(userSchema, connection, 'User') {
    static getByUsername(username) {
        return this
            .findOne({
                username: username
            })
            .exec();
    }
}
```

## Create new documents

Using the document model, we can initialize new document instances and populate the database:

```javascript
let user = new User({
    username: 'JohnDoe'
})

user.save()
    .then(function(savedUser) {
        ...
    })
```

## Query the collection

Querying documents from the database and update it:

```javascript
User.findOne({ username: 'JohnDoe' })
    .then(function(user) {
        const modified = user.merge({
            username: "John"
        })

        return modified.save()
    })
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://samypesse.gitbook.io/blini/guides/getting-started.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
