New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

lite-model

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lite-model - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

2

dist/builder.js

@@ -123,3 +123,3 @@ "use strict";

var fieldStr = fields.join(',');
var placeholder = fields.map(function (_) { return '?'; }).join(',');
var placeholder = fields.map(function () { return '?'; }).join(',');
var sql = 'INSERT INTO `%s` (%s) VALUES (%s)';

@@ -126,0 +126,0 @@ var preSql = (0, printj_1.sprintf)(sql, this.tableName, fieldStr, placeholder);

@@ -16,3 +16,3 @@ import { Options } from 'better-sqlite3';

export interface ModelOpts {
dbFile: string;
dbFile?: string;
table?: string;

@@ -19,0 +19,0 @@ dbOptions?: Options;

@@ -8,5 +8,6 @@ import Sqlite from 'better-sqlite3';

protected _dbFile: string;
private _attributes;
private _changed;
private _pk;
readonly _options: ModelOpts;
_attributes: Dict;
constructor(options: ModelOpts);

@@ -16,3 +17,3 @@ get db(): Sqlite.Database;

attr(name: string, value: any): void;
clone<T extends Model>(instance: T): T;
clone(): Model;
instance(data: Dict): Model;

@@ -19,0 +20,0 @@ toObject(): Dict;

"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -25,2 +16,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

this._pk = [];
this._options = options;
this.initialize(options);

@@ -59,43 +51,22 @@ }

};
Model.prototype.clone = function (instance) {
return Object.assign(Object.create(Model.prototype), instance);
Model.prototype.clone = function () {
return new this.constructor(this._options);
};
Model.prototype.instance = function (data) {
this._changed = {};
this._attributes = {};
var instance = this.clone(this);
for (var key in data) {
var instance = this.clone();
var _loop_1 = function (key) {
instance.attr(key, data[key]);
}
var handler = {
constructor: function (target, args) {
return new (target.bind.apply(target, __spreadArray([void 0], args, false)))();
},
get: function (target, key) {
if (Reflect.has(target._changed, key)) {
return Reflect.get(target._changed, key);
Object.defineProperty(instance, key, {
set: function (value) {
instance.attr(key, value);
},
get: function () {
return instance._attributes[key];
}
if (Reflect.has(target._attributes, key)) {
return Reflect.get(target._attributes, key);
}
if (Reflect.has(instance, key)) {
return instance[key];
}
return undefined;
},
set: function (target, key, value) {
if (value instanceof Function) {
return Reflect.set(target, key, value);
}
if (Reflect.has(target._attributes, key) && !(0, ramda_1.equals)(Reflect.get(target._attributes, key), value)) {
Reflect.set(target._changed, key, value);
}
if (Reflect.has(instance, key)) {
return instance[key] = value;
}
return true;
}
});
};
var proxy = new Proxy(instance, handler);
return proxy;
for (var key in data) {
_loop_1(key);
}
return instance;
};

@@ -163,2 +134,3 @@ Model.prototype.toObject = function () {

var _b = builder.table(this._table).insert(data), sql = _b.sql, params = _b.params;
console.log(sql, params);
var lastInsertRowid = (_a = this.db.prepare(sql)).run.apply(_a, params).lastInsertRowid;

@@ -165,0 +137,0 @@ return this.findById(lastInsertRowid);

@@ -12,3 +12,4 @@ import { Dict } from './interface';

constructor();
genNode(entities: any, isChild?: boolean): void;
getNodeByKV(key: string, value: any): void;
genNode(entities: Dict, isChild?: boolean): void;
getDefaultNode(isChild: boolean): Node;

@@ -15,0 +16,0 @@ parse(entities: any): {

@@ -26,8 +26,22 @@ "use strict";

}
Parser.prototype.getNodeByKV = function (key, value) {
var _this = this;
Object.entries(value).map(function (_a) {
var _b;
var op = _a[0], v = _a[1];
var node = {
type: 'field',
name: key,
value: (_b = {}, _b[op] = v, _b),
isChild: true,
connector: ' AND '
};
_this.tree.push(node);
});
};
Parser.prototype.genNode = function (entities, isChild) {
var _this = this;
if (isChild === void 0) { isChild = false; }
Object.entries(entities).forEach(function (item) {
var key = item[0];
var v = item[1];
Object.entries(entities).forEach(function (_a) {
var key = _a[0], v = _a[1];
var value = v && typeof v === 'object' ? v : { $eq: v };

@@ -41,13 +55,3 @@ if (!(key in LOGICAL)) {

else {
Object.keys(value).map(function (op) {
var _a;
var node = {
type: 'field',
name: key,
value: (_a = {}, _a[op] = value[op], _a),
isChild: true,
connector: ' AND '
};
_this.tree.push(node);
});
_this.getNodeByKV(key, value);
}

@@ -54,0 +58,0 @@ }

@@ -6,5 +6,9 @@ import { Model, Schema } from '../lib';

_table = 'users';
constructor(options) {
super(options);
}
}
const model = new User({
dbFile, schema: { id: { type: 'string', pk: true } }
dbFile,
schema: { id: { type: 'string', pk: true } }
});

@@ -29,9 +33,4 @@ const res = model.exec(`CREATE TABLE IF NOT EXISTS users (

// model.insert({
// name: 'jerry',
// gender: 'female',
// age: 31,
// mail: 'jerry@world.cc'
// });
const user = model.findOne({ id: { $gte: 1 } }).toJSON();
const user = model.findOne({ id: { $gte: 1, $lte: 200 } }).toJSON();
console.log('uuuuuuuuuu', user);
const u2 = model.upsert({

@@ -43,3 +42,3 @@ id: user.id,

mail: 'tommy@hello.cc'
})
console.log(user, u2);
}) as any;
console.log(user.name, u2.age);

@@ -138,3 +138,3 @@ import { Parser } from "./parser";

const fieldStr = fields.join(',');
const placeholder = fields.map(_ => '?').join(',');
const placeholder = fields.map(() => '?').join(',');
const sql = 'INSERT INTO `%s` (%s) VALUES (%s)';

@@ -141,0 +141,0 @@ const preSql = sprintf(sql, this.tableName, fieldStr, placeholder);

@@ -0,1 +1,2 @@

export * from './model';

@@ -2,0 +3,0 @@ export * from './builder';

@@ -19,3 +19,3 @@ import { Options } from 'better-sqlite3';

export interface ModelOpts {
dbFile: string;
dbFile?: string;
table?: string;

@@ -22,0 +22,0 @@ dbOptions?: Options;

@@ -5,3 +5,3 @@ import Sqlite from 'better-sqlite3';

import DB from './db';
import { isEmpty, pick, has, equals } from 'ramda';
import { isEmpty, pick, has } from 'ramda';

@@ -13,5 +13,6 @@ export class Model {

protected _dbFile: string;
private _attributes: Dict;
private _changed: Dict;
private _pk: string[];
readonly _options: ModelOpts;
public _attributes: Dict;
constructor(options: ModelOpts) {

@@ -21,2 +22,3 @@ this._attributes = {};

this._pk = [];
this._options = options;
this.initialize(options);

@@ -29,2 +31,3 @@ }

initialize(config: ModelOpts): void {

@@ -56,44 +59,20 @@ if (this._db) {

clone<T extends Model>(instance: T): T {
return Object.assign(Object.create(Model.prototype), instance);
clone(): Model {
return new (this.constructor as new (options: ModelOpts) => this)(this._options as ModelOpts)
}
instance(data: Dict): Model {
this._changed = {};
this._attributes = {};
const instance = this.clone(this);
const instance = this.clone();
for (const key in data) {
instance.attr(key, data[key]);
Object.defineProperty(instance, key, {
set: (value: any) => {
instance.attr(key, value);
},
get: () => {
return instance._attributes[key];
}
});
}
const handler = {
constructor(target, args) {
return new target(...args);
},
get(target: any, key: string) {
if (Reflect.has(target._changed, key)) {
return Reflect.get(target._changed, key);
}
if (Reflect.has(target._attributes, key)) {
return Reflect.get(target._attributes, key);
}
if (Reflect.has(instance, key)) {
return instance[key];
}
return undefined;
},
set(target: any, key: string, value: any): boolean {
if (value instanceof Function) {
return Reflect.set(target, key, value);
}
if (Reflect.has(target._attributes, key) && !equals(Reflect.get(target._attributes, key), value)) {
Reflect.set(target._changed, key, value);
}
if (Reflect.has(instance, key)) {
return instance[key] = value;
}
return true;
}
};
const proxy = new Proxy(instance, handler);
return proxy;
return instance;
}

@@ -166,2 +145,3 @@

const { sql, params } = builder.table(this._table).insert(data);
console.log(sql, params);
const { lastInsertRowid } = this.db.prepare(sql).run(...params);

@@ -168,0 +148,0 @@ return this.findById(lastInsertRowid);

@@ -38,6 +38,17 @@ import { sprintf } from 'printj'

genNode(entities: any, isChild = false): void {
Object.entries(entities).forEach((item: any[]) => {
const key: string = item[0];
const v: any = item[1];
getNodeByKV(key: string, value: any): void {
Object.entries(value).map(([op, v]) => {
const node: Node = {
type: 'field',
name: key,
value: { [op]: v },
isChild: true,
connector: ' AND '
};
this.tree.push(node);
});
}
genNode(entities: Dict, isChild = false): void {
Object.entries(entities).forEach(([key, v]: [key: string, v: any]) => {
const value = v && typeof v === 'object' ? v : { $eq: v };

@@ -50,12 +61,3 @@ if (!(key in LOGICAL)) {

} else {
Object.keys(value).map(op => {
const node: Node = {
type: 'field',
name: key,
value: { [op]: value[op] },
isChild: true,
connector: ' AND '
};
this.tree.push(node);
});
this.getNodeByKV(key, value);
}

@@ -91,3 +93,3 @@ } else {

let level = 0;
const params: any[] = [];
const params = [];
this.tree.forEach((node: Node) => {

@@ -124,8 +126,8 @@ if (node.type === 'field') {

const connector = node.connector?.toUpperCase().substr(1);
const values: any[] = [];
const values = [];
Object.entries(node.value).forEach(element => {
const operator: string = element[0];
const value: any = element[1];
const value = element[1];
// const name = `$${node.name}`;
const temp: any[] = [field];
const temp = [field];
if (operator === '$inc') {

@@ -132,0 +134,0 @@ temp.push(this.increment(node.name, value));

{
"name": "lite-model",
"version": "0.0.4",
"version": "0.0.5",
"main": "dist/index.js",

@@ -5,0 +5,0 @@ "scripts": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc