# Creation

Each entity is an instance of a Model and represents an entity stored in the Datastore of a certain *Kind*.\
To create an entity you call the Model constructor

```javascript
new Model(
    /* {object} -- The entity data to be saved */
    <data>,
    /* {int|string}. -- optional. The id of the entity.
                        If not passed the Datastore will automatically 
                        generate a random one
     */
    <id>,
    /* {Array} -- optional. Ancestors of the entity. ex: ['ParentEntity', 1234 ] */
    <ancestors>,
    /* {string} -- optional. A specific namespace */
    <namespace>
)

example:
const blogPost = new BlogPost({title: 'title of the post', author: 'John Snow' });
```

## id

**(optional)**

By default, if you don't pass an id when you create an instance, the entity id will be auto-generated. If you want to manually give the entity an id, pass it as a second parameter during the instantiation.

```javascript
// String id
const blogPost = new BlogPost(data, 'stringId');

// Integer ir
const blogPost = new BlogPost(data, 1234);
```

If the ID integer is **outside the bounds of a JavaScript Number object**, you have to create an **Int** calling the int() method from @google-cloud/datastore

Reminder: *gstore.ds* is an alias to the google-cloud **datastore** instance.

```javascript
// long Key ID 
const blogPost = new BlogPost(data, gstore.ds.int('100000000000001234'));
```

## ancestors

**(optional)**

You can define the ancestors of the entity.

```javascript
// Auto generated id with an ancestor
const blogPost = new BlogPost(data, null, ['Parent', 'keyname']);

// Manual id on an ancestor
const blogPost = new BlogPost(data, 1234, ['Parent', 'keyname']);
```

## namespace

**(optional)**

By default entities keys are generated with the default namespace (defined when setting up the '@google-cloud/datastore'). You can create models instances on another namespace by passing it as a third parameter.

```javascript
// Creates an entity with auto-generated id on the namespace "dev-com.my-domain"
const blogPost = new BlogPost(data, null, null, 'dev.com.my-domain');
```


---

# Agent Instructions: 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:

```
GET https://sebloix.gitbook.io/gstore-node/v8.x/entity/creation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
