Cơ sở dữ liệu JavaScript đơn giản

Giữ nội dung db của bạn. Nếu bạn đang sử dụng bộ điều hợp đi kèm với lowdb, nó có thể là bất kỳ loại nào được hỗ trợ bởi

// Remember to set type: module in package.json or use .mjs extension
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

import { Low } from 'lowdb'
import { JSONFile } from 'lowdb/node'

// File path
const __dirname = dirname(fileURLToPath(import.meta.url));
const file = join(__dirname, 'db.json')

// Configure lowdb to write to JSONFile
const adapter = new JSONFile(file)
const db = new Low(adapter)

// Read data from JSON file, this will set db.data content
await db.read()

// If db.json doesn't exist, db.data will be null
// Use the code below to set default data
// db.data = db.data || { posts: [] } // For Node < v15.x
db.data ||= { posts: [] }             // For Node >= 15.x

// Create and query items using native JS API
db.data.posts.push('hello world')
const firstPost = db.data.posts[0]

// Alternatively, you can also use this syntax if you prefer
const { posts } = db.data
posts.push('hello world')

// Finally write db.data content to file
await db.write()
2

Ví dụ

// db.json
{
  "posts": [
    { "id": 1, "title": "lowdb is awesome" }
  ]
}
1

bộ điều hợp

Bộ điều hợp Lowdb

npm install lowdb
5
npm install lowdb
6

Bộ điều hợp để đọc và ghi tệp JSON

// db.json
{
  "posts": [
    { "id": 1, "title": "lowdb is awesome" }
  ]
}
2

// Remember to set type: module in package.json or use .mjs extension
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

import { Low } from 'lowdb'
import { JSONFile } from 'lowdb/node'

// File path
const __dirname = dirname(fileURLToPath(import.meta.url));
const file = join(__dirname, 'db.json')

// Configure lowdb to write to JSONFile
const adapter = new JSONFile(file)
const db = new Low(adapter)

// Read data from JSON file, this will set db.data content
await db.read()

// If db.json doesn't exist, db.data will be null
// Use the code below to set default data
// db.data = db.data || { posts: [] } // For Node < v15.x
db.data ||= { posts: [] }             // For Node >= 15.x

// Create and query items using native JS API
db.data.posts.push('hello world')
const firstPost = db.data.posts[0]

// Alternatively, you can also use this syntax if you prefer
const { posts } = db.data
posts.push('hello world')

// Finally write db.data content to file
await db.write()
5
// Remember to set type: module in package.json or use .mjs extension
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

import { Low } from 'lowdb'
import { JSONFile } from 'lowdb/node'

// File path
const __dirname = dirname(fileURLToPath(import.meta.url));
const file = join(__dirname, 'db.json')

// Configure lowdb to write to JSONFile
const adapter = new JSONFile(file)
const db = new Low(adapter)

// Read data from JSON file, this will set db.data content
await db.read()

// If db.json doesn't exist, db.data will be null
// Use the code below to set default data
// db.data = db.data || { posts: [] } // For Node < v15.x
db.data ||= { posts: [] }             // For Node >= 15.x

// Create and query items using native JS API
db.data.posts.push('hello world')
const firstPost = db.data.posts[0]

// Alternatively, you can also use this syntax if you prefer
const { posts } = db.data
posts.push('hello world')

// Finally write db.data content to file
await db.write()
6

Bộ điều hợp trong bộ nhớ. Hữu ích để tăng tốc kiểm tra đơn vị. Xem thư mục

// db.json
{
  "posts": [
    { "id": 1, "title": "lowdb is awesome" }
  ]
}
9

// db.json
{
  "posts": [
    { "id": 1, "title": "lowdb is awesome" }
  ]
}
3

// Remember to set type: module in package.json or use .mjs extension
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

import { Low } from 'lowdb'
import { JSONFile } from 'lowdb/node'

// File path
const __dirname = dirname(fileURLToPath(import.meta.url));
const file = join(__dirname, 'db.json')

// Configure lowdb to write to JSONFile
const adapter = new JSONFile(file)
const db = new Low(adapter)

// Read data from JSON file, this will set db.data content
await db.read()

// If db.json doesn't exist, db.data will be null
// Use the code below to set default data
// db.data = db.data || { posts: [] } // For Node < v15.x
db.data ||= { posts: [] }             // For Node >= 15.x

// Create and query items using native JS API
db.data.posts.push('hello world')
const firstPost = db.data.posts[0]

// Alternatively, you can also use this syntax if you prefer
const { posts } = db.data
posts.push('hello world')

// Finally write db.data content to file
await db.write()
8

Bộ điều hợp đồng bộ cho

// Remember to set type: module in package.json or use .mjs extension
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

import { Low } from 'lowdb'
import { JSONFile } from 'lowdb/node'

// File path
const __dirname = dirname(fileURLToPath(import.meta.url));
const file = join(__dirname, 'db.json')

// Configure lowdb to write to JSONFile
const adapter = new JSONFile(file)
const db = new Low(adapter)

// Read data from JSON file, this will set db.data content
await db.read()

// If db.json doesn't exist, db.data will be null
// Use the code below to set default data
// db.data = db.data || { posts: [] } // For Node < v15.x
db.data ||= { posts: [] }             // For Node >= 15.x

// Create and query items using native JS API
db.data.posts.push('hello world')
const firstPost = db.data.posts[0]

// Alternatively, you can also use this syntax if you prefer
const { posts } = db.data
posts.push('hello world')

// Finally write db.data content to file
await db.write()
9

// db.json
{
  "posts": [
    { "id": 1, "title": "lowdb is awesome" }
  ]
}
4

// db.json
{
  "posts": [ "hello world" ]
}
0
// db.json
{
  "posts": [ "hello world" ]
}
1

Bộ điều hợp để đọc và viết văn bản. Hữu ích để tạo bộ điều hợp tùy chỉnh

Bộ điều hợp của bên thứ ba

Nếu bạn đã xuất bản bộ điều hợp cho lowdb, vui lòng tạo PR để thêm nó vào đây

Viết bộ điều hợp của riêng bạn

Bạn có thể muốn tạo một bộ điều hợp để ghi

npm install lowdb
4 vào YAML, XML, mã hóa dữ liệu, bộ lưu trữ từ xa,

Bộ điều hợp là một lớp đơn giản chỉ cần hiển thị hai phương thức

// db.json
{
  "posts": [
    { "id": 1, "title": "lowdb is awesome" }
  ]
}
5

Ví dụ: giả sử bạn có một số bộ nhớ không đồng bộ và muốn tạo bộ điều hợp cho bộ nhớ đó

// db.json
{
  "posts": [
    { "id": 1, "title": "lowdb is awesome" }
  ]
}
6

Xem

// db.json
{
  "posts": [ "hello world" ]
}
3 để biết thêm ví dụ

Tuần tự hóa tùy chỉnh

Để tạo bộ điều hợp cho định dạng khác ngoài JSON, bạn có thể sử dụng

// db.json
{
  "posts": [ "hello world" ]
}
0 hoặc
// db.json
{
  "posts": [ "hello world" ]
}
1

Ví dụ

// db.json
{
  "posts": [
    { "id": 1, "title": "lowdb is awesome" }
  ]
}
7

Hạn mức

Lowdb không hỗ trợ mô-đun cụm của Node

Nếu bạn có các đối tượng JavaScript lớn (

// db.json
{
  "posts": [ "hello world" ]
}
6), bạn có thể gặp phải một số vấn đề về hiệu suất. Điều này là do bất cứ khi nào bạn gọi
// db.json
{
  "posts": [ "hello world" ]
}
7, toàn bộ
npm install lowdb
4 được đánh số thứ tự bằng cách sử dụng
// Remember to set type: module in package.json or use .mjs extension
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

import { Low } from 'lowdb'
import { JSONFile } from 'lowdb/node'

// File path
const __dirname = dirname(fileURLToPath(import.meta.url));
const file = join(__dirname, 'db.json')

// Configure lowdb to write to JSONFile
const adapter = new JSONFile(file)
const db = new Low(adapter)

// Read data from JSON file, this will set db.data content
await db.read()

// If db.json doesn't exist, db.data will be null
// Use the code below to set default data
// db.data = db.data || { posts: [] } // For Node < v15.x
db.data ||= { posts: [] }             // For Node >= 15.x

// Create and query items using native JS API
db.data.posts.push('hello world')
const firstPost = db.data.posts[0]

// Alternatively, you can also use this syntax if you prefer
const { posts } = db.data
posts.push('hello world')

// Finally write db.data content to file
await db.write()
2 và được ghi vào bộ lưu trữ

Tùy thuộc vào trường hợp sử dụng của bạn, điều này có thể tốt hoặc không. Nó có thể được giảm thiểu bằng cách thực hiện các hoạt động hàng loạt và chỉ gọi

// db.json
{
  "posts": [ "hello world" ]
}
7 khi bạn cần

Tôi có thể tạo cơ sở dữ liệu bằng JavaScript không?

Câu lệnh CREATE DATABASE dùng để tạo cơ sở dữ liệu trong MySQL . Để tạo cơ sở dữ liệu có tên "javatpoint".

Cơ sở dữ liệu nào hoạt động tốt nhất với JavaScript?

js hỗ trợ tất cả các loại cơ sở dữ liệu bất kể đó là cơ sở dữ liệu quan hệ hay cơ sở dữ liệu NoSQL. Tuy nhiên, Cơ sở dữ liệu NoSQL như MongoDb phù hợp nhất với Node. js.

Cơ sở dữ liệu đơn giản nhất là gì?

Hệ thống tệp là cơ sở dữ liệu đơn giản nhất .
khả năng lưu trữ dữ liệu liên tục đáng tin cậy - gọi fsync sẽ đảm bảo rằng tệp được ghi vào ổ cứng và ổ cứng là thứ chúng ta nghĩ đến khi nói về lưu trữ liên tục
khả năng truy xuất dữ liệu một cách đáng tin cậy - Bạn có thể đọc nội dung của tệp

JavaScript có thể xử lý cơ sở dữ liệu không?

JavaScript là ngôn ngữ phía máy khách chạy trong trình duyệt và MySQL là công nghệ phía máy chủ chạy trên máy chủ. Bạn cần sử dụng ngôn ngữ phía máy chủ Node. js để kết nối với Cơ sở dữ liệu MySQL