
Company News
Andrew Becherer Joins Socket as Chief Information Security Officer
Socket’s first CISO brings deep experience securing high-growth SaaS companies as open source supply chain threats accelerate.
field-database
Advanced tools
fieldDB object modeling for node.js
npm i filed-database
const {FieldDocumentType, Model} = require('field-database')
const schema = {
messageText: {type: 'string', required: true},
date: {type: 'number', required: true},
note: {type: 'string', default: null},
deleted: {type: 'boolean', default: false},
likes: {type: 'object', default: []}
}
const Message = Model('message', schema)
export default Message
using TypeScript:
import {FieldDocumentType, Model} from 'field-database'
export type MessageType = FieldDocumentType & {
messageText: string
date: number
note: string | null
deleted: boolean
likes: Array<string>
}
const schema = {
messageText: {type: 'string', required: true},
date: {type: 'number', required: true},
note: {type: 'string', default: null},
deleted: {type: 'boolean', default: false},
likes: {type: 'object', default: []}
}
const Message = Model<MessageType>('message', schema)
export default Message
When we create a schema for the model, we may specify type, require and default parameters.
type may be:
type is required in schema, other are optional. Default value of required is false, for default is undefined
import {Express} from 'express'
const express = require('express')
import field from 'field-database'
const app: Express = express()
const PORT: number = 5000
async function start() {
try {
await field.connect({
login: 'test',
password: '111111',
projectId: 'Project ID'
})
console.log('FieldDB is connected')
app.listen(PORT, () => log.info(`Server has been started on port ${PORT}`))
} catch (e: any) {
console.log(`Server Error: ${e.message}`)
process.exit(1)
}
}
start()
import Message from '../models/Message'
const router: IRouter = Router()
router.put('/',
async (req, res) => {
try {
const {messageText} = req.body as {messageText: string}
const message = new Message({
messageText,
date: Date.now()
})
await message.save()
res.json({message})
} catch (e: any) {
log.error(e.message)
res.status(500).json({message: 'Something went wrong'})
}
}
)
If we try to look what object message is, we can see:
{
_id: 'some id',
_creationDate: 1637876142324,
_updatingDate: null,
messageText: 'some message text',
date: 1637876142324,
note: null,
deleted: false,
likes: []
}
await message.save()
// we can use this method without any parameters
// and get all objects from collection with model Message
const messages = await Message.find()
// as well as with object parameter named filter
// in this case we will get only objects according to filter value
const ownerMessages = await Message.find({ownerId: 'some id of owner'})
// filter can be compound
const ownerDeletedMessages = await Message.find({ownerId: 'some id of owner', deleted: true})
// we should to provide only one parameter: id
const message = await Message.findById('some message id')
// if database find corresponding object we get object, else: null
// but now we should to provide any filter instead of id
const message = await Message.findById({messageText: 'This is message'})
// in this case we get the first found object with field 'messageText' equal to 'This is message'
// or null if object not found
let newMessageText = 'this is new message text'
// if we need updated object
const message = await Message.findByIdAndUpdate('some message id', {messageText: newMessageText})
// also we can ignore promise payload
await Message.findByIdAndUpdate('some message id', {messageText: newMessageText})
await Message.findByIdAndDelete('some message id')
With model creation we should provide all required parameters without default values. Also we have to pass values of the correct types.
If we make the mistake, we get one of following error messages:
Property "messageText" is required on type Message
Property "messageLikes" does not exist on type Message
Property "messageText" should be "string" type but got "number"
FAQs
fieldDB object modeling for node.js
We found that field-database demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket’s first CISO brings deep experience securing high-growth SaaS companies as open source supply chain threats accelerate.

Company News
Replit is integrating Socket Firewall into its AI-powered development experience to help protect builders from malicious open source packages.

Security News
npm confirmed a tooling bug incorrectly marked several one-character packages as security holders and said it was working on a rollback.