Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace file #261

Open
strukturart opened this issue Mar 24, 2024 · 1 comment
Open

replace file #261

strukturart opened this issue Mar 24, 2024 · 1 comment

Comments

@strukturart
Copy link

I would like to load a JSON file from an external server and replace the one that already exists, because I mainly want to use sleekDB's search & filter. Is there the possibility to replace a file in sleekDB?

I tried that, but a new file with a new _id is always created?

 $databaseDirectory = $_SERVER['DOCUMENT_ROOT'] . "/data/test";

            $storeConfiguration = [
                "auto_cache" => true,
                'timeout' => false,
            ];
            $userStore = new \SleekDB\Store("customers", $databaseDirectory, $storeConfiguration);

            $newUsers = [
                [
                    "username" => "Lisa",
                    "age" => 17,
                    "address" => [
                        "street" => "up street",
                        "streetNumber" => 48,
                        "postalCode" => "1822",
                    ],
                ],
                [
                    "username" => "Bob",
                    "age" => 20,
                    "address" => [
                        "street" => "down street",
                        "streetNumber" => 12,
                        "postalCode" => "8174",
                    ],
                ],
            ];



            // Update all existing documents with the new data
            $userStore->updateOrInsert($newUsers);
@basteyy
Copy link

basteyy commented Jul 7, 2024

The first thought is, that you need to delete all the old users.

Perhaps I get you wrong. As long, you won't provide an _id, sleek will create new users. For updating, the _id is required. A workaround could be this:

$databaseDirectory = $_SERVER['DOCUMENT_ROOT'] . "/data/test";

$storeConfiguration = [
    "auto_cache" => true,
    'timeout' => false,
];
$userStore = new \SleekDB\Store("customers", $databaseDirectory, $storeConfiguration);

$newUsers = [
    [
        "username" => "Lisa",
        "age" => 17,
        "address" => [
            "street" => "up street",
            "streetNumber" => 48,
            "postalCode" => "1822",
        ],
    ],
    [
        "username" => "Bob",
        "age" => 20,
        "address" => [
            "street" => "down street",
            "streetNumber" => 12,
            "postalCode" => "8174",
        ],
    ],
];

$oldUsers = $userStore->findAll();

$updateOrInsertData = [];

foreach ($newUsers as $user) {
    // Check if there is already a user with that username
    if ($oUser = $userStore->findOneBy(['username', '=', $user['username']])) {
        // Already inside database, add _id to the new element
        $updateOrInsertData[] = array_merge($user, ['_id' => $oUser['_id']]);
    } else {
        // Not inside database, just add it
        $updateOrInsertData[] = $user;
    }
}

// Update all existing documents with the new data
$userStore->updateOrInsert($newUsers);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
  NODES
COMMUNITY 2
Project 5
USERS 15