@minni-im/tape-recorder

5.0.0-beta.4 • Public • Published

@minni-im/tape-recorder

a tiny couchdb orm on top of nano

why?

nano is already great. But manipulating Documents and Models classes is better sometimes.

Install

$ npm install --save @minni-im/tape-recorder

Usage

Basic usage

import recorder from "@minni-im/tape-recorder";

const { registerModel } = recorder("http://localhost:5984/<dbname>");

const User = registerModel("User", {
	firstName: String,
	lastName: String,
});

const john = new User({
	firtName: "John",
	lastName: "Doe",
});
await john.save();

Schema definitions

import recorder, { Schema } from "@minni-im/tape-recorder";

const { registerModel } = recorder("http://localhost:5984/<dbname>");

const UserSchema = new Schema({
	firstName: String,
	lastName: String,
	email: {
		type: String,
		view: true, // will create a couchdb view named "by_email"
	},
});

UserSchema.virtual("fullName", {
	get() {
		return this.firstName + " " + this.lastName;
	},
});

const User = registerModel("User", UserSchema);

CRUD usage

import recorder, { Schema } from "@minni-im/tape-recorder";

const { registerModel } = recorder("http://localhost:5984/<dbname>");

// Schema
const UserSchema = new Schema({
	firstName: String,
	lastName: String,
});
const User = registerModel("User", UserSchema);

// Create
const user = new User({
	firstName: "John",
	lastName: "Doe",
});
await user.save();

// or
const user = await User.create({
	firstName: "John",
	lastName: "Doe",
});

// Update
user.firstName = "Jane";
await user.save();

// Delete
await user.delete();

// Read
const users = await User.findAll();
const user = await User.findById(userId);

Retrieving models by name

import recorder, { Schema } from "@minni-im/tape-recorder";

const { registerModel, model } = recorder("http://localhost:5984/<dbname>");

const User = registerModel("User", {
	firstName: String,
	lastName: String,
});

User === model("User");

Authentication

To connect using authentication, you can provide any nano supported options to the recorder() method.

import recorder from "@minni-im/tape-recorder";

const { registerModel, model } = recorder({
	url: "http:.//localhost:5984/<dbName>",
	requestDefaults: {
		auth: {
			username: process.env.COUCH_USER,
			password: process.env.COUCH_PASSWORD,
		},
	},
});

license

copyright 2022 Benoit Charbonnier

licensed under the apache license, version 2.0 (the "license"); you may not use this file except in compliance with the license. you may obtain a copy of the license at

http://www.apache.org/licenses/LICENSE-2.0.html unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "as is" basis, without warranties or conditions of any kind, either express or implied. see the license for the specific language governing permissions and limitations under the license.

Package Sidebar

Install

npm i @minni-im/tape-recorder

Weekly Downloads

10

Version

5.0.0-beta.4

License

Apache-2.0

Unpacked Size

19.5 kB

Total Files

3

Last publish

Collaborators

  • bcharbonnier