【问题标题】:How to hardcode / prepopulate LokiJS Database如何硬编码/预填充 LokiJS 数据库
【发布时间】:2018-04-24 14:01:19
【问题描述】:

我刚开始使用 LokiJS,我有一个基本的问题我无法解决:

有没有办法对 LokiJS 数据库进行硬编码?还是我必须通过 Javascript 添加所有数据?

对我来说似乎有必要使用 PHPMyAdmin 之类的东西来检查/添加/删除数据库中的实际数据,但到目前为止我还没有发现使用 LokiJS 数据库来执行此操作。这不是可用性方面的一大损失吗?

【问题讨论】:

    标签: lokijs


    【解决方案1】:

    Lokijs.org

    适用于 node.js、浏览器和 cordova 的快速、内存中面向文档的数据存储。

    用例:

    • 在有限资源(类似Raspberry Pi)的设备上实现类似数据库的功能。
    • 无需运行服务器即可创建安全的应用程序。
    • 将数据持久化并重用,用于客户端的功能丰富的应用程序。
    • ..

    列表最多可以增加 100 分。

    可用性大幅下降?

    没有。

    因为它实现了引入它的目的。 (在内存数据存储中)

    有没有办法对 LokiJS 数据库进行硬编码?

    是的。

    创建 yourExampleDB.json 或 yourExampleDB.db 文件并按以下方式插入数据。

    {
    "filename": "yourExampleDB.db",
    "collections": [{
        "name": "entries",
        "data": [{
            "id": 17948697,
            "properties": { ... },
            "meta": {
                "revision": 0,
                "created": 1524651771378,
                "version": 0
            },
            "$loki": 1
        }, {
            "id": 17948705,
            "properties": { ... },
            "meta": {
                "revision": 0,
                "created": 1524651771378,
                "version": 0
            },
            "$loki": 2
        },
         ... 
         ...
         ...
        {
            "id": 11699810,
            "properties": { ... },
            "meta": {
                "revision": 0,
                "created": 1524651771402,
                "version": 0
            },
            "$loki": 11299
        }],
        "idIndex": [1, 2, ... 11298, 11299],
        "binaryIndices": {},
        "constraints": null,
        "uniqueNames": [],
        "transforms": {},
        "objType": "entries",
        "dirty": false,
        "cachedIndex": null,
        "cachedBinaryIndex": null,
        "cachedData": null,
        "adaptiveBinaryIndices": true,
        "transactional": false,
        "cloneObjects": false,
        "cloneMethod": "parse-stringify",
        "asyncListeners": false,
        "disableMeta": false,
        "disableChangesApi": true,
        "disableDeltaChangesApi": true,
        "autoupdate": false,
        "serializableIndices": true,
        "ttl": null,
        "maxId": 11299,
        "DynamicViews": [],
        "events": {
            "insert": [null],
            "update": [null],
            "pre-insert": [],
            "pre-update": [],
            "close": [],
            "flushbuffer": [],
            "error": [],
            "delete": [null],
            "warning": [null]
        },
        "changes": []
    }, {
        "name": "messages",
        "data": [{
            "txt": "I will only insert into this collection during databaseInitialize.",
            "meta": {
                "revision": 0,
                "created": 1524651771378,
                "version": 0
            },
            "$loki": 1
        }],
        "idIndex": [1],
        "binaryIndices": {},
        "constraints": null,
        "uniqueNames": [],
        "transforms": {},
        "objType": "messages",
        "dirty": false,
        "cachedIndex": null,
        "cachedBinaryIndex": null,
        "cachedData": null,
        "adaptiveBinaryIndices": true,
        "transactional": false,
        "cloneObjects": false,
        "cloneMethod": "parse-stringify",
        "asyncListeners": false,
        "disableMeta": false,
        "disableChangesApi": true,
        "disableDeltaChangesApi": true,
        "autoupdate": false,
        "serializableIndices": true,
        "ttl": null,
        "maxId": 1,
        "DynamicViews": [],
        "events": {
            "insert": [null],
            "update": [null],
            "pre-insert": [],
            "pre-update": [],
            "close": [],
            "flushbuffer": [],
            "error": [],
            "delete": [null],
            "warning": [null]
        },
        "changes": []
    }],
    "databaseVersion": 1.5,
    "engineVersion": 1.5,
    "autosave": false,
    "autosaveInterval": 5000,
    "autosaveHandle": null,
    "throttledSaves": true,
    "options": {
        "serializationMethod": "normal",
        "destructureDelimiter": "$<\n"
    },
    "persistenceMethod": "fs",
    "persistenceAdapter": null,
    "verbose": false,
    "events": {
        "init": [null],
        "loaded": [],
        "flushChanges": [],
        "close": [],
        "changes": [],
        "warning": []
    },
    "ENV": "NODEJS"
    

    }

    这是 lokijs db 的有效示例副本。

    类似 PHPMyAdmin 的东西来检查/添加/删除?

    没有。

    或者我是否已经通过 JavaScript 添加所有数据?

    是的

    要遵循的步骤

    • 需要 lokijs 模块。
    • 创建数据库。
    • 创建一个集合。
    • 插入您的数据。
    • 坚持下去(手动/自动)

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-26
      相关资源
      最近更新 更多