tauri-plugin-mongoose 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,3 +1,62 @@
1
- declare function connect(url: string): Promise<void>;
1
+ declare class Time {
2
+ hours: number;
3
+ minutes: number;
4
+ seconds: number;
5
+ milliseconds: number;
6
+ constructor(hours: number, minutes: number, seconds: number, milliseconds: number);
7
+ toString(_12_hour?: boolean, format?: {
8
+ hours?: string;
9
+ minutes?: string;
10
+ seconds?: string;
11
+ milliseconds?: string;
12
+ }): string;
13
+ static fromString(timeString: string): Time;
14
+ getTime(): Date;
15
+ static fromDate(date: Date): Time;
16
+ }
2
17
 
3
- export { connect };
18
+ type SchemaTypes = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'date' | 'time';
19
+ interface SchemaItem {
20
+ type: SchemaTypes;
21
+ default?: Function;
22
+ required?: boolean;
23
+ unique?: boolean;
24
+ ref?: string;
25
+ }
26
+ interface Schema {
27
+ [key: string]: SchemaItem;
28
+ }
29
+ type TypeMap = {
30
+ string: string;
31
+ number: number;
32
+ boolean: boolean;
33
+ date: Date;
34
+ time: Time;
35
+ object: object;
36
+ array: any[];
37
+ buffer: Uint8Array;
38
+ mixed: any;
39
+ };
40
+ type InferSchemaType<T extends Schema> = {
41
+ [K in keyof T]?: T[K]['required'] extends true ? TypeMap[T[K]['type']] : TypeMap[T[K]['type']] | undefined;
42
+ } & {
43
+ [K in keyof T as T[K]['required'] extends true ? K : never]-?: TypeMap[T[K]['type']];
44
+ };
45
+
46
+ declare class Model {
47
+ name: string;
48
+ schema: Schema;
49
+ constructor(name: string, schema: Schema);
50
+ getById(id: string): Promise<InferSchemaType<typeof this.schema>>;
51
+ validate(doc: InferSchemaType<typeof this.schema>): Promise<InferSchemaType<Schema>>;
52
+ create(doc: InferSchemaType<typeof this.schema>): Promise<InferSchemaType<typeof this.schema>>;
53
+ }
54
+
55
+ interface ConnectOptions {
56
+ url: string;
57
+ dbName?: string;
58
+ }
59
+ declare function connect(options: ConnectOptions): Promise<void>;
60
+ declare function connect(url: string, dbName?: string): Promise<void>;
61
+
62
+ export { type ConnectOptions, type InferSchemaType, Model, type Schema, type SchemaItem, type SchemaTypes, type TypeMap, connect };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,62 @@
1
- declare function connect(url: string): Promise<void>;
1
+ declare class Time {
2
+ hours: number;
3
+ minutes: number;
4
+ seconds: number;
5
+ milliseconds: number;
6
+ constructor(hours: number, minutes: number, seconds: number, milliseconds: number);
7
+ toString(_12_hour?: boolean, format?: {
8
+ hours?: string;
9
+ minutes?: string;
10
+ seconds?: string;
11
+ milliseconds?: string;
12
+ }): string;
13
+ static fromString(timeString: string): Time;
14
+ getTime(): Date;
15
+ static fromDate(date: Date): Time;
16
+ }
2
17
 
3
- export { connect };
18
+ type SchemaTypes = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'date' | 'time';
19
+ interface SchemaItem {
20
+ type: SchemaTypes;
21
+ default?: Function;
22
+ required?: boolean;
23
+ unique?: boolean;
24
+ ref?: string;
25
+ }
26
+ interface Schema {
27
+ [key: string]: SchemaItem;
28
+ }
29
+ type TypeMap = {
30
+ string: string;
31
+ number: number;
32
+ boolean: boolean;
33
+ date: Date;
34
+ time: Time;
35
+ object: object;
36
+ array: any[];
37
+ buffer: Uint8Array;
38
+ mixed: any;
39
+ };
40
+ type InferSchemaType<T extends Schema> = {
41
+ [K in keyof T]?: T[K]['required'] extends true ? TypeMap[T[K]['type']] : TypeMap[T[K]['type']] | undefined;
42
+ } & {
43
+ [K in keyof T as T[K]['required'] extends true ? K : never]-?: TypeMap[T[K]['type']];
44
+ };
45
+
46
+ declare class Model {
47
+ name: string;
48
+ schema: Schema;
49
+ constructor(name: string, schema: Schema);
50
+ getById(id: string): Promise<InferSchemaType<typeof this.schema>>;
51
+ validate(doc: InferSchemaType<typeof this.schema>): Promise<InferSchemaType<Schema>>;
52
+ create(doc: InferSchemaType<typeof this.schema>): Promise<InferSchemaType<typeof this.schema>>;
53
+ }
54
+
55
+ interface ConnectOptions {
56
+ url: string;
57
+ dbName?: string;
58
+ }
59
+ declare function connect(options: ConnectOptions): Promise<void>;
60
+ declare function connect(url: string, dbName?: string): Promise<void>;
61
+
62
+ export { type ConnectOptions, type InferSchemaType, Model, type Schema, type SchemaItem, type SchemaTypes, type TypeMap, connect };
package/dist/index.js CHANGED
@@ -20,16 +20,137 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ Model: () => Model,
23
24
  connect: () => connect
24
25
  });
25
26
  module.exports = __toCommonJS(index_exports);
27
+ var import_core2 = require("@tauri-apps/api/core");
28
+
29
+ // model/index.ts
26
30
  var import_core = require("@tauri-apps/api/core");
27
- async function connect(url) {
28
- await (0, import_core.invoke)("plugin:mongoose|connect", {
29
- url
30
- });
31
+
32
+ // types/time.type.ts
33
+ var isTime = (obj) => {
34
+ return obj && typeof obj.hours === "number" && typeof obj.minutes === "number" && typeof obj.seconds === "number" && typeof obj.milliseconds === "number";
35
+ };
36
+ var Time = class _Time {
37
+ constructor(hours, minutes, seconds, milliseconds) {
38
+ this.hours = hours;
39
+ this.minutes = minutes;
40
+ this.seconds = seconds;
41
+ this.milliseconds = milliseconds;
42
+ }
43
+ toString(_12_hour = false, format = { hours: "2-digit", minutes: "2-digit" }) {
44
+ const pad = (num, size = 2) => num.toString().padStart(size, "0");
45
+ let h = this.hours;
46
+ let am_pm = "";
47
+ if (_12_hour) {
48
+ am_pm = h >= 12 ? " PM" : " AM";
49
+ h = h % 12 || 12;
50
+ }
51
+ let result = `${pad(h)}:${pad(this.minutes)}`;
52
+ if (format.seconds) {
53
+ result += `:${pad(this.seconds)}`;
54
+ }
55
+ if (format.milliseconds) {
56
+ result += `.${pad(this.milliseconds, 3)}`;
57
+ }
58
+ return result + am_pm;
59
+ }
60
+ static fromString(timeString) {
61
+ const [time, am_pm] = timeString.split(" ");
62
+ const [hours, minutes, seconds] = time.split(":").map(Number);
63
+ const milliseconds = 0;
64
+ return new _Time(hours, minutes, seconds, milliseconds);
65
+ }
66
+ getTime() {
67
+ return new Date(0, 0, 0, this.hours, this.minutes, this.seconds, this.milliseconds);
68
+ }
69
+ static fromDate(date) {
70
+ return new _Time(date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
71
+ }
72
+ };
73
+
74
+ // model/index.ts
75
+ var Model = class {
76
+ constructor(name, schema) {
77
+ this.name = name;
78
+ this.schema = schema;
79
+ }
80
+ async getById(id) {
81
+ const doc = await (0, import_core.invoke)("plugin:mongoose|get_by_id", {
82
+ collection: this.name,
83
+ id
84
+ });
85
+ return this.validate(doc);
86
+ }
87
+ async validate(doc) {
88
+ for (const key in this.schema) {
89
+ const schemaItem = this.schema[key];
90
+ const value = doc[key];
91
+ if (schemaItem.required && (value === void 0 || value === null || value === "")) {
92
+ throw new Error(`Property ${key} is required`);
93
+ }
94
+ if (value !== void 0 && value !== null) {
95
+ if (schemaItem.type === "string" && typeof value !== "string") {
96
+ throw new Error(`Property ${key} must be a string`);
97
+ }
98
+ if (schemaItem.type === "number" && typeof value !== "number") {
99
+ throw new Error(`Property ${key} must be a number`);
100
+ }
101
+ if (schemaItem.type === "boolean" && typeof value !== "boolean") {
102
+ throw new Error(`Property ${key} must be a boolean`);
103
+ }
104
+ if (schemaItem.type === "object" && typeof value !== "object") {
105
+ throw new Error(`Property ${key} must be an object`);
106
+ }
107
+ if (schemaItem.type === "array" && !Array.isArray(value)) {
108
+ throw new Error(`Property ${key} must be an array`);
109
+ }
110
+ if (schemaItem.type === "date" && !(value instanceof Date)) {
111
+ throw new Error(`Property ${key} must be a date`);
112
+ }
113
+ if (schemaItem.type === "time" && !(value instanceof Time)) {
114
+ throw new Error(`Property ${key} must be a time`);
115
+ }
116
+ if (schemaItem.type === "time" && !isTime(value)) {
117
+ throw new Error(`Property ${key} must be a time`);
118
+ }
119
+ }
120
+ if (schemaItem.default && value === void 0) {
121
+ doc[key] = schemaItem.default();
122
+ }
123
+ if (schemaItem.unique && value !== void 0 && value !== null) {
124
+ const existing = await (0, import_core.invoke)("plugin:mongoose|get_by_id", {
125
+ collection: this.name,
126
+ id: value
127
+ });
128
+ if (existing) {
129
+ throw new Error(`Property ${key} must be unique`);
130
+ }
131
+ }
132
+ }
133
+ return doc;
134
+ }
135
+ async create(doc) {
136
+ const validatedDoc = await this.validate(doc);
137
+ return await (0, import_core.invoke)("plugin:mongoose|create", {
138
+ collection: this.name,
139
+ document: validatedDoc
140
+ });
141
+ }
142
+ };
143
+
144
+ // index.ts
145
+ async function connect(urlOrOptions, dbName) {
146
+ if (typeof urlOrOptions === "string") {
147
+ await (0, import_core2.invoke)("plugin:mongoose|connect", { url: urlOrOptions, dbName });
148
+ } else {
149
+ await (0, import_core2.invoke)("plugin:mongoose|connect", { url: urlOrOptions.url, dbName: urlOrOptions.dbName });
150
+ }
31
151
  }
32
152
  // Annotate the CommonJS export names for ESM import in node:
33
153
  0 && (module.exports = {
154
+ Model,
34
155
  connect
35
156
  });
package/dist/index.mjs CHANGED
@@ -1,10 +1,130 @@
1
1
  // index.ts
2
+ import { invoke as invoke2 } from "@tauri-apps/api/core";
3
+
4
+ // model/index.ts
2
5
  import { invoke } from "@tauri-apps/api/core";
3
- async function connect(url) {
4
- await invoke("plugin:mongoose|connect", {
5
- url
6
- });
6
+
7
+ // types/time.type.ts
8
+ var isTime = (obj) => {
9
+ return obj && typeof obj.hours === "number" && typeof obj.minutes === "number" && typeof obj.seconds === "number" && typeof obj.milliseconds === "number";
10
+ };
11
+ var Time = class _Time {
12
+ constructor(hours, minutes, seconds, milliseconds) {
13
+ this.hours = hours;
14
+ this.minutes = minutes;
15
+ this.seconds = seconds;
16
+ this.milliseconds = milliseconds;
17
+ }
18
+ toString(_12_hour = false, format = { hours: "2-digit", minutes: "2-digit" }) {
19
+ const pad = (num, size = 2) => num.toString().padStart(size, "0");
20
+ let h = this.hours;
21
+ let am_pm = "";
22
+ if (_12_hour) {
23
+ am_pm = h >= 12 ? " PM" : " AM";
24
+ h = h % 12 || 12;
25
+ }
26
+ let result = `${pad(h)}:${pad(this.minutes)}`;
27
+ if (format.seconds) {
28
+ result += `:${pad(this.seconds)}`;
29
+ }
30
+ if (format.milliseconds) {
31
+ result += `.${pad(this.milliseconds, 3)}`;
32
+ }
33
+ return result + am_pm;
34
+ }
35
+ static fromString(timeString) {
36
+ const [time, am_pm] = timeString.split(" ");
37
+ const [hours, minutes, seconds] = time.split(":").map(Number);
38
+ const milliseconds = 0;
39
+ return new _Time(hours, minutes, seconds, milliseconds);
40
+ }
41
+ getTime() {
42
+ return new Date(0, 0, 0, this.hours, this.minutes, this.seconds, this.milliseconds);
43
+ }
44
+ static fromDate(date) {
45
+ return new _Time(date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
46
+ }
47
+ };
48
+
49
+ // model/index.ts
50
+ var Model = class {
51
+ constructor(name, schema) {
52
+ this.name = name;
53
+ this.schema = schema;
54
+ }
55
+ async getById(id) {
56
+ const doc = await invoke("plugin:mongoose|get_by_id", {
57
+ collection: this.name,
58
+ id
59
+ });
60
+ return this.validate(doc);
61
+ }
62
+ async validate(doc) {
63
+ for (const key in this.schema) {
64
+ const schemaItem = this.schema[key];
65
+ const value = doc[key];
66
+ if (schemaItem.required && (value === void 0 || value === null || value === "")) {
67
+ throw new Error(`Property ${key} is required`);
68
+ }
69
+ if (value !== void 0 && value !== null) {
70
+ if (schemaItem.type === "string" && typeof value !== "string") {
71
+ throw new Error(`Property ${key} must be a string`);
72
+ }
73
+ if (schemaItem.type === "number" && typeof value !== "number") {
74
+ throw new Error(`Property ${key} must be a number`);
75
+ }
76
+ if (schemaItem.type === "boolean" && typeof value !== "boolean") {
77
+ throw new Error(`Property ${key} must be a boolean`);
78
+ }
79
+ if (schemaItem.type === "object" && typeof value !== "object") {
80
+ throw new Error(`Property ${key} must be an object`);
81
+ }
82
+ if (schemaItem.type === "array" && !Array.isArray(value)) {
83
+ throw new Error(`Property ${key} must be an array`);
84
+ }
85
+ if (schemaItem.type === "date" && !(value instanceof Date)) {
86
+ throw new Error(`Property ${key} must be a date`);
87
+ }
88
+ if (schemaItem.type === "time" && !(value instanceof Time)) {
89
+ throw new Error(`Property ${key} must be a time`);
90
+ }
91
+ if (schemaItem.type === "time" && !isTime(value)) {
92
+ throw new Error(`Property ${key} must be a time`);
93
+ }
94
+ }
95
+ if (schemaItem.default && value === void 0) {
96
+ doc[key] = schemaItem.default();
97
+ }
98
+ if (schemaItem.unique && value !== void 0 && value !== null) {
99
+ const existing = await invoke("plugin:mongoose|get_by_id", {
100
+ collection: this.name,
101
+ id: value
102
+ });
103
+ if (existing) {
104
+ throw new Error(`Property ${key} must be unique`);
105
+ }
106
+ }
107
+ }
108
+ return doc;
109
+ }
110
+ async create(doc) {
111
+ const validatedDoc = await this.validate(doc);
112
+ return await invoke("plugin:mongoose|create", {
113
+ collection: this.name,
114
+ document: validatedDoc
115
+ });
116
+ }
117
+ };
118
+
119
+ // index.ts
120
+ async function connect(urlOrOptions, dbName) {
121
+ if (typeof urlOrOptions === "string") {
122
+ await invoke2("plugin:mongoose|connect", { url: urlOrOptions, dbName });
123
+ } else {
124
+ await invoke2("plugin:mongoose|connect", { url: urlOrOptions.url, dbName: urlOrOptions.dbName });
125
+ }
7
126
  }
8
127
  export {
128
+ Model,
9
129
  connect
10
130
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tauri-plugin-mongoose",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Tauri plugin for MongoDB/Mongoose-like database operations",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",