Piece

Piece

(protected) new Piece(base, client, options)

Initialize a new Piece. You need to use MuffinClient#piece or MuffinClient#multi to do that.

Since:
  • 1.0
Source:
Parameters:
Name Type Description
base Collection

The Collection from MongoDB.

client MuffinClient

The client that instantiated the Piece.

options PieceOptions

Actually there is only the fetchAll option.

Example
const Muffin = require("muffindb");
const client = new Muffin.Client(options);

const piece = client.piece("example", { fetchAll: true, cacheSyncAuto: true })

Members

base :Collection

The collection wrapped by the piece.

Since:
  • 1.0
Source:
Type:
  • Collection

cache :Map

An optional cache, it can be useful but it can also uses a lot of ram.

Since:
  • 1.2
Source:
Type:
  • Map

client :MuffinClient

The client that instantiated the Piece.

Since:
  • 1.0
Source:
Type:

hasCache :boolean

If set to true, the cache is available.

Since:
  • 1.2
Source:
Type:
  • boolean

isCacheReady :Promise

If the fetchAll option has been set to true, this property will be true when all the data have been cached. Otherwise it is always true.

Since:
  • 1.3
Source:
Type:
  • Promise

Methods

(async) clear() → {Promise.<void>}

Deletes all the documents.

Since:
  • 1.0
Source:
Returns:
Type:
Promise.<void>

A promise

(async) delete(key, pathopt) → {Promise.<void>}

Deletes a document in the database.

Since:
  • 1.0
Source:
Parameters:
Name Type Attributes Default Description
key *

The key.

path string <optional>
null

Optional. The path to the property to delete. Can be a dot-separated path, such as "prop1.subprop2.subprop3".

Returns:
Type:
Promise.<void>

A promise

(async) ensure(key, val, pathopt, rawopt) → {Promise.<any>}

If the document doesn't exists : creates it and returns it, if it does exists : returns it.

Since:
  • 1.0
Source:
Parameters:
Name Type Attributes Default Description
key *

The key to check if it exists or to set a document or a property inside the value.

val *

The value to set if the key doesn't exist.

path string <optional>
null

Optional. The path to the property to ensure. Can be a dot-separated path, such as "prop1.subprop2.subprop3".

raw boolean <optional>
false

Optional. Returns the full object, i.e. : { _id: "foo", value: "bar" }. Not used if you don't use the cache.

Returns:
Type:
Promise.<any>

If raw is set to false, returns the value found in the database for this key.

Example
// If foo exists it returns its value, if it doesn't it set its value to "bar" and returns it
const data = piece.ensure("foo", "bar");

evict(key, pathopt) → {void}

Do not works if the cache is not activated. Removes a cached element, it does not touch the real database.

Since:
  • 1.2
Source:
Parameters:
Name Type Attributes Default Description
key *

The key.

path string <optional>
null

Optional. The path to the property to uncache. Can be a dot-separated path, such as "prop1.subprop2.subprop3".

Returns:
Type:
void
  • Nothing

evictAll() → {void}

Do not works if the cache is not activated. Removes all the cached elements. It does not touch the real database.

Since:
  • 1.2
Source:
Returns:
Type:
void
  • Nothing

(async) fetch(key, pathopt, rawopt) → {Promise.<any>}

Do not works if the cache is not activated. Fetch a document on the database.

Since:
  • 1.2
Source:
Parameters:
Name Type Attributes Default Description
key *

The key of the document to get

path string <optional>
null

Optional. The path to the property to take inside the value. Can be a dot-separated path, such as "prop1.subprop2.subprop3".

raw boolean <optional>
false

Optional. Returns the full object, i.e. : { _id: "foo", value: "bar" }. Not used if you don't use the cache.

Returns:
Type:
Promise.<any>

If raw is set to false, returns the value found in the database for this key.

Example
// Returns the value for the key foo and update the cache
const data = await piece.fetch("foo");

// Returns the value of the property bar and update the cache
const data = await piece.fetch("foo", "bar")

(async) fetchAll() → {Promise.<void>}

Fetch all the database and caches it all. It also updates already cached data.

Since:
  • 1.2
Source:
Returns:
Type:
Promise.<void>

Nothing

(async) get(key, pathopt, rawopt) → {Promise.<any>}

Gets a document in the database.

Since:
  • 1.0
Source:
Parameters:
Name Type Attributes Default Description
key *

The key of the document to get.

path string <optional>
null

Optional. The path to the property to take inside the value. Can be a dot-separated path, such as "prop1.subprop2.subprop3".

raw boolean <optional>
false

Optional. Returns the full object, i.e. : { _id: "foo", value: "bar" }. Not used if you don't use the cache.

Returns:
Type:
Promise.<any>

If raw is set to false, returns the value found in the database for this key.

Example
// Returns the value for the key foo
const data = await piece.get("foo");

// Returns the value of the property bar
const data = await piece.get("foo", "bar")

(async) has(key, pathopt) → {Promise.<boolean>}

Checks if a document exists.

Since:
  • 1.0
Source:
Parameters:
Name Type Attributes Default Description
key *

The key of the document to check.

path string <optional>
null

Optional. The path to the property to check. Can be a dot-separated path, such as "prop1.subprop2.subprop3".

Returns:
Type:
Promise.<boolean>

A promise

Example
// If the key "foo" doesn't exists, it returns
if (!await piece.has("foo")) return;

// If the property "bar" of the value of "foo" dosn't exists, it returns
if (!await piece.has("foo", "bar")) return;

(async) keyArray(cacheopt) → {Promise.<Array.<*>>}

Since:
  • 1.0
Source:
Parameters:
Name Type Attributes Default Description
cache boolean <optional>
true

Optional. If there is a cache, per defaut it is set to true and it will takes cached data. If you set it to false, it will takes the data from the Mongo server.

Returns:
Type:
Promise.<Array.<*>>

A promise. When resolved, returns an array with the keys of all the documents

(async) push(key, val, pathopt, allowDupesopt) → {Promise.<void>}

Push to an array value.

Since:
  • 1.2
Source:
Parameters:
Name Type Attributes Default Description
key *

The key of the document.

val *

The value to push.

path string <optional>
null

Optional. The path to the property to modify inside the element. Can be a dot-separated path, such as "prop1.subprop2.subprop3".

allowDupes boolean <optional>
false

Optional. Allow duplicate values in the array.

Returns:
Type:
Promise.<void>

A promise

Example
// Adds the value "bar" to the array that is the value of the key "foo"
await piece.push("foo", "bar");

// We can also do that for properties that are arrays
await piece.push("foo", "baz", "bar");

(async) rawArray() → {Promise.<Array.<any>>}

Since:
  • 1.0
Source:
Returns:
Type:
Promise.<Array.<any>>

An array with all the documents of the database

(async) set(key, val, pathopt) → {Promise.<void>}

Sets a document into the database.

Since:
  • 1.0
Source:
Parameters:
Name Type Attributes Default Description
key *

The key of the document to set.

val *

The value of the document to set into the database.

path string <optional>
null

The path to the property to modify inside the value. Can be a dot-separated path, such as "prop1.subprop2.subprop3".

Returns:
Type:
Promise.<void>

A promise

Example
// Sets the value "bar" to the key "foo"
await piece.set("foo", "bar");

// Sets the value "baz" to the property bar
await piece.set("foo", "baz", "bar");

(async) size(fastopt) → {Promise.<number>}

Since:
  • 1.0
Source:
Parameters:
Name Type Attributes Default Description
fast boolean <optional>
false

Optional. Set to true if your database is very big (the size will be less precise but it will be faster).

Returns:
Type:
Promise.<number>

A promise returning the size of the database when resolved

(async) valueArray(cacheopt) → {Promise.<Array.<*>>}

Since:
  • 1.0
Source:
Parameters:
Name Type Attributes Default Description
cache boolean <optional>
true

Optional. If there is a cache, per defaut it is set to true and it will takes cached data. If you set it to false, it will takes the data from the Mongo server.

Returns:
Type:
Promise.<Array.<*>>

A promise. When resolved, returns an array with the values of all the documents

Events

change

Emit when a change occurs on the database.

Since:
  • 1.2
Source:
Type:
  • any