发布于 2026-01-06 13 阅读
0

最新消息!全新持久化 NoSQL 数据库(仅 18 KiB!)欢迎使用 Trin.DB!加入我们的社区 Web 和移动开发者 PH [ Facebook 主页 | 群组 ],支持我们!

最新消息!全新持久化 NoSQL 数据库(仅 18 KiB!)

欢迎来到Trin.DB!

加入并支持我们的社区
Web 和移动开发者 PH
[ Facebook 页面|群组]

欢迎来到Trin.DB!

快速的 RESTful 持久化或内存 NoSQL 数据库 (18 KiB only!)

Github 仓库:https://github.com/trinly01/TrinDB

安装

npm install trin.db
Enter fullscreen mode Exit fullscreen mode

或者

yarn add trin.db
Enter fullscreen mode Exit fullscreen mode

用法

const express = require('express')
const app = express()
const port = process.env.PORT || 3000
const trinDB = require('trin.db')

app.use(express.json())               // required for RESTful APIs

app.listen(port, async () => {
  app.trinDB = {
    todos: await trinDB({             // Todos Service
      filename: 'trinDb/todos.db',    // get records from a file
      inMemoryOnly: false,            // Optional
      restful                         // Optional
    })
  }
})

// Other Options

const restful = {                     // Optional
  app,                                // express app
  url: '/todos',                      // API end-point
  hooks                               // Optional
}

const hooks = ({app, service}) => ({  // Hooks Example
  before: {
    all: [
      (req, res, next) => {
        console.log('before all hook')
        next()
      }
    ],
    get: [],
    find: [],
    create: [],
    patch: [],
    remove: []
  }
  after: {
    all: [
      (result) => {
        console.log(result)
        return result
      }
    ],
  }
})
Enter fullscreen mode Exit fullscreen mode

await trinDB( <object>)

返回 trinDB 服务

对象属性 类型 描述 默认 模式
文件名 <string> 文件路径。如果处于持久模式,则为必填项。 不适用 执着的
仅内存 <boolean> (可选)如果启用true,数据库将处于非持久模式。 false 内存
宁静 <object> ( 选修的 ){ app, url, hooks } 不适用 执着的

service.create( <object>)

返回已创建的内容<object>

/* example existing records (service.data)
  {
    asd: { _id: 'asd', text: 'Hello', read: true, nested: { prop: 'xander' } },
    zxc: { _id: 'zxc', text: 'World', read: false, nested: { prop: 'ford' } }
  }
*/

const result = service.create({
  text: 'Trinmar Pogi'
})

console.log(result)
// { _id: 'qwe', text: 'Trinmar Pogi' }

console.log(service.data)
/* service.data with the newly created object
  {
    asd: { _id: 'asd', text: 'Hello', read: true, nested: { prop: 'xander' } },
    zxc: { _id: 'zxc', text: 'World', read: false, nested: { prop: 'ford' } },
    qwe: { _id: 'qwe', text: 'Trinmar Pogi' }
  }
*/
Enter fullscreen mode Exit fullscreen mode

RESTful API

curl --location --request POST 'http://localhost:3000/todos' \
--header 'Content-Type: application/json' \
--data-raw '{
  "text": "Trinmar Pogi"
}'
Enter fullscreen mode Exit fullscreen mode

service.find( <object>)

返回找到的数据<object>

/* example existing records (service.data)
  {
    asd: { _id: 'asd', firstName: 'Trinmar', lastName: 'Pogi', age: 20 },
    zxc: { _id: 'zxc', firstName: 'Trinly Zion', lastName: 'Boado', age: 1 },
    qwe: { _id: 'qwe', firstName: 'Lovely', lastName: 'Boado', age: 18 }
  }
*/

// Equality
result = service.find({
  query: {
    lastName: 'Pogi' // equality
  },
  limit: 10, // default 10
  skip: 0 // default 0
})
console.log(result)
/*
  {
    total: 1,
    limit: 10,
    skip: 0,
    data: {
      asd: { _id: 'asd', firstName: 'Trinmar', lastName: 'Pogi', age: 20 }
    }
  }
*/
Enter fullscreen mode Exit fullscreen mode

RESTful API

curl --location --request GET 'http://localhost:3000/todos?lastName=Pogi&$limit=10&$skip=0'
Enter fullscreen mode Exit fullscreen mode

复杂查询(条件 >, >==, <, <==, &&, || )

// Map data or select specific props
result = service.find({
  query (obj) {
    return ob.age < 20
  },
  map (obj) {
    return {
      fullName: obj.firstName + ' '+  obj.lastName
    }
  }
})
console.log(result)
/*
  {
    total: 2,
    limit: 10,
    skip: 0,
    data: {
      zxc: { _id: 'zxc', firstName: 'Trinly Zion Boado' },
      qwe: { _id: 'qwe', firstName: 'Lovely Boado' }
    }
  }
*/
Enter fullscreen mode Exit fullscreen mode

service.search( keywords)

模糊搜索根据关键词(<String>)查找数据,并按排序方式返回数据。_score

/* example existing records (service.data)
  {
    asd: { _id: 'asd', firstName: 'Trinmar', lastName: 'Boado' },
    zxc: { _id: 'zxc', firstName: 'Trinly Zion', lastName: 'Boado' },
    qwe: { _id: 'qwe', firstName: 'Lovely', lastName: 'Boado' }
  }
*/

result = service.search('ly oad')

console.log(result)
/*
  {
    total: 3,
    data: {
      qwe: { _score: 2, _id: 'qwe', firstName: 'Lovely', lastName: 'Boado', age: 18 },
      zxc: { _score: 2, _id: 'zxc', firstName: 'Trinly Zion', lastName: 'Boado', age: 1 },
      asd: { _score: 1, _id: 'asd', firstName: 'Trinmar', lastName: 'Pogi', age: 20 },
    }
  }
*/
Enter fullscreen mode Exit fullscreen mode

RESTful API

curl --location --request GET 'http://localhost:3000/todos?$search=ly%20oad'
Enter fullscreen mode Exit fullscreen mode

service.patch(_id, <object>)

返回已创建的内容<object>

// { _id: 'q12m3k', firstName: 'Trinmar', lastName: 'Boado' nested: { counter: 123 } }

const result = service.patch('q12m3k', {
  lastName: 'Pogi',
  children: ['Trinly Zion'],
  'nested.counter': 456
})

console.log(result)
// { _id: 'q12m3k', lastName: 'Pogi' children: ['Trinly Zion'], 'nested.counter': 456 }

console.log(service.data['q12m3k'])
// { _id: 'q12m3k', firstName: 'Trinmar', lastName: 'Pogi', nested: { prop: 456 }, children: ['Trinly Zion'] }
Enter fullscreen mode Exit fullscreen mode

RESTful API

curl --location --request PATCH 'http://localhost:3000/todos/:_id' \
--header 'Content-Type: application/json' \
--data-raw '{
    "lastName": "Pogi",
    "children": ["Trinly Zion"],
    "nested.counter": 456
}'
Enter fullscreen mode Exit fullscreen mode

service.remove(_id)

返回已移除的内容<object>

service.remove('q12m3k')

console.log(service.data['q12m3k'])
// undefined
Enter fullscreen mode Exit fullscreen mode

RESTful API

curl --location --request DELETE 'http://localhost:3000/todos/:_id'
Enter fullscreen mode Exit fullscreen mode

service.removeProps(_id, <object>)

返回已移除的<object>属性。

// { _id: 'q12m3k', firstName: 'Trinmar', lastName: 'Pogi', nested: { prop: 456 }, children: ['Trinly Zion'] }

service.removeProps('q12m3k', {
  lastName: true,
  'nested.prop': true
  firstName: false
})

console.log(service.data['q12m3k'])
// { _id: 'q12m3k', firstName: 'Trinmar', children: ['Trinly Zion'] }
Enter fullscreen mode Exit fullscreen mode

RESTful API

curl --location --request PATCH 'http://localhost:3000/todos/:_id' \
--header 'Content-Type: application/json' \
--data-raw '{
    "$action": "removeProps"
    "lastName": true,
    "nested.prop": true,
    "firstName": false
}'
Enter fullscreen mode Exit fullscreen mode

service.inc(_id, <object>)

递增特定属性并返回<object>

// { _id: 'q12m3k', firstName: 'Trinmar', lastName: 'Pogi', nested: { prop: 456 }, children: ['Trinly Zion'] }

service.inc('q12m3k', {
  'nested.prop': 5
})

console.log(service.data['q12m3k'])
// { _id: 'q12m3k', firstName: 'Trinmar', lastName: 'Pogi', nested: { prop: 461 }, children: ['Trinly Zion'] }
Enter fullscreen mode Exit fullscreen mode

RESTful API

curl --location --request PATCH 'http://localhost:3000/todos/:_id' \
--header 'Content-Type: application/json' \
--data-raw '{
    "$action": "inc"
    "nested.prop": 5
}'
Enter fullscreen mode Exit fullscreen mode

service.splice(_id, <object>)

根据索引删除元素并返回结果。<object>

// { _id: 'q12m3k', children: ['Trinly Zion', 'Trinmar Boado'] }

service.splice('q12m3k', {
  'children': 1
})

console.log(service.data['q12m3k'])
// { _id: 'q12m3k', children: ['Trinly Zion'] }
Enter fullscreen mode Exit fullscreen mode

RESTful API

curl --location --request PATCH 'http://localhost:3000/todos/:_id' \
--header 'Content-Type: application/json' \
--data-raw '{
    "$action": "splice"
    "children": 1
}'
Enter fullscreen mode Exit fullscreen mode

service.push(_id, <object>)

向数组末尾添加一个或多个元素并返回结果。<object>

// { _id: 'q12m3k', children: ['Trinly Zion', 'Trinmar Boado'] }

service.push('q12m3k', {
  'children': 'Lovely Boado'
})

console.log(service.data['q12m3k'])
// { _id: 'q12m3k', children: ['Trinly Zion', 'Trinmar Boado', 'Lovely Boado'] }
Enter fullscreen mode Exit fullscreen mode

RESTful API

curl --location --request PATCH 'http://localhost:3000/todos/:_id' \
--header 'Content-Type: application/json' \
--data-raw '{
    "$action": "push"
    "children": "Lovely Boado'"
}'
Enter fullscreen mode Exit fullscreen mode

service.unshift(_id, <object>)

向字符串开头添加一个或多个元素array并返回该字符串<object>

// { _id: 'q12m3k', children: ['Trinly Zion', 'Trinmar Boado'] }

service.unshift('q12m3k', {
  'children': 'Lovely Boado'
})

console.log(service.data['q12m3k'])
// { _id: 'q12m3k', children: ['Lovely Boado', 'Trinly Zion', 'Trinmar Boado'] }
Enter fullscreen mode Exit fullscreen mode

RESTful API

curl --location --request PATCH 'http://localhost:3000/todos/:_id' \
--header 'Content-Type: application/json' \
--data-raw '{
    "$action": "unshift"
    "children": "Lovely Boado'"
}'
Enter fullscreen mode Exit fullscreen mode

service.sort(data, <object>)

根据排序规则对数据进行排序<object>,并返回排序后的数据。

/* example existing records (service.data)
  {
    asd: { _id: 'asd', firstName: 'Trinmar', lastName: 'Pogi', age: 20 },
    zxc: { _id: 'zxc', firstName: 'Trinly Zion', lastName: 'Boado', age: 1 },
    qwe: { _id: 'qwe', firstName: 'Lovely', lastName: 'Boado', age: 18 }
  }
*/

// Descending (-1)
result = service.sort({
  data: service.data, // (Optional) if not defined, service.data will be used
  params: {
    age: -1
  }
})

console.log(result)
/*
  {
    asd: { _id: 'asd', firstName: 'Trinmar', lastName: 'Pogi', age: 20 },
    qwe: { _id: 'qwe', firstName: 'Lovely', lastName: 'Boado', age: 18 },
    zxc: { _id: 'zxc', firstName: 'Trinly Zion', lastName: 'Boado', age: 1 }
  }
*/
Enter fullscreen mode Exit fullscreen mode

service.copmact(filename, <object>)

将压缩数据写入文件
| 参数 | 类型 | 描述 | 默认值 |
|--|--|--|--|
|文件名| <string>| (可选)文件路径 | 当前 |
| | <object>| (可选)TrinDB 对象 | service.data|

service.copmact('test.db', service.data)
Enter fullscreen mode Exit fullscreen mode

Github 仓库:https://github.com/trinly01/TrinDB

加入并支持我们的社区
Web 和移动开发者 PH
[ Facebook 页面|群组]

文章来源:https://dev.to/trinly01/just-in-a-new-persistent-nosql-database-18-kib-only-14a7