/gm_bson

bson for gmod

Primary LanguageRustMozilla Public License 2.0MPL-2.0

gm_bson

bson for gmod

Docs

Bson

bson.as_json

Alias: bson.as_relaxed_json
Attribute, computed on __index call.

Converts BSON to relaxed extJSON.

local bsn = bson.from_lua { a = 1 }
print(bsn.as_json) --> {"a":1}

bson.as_canonical_json

Attribute, computed on __index call.

Converts BSON to canonical extJSON.

local bsn = bson.from_lua { a = 1 }
print(bsn.as_canonical_json) --> {"$numberInt":1}

bson.__tostring(self)

Metamethod, computed on call.

Same as bson.as_json

print(bson.from_lua {a=1}} --> {"a":1}

bson.from_lua(value: any): Bson

Converts any lua type into Bson.
Unsupported types: function, lightuserdata, thread

local data = bson.from_lua {
  _id = bson.objectid(),
  name = "John",
  age = 31,
}

bson.to_lua(value: Bson): any

Converts Bson value into lua.

local table = bson.to_lua(data)
-- _id = objectid(smth),
-- name = "John",
-- age = 31,

bson.objectid(oid: string | nil): objectid

Creates new ObjectID from given Base64 value or generates a new one.

local oid = bson.objectid()
-- or
local oid = bson.objectid("stmth")

bson.datetime(ms: integer | nil): datetime

Creates new Datetime from given timestamp or from current time.

local dt = bson.datetime(1)
-- or
local dt = bson.datetime()

bson.timestamp(time: integer, increment: integer | nil): timestamp

Creates new Timestamp from given time and increment, if present.

local ts = bson.timestamp(1, 1)
-- or
local ts = bson.timestamp(1)

bson.binary(bin: string): binary

Creates new Generic Binary from given string.

local bin = bson.binary "123"

bson.regex(pattern: string, options: string | nil): regex

Creates new Regex from given pattern and options, if present.
Patterns must be in ECMAScript format.

local rx = bson.regex("\\w*", "i")
-- or 
local rx = bson.regex("\\d+")

bson.code(code: string): code

Creates new Code from given string.
Code must be written in JavaScript

local code = bson.code("console.log()")

bson.minkey(): minkey

Creates new MinKey

local mnk = bson.minkey()

bson.maxkey(): maxkey

Creates new MaxKey

local mxk = bson.maxkey()

bson.decimal128(dcml: string): decimal128

Creates new Decimal128 from given string.
String must be a valid number

local dcml = bson.decimal128 "123"